trace.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 Nadia Yvette Chambers
  13. */
  14. #include <linux/ring_buffer.h>
  15. #include <generated/utsrelease.h>
  16. #include <linux/stacktrace.h>
  17. #include <linux/writeback.h>
  18. #include <linux/kallsyms.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/notifier.h>
  21. #include <linux/irqflags.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/linkage.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kprobes.h>
  28. #include <linux/ftrace.h>
  29. #include <linux/module.h>
  30. #include <linux/percpu.h>
  31. #include <linux/splice.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/string.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/slab.h>
  36. #include <linux/ctype.h>
  37. #include <linux/init.h>
  38. #include <linux/poll.h>
  39. #include <linux/nmi.h>
  40. #include <linux/fs.h>
  41. #include <linux/sched/rt.h>
  42. #include "trace.h"
  43. #include "trace_output.h"
  44. /*
  45. * On boot up, the ring buffer is set to the minimum size, so that
  46. * we do not waste memory on systems that are not using tracing.
  47. */
  48. bool ring_buffer_expanded;
  49. /*
  50. * We need to change this state when a selftest is running.
  51. * A selftest will lurk into the ring-buffer to count the
  52. * entries inserted during the selftest although some concurrent
  53. * insertions into the ring-buffer such as trace_printk could occurred
  54. * at the same time, giving false positive or negative results.
  55. */
  56. static bool __read_mostly tracing_selftest_running;
  57. /*
  58. * If a tracer is running, we do not want to run SELFTEST.
  59. */
  60. bool __read_mostly tracing_selftest_disabled;
  61. /* For tracers that don't implement custom flags */
  62. static struct tracer_opt dummy_tracer_opt[] = {
  63. { }
  64. };
  65. static struct tracer_flags dummy_tracer_flags = {
  66. .val = 0,
  67. .opts = dummy_tracer_opt
  68. };
  69. static int dummy_set_flag(u32 old_flags, u32 bit, int set)
  70. {
  71. return 0;
  72. }
  73. /*
  74. * To prevent the comm cache from being overwritten when no
  75. * tracing is active, only save the comm when a trace event
  76. * occurred.
  77. */
  78. static DEFINE_PER_CPU(bool, trace_cmdline_save);
  79. /*
  80. * Kill all tracing for good (never come back).
  81. * It is initialized to 1 but will turn to zero if the initialization
  82. * of the tracer is successful. But that is the only place that sets
  83. * this back to zero.
  84. */
  85. static int tracing_disabled = 1;
  86. DEFINE_PER_CPU(int, ftrace_cpu_disabled);
  87. cpumask_var_t __read_mostly tracing_buffer_mask;
  88. /*
  89. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  90. *
  91. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  92. * is set, then ftrace_dump is called. This will output the contents
  93. * of the ftrace buffers to the console. This is very useful for
  94. * capturing traces that lead to crashes and outputing it to a
  95. * serial console.
  96. *
  97. * It is default off, but you can enable it with either specifying
  98. * "ftrace_dump_on_oops" in the kernel command line, or setting
  99. * /proc/sys/kernel/ftrace_dump_on_oops
  100. * Set 1 if you want to dump buffers of all CPUs
  101. * Set 2 if you want to dump the buffer of the CPU that triggered oops
  102. */
  103. enum ftrace_dump_mode ftrace_dump_on_oops;
  104. static int tracing_set_tracer(const char *buf);
  105. #define MAX_TRACER_SIZE 100
  106. static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
  107. static char *default_bootup_tracer;
  108. static bool allocate_snapshot;
  109. static int __init set_cmdline_ftrace(char *str)
  110. {
  111. strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
  112. default_bootup_tracer = bootup_tracer_buf;
  113. /* We are using ftrace early, expand it */
  114. ring_buffer_expanded = true;
  115. return 1;
  116. }
  117. __setup("ftrace=", set_cmdline_ftrace);
  118. static int __init set_ftrace_dump_on_oops(char *str)
  119. {
  120. if (*str++ != '=' || !*str) {
  121. ftrace_dump_on_oops = DUMP_ALL;
  122. return 1;
  123. }
  124. if (!strcmp("orig_cpu", str)) {
  125. ftrace_dump_on_oops = DUMP_ORIG;
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  131. static int __init boot_alloc_snapshot(char *str)
  132. {
  133. allocate_snapshot = true;
  134. /* We also need the main ring buffer expanded */
  135. ring_buffer_expanded = true;
  136. return 1;
  137. }
  138. __setup("alloc_snapshot", boot_alloc_snapshot);
  139. static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
  140. static char *trace_boot_options __initdata;
  141. static int __init set_trace_boot_options(char *str)
  142. {
  143. strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
  144. trace_boot_options = trace_boot_options_buf;
  145. return 0;
  146. }
  147. __setup("trace_options=", set_trace_boot_options);
  148. unsigned long long ns2usecs(cycle_t nsec)
  149. {
  150. nsec += 500;
  151. do_div(nsec, 1000);
  152. return nsec;
  153. }
  154. /*
  155. * The global_trace is the descriptor that holds the tracing
  156. * buffers for the live tracing. For each CPU, it contains
  157. * a link list of pages that will store trace entries. The
  158. * page descriptor of the pages in the memory is used to hold
  159. * the link list by linking the lru item in the page descriptor
  160. * to each of the pages in the buffer per CPU.
  161. *
  162. * For each active CPU there is a data field that holds the
  163. * pages for the buffer for that CPU. Each CPU has the same number
  164. * of pages allocated for its buffer.
  165. */
  166. static struct trace_array global_trace;
  167. LIST_HEAD(ftrace_trace_arrays);
  168. int filter_current_check_discard(struct ring_buffer *buffer,
  169. struct ftrace_event_call *call, void *rec,
  170. struct ring_buffer_event *event)
  171. {
  172. return filter_check_discard(call, rec, buffer, event);
  173. }
  174. EXPORT_SYMBOL_GPL(filter_current_check_discard);
  175. cycle_t ftrace_now(int cpu)
  176. {
  177. u64 ts;
  178. /* Early boot up does not have a buffer yet */
  179. if (!global_trace.trace_buffer.buffer)
  180. return trace_clock_local();
  181. ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
  182. ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
  183. return ts;
  184. }
  185. int tracing_is_enabled(void)
  186. {
  187. return tracing_is_on();
  188. }
  189. /*
  190. * trace_buf_size is the size in bytes that is allocated
  191. * for a buffer. Note, the number of bytes is always rounded
  192. * to page size.
  193. *
  194. * This number is purposely set to a low number of 16384.
  195. * If the dump on oops happens, it will be much appreciated
  196. * to not have to wait for all that output. Anyway this can be
  197. * boot time and run time configurable.
  198. */
  199. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  200. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  201. /* trace_types holds a link list of available tracers. */
  202. static struct tracer *trace_types __read_mostly;
  203. /*
  204. * trace_types_lock is used to protect the trace_types list.
  205. */
  206. static DEFINE_MUTEX(trace_types_lock);
  207. /*
  208. * serialize the access of the ring buffer
  209. *
  210. * ring buffer serializes readers, but it is low level protection.
  211. * The validity of the events (which returns by ring_buffer_peek() ..etc)
  212. * are not protected by ring buffer.
  213. *
  214. * The content of events may become garbage if we allow other process consumes
  215. * these events concurrently:
  216. * A) the page of the consumed events may become a normal page
  217. * (not reader page) in ring buffer, and this page will be rewrited
  218. * by events producer.
  219. * B) The page of the consumed events may become a page for splice_read,
  220. * and this page will be returned to system.
  221. *
  222. * These primitives allow multi process access to different cpu ring buffer
  223. * concurrently.
  224. *
  225. * These primitives don't distinguish read-only and read-consume access.
  226. * Multi read-only access are also serialized.
  227. */
  228. #ifdef CONFIG_SMP
  229. static DECLARE_RWSEM(all_cpu_access_lock);
  230. static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
  231. static inline void trace_access_lock(int cpu)
  232. {
  233. if (cpu == RING_BUFFER_ALL_CPUS) {
  234. /* gain it for accessing the whole ring buffer. */
  235. down_write(&all_cpu_access_lock);
  236. } else {
  237. /* gain it for accessing a cpu ring buffer. */
  238. /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
  239. down_read(&all_cpu_access_lock);
  240. /* Secondly block other access to this @cpu ring buffer. */
  241. mutex_lock(&per_cpu(cpu_access_lock, cpu));
  242. }
  243. }
  244. static inline void trace_access_unlock(int cpu)
  245. {
  246. if (cpu == RING_BUFFER_ALL_CPUS) {
  247. up_write(&all_cpu_access_lock);
  248. } else {
  249. mutex_unlock(&per_cpu(cpu_access_lock, cpu));
  250. up_read(&all_cpu_access_lock);
  251. }
  252. }
  253. static inline void trace_access_lock_init(void)
  254. {
  255. int cpu;
  256. for_each_possible_cpu(cpu)
  257. mutex_init(&per_cpu(cpu_access_lock, cpu));
  258. }
  259. #else
  260. static DEFINE_MUTEX(access_lock);
  261. static inline void trace_access_lock(int cpu)
  262. {
  263. (void)cpu;
  264. mutex_lock(&access_lock);
  265. }
  266. static inline void trace_access_unlock(int cpu)
  267. {
  268. (void)cpu;
  269. mutex_unlock(&access_lock);
  270. }
  271. static inline void trace_access_lock_init(void)
  272. {
  273. }
  274. #endif
  275. /* trace_flags holds trace_options default values */
  276. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
  277. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
  278. TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
  279. TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
  280. /**
  281. * tracing_on - enable tracing buffers
  282. *
  283. * This function enables tracing buffers that may have been
  284. * disabled with tracing_off.
  285. */
  286. void tracing_on(void)
  287. {
  288. if (global_trace.trace_buffer.buffer)
  289. ring_buffer_record_on(global_trace.trace_buffer.buffer);
  290. /*
  291. * This flag is only looked at when buffers haven't been
  292. * allocated yet. We don't really care about the race
  293. * between setting this flag and actually turning
  294. * on the buffer.
  295. */
  296. global_trace.buffer_disabled = 0;
  297. }
  298. EXPORT_SYMBOL_GPL(tracing_on);
  299. /**
  300. * __trace_puts - write a constant string into the trace buffer.
  301. * @ip: The address of the caller
  302. * @str: The constant string to write
  303. * @size: The size of the string.
  304. */
  305. int __trace_puts(unsigned long ip, const char *str, int size)
  306. {
  307. struct ring_buffer_event *event;
  308. struct ring_buffer *buffer;
  309. struct print_entry *entry;
  310. unsigned long irq_flags;
  311. int alloc;
  312. alloc = sizeof(*entry) + size + 2; /* possible \n added */
  313. local_save_flags(irq_flags);
  314. buffer = global_trace.trace_buffer.buffer;
  315. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
  316. irq_flags, preempt_count());
  317. if (!event)
  318. return 0;
  319. entry = ring_buffer_event_data(event);
  320. entry->ip = ip;
  321. memcpy(&entry->buf, str, size);
  322. /* Add a newline if necessary */
  323. if (entry->buf[size - 1] != '\n') {
  324. entry->buf[size] = '\n';
  325. entry->buf[size + 1] = '\0';
  326. } else
  327. entry->buf[size] = '\0';
  328. __buffer_unlock_commit(buffer, event);
  329. return size;
  330. }
  331. EXPORT_SYMBOL_GPL(__trace_puts);
  332. /**
  333. * __trace_bputs - write the pointer to a constant string into trace buffer
  334. * @ip: The address of the caller
  335. * @str: The constant string to write to the buffer to
  336. */
  337. int __trace_bputs(unsigned long ip, const char *str)
  338. {
  339. struct ring_buffer_event *event;
  340. struct ring_buffer *buffer;
  341. struct bputs_entry *entry;
  342. unsigned long irq_flags;
  343. int size = sizeof(struct bputs_entry);
  344. local_save_flags(irq_flags);
  345. buffer = global_trace.trace_buffer.buffer;
  346. event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
  347. irq_flags, preempt_count());
  348. if (!event)
  349. return 0;
  350. entry = ring_buffer_event_data(event);
  351. entry->ip = ip;
  352. entry->str = str;
  353. __buffer_unlock_commit(buffer, event);
  354. return 1;
  355. }
  356. EXPORT_SYMBOL_GPL(__trace_bputs);
  357. #ifdef CONFIG_TRACER_SNAPSHOT
  358. /**
  359. * trace_snapshot - take a snapshot of the current buffer.
  360. *
  361. * This causes a swap between the snapshot buffer and the current live
  362. * tracing buffer. You can use this to take snapshots of the live
  363. * trace when some condition is triggered, but continue to trace.
  364. *
  365. * Note, make sure to allocate the snapshot with either
  366. * a tracing_snapshot_alloc(), or by doing it manually
  367. * with: echo 1 > /sys/kernel/debug/tracing/snapshot
  368. *
  369. * If the snapshot buffer is not allocated, it will stop tracing.
  370. * Basically making a permanent snapshot.
  371. */
  372. void tracing_snapshot(void)
  373. {
  374. struct trace_array *tr = &global_trace;
  375. struct tracer *tracer = tr->current_trace;
  376. unsigned long flags;
  377. if (in_nmi()) {
  378. internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
  379. internal_trace_puts("*** snapshot is being ignored ***\n");
  380. return;
  381. }
  382. if (!tr->allocated_snapshot) {
  383. internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
  384. internal_trace_puts("*** stopping trace here! ***\n");
  385. tracing_off();
  386. return;
  387. }
  388. /* Note, snapshot can not be used when the tracer uses it */
  389. if (tracer->use_max_tr) {
  390. internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
  391. internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
  392. return;
  393. }
  394. local_irq_save(flags);
  395. update_max_tr(tr, current, smp_processor_id());
  396. local_irq_restore(flags);
  397. }
  398. EXPORT_SYMBOL_GPL(tracing_snapshot);
  399. static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
  400. struct trace_buffer *size_buf, int cpu_id);
  401. static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
  402. static int alloc_snapshot(struct trace_array *tr)
  403. {
  404. int ret;
  405. if (!tr->allocated_snapshot) {
  406. /* allocate spare buffer */
  407. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  408. &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
  409. if (ret < 0)
  410. return ret;
  411. tr->allocated_snapshot = true;
  412. }
  413. return 0;
  414. }
  415. void free_snapshot(struct trace_array *tr)
  416. {
  417. /*
  418. * We don't free the ring buffer. instead, resize it because
  419. * The max_tr ring buffer has some state (e.g. ring->clock) and
  420. * we want preserve it.
  421. */
  422. ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
  423. set_buffer_entries(&tr->max_buffer, 1);
  424. tracing_reset_online_cpus(&tr->max_buffer);
  425. tr->allocated_snapshot = false;
  426. }
  427. /**
  428. * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
  429. *
  430. * This is similar to trace_snapshot(), but it will allocate the
  431. * snapshot buffer if it isn't already allocated. Use this only
  432. * where it is safe to sleep, as the allocation may sleep.
  433. *
  434. * This causes a swap between the snapshot buffer and the current live
  435. * tracing buffer. You can use this to take snapshots of the live
  436. * trace when some condition is triggered, but continue to trace.
  437. */
  438. void tracing_snapshot_alloc(void)
  439. {
  440. struct trace_array *tr = &global_trace;
  441. int ret;
  442. ret = alloc_snapshot(tr);
  443. if (WARN_ON(ret < 0))
  444. return;
  445. tracing_snapshot();
  446. }
  447. EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
  448. #else
  449. void tracing_snapshot(void)
  450. {
  451. WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
  452. }
  453. EXPORT_SYMBOL_GPL(tracing_snapshot);
  454. void tracing_snapshot_alloc(void)
  455. {
  456. /* Give warning */
  457. tracing_snapshot();
  458. }
  459. EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
  460. #endif /* CONFIG_TRACER_SNAPSHOT */
  461. /**
  462. * tracing_off - turn off tracing buffers
  463. *
  464. * This function stops the tracing buffers from recording data.
  465. * It does not disable any overhead the tracers themselves may
  466. * be causing. This function simply causes all recording to
  467. * the ring buffers to fail.
  468. */
  469. void tracing_off(void)
  470. {
  471. if (global_trace.trace_buffer.buffer)
  472. ring_buffer_record_off(global_trace.trace_buffer.buffer);
  473. /*
  474. * This flag is only looked at when buffers haven't been
  475. * allocated yet. We don't really care about the race
  476. * between setting this flag and actually turning
  477. * on the buffer.
  478. */
  479. global_trace.buffer_disabled = 1;
  480. }
  481. EXPORT_SYMBOL_GPL(tracing_off);
  482. /**
  483. * tracing_is_on - show state of ring buffers enabled
  484. */
  485. int tracing_is_on(void)
  486. {
  487. if (global_trace.trace_buffer.buffer)
  488. return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
  489. return !global_trace.buffer_disabled;
  490. }
  491. EXPORT_SYMBOL_GPL(tracing_is_on);
  492. static int __init set_buf_size(char *str)
  493. {
  494. unsigned long buf_size;
  495. if (!str)
  496. return 0;
  497. buf_size = memparse(str, &str);
  498. /* nr_entries can not be zero */
  499. if (buf_size == 0)
  500. return 0;
  501. trace_buf_size = buf_size;
  502. return 1;
  503. }
  504. __setup("trace_buf_size=", set_buf_size);
  505. static int __init set_tracing_thresh(char *str)
  506. {
  507. unsigned long threshold;
  508. int ret;
  509. if (!str)
  510. return 0;
  511. ret = kstrtoul(str, 0, &threshold);
  512. if (ret < 0)
  513. return 0;
  514. tracing_thresh = threshold * 1000;
  515. return 1;
  516. }
  517. __setup("tracing_thresh=", set_tracing_thresh);
  518. unsigned long nsecs_to_usecs(unsigned long nsecs)
  519. {
  520. return nsecs / 1000;
  521. }
  522. /* These must match the bit postions in trace_iterator_flags */
  523. static const char *trace_options[] = {
  524. "print-parent",
  525. "sym-offset",
  526. "sym-addr",
  527. "verbose",
  528. "raw",
  529. "hex",
  530. "bin",
  531. "block",
  532. "stacktrace",
  533. "trace_printk",
  534. "ftrace_preempt",
  535. "branch",
  536. "annotate",
  537. "userstacktrace",
  538. "sym-userobj",
  539. "printk-msg-only",
  540. "context-info",
  541. "latency-format",
  542. "sleep-time",
  543. "graph-time",
  544. "record-cmd",
  545. "overwrite",
  546. "disable_on_free",
  547. "irq-info",
  548. "markers",
  549. NULL
  550. };
  551. static struct {
  552. u64 (*func)(void);
  553. const char *name;
  554. int in_ns; /* is this clock in nanoseconds? */
  555. } trace_clocks[] = {
  556. { trace_clock_local, "local", 1 },
  557. { trace_clock_global, "global", 1 },
  558. { trace_clock_counter, "counter", 0 },
  559. ARCH_TRACE_CLOCKS
  560. };
  561. int trace_clock_id;
  562. /*
  563. * trace_parser_get_init - gets the buffer for trace parser
  564. */
  565. int trace_parser_get_init(struct trace_parser *parser, int size)
  566. {
  567. memset(parser, 0, sizeof(*parser));
  568. parser->buffer = kmalloc(size, GFP_KERNEL);
  569. if (!parser->buffer)
  570. return 1;
  571. parser->size = size;
  572. return 0;
  573. }
  574. /*
  575. * trace_parser_put - frees the buffer for trace parser
  576. */
  577. void trace_parser_put(struct trace_parser *parser)
  578. {
  579. kfree(parser->buffer);
  580. }
  581. /*
  582. * trace_get_user - reads the user input string separated by space
  583. * (matched by isspace(ch))
  584. *
  585. * For each string found the 'struct trace_parser' is updated,
  586. * and the function returns.
  587. *
  588. * Returns number of bytes read.
  589. *
  590. * See kernel/trace/trace.h for 'struct trace_parser' details.
  591. */
  592. int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  593. size_t cnt, loff_t *ppos)
  594. {
  595. char ch;
  596. size_t read = 0;
  597. ssize_t ret;
  598. if (!*ppos)
  599. trace_parser_clear(parser);
  600. ret = get_user(ch, ubuf++);
  601. if (ret)
  602. goto out;
  603. read++;
  604. cnt--;
  605. /*
  606. * The parser is not finished with the last write,
  607. * continue reading the user input without skipping spaces.
  608. */
  609. if (!parser->cont) {
  610. /* skip white space */
  611. while (cnt && isspace(ch)) {
  612. ret = get_user(ch, ubuf++);
  613. if (ret)
  614. goto out;
  615. read++;
  616. cnt--;
  617. }
  618. /* only spaces were written */
  619. if (isspace(ch)) {
  620. *ppos += read;
  621. ret = read;
  622. goto out;
  623. }
  624. parser->idx = 0;
  625. }
  626. /* read the non-space input */
  627. while (cnt && !isspace(ch)) {
  628. if (parser->idx < parser->size - 1)
  629. parser->buffer[parser->idx++] = ch;
  630. else {
  631. ret = -EINVAL;
  632. goto out;
  633. }
  634. ret = get_user(ch, ubuf++);
  635. if (ret)
  636. goto out;
  637. read++;
  638. cnt--;
  639. }
  640. /* We either got finished input or we have to wait for another call. */
  641. if (isspace(ch)) {
  642. parser->buffer[parser->idx] = 0;
  643. parser->cont = false;
  644. } else {
  645. parser->cont = true;
  646. parser->buffer[parser->idx++] = ch;
  647. }
  648. *ppos += read;
  649. ret = read;
  650. out:
  651. return ret;
  652. }
  653. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  654. {
  655. int len;
  656. int ret;
  657. if (!cnt)
  658. return 0;
  659. if (s->len <= s->readpos)
  660. return -EBUSY;
  661. len = s->len - s->readpos;
  662. if (cnt > len)
  663. cnt = len;
  664. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  665. if (ret == cnt)
  666. return -EFAULT;
  667. cnt -= ret;
  668. s->readpos += cnt;
  669. return cnt;
  670. }
  671. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  672. {
  673. int len;
  674. if (s->len <= s->readpos)
  675. return -EBUSY;
  676. len = s->len - s->readpos;
  677. if (cnt > len)
  678. cnt = len;
  679. memcpy(buf, s->buffer + s->readpos, cnt);
  680. s->readpos += cnt;
  681. return cnt;
  682. }
  683. /*
  684. * ftrace_max_lock is used to protect the swapping of buffers
  685. * when taking a max snapshot. The buffers themselves are
  686. * protected by per_cpu spinlocks. But the action of the swap
  687. * needs its own lock.
  688. *
  689. * This is defined as a arch_spinlock_t in order to help
  690. * with performance when lockdep debugging is enabled.
  691. *
  692. * It is also used in other places outside the update_max_tr
  693. * so it needs to be defined outside of the
  694. * CONFIG_TRACER_MAX_TRACE.
  695. */
  696. static arch_spinlock_t ftrace_max_lock =
  697. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  698. unsigned long __read_mostly tracing_thresh;
  699. #ifdef CONFIG_TRACER_MAX_TRACE
  700. unsigned long __read_mostly tracing_max_latency;
  701. /*
  702. * Copy the new maximum trace into the separate maximum-trace
  703. * structure. (this way the maximum trace is permanently saved,
  704. * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
  705. */
  706. static void
  707. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  708. {
  709. struct trace_buffer *trace_buf = &tr->trace_buffer;
  710. struct trace_buffer *max_buf = &tr->max_buffer;
  711. struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
  712. struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
  713. max_buf->cpu = cpu;
  714. max_buf->time_start = data->preempt_timestamp;
  715. max_data->saved_latency = tracing_max_latency;
  716. max_data->critical_start = data->critical_start;
  717. max_data->critical_end = data->critical_end;
  718. memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
  719. max_data->pid = tsk->pid;
  720. max_data->uid = task_uid(tsk);
  721. max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  722. max_data->policy = tsk->policy;
  723. max_data->rt_priority = tsk->rt_priority;
  724. /* record this tasks comm */
  725. tracing_record_cmdline(tsk);
  726. }
  727. /**
  728. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  729. * @tr: tracer
  730. * @tsk: the task with the latency
  731. * @cpu: The cpu that initiated the trace.
  732. *
  733. * Flip the buffers between the @tr and the max_tr and record information
  734. * about which task was the cause of this latency.
  735. */
  736. void
  737. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  738. {
  739. struct ring_buffer *buf;
  740. if (tr->stop_count)
  741. return;
  742. WARN_ON_ONCE(!irqs_disabled());
  743. if (!tr->allocated_snapshot) {
  744. /* Only the nop tracer should hit this when disabling */
  745. WARN_ON_ONCE(tr->current_trace != &nop_trace);
  746. return;
  747. }
  748. arch_spin_lock(&ftrace_max_lock);
  749. buf = tr->trace_buffer.buffer;
  750. tr->trace_buffer.buffer = tr->max_buffer.buffer;
  751. tr->max_buffer.buffer = buf;
  752. __update_max_tr(tr, tsk, cpu);
  753. arch_spin_unlock(&ftrace_max_lock);
  754. }
  755. /**
  756. * update_max_tr_single - only copy one trace over, and reset the rest
  757. * @tr - tracer
  758. * @tsk - task with the latency
  759. * @cpu - the cpu of the buffer to copy.
  760. *
  761. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  762. */
  763. void
  764. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  765. {
  766. int ret;
  767. if (tr->stop_count)
  768. return;
  769. WARN_ON_ONCE(!irqs_disabled());
  770. if (WARN_ON_ONCE(!tr->allocated_snapshot))
  771. return;
  772. arch_spin_lock(&ftrace_max_lock);
  773. ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
  774. if (ret == -EBUSY) {
  775. /*
  776. * We failed to swap the buffer due to a commit taking
  777. * place on this CPU. We fail to record, but we reset
  778. * the max trace buffer (no one writes directly to it)
  779. * and flag that it failed.
  780. */
  781. trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
  782. "Failed to swap buffers due to commit in progress\n");
  783. }
  784. WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
  785. __update_max_tr(tr, tsk, cpu);
  786. arch_spin_unlock(&ftrace_max_lock);
  787. }
  788. #endif /* CONFIG_TRACER_MAX_TRACE */
  789. static void default_wait_pipe(struct trace_iterator *iter)
  790. {
  791. /* Iterators are static, they should be filled or empty */
  792. if (trace_buffer_iter(iter, iter->cpu_file))
  793. return;
  794. ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
  795. }
  796. #ifdef CONFIG_FTRACE_STARTUP_TEST
  797. static int run_tracer_selftest(struct tracer *type)
  798. {
  799. struct trace_array *tr = &global_trace;
  800. struct tracer *saved_tracer = tr->current_trace;
  801. int ret;
  802. if (!type->selftest || tracing_selftest_disabled)
  803. return 0;
  804. /*
  805. * Run a selftest on this tracer.
  806. * Here we reset the trace buffer, and set the current
  807. * tracer to be this tracer. The tracer can then run some
  808. * internal tracing to verify that everything is in order.
  809. * If we fail, we do not register this tracer.
  810. */
  811. tracing_reset_online_cpus(&tr->trace_buffer);
  812. tr->current_trace = type;
  813. #ifdef CONFIG_TRACER_MAX_TRACE
  814. if (type->use_max_tr) {
  815. /* If we expanded the buffers, make sure the max is expanded too */
  816. if (ring_buffer_expanded)
  817. ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
  818. RING_BUFFER_ALL_CPUS);
  819. tr->allocated_snapshot = true;
  820. }
  821. #endif
  822. /* the test is responsible for initializing and enabling */
  823. pr_info("Testing tracer %s: ", type->name);
  824. ret = type->selftest(type, tr);
  825. /* the test is responsible for resetting too */
  826. tr->current_trace = saved_tracer;
  827. if (ret) {
  828. printk(KERN_CONT "FAILED!\n");
  829. /* Add the warning after printing 'FAILED' */
  830. WARN_ON(1);
  831. return -1;
  832. }
  833. /* Only reset on passing, to avoid touching corrupted buffers */
  834. tracing_reset_online_cpus(&tr->trace_buffer);
  835. #ifdef CONFIG_TRACER_MAX_TRACE
  836. if (type->use_max_tr) {
  837. tr->allocated_snapshot = false;
  838. /* Shrink the max buffer again */
  839. if (ring_buffer_expanded)
  840. ring_buffer_resize(tr->max_buffer.buffer, 1,
  841. RING_BUFFER_ALL_CPUS);
  842. }
  843. #endif
  844. printk(KERN_CONT "PASSED\n");
  845. return 0;
  846. }
  847. #else
  848. static inline int run_tracer_selftest(struct tracer *type)
  849. {
  850. return 0;
  851. }
  852. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  853. /**
  854. * register_tracer - register a tracer with the ftrace system.
  855. * @type - the plugin for the tracer
  856. *
  857. * Register a new plugin tracer.
  858. */
  859. int register_tracer(struct tracer *type)
  860. {
  861. struct tracer *t;
  862. int ret = 0;
  863. if (!type->name) {
  864. pr_info("Tracer must have a name\n");
  865. return -1;
  866. }
  867. if (strlen(type->name) >= MAX_TRACER_SIZE) {
  868. pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
  869. return -1;
  870. }
  871. mutex_lock(&trace_types_lock);
  872. tracing_selftest_running = true;
  873. for (t = trace_types; t; t = t->next) {
  874. if (strcmp(type->name, t->name) == 0) {
  875. /* already found */
  876. pr_info("Tracer %s already registered\n",
  877. type->name);
  878. ret = -1;
  879. goto out;
  880. }
  881. }
  882. if (!type->set_flag)
  883. type->set_flag = &dummy_set_flag;
  884. if (!type->flags)
  885. type->flags = &dummy_tracer_flags;
  886. else
  887. if (!type->flags->opts)
  888. type->flags->opts = dummy_tracer_opt;
  889. if (!type->wait_pipe)
  890. type->wait_pipe = default_wait_pipe;
  891. ret = run_tracer_selftest(type);
  892. if (ret < 0)
  893. goto out;
  894. type->next = trace_types;
  895. trace_types = type;
  896. out:
  897. tracing_selftest_running = false;
  898. mutex_unlock(&trace_types_lock);
  899. if (ret || !default_bootup_tracer)
  900. goto out_unlock;
  901. if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
  902. goto out_unlock;
  903. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  904. /* Do we want this tracer to start on bootup? */
  905. tracing_set_tracer(type->name);
  906. default_bootup_tracer = NULL;
  907. /* disable other selftests, since this will break it. */
  908. tracing_selftest_disabled = true;
  909. #ifdef CONFIG_FTRACE_STARTUP_TEST
  910. printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
  911. type->name);
  912. #endif
  913. out_unlock:
  914. return ret;
  915. }
  916. void tracing_reset(struct trace_buffer *buf, int cpu)
  917. {
  918. struct ring_buffer *buffer = buf->buffer;
  919. if (!buffer)
  920. return;
  921. ring_buffer_record_disable(buffer);
  922. /* Make sure all commits have finished */
  923. synchronize_sched();
  924. ring_buffer_reset_cpu(buffer, cpu);
  925. ring_buffer_record_enable(buffer);
  926. }
  927. void tracing_reset_online_cpus(struct trace_buffer *buf)
  928. {
  929. struct ring_buffer *buffer = buf->buffer;
  930. int cpu;
  931. if (!buffer)
  932. return;
  933. ring_buffer_record_disable(buffer);
  934. /* Make sure all commits have finished */
  935. synchronize_sched();
  936. buf->time_start = ftrace_now(buf->cpu);
  937. for_each_online_cpu(cpu)
  938. ring_buffer_reset_cpu(buffer, cpu);
  939. ring_buffer_record_enable(buffer);
  940. }
  941. void tracing_reset_current(int cpu)
  942. {
  943. tracing_reset(&global_trace.trace_buffer, cpu);
  944. }
  945. void tracing_reset_all_online_cpus(void)
  946. {
  947. struct trace_array *tr;
  948. mutex_lock(&trace_types_lock);
  949. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  950. tracing_reset_online_cpus(&tr->trace_buffer);
  951. #ifdef CONFIG_TRACER_MAX_TRACE
  952. tracing_reset_online_cpus(&tr->max_buffer);
  953. #endif
  954. }
  955. mutex_unlock(&trace_types_lock);
  956. }
  957. #define SAVED_CMDLINES 128
  958. #define NO_CMDLINE_MAP UINT_MAX
  959. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  960. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  961. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  962. static int cmdline_idx;
  963. static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  964. /* temporary disable recording */
  965. static atomic_t trace_record_cmdline_disabled __read_mostly;
  966. static void trace_init_cmdlines(void)
  967. {
  968. memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
  969. memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
  970. cmdline_idx = 0;
  971. }
  972. int is_tracing_stopped(void)
  973. {
  974. return global_trace.stop_count;
  975. }
  976. /**
  977. * ftrace_off_permanent - disable all ftrace code permanently
  978. *
  979. * This should only be called when a serious anomally has
  980. * been detected. This will turn off the function tracing,
  981. * ring buffers, and other tracing utilites. It takes no
  982. * locks and can be called from any context.
  983. */
  984. void ftrace_off_permanent(void)
  985. {
  986. tracing_disabled = 1;
  987. ftrace_stop();
  988. tracing_off_permanent();
  989. }
  990. /**
  991. * tracing_start - quick start of the tracer
  992. *
  993. * If tracing is enabled but was stopped by tracing_stop,
  994. * this will start the tracer back up.
  995. */
  996. void tracing_start(void)
  997. {
  998. struct ring_buffer *buffer;
  999. unsigned long flags;
  1000. if (tracing_disabled)
  1001. return;
  1002. raw_spin_lock_irqsave(&global_trace.start_lock, flags);
  1003. if (--global_trace.stop_count) {
  1004. if (global_trace.stop_count < 0) {
  1005. /* Someone screwed up their debugging */
  1006. WARN_ON_ONCE(1);
  1007. global_trace.stop_count = 0;
  1008. }
  1009. goto out;
  1010. }
  1011. /* Prevent the buffers from switching */
  1012. arch_spin_lock(&ftrace_max_lock);
  1013. buffer = global_trace.trace_buffer.buffer;
  1014. if (buffer)
  1015. ring_buffer_record_enable(buffer);
  1016. #ifdef CONFIG_TRACER_MAX_TRACE
  1017. buffer = global_trace.max_buffer.buffer;
  1018. if (buffer)
  1019. ring_buffer_record_enable(buffer);
  1020. #endif
  1021. arch_spin_unlock(&ftrace_max_lock);
  1022. ftrace_start();
  1023. out:
  1024. raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
  1025. }
  1026. static void tracing_start_tr(struct trace_array *tr)
  1027. {
  1028. struct ring_buffer *buffer;
  1029. unsigned long flags;
  1030. if (tracing_disabled)
  1031. return;
  1032. /* If global, we need to also start the max tracer */
  1033. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  1034. return tracing_start();
  1035. raw_spin_lock_irqsave(&tr->start_lock, flags);
  1036. if (--tr->stop_count) {
  1037. if (tr->stop_count < 0) {
  1038. /* Someone screwed up their debugging */
  1039. WARN_ON_ONCE(1);
  1040. tr->stop_count = 0;
  1041. }
  1042. goto out;
  1043. }
  1044. buffer = tr->trace_buffer.buffer;
  1045. if (buffer)
  1046. ring_buffer_record_enable(buffer);
  1047. out:
  1048. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  1049. }
  1050. /**
  1051. * tracing_stop - quick stop of the tracer
  1052. *
  1053. * Light weight way to stop tracing. Use in conjunction with
  1054. * tracing_start.
  1055. */
  1056. void tracing_stop(void)
  1057. {
  1058. struct ring_buffer *buffer;
  1059. unsigned long flags;
  1060. ftrace_stop();
  1061. raw_spin_lock_irqsave(&global_trace.start_lock, flags);
  1062. if (global_trace.stop_count++)
  1063. goto out;
  1064. /* Prevent the buffers from switching */
  1065. arch_spin_lock(&ftrace_max_lock);
  1066. buffer = global_trace.trace_buffer.buffer;
  1067. if (buffer)
  1068. ring_buffer_record_disable(buffer);
  1069. #ifdef CONFIG_TRACER_MAX_TRACE
  1070. buffer = global_trace.max_buffer.buffer;
  1071. if (buffer)
  1072. ring_buffer_record_disable(buffer);
  1073. #endif
  1074. arch_spin_unlock(&ftrace_max_lock);
  1075. out:
  1076. raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
  1077. }
  1078. static void tracing_stop_tr(struct trace_array *tr)
  1079. {
  1080. struct ring_buffer *buffer;
  1081. unsigned long flags;
  1082. /* If global, we need to also stop the max tracer */
  1083. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  1084. return tracing_stop();
  1085. raw_spin_lock_irqsave(&tr->start_lock, flags);
  1086. if (tr->stop_count++)
  1087. goto out;
  1088. buffer = tr->trace_buffer.buffer;
  1089. if (buffer)
  1090. ring_buffer_record_disable(buffer);
  1091. out:
  1092. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  1093. }
  1094. void trace_stop_cmdline_recording(void);
  1095. static void trace_save_cmdline(struct task_struct *tsk)
  1096. {
  1097. unsigned pid, idx;
  1098. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  1099. return;
  1100. /*
  1101. * It's not the end of the world if we don't get
  1102. * the lock, but we also don't want to spin
  1103. * nor do we want to disable interrupts,
  1104. * so if we miss here, then better luck next time.
  1105. */
  1106. if (!arch_spin_trylock(&trace_cmdline_lock))
  1107. return;
  1108. idx = map_pid_to_cmdline[tsk->pid];
  1109. if (idx == NO_CMDLINE_MAP) {
  1110. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  1111. /*
  1112. * Check whether the cmdline buffer at idx has a pid
  1113. * mapped. We are going to overwrite that entry so we
  1114. * need to clear the map_pid_to_cmdline. Otherwise we
  1115. * would read the new comm for the old pid.
  1116. */
  1117. pid = map_cmdline_to_pid[idx];
  1118. if (pid != NO_CMDLINE_MAP)
  1119. map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
  1120. map_cmdline_to_pid[idx] = tsk->pid;
  1121. map_pid_to_cmdline[tsk->pid] = idx;
  1122. cmdline_idx = idx;
  1123. }
  1124. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  1125. arch_spin_unlock(&trace_cmdline_lock);
  1126. }
  1127. void trace_find_cmdline(int pid, char comm[])
  1128. {
  1129. unsigned map;
  1130. if (!pid) {
  1131. strcpy(comm, "<idle>");
  1132. return;
  1133. }
  1134. if (WARN_ON_ONCE(pid < 0)) {
  1135. strcpy(comm, "<XXX>");
  1136. return;
  1137. }
  1138. if (pid > PID_MAX_DEFAULT) {
  1139. strcpy(comm, "<...>");
  1140. return;
  1141. }
  1142. preempt_disable();
  1143. arch_spin_lock(&trace_cmdline_lock);
  1144. map = map_pid_to_cmdline[pid];
  1145. if (map != NO_CMDLINE_MAP)
  1146. strcpy(comm, saved_cmdlines[map]);
  1147. else
  1148. strcpy(comm, "<...>");
  1149. arch_spin_unlock(&trace_cmdline_lock);
  1150. preempt_enable();
  1151. }
  1152. void tracing_record_cmdline(struct task_struct *tsk)
  1153. {
  1154. if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
  1155. return;
  1156. if (!__this_cpu_read(trace_cmdline_save))
  1157. return;
  1158. __this_cpu_write(trace_cmdline_save, false);
  1159. trace_save_cmdline(tsk);
  1160. }
  1161. void
  1162. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  1163. int pc)
  1164. {
  1165. struct task_struct *tsk = current;
  1166. entry->preempt_count = pc & 0xff;
  1167. entry->pid = (tsk) ? tsk->pid : 0;
  1168. entry->flags =
  1169. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  1170. (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  1171. #else
  1172. TRACE_FLAG_IRQS_NOSUPPORT |
  1173. #endif
  1174. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  1175. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  1176. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  1177. }
  1178. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  1179. struct ring_buffer_event *
  1180. trace_buffer_lock_reserve(struct ring_buffer *buffer,
  1181. int type,
  1182. unsigned long len,
  1183. unsigned long flags, int pc)
  1184. {
  1185. struct ring_buffer_event *event;
  1186. event = ring_buffer_lock_reserve(buffer, len);
  1187. if (event != NULL) {
  1188. struct trace_entry *ent = ring_buffer_event_data(event);
  1189. tracing_generic_entry_update(ent, flags, pc);
  1190. ent->type = type;
  1191. }
  1192. return event;
  1193. }
  1194. void
  1195. __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
  1196. {
  1197. __this_cpu_write(trace_cmdline_save, true);
  1198. ring_buffer_unlock_commit(buffer, event);
  1199. }
  1200. static inline void
  1201. __trace_buffer_unlock_commit(struct ring_buffer *buffer,
  1202. struct ring_buffer_event *event,
  1203. unsigned long flags, int pc)
  1204. {
  1205. __buffer_unlock_commit(buffer, event);
  1206. ftrace_trace_stack(buffer, flags, 6, pc);
  1207. ftrace_trace_userstack(buffer, flags, pc);
  1208. }
  1209. void trace_buffer_unlock_commit(struct ring_buffer *buffer,
  1210. struct ring_buffer_event *event,
  1211. unsigned long flags, int pc)
  1212. {
  1213. __trace_buffer_unlock_commit(buffer, event, flags, pc);
  1214. }
  1215. EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
  1216. struct ring_buffer_event *
  1217. trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
  1218. struct ftrace_event_file *ftrace_file,
  1219. int type, unsigned long len,
  1220. unsigned long flags, int pc)
  1221. {
  1222. *current_rb = ftrace_file->tr->trace_buffer.buffer;
  1223. return trace_buffer_lock_reserve(*current_rb,
  1224. type, len, flags, pc);
  1225. }
  1226. EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
  1227. struct ring_buffer_event *
  1228. trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
  1229. int type, unsigned long len,
  1230. unsigned long flags, int pc)
  1231. {
  1232. *current_rb = global_trace.trace_buffer.buffer;
  1233. return trace_buffer_lock_reserve(*current_rb,
  1234. type, len, flags, pc);
  1235. }
  1236. EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
  1237. void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
  1238. struct ring_buffer_event *event,
  1239. unsigned long flags, int pc)
  1240. {
  1241. __trace_buffer_unlock_commit(buffer, event, flags, pc);
  1242. }
  1243. EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
  1244. void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
  1245. struct ring_buffer_event *event,
  1246. unsigned long flags, int pc,
  1247. struct pt_regs *regs)
  1248. {
  1249. __buffer_unlock_commit(buffer, event);
  1250. ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
  1251. ftrace_trace_userstack(buffer, flags, pc);
  1252. }
  1253. EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
  1254. void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
  1255. struct ring_buffer_event *event)
  1256. {
  1257. ring_buffer_discard_commit(buffer, event);
  1258. }
  1259. EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
  1260. void
  1261. trace_function(struct trace_array *tr,
  1262. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  1263. int pc)
  1264. {
  1265. struct ftrace_event_call *call = &event_function;
  1266. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  1267. struct ring_buffer_event *event;
  1268. struct ftrace_entry *entry;
  1269. /* If we are reading the ring buffer, don't trace */
  1270. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  1271. return;
  1272. event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
  1273. flags, pc);
  1274. if (!event)
  1275. return;
  1276. entry = ring_buffer_event_data(event);
  1277. entry->ip = ip;
  1278. entry->parent_ip = parent_ip;
  1279. if (!filter_check_discard(call, entry, buffer, event))
  1280. __buffer_unlock_commit(buffer, event);
  1281. }
  1282. void
  1283. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  1284. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  1285. int pc)
  1286. {
  1287. if (likely(!atomic_read(&data->disabled)))
  1288. trace_function(tr, ip, parent_ip, flags, pc);
  1289. }
  1290. #ifdef CONFIG_STACKTRACE
  1291. #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
  1292. struct ftrace_stack {
  1293. unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
  1294. };
  1295. static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
  1296. static DEFINE_PER_CPU(int, ftrace_stack_reserve);
  1297. static void __ftrace_trace_stack(struct ring_buffer *buffer,
  1298. unsigned long flags,
  1299. int skip, int pc, struct pt_regs *regs)
  1300. {
  1301. struct ftrace_event_call *call = &event_kernel_stack;
  1302. struct ring_buffer_event *event;
  1303. struct stack_entry *entry;
  1304. struct stack_trace trace;
  1305. int use_stack;
  1306. int size = FTRACE_STACK_ENTRIES;
  1307. trace.nr_entries = 0;
  1308. trace.skip = skip;
  1309. /*
  1310. * Since events can happen in NMIs there's no safe way to
  1311. * use the per cpu ftrace_stacks. We reserve it and if an interrupt
  1312. * or NMI comes in, it will just have to use the default
  1313. * FTRACE_STACK_SIZE.
  1314. */
  1315. preempt_disable_notrace();
  1316. use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
  1317. /*
  1318. * We don't need any atomic variables, just a barrier.
  1319. * If an interrupt comes in, we don't care, because it would
  1320. * have exited and put the counter back to what we want.
  1321. * We just need a barrier to keep gcc from moving things
  1322. * around.
  1323. */
  1324. barrier();
  1325. if (use_stack == 1) {
  1326. trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
  1327. trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
  1328. if (regs)
  1329. save_stack_trace_regs(regs, &trace);
  1330. else
  1331. save_stack_trace(&trace);
  1332. if (trace.nr_entries > size)
  1333. size = trace.nr_entries;
  1334. } else
  1335. /* From now on, use_stack is a boolean */
  1336. use_stack = 0;
  1337. size *= sizeof(unsigned long);
  1338. event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
  1339. sizeof(*entry) + size, flags, pc);
  1340. if (!event)
  1341. goto out;
  1342. entry = ring_buffer_event_data(event);
  1343. memset(&entry->caller, 0, size);
  1344. if (use_stack)
  1345. memcpy(&entry->caller, trace.entries,
  1346. trace.nr_entries * sizeof(unsigned long));
  1347. else {
  1348. trace.max_entries = FTRACE_STACK_ENTRIES;
  1349. trace.entries = entry->caller;
  1350. if (regs)
  1351. save_stack_trace_regs(regs, &trace);
  1352. else
  1353. save_stack_trace(&trace);
  1354. }
  1355. entry->size = trace.nr_entries;
  1356. if (!filter_check_discard(call, entry, buffer, event))
  1357. __buffer_unlock_commit(buffer, event);
  1358. out:
  1359. /* Again, don't let gcc optimize things here */
  1360. barrier();
  1361. __this_cpu_dec(ftrace_stack_reserve);
  1362. preempt_enable_notrace();
  1363. }
  1364. void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
  1365. int skip, int pc, struct pt_regs *regs)
  1366. {
  1367. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  1368. return;
  1369. __ftrace_trace_stack(buffer, flags, skip, pc, regs);
  1370. }
  1371. void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
  1372. int skip, int pc)
  1373. {
  1374. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  1375. return;
  1376. __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
  1377. }
  1378. void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
  1379. int pc)
  1380. {
  1381. __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
  1382. }
  1383. /**
  1384. * trace_dump_stack - record a stack back trace in the trace buffer
  1385. * @skip: Number of functions to skip (helper handlers)
  1386. */
  1387. void trace_dump_stack(int skip)
  1388. {
  1389. unsigned long flags;
  1390. if (tracing_disabled || tracing_selftest_running)
  1391. return;
  1392. local_save_flags(flags);
  1393. /*
  1394. * Skip 3 more, seems to get us at the caller of
  1395. * this function.
  1396. */
  1397. skip += 3;
  1398. __ftrace_trace_stack(global_trace.trace_buffer.buffer,
  1399. flags, skip, preempt_count(), NULL);
  1400. }
  1401. static DEFINE_PER_CPU(int, user_stack_count);
  1402. void
  1403. ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
  1404. {
  1405. struct ftrace_event_call *call = &event_user_stack;
  1406. struct ring_buffer_event *event;
  1407. struct userstack_entry *entry;
  1408. struct stack_trace trace;
  1409. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  1410. return;
  1411. /*
  1412. * NMIs can not handle page faults, even with fix ups.
  1413. * The save user stack can (and often does) fault.
  1414. */
  1415. if (unlikely(in_nmi()))
  1416. return;
  1417. /*
  1418. * prevent recursion, since the user stack tracing may
  1419. * trigger other kernel events.
  1420. */
  1421. preempt_disable();
  1422. if (__this_cpu_read(user_stack_count))
  1423. goto out;
  1424. __this_cpu_inc(user_stack_count);
  1425. event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
  1426. sizeof(*entry), flags, pc);
  1427. if (!event)
  1428. goto out_drop_count;
  1429. entry = ring_buffer_event_data(event);
  1430. entry->tgid = current->tgid;
  1431. memset(&entry->caller, 0, sizeof(entry->caller));
  1432. trace.nr_entries = 0;
  1433. trace.max_entries = FTRACE_STACK_ENTRIES;
  1434. trace.skip = 0;
  1435. trace.entries = entry->caller;
  1436. save_stack_trace_user(&trace);
  1437. if (!filter_check_discard(call, entry, buffer, event))
  1438. __buffer_unlock_commit(buffer, event);
  1439. out_drop_count:
  1440. __this_cpu_dec(user_stack_count);
  1441. out:
  1442. preempt_enable();
  1443. }
  1444. #ifdef UNUSED
  1445. static void __trace_userstack(struct trace_array *tr, unsigned long flags)
  1446. {
  1447. ftrace_trace_userstack(tr, flags, preempt_count());
  1448. }
  1449. #endif /* UNUSED */
  1450. #endif /* CONFIG_STACKTRACE */
  1451. /* created for use with alloc_percpu */
  1452. struct trace_buffer_struct {
  1453. char buffer[TRACE_BUF_SIZE];
  1454. };
  1455. static struct trace_buffer_struct *trace_percpu_buffer;
  1456. static struct trace_buffer_struct *trace_percpu_sirq_buffer;
  1457. static struct trace_buffer_struct *trace_percpu_irq_buffer;
  1458. static struct trace_buffer_struct *trace_percpu_nmi_buffer;
  1459. /*
  1460. * The buffer used is dependent on the context. There is a per cpu
  1461. * buffer for normal context, softirq contex, hard irq context and
  1462. * for NMI context. Thise allows for lockless recording.
  1463. *
  1464. * Note, if the buffers failed to be allocated, then this returns NULL
  1465. */
  1466. static char *get_trace_buf(void)
  1467. {
  1468. struct trace_buffer_struct *percpu_buffer;
  1469. /*
  1470. * If we have allocated per cpu buffers, then we do not
  1471. * need to do any locking.
  1472. */
  1473. if (in_nmi())
  1474. percpu_buffer = trace_percpu_nmi_buffer;
  1475. else if (in_irq())
  1476. percpu_buffer = trace_percpu_irq_buffer;
  1477. else if (in_softirq())
  1478. percpu_buffer = trace_percpu_sirq_buffer;
  1479. else
  1480. percpu_buffer = trace_percpu_buffer;
  1481. if (!percpu_buffer)
  1482. return NULL;
  1483. return this_cpu_ptr(&percpu_buffer->buffer[0]);
  1484. }
  1485. static int alloc_percpu_trace_buffer(void)
  1486. {
  1487. struct trace_buffer_struct *buffers;
  1488. struct trace_buffer_struct *sirq_buffers;
  1489. struct trace_buffer_struct *irq_buffers;
  1490. struct trace_buffer_struct *nmi_buffers;
  1491. buffers = alloc_percpu(struct trace_buffer_struct);
  1492. if (!buffers)
  1493. goto err_warn;
  1494. sirq_buffers = alloc_percpu(struct trace_buffer_struct);
  1495. if (!sirq_buffers)
  1496. goto err_sirq;
  1497. irq_buffers = alloc_percpu(struct trace_buffer_struct);
  1498. if (!irq_buffers)
  1499. goto err_irq;
  1500. nmi_buffers = alloc_percpu(struct trace_buffer_struct);
  1501. if (!nmi_buffers)
  1502. goto err_nmi;
  1503. trace_percpu_buffer = buffers;
  1504. trace_percpu_sirq_buffer = sirq_buffers;
  1505. trace_percpu_irq_buffer = irq_buffers;
  1506. trace_percpu_nmi_buffer = nmi_buffers;
  1507. return 0;
  1508. err_nmi:
  1509. free_percpu(irq_buffers);
  1510. err_irq:
  1511. free_percpu(sirq_buffers);
  1512. err_sirq:
  1513. free_percpu(buffers);
  1514. err_warn:
  1515. WARN(1, "Could not allocate percpu trace_printk buffer");
  1516. return -ENOMEM;
  1517. }
  1518. static int buffers_allocated;
  1519. void trace_printk_init_buffers(void)
  1520. {
  1521. if (buffers_allocated)
  1522. return;
  1523. if (alloc_percpu_trace_buffer())
  1524. return;
  1525. pr_info("ftrace: Allocated trace_printk buffers\n");
  1526. /* Expand the buffers to set size */
  1527. tracing_update_buffers();
  1528. buffers_allocated = 1;
  1529. /*
  1530. * trace_printk_init_buffers() can be called by modules.
  1531. * If that happens, then we need to start cmdline recording
  1532. * directly here. If the global_trace.buffer is already
  1533. * allocated here, then this was called by module code.
  1534. */
  1535. if (global_trace.trace_buffer.buffer)
  1536. tracing_start_cmdline_record();
  1537. }
  1538. void trace_printk_start_comm(void)
  1539. {
  1540. /* Start tracing comms if trace printk is set */
  1541. if (!buffers_allocated)
  1542. return;
  1543. tracing_start_cmdline_record();
  1544. }
  1545. static void trace_printk_start_stop_comm(int enabled)
  1546. {
  1547. if (!buffers_allocated)
  1548. return;
  1549. if (enabled)
  1550. tracing_start_cmdline_record();
  1551. else
  1552. tracing_stop_cmdline_record();
  1553. }
  1554. /**
  1555. * trace_vbprintk - write binary msg to tracing buffer
  1556. *
  1557. */
  1558. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  1559. {
  1560. struct ftrace_event_call *call = &event_bprint;
  1561. struct ring_buffer_event *event;
  1562. struct ring_buffer *buffer;
  1563. struct trace_array *tr = &global_trace;
  1564. struct bprint_entry *entry;
  1565. unsigned long flags;
  1566. char *tbuffer;
  1567. int len = 0, size, pc;
  1568. if (unlikely(tracing_selftest_running || tracing_disabled))
  1569. return 0;
  1570. /* Don't pollute graph traces with trace_vprintk internals */
  1571. pause_graph_tracing();
  1572. pc = preempt_count();
  1573. preempt_disable_notrace();
  1574. tbuffer = get_trace_buf();
  1575. if (!tbuffer) {
  1576. len = 0;
  1577. goto out;
  1578. }
  1579. len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
  1580. if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
  1581. goto out;
  1582. local_save_flags(flags);
  1583. size = sizeof(*entry) + sizeof(u32) * len;
  1584. buffer = tr->trace_buffer.buffer;
  1585. event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
  1586. flags, pc);
  1587. if (!event)
  1588. goto out;
  1589. entry = ring_buffer_event_data(event);
  1590. entry->ip = ip;
  1591. entry->fmt = fmt;
  1592. memcpy(entry->buf, tbuffer, sizeof(u32) * len);
  1593. if (!filter_check_discard(call, entry, buffer, event)) {
  1594. __buffer_unlock_commit(buffer, event);
  1595. ftrace_trace_stack(buffer, flags, 6, pc);
  1596. }
  1597. out:
  1598. preempt_enable_notrace();
  1599. unpause_graph_tracing();
  1600. return len;
  1601. }
  1602. EXPORT_SYMBOL_GPL(trace_vbprintk);
  1603. static int
  1604. __trace_array_vprintk(struct ring_buffer *buffer,
  1605. unsigned long ip, const char *fmt, va_list args)
  1606. {
  1607. struct ftrace_event_call *call = &event_print;
  1608. struct ring_buffer_event *event;
  1609. int len = 0, size, pc;
  1610. struct print_entry *entry;
  1611. unsigned long flags;
  1612. char *tbuffer;
  1613. if (tracing_disabled || tracing_selftest_running)
  1614. return 0;
  1615. /* Don't pollute graph traces with trace_vprintk internals */
  1616. pause_graph_tracing();
  1617. pc = preempt_count();
  1618. preempt_disable_notrace();
  1619. tbuffer = get_trace_buf();
  1620. if (!tbuffer) {
  1621. len = 0;
  1622. goto out;
  1623. }
  1624. len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
  1625. if (len > TRACE_BUF_SIZE)
  1626. goto out;
  1627. local_save_flags(flags);
  1628. size = sizeof(*entry) + len + 1;
  1629. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  1630. flags, pc);
  1631. if (!event)
  1632. goto out;
  1633. entry = ring_buffer_event_data(event);
  1634. entry->ip = ip;
  1635. memcpy(&entry->buf, tbuffer, len);
  1636. entry->buf[len] = '\0';
  1637. if (!filter_check_discard(call, entry, buffer, event)) {
  1638. __buffer_unlock_commit(buffer, event);
  1639. ftrace_trace_stack(buffer, flags, 6, pc);
  1640. }
  1641. out:
  1642. preempt_enable_notrace();
  1643. unpause_graph_tracing();
  1644. return len;
  1645. }
  1646. int trace_array_vprintk(struct trace_array *tr,
  1647. unsigned long ip, const char *fmt, va_list args)
  1648. {
  1649. return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
  1650. }
  1651. int trace_array_printk(struct trace_array *tr,
  1652. unsigned long ip, const char *fmt, ...)
  1653. {
  1654. int ret;
  1655. va_list ap;
  1656. if (!(trace_flags & TRACE_ITER_PRINTK))
  1657. return 0;
  1658. va_start(ap, fmt);
  1659. ret = trace_array_vprintk(tr, ip, fmt, ap);
  1660. va_end(ap);
  1661. return ret;
  1662. }
  1663. int trace_array_printk_buf(struct ring_buffer *buffer,
  1664. unsigned long ip, const char *fmt, ...)
  1665. {
  1666. int ret;
  1667. va_list ap;
  1668. if (!(trace_flags & TRACE_ITER_PRINTK))
  1669. return 0;
  1670. va_start(ap, fmt);
  1671. ret = __trace_array_vprintk(buffer, ip, fmt, ap);
  1672. va_end(ap);
  1673. return ret;
  1674. }
  1675. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  1676. {
  1677. return trace_array_vprintk(&global_trace, ip, fmt, args);
  1678. }
  1679. EXPORT_SYMBOL_GPL(trace_vprintk);
  1680. static void trace_iterator_increment(struct trace_iterator *iter)
  1681. {
  1682. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
  1683. iter->idx++;
  1684. if (buf_iter)
  1685. ring_buffer_read(buf_iter, NULL);
  1686. }
  1687. static struct trace_entry *
  1688. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
  1689. unsigned long *lost_events)
  1690. {
  1691. struct ring_buffer_event *event;
  1692. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
  1693. if (buf_iter)
  1694. event = ring_buffer_iter_peek(buf_iter, ts);
  1695. else
  1696. event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
  1697. lost_events);
  1698. if (event) {
  1699. iter->ent_size = ring_buffer_event_length(event);
  1700. return ring_buffer_event_data(event);
  1701. }
  1702. iter->ent_size = 0;
  1703. return NULL;
  1704. }
  1705. static struct trace_entry *
  1706. __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
  1707. unsigned long *missing_events, u64 *ent_ts)
  1708. {
  1709. struct ring_buffer *buffer = iter->trace_buffer->buffer;
  1710. struct trace_entry *ent, *next = NULL;
  1711. unsigned long lost_events = 0, next_lost = 0;
  1712. int cpu_file = iter->cpu_file;
  1713. u64 next_ts = 0, ts;
  1714. int next_cpu = -1;
  1715. int next_size = 0;
  1716. int cpu;
  1717. /*
  1718. * If we are in a per_cpu trace file, don't bother by iterating over
  1719. * all cpu and peek directly.
  1720. */
  1721. if (cpu_file > RING_BUFFER_ALL_CPUS) {
  1722. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1723. return NULL;
  1724. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  1725. if (ent_cpu)
  1726. *ent_cpu = cpu_file;
  1727. return ent;
  1728. }
  1729. for_each_tracing_cpu(cpu) {
  1730. if (ring_buffer_empty_cpu(buffer, cpu))
  1731. continue;
  1732. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  1733. /*
  1734. * Pick the entry with the smallest timestamp:
  1735. */
  1736. if (ent && (!next || ts < next_ts)) {
  1737. next = ent;
  1738. next_cpu = cpu;
  1739. next_ts = ts;
  1740. next_lost = lost_events;
  1741. next_size = iter->ent_size;
  1742. }
  1743. }
  1744. iter->ent_size = next_size;
  1745. if (ent_cpu)
  1746. *ent_cpu = next_cpu;
  1747. if (ent_ts)
  1748. *ent_ts = next_ts;
  1749. if (missing_events)
  1750. *missing_events = next_lost;
  1751. return next;
  1752. }
  1753. /* Find the next real entry, without updating the iterator itself */
  1754. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1755. int *ent_cpu, u64 *ent_ts)
  1756. {
  1757. return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  1758. }
  1759. /* Find the next real entry, and increment the iterator to the next entry */
  1760. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  1761. {
  1762. iter->ent = __find_next_entry(iter, &iter->cpu,
  1763. &iter->lost_events, &iter->ts);
  1764. if (iter->ent)
  1765. trace_iterator_increment(iter);
  1766. return iter->ent ? iter : NULL;
  1767. }
  1768. static void trace_consume(struct trace_iterator *iter)
  1769. {
  1770. ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
  1771. &iter->lost_events);
  1772. }
  1773. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1774. {
  1775. struct trace_iterator *iter = m->private;
  1776. int i = (int)*pos;
  1777. void *ent;
  1778. WARN_ON_ONCE(iter->leftover);
  1779. (*pos)++;
  1780. /* can't go backwards */
  1781. if (iter->idx > i)
  1782. return NULL;
  1783. if (iter->idx < 0)
  1784. ent = trace_find_next_entry_inc(iter);
  1785. else
  1786. ent = iter;
  1787. while (ent && iter->idx < i)
  1788. ent = trace_find_next_entry_inc(iter);
  1789. iter->pos = *pos;
  1790. return ent;
  1791. }
  1792. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  1793. {
  1794. struct ring_buffer_event *event;
  1795. struct ring_buffer_iter *buf_iter;
  1796. unsigned long entries = 0;
  1797. u64 ts;
  1798. per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
  1799. buf_iter = trace_buffer_iter(iter, cpu);
  1800. if (!buf_iter)
  1801. return;
  1802. ring_buffer_iter_reset(buf_iter);
  1803. /*
  1804. * We could have the case with the max latency tracers
  1805. * that a reset never took place on a cpu. This is evident
  1806. * by the timestamp being before the start of the buffer.
  1807. */
  1808. while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
  1809. if (ts >= iter->trace_buffer->time_start)
  1810. break;
  1811. entries++;
  1812. ring_buffer_read(buf_iter, NULL);
  1813. }
  1814. per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
  1815. }
  1816. /*
  1817. * The current tracer is copied to avoid a global locking
  1818. * all around.
  1819. */
  1820. static void *s_start(struct seq_file *m, loff_t *pos)
  1821. {
  1822. struct trace_iterator *iter = m->private;
  1823. struct trace_array *tr = iter->tr;
  1824. int cpu_file = iter->cpu_file;
  1825. void *p = NULL;
  1826. loff_t l = 0;
  1827. int cpu;
  1828. /*
  1829. * copy the tracer to avoid using a global lock all around.
  1830. * iter->trace is a copy of current_trace, the pointer to the
  1831. * name may be used instead of a strcmp(), as iter->trace->name
  1832. * will point to the same string as current_trace->name.
  1833. */
  1834. mutex_lock(&trace_types_lock);
  1835. if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
  1836. *iter->trace = *tr->current_trace;
  1837. mutex_unlock(&trace_types_lock);
  1838. #ifdef CONFIG_TRACER_MAX_TRACE
  1839. if (iter->snapshot && iter->trace->use_max_tr)
  1840. return ERR_PTR(-EBUSY);
  1841. #endif
  1842. if (!iter->snapshot)
  1843. atomic_inc(&trace_record_cmdline_disabled);
  1844. if (*pos != iter->pos) {
  1845. iter->ent = NULL;
  1846. iter->cpu = 0;
  1847. iter->idx = -1;
  1848. if (cpu_file == RING_BUFFER_ALL_CPUS) {
  1849. for_each_tracing_cpu(cpu)
  1850. tracing_iter_reset(iter, cpu);
  1851. } else
  1852. tracing_iter_reset(iter, cpu_file);
  1853. iter->leftover = 0;
  1854. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1855. ;
  1856. } else {
  1857. /*
  1858. * If we overflowed the seq_file before, then we want
  1859. * to just reuse the trace_seq buffer again.
  1860. */
  1861. if (iter->leftover)
  1862. p = iter;
  1863. else {
  1864. l = *pos - 1;
  1865. p = s_next(m, p, &l);
  1866. }
  1867. }
  1868. trace_event_read_lock();
  1869. trace_access_lock(cpu_file);
  1870. return p;
  1871. }
  1872. static void s_stop(struct seq_file *m, void *p)
  1873. {
  1874. struct trace_iterator *iter = m->private;
  1875. #ifdef CONFIG_TRACER_MAX_TRACE
  1876. if (iter->snapshot && iter->trace->use_max_tr)
  1877. return;
  1878. #endif
  1879. if (!iter->snapshot)
  1880. atomic_dec(&trace_record_cmdline_disabled);
  1881. trace_access_unlock(iter->cpu_file);
  1882. trace_event_read_unlock();
  1883. }
  1884. static void
  1885. get_total_entries(struct trace_buffer *buf,
  1886. unsigned long *total, unsigned long *entries)
  1887. {
  1888. unsigned long count;
  1889. int cpu;
  1890. *total = 0;
  1891. *entries = 0;
  1892. for_each_tracing_cpu(cpu) {
  1893. count = ring_buffer_entries_cpu(buf->buffer, cpu);
  1894. /*
  1895. * If this buffer has skipped entries, then we hold all
  1896. * entries for the trace and we need to ignore the
  1897. * ones before the time stamp.
  1898. */
  1899. if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
  1900. count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
  1901. /* total is the same as the entries */
  1902. *total += count;
  1903. } else
  1904. *total += count +
  1905. ring_buffer_overrun_cpu(buf->buffer, cpu);
  1906. *entries += count;
  1907. }
  1908. }
  1909. static void print_lat_help_header(struct seq_file *m)
  1910. {
  1911. seq_puts(m, "# _------=> CPU# \n");
  1912. seq_puts(m, "# / _-----=> irqs-off \n");
  1913. seq_puts(m, "# | / _----=> need-resched \n");
  1914. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1915. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1916. seq_puts(m, "# |||| / delay \n");
  1917. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1918. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1919. }
  1920. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  1921. {
  1922. unsigned long total;
  1923. unsigned long entries;
  1924. get_total_entries(buf, &total, &entries);
  1925. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  1926. entries, total, num_online_cpus());
  1927. seq_puts(m, "#\n");
  1928. }
  1929. static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
  1930. {
  1931. print_event_info(buf, m);
  1932. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1933. seq_puts(m, "# | | | | |\n");
  1934. }
  1935. static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
  1936. {
  1937. print_event_info(buf, m);
  1938. seq_puts(m, "# _-----=> irqs-off\n");
  1939. seq_puts(m, "# / _----=> need-resched\n");
  1940. seq_puts(m, "# | / _---=> hardirq/softirq\n");
  1941. seq_puts(m, "# || / _--=> preempt-depth\n");
  1942. seq_puts(m, "# ||| / delay\n");
  1943. seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
  1944. seq_puts(m, "# | | | |||| | |\n");
  1945. }
  1946. void
  1947. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1948. {
  1949. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1950. struct trace_buffer *buf = iter->trace_buffer;
  1951. struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
  1952. struct tracer *type = iter->trace;
  1953. unsigned long entries;
  1954. unsigned long total;
  1955. const char *name = "preemption";
  1956. name = type->name;
  1957. get_total_entries(buf, &total, &entries);
  1958. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1959. name, UTS_RELEASE);
  1960. seq_puts(m, "# -----------------------------------"
  1961. "---------------------------------\n");
  1962. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1963. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1964. nsecs_to_usecs(data->saved_latency),
  1965. entries,
  1966. total,
  1967. buf->cpu,
  1968. #if defined(CONFIG_PREEMPT_NONE)
  1969. "server",
  1970. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1971. "desktop",
  1972. #elif defined(CONFIG_PREEMPT)
  1973. "preempt",
  1974. #else
  1975. "unknown",
  1976. #endif
  1977. /* These are reserved for later use */
  1978. 0, 0, 0, 0);
  1979. #ifdef CONFIG_SMP
  1980. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1981. #else
  1982. seq_puts(m, ")\n");
  1983. #endif
  1984. seq_puts(m, "# -----------------\n");
  1985. seq_printf(m, "# | task: %.16s-%d "
  1986. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1987. data->comm, data->pid,
  1988. from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
  1989. data->policy, data->rt_priority);
  1990. seq_puts(m, "# -----------------\n");
  1991. if (data->critical_start) {
  1992. seq_puts(m, "# => started at: ");
  1993. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1994. trace_print_seq(m, &iter->seq);
  1995. seq_puts(m, "\n# => ended at: ");
  1996. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1997. trace_print_seq(m, &iter->seq);
  1998. seq_puts(m, "\n#\n");
  1999. }
  2000. seq_puts(m, "#\n");
  2001. }
  2002. static void test_cpu_buff_start(struct trace_iterator *iter)
  2003. {
  2004. struct trace_seq *s = &iter->seq;
  2005. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  2006. return;
  2007. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  2008. return;
  2009. if (cpumask_test_cpu(iter->cpu, iter->started))
  2010. return;
  2011. if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
  2012. return;
  2013. cpumask_set_cpu(iter->cpu, iter->started);
  2014. /* Don't print started cpu buffer for the first entry of the trace */
  2015. if (iter->idx > 1)
  2016. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  2017. iter->cpu);
  2018. }
  2019. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  2020. {
  2021. struct trace_seq *s = &iter->seq;
  2022. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  2023. struct trace_entry *entry;
  2024. struct trace_event *event;
  2025. entry = iter->ent;
  2026. test_cpu_buff_start(iter);
  2027. event = ftrace_find_event(entry->type);
  2028. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2029. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  2030. if (!trace_print_lat_context(iter))
  2031. goto partial;
  2032. } else {
  2033. if (!trace_print_context(iter))
  2034. goto partial;
  2035. }
  2036. }
  2037. if (event)
  2038. return event->funcs->trace(iter, sym_flags, event);
  2039. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  2040. goto partial;
  2041. return TRACE_TYPE_HANDLED;
  2042. partial:
  2043. return TRACE_TYPE_PARTIAL_LINE;
  2044. }
  2045. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  2046. {
  2047. struct trace_seq *s = &iter->seq;
  2048. struct trace_entry *entry;
  2049. struct trace_event *event;
  2050. entry = iter->ent;
  2051. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2052. if (!trace_seq_printf(s, "%d %d %llu ",
  2053. entry->pid, iter->cpu, iter->ts))
  2054. goto partial;
  2055. }
  2056. event = ftrace_find_event(entry->type);
  2057. if (event)
  2058. return event->funcs->raw(iter, 0, event);
  2059. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  2060. goto partial;
  2061. return TRACE_TYPE_HANDLED;
  2062. partial:
  2063. return TRACE_TYPE_PARTIAL_LINE;
  2064. }
  2065. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  2066. {
  2067. struct trace_seq *s = &iter->seq;
  2068. unsigned char newline = '\n';
  2069. struct trace_entry *entry;
  2070. struct trace_event *event;
  2071. entry = iter->ent;
  2072. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2073. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  2074. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  2075. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  2076. }
  2077. event = ftrace_find_event(entry->type);
  2078. if (event) {
  2079. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  2080. if (ret != TRACE_TYPE_HANDLED)
  2081. return ret;
  2082. }
  2083. SEQ_PUT_FIELD_RET(s, newline);
  2084. return TRACE_TYPE_HANDLED;
  2085. }
  2086. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  2087. {
  2088. struct trace_seq *s = &iter->seq;
  2089. struct trace_entry *entry;
  2090. struct trace_event *event;
  2091. entry = iter->ent;
  2092. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2093. SEQ_PUT_FIELD_RET(s, entry->pid);
  2094. SEQ_PUT_FIELD_RET(s, iter->cpu);
  2095. SEQ_PUT_FIELD_RET(s, iter->ts);
  2096. }
  2097. event = ftrace_find_event(entry->type);
  2098. return event ? event->funcs->binary(iter, 0, event) :
  2099. TRACE_TYPE_HANDLED;
  2100. }
  2101. int trace_empty(struct trace_iterator *iter)
  2102. {
  2103. struct ring_buffer_iter *buf_iter;
  2104. int cpu;
  2105. /* If we are looking at one CPU buffer, only check that one */
  2106. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  2107. cpu = iter->cpu_file;
  2108. buf_iter = trace_buffer_iter(iter, cpu);
  2109. if (buf_iter) {
  2110. if (!ring_buffer_iter_empty(buf_iter))
  2111. return 0;
  2112. } else {
  2113. if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
  2114. return 0;
  2115. }
  2116. return 1;
  2117. }
  2118. for_each_tracing_cpu(cpu) {
  2119. buf_iter = trace_buffer_iter(iter, cpu);
  2120. if (buf_iter) {
  2121. if (!ring_buffer_iter_empty(buf_iter))
  2122. return 0;
  2123. } else {
  2124. if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
  2125. return 0;
  2126. }
  2127. }
  2128. return 1;
  2129. }
  2130. /* Called with trace_event_read_lock() held. */
  2131. enum print_line_t print_trace_line(struct trace_iterator *iter)
  2132. {
  2133. enum print_line_t ret;
  2134. if (iter->lost_events &&
  2135. !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  2136. iter->cpu, iter->lost_events))
  2137. return TRACE_TYPE_PARTIAL_LINE;
  2138. if (iter->trace && iter->trace->print_line) {
  2139. ret = iter->trace->print_line(iter);
  2140. if (ret != TRACE_TYPE_UNHANDLED)
  2141. return ret;
  2142. }
  2143. if (iter->ent->type == TRACE_BPUTS &&
  2144. trace_flags & TRACE_ITER_PRINTK &&
  2145. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2146. return trace_print_bputs_msg_only(iter);
  2147. if (iter->ent->type == TRACE_BPRINT &&
  2148. trace_flags & TRACE_ITER_PRINTK &&
  2149. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2150. return trace_print_bprintk_msg_only(iter);
  2151. if (iter->ent->type == TRACE_PRINT &&
  2152. trace_flags & TRACE_ITER_PRINTK &&
  2153. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2154. return trace_print_printk_msg_only(iter);
  2155. if (trace_flags & TRACE_ITER_BIN)
  2156. return print_bin_fmt(iter);
  2157. if (trace_flags & TRACE_ITER_HEX)
  2158. return print_hex_fmt(iter);
  2159. if (trace_flags & TRACE_ITER_RAW)
  2160. return print_raw_fmt(iter);
  2161. return print_trace_fmt(iter);
  2162. }
  2163. void trace_latency_header(struct seq_file *m)
  2164. {
  2165. struct trace_iterator *iter = m->private;
  2166. /* print nothing if the buffers are empty */
  2167. if (trace_empty(iter))
  2168. return;
  2169. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  2170. print_trace_header(m, iter);
  2171. if (!(trace_flags & TRACE_ITER_VERBOSE))
  2172. print_lat_help_header(m);
  2173. }
  2174. void trace_default_header(struct seq_file *m)
  2175. {
  2176. struct trace_iterator *iter = m->private;
  2177. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  2178. return;
  2179. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  2180. /* print nothing if the buffers are empty */
  2181. if (trace_empty(iter))
  2182. return;
  2183. print_trace_header(m, iter);
  2184. if (!(trace_flags & TRACE_ITER_VERBOSE))
  2185. print_lat_help_header(m);
  2186. } else {
  2187. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  2188. if (trace_flags & TRACE_ITER_IRQ_INFO)
  2189. print_func_help_header_irq(iter->trace_buffer, m);
  2190. else
  2191. print_func_help_header(iter->trace_buffer, m);
  2192. }
  2193. }
  2194. }
  2195. static void test_ftrace_alive(struct seq_file *m)
  2196. {
  2197. if (!ftrace_is_dead())
  2198. return;
  2199. seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  2200. seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
  2201. }
  2202. #ifdef CONFIG_TRACER_MAX_TRACE
  2203. static void show_snapshot_main_help(struct seq_file *m)
  2204. {
  2205. seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
  2206. seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
  2207. seq_printf(m, "# Takes a snapshot of the main buffer.\n");
  2208. seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
  2209. seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
  2210. seq_printf(m, "# is not a '0' or '1')\n");
  2211. }
  2212. static void show_snapshot_percpu_help(struct seq_file *m)
  2213. {
  2214. seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
  2215. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  2216. seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
  2217. seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
  2218. #else
  2219. seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
  2220. seq_printf(m, "# Must use main snapshot file to allocate.\n");
  2221. #endif
  2222. seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
  2223. seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
  2224. seq_printf(m, "# is not a '0' or '1')\n");
  2225. }
  2226. static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
  2227. {
  2228. if (iter->tr->allocated_snapshot)
  2229. seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
  2230. else
  2231. seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
  2232. seq_printf(m, "# Snapshot commands:\n");
  2233. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  2234. show_snapshot_main_help(m);
  2235. else
  2236. show_snapshot_percpu_help(m);
  2237. }
  2238. #else
  2239. /* Should never be called */
  2240. static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
  2241. #endif
  2242. static int s_show(struct seq_file *m, void *v)
  2243. {
  2244. struct trace_iterator *iter = v;
  2245. int ret;
  2246. if (iter->ent == NULL) {
  2247. if (iter->tr) {
  2248. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  2249. seq_puts(m, "#\n");
  2250. test_ftrace_alive(m);
  2251. }
  2252. if (iter->snapshot && trace_empty(iter))
  2253. print_snapshot_help(m, iter);
  2254. else if (iter->trace && iter->trace->print_header)
  2255. iter->trace->print_header(m);
  2256. else
  2257. trace_default_header(m);
  2258. } else if (iter->leftover) {
  2259. /*
  2260. * If we filled the seq_file buffer earlier, we
  2261. * want to just show it now.
  2262. */
  2263. ret = trace_print_seq(m, &iter->seq);
  2264. /* ret should this time be zero, but you never know */
  2265. iter->leftover = ret;
  2266. } else {
  2267. print_trace_line(iter);
  2268. ret = trace_print_seq(m, &iter->seq);
  2269. /*
  2270. * If we overflow the seq_file buffer, then it will
  2271. * ask us for this data again at start up.
  2272. * Use that instead.
  2273. * ret is 0 if seq_file write succeeded.
  2274. * -1 otherwise.
  2275. */
  2276. iter->leftover = ret;
  2277. }
  2278. return 0;
  2279. }
  2280. static const struct seq_operations tracer_seq_ops = {
  2281. .start = s_start,
  2282. .next = s_next,
  2283. .stop = s_stop,
  2284. .show = s_show,
  2285. };
  2286. static struct trace_iterator *
  2287. __tracing_open(struct inode *inode, struct file *file, bool snapshot)
  2288. {
  2289. struct trace_cpu *tc = inode->i_private;
  2290. struct trace_array *tr = tc->tr;
  2291. struct trace_iterator *iter;
  2292. int cpu;
  2293. if (tracing_disabled)
  2294. return ERR_PTR(-ENODEV);
  2295. iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
  2296. if (!iter)
  2297. return ERR_PTR(-ENOMEM);
  2298. iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
  2299. GFP_KERNEL);
  2300. if (!iter->buffer_iter)
  2301. goto release;
  2302. /*
  2303. * We make a copy of the current tracer to avoid concurrent
  2304. * changes on it while we are reading.
  2305. */
  2306. mutex_lock(&trace_types_lock);
  2307. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  2308. if (!iter->trace)
  2309. goto fail;
  2310. *iter->trace = *tr->current_trace;
  2311. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  2312. goto fail;
  2313. iter->tr = tr;
  2314. #ifdef CONFIG_TRACER_MAX_TRACE
  2315. /* Currently only the top directory has a snapshot */
  2316. if (tr->current_trace->print_max || snapshot)
  2317. iter->trace_buffer = &tr->max_buffer;
  2318. else
  2319. #endif
  2320. iter->trace_buffer = &tr->trace_buffer;
  2321. iter->snapshot = snapshot;
  2322. iter->pos = -1;
  2323. mutex_init(&iter->mutex);
  2324. iter->cpu_file = tc->cpu;
  2325. /* Notify the tracer early; before we stop tracing. */
  2326. if (iter->trace && iter->trace->open)
  2327. iter->trace->open(iter);
  2328. /* Annotate start of buffers if we had overruns */
  2329. if (ring_buffer_overruns(iter->trace_buffer->buffer))
  2330. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  2331. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  2332. if (trace_clocks[trace_clock_id].in_ns)
  2333. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  2334. /* stop the trace while dumping if we are not opening "snapshot" */
  2335. if (!iter->snapshot)
  2336. tracing_stop_tr(tr);
  2337. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  2338. for_each_tracing_cpu(cpu) {
  2339. iter->buffer_iter[cpu] =
  2340. ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
  2341. }
  2342. ring_buffer_read_prepare_sync();
  2343. for_each_tracing_cpu(cpu) {
  2344. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2345. tracing_iter_reset(iter, cpu);
  2346. }
  2347. } else {
  2348. cpu = iter->cpu_file;
  2349. iter->buffer_iter[cpu] =
  2350. ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
  2351. ring_buffer_read_prepare_sync();
  2352. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2353. tracing_iter_reset(iter, cpu);
  2354. }
  2355. tr->ref++;
  2356. mutex_unlock(&trace_types_lock);
  2357. return iter;
  2358. fail:
  2359. mutex_unlock(&trace_types_lock);
  2360. kfree(iter->trace);
  2361. kfree(iter->buffer_iter);
  2362. release:
  2363. seq_release_private(inode, file);
  2364. return ERR_PTR(-ENOMEM);
  2365. }
  2366. int tracing_open_generic(struct inode *inode, struct file *filp)
  2367. {
  2368. if (tracing_disabled)
  2369. return -ENODEV;
  2370. filp->private_data = inode->i_private;
  2371. return 0;
  2372. }
  2373. static int tracing_release(struct inode *inode, struct file *file)
  2374. {
  2375. struct seq_file *m = file->private_data;
  2376. struct trace_iterator *iter;
  2377. struct trace_array *tr;
  2378. int cpu;
  2379. if (!(file->f_mode & FMODE_READ))
  2380. return 0;
  2381. iter = m->private;
  2382. tr = iter->tr;
  2383. mutex_lock(&trace_types_lock);
  2384. WARN_ON(!tr->ref);
  2385. tr->ref--;
  2386. for_each_tracing_cpu(cpu) {
  2387. if (iter->buffer_iter[cpu])
  2388. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2389. }
  2390. if (iter->trace && iter->trace->close)
  2391. iter->trace->close(iter);
  2392. if (!iter->snapshot)
  2393. /* reenable tracing if it was previously enabled */
  2394. tracing_start_tr(tr);
  2395. mutex_unlock(&trace_types_lock);
  2396. mutex_destroy(&iter->mutex);
  2397. free_cpumask_var(iter->started);
  2398. kfree(iter->trace);
  2399. kfree(iter->buffer_iter);
  2400. seq_release_private(inode, file);
  2401. return 0;
  2402. }
  2403. static int tracing_open(struct inode *inode, struct file *file)
  2404. {
  2405. struct trace_iterator *iter;
  2406. int ret = 0;
  2407. /* If this file was open for write, then erase contents */
  2408. if ((file->f_mode & FMODE_WRITE) &&
  2409. (file->f_flags & O_TRUNC)) {
  2410. struct trace_cpu *tc = inode->i_private;
  2411. struct trace_array *tr = tc->tr;
  2412. if (tc->cpu == RING_BUFFER_ALL_CPUS)
  2413. tracing_reset_online_cpus(&tr->trace_buffer);
  2414. else
  2415. tracing_reset(&tr->trace_buffer, tc->cpu);
  2416. }
  2417. if (file->f_mode & FMODE_READ) {
  2418. iter = __tracing_open(inode, file, false);
  2419. if (IS_ERR(iter))
  2420. ret = PTR_ERR(iter);
  2421. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2422. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2423. }
  2424. return ret;
  2425. }
  2426. static void *
  2427. t_next(struct seq_file *m, void *v, loff_t *pos)
  2428. {
  2429. struct tracer *t = v;
  2430. (*pos)++;
  2431. if (t)
  2432. t = t->next;
  2433. return t;
  2434. }
  2435. static void *t_start(struct seq_file *m, loff_t *pos)
  2436. {
  2437. struct tracer *t;
  2438. loff_t l = 0;
  2439. mutex_lock(&trace_types_lock);
  2440. for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
  2441. ;
  2442. return t;
  2443. }
  2444. static void t_stop(struct seq_file *m, void *p)
  2445. {
  2446. mutex_unlock(&trace_types_lock);
  2447. }
  2448. static int t_show(struct seq_file *m, void *v)
  2449. {
  2450. struct tracer *t = v;
  2451. if (!t)
  2452. return 0;
  2453. seq_printf(m, "%s", t->name);
  2454. if (t->next)
  2455. seq_putc(m, ' ');
  2456. else
  2457. seq_putc(m, '\n');
  2458. return 0;
  2459. }
  2460. static const struct seq_operations show_traces_seq_ops = {
  2461. .start = t_start,
  2462. .next = t_next,
  2463. .stop = t_stop,
  2464. .show = t_show,
  2465. };
  2466. static int show_traces_open(struct inode *inode, struct file *file)
  2467. {
  2468. if (tracing_disabled)
  2469. return -ENODEV;
  2470. return seq_open(file, &show_traces_seq_ops);
  2471. }
  2472. static ssize_t
  2473. tracing_write_stub(struct file *filp, const char __user *ubuf,
  2474. size_t count, loff_t *ppos)
  2475. {
  2476. return count;
  2477. }
  2478. static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
  2479. {
  2480. if (file->f_mode & FMODE_READ)
  2481. return seq_lseek(file, offset, origin);
  2482. else
  2483. return 0;
  2484. }
  2485. static const struct file_operations tracing_fops = {
  2486. .open = tracing_open,
  2487. .read = seq_read,
  2488. .write = tracing_write_stub,
  2489. .llseek = tracing_seek,
  2490. .release = tracing_release,
  2491. };
  2492. static const struct file_operations show_traces_fops = {
  2493. .open = show_traces_open,
  2494. .read = seq_read,
  2495. .release = seq_release,
  2496. .llseek = seq_lseek,
  2497. };
  2498. /*
  2499. * Only trace on a CPU if the bitmask is set:
  2500. */
  2501. static cpumask_var_t tracing_cpumask;
  2502. /*
  2503. * The tracer itself will not take this lock, but still we want
  2504. * to provide a consistent cpumask to user-space:
  2505. */
  2506. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2507. /*
  2508. * Temporary storage for the character representation of the
  2509. * CPU bitmask (and one more byte for the newline):
  2510. */
  2511. static char mask_str[NR_CPUS + 1];
  2512. static ssize_t
  2513. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2514. size_t count, loff_t *ppos)
  2515. {
  2516. int len;
  2517. mutex_lock(&tracing_cpumask_update_lock);
  2518. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2519. if (count - len < 2) {
  2520. count = -EINVAL;
  2521. goto out_err;
  2522. }
  2523. len += sprintf(mask_str + len, "\n");
  2524. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2525. out_err:
  2526. mutex_unlock(&tracing_cpumask_update_lock);
  2527. return count;
  2528. }
  2529. static ssize_t
  2530. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2531. size_t count, loff_t *ppos)
  2532. {
  2533. struct trace_array *tr = filp->private_data;
  2534. cpumask_var_t tracing_cpumask_new;
  2535. int err, cpu;
  2536. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  2537. return -ENOMEM;
  2538. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2539. if (err)
  2540. goto err_unlock;
  2541. mutex_lock(&tracing_cpumask_update_lock);
  2542. local_irq_disable();
  2543. arch_spin_lock(&ftrace_max_lock);
  2544. for_each_tracing_cpu(cpu) {
  2545. /*
  2546. * Increase/decrease the disabled counter if we are
  2547. * about to flip a bit in the cpumask:
  2548. */
  2549. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  2550. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2551. atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2552. ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
  2553. }
  2554. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  2555. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2556. atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2557. ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
  2558. }
  2559. }
  2560. arch_spin_unlock(&ftrace_max_lock);
  2561. local_irq_enable();
  2562. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  2563. mutex_unlock(&tracing_cpumask_update_lock);
  2564. free_cpumask_var(tracing_cpumask_new);
  2565. return count;
  2566. err_unlock:
  2567. free_cpumask_var(tracing_cpumask_new);
  2568. return err;
  2569. }
  2570. static const struct file_operations tracing_cpumask_fops = {
  2571. .open = tracing_open_generic,
  2572. .read = tracing_cpumask_read,
  2573. .write = tracing_cpumask_write,
  2574. .llseek = generic_file_llseek,
  2575. };
  2576. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2577. {
  2578. struct tracer_opt *trace_opts;
  2579. struct trace_array *tr = m->private;
  2580. u32 tracer_flags;
  2581. int i;
  2582. mutex_lock(&trace_types_lock);
  2583. tracer_flags = tr->current_trace->flags->val;
  2584. trace_opts = tr->current_trace->flags->opts;
  2585. for (i = 0; trace_options[i]; i++) {
  2586. if (trace_flags & (1 << i))
  2587. seq_printf(m, "%s\n", trace_options[i]);
  2588. else
  2589. seq_printf(m, "no%s\n", trace_options[i]);
  2590. }
  2591. for (i = 0; trace_opts[i].name; i++) {
  2592. if (tracer_flags & trace_opts[i].bit)
  2593. seq_printf(m, "%s\n", trace_opts[i].name);
  2594. else
  2595. seq_printf(m, "no%s\n", trace_opts[i].name);
  2596. }
  2597. mutex_unlock(&trace_types_lock);
  2598. return 0;
  2599. }
  2600. static int __set_tracer_option(struct tracer *trace,
  2601. struct tracer_flags *tracer_flags,
  2602. struct tracer_opt *opts, int neg)
  2603. {
  2604. int ret;
  2605. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2606. if (ret)
  2607. return ret;
  2608. if (neg)
  2609. tracer_flags->val &= ~opts->bit;
  2610. else
  2611. tracer_flags->val |= opts->bit;
  2612. return 0;
  2613. }
  2614. /* Try to assign a tracer specific option */
  2615. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2616. {
  2617. struct tracer_flags *tracer_flags = trace->flags;
  2618. struct tracer_opt *opts = NULL;
  2619. int i;
  2620. for (i = 0; tracer_flags->opts[i].name; i++) {
  2621. opts = &tracer_flags->opts[i];
  2622. if (strcmp(cmp, opts->name) == 0)
  2623. return __set_tracer_option(trace, trace->flags,
  2624. opts, neg);
  2625. }
  2626. return -EINVAL;
  2627. }
  2628. /* Some tracers require overwrite to stay enabled */
  2629. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  2630. {
  2631. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  2632. return -1;
  2633. return 0;
  2634. }
  2635. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
  2636. {
  2637. /* do nothing if flag is already set */
  2638. if (!!(trace_flags & mask) == !!enabled)
  2639. return 0;
  2640. /* Give the tracer a chance to approve the change */
  2641. if (tr->current_trace->flag_changed)
  2642. if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
  2643. return -EINVAL;
  2644. if (enabled)
  2645. trace_flags |= mask;
  2646. else
  2647. trace_flags &= ~mask;
  2648. if (mask == TRACE_ITER_RECORD_CMD)
  2649. trace_event_enable_cmd_record(enabled);
  2650. if (mask == TRACE_ITER_OVERWRITE) {
  2651. ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
  2652. #ifdef CONFIG_TRACER_MAX_TRACE
  2653. ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
  2654. #endif
  2655. }
  2656. if (mask == TRACE_ITER_PRINTK)
  2657. trace_printk_start_stop_comm(enabled);
  2658. return 0;
  2659. }
  2660. static int trace_set_options(struct trace_array *tr, char *option)
  2661. {
  2662. char *cmp;
  2663. int neg = 0;
  2664. int ret = -ENODEV;
  2665. int i;
  2666. cmp = strstrip(option);
  2667. if (strncmp(cmp, "no", 2) == 0) {
  2668. neg = 1;
  2669. cmp += 2;
  2670. }
  2671. mutex_lock(&trace_types_lock);
  2672. for (i = 0; trace_options[i]; i++) {
  2673. if (strcmp(cmp, trace_options[i]) == 0) {
  2674. ret = set_tracer_flag(tr, 1 << i, !neg);
  2675. break;
  2676. }
  2677. }
  2678. /* If no option could be set, test the specific tracer options */
  2679. if (!trace_options[i])
  2680. ret = set_tracer_option(tr->current_trace, cmp, neg);
  2681. mutex_unlock(&trace_types_lock);
  2682. return ret;
  2683. }
  2684. static ssize_t
  2685. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2686. size_t cnt, loff_t *ppos)
  2687. {
  2688. struct seq_file *m = filp->private_data;
  2689. struct trace_array *tr = m->private;
  2690. char buf[64];
  2691. int ret;
  2692. if (cnt >= sizeof(buf))
  2693. return -EINVAL;
  2694. if (copy_from_user(&buf, ubuf, cnt))
  2695. return -EFAULT;
  2696. buf[cnt] = 0;
  2697. ret = trace_set_options(tr, buf);
  2698. if (ret < 0)
  2699. return ret;
  2700. *ppos += cnt;
  2701. return cnt;
  2702. }
  2703. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2704. {
  2705. if (tracing_disabled)
  2706. return -ENODEV;
  2707. return single_open(file, tracing_trace_options_show, inode->i_private);
  2708. }
  2709. static const struct file_operations tracing_iter_fops = {
  2710. .open = tracing_trace_options_open,
  2711. .read = seq_read,
  2712. .llseek = seq_lseek,
  2713. .release = single_release,
  2714. .write = tracing_trace_options_write,
  2715. };
  2716. static const char readme_msg[] =
  2717. "tracing mini-HOWTO:\n\n"
  2718. "# mount -t debugfs nodev /sys/kernel/debug\n\n"
  2719. "# cat /sys/kernel/debug/tracing/available_tracers\n"
  2720. "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
  2721. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2722. "nop\n"
  2723. "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
  2724. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2725. "wakeup\n"
  2726. "# cat /sys/kernel/debug/tracing/trace_options\n"
  2727. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2728. "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
  2729. "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
  2730. "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
  2731. "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
  2732. ;
  2733. static ssize_t
  2734. tracing_readme_read(struct file *filp, char __user *ubuf,
  2735. size_t cnt, loff_t *ppos)
  2736. {
  2737. return simple_read_from_buffer(ubuf, cnt, ppos,
  2738. readme_msg, strlen(readme_msg));
  2739. }
  2740. static const struct file_operations tracing_readme_fops = {
  2741. .open = tracing_open_generic,
  2742. .read = tracing_readme_read,
  2743. .llseek = generic_file_llseek,
  2744. };
  2745. static ssize_t
  2746. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2747. size_t cnt, loff_t *ppos)
  2748. {
  2749. char *buf_comm;
  2750. char *file_buf;
  2751. char *buf;
  2752. int len = 0;
  2753. int pid;
  2754. int i;
  2755. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2756. if (!file_buf)
  2757. return -ENOMEM;
  2758. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2759. if (!buf_comm) {
  2760. kfree(file_buf);
  2761. return -ENOMEM;
  2762. }
  2763. buf = file_buf;
  2764. for (i = 0; i < SAVED_CMDLINES; i++) {
  2765. int r;
  2766. pid = map_cmdline_to_pid[i];
  2767. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2768. continue;
  2769. trace_find_cmdline(pid, buf_comm);
  2770. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2771. buf += r;
  2772. len += r;
  2773. }
  2774. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2775. file_buf, len);
  2776. kfree(file_buf);
  2777. kfree(buf_comm);
  2778. return len;
  2779. }
  2780. static const struct file_operations tracing_saved_cmdlines_fops = {
  2781. .open = tracing_open_generic,
  2782. .read = tracing_saved_cmdlines_read,
  2783. .llseek = generic_file_llseek,
  2784. };
  2785. static ssize_t
  2786. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2787. size_t cnt, loff_t *ppos)
  2788. {
  2789. struct trace_array *tr = filp->private_data;
  2790. char buf[MAX_TRACER_SIZE+2];
  2791. int r;
  2792. mutex_lock(&trace_types_lock);
  2793. r = sprintf(buf, "%s\n", tr->current_trace->name);
  2794. mutex_unlock(&trace_types_lock);
  2795. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2796. }
  2797. int tracer_init(struct tracer *t, struct trace_array *tr)
  2798. {
  2799. tracing_reset_online_cpus(&tr->trace_buffer);
  2800. return t->init(tr);
  2801. }
  2802. static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
  2803. {
  2804. int cpu;
  2805. for_each_tracing_cpu(cpu)
  2806. per_cpu_ptr(buf->data, cpu)->entries = val;
  2807. }
  2808. #ifdef CONFIG_TRACER_MAX_TRACE
  2809. /* resize @tr's buffer to the size of @size_tr's entries */
  2810. static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
  2811. struct trace_buffer *size_buf, int cpu_id)
  2812. {
  2813. int cpu, ret = 0;
  2814. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  2815. for_each_tracing_cpu(cpu) {
  2816. ret = ring_buffer_resize(trace_buf->buffer,
  2817. per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
  2818. if (ret < 0)
  2819. break;
  2820. per_cpu_ptr(trace_buf->data, cpu)->entries =
  2821. per_cpu_ptr(size_buf->data, cpu)->entries;
  2822. }
  2823. } else {
  2824. ret = ring_buffer_resize(trace_buf->buffer,
  2825. per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
  2826. if (ret == 0)
  2827. per_cpu_ptr(trace_buf->data, cpu_id)->entries =
  2828. per_cpu_ptr(size_buf->data, cpu_id)->entries;
  2829. }
  2830. return ret;
  2831. }
  2832. #endif /* CONFIG_TRACER_MAX_TRACE */
  2833. static int __tracing_resize_ring_buffer(struct trace_array *tr,
  2834. unsigned long size, int cpu)
  2835. {
  2836. int ret;
  2837. /*
  2838. * If kernel or user changes the size of the ring buffer
  2839. * we use the size that was given, and we can forget about
  2840. * expanding it later.
  2841. */
  2842. ring_buffer_expanded = true;
  2843. /* May be called before buffers are initialized */
  2844. if (!tr->trace_buffer.buffer)
  2845. return 0;
  2846. ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
  2847. if (ret < 0)
  2848. return ret;
  2849. #ifdef CONFIG_TRACER_MAX_TRACE
  2850. if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
  2851. !tr->current_trace->use_max_tr)
  2852. goto out;
  2853. ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
  2854. if (ret < 0) {
  2855. int r = resize_buffer_duplicate_size(&tr->trace_buffer,
  2856. &tr->trace_buffer, cpu);
  2857. if (r < 0) {
  2858. /*
  2859. * AARGH! We are left with different
  2860. * size max buffer!!!!
  2861. * The max buffer is our "snapshot" buffer.
  2862. * When a tracer needs a snapshot (one of the
  2863. * latency tracers), it swaps the max buffer
  2864. * with the saved snap shot. We succeeded to
  2865. * update the size of the main buffer, but failed to
  2866. * update the size of the max buffer. But when we tried
  2867. * to reset the main buffer to the original size, we
  2868. * failed there too. This is very unlikely to
  2869. * happen, but if it does, warn and kill all
  2870. * tracing.
  2871. */
  2872. WARN_ON(1);
  2873. tracing_disabled = 1;
  2874. }
  2875. return ret;
  2876. }
  2877. if (cpu == RING_BUFFER_ALL_CPUS)
  2878. set_buffer_entries(&tr->max_buffer, size);
  2879. else
  2880. per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
  2881. out:
  2882. #endif /* CONFIG_TRACER_MAX_TRACE */
  2883. if (cpu == RING_BUFFER_ALL_CPUS)
  2884. set_buffer_entries(&tr->trace_buffer, size);
  2885. else
  2886. per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
  2887. return ret;
  2888. }
  2889. static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
  2890. unsigned long size, int cpu_id)
  2891. {
  2892. int ret = size;
  2893. mutex_lock(&trace_types_lock);
  2894. if (cpu_id != RING_BUFFER_ALL_CPUS) {
  2895. /* make sure, this cpu is enabled in the mask */
  2896. if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
  2897. ret = -EINVAL;
  2898. goto out;
  2899. }
  2900. }
  2901. ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
  2902. if (ret < 0)
  2903. ret = -ENOMEM;
  2904. out:
  2905. mutex_unlock(&trace_types_lock);
  2906. return ret;
  2907. }
  2908. /**
  2909. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2910. *
  2911. * To save on memory when the tracing is never used on a system with it
  2912. * configured in. The ring buffers are set to a minimum size. But once
  2913. * a user starts to use the tracing facility, then they need to grow
  2914. * to their default size.
  2915. *
  2916. * This function is to be called when a tracer is about to be used.
  2917. */
  2918. int tracing_update_buffers(void)
  2919. {
  2920. int ret = 0;
  2921. mutex_lock(&trace_types_lock);
  2922. if (!ring_buffer_expanded)
  2923. ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
  2924. RING_BUFFER_ALL_CPUS);
  2925. mutex_unlock(&trace_types_lock);
  2926. return ret;
  2927. }
  2928. struct trace_option_dentry;
  2929. static struct trace_option_dentry *
  2930. create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
  2931. static void
  2932. destroy_trace_option_files(struct trace_option_dentry *topts);
  2933. static int tracing_set_tracer(const char *buf)
  2934. {
  2935. static struct trace_option_dentry *topts;
  2936. struct trace_array *tr = &global_trace;
  2937. struct tracer *t;
  2938. #ifdef CONFIG_TRACER_MAX_TRACE
  2939. bool had_max_tr;
  2940. #endif
  2941. int ret = 0;
  2942. mutex_lock(&trace_types_lock);
  2943. if (!ring_buffer_expanded) {
  2944. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  2945. RING_BUFFER_ALL_CPUS);
  2946. if (ret < 0)
  2947. goto out;
  2948. ret = 0;
  2949. }
  2950. for (t = trace_types; t; t = t->next) {
  2951. if (strcmp(t->name, buf) == 0)
  2952. break;
  2953. }
  2954. if (!t) {
  2955. ret = -EINVAL;
  2956. goto out;
  2957. }
  2958. if (t == tr->current_trace)
  2959. goto out;
  2960. trace_branch_disable();
  2961. tr->current_trace->enabled = false;
  2962. if (tr->current_trace->reset)
  2963. tr->current_trace->reset(tr);
  2964. /* Current trace needs to be nop_trace before synchronize_sched */
  2965. tr->current_trace = &nop_trace;
  2966. #ifdef CONFIG_TRACER_MAX_TRACE
  2967. had_max_tr = tr->allocated_snapshot;
  2968. if (had_max_tr && !t->use_max_tr) {
  2969. /*
  2970. * We need to make sure that the update_max_tr sees that
  2971. * current_trace changed to nop_trace to keep it from
  2972. * swapping the buffers after we resize it.
  2973. * The update_max_tr is called from interrupts disabled
  2974. * so a synchronized_sched() is sufficient.
  2975. */
  2976. synchronize_sched();
  2977. free_snapshot(tr);
  2978. }
  2979. #endif
  2980. destroy_trace_option_files(topts);
  2981. topts = create_trace_option_files(tr, t);
  2982. #ifdef CONFIG_TRACER_MAX_TRACE
  2983. if (t->use_max_tr && !had_max_tr) {
  2984. ret = alloc_snapshot(tr);
  2985. if (ret < 0)
  2986. goto out;
  2987. }
  2988. #endif
  2989. if (t->init) {
  2990. ret = tracer_init(t, tr);
  2991. if (ret)
  2992. goto out;
  2993. }
  2994. tr->current_trace = t;
  2995. tr->current_trace->enabled = true;
  2996. trace_branch_enable(tr);
  2997. out:
  2998. mutex_unlock(&trace_types_lock);
  2999. return ret;
  3000. }
  3001. static ssize_t
  3002. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  3003. size_t cnt, loff_t *ppos)
  3004. {
  3005. char buf[MAX_TRACER_SIZE+1];
  3006. int i;
  3007. size_t ret;
  3008. int err;
  3009. ret = cnt;
  3010. if (cnt > MAX_TRACER_SIZE)
  3011. cnt = MAX_TRACER_SIZE;
  3012. if (copy_from_user(&buf, ubuf, cnt))
  3013. return -EFAULT;
  3014. buf[cnt] = 0;
  3015. /* strip ending whitespace. */
  3016. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  3017. buf[i] = 0;
  3018. err = tracing_set_tracer(buf);
  3019. if (err)
  3020. return err;
  3021. *ppos += ret;
  3022. return ret;
  3023. }
  3024. static ssize_t
  3025. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  3026. size_t cnt, loff_t *ppos)
  3027. {
  3028. unsigned long *ptr = filp->private_data;
  3029. char buf[64];
  3030. int r;
  3031. r = snprintf(buf, sizeof(buf), "%ld\n",
  3032. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  3033. if (r > sizeof(buf))
  3034. r = sizeof(buf);
  3035. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3036. }
  3037. static ssize_t
  3038. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  3039. size_t cnt, loff_t *ppos)
  3040. {
  3041. unsigned long *ptr = filp->private_data;
  3042. unsigned long val;
  3043. int ret;
  3044. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3045. if (ret)
  3046. return ret;
  3047. *ptr = val * 1000;
  3048. return cnt;
  3049. }
  3050. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  3051. {
  3052. struct trace_cpu *tc = inode->i_private;
  3053. struct trace_array *tr = tc->tr;
  3054. struct trace_iterator *iter;
  3055. int ret = 0;
  3056. if (tracing_disabled)
  3057. return -ENODEV;
  3058. mutex_lock(&trace_types_lock);
  3059. /* create a buffer to store the information to pass to userspace */
  3060. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3061. if (!iter) {
  3062. ret = -ENOMEM;
  3063. goto out;
  3064. }
  3065. /*
  3066. * We make a copy of the current tracer to avoid concurrent
  3067. * changes on it while we are reading.
  3068. */
  3069. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  3070. if (!iter->trace) {
  3071. ret = -ENOMEM;
  3072. goto fail;
  3073. }
  3074. *iter->trace = *tr->current_trace;
  3075. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  3076. ret = -ENOMEM;
  3077. goto fail;
  3078. }
  3079. /* trace pipe does not show start of buffer */
  3080. cpumask_setall(iter->started);
  3081. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  3082. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  3083. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  3084. if (trace_clocks[trace_clock_id].in_ns)
  3085. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  3086. iter->cpu_file = tc->cpu;
  3087. iter->tr = tc->tr;
  3088. iter->trace_buffer = &tc->tr->trace_buffer;
  3089. mutex_init(&iter->mutex);
  3090. filp->private_data = iter;
  3091. if (iter->trace->pipe_open)
  3092. iter->trace->pipe_open(iter);
  3093. nonseekable_open(inode, filp);
  3094. out:
  3095. mutex_unlock(&trace_types_lock);
  3096. return ret;
  3097. fail:
  3098. kfree(iter->trace);
  3099. kfree(iter);
  3100. mutex_unlock(&trace_types_lock);
  3101. return ret;
  3102. }
  3103. static int tracing_release_pipe(struct inode *inode, struct file *file)
  3104. {
  3105. struct trace_iterator *iter = file->private_data;
  3106. mutex_lock(&trace_types_lock);
  3107. if (iter->trace->pipe_close)
  3108. iter->trace->pipe_close(iter);
  3109. mutex_unlock(&trace_types_lock);
  3110. free_cpumask_var(iter->started);
  3111. mutex_destroy(&iter->mutex);
  3112. kfree(iter->trace);
  3113. kfree(iter);
  3114. return 0;
  3115. }
  3116. static unsigned int
  3117. trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
  3118. {
  3119. /* Iterators are static, they should be filled or empty */
  3120. if (trace_buffer_iter(iter, iter->cpu_file))
  3121. return POLLIN | POLLRDNORM;
  3122. if (trace_flags & TRACE_ITER_BLOCK)
  3123. /*
  3124. * Always select as readable when in blocking mode
  3125. */
  3126. return POLLIN | POLLRDNORM;
  3127. else
  3128. return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
  3129. filp, poll_table);
  3130. }
  3131. static unsigned int
  3132. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  3133. {
  3134. struct trace_iterator *iter = filp->private_data;
  3135. return trace_poll(iter, filp, poll_table);
  3136. }
  3137. /*
  3138. * This is a make-shift waitqueue.
  3139. * A tracer might use this callback on some rare cases:
  3140. *
  3141. * 1) the current tracer might hold the runqueue lock when it wakes up
  3142. * a reader, hence a deadlock (sched, function, and function graph tracers)
  3143. * 2) the function tracers, trace all functions, we don't want
  3144. * the overhead of calling wake_up and friends
  3145. * (and tracing them too)
  3146. *
  3147. * Anyway, this is really very primitive wakeup.
  3148. */
  3149. void poll_wait_pipe(struct trace_iterator *iter)
  3150. {
  3151. set_current_state(TASK_INTERRUPTIBLE);
  3152. /* sleep for 100 msecs, and try again. */
  3153. schedule_timeout(HZ / 10);
  3154. }
  3155. /* Must be called with trace_types_lock mutex held. */
  3156. static int tracing_wait_pipe(struct file *filp)
  3157. {
  3158. struct trace_iterator *iter = filp->private_data;
  3159. while (trace_empty(iter)) {
  3160. if ((filp->f_flags & O_NONBLOCK)) {
  3161. return -EAGAIN;
  3162. }
  3163. mutex_unlock(&iter->mutex);
  3164. iter->trace->wait_pipe(iter);
  3165. mutex_lock(&iter->mutex);
  3166. if (signal_pending(current))
  3167. return -EINTR;
  3168. /*
  3169. * We block until we read something and tracing is disabled.
  3170. * We still block if tracing is disabled, but we have never
  3171. * read anything. This allows a user to cat this file, and
  3172. * then enable tracing. But after we have read something,
  3173. * we give an EOF when tracing is again disabled.
  3174. *
  3175. * iter->pos will be 0 if we haven't read anything.
  3176. */
  3177. if (!tracing_is_enabled() && iter->pos)
  3178. break;
  3179. }
  3180. return 1;
  3181. }
  3182. /*
  3183. * Consumer reader.
  3184. */
  3185. static ssize_t
  3186. tracing_read_pipe(struct file *filp, char __user *ubuf,
  3187. size_t cnt, loff_t *ppos)
  3188. {
  3189. struct trace_iterator *iter = filp->private_data;
  3190. struct trace_array *tr = iter->tr;
  3191. ssize_t sret;
  3192. /* return any leftover data */
  3193. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3194. if (sret != -EBUSY)
  3195. return sret;
  3196. trace_seq_init(&iter->seq);
  3197. /* copy the tracer to avoid using a global lock all around */
  3198. mutex_lock(&trace_types_lock);
  3199. if (unlikely(iter->trace->name != tr->current_trace->name))
  3200. *iter->trace = *tr->current_trace;
  3201. mutex_unlock(&trace_types_lock);
  3202. /*
  3203. * Avoid more than one consumer on a single file descriptor
  3204. * This is just a matter of traces coherency, the ring buffer itself
  3205. * is protected.
  3206. */
  3207. mutex_lock(&iter->mutex);
  3208. if (iter->trace->read) {
  3209. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  3210. if (sret)
  3211. goto out;
  3212. }
  3213. waitagain:
  3214. sret = tracing_wait_pipe(filp);
  3215. if (sret <= 0)
  3216. goto out;
  3217. /* stop when tracing is finished */
  3218. if (trace_empty(iter)) {
  3219. sret = 0;
  3220. goto out;
  3221. }
  3222. if (cnt >= PAGE_SIZE)
  3223. cnt = PAGE_SIZE - 1;
  3224. /* reset all but tr, trace, and overruns */
  3225. memset(&iter->seq, 0,
  3226. sizeof(struct trace_iterator) -
  3227. offsetof(struct trace_iterator, seq));
  3228. iter->pos = -1;
  3229. trace_event_read_lock();
  3230. trace_access_lock(iter->cpu_file);
  3231. while (trace_find_next_entry_inc(iter) != NULL) {
  3232. enum print_line_t ret;
  3233. int len = iter->seq.len;
  3234. ret = print_trace_line(iter);
  3235. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3236. /* don't print partial lines */
  3237. iter->seq.len = len;
  3238. break;
  3239. }
  3240. if (ret != TRACE_TYPE_NO_CONSUME)
  3241. trace_consume(iter);
  3242. if (iter->seq.len >= cnt)
  3243. break;
  3244. /*
  3245. * Setting the full flag means we reached the trace_seq buffer
  3246. * size and we should leave by partial output condition above.
  3247. * One of the trace_seq_* functions is not used properly.
  3248. */
  3249. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  3250. iter->ent->type);
  3251. }
  3252. trace_access_unlock(iter->cpu_file);
  3253. trace_event_read_unlock();
  3254. /* Now copy what we have to the user */
  3255. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3256. if (iter->seq.readpos >= iter->seq.len)
  3257. trace_seq_init(&iter->seq);
  3258. /*
  3259. * If there was nothing to send to user, in spite of consuming trace
  3260. * entries, go back to wait for more entries.
  3261. */
  3262. if (sret == -EBUSY)
  3263. goto waitagain;
  3264. out:
  3265. mutex_unlock(&iter->mutex);
  3266. return sret;
  3267. }
  3268. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  3269. struct pipe_buffer *buf)
  3270. {
  3271. __free_page(buf->page);
  3272. }
  3273. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  3274. unsigned int idx)
  3275. {
  3276. __free_page(spd->pages[idx]);
  3277. }
  3278. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  3279. .can_merge = 0,
  3280. .map = generic_pipe_buf_map,
  3281. .unmap = generic_pipe_buf_unmap,
  3282. .confirm = generic_pipe_buf_confirm,
  3283. .release = tracing_pipe_buf_release,
  3284. .steal = generic_pipe_buf_steal,
  3285. .get = generic_pipe_buf_get,
  3286. };
  3287. static size_t
  3288. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  3289. {
  3290. size_t count;
  3291. int ret;
  3292. /* Seq buffer is page-sized, exactly what we need. */
  3293. for (;;) {
  3294. count = iter->seq.len;
  3295. ret = print_trace_line(iter);
  3296. count = iter->seq.len - count;
  3297. if (rem < count) {
  3298. rem = 0;
  3299. iter->seq.len -= count;
  3300. break;
  3301. }
  3302. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3303. iter->seq.len -= count;
  3304. break;
  3305. }
  3306. if (ret != TRACE_TYPE_NO_CONSUME)
  3307. trace_consume(iter);
  3308. rem -= count;
  3309. if (!trace_find_next_entry_inc(iter)) {
  3310. rem = 0;
  3311. iter->ent = NULL;
  3312. break;
  3313. }
  3314. }
  3315. return rem;
  3316. }
  3317. static ssize_t tracing_splice_read_pipe(struct file *filp,
  3318. loff_t *ppos,
  3319. struct pipe_inode_info *pipe,
  3320. size_t len,
  3321. unsigned int flags)
  3322. {
  3323. struct page *pages_def[PIPE_DEF_BUFFERS];
  3324. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3325. struct trace_iterator *iter = filp->private_data;
  3326. struct splice_pipe_desc spd = {
  3327. .pages = pages_def,
  3328. .partial = partial_def,
  3329. .nr_pages = 0, /* This gets updated below. */
  3330. .nr_pages_max = PIPE_DEF_BUFFERS,
  3331. .flags = flags,
  3332. .ops = &tracing_pipe_buf_ops,
  3333. .spd_release = tracing_spd_release_pipe,
  3334. };
  3335. struct trace_array *tr = iter->tr;
  3336. ssize_t ret;
  3337. size_t rem;
  3338. unsigned int i;
  3339. if (splice_grow_spd(pipe, &spd))
  3340. return -ENOMEM;
  3341. /* copy the tracer to avoid using a global lock all around */
  3342. mutex_lock(&trace_types_lock);
  3343. if (unlikely(iter->trace->name != tr->current_trace->name))
  3344. *iter->trace = *tr->current_trace;
  3345. mutex_unlock(&trace_types_lock);
  3346. mutex_lock(&iter->mutex);
  3347. if (iter->trace->splice_read) {
  3348. ret = iter->trace->splice_read(iter, filp,
  3349. ppos, pipe, len, flags);
  3350. if (ret)
  3351. goto out_err;
  3352. }
  3353. ret = tracing_wait_pipe(filp);
  3354. if (ret <= 0)
  3355. goto out_err;
  3356. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  3357. ret = -EFAULT;
  3358. goto out_err;
  3359. }
  3360. trace_event_read_lock();
  3361. trace_access_lock(iter->cpu_file);
  3362. /* Fill as many pages as possible. */
  3363. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  3364. spd.pages[i] = alloc_page(GFP_KERNEL);
  3365. if (!spd.pages[i])
  3366. break;
  3367. rem = tracing_fill_pipe_page(rem, iter);
  3368. /* Copy the data into the page, so we can start over. */
  3369. ret = trace_seq_to_buffer(&iter->seq,
  3370. page_address(spd.pages[i]),
  3371. iter->seq.len);
  3372. if (ret < 0) {
  3373. __free_page(spd.pages[i]);
  3374. break;
  3375. }
  3376. spd.partial[i].offset = 0;
  3377. spd.partial[i].len = iter->seq.len;
  3378. trace_seq_init(&iter->seq);
  3379. }
  3380. trace_access_unlock(iter->cpu_file);
  3381. trace_event_read_unlock();
  3382. mutex_unlock(&iter->mutex);
  3383. spd.nr_pages = i;
  3384. ret = splice_to_pipe(pipe, &spd);
  3385. out:
  3386. splice_shrink_spd(&spd);
  3387. return ret;
  3388. out_err:
  3389. mutex_unlock(&iter->mutex);
  3390. goto out;
  3391. }
  3392. static ssize_t
  3393. tracing_entries_read(struct file *filp, char __user *ubuf,
  3394. size_t cnt, loff_t *ppos)
  3395. {
  3396. struct trace_cpu *tc = filp->private_data;
  3397. struct trace_array *tr = tc->tr;
  3398. char buf[64];
  3399. int r = 0;
  3400. ssize_t ret;
  3401. mutex_lock(&trace_types_lock);
  3402. if (tc->cpu == RING_BUFFER_ALL_CPUS) {
  3403. int cpu, buf_size_same;
  3404. unsigned long size;
  3405. size = 0;
  3406. buf_size_same = 1;
  3407. /* check if all cpu sizes are same */
  3408. for_each_tracing_cpu(cpu) {
  3409. /* fill in the size from first enabled cpu */
  3410. if (size == 0)
  3411. size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
  3412. if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
  3413. buf_size_same = 0;
  3414. break;
  3415. }
  3416. }
  3417. if (buf_size_same) {
  3418. if (!ring_buffer_expanded)
  3419. r = sprintf(buf, "%lu (expanded: %lu)\n",
  3420. size >> 10,
  3421. trace_buf_size >> 10);
  3422. else
  3423. r = sprintf(buf, "%lu\n", size >> 10);
  3424. } else
  3425. r = sprintf(buf, "X\n");
  3426. } else
  3427. r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
  3428. mutex_unlock(&trace_types_lock);
  3429. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3430. return ret;
  3431. }
  3432. static ssize_t
  3433. tracing_entries_write(struct file *filp, const char __user *ubuf,
  3434. size_t cnt, loff_t *ppos)
  3435. {
  3436. struct trace_cpu *tc = filp->private_data;
  3437. unsigned long val;
  3438. int ret;
  3439. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3440. if (ret)
  3441. return ret;
  3442. /* must have at least 1 entry */
  3443. if (!val)
  3444. return -EINVAL;
  3445. /* value is in KB */
  3446. val <<= 10;
  3447. ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
  3448. if (ret < 0)
  3449. return ret;
  3450. *ppos += cnt;
  3451. return cnt;
  3452. }
  3453. static ssize_t
  3454. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  3455. size_t cnt, loff_t *ppos)
  3456. {
  3457. struct trace_array *tr = filp->private_data;
  3458. char buf[64];
  3459. int r, cpu;
  3460. unsigned long size = 0, expanded_size = 0;
  3461. mutex_lock(&trace_types_lock);
  3462. for_each_tracing_cpu(cpu) {
  3463. size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
  3464. if (!ring_buffer_expanded)
  3465. expanded_size += trace_buf_size >> 10;
  3466. }
  3467. if (ring_buffer_expanded)
  3468. r = sprintf(buf, "%lu\n", size);
  3469. else
  3470. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  3471. mutex_unlock(&trace_types_lock);
  3472. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3473. }
  3474. static ssize_t
  3475. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  3476. size_t cnt, loff_t *ppos)
  3477. {
  3478. /*
  3479. * There is no need to read what the user has written, this function
  3480. * is just to make sure that there is no error when "echo" is used
  3481. */
  3482. *ppos += cnt;
  3483. return cnt;
  3484. }
  3485. static int
  3486. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  3487. {
  3488. struct trace_array *tr = inode->i_private;
  3489. /* disable tracing ? */
  3490. if (trace_flags & TRACE_ITER_STOP_ON_FREE)
  3491. tracing_off();
  3492. /* resize the ring buffer to 0 */
  3493. tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
  3494. return 0;
  3495. }
  3496. static ssize_t
  3497. tracing_mark_write(struct file *filp, const char __user *ubuf,
  3498. size_t cnt, loff_t *fpos)
  3499. {
  3500. unsigned long addr = (unsigned long)ubuf;
  3501. struct ring_buffer_event *event;
  3502. struct ring_buffer *buffer;
  3503. struct print_entry *entry;
  3504. unsigned long irq_flags;
  3505. struct page *pages[2];
  3506. void *map_page[2];
  3507. int nr_pages = 1;
  3508. ssize_t written;
  3509. int offset;
  3510. int size;
  3511. int len;
  3512. int ret;
  3513. int i;
  3514. if (tracing_disabled)
  3515. return -EINVAL;
  3516. if (!(trace_flags & TRACE_ITER_MARKERS))
  3517. return -EINVAL;
  3518. if (cnt > TRACE_BUF_SIZE)
  3519. cnt = TRACE_BUF_SIZE;
  3520. /*
  3521. * Userspace is injecting traces into the kernel trace buffer.
  3522. * We want to be as non intrusive as possible.
  3523. * To do so, we do not want to allocate any special buffers
  3524. * or take any locks, but instead write the userspace data
  3525. * straight into the ring buffer.
  3526. *
  3527. * First we need to pin the userspace buffer into memory,
  3528. * which, most likely it is, because it just referenced it.
  3529. * But there's no guarantee that it is. By using get_user_pages_fast()
  3530. * and kmap_atomic/kunmap_atomic() we can get access to the
  3531. * pages directly. We then write the data directly into the
  3532. * ring buffer.
  3533. */
  3534. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  3535. /* check if we cross pages */
  3536. if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
  3537. nr_pages = 2;
  3538. offset = addr & (PAGE_SIZE - 1);
  3539. addr &= PAGE_MASK;
  3540. ret = get_user_pages_fast(addr, nr_pages, 0, pages);
  3541. if (ret < nr_pages) {
  3542. while (--ret >= 0)
  3543. put_page(pages[ret]);
  3544. written = -EFAULT;
  3545. goto out;
  3546. }
  3547. for (i = 0; i < nr_pages; i++)
  3548. map_page[i] = kmap_atomic(pages[i]);
  3549. local_save_flags(irq_flags);
  3550. size = sizeof(*entry) + cnt + 2; /* possible \n added */
  3551. buffer = global_trace.trace_buffer.buffer;
  3552. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  3553. irq_flags, preempt_count());
  3554. if (!event) {
  3555. /* Ring buffer disabled, return as if not open for write */
  3556. written = -EBADF;
  3557. goto out_unlock;
  3558. }
  3559. entry = ring_buffer_event_data(event);
  3560. entry->ip = _THIS_IP_;
  3561. if (nr_pages == 2) {
  3562. len = PAGE_SIZE - offset;
  3563. memcpy(&entry->buf, map_page[0] + offset, len);
  3564. memcpy(&entry->buf[len], map_page[1], cnt - len);
  3565. } else
  3566. memcpy(&entry->buf, map_page[0] + offset, cnt);
  3567. if (entry->buf[cnt - 1] != '\n') {
  3568. entry->buf[cnt] = '\n';
  3569. entry->buf[cnt + 1] = '\0';
  3570. } else
  3571. entry->buf[cnt] = '\0';
  3572. __buffer_unlock_commit(buffer, event);
  3573. written = cnt;
  3574. *fpos += written;
  3575. out_unlock:
  3576. for (i = 0; i < nr_pages; i++){
  3577. kunmap_atomic(map_page[i]);
  3578. put_page(pages[i]);
  3579. }
  3580. out:
  3581. return written;
  3582. }
  3583. static int tracing_clock_show(struct seq_file *m, void *v)
  3584. {
  3585. struct trace_array *tr = m->private;
  3586. int i;
  3587. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  3588. seq_printf(m,
  3589. "%s%s%s%s", i ? " " : "",
  3590. i == tr->clock_id ? "[" : "", trace_clocks[i].name,
  3591. i == tr->clock_id ? "]" : "");
  3592. seq_putc(m, '\n');
  3593. return 0;
  3594. }
  3595. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  3596. size_t cnt, loff_t *fpos)
  3597. {
  3598. struct seq_file *m = filp->private_data;
  3599. struct trace_array *tr = m->private;
  3600. char buf[64];
  3601. const char *clockstr;
  3602. int i;
  3603. if (cnt >= sizeof(buf))
  3604. return -EINVAL;
  3605. if (copy_from_user(&buf, ubuf, cnt))
  3606. return -EFAULT;
  3607. buf[cnt] = 0;
  3608. clockstr = strstrip(buf);
  3609. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  3610. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  3611. break;
  3612. }
  3613. if (i == ARRAY_SIZE(trace_clocks))
  3614. return -EINVAL;
  3615. mutex_lock(&trace_types_lock);
  3616. tr->clock_id = i;
  3617. ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
  3618. /*
  3619. * New clock may not be consistent with the previous clock.
  3620. * Reset the buffer so that it doesn't have incomparable timestamps.
  3621. */
  3622. tracing_reset_online_cpus(&global_trace.trace_buffer);
  3623. #ifdef CONFIG_TRACER_MAX_TRACE
  3624. if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
  3625. ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
  3626. tracing_reset_online_cpus(&global_trace.max_buffer);
  3627. #endif
  3628. mutex_unlock(&trace_types_lock);
  3629. *fpos += cnt;
  3630. return cnt;
  3631. }
  3632. static int tracing_clock_open(struct inode *inode, struct file *file)
  3633. {
  3634. if (tracing_disabled)
  3635. return -ENODEV;
  3636. return single_open(file, tracing_clock_show, inode->i_private);
  3637. }
  3638. struct ftrace_buffer_info {
  3639. struct trace_iterator iter;
  3640. void *spare;
  3641. unsigned int read;
  3642. };
  3643. #ifdef CONFIG_TRACER_SNAPSHOT
  3644. static int tracing_snapshot_open(struct inode *inode, struct file *file)
  3645. {
  3646. struct trace_cpu *tc = inode->i_private;
  3647. struct trace_iterator *iter;
  3648. struct seq_file *m;
  3649. int ret = 0;
  3650. if (file->f_mode & FMODE_READ) {
  3651. iter = __tracing_open(inode, file, true);
  3652. if (IS_ERR(iter))
  3653. ret = PTR_ERR(iter);
  3654. } else {
  3655. /* Writes still need the seq_file to hold the private data */
  3656. m = kzalloc(sizeof(*m), GFP_KERNEL);
  3657. if (!m)
  3658. return -ENOMEM;
  3659. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3660. if (!iter) {
  3661. kfree(m);
  3662. return -ENOMEM;
  3663. }
  3664. iter->tr = tc->tr;
  3665. iter->trace_buffer = &tc->tr->max_buffer;
  3666. iter->cpu_file = tc->cpu;
  3667. m->private = iter;
  3668. file->private_data = m;
  3669. }
  3670. return ret;
  3671. }
  3672. static ssize_t
  3673. tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3674. loff_t *ppos)
  3675. {
  3676. struct seq_file *m = filp->private_data;
  3677. struct trace_iterator *iter = m->private;
  3678. struct trace_array *tr = iter->tr;
  3679. unsigned long val;
  3680. int ret;
  3681. ret = tracing_update_buffers();
  3682. if (ret < 0)
  3683. return ret;
  3684. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3685. if (ret)
  3686. return ret;
  3687. mutex_lock(&trace_types_lock);
  3688. if (tr->current_trace->use_max_tr) {
  3689. ret = -EBUSY;
  3690. goto out;
  3691. }
  3692. switch (val) {
  3693. case 0:
  3694. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3695. ret = -EINVAL;
  3696. break;
  3697. }
  3698. if (tr->allocated_snapshot)
  3699. free_snapshot(tr);
  3700. break;
  3701. case 1:
  3702. /* Only allow per-cpu swap if the ring buffer supports it */
  3703. #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
  3704. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3705. ret = -EINVAL;
  3706. break;
  3707. }
  3708. #endif
  3709. if (!tr->allocated_snapshot) {
  3710. ret = alloc_snapshot(tr);
  3711. if (ret < 0)
  3712. break;
  3713. }
  3714. local_irq_disable();
  3715. /* Now, we're going to swap */
  3716. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3717. update_max_tr(tr, current, smp_processor_id());
  3718. else
  3719. update_max_tr_single(tr, current, iter->cpu_file);
  3720. local_irq_enable();
  3721. break;
  3722. default:
  3723. if (tr->allocated_snapshot) {
  3724. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3725. tracing_reset_online_cpus(&tr->max_buffer);
  3726. else
  3727. tracing_reset(&tr->max_buffer, iter->cpu_file);
  3728. }
  3729. break;
  3730. }
  3731. if (ret >= 0) {
  3732. *ppos += cnt;
  3733. ret = cnt;
  3734. }
  3735. out:
  3736. mutex_unlock(&trace_types_lock);
  3737. return ret;
  3738. }
  3739. static int tracing_snapshot_release(struct inode *inode, struct file *file)
  3740. {
  3741. struct seq_file *m = file->private_data;
  3742. if (file->f_mode & FMODE_READ)
  3743. return tracing_release(inode, file);
  3744. /* If write only, the seq_file is just a stub */
  3745. if (m)
  3746. kfree(m->private);
  3747. kfree(m);
  3748. return 0;
  3749. }
  3750. static int tracing_buffers_open(struct inode *inode, struct file *filp);
  3751. static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
  3752. size_t count, loff_t *ppos);
  3753. static int tracing_buffers_release(struct inode *inode, struct file *file);
  3754. static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3755. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  3756. static int snapshot_raw_open(struct inode *inode, struct file *filp)
  3757. {
  3758. struct ftrace_buffer_info *info;
  3759. int ret;
  3760. ret = tracing_buffers_open(inode, filp);
  3761. if (ret < 0)
  3762. return ret;
  3763. info = filp->private_data;
  3764. if (info->iter.trace->use_max_tr) {
  3765. tracing_buffers_release(inode, filp);
  3766. return -EBUSY;
  3767. }
  3768. info->iter.snapshot = true;
  3769. info->iter.trace_buffer = &info->iter.tr->max_buffer;
  3770. return ret;
  3771. }
  3772. #endif /* CONFIG_TRACER_SNAPSHOT */
  3773. static const struct file_operations tracing_max_lat_fops = {
  3774. .open = tracing_open_generic,
  3775. .read = tracing_max_lat_read,
  3776. .write = tracing_max_lat_write,
  3777. .llseek = generic_file_llseek,
  3778. };
  3779. static const struct file_operations set_tracer_fops = {
  3780. .open = tracing_open_generic,
  3781. .read = tracing_set_trace_read,
  3782. .write = tracing_set_trace_write,
  3783. .llseek = generic_file_llseek,
  3784. };
  3785. static const struct file_operations tracing_pipe_fops = {
  3786. .open = tracing_open_pipe,
  3787. .poll = tracing_poll_pipe,
  3788. .read = tracing_read_pipe,
  3789. .splice_read = tracing_splice_read_pipe,
  3790. .release = tracing_release_pipe,
  3791. .llseek = no_llseek,
  3792. };
  3793. static const struct file_operations tracing_entries_fops = {
  3794. .open = tracing_open_generic,
  3795. .read = tracing_entries_read,
  3796. .write = tracing_entries_write,
  3797. .llseek = generic_file_llseek,
  3798. };
  3799. static const struct file_operations tracing_total_entries_fops = {
  3800. .open = tracing_open_generic,
  3801. .read = tracing_total_entries_read,
  3802. .llseek = generic_file_llseek,
  3803. };
  3804. static const struct file_operations tracing_free_buffer_fops = {
  3805. .write = tracing_free_buffer_write,
  3806. .release = tracing_free_buffer_release,
  3807. };
  3808. static const struct file_operations tracing_mark_fops = {
  3809. .open = tracing_open_generic,
  3810. .write = tracing_mark_write,
  3811. .llseek = generic_file_llseek,
  3812. };
  3813. static const struct file_operations trace_clock_fops = {
  3814. .open = tracing_clock_open,
  3815. .read = seq_read,
  3816. .llseek = seq_lseek,
  3817. .release = single_release,
  3818. .write = tracing_clock_write,
  3819. };
  3820. #ifdef CONFIG_TRACER_SNAPSHOT
  3821. static const struct file_operations snapshot_fops = {
  3822. .open = tracing_snapshot_open,
  3823. .read = seq_read,
  3824. .write = tracing_snapshot_write,
  3825. .llseek = tracing_seek,
  3826. .release = tracing_snapshot_release,
  3827. };
  3828. static const struct file_operations snapshot_raw_fops = {
  3829. .open = snapshot_raw_open,
  3830. .read = tracing_buffers_read,
  3831. .release = tracing_buffers_release,
  3832. .splice_read = tracing_buffers_splice_read,
  3833. .llseek = no_llseek,
  3834. };
  3835. #endif /* CONFIG_TRACER_SNAPSHOT */
  3836. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  3837. {
  3838. struct trace_cpu *tc = inode->i_private;
  3839. struct trace_array *tr = tc->tr;
  3840. struct ftrace_buffer_info *info;
  3841. if (tracing_disabled)
  3842. return -ENODEV;
  3843. info = kzalloc(sizeof(*info), GFP_KERNEL);
  3844. if (!info)
  3845. return -ENOMEM;
  3846. mutex_lock(&trace_types_lock);
  3847. tr->ref++;
  3848. info->iter.tr = tr;
  3849. info->iter.cpu_file = tc->cpu;
  3850. info->iter.trace = tr->current_trace;
  3851. info->iter.trace_buffer = &tr->trace_buffer;
  3852. info->spare = NULL;
  3853. /* Force reading ring buffer for first read */
  3854. info->read = (unsigned int)-1;
  3855. filp->private_data = info;
  3856. mutex_unlock(&trace_types_lock);
  3857. return nonseekable_open(inode, filp);
  3858. }
  3859. static unsigned int
  3860. tracing_buffers_poll(struct file *filp, poll_table *poll_table)
  3861. {
  3862. struct ftrace_buffer_info *info = filp->private_data;
  3863. struct trace_iterator *iter = &info->iter;
  3864. return trace_poll(iter, filp, poll_table);
  3865. }
  3866. static ssize_t
  3867. tracing_buffers_read(struct file *filp, char __user *ubuf,
  3868. size_t count, loff_t *ppos)
  3869. {
  3870. struct ftrace_buffer_info *info = filp->private_data;
  3871. struct trace_iterator *iter = &info->iter;
  3872. ssize_t ret;
  3873. ssize_t size;
  3874. if (!count)
  3875. return 0;
  3876. mutex_lock(&trace_types_lock);
  3877. #ifdef CONFIG_TRACER_MAX_TRACE
  3878. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  3879. size = -EBUSY;
  3880. goto out_unlock;
  3881. }
  3882. #endif
  3883. if (!info->spare)
  3884. info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
  3885. iter->cpu_file);
  3886. size = -ENOMEM;
  3887. if (!info->spare)
  3888. goto out_unlock;
  3889. /* Do we have previous read data to read? */
  3890. if (info->read < PAGE_SIZE)
  3891. goto read;
  3892. again:
  3893. trace_access_lock(iter->cpu_file);
  3894. ret = ring_buffer_read_page(iter->trace_buffer->buffer,
  3895. &info->spare,
  3896. count,
  3897. iter->cpu_file, 0);
  3898. trace_access_unlock(iter->cpu_file);
  3899. if (ret < 0) {
  3900. if (trace_empty(iter)) {
  3901. if ((filp->f_flags & O_NONBLOCK)) {
  3902. size = -EAGAIN;
  3903. goto out_unlock;
  3904. }
  3905. mutex_unlock(&trace_types_lock);
  3906. iter->trace->wait_pipe(iter);
  3907. mutex_lock(&trace_types_lock);
  3908. if (signal_pending(current)) {
  3909. size = -EINTR;
  3910. goto out_unlock;
  3911. }
  3912. goto again;
  3913. }
  3914. size = 0;
  3915. goto out_unlock;
  3916. }
  3917. info->read = 0;
  3918. read:
  3919. size = PAGE_SIZE - info->read;
  3920. if (size > count)
  3921. size = count;
  3922. ret = copy_to_user(ubuf, info->spare + info->read, size);
  3923. if (ret == size) {
  3924. size = -EFAULT;
  3925. goto out_unlock;
  3926. }
  3927. size -= ret;
  3928. *ppos += size;
  3929. info->read += size;
  3930. out_unlock:
  3931. mutex_unlock(&trace_types_lock);
  3932. return size;
  3933. }
  3934. static int tracing_buffers_release(struct inode *inode, struct file *file)
  3935. {
  3936. struct ftrace_buffer_info *info = file->private_data;
  3937. struct trace_iterator *iter = &info->iter;
  3938. mutex_lock(&trace_types_lock);
  3939. WARN_ON(!iter->tr->ref);
  3940. iter->tr->ref--;
  3941. if (info->spare)
  3942. ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
  3943. kfree(info);
  3944. mutex_unlock(&trace_types_lock);
  3945. return 0;
  3946. }
  3947. struct buffer_ref {
  3948. struct ring_buffer *buffer;
  3949. void *page;
  3950. int ref;
  3951. };
  3952. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  3953. struct pipe_buffer *buf)
  3954. {
  3955. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3956. if (--ref->ref)
  3957. return;
  3958. ring_buffer_free_read_page(ref->buffer, ref->page);
  3959. kfree(ref);
  3960. buf->private = 0;
  3961. }
  3962. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  3963. struct pipe_buffer *buf)
  3964. {
  3965. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3966. ref->ref++;
  3967. }
  3968. /* Pipe buffer operations for a buffer. */
  3969. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  3970. .can_merge = 0,
  3971. .map = generic_pipe_buf_map,
  3972. .unmap = generic_pipe_buf_unmap,
  3973. .confirm = generic_pipe_buf_confirm,
  3974. .release = buffer_pipe_buf_release,
  3975. .steal = generic_pipe_buf_steal,
  3976. .get = buffer_pipe_buf_get,
  3977. };
  3978. /*
  3979. * Callback from splice_to_pipe(), if we need to release some pages
  3980. * at the end of the spd in case we error'ed out in filling the pipe.
  3981. */
  3982. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  3983. {
  3984. struct buffer_ref *ref =
  3985. (struct buffer_ref *)spd->partial[i].private;
  3986. if (--ref->ref)
  3987. return;
  3988. ring_buffer_free_read_page(ref->buffer, ref->page);
  3989. kfree(ref);
  3990. spd->partial[i].private = 0;
  3991. }
  3992. static ssize_t
  3993. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3994. struct pipe_inode_info *pipe, size_t len,
  3995. unsigned int flags)
  3996. {
  3997. struct ftrace_buffer_info *info = file->private_data;
  3998. struct trace_iterator *iter = &info->iter;
  3999. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  4000. struct page *pages_def[PIPE_DEF_BUFFERS];
  4001. struct splice_pipe_desc spd = {
  4002. .pages = pages_def,
  4003. .partial = partial_def,
  4004. .nr_pages_max = PIPE_DEF_BUFFERS,
  4005. .flags = flags,
  4006. .ops = &buffer_pipe_buf_ops,
  4007. .spd_release = buffer_spd_release,
  4008. };
  4009. struct buffer_ref *ref;
  4010. int entries, size, i;
  4011. ssize_t ret;
  4012. mutex_lock(&trace_types_lock);
  4013. #ifdef CONFIG_TRACER_MAX_TRACE
  4014. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  4015. ret = -EBUSY;
  4016. goto out;
  4017. }
  4018. #endif
  4019. if (splice_grow_spd(pipe, &spd)) {
  4020. ret = -ENOMEM;
  4021. goto out;
  4022. }
  4023. if (*ppos & (PAGE_SIZE - 1)) {
  4024. ret = -EINVAL;
  4025. goto out;
  4026. }
  4027. if (len & (PAGE_SIZE - 1)) {
  4028. if (len < PAGE_SIZE) {
  4029. ret = -EINVAL;
  4030. goto out;
  4031. }
  4032. len &= PAGE_MASK;
  4033. }
  4034. again:
  4035. trace_access_lock(iter->cpu_file);
  4036. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  4037. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  4038. struct page *page;
  4039. int r;
  4040. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  4041. if (!ref)
  4042. break;
  4043. ref->ref = 1;
  4044. ref->buffer = iter->trace_buffer->buffer;
  4045. ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
  4046. if (!ref->page) {
  4047. kfree(ref);
  4048. break;
  4049. }
  4050. r = ring_buffer_read_page(ref->buffer, &ref->page,
  4051. len, iter->cpu_file, 1);
  4052. if (r < 0) {
  4053. ring_buffer_free_read_page(ref->buffer, ref->page);
  4054. kfree(ref);
  4055. break;
  4056. }
  4057. /*
  4058. * zero out any left over data, this is going to
  4059. * user land.
  4060. */
  4061. size = ring_buffer_page_len(ref->page);
  4062. if (size < PAGE_SIZE)
  4063. memset(ref->page + size, 0, PAGE_SIZE - size);
  4064. page = virt_to_page(ref->page);
  4065. spd.pages[i] = page;
  4066. spd.partial[i].len = PAGE_SIZE;
  4067. spd.partial[i].offset = 0;
  4068. spd.partial[i].private = (unsigned long)ref;
  4069. spd.nr_pages++;
  4070. *ppos += PAGE_SIZE;
  4071. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  4072. }
  4073. trace_access_unlock(iter->cpu_file);
  4074. spd.nr_pages = i;
  4075. /* did we read anything? */
  4076. if (!spd.nr_pages) {
  4077. if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
  4078. ret = -EAGAIN;
  4079. goto out;
  4080. }
  4081. mutex_unlock(&trace_types_lock);
  4082. iter->trace->wait_pipe(iter);
  4083. mutex_lock(&trace_types_lock);
  4084. if (signal_pending(current)) {
  4085. ret = -EINTR;
  4086. goto out;
  4087. }
  4088. goto again;
  4089. }
  4090. ret = splice_to_pipe(pipe, &spd);
  4091. splice_shrink_spd(&spd);
  4092. out:
  4093. mutex_unlock(&trace_types_lock);
  4094. return ret;
  4095. }
  4096. static const struct file_operations tracing_buffers_fops = {
  4097. .open = tracing_buffers_open,
  4098. .read = tracing_buffers_read,
  4099. .poll = tracing_buffers_poll,
  4100. .release = tracing_buffers_release,
  4101. .splice_read = tracing_buffers_splice_read,
  4102. .llseek = no_llseek,
  4103. };
  4104. static ssize_t
  4105. tracing_stats_read(struct file *filp, char __user *ubuf,
  4106. size_t count, loff_t *ppos)
  4107. {
  4108. struct trace_cpu *tc = filp->private_data;
  4109. struct trace_array *tr = tc->tr;
  4110. struct trace_buffer *trace_buf = &tr->trace_buffer;
  4111. struct trace_seq *s;
  4112. unsigned long cnt;
  4113. unsigned long long t;
  4114. unsigned long usec_rem;
  4115. int cpu = tc->cpu;
  4116. s = kmalloc(sizeof(*s), GFP_KERNEL);
  4117. if (!s)
  4118. return -ENOMEM;
  4119. trace_seq_init(s);
  4120. cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
  4121. trace_seq_printf(s, "entries: %ld\n", cnt);
  4122. cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
  4123. trace_seq_printf(s, "overrun: %ld\n", cnt);
  4124. cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
  4125. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  4126. cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
  4127. trace_seq_printf(s, "bytes: %ld\n", cnt);
  4128. if (trace_clocks[trace_clock_id].in_ns) {
  4129. /* local or global for trace_clock */
  4130. t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4131. usec_rem = do_div(t, USEC_PER_SEC);
  4132. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
  4133. t, usec_rem);
  4134. t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4135. usec_rem = do_div(t, USEC_PER_SEC);
  4136. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  4137. } else {
  4138. /* counter or tsc mode for trace_clock */
  4139. trace_seq_printf(s, "oldest event ts: %llu\n",
  4140. ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4141. trace_seq_printf(s, "now ts: %llu\n",
  4142. ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4143. }
  4144. cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
  4145. trace_seq_printf(s, "dropped events: %ld\n", cnt);
  4146. cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
  4147. trace_seq_printf(s, "read events: %ld\n", cnt);
  4148. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  4149. kfree(s);
  4150. return count;
  4151. }
  4152. static const struct file_operations tracing_stats_fops = {
  4153. .open = tracing_open_generic,
  4154. .read = tracing_stats_read,
  4155. .llseek = generic_file_llseek,
  4156. };
  4157. #ifdef CONFIG_DYNAMIC_FTRACE
  4158. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  4159. {
  4160. return 0;
  4161. }
  4162. static ssize_t
  4163. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  4164. size_t cnt, loff_t *ppos)
  4165. {
  4166. static char ftrace_dyn_info_buffer[1024];
  4167. static DEFINE_MUTEX(dyn_info_mutex);
  4168. unsigned long *p = filp->private_data;
  4169. char *buf = ftrace_dyn_info_buffer;
  4170. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  4171. int r;
  4172. mutex_lock(&dyn_info_mutex);
  4173. r = sprintf(buf, "%ld ", *p);
  4174. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  4175. buf[r++] = '\n';
  4176. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4177. mutex_unlock(&dyn_info_mutex);
  4178. return r;
  4179. }
  4180. static const struct file_operations tracing_dyn_info_fops = {
  4181. .open = tracing_open_generic,
  4182. .read = tracing_read_dyn_info,
  4183. .llseek = generic_file_llseek,
  4184. };
  4185. #endif /* CONFIG_DYNAMIC_FTRACE */
  4186. #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
  4187. static void
  4188. ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
  4189. {
  4190. tracing_snapshot();
  4191. }
  4192. static void
  4193. ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
  4194. {
  4195. unsigned long *count = (long *)data;
  4196. if (!*count)
  4197. return;
  4198. if (*count != -1)
  4199. (*count)--;
  4200. tracing_snapshot();
  4201. }
  4202. static int
  4203. ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
  4204. struct ftrace_probe_ops *ops, void *data)
  4205. {
  4206. long count = (long)data;
  4207. seq_printf(m, "%ps:", (void *)ip);
  4208. seq_printf(m, "snapshot");
  4209. if (count == -1)
  4210. seq_printf(m, ":unlimited\n");
  4211. else
  4212. seq_printf(m, ":count=%ld\n", count);
  4213. return 0;
  4214. }
  4215. static struct ftrace_probe_ops snapshot_probe_ops = {
  4216. .func = ftrace_snapshot,
  4217. .print = ftrace_snapshot_print,
  4218. };
  4219. static struct ftrace_probe_ops snapshot_count_probe_ops = {
  4220. .func = ftrace_count_snapshot,
  4221. .print = ftrace_snapshot_print,
  4222. };
  4223. static int
  4224. ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
  4225. char *glob, char *cmd, char *param, int enable)
  4226. {
  4227. struct ftrace_probe_ops *ops;
  4228. void *count = (void *)-1;
  4229. char *number;
  4230. int ret;
  4231. /* hash funcs only work with set_ftrace_filter */
  4232. if (!enable)
  4233. return -EINVAL;
  4234. ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
  4235. if (glob[0] == '!') {
  4236. unregister_ftrace_function_probe_func(glob+1, ops);
  4237. return 0;
  4238. }
  4239. if (!param)
  4240. goto out_reg;
  4241. number = strsep(&param, ":");
  4242. if (!strlen(number))
  4243. goto out_reg;
  4244. /*
  4245. * We use the callback data field (which is a pointer)
  4246. * as our counter.
  4247. */
  4248. ret = kstrtoul(number, 0, (unsigned long *)&count);
  4249. if (ret)
  4250. return ret;
  4251. out_reg:
  4252. ret = register_ftrace_function_probe(glob, ops, count);
  4253. if (ret >= 0)
  4254. alloc_snapshot(&global_trace);
  4255. return ret < 0 ? ret : 0;
  4256. }
  4257. static struct ftrace_func_command ftrace_snapshot_cmd = {
  4258. .name = "snapshot",
  4259. .func = ftrace_trace_snapshot_callback,
  4260. };
  4261. static int register_snapshot_cmd(void)
  4262. {
  4263. return register_ftrace_command(&ftrace_snapshot_cmd);
  4264. }
  4265. #else
  4266. static inline int register_snapshot_cmd(void) { return 0; }
  4267. #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
  4268. struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
  4269. {
  4270. static int once;
  4271. if (tr->dir)
  4272. return tr->dir;
  4273. if (!debugfs_initialized())
  4274. return NULL;
  4275. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  4276. tr->dir = debugfs_create_dir("tracing", NULL);
  4277. if (!tr->dir && !once) {
  4278. once = 1;
  4279. pr_warning("Could not create debugfs directory 'tracing'\n");
  4280. return NULL;
  4281. }
  4282. return tr->dir;
  4283. }
  4284. struct dentry *tracing_init_dentry(void)
  4285. {
  4286. return tracing_init_dentry_tr(&global_trace);
  4287. }
  4288. static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
  4289. {
  4290. struct dentry *d_tracer;
  4291. if (tr->percpu_dir)
  4292. return tr->percpu_dir;
  4293. d_tracer = tracing_init_dentry_tr(tr);
  4294. if (!d_tracer)
  4295. return NULL;
  4296. tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
  4297. WARN_ONCE(!tr->percpu_dir,
  4298. "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
  4299. return tr->percpu_dir;
  4300. }
  4301. static void
  4302. tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
  4303. {
  4304. struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  4305. struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
  4306. struct dentry *d_cpu;
  4307. char cpu_dir[30]; /* 30 characters should be more than enough */
  4308. if (!d_percpu)
  4309. return;
  4310. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  4311. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  4312. if (!d_cpu) {
  4313. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  4314. return;
  4315. }
  4316. /* per cpu trace_pipe */
  4317. trace_create_file("trace_pipe", 0444, d_cpu,
  4318. (void *)&data->trace_cpu, &tracing_pipe_fops);
  4319. /* per cpu trace */
  4320. trace_create_file("trace", 0644, d_cpu,
  4321. (void *)&data->trace_cpu, &tracing_fops);
  4322. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  4323. (void *)&data->trace_cpu, &tracing_buffers_fops);
  4324. trace_create_file("stats", 0444, d_cpu,
  4325. (void *)&data->trace_cpu, &tracing_stats_fops);
  4326. trace_create_file("buffer_size_kb", 0444, d_cpu,
  4327. (void *)&data->trace_cpu, &tracing_entries_fops);
  4328. #ifdef CONFIG_TRACER_SNAPSHOT
  4329. trace_create_file("snapshot", 0644, d_cpu,
  4330. (void *)&data->trace_cpu, &snapshot_fops);
  4331. trace_create_file("snapshot_raw", 0444, d_cpu,
  4332. (void *)&data->trace_cpu, &snapshot_raw_fops);
  4333. #endif
  4334. }
  4335. #ifdef CONFIG_FTRACE_SELFTEST
  4336. /* Let selftest have access to static functions in this file */
  4337. #include "trace_selftest.c"
  4338. #endif
  4339. struct trace_option_dentry {
  4340. struct tracer_opt *opt;
  4341. struct tracer_flags *flags;
  4342. struct trace_array *tr;
  4343. struct dentry *entry;
  4344. };
  4345. static ssize_t
  4346. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  4347. loff_t *ppos)
  4348. {
  4349. struct trace_option_dentry *topt = filp->private_data;
  4350. char *buf;
  4351. if (topt->flags->val & topt->opt->bit)
  4352. buf = "1\n";
  4353. else
  4354. buf = "0\n";
  4355. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4356. }
  4357. static ssize_t
  4358. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4359. loff_t *ppos)
  4360. {
  4361. struct trace_option_dentry *topt = filp->private_data;
  4362. unsigned long val;
  4363. int ret;
  4364. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4365. if (ret)
  4366. return ret;
  4367. if (val != 0 && val != 1)
  4368. return -EINVAL;
  4369. if (!!(topt->flags->val & topt->opt->bit) != val) {
  4370. mutex_lock(&trace_types_lock);
  4371. ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
  4372. topt->opt, !val);
  4373. mutex_unlock(&trace_types_lock);
  4374. if (ret)
  4375. return ret;
  4376. }
  4377. *ppos += cnt;
  4378. return cnt;
  4379. }
  4380. static const struct file_operations trace_options_fops = {
  4381. .open = tracing_open_generic,
  4382. .read = trace_options_read,
  4383. .write = trace_options_write,
  4384. .llseek = generic_file_llseek,
  4385. };
  4386. static ssize_t
  4387. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  4388. loff_t *ppos)
  4389. {
  4390. long index = (long)filp->private_data;
  4391. char *buf;
  4392. if (trace_flags & (1 << index))
  4393. buf = "1\n";
  4394. else
  4395. buf = "0\n";
  4396. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4397. }
  4398. static ssize_t
  4399. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4400. loff_t *ppos)
  4401. {
  4402. struct trace_array *tr = &global_trace;
  4403. long index = (long)filp->private_data;
  4404. unsigned long val;
  4405. int ret;
  4406. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4407. if (ret)
  4408. return ret;
  4409. if (val != 0 && val != 1)
  4410. return -EINVAL;
  4411. mutex_lock(&trace_types_lock);
  4412. ret = set_tracer_flag(tr, 1 << index, val);
  4413. mutex_unlock(&trace_types_lock);
  4414. if (ret < 0)
  4415. return ret;
  4416. *ppos += cnt;
  4417. return cnt;
  4418. }
  4419. static const struct file_operations trace_options_core_fops = {
  4420. .open = tracing_open_generic,
  4421. .read = trace_options_core_read,
  4422. .write = trace_options_core_write,
  4423. .llseek = generic_file_llseek,
  4424. };
  4425. struct dentry *trace_create_file(const char *name,
  4426. umode_t mode,
  4427. struct dentry *parent,
  4428. void *data,
  4429. const struct file_operations *fops)
  4430. {
  4431. struct dentry *ret;
  4432. ret = debugfs_create_file(name, mode, parent, data, fops);
  4433. if (!ret)
  4434. pr_warning("Could not create debugfs '%s' entry\n", name);
  4435. return ret;
  4436. }
  4437. static struct dentry *trace_options_init_dentry(struct trace_array *tr)
  4438. {
  4439. struct dentry *d_tracer;
  4440. if (tr->options)
  4441. return tr->options;
  4442. d_tracer = tracing_init_dentry_tr(tr);
  4443. if (!d_tracer)
  4444. return NULL;
  4445. tr->options = debugfs_create_dir("options", d_tracer);
  4446. if (!tr->options) {
  4447. pr_warning("Could not create debugfs directory 'options'\n");
  4448. return NULL;
  4449. }
  4450. return tr->options;
  4451. }
  4452. static void
  4453. create_trace_option_file(struct trace_array *tr,
  4454. struct trace_option_dentry *topt,
  4455. struct tracer_flags *flags,
  4456. struct tracer_opt *opt)
  4457. {
  4458. struct dentry *t_options;
  4459. t_options = trace_options_init_dentry(tr);
  4460. if (!t_options)
  4461. return;
  4462. topt->flags = flags;
  4463. topt->opt = opt;
  4464. topt->tr = tr;
  4465. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  4466. &trace_options_fops);
  4467. }
  4468. static struct trace_option_dentry *
  4469. create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
  4470. {
  4471. struct trace_option_dentry *topts;
  4472. struct tracer_flags *flags;
  4473. struct tracer_opt *opts;
  4474. int cnt;
  4475. if (!tracer)
  4476. return NULL;
  4477. flags = tracer->flags;
  4478. if (!flags || !flags->opts)
  4479. return NULL;
  4480. opts = flags->opts;
  4481. for (cnt = 0; opts[cnt].name; cnt++)
  4482. ;
  4483. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  4484. if (!topts)
  4485. return NULL;
  4486. for (cnt = 0; opts[cnt].name; cnt++)
  4487. create_trace_option_file(tr, &topts[cnt], flags,
  4488. &opts[cnt]);
  4489. return topts;
  4490. }
  4491. static void
  4492. destroy_trace_option_files(struct trace_option_dentry *topts)
  4493. {
  4494. int cnt;
  4495. if (!topts)
  4496. return;
  4497. for (cnt = 0; topts[cnt].opt; cnt++) {
  4498. if (topts[cnt].entry)
  4499. debugfs_remove(topts[cnt].entry);
  4500. }
  4501. kfree(topts);
  4502. }
  4503. static struct dentry *
  4504. create_trace_option_core_file(struct trace_array *tr,
  4505. const char *option, long index)
  4506. {
  4507. struct dentry *t_options;
  4508. t_options = trace_options_init_dentry(tr);
  4509. if (!t_options)
  4510. return NULL;
  4511. return trace_create_file(option, 0644, t_options, (void *)index,
  4512. &trace_options_core_fops);
  4513. }
  4514. static __init void create_trace_options_dir(struct trace_array *tr)
  4515. {
  4516. struct dentry *t_options;
  4517. int i;
  4518. t_options = trace_options_init_dentry(tr);
  4519. if (!t_options)
  4520. return;
  4521. for (i = 0; trace_options[i]; i++)
  4522. create_trace_option_core_file(tr, trace_options[i], i);
  4523. }
  4524. static ssize_t
  4525. rb_simple_read(struct file *filp, char __user *ubuf,
  4526. size_t cnt, loff_t *ppos)
  4527. {
  4528. struct trace_array *tr = filp->private_data;
  4529. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4530. char buf[64];
  4531. int r;
  4532. if (buffer)
  4533. r = ring_buffer_record_is_on(buffer);
  4534. else
  4535. r = 0;
  4536. r = sprintf(buf, "%d\n", r);
  4537. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4538. }
  4539. static ssize_t
  4540. rb_simple_write(struct file *filp, const char __user *ubuf,
  4541. size_t cnt, loff_t *ppos)
  4542. {
  4543. struct trace_array *tr = filp->private_data;
  4544. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4545. unsigned long val;
  4546. int ret;
  4547. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4548. if (ret)
  4549. return ret;
  4550. if (buffer) {
  4551. mutex_lock(&trace_types_lock);
  4552. if (val) {
  4553. ring_buffer_record_on(buffer);
  4554. if (tr->current_trace->start)
  4555. tr->current_trace->start(tr);
  4556. } else {
  4557. ring_buffer_record_off(buffer);
  4558. if (tr->current_trace->stop)
  4559. tr->current_trace->stop(tr);
  4560. }
  4561. mutex_unlock(&trace_types_lock);
  4562. }
  4563. (*ppos)++;
  4564. return cnt;
  4565. }
  4566. static const struct file_operations rb_simple_fops = {
  4567. .open = tracing_open_generic,
  4568. .read = rb_simple_read,
  4569. .write = rb_simple_write,
  4570. .llseek = default_llseek,
  4571. };
  4572. struct dentry *trace_instance_dir;
  4573. static void
  4574. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
  4575. static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
  4576. {
  4577. int cpu;
  4578. for_each_tracing_cpu(cpu) {
  4579. memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
  4580. per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
  4581. per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
  4582. }
  4583. }
  4584. static int
  4585. allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
  4586. {
  4587. enum ring_buffer_flags rb_flags;
  4588. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  4589. buf->buffer = ring_buffer_alloc(size, rb_flags);
  4590. if (!buf->buffer)
  4591. return -ENOMEM;
  4592. buf->data = alloc_percpu(struct trace_array_cpu);
  4593. if (!buf->data) {
  4594. ring_buffer_free(buf->buffer);
  4595. return -ENOMEM;
  4596. }
  4597. init_trace_buffers(tr, buf);
  4598. /* Allocate the first page for all buffers */
  4599. set_buffer_entries(&tr->trace_buffer,
  4600. ring_buffer_size(tr->trace_buffer.buffer, 0));
  4601. return 0;
  4602. }
  4603. static int allocate_trace_buffers(struct trace_array *tr, int size)
  4604. {
  4605. int ret;
  4606. ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
  4607. if (ret)
  4608. return ret;
  4609. #ifdef CONFIG_TRACER_MAX_TRACE
  4610. ret = allocate_trace_buffer(tr, &tr->max_buffer,
  4611. allocate_snapshot ? size : 1);
  4612. if (WARN_ON(ret)) {
  4613. ring_buffer_free(tr->trace_buffer.buffer);
  4614. free_percpu(tr->trace_buffer.data);
  4615. return -ENOMEM;
  4616. }
  4617. tr->allocated_snapshot = allocate_snapshot;
  4618. /*
  4619. * Only the top level trace array gets its snapshot allocated
  4620. * from the kernel command line.
  4621. */
  4622. allocate_snapshot = false;
  4623. #endif
  4624. return 0;
  4625. }
  4626. static int new_instance_create(const char *name)
  4627. {
  4628. struct trace_array *tr;
  4629. int ret;
  4630. mutex_lock(&trace_types_lock);
  4631. ret = -EEXIST;
  4632. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4633. if (tr->name && strcmp(tr->name, name) == 0)
  4634. goto out_unlock;
  4635. }
  4636. ret = -ENOMEM;
  4637. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  4638. if (!tr)
  4639. goto out_unlock;
  4640. tr->name = kstrdup(name, GFP_KERNEL);
  4641. if (!tr->name)
  4642. goto out_free_tr;
  4643. raw_spin_lock_init(&tr->start_lock);
  4644. tr->current_trace = &nop_trace;
  4645. INIT_LIST_HEAD(&tr->systems);
  4646. INIT_LIST_HEAD(&tr->events);
  4647. if (allocate_trace_buffers(tr, trace_buf_size) < 0)
  4648. goto out_free_tr;
  4649. /* Holder for file callbacks */
  4650. tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
  4651. tr->trace_cpu.tr = tr;
  4652. tr->dir = debugfs_create_dir(name, trace_instance_dir);
  4653. if (!tr->dir)
  4654. goto out_free_tr;
  4655. ret = event_trace_add_tracer(tr->dir, tr);
  4656. if (ret)
  4657. goto out_free_tr;
  4658. init_tracer_debugfs(tr, tr->dir);
  4659. list_add(&tr->list, &ftrace_trace_arrays);
  4660. mutex_unlock(&trace_types_lock);
  4661. return 0;
  4662. out_free_tr:
  4663. if (tr->trace_buffer.buffer)
  4664. ring_buffer_free(tr->trace_buffer.buffer);
  4665. kfree(tr->name);
  4666. kfree(tr);
  4667. out_unlock:
  4668. mutex_unlock(&trace_types_lock);
  4669. return ret;
  4670. }
  4671. static int instance_delete(const char *name)
  4672. {
  4673. struct trace_array *tr;
  4674. int found = 0;
  4675. int ret;
  4676. mutex_lock(&trace_types_lock);
  4677. ret = -ENODEV;
  4678. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4679. if (tr->name && strcmp(tr->name, name) == 0) {
  4680. found = 1;
  4681. break;
  4682. }
  4683. }
  4684. if (!found)
  4685. goto out_unlock;
  4686. ret = -EBUSY;
  4687. if (tr->ref)
  4688. goto out_unlock;
  4689. list_del(&tr->list);
  4690. event_trace_del_tracer(tr);
  4691. debugfs_remove_recursive(tr->dir);
  4692. free_percpu(tr->trace_buffer.data);
  4693. ring_buffer_free(tr->trace_buffer.buffer);
  4694. kfree(tr->name);
  4695. kfree(tr);
  4696. ret = 0;
  4697. out_unlock:
  4698. mutex_unlock(&trace_types_lock);
  4699. return ret;
  4700. }
  4701. static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
  4702. {
  4703. struct dentry *parent;
  4704. int ret;
  4705. /* Paranoid: Make sure the parent is the "instances" directory */
  4706. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4707. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4708. return -ENOENT;
  4709. /*
  4710. * The inode mutex is locked, but debugfs_create_dir() will also
  4711. * take the mutex. As the instances directory can not be destroyed
  4712. * or changed in any other way, it is safe to unlock it, and
  4713. * let the dentry try. If two users try to make the same dir at
  4714. * the same time, then the new_instance_create() will determine the
  4715. * winner.
  4716. */
  4717. mutex_unlock(&inode->i_mutex);
  4718. ret = new_instance_create(dentry->d_iname);
  4719. mutex_lock(&inode->i_mutex);
  4720. return ret;
  4721. }
  4722. static int instance_rmdir(struct inode *inode, struct dentry *dentry)
  4723. {
  4724. struct dentry *parent;
  4725. int ret;
  4726. /* Paranoid: Make sure the parent is the "instances" directory */
  4727. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4728. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4729. return -ENOENT;
  4730. /* The caller did a dget() on dentry */
  4731. mutex_unlock(&dentry->d_inode->i_mutex);
  4732. /*
  4733. * The inode mutex is locked, but debugfs_create_dir() will also
  4734. * take the mutex. As the instances directory can not be destroyed
  4735. * or changed in any other way, it is safe to unlock it, and
  4736. * let the dentry try. If two users try to make the same dir at
  4737. * the same time, then the instance_delete() will determine the
  4738. * winner.
  4739. */
  4740. mutex_unlock(&inode->i_mutex);
  4741. ret = instance_delete(dentry->d_iname);
  4742. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  4743. mutex_lock(&dentry->d_inode->i_mutex);
  4744. return ret;
  4745. }
  4746. static const struct inode_operations instance_dir_inode_operations = {
  4747. .lookup = simple_lookup,
  4748. .mkdir = instance_mkdir,
  4749. .rmdir = instance_rmdir,
  4750. };
  4751. static __init void create_trace_instances(struct dentry *d_tracer)
  4752. {
  4753. trace_instance_dir = debugfs_create_dir("instances", d_tracer);
  4754. if (WARN_ON(!trace_instance_dir))
  4755. return;
  4756. /* Hijack the dir inode operations, to allow mkdir */
  4757. trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
  4758. }
  4759. static void
  4760. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
  4761. {
  4762. int cpu;
  4763. trace_create_file("trace_options", 0644, d_tracer,
  4764. tr, &tracing_iter_fops);
  4765. trace_create_file("trace", 0644, d_tracer,
  4766. (void *)&tr->trace_cpu, &tracing_fops);
  4767. trace_create_file("trace_pipe", 0444, d_tracer,
  4768. (void *)&tr->trace_cpu, &tracing_pipe_fops);
  4769. trace_create_file("buffer_size_kb", 0644, d_tracer,
  4770. (void *)&tr->trace_cpu, &tracing_entries_fops);
  4771. trace_create_file("buffer_total_size_kb", 0444, d_tracer,
  4772. tr, &tracing_total_entries_fops);
  4773. trace_create_file("free_buffer", 0644, d_tracer,
  4774. tr, &tracing_free_buffer_fops);
  4775. trace_create_file("trace_marker", 0220, d_tracer,
  4776. tr, &tracing_mark_fops);
  4777. trace_create_file("trace_clock", 0644, d_tracer, tr,
  4778. &trace_clock_fops);
  4779. trace_create_file("tracing_on", 0644, d_tracer,
  4780. tr, &rb_simple_fops);
  4781. #ifdef CONFIG_TRACER_SNAPSHOT
  4782. trace_create_file("snapshot", 0644, d_tracer,
  4783. (void *)&tr->trace_cpu, &snapshot_fops);
  4784. #endif
  4785. for_each_tracing_cpu(cpu)
  4786. tracing_init_debugfs_percpu(tr, cpu);
  4787. }
  4788. static __init int tracer_init_debugfs(void)
  4789. {
  4790. struct dentry *d_tracer;
  4791. trace_access_lock_init();
  4792. d_tracer = tracing_init_dentry();
  4793. init_tracer_debugfs(&global_trace, d_tracer);
  4794. trace_create_file("tracing_cpumask", 0644, d_tracer,
  4795. &global_trace, &tracing_cpumask_fops);
  4796. trace_create_file("available_tracers", 0444, d_tracer,
  4797. &global_trace, &show_traces_fops);
  4798. trace_create_file("current_tracer", 0644, d_tracer,
  4799. &global_trace, &set_tracer_fops);
  4800. #ifdef CONFIG_TRACER_MAX_TRACE
  4801. trace_create_file("tracing_max_latency", 0644, d_tracer,
  4802. &tracing_max_latency, &tracing_max_lat_fops);
  4803. #endif
  4804. trace_create_file("tracing_thresh", 0644, d_tracer,
  4805. &tracing_thresh, &tracing_max_lat_fops);
  4806. trace_create_file("README", 0444, d_tracer,
  4807. NULL, &tracing_readme_fops);
  4808. trace_create_file("saved_cmdlines", 0444, d_tracer,
  4809. NULL, &tracing_saved_cmdlines_fops);
  4810. #ifdef CONFIG_DYNAMIC_FTRACE
  4811. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  4812. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  4813. #endif
  4814. create_trace_instances(d_tracer);
  4815. create_trace_options_dir(&global_trace);
  4816. return 0;
  4817. }
  4818. static int trace_panic_handler(struct notifier_block *this,
  4819. unsigned long event, void *unused)
  4820. {
  4821. if (ftrace_dump_on_oops)
  4822. ftrace_dump(ftrace_dump_on_oops);
  4823. return NOTIFY_OK;
  4824. }
  4825. static struct notifier_block trace_panic_notifier = {
  4826. .notifier_call = trace_panic_handler,
  4827. .next = NULL,
  4828. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  4829. };
  4830. static int trace_die_handler(struct notifier_block *self,
  4831. unsigned long val,
  4832. void *data)
  4833. {
  4834. switch (val) {
  4835. case DIE_OOPS:
  4836. if (ftrace_dump_on_oops)
  4837. ftrace_dump(ftrace_dump_on_oops);
  4838. break;
  4839. default:
  4840. break;
  4841. }
  4842. return NOTIFY_OK;
  4843. }
  4844. static struct notifier_block trace_die_notifier = {
  4845. .notifier_call = trace_die_handler,
  4846. .priority = 200
  4847. };
  4848. /*
  4849. * printk is set to max of 1024, we really don't need it that big.
  4850. * Nothing should be printing 1000 characters anyway.
  4851. */
  4852. #define TRACE_MAX_PRINT 1000
  4853. /*
  4854. * Define here KERN_TRACE so that we have one place to modify
  4855. * it if we decide to change what log level the ftrace dump
  4856. * should be at.
  4857. */
  4858. #define KERN_TRACE KERN_EMERG
  4859. void
  4860. trace_printk_seq(struct trace_seq *s)
  4861. {
  4862. /* Probably should print a warning here. */
  4863. if (s->len >= 1000)
  4864. s->len = 1000;
  4865. /* should be zero ended, but we are paranoid. */
  4866. s->buffer[s->len] = 0;
  4867. printk(KERN_TRACE "%s", s->buffer);
  4868. trace_seq_init(s);
  4869. }
  4870. void trace_init_global_iter(struct trace_iterator *iter)
  4871. {
  4872. iter->tr = &global_trace;
  4873. iter->trace = iter->tr->current_trace;
  4874. iter->cpu_file = RING_BUFFER_ALL_CPUS;
  4875. iter->trace_buffer = &global_trace.trace_buffer;
  4876. }
  4877. static void
  4878. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
  4879. {
  4880. static arch_spinlock_t ftrace_dump_lock =
  4881. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  4882. /* use static because iter can be a bit big for the stack */
  4883. static struct trace_iterator iter;
  4884. unsigned int old_userobj;
  4885. static int dump_ran;
  4886. unsigned long flags;
  4887. int cnt = 0, cpu;
  4888. /* only one dump */
  4889. local_irq_save(flags);
  4890. arch_spin_lock(&ftrace_dump_lock);
  4891. if (dump_ran)
  4892. goto out;
  4893. dump_ran = 1;
  4894. tracing_off();
  4895. /* Did function tracer already get disabled? */
  4896. if (ftrace_is_dead()) {
  4897. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  4898. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  4899. }
  4900. if (disable_tracing)
  4901. ftrace_kill();
  4902. /* Simulate the iterator */
  4903. trace_init_global_iter(&iter);
  4904. for_each_tracing_cpu(cpu) {
  4905. atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
  4906. }
  4907. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  4908. /* don't look at user memory in panic mode */
  4909. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  4910. switch (oops_dump_mode) {
  4911. case DUMP_ALL:
  4912. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  4913. break;
  4914. case DUMP_ORIG:
  4915. iter.cpu_file = raw_smp_processor_id();
  4916. break;
  4917. case DUMP_NONE:
  4918. goto out_enable;
  4919. default:
  4920. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  4921. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  4922. }
  4923. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  4924. /*
  4925. * We need to stop all tracing on all CPUS to read the
  4926. * the next buffer. This is a bit expensive, but is
  4927. * not done often. We fill all what we can read,
  4928. * and then release the locks again.
  4929. */
  4930. while (!trace_empty(&iter)) {
  4931. if (!cnt)
  4932. printk(KERN_TRACE "---------------------------------\n");
  4933. cnt++;
  4934. /* reset all but tr, trace, and overruns */
  4935. memset(&iter.seq, 0,
  4936. sizeof(struct trace_iterator) -
  4937. offsetof(struct trace_iterator, seq));
  4938. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  4939. iter.pos = -1;
  4940. if (trace_find_next_entry_inc(&iter) != NULL) {
  4941. int ret;
  4942. ret = print_trace_line(&iter);
  4943. if (ret != TRACE_TYPE_NO_CONSUME)
  4944. trace_consume(&iter);
  4945. }
  4946. touch_nmi_watchdog();
  4947. trace_printk_seq(&iter.seq);
  4948. }
  4949. if (!cnt)
  4950. printk(KERN_TRACE " (ftrace buffer empty)\n");
  4951. else
  4952. printk(KERN_TRACE "---------------------------------\n");
  4953. out_enable:
  4954. /* Re-enable tracing if requested */
  4955. if (!disable_tracing) {
  4956. trace_flags |= old_userobj;
  4957. for_each_tracing_cpu(cpu) {
  4958. atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  4959. }
  4960. tracing_on();
  4961. }
  4962. out:
  4963. arch_spin_unlock(&ftrace_dump_lock);
  4964. local_irq_restore(flags);
  4965. }
  4966. /* By default: disable tracing after the dump */
  4967. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  4968. {
  4969. __ftrace_dump(true, oops_dump_mode);
  4970. }
  4971. EXPORT_SYMBOL_GPL(ftrace_dump);
  4972. __init static int tracer_alloc_buffers(void)
  4973. {
  4974. int ring_buf_size;
  4975. int ret = -ENOMEM;
  4976. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  4977. goto out;
  4978. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  4979. goto out_free_buffer_mask;
  4980. /* Only allocate trace_printk buffers if a trace_printk exists */
  4981. if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
  4982. /* Must be called before global_trace.buffer is allocated */
  4983. trace_printk_init_buffers();
  4984. /* To save memory, keep the ring buffer size to its minimum */
  4985. if (ring_buffer_expanded)
  4986. ring_buf_size = trace_buf_size;
  4987. else
  4988. ring_buf_size = 1;
  4989. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  4990. cpumask_copy(tracing_cpumask, cpu_all_mask);
  4991. raw_spin_lock_init(&global_trace.start_lock);
  4992. /* TODO: make the number of buffers hot pluggable with CPUS */
  4993. if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
  4994. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  4995. WARN_ON(1);
  4996. goto out_free_cpumask;
  4997. }
  4998. if (global_trace.buffer_disabled)
  4999. tracing_off();
  5000. trace_init_cmdlines();
  5001. register_tracer(&nop_trace);
  5002. global_trace.current_trace = &nop_trace;
  5003. /* All seems OK, enable tracing */
  5004. tracing_disabled = 0;
  5005. atomic_notifier_chain_register(&panic_notifier_list,
  5006. &trace_panic_notifier);
  5007. register_die_notifier(&trace_die_notifier);
  5008. global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
  5009. /* Holder for file callbacks */
  5010. global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
  5011. global_trace.trace_cpu.tr = &global_trace;
  5012. INIT_LIST_HEAD(&global_trace.systems);
  5013. INIT_LIST_HEAD(&global_trace.events);
  5014. list_add(&global_trace.list, &ftrace_trace_arrays);
  5015. while (trace_boot_options) {
  5016. char *option;
  5017. option = strsep(&trace_boot_options, ",");
  5018. trace_set_options(&global_trace, option);
  5019. }
  5020. register_snapshot_cmd();
  5021. return 0;
  5022. out_free_cpumask:
  5023. free_percpu(global_trace.trace_buffer.data);
  5024. #ifdef CONFIG_TRACER_MAX_TRACE
  5025. free_percpu(global_trace.max_buffer.data);
  5026. #endif
  5027. free_cpumask_var(tracing_cpumask);
  5028. out_free_buffer_mask:
  5029. free_cpumask_var(tracing_buffer_mask);
  5030. out:
  5031. return ret;
  5032. }
  5033. __init static int clear_boot_tracer(void)
  5034. {
  5035. /*
  5036. * The default tracer at boot buffer is an init section.
  5037. * This function is called in lateinit. If we did not
  5038. * find the boot tracer, then clear it out, to prevent
  5039. * later registration from accessing the buffer that is
  5040. * about to be freed.
  5041. */
  5042. if (!default_bootup_tracer)
  5043. return 0;
  5044. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  5045. default_bootup_tracer);
  5046. default_bootup_tracer = NULL;
  5047. return 0;
  5048. }
  5049. early_initcall(tracer_alloc_buffers);
  5050. fs_initcall(tracer_init_debugfs);
  5051. late_initcall(clear_boot_tracer);