trace.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114
  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. */
  1386. void trace_dump_stack(void)
  1387. {
  1388. unsigned long flags;
  1389. if (tracing_disabled || tracing_selftest_running)
  1390. return;
  1391. local_save_flags(flags);
  1392. /* skipping 3 traces, seems to get us at the caller of this function */
  1393. __ftrace_trace_stack(global_trace.trace_buffer.buffer, flags, 3,
  1394. preempt_count(), NULL);
  1395. }
  1396. static DEFINE_PER_CPU(int, user_stack_count);
  1397. void
  1398. ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
  1399. {
  1400. struct ftrace_event_call *call = &event_user_stack;
  1401. struct ring_buffer_event *event;
  1402. struct userstack_entry *entry;
  1403. struct stack_trace trace;
  1404. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  1405. return;
  1406. /*
  1407. * NMIs can not handle page faults, even with fix ups.
  1408. * The save user stack can (and often does) fault.
  1409. */
  1410. if (unlikely(in_nmi()))
  1411. return;
  1412. /*
  1413. * prevent recursion, since the user stack tracing may
  1414. * trigger other kernel events.
  1415. */
  1416. preempt_disable();
  1417. if (__this_cpu_read(user_stack_count))
  1418. goto out;
  1419. __this_cpu_inc(user_stack_count);
  1420. event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
  1421. sizeof(*entry), flags, pc);
  1422. if (!event)
  1423. goto out_drop_count;
  1424. entry = ring_buffer_event_data(event);
  1425. entry->tgid = current->tgid;
  1426. memset(&entry->caller, 0, sizeof(entry->caller));
  1427. trace.nr_entries = 0;
  1428. trace.max_entries = FTRACE_STACK_ENTRIES;
  1429. trace.skip = 0;
  1430. trace.entries = entry->caller;
  1431. save_stack_trace_user(&trace);
  1432. if (!filter_check_discard(call, entry, buffer, event))
  1433. __buffer_unlock_commit(buffer, event);
  1434. out_drop_count:
  1435. __this_cpu_dec(user_stack_count);
  1436. out:
  1437. preempt_enable();
  1438. }
  1439. #ifdef UNUSED
  1440. static void __trace_userstack(struct trace_array *tr, unsigned long flags)
  1441. {
  1442. ftrace_trace_userstack(tr, flags, preempt_count());
  1443. }
  1444. #endif /* UNUSED */
  1445. #endif /* CONFIG_STACKTRACE */
  1446. /* created for use with alloc_percpu */
  1447. struct trace_buffer_struct {
  1448. char buffer[TRACE_BUF_SIZE];
  1449. };
  1450. static struct trace_buffer_struct *trace_percpu_buffer;
  1451. static struct trace_buffer_struct *trace_percpu_sirq_buffer;
  1452. static struct trace_buffer_struct *trace_percpu_irq_buffer;
  1453. static struct trace_buffer_struct *trace_percpu_nmi_buffer;
  1454. /*
  1455. * The buffer used is dependent on the context. There is a per cpu
  1456. * buffer for normal context, softirq contex, hard irq context and
  1457. * for NMI context. Thise allows for lockless recording.
  1458. *
  1459. * Note, if the buffers failed to be allocated, then this returns NULL
  1460. */
  1461. static char *get_trace_buf(void)
  1462. {
  1463. struct trace_buffer_struct *percpu_buffer;
  1464. /*
  1465. * If we have allocated per cpu buffers, then we do not
  1466. * need to do any locking.
  1467. */
  1468. if (in_nmi())
  1469. percpu_buffer = trace_percpu_nmi_buffer;
  1470. else if (in_irq())
  1471. percpu_buffer = trace_percpu_irq_buffer;
  1472. else if (in_softirq())
  1473. percpu_buffer = trace_percpu_sirq_buffer;
  1474. else
  1475. percpu_buffer = trace_percpu_buffer;
  1476. if (!percpu_buffer)
  1477. return NULL;
  1478. return this_cpu_ptr(&percpu_buffer->buffer[0]);
  1479. }
  1480. static int alloc_percpu_trace_buffer(void)
  1481. {
  1482. struct trace_buffer_struct *buffers;
  1483. struct trace_buffer_struct *sirq_buffers;
  1484. struct trace_buffer_struct *irq_buffers;
  1485. struct trace_buffer_struct *nmi_buffers;
  1486. buffers = alloc_percpu(struct trace_buffer_struct);
  1487. if (!buffers)
  1488. goto err_warn;
  1489. sirq_buffers = alloc_percpu(struct trace_buffer_struct);
  1490. if (!sirq_buffers)
  1491. goto err_sirq;
  1492. irq_buffers = alloc_percpu(struct trace_buffer_struct);
  1493. if (!irq_buffers)
  1494. goto err_irq;
  1495. nmi_buffers = alloc_percpu(struct trace_buffer_struct);
  1496. if (!nmi_buffers)
  1497. goto err_nmi;
  1498. trace_percpu_buffer = buffers;
  1499. trace_percpu_sirq_buffer = sirq_buffers;
  1500. trace_percpu_irq_buffer = irq_buffers;
  1501. trace_percpu_nmi_buffer = nmi_buffers;
  1502. return 0;
  1503. err_nmi:
  1504. free_percpu(irq_buffers);
  1505. err_irq:
  1506. free_percpu(sirq_buffers);
  1507. err_sirq:
  1508. free_percpu(buffers);
  1509. err_warn:
  1510. WARN(1, "Could not allocate percpu trace_printk buffer");
  1511. return -ENOMEM;
  1512. }
  1513. static int buffers_allocated;
  1514. void trace_printk_init_buffers(void)
  1515. {
  1516. if (buffers_allocated)
  1517. return;
  1518. if (alloc_percpu_trace_buffer())
  1519. return;
  1520. pr_info("ftrace: Allocated trace_printk buffers\n");
  1521. /* Expand the buffers to set size */
  1522. tracing_update_buffers();
  1523. buffers_allocated = 1;
  1524. /*
  1525. * trace_printk_init_buffers() can be called by modules.
  1526. * If that happens, then we need to start cmdline recording
  1527. * directly here. If the global_trace.buffer is already
  1528. * allocated here, then this was called by module code.
  1529. */
  1530. if (global_trace.trace_buffer.buffer)
  1531. tracing_start_cmdline_record();
  1532. }
  1533. void trace_printk_start_comm(void)
  1534. {
  1535. /* Start tracing comms if trace printk is set */
  1536. if (!buffers_allocated)
  1537. return;
  1538. tracing_start_cmdline_record();
  1539. }
  1540. static void trace_printk_start_stop_comm(int enabled)
  1541. {
  1542. if (!buffers_allocated)
  1543. return;
  1544. if (enabled)
  1545. tracing_start_cmdline_record();
  1546. else
  1547. tracing_stop_cmdline_record();
  1548. }
  1549. /**
  1550. * trace_vbprintk - write binary msg to tracing buffer
  1551. *
  1552. */
  1553. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  1554. {
  1555. struct ftrace_event_call *call = &event_bprint;
  1556. struct ring_buffer_event *event;
  1557. struct ring_buffer *buffer;
  1558. struct trace_array *tr = &global_trace;
  1559. struct bprint_entry *entry;
  1560. unsigned long flags;
  1561. char *tbuffer;
  1562. int len = 0, size, pc;
  1563. if (unlikely(tracing_selftest_running || tracing_disabled))
  1564. return 0;
  1565. /* Don't pollute graph traces with trace_vprintk internals */
  1566. pause_graph_tracing();
  1567. pc = preempt_count();
  1568. preempt_disable_notrace();
  1569. tbuffer = get_trace_buf();
  1570. if (!tbuffer) {
  1571. len = 0;
  1572. goto out;
  1573. }
  1574. len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
  1575. if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
  1576. goto out;
  1577. local_save_flags(flags);
  1578. size = sizeof(*entry) + sizeof(u32) * len;
  1579. buffer = tr->trace_buffer.buffer;
  1580. event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
  1581. flags, pc);
  1582. if (!event)
  1583. goto out;
  1584. entry = ring_buffer_event_data(event);
  1585. entry->ip = ip;
  1586. entry->fmt = fmt;
  1587. memcpy(entry->buf, tbuffer, sizeof(u32) * len);
  1588. if (!filter_check_discard(call, entry, buffer, event)) {
  1589. __buffer_unlock_commit(buffer, event);
  1590. ftrace_trace_stack(buffer, flags, 6, pc);
  1591. }
  1592. out:
  1593. preempt_enable_notrace();
  1594. unpause_graph_tracing();
  1595. return len;
  1596. }
  1597. EXPORT_SYMBOL_GPL(trace_vbprintk);
  1598. static int
  1599. __trace_array_vprintk(struct ring_buffer *buffer,
  1600. unsigned long ip, const char *fmt, va_list args)
  1601. {
  1602. struct ftrace_event_call *call = &event_print;
  1603. struct ring_buffer_event *event;
  1604. int len = 0, size, pc;
  1605. struct print_entry *entry;
  1606. unsigned long flags;
  1607. char *tbuffer;
  1608. if (tracing_disabled || tracing_selftest_running)
  1609. return 0;
  1610. /* Don't pollute graph traces with trace_vprintk internals */
  1611. pause_graph_tracing();
  1612. pc = preempt_count();
  1613. preempt_disable_notrace();
  1614. tbuffer = get_trace_buf();
  1615. if (!tbuffer) {
  1616. len = 0;
  1617. goto out;
  1618. }
  1619. len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
  1620. if (len > TRACE_BUF_SIZE)
  1621. goto out;
  1622. local_save_flags(flags);
  1623. size = sizeof(*entry) + len + 1;
  1624. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  1625. flags, pc);
  1626. if (!event)
  1627. goto out;
  1628. entry = ring_buffer_event_data(event);
  1629. entry->ip = ip;
  1630. memcpy(&entry->buf, tbuffer, len);
  1631. entry->buf[len] = '\0';
  1632. if (!filter_check_discard(call, entry, buffer, event)) {
  1633. __buffer_unlock_commit(buffer, event);
  1634. ftrace_trace_stack(buffer, flags, 6, pc);
  1635. }
  1636. out:
  1637. preempt_enable_notrace();
  1638. unpause_graph_tracing();
  1639. return len;
  1640. }
  1641. int trace_array_vprintk(struct trace_array *tr,
  1642. unsigned long ip, const char *fmt, va_list args)
  1643. {
  1644. return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
  1645. }
  1646. int trace_array_printk(struct trace_array *tr,
  1647. unsigned long ip, const char *fmt, ...)
  1648. {
  1649. int ret;
  1650. va_list ap;
  1651. if (!(trace_flags & TRACE_ITER_PRINTK))
  1652. return 0;
  1653. va_start(ap, fmt);
  1654. ret = trace_array_vprintk(tr, ip, fmt, ap);
  1655. va_end(ap);
  1656. return ret;
  1657. }
  1658. int trace_array_printk_buf(struct ring_buffer *buffer,
  1659. unsigned long ip, const char *fmt, ...)
  1660. {
  1661. int ret;
  1662. va_list ap;
  1663. if (!(trace_flags & TRACE_ITER_PRINTK))
  1664. return 0;
  1665. va_start(ap, fmt);
  1666. ret = __trace_array_vprintk(buffer, ip, fmt, ap);
  1667. va_end(ap);
  1668. return ret;
  1669. }
  1670. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  1671. {
  1672. return trace_array_vprintk(&global_trace, ip, fmt, args);
  1673. }
  1674. EXPORT_SYMBOL_GPL(trace_vprintk);
  1675. static void trace_iterator_increment(struct trace_iterator *iter)
  1676. {
  1677. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
  1678. iter->idx++;
  1679. if (buf_iter)
  1680. ring_buffer_read(buf_iter, NULL);
  1681. }
  1682. static struct trace_entry *
  1683. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
  1684. unsigned long *lost_events)
  1685. {
  1686. struct ring_buffer_event *event;
  1687. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
  1688. if (buf_iter)
  1689. event = ring_buffer_iter_peek(buf_iter, ts);
  1690. else
  1691. event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
  1692. lost_events);
  1693. if (event) {
  1694. iter->ent_size = ring_buffer_event_length(event);
  1695. return ring_buffer_event_data(event);
  1696. }
  1697. iter->ent_size = 0;
  1698. return NULL;
  1699. }
  1700. static struct trace_entry *
  1701. __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
  1702. unsigned long *missing_events, u64 *ent_ts)
  1703. {
  1704. struct ring_buffer *buffer = iter->trace_buffer->buffer;
  1705. struct trace_entry *ent, *next = NULL;
  1706. unsigned long lost_events = 0, next_lost = 0;
  1707. int cpu_file = iter->cpu_file;
  1708. u64 next_ts = 0, ts;
  1709. int next_cpu = -1;
  1710. int next_size = 0;
  1711. int cpu;
  1712. /*
  1713. * If we are in a per_cpu trace file, don't bother by iterating over
  1714. * all cpu and peek directly.
  1715. */
  1716. if (cpu_file > RING_BUFFER_ALL_CPUS) {
  1717. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1718. return NULL;
  1719. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  1720. if (ent_cpu)
  1721. *ent_cpu = cpu_file;
  1722. return ent;
  1723. }
  1724. for_each_tracing_cpu(cpu) {
  1725. if (ring_buffer_empty_cpu(buffer, cpu))
  1726. continue;
  1727. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  1728. /*
  1729. * Pick the entry with the smallest timestamp:
  1730. */
  1731. if (ent && (!next || ts < next_ts)) {
  1732. next = ent;
  1733. next_cpu = cpu;
  1734. next_ts = ts;
  1735. next_lost = lost_events;
  1736. next_size = iter->ent_size;
  1737. }
  1738. }
  1739. iter->ent_size = next_size;
  1740. if (ent_cpu)
  1741. *ent_cpu = next_cpu;
  1742. if (ent_ts)
  1743. *ent_ts = next_ts;
  1744. if (missing_events)
  1745. *missing_events = next_lost;
  1746. return next;
  1747. }
  1748. /* Find the next real entry, without updating the iterator itself */
  1749. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1750. int *ent_cpu, u64 *ent_ts)
  1751. {
  1752. return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  1753. }
  1754. /* Find the next real entry, and increment the iterator to the next entry */
  1755. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  1756. {
  1757. iter->ent = __find_next_entry(iter, &iter->cpu,
  1758. &iter->lost_events, &iter->ts);
  1759. if (iter->ent)
  1760. trace_iterator_increment(iter);
  1761. return iter->ent ? iter : NULL;
  1762. }
  1763. static void trace_consume(struct trace_iterator *iter)
  1764. {
  1765. ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
  1766. &iter->lost_events);
  1767. }
  1768. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1769. {
  1770. struct trace_iterator *iter = m->private;
  1771. int i = (int)*pos;
  1772. void *ent;
  1773. WARN_ON_ONCE(iter->leftover);
  1774. (*pos)++;
  1775. /* can't go backwards */
  1776. if (iter->idx > i)
  1777. return NULL;
  1778. if (iter->idx < 0)
  1779. ent = trace_find_next_entry_inc(iter);
  1780. else
  1781. ent = iter;
  1782. while (ent && iter->idx < i)
  1783. ent = trace_find_next_entry_inc(iter);
  1784. iter->pos = *pos;
  1785. return ent;
  1786. }
  1787. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  1788. {
  1789. struct ring_buffer_event *event;
  1790. struct ring_buffer_iter *buf_iter;
  1791. unsigned long entries = 0;
  1792. u64 ts;
  1793. per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
  1794. buf_iter = trace_buffer_iter(iter, cpu);
  1795. if (!buf_iter)
  1796. return;
  1797. ring_buffer_iter_reset(buf_iter);
  1798. /*
  1799. * We could have the case with the max latency tracers
  1800. * that a reset never took place on a cpu. This is evident
  1801. * by the timestamp being before the start of the buffer.
  1802. */
  1803. while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
  1804. if (ts >= iter->trace_buffer->time_start)
  1805. break;
  1806. entries++;
  1807. ring_buffer_read(buf_iter, NULL);
  1808. }
  1809. per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
  1810. }
  1811. /*
  1812. * The current tracer is copied to avoid a global locking
  1813. * all around.
  1814. */
  1815. static void *s_start(struct seq_file *m, loff_t *pos)
  1816. {
  1817. struct trace_iterator *iter = m->private;
  1818. struct trace_array *tr = iter->tr;
  1819. int cpu_file = iter->cpu_file;
  1820. void *p = NULL;
  1821. loff_t l = 0;
  1822. int cpu;
  1823. /*
  1824. * copy the tracer to avoid using a global lock all around.
  1825. * iter->trace is a copy of current_trace, the pointer to the
  1826. * name may be used instead of a strcmp(), as iter->trace->name
  1827. * will point to the same string as current_trace->name.
  1828. */
  1829. mutex_lock(&trace_types_lock);
  1830. if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
  1831. *iter->trace = *tr->current_trace;
  1832. mutex_unlock(&trace_types_lock);
  1833. #ifdef CONFIG_TRACER_MAX_TRACE
  1834. if (iter->snapshot && iter->trace->use_max_tr)
  1835. return ERR_PTR(-EBUSY);
  1836. #endif
  1837. if (!iter->snapshot)
  1838. atomic_inc(&trace_record_cmdline_disabled);
  1839. if (*pos != iter->pos) {
  1840. iter->ent = NULL;
  1841. iter->cpu = 0;
  1842. iter->idx = -1;
  1843. if (cpu_file == RING_BUFFER_ALL_CPUS) {
  1844. for_each_tracing_cpu(cpu)
  1845. tracing_iter_reset(iter, cpu);
  1846. } else
  1847. tracing_iter_reset(iter, cpu_file);
  1848. iter->leftover = 0;
  1849. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1850. ;
  1851. } else {
  1852. /*
  1853. * If we overflowed the seq_file before, then we want
  1854. * to just reuse the trace_seq buffer again.
  1855. */
  1856. if (iter->leftover)
  1857. p = iter;
  1858. else {
  1859. l = *pos - 1;
  1860. p = s_next(m, p, &l);
  1861. }
  1862. }
  1863. trace_event_read_lock();
  1864. trace_access_lock(cpu_file);
  1865. return p;
  1866. }
  1867. static void s_stop(struct seq_file *m, void *p)
  1868. {
  1869. struct trace_iterator *iter = m->private;
  1870. #ifdef CONFIG_TRACER_MAX_TRACE
  1871. if (iter->snapshot && iter->trace->use_max_tr)
  1872. return;
  1873. #endif
  1874. if (!iter->snapshot)
  1875. atomic_dec(&trace_record_cmdline_disabled);
  1876. trace_access_unlock(iter->cpu_file);
  1877. trace_event_read_unlock();
  1878. }
  1879. static void
  1880. get_total_entries(struct trace_buffer *buf,
  1881. unsigned long *total, unsigned long *entries)
  1882. {
  1883. unsigned long count;
  1884. int cpu;
  1885. *total = 0;
  1886. *entries = 0;
  1887. for_each_tracing_cpu(cpu) {
  1888. count = ring_buffer_entries_cpu(buf->buffer, cpu);
  1889. /*
  1890. * If this buffer has skipped entries, then we hold all
  1891. * entries for the trace and we need to ignore the
  1892. * ones before the time stamp.
  1893. */
  1894. if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
  1895. count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
  1896. /* total is the same as the entries */
  1897. *total += count;
  1898. } else
  1899. *total += count +
  1900. ring_buffer_overrun_cpu(buf->buffer, cpu);
  1901. *entries += count;
  1902. }
  1903. }
  1904. static void print_lat_help_header(struct seq_file *m)
  1905. {
  1906. seq_puts(m, "# _------=> CPU# \n");
  1907. seq_puts(m, "# / _-----=> irqs-off \n");
  1908. seq_puts(m, "# | / _----=> need-resched \n");
  1909. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1910. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1911. seq_puts(m, "# |||| / delay \n");
  1912. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1913. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1914. }
  1915. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  1916. {
  1917. unsigned long total;
  1918. unsigned long entries;
  1919. get_total_entries(buf, &total, &entries);
  1920. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  1921. entries, total, num_online_cpus());
  1922. seq_puts(m, "#\n");
  1923. }
  1924. static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
  1925. {
  1926. print_event_info(buf, m);
  1927. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1928. seq_puts(m, "# | | | | |\n");
  1929. }
  1930. static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
  1931. {
  1932. print_event_info(buf, m);
  1933. seq_puts(m, "# _-----=> irqs-off\n");
  1934. seq_puts(m, "# / _----=> need-resched\n");
  1935. seq_puts(m, "# | / _---=> hardirq/softirq\n");
  1936. seq_puts(m, "# || / _--=> preempt-depth\n");
  1937. seq_puts(m, "# ||| / delay\n");
  1938. seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
  1939. seq_puts(m, "# | | | |||| | |\n");
  1940. }
  1941. void
  1942. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1943. {
  1944. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1945. struct trace_buffer *buf = iter->trace_buffer;
  1946. struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
  1947. struct tracer *type = iter->trace;
  1948. unsigned long entries;
  1949. unsigned long total;
  1950. const char *name = "preemption";
  1951. name = type->name;
  1952. get_total_entries(buf, &total, &entries);
  1953. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1954. name, UTS_RELEASE);
  1955. seq_puts(m, "# -----------------------------------"
  1956. "---------------------------------\n");
  1957. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1958. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1959. nsecs_to_usecs(data->saved_latency),
  1960. entries,
  1961. total,
  1962. buf->cpu,
  1963. #if defined(CONFIG_PREEMPT_NONE)
  1964. "server",
  1965. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1966. "desktop",
  1967. #elif defined(CONFIG_PREEMPT)
  1968. "preempt",
  1969. #else
  1970. "unknown",
  1971. #endif
  1972. /* These are reserved for later use */
  1973. 0, 0, 0, 0);
  1974. #ifdef CONFIG_SMP
  1975. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1976. #else
  1977. seq_puts(m, ")\n");
  1978. #endif
  1979. seq_puts(m, "# -----------------\n");
  1980. seq_printf(m, "# | task: %.16s-%d "
  1981. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1982. data->comm, data->pid,
  1983. from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
  1984. data->policy, data->rt_priority);
  1985. seq_puts(m, "# -----------------\n");
  1986. if (data->critical_start) {
  1987. seq_puts(m, "# => started at: ");
  1988. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1989. trace_print_seq(m, &iter->seq);
  1990. seq_puts(m, "\n# => ended at: ");
  1991. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1992. trace_print_seq(m, &iter->seq);
  1993. seq_puts(m, "\n#\n");
  1994. }
  1995. seq_puts(m, "#\n");
  1996. }
  1997. static void test_cpu_buff_start(struct trace_iterator *iter)
  1998. {
  1999. struct trace_seq *s = &iter->seq;
  2000. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  2001. return;
  2002. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  2003. return;
  2004. if (cpumask_test_cpu(iter->cpu, iter->started))
  2005. return;
  2006. if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
  2007. return;
  2008. cpumask_set_cpu(iter->cpu, iter->started);
  2009. /* Don't print started cpu buffer for the first entry of the trace */
  2010. if (iter->idx > 1)
  2011. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  2012. iter->cpu);
  2013. }
  2014. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  2015. {
  2016. struct trace_seq *s = &iter->seq;
  2017. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  2018. struct trace_entry *entry;
  2019. struct trace_event *event;
  2020. entry = iter->ent;
  2021. test_cpu_buff_start(iter);
  2022. event = ftrace_find_event(entry->type);
  2023. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2024. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  2025. if (!trace_print_lat_context(iter))
  2026. goto partial;
  2027. } else {
  2028. if (!trace_print_context(iter))
  2029. goto partial;
  2030. }
  2031. }
  2032. if (event)
  2033. return event->funcs->trace(iter, sym_flags, event);
  2034. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  2035. goto partial;
  2036. return TRACE_TYPE_HANDLED;
  2037. partial:
  2038. return TRACE_TYPE_PARTIAL_LINE;
  2039. }
  2040. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  2041. {
  2042. struct trace_seq *s = &iter->seq;
  2043. struct trace_entry *entry;
  2044. struct trace_event *event;
  2045. entry = iter->ent;
  2046. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2047. if (!trace_seq_printf(s, "%d %d %llu ",
  2048. entry->pid, iter->cpu, iter->ts))
  2049. goto partial;
  2050. }
  2051. event = ftrace_find_event(entry->type);
  2052. if (event)
  2053. return event->funcs->raw(iter, 0, event);
  2054. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  2055. goto partial;
  2056. return TRACE_TYPE_HANDLED;
  2057. partial:
  2058. return TRACE_TYPE_PARTIAL_LINE;
  2059. }
  2060. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  2061. {
  2062. struct trace_seq *s = &iter->seq;
  2063. unsigned char newline = '\n';
  2064. struct trace_entry *entry;
  2065. struct trace_event *event;
  2066. entry = iter->ent;
  2067. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2068. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  2069. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  2070. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  2071. }
  2072. event = ftrace_find_event(entry->type);
  2073. if (event) {
  2074. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  2075. if (ret != TRACE_TYPE_HANDLED)
  2076. return ret;
  2077. }
  2078. SEQ_PUT_FIELD_RET(s, newline);
  2079. return TRACE_TYPE_HANDLED;
  2080. }
  2081. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  2082. {
  2083. struct trace_seq *s = &iter->seq;
  2084. struct trace_entry *entry;
  2085. struct trace_event *event;
  2086. entry = iter->ent;
  2087. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  2088. SEQ_PUT_FIELD_RET(s, entry->pid);
  2089. SEQ_PUT_FIELD_RET(s, iter->cpu);
  2090. SEQ_PUT_FIELD_RET(s, iter->ts);
  2091. }
  2092. event = ftrace_find_event(entry->type);
  2093. return event ? event->funcs->binary(iter, 0, event) :
  2094. TRACE_TYPE_HANDLED;
  2095. }
  2096. int trace_empty(struct trace_iterator *iter)
  2097. {
  2098. struct ring_buffer_iter *buf_iter;
  2099. int cpu;
  2100. /* If we are looking at one CPU buffer, only check that one */
  2101. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  2102. cpu = iter->cpu_file;
  2103. buf_iter = trace_buffer_iter(iter, cpu);
  2104. if (buf_iter) {
  2105. if (!ring_buffer_iter_empty(buf_iter))
  2106. return 0;
  2107. } else {
  2108. if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
  2109. return 0;
  2110. }
  2111. return 1;
  2112. }
  2113. for_each_tracing_cpu(cpu) {
  2114. buf_iter = trace_buffer_iter(iter, cpu);
  2115. if (buf_iter) {
  2116. if (!ring_buffer_iter_empty(buf_iter))
  2117. return 0;
  2118. } else {
  2119. if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
  2120. return 0;
  2121. }
  2122. }
  2123. return 1;
  2124. }
  2125. /* Called with trace_event_read_lock() held. */
  2126. enum print_line_t print_trace_line(struct trace_iterator *iter)
  2127. {
  2128. enum print_line_t ret;
  2129. if (iter->lost_events &&
  2130. !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  2131. iter->cpu, iter->lost_events))
  2132. return TRACE_TYPE_PARTIAL_LINE;
  2133. if (iter->trace && iter->trace->print_line) {
  2134. ret = iter->trace->print_line(iter);
  2135. if (ret != TRACE_TYPE_UNHANDLED)
  2136. return ret;
  2137. }
  2138. if (iter->ent->type == TRACE_BPUTS &&
  2139. trace_flags & TRACE_ITER_PRINTK &&
  2140. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2141. return trace_print_bputs_msg_only(iter);
  2142. if (iter->ent->type == TRACE_BPRINT &&
  2143. trace_flags & TRACE_ITER_PRINTK &&
  2144. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2145. return trace_print_bprintk_msg_only(iter);
  2146. if (iter->ent->type == TRACE_PRINT &&
  2147. trace_flags & TRACE_ITER_PRINTK &&
  2148. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2149. return trace_print_printk_msg_only(iter);
  2150. if (trace_flags & TRACE_ITER_BIN)
  2151. return print_bin_fmt(iter);
  2152. if (trace_flags & TRACE_ITER_HEX)
  2153. return print_hex_fmt(iter);
  2154. if (trace_flags & TRACE_ITER_RAW)
  2155. return print_raw_fmt(iter);
  2156. return print_trace_fmt(iter);
  2157. }
  2158. void trace_latency_header(struct seq_file *m)
  2159. {
  2160. struct trace_iterator *iter = m->private;
  2161. /* print nothing if the buffers are empty */
  2162. if (trace_empty(iter))
  2163. return;
  2164. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  2165. print_trace_header(m, iter);
  2166. if (!(trace_flags & TRACE_ITER_VERBOSE))
  2167. print_lat_help_header(m);
  2168. }
  2169. void trace_default_header(struct seq_file *m)
  2170. {
  2171. struct trace_iterator *iter = m->private;
  2172. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  2173. return;
  2174. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  2175. /* print nothing if the buffers are empty */
  2176. if (trace_empty(iter))
  2177. return;
  2178. print_trace_header(m, iter);
  2179. if (!(trace_flags & TRACE_ITER_VERBOSE))
  2180. print_lat_help_header(m);
  2181. } else {
  2182. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  2183. if (trace_flags & TRACE_ITER_IRQ_INFO)
  2184. print_func_help_header_irq(iter->trace_buffer, m);
  2185. else
  2186. print_func_help_header(iter->trace_buffer, m);
  2187. }
  2188. }
  2189. }
  2190. static void test_ftrace_alive(struct seq_file *m)
  2191. {
  2192. if (!ftrace_is_dead())
  2193. return;
  2194. seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  2195. seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
  2196. }
  2197. #ifdef CONFIG_TRACER_MAX_TRACE
  2198. static void show_snapshot_main_help(struct seq_file *m)
  2199. {
  2200. seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
  2201. seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
  2202. seq_printf(m, "# Takes a snapshot of the main buffer.\n");
  2203. seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
  2204. seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
  2205. seq_printf(m, "# is not a '0' or '1')\n");
  2206. }
  2207. static void show_snapshot_percpu_help(struct seq_file *m)
  2208. {
  2209. seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
  2210. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  2211. seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
  2212. seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
  2213. #else
  2214. seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
  2215. seq_printf(m, "# Must use main snapshot file to allocate.\n");
  2216. #endif
  2217. seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
  2218. seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
  2219. seq_printf(m, "# is not a '0' or '1')\n");
  2220. }
  2221. static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
  2222. {
  2223. if (iter->tr->allocated_snapshot)
  2224. seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
  2225. else
  2226. seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
  2227. seq_printf(m, "# Snapshot commands:\n");
  2228. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  2229. show_snapshot_main_help(m);
  2230. else
  2231. show_snapshot_percpu_help(m);
  2232. }
  2233. #else
  2234. /* Should never be called */
  2235. static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
  2236. #endif
  2237. static int s_show(struct seq_file *m, void *v)
  2238. {
  2239. struct trace_iterator *iter = v;
  2240. int ret;
  2241. if (iter->ent == NULL) {
  2242. if (iter->tr) {
  2243. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  2244. seq_puts(m, "#\n");
  2245. test_ftrace_alive(m);
  2246. }
  2247. if (iter->snapshot && trace_empty(iter))
  2248. print_snapshot_help(m, iter);
  2249. else if (iter->trace && iter->trace->print_header)
  2250. iter->trace->print_header(m);
  2251. else
  2252. trace_default_header(m);
  2253. } else if (iter->leftover) {
  2254. /*
  2255. * If we filled the seq_file buffer earlier, we
  2256. * want to just show it now.
  2257. */
  2258. ret = trace_print_seq(m, &iter->seq);
  2259. /* ret should this time be zero, but you never know */
  2260. iter->leftover = ret;
  2261. } else {
  2262. print_trace_line(iter);
  2263. ret = trace_print_seq(m, &iter->seq);
  2264. /*
  2265. * If we overflow the seq_file buffer, then it will
  2266. * ask us for this data again at start up.
  2267. * Use that instead.
  2268. * ret is 0 if seq_file write succeeded.
  2269. * -1 otherwise.
  2270. */
  2271. iter->leftover = ret;
  2272. }
  2273. return 0;
  2274. }
  2275. static const struct seq_operations tracer_seq_ops = {
  2276. .start = s_start,
  2277. .next = s_next,
  2278. .stop = s_stop,
  2279. .show = s_show,
  2280. };
  2281. static struct trace_iterator *
  2282. __tracing_open(struct inode *inode, struct file *file, bool snapshot)
  2283. {
  2284. struct trace_cpu *tc = inode->i_private;
  2285. struct trace_array *tr = tc->tr;
  2286. struct trace_iterator *iter;
  2287. int cpu;
  2288. if (tracing_disabled)
  2289. return ERR_PTR(-ENODEV);
  2290. iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
  2291. if (!iter)
  2292. return ERR_PTR(-ENOMEM);
  2293. iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
  2294. GFP_KERNEL);
  2295. if (!iter->buffer_iter)
  2296. goto release;
  2297. /*
  2298. * We make a copy of the current tracer to avoid concurrent
  2299. * changes on it while we are reading.
  2300. */
  2301. mutex_lock(&trace_types_lock);
  2302. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  2303. if (!iter->trace)
  2304. goto fail;
  2305. *iter->trace = *tr->current_trace;
  2306. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  2307. goto fail;
  2308. iter->tr = tr;
  2309. #ifdef CONFIG_TRACER_MAX_TRACE
  2310. /* Currently only the top directory has a snapshot */
  2311. if (tr->current_trace->print_max || snapshot)
  2312. iter->trace_buffer = &tr->max_buffer;
  2313. else
  2314. #endif
  2315. iter->trace_buffer = &tr->trace_buffer;
  2316. iter->snapshot = snapshot;
  2317. iter->pos = -1;
  2318. mutex_init(&iter->mutex);
  2319. iter->cpu_file = tc->cpu;
  2320. /* Notify the tracer early; before we stop tracing. */
  2321. if (iter->trace && iter->trace->open)
  2322. iter->trace->open(iter);
  2323. /* Annotate start of buffers if we had overruns */
  2324. if (ring_buffer_overruns(iter->trace_buffer->buffer))
  2325. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  2326. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  2327. if (trace_clocks[trace_clock_id].in_ns)
  2328. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  2329. /* stop the trace while dumping if we are not opening "snapshot" */
  2330. if (!iter->snapshot)
  2331. tracing_stop_tr(tr);
  2332. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  2333. for_each_tracing_cpu(cpu) {
  2334. iter->buffer_iter[cpu] =
  2335. ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
  2336. }
  2337. ring_buffer_read_prepare_sync();
  2338. for_each_tracing_cpu(cpu) {
  2339. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2340. tracing_iter_reset(iter, cpu);
  2341. }
  2342. } else {
  2343. cpu = iter->cpu_file;
  2344. iter->buffer_iter[cpu] =
  2345. ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
  2346. ring_buffer_read_prepare_sync();
  2347. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2348. tracing_iter_reset(iter, cpu);
  2349. }
  2350. tr->ref++;
  2351. mutex_unlock(&trace_types_lock);
  2352. return iter;
  2353. fail:
  2354. mutex_unlock(&trace_types_lock);
  2355. kfree(iter->trace);
  2356. kfree(iter->buffer_iter);
  2357. release:
  2358. seq_release_private(inode, file);
  2359. return ERR_PTR(-ENOMEM);
  2360. }
  2361. int tracing_open_generic(struct inode *inode, struct file *filp)
  2362. {
  2363. if (tracing_disabled)
  2364. return -ENODEV;
  2365. filp->private_data = inode->i_private;
  2366. return 0;
  2367. }
  2368. static int tracing_release(struct inode *inode, struct file *file)
  2369. {
  2370. struct seq_file *m = file->private_data;
  2371. struct trace_iterator *iter;
  2372. struct trace_array *tr;
  2373. int cpu;
  2374. if (!(file->f_mode & FMODE_READ))
  2375. return 0;
  2376. iter = m->private;
  2377. tr = iter->tr;
  2378. mutex_lock(&trace_types_lock);
  2379. WARN_ON(!tr->ref);
  2380. tr->ref--;
  2381. for_each_tracing_cpu(cpu) {
  2382. if (iter->buffer_iter[cpu])
  2383. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2384. }
  2385. if (iter->trace && iter->trace->close)
  2386. iter->trace->close(iter);
  2387. if (!iter->snapshot)
  2388. /* reenable tracing if it was previously enabled */
  2389. tracing_start_tr(tr);
  2390. mutex_unlock(&trace_types_lock);
  2391. mutex_destroy(&iter->mutex);
  2392. free_cpumask_var(iter->started);
  2393. kfree(iter->trace);
  2394. kfree(iter->buffer_iter);
  2395. seq_release_private(inode, file);
  2396. return 0;
  2397. }
  2398. static int tracing_open(struct inode *inode, struct file *file)
  2399. {
  2400. struct trace_iterator *iter;
  2401. int ret = 0;
  2402. /* If this file was open for write, then erase contents */
  2403. if ((file->f_mode & FMODE_WRITE) &&
  2404. (file->f_flags & O_TRUNC)) {
  2405. struct trace_cpu *tc = inode->i_private;
  2406. struct trace_array *tr = tc->tr;
  2407. if (tc->cpu == RING_BUFFER_ALL_CPUS)
  2408. tracing_reset_online_cpus(&tr->trace_buffer);
  2409. else
  2410. tracing_reset(&tr->trace_buffer, tc->cpu);
  2411. }
  2412. if (file->f_mode & FMODE_READ) {
  2413. iter = __tracing_open(inode, file, false);
  2414. if (IS_ERR(iter))
  2415. ret = PTR_ERR(iter);
  2416. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2417. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2418. }
  2419. return ret;
  2420. }
  2421. static void *
  2422. t_next(struct seq_file *m, void *v, loff_t *pos)
  2423. {
  2424. struct tracer *t = v;
  2425. (*pos)++;
  2426. if (t)
  2427. t = t->next;
  2428. return t;
  2429. }
  2430. static void *t_start(struct seq_file *m, loff_t *pos)
  2431. {
  2432. struct tracer *t;
  2433. loff_t l = 0;
  2434. mutex_lock(&trace_types_lock);
  2435. for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
  2436. ;
  2437. return t;
  2438. }
  2439. static void t_stop(struct seq_file *m, void *p)
  2440. {
  2441. mutex_unlock(&trace_types_lock);
  2442. }
  2443. static int t_show(struct seq_file *m, void *v)
  2444. {
  2445. struct tracer *t = v;
  2446. if (!t)
  2447. return 0;
  2448. seq_printf(m, "%s", t->name);
  2449. if (t->next)
  2450. seq_putc(m, ' ');
  2451. else
  2452. seq_putc(m, '\n');
  2453. return 0;
  2454. }
  2455. static const struct seq_operations show_traces_seq_ops = {
  2456. .start = t_start,
  2457. .next = t_next,
  2458. .stop = t_stop,
  2459. .show = t_show,
  2460. };
  2461. static int show_traces_open(struct inode *inode, struct file *file)
  2462. {
  2463. if (tracing_disabled)
  2464. return -ENODEV;
  2465. return seq_open(file, &show_traces_seq_ops);
  2466. }
  2467. static ssize_t
  2468. tracing_write_stub(struct file *filp, const char __user *ubuf,
  2469. size_t count, loff_t *ppos)
  2470. {
  2471. return count;
  2472. }
  2473. static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
  2474. {
  2475. if (file->f_mode & FMODE_READ)
  2476. return seq_lseek(file, offset, origin);
  2477. else
  2478. return 0;
  2479. }
  2480. static const struct file_operations tracing_fops = {
  2481. .open = tracing_open,
  2482. .read = seq_read,
  2483. .write = tracing_write_stub,
  2484. .llseek = tracing_seek,
  2485. .release = tracing_release,
  2486. };
  2487. static const struct file_operations show_traces_fops = {
  2488. .open = show_traces_open,
  2489. .read = seq_read,
  2490. .release = seq_release,
  2491. .llseek = seq_lseek,
  2492. };
  2493. /*
  2494. * Only trace on a CPU if the bitmask is set:
  2495. */
  2496. static cpumask_var_t tracing_cpumask;
  2497. /*
  2498. * The tracer itself will not take this lock, but still we want
  2499. * to provide a consistent cpumask to user-space:
  2500. */
  2501. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2502. /*
  2503. * Temporary storage for the character representation of the
  2504. * CPU bitmask (and one more byte for the newline):
  2505. */
  2506. static char mask_str[NR_CPUS + 1];
  2507. static ssize_t
  2508. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2509. size_t count, loff_t *ppos)
  2510. {
  2511. int len;
  2512. mutex_lock(&tracing_cpumask_update_lock);
  2513. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2514. if (count - len < 2) {
  2515. count = -EINVAL;
  2516. goto out_err;
  2517. }
  2518. len += sprintf(mask_str + len, "\n");
  2519. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2520. out_err:
  2521. mutex_unlock(&tracing_cpumask_update_lock);
  2522. return count;
  2523. }
  2524. static ssize_t
  2525. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2526. size_t count, loff_t *ppos)
  2527. {
  2528. struct trace_array *tr = filp->private_data;
  2529. cpumask_var_t tracing_cpumask_new;
  2530. int err, cpu;
  2531. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  2532. return -ENOMEM;
  2533. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2534. if (err)
  2535. goto err_unlock;
  2536. mutex_lock(&tracing_cpumask_update_lock);
  2537. local_irq_disable();
  2538. arch_spin_lock(&ftrace_max_lock);
  2539. for_each_tracing_cpu(cpu) {
  2540. /*
  2541. * Increase/decrease the disabled counter if we are
  2542. * about to flip a bit in the cpumask:
  2543. */
  2544. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  2545. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2546. atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2547. ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
  2548. }
  2549. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  2550. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2551. atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2552. ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
  2553. }
  2554. }
  2555. arch_spin_unlock(&ftrace_max_lock);
  2556. local_irq_enable();
  2557. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  2558. mutex_unlock(&tracing_cpumask_update_lock);
  2559. free_cpumask_var(tracing_cpumask_new);
  2560. return count;
  2561. err_unlock:
  2562. free_cpumask_var(tracing_cpumask_new);
  2563. return err;
  2564. }
  2565. static const struct file_operations tracing_cpumask_fops = {
  2566. .open = tracing_open_generic,
  2567. .read = tracing_cpumask_read,
  2568. .write = tracing_cpumask_write,
  2569. .llseek = generic_file_llseek,
  2570. };
  2571. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2572. {
  2573. struct tracer_opt *trace_opts;
  2574. struct trace_array *tr = m->private;
  2575. u32 tracer_flags;
  2576. int i;
  2577. mutex_lock(&trace_types_lock);
  2578. tracer_flags = tr->current_trace->flags->val;
  2579. trace_opts = tr->current_trace->flags->opts;
  2580. for (i = 0; trace_options[i]; i++) {
  2581. if (trace_flags & (1 << i))
  2582. seq_printf(m, "%s\n", trace_options[i]);
  2583. else
  2584. seq_printf(m, "no%s\n", trace_options[i]);
  2585. }
  2586. for (i = 0; trace_opts[i].name; i++) {
  2587. if (tracer_flags & trace_opts[i].bit)
  2588. seq_printf(m, "%s\n", trace_opts[i].name);
  2589. else
  2590. seq_printf(m, "no%s\n", trace_opts[i].name);
  2591. }
  2592. mutex_unlock(&trace_types_lock);
  2593. return 0;
  2594. }
  2595. static int __set_tracer_option(struct tracer *trace,
  2596. struct tracer_flags *tracer_flags,
  2597. struct tracer_opt *opts, int neg)
  2598. {
  2599. int ret;
  2600. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2601. if (ret)
  2602. return ret;
  2603. if (neg)
  2604. tracer_flags->val &= ~opts->bit;
  2605. else
  2606. tracer_flags->val |= opts->bit;
  2607. return 0;
  2608. }
  2609. /* Try to assign a tracer specific option */
  2610. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2611. {
  2612. struct tracer_flags *tracer_flags = trace->flags;
  2613. struct tracer_opt *opts = NULL;
  2614. int i;
  2615. for (i = 0; tracer_flags->opts[i].name; i++) {
  2616. opts = &tracer_flags->opts[i];
  2617. if (strcmp(cmp, opts->name) == 0)
  2618. return __set_tracer_option(trace, trace->flags,
  2619. opts, neg);
  2620. }
  2621. return -EINVAL;
  2622. }
  2623. /* Some tracers require overwrite to stay enabled */
  2624. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  2625. {
  2626. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  2627. return -1;
  2628. return 0;
  2629. }
  2630. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
  2631. {
  2632. /* do nothing if flag is already set */
  2633. if (!!(trace_flags & mask) == !!enabled)
  2634. return 0;
  2635. /* Give the tracer a chance to approve the change */
  2636. if (tr->current_trace->flag_changed)
  2637. if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
  2638. return -EINVAL;
  2639. if (enabled)
  2640. trace_flags |= mask;
  2641. else
  2642. trace_flags &= ~mask;
  2643. if (mask == TRACE_ITER_RECORD_CMD)
  2644. trace_event_enable_cmd_record(enabled);
  2645. if (mask == TRACE_ITER_OVERWRITE) {
  2646. ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
  2647. #ifdef CONFIG_TRACER_MAX_TRACE
  2648. ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
  2649. #endif
  2650. }
  2651. if (mask == TRACE_ITER_PRINTK)
  2652. trace_printk_start_stop_comm(enabled);
  2653. return 0;
  2654. }
  2655. static int trace_set_options(struct trace_array *tr, char *option)
  2656. {
  2657. char *cmp;
  2658. int neg = 0;
  2659. int ret = -ENODEV;
  2660. int i;
  2661. cmp = strstrip(option);
  2662. if (strncmp(cmp, "no", 2) == 0) {
  2663. neg = 1;
  2664. cmp += 2;
  2665. }
  2666. mutex_lock(&trace_types_lock);
  2667. for (i = 0; trace_options[i]; i++) {
  2668. if (strcmp(cmp, trace_options[i]) == 0) {
  2669. ret = set_tracer_flag(tr, 1 << i, !neg);
  2670. break;
  2671. }
  2672. }
  2673. /* If no option could be set, test the specific tracer options */
  2674. if (!trace_options[i])
  2675. ret = set_tracer_option(tr->current_trace, cmp, neg);
  2676. mutex_unlock(&trace_types_lock);
  2677. return ret;
  2678. }
  2679. static ssize_t
  2680. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2681. size_t cnt, loff_t *ppos)
  2682. {
  2683. struct seq_file *m = filp->private_data;
  2684. struct trace_array *tr = m->private;
  2685. char buf[64];
  2686. int ret;
  2687. if (cnt >= sizeof(buf))
  2688. return -EINVAL;
  2689. if (copy_from_user(&buf, ubuf, cnt))
  2690. return -EFAULT;
  2691. buf[cnt] = 0;
  2692. ret = trace_set_options(tr, buf);
  2693. if (ret < 0)
  2694. return ret;
  2695. *ppos += cnt;
  2696. return cnt;
  2697. }
  2698. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2699. {
  2700. if (tracing_disabled)
  2701. return -ENODEV;
  2702. return single_open(file, tracing_trace_options_show, inode->i_private);
  2703. }
  2704. static const struct file_operations tracing_iter_fops = {
  2705. .open = tracing_trace_options_open,
  2706. .read = seq_read,
  2707. .llseek = seq_lseek,
  2708. .release = single_release,
  2709. .write = tracing_trace_options_write,
  2710. };
  2711. static const char readme_msg[] =
  2712. "tracing mini-HOWTO:\n\n"
  2713. "# mount -t debugfs nodev /sys/kernel/debug\n\n"
  2714. "# cat /sys/kernel/debug/tracing/available_tracers\n"
  2715. "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
  2716. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2717. "nop\n"
  2718. "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
  2719. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2720. "wakeup\n"
  2721. "# cat /sys/kernel/debug/tracing/trace_options\n"
  2722. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2723. "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
  2724. "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
  2725. "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
  2726. "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
  2727. ;
  2728. static ssize_t
  2729. tracing_readme_read(struct file *filp, char __user *ubuf,
  2730. size_t cnt, loff_t *ppos)
  2731. {
  2732. return simple_read_from_buffer(ubuf, cnt, ppos,
  2733. readme_msg, strlen(readme_msg));
  2734. }
  2735. static const struct file_operations tracing_readme_fops = {
  2736. .open = tracing_open_generic,
  2737. .read = tracing_readme_read,
  2738. .llseek = generic_file_llseek,
  2739. };
  2740. static ssize_t
  2741. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2742. size_t cnt, loff_t *ppos)
  2743. {
  2744. char *buf_comm;
  2745. char *file_buf;
  2746. char *buf;
  2747. int len = 0;
  2748. int pid;
  2749. int i;
  2750. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2751. if (!file_buf)
  2752. return -ENOMEM;
  2753. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2754. if (!buf_comm) {
  2755. kfree(file_buf);
  2756. return -ENOMEM;
  2757. }
  2758. buf = file_buf;
  2759. for (i = 0; i < SAVED_CMDLINES; i++) {
  2760. int r;
  2761. pid = map_cmdline_to_pid[i];
  2762. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2763. continue;
  2764. trace_find_cmdline(pid, buf_comm);
  2765. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2766. buf += r;
  2767. len += r;
  2768. }
  2769. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2770. file_buf, len);
  2771. kfree(file_buf);
  2772. kfree(buf_comm);
  2773. return len;
  2774. }
  2775. static const struct file_operations tracing_saved_cmdlines_fops = {
  2776. .open = tracing_open_generic,
  2777. .read = tracing_saved_cmdlines_read,
  2778. .llseek = generic_file_llseek,
  2779. };
  2780. static ssize_t
  2781. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2782. size_t cnt, loff_t *ppos)
  2783. {
  2784. struct trace_array *tr = filp->private_data;
  2785. char buf[MAX_TRACER_SIZE+2];
  2786. int r;
  2787. mutex_lock(&trace_types_lock);
  2788. r = sprintf(buf, "%s\n", tr->current_trace->name);
  2789. mutex_unlock(&trace_types_lock);
  2790. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2791. }
  2792. int tracer_init(struct tracer *t, struct trace_array *tr)
  2793. {
  2794. tracing_reset_online_cpus(&tr->trace_buffer);
  2795. return t->init(tr);
  2796. }
  2797. static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
  2798. {
  2799. int cpu;
  2800. for_each_tracing_cpu(cpu)
  2801. per_cpu_ptr(buf->data, cpu)->entries = val;
  2802. }
  2803. #ifdef CONFIG_TRACER_MAX_TRACE
  2804. /* resize @tr's buffer to the size of @size_tr's entries */
  2805. static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
  2806. struct trace_buffer *size_buf, int cpu_id)
  2807. {
  2808. int cpu, ret = 0;
  2809. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  2810. for_each_tracing_cpu(cpu) {
  2811. ret = ring_buffer_resize(trace_buf->buffer,
  2812. per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
  2813. if (ret < 0)
  2814. break;
  2815. per_cpu_ptr(trace_buf->data, cpu)->entries =
  2816. per_cpu_ptr(size_buf->data, cpu)->entries;
  2817. }
  2818. } else {
  2819. ret = ring_buffer_resize(trace_buf->buffer,
  2820. per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
  2821. if (ret == 0)
  2822. per_cpu_ptr(trace_buf->data, cpu_id)->entries =
  2823. per_cpu_ptr(size_buf->data, cpu_id)->entries;
  2824. }
  2825. return ret;
  2826. }
  2827. #endif /* CONFIG_TRACER_MAX_TRACE */
  2828. static int __tracing_resize_ring_buffer(struct trace_array *tr,
  2829. unsigned long size, int cpu)
  2830. {
  2831. int ret;
  2832. /*
  2833. * If kernel or user changes the size of the ring buffer
  2834. * we use the size that was given, and we can forget about
  2835. * expanding it later.
  2836. */
  2837. ring_buffer_expanded = true;
  2838. /* May be called before buffers are initialized */
  2839. if (!tr->trace_buffer.buffer)
  2840. return 0;
  2841. ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
  2842. if (ret < 0)
  2843. return ret;
  2844. #ifdef CONFIG_TRACER_MAX_TRACE
  2845. if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
  2846. !tr->current_trace->use_max_tr)
  2847. goto out;
  2848. ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
  2849. if (ret < 0) {
  2850. int r = resize_buffer_duplicate_size(&tr->trace_buffer,
  2851. &tr->trace_buffer, cpu);
  2852. if (r < 0) {
  2853. /*
  2854. * AARGH! We are left with different
  2855. * size max buffer!!!!
  2856. * The max buffer is our "snapshot" buffer.
  2857. * When a tracer needs a snapshot (one of the
  2858. * latency tracers), it swaps the max buffer
  2859. * with the saved snap shot. We succeeded to
  2860. * update the size of the main buffer, but failed to
  2861. * update the size of the max buffer. But when we tried
  2862. * to reset the main buffer to the original size, we
  2863. * failed there too. This is very unlikely to
  2864. * happen, but if it does, warn and kill all
  2865. * tracing.
  2866. */
  2867. WARN_ON(1);
  2868. tracing_disabled = 1;
  2869. }
  2870. return ret;
  2871. }
  2872. if (cpu == RING_BUFFER_ALL_CPUS)
  2873. set_buffer_entries(&tr->max_buffer, size);
  2874. else
  2875. per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
  2876. out:
  2877. #endif /* CONFIG_TRACER_MAX_TRACE */
  2878. if (cpu == RING_BUFFER_ALL_CPUS)
  2879. set_buffer_entries(&tr->trace_buffer, size);
  2880. else
  2881. per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
  2882. return ret;
  2883. }
  2884. static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
  2885. unsigned long size, int cpu_id)
  2886. {
  2887. int ret = size;
  2888. mutex_lock(&trace_types_lock);
  2889. if (cpu_id != RING_BUFFER_ALL_CPUS) {
  2890. /* make sure, this cpu is enabled in the mask */
  2891. if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
  2892. ret = -EINVAL;
  2893. goto out;
  2894. }
  2895. }
  2896. ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
  2897. if (ret < 0)
  2898. ret = -ENOMEM;
  2899. out:
  2900. mutex_unlock(&trace_types_lock);
  2901. return ret;
  2902. }
  2903. /**
  2904. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2905. *
  2906. * To save on memory when the tracing is never used on a system with it
  2907. * configured in. The ring buffers are set to a minimum size. But once
  2908. * a user starts to use the tracing facility, then they need to grow
  2909. * to their default size.
  2910. *
  2911. * This function is to be called when a tracer is about to be used.
  2912. */
  2913. int tracing_update_buffers(void)
  2914. {
  2915. int ret = 0;
  2916. mutex_lock(&trace_types_lock);
  2917. if (!ring_buffer_expanded)
  2918. ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
  2919. RING_BUFFER_ALL_CPUS);
  2920. mutex_unlock(&trace_types_lock);
  2921. return ret;
  2922. }
  2923. struct trace_option_dentry;
  2924. static struct trace_option_dentry *
  2925. create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
  2926. static void
  2927. destroy_trace_option_files(struct trace_option_dentry *topts);
  2928. static int tracing_set_tracer(const char *buf)
  2929. {
  2930. static struct trace_option_dentry *topts;
  2931. struct trace_array *tr = &global_trace;
  2932. struct tracer *t;
  2933. #ifdef CONFIG_TRACER_MAX_TRACE
  2934. bool had_max_tr;
  2935. #endif
  2936. int ret = 0;
  2937. mutex_lock(&trace_types_lock);
  2938. if (!ring_buffer_expanded) {
  2939. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  2940. RING_BUFFER_ALL_CPUS);
  2941. if (ret < 0)
  2942. goto out;
  2943. ret = 0;
  2944. }
  2945. for (t = trace_types; t; t = t->next) {
  2946. if (strcmp(t->name, buf) == 0)
  2947. break;
  2948. }
  2949. if (!t) {
  2950. ret = -EINVAL;
  2951. goto out;
  2952. }
  2953. if (t == tr->current_trace)
  2954. goto out;
  2955. trace_branch_disable();
  2956. tr->current_trace->enabled = false;
  2957. if (tr->current_trace->reset)
  2958. tr->current_trace->reset(tr);
  2959. /* Current trace needs to be nop_trace before synchronize_sched */
  2960. tr->current_trace = &nop_trace;
  2961. #ifdef CONFIG_TRACER_MAX_TRACE
  2962. had_max_tr = tr->allocated_snapshot;
  2963. if (had_max_tr && !t->use_max_tr) {
  2964. /*
  2965. * We need to make sure that the update_max_tr sees that
  2966. * current_trace changed to nop_trace to keep it from
  2967. * swapping the buffers after we resize it.
  2968. * The update_max_tr is called from interrupts disabled
  2969. * so a synchronized_sched() is sufficient.
  2970. */
  2971. synchronize_sched();
  2972. free_snapshot(tr);
  2973. }
  2974. #endif
  2975. destroy_trace_option_files(topts);
  2976. topts = create_trace_option_files(tr, t);
  2977. #ifdef CONFIG_TRACER_MAX_TRACE
  2978. if (t->use_max_tr && !had_max_tr) {
  2979. ret = alloc_snapshot(tr);
  2980. if (ret < 0)
  2981. goto out;
  2982. }
  2983. #endif
  2984. if (t->init) {
  2985. ret = tracer_init(t, tr);
  2986. if (ret)
  2987. goto out;
  2988. }
  2989. tr->current_trace = t;
  2990. tr->current_trace->enabled = true;
  2991. trace_branch_enable(tr);
  2992. out:
  2993. mutex_unlock(&trace_types_lock);
  2994. return ret;
  2995. }
  2996. static ssize_t
  2997. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2998. size_t cnt, loff_t *ppos)
  2999. {
  3000. char buf[MAX_TRACER_SIZE+1];
  3001. int i;
  3002. size_t ret;
  3003. int err;
  3004. ret = cnt;
  3005. if (cnt > MAX_TRACER_SIZE)
  3006. cnt = MAX_TRACER_SIZE;
  3007. if (copy_from_user(&buf, ubuf, cnt))
  3008. return -EFAULT;
  3009. buf[cnt] = 0;
  3010. /* strip ending whitespace. */
  3011. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  3012. buf[i] = 0;
  3013. err = tracing_set_tracer(buf);
  3014. if (err)
  3015. return err;
  3016. *ppos += ret;
  3017. return ret;
  3018. }
  3019. static ssize_t
  3020. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  3021. size_t cnt, loff_t *ppos)
  3022. {
  3023. unsigned long *ptr = filp->private_data;
  3024. char buf[64];
  3025. int r;
  3026. r = snprintf(buf, sizeof(buf), "%ld\n",
  3027. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  3028. if (r > sizeof(buf))
  3029. r = sizeof(buf);
  3030. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3031. }
  3032. static ssize_t
  3033. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  3034. size_t cnt, loff_t *ppos)
  3035. {
  3036. unsigned long *ptr = filp->private_data;
  3037. unsigned long val;
  3038. int ret;
  3039. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3040. if (ret)
  3041. return ret;
  3042. *ptr = val * 1000;
  3043. return cnt;
  3044. }
  3045. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  3046. {
  3047. struct trace_cpu *tc = inode->i_private;
  3048. struct trace_array *tr = tc->tr;
  3049. struct trace_iterator *iter;
  3050. int ret = 0;
  3051. if (tracing_disabled)
  3052. return -ENODEV;
  3053. mutex_lock(&trace_types_lock);
  3054. /* create a buffer to store the information to pass to userspace */
  3055. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3056. if (!iter) {
  3057. ret = -ENOMEM;
  3058. goto out;
  3059. }
  3060. /*
  3061. * We make a copy of the current tracer to avoid concurrent
  3062. * changes on it while we are reading.
  3063. */
  3064. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  3065. if (!iter->trace) {
  3066. ret = -ENOMEM;
  3067. goto fail;
  3068. }
  3069. *iter->trace = *tr->current_trace;
  3070. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  3071. ret = -ENOMEM;
  3072. goto fail;
  3073. }
  3074. /* trace pipe does not show start of buffer */
  3075. cpumask_setall(iter->started);
  3076. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  3077. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  3078. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  3079. if (trace_clocks[trace_clock_id].in_ns)
  3080. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  3081. iter->cpu_file = tc->cpu;
  3082. iter->tr = tc->tr;
  3083. iter->trace_buffer = &tc->tr->trace_buffer;
  3084. mutex_init(&iter->mutex);
  3085. filp->private_data = iter;
  3086. if (iter->trace->pipe_open)
  3087. iter->trace->pipe_open(iter);
  3088. nonseekable_open(inode, filp);
  3089. out:
  3090. mutex_unlock(&trace_types_lock);
  3091. return ret;
  3092. fail:
  3093. kfree(iter->trace);
  3094. kfree(iter);
  3095. mutex_unlock(&trace_types_lock);
  3096. return ret;
  3097. }
  3098. static int tracing_release_pipe(struct inode *inode, struct file *file)
  3099. {
  3100. struct trace_iterator *iter = file->private_data;
  3101. mutex_lock(&trace_types_lock);
  3102. if (iter->trace->pipe_close)
  3103. iter->trace->pipe_close(iter);
  3104. mutex_unlock(&trace_types_lock);
  3105. free_cpumask_var(iter->started);
  3106. mutex_destroy(&iter->mutex);
  3107. kfree(iter->trace);
  3108. kfree(iter);
  3109. return 0;
  3110. }
  3111. static unsigned int
  3112. trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
  3113. {
  3114. /* Iterators are static, they should be filled or empty */
  3115. if (trace_buffer_iter(iter, iter->cpu_file))
  3116. return POLLIN | POLLRDNORM;
  3117. if (trace_flags & TRACE_ITER_BLOCK)
  3118. /*
  3119. * Always select as readable when in blocking mode
  3120. */
  3121. return POLLIN | POLLRDNORM;
  3122. else
  3123. return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
  3124. filp, poll_table);
  3125. }
  3126. static unsigned int
  3127. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  3128. {
  3129. struct trace_iterator *iter = filp->private_data;
  3130. return trace_poll(iter, filp, poll_table);
  3131. }
  3132. /*
  3133. * This is a make-shift waitqueue.
  3134. * A tracer might use this callback on some rare cases:
  3135. *
  3136. * 1) the current tracer might hold the runqueue lock when it wakes up
  3137. * a reader, hence a deadlock (sched, function, and function graph tracers)
  3138. * 2) the function tracers, trace all functions, we don't want
  3139. * the overhead of calling wake_up and friends
  3140. * (and tracing them too)
  3141. *
  3142. * Anyway, this is really very primitive wakeup.
  3143. */
  3144. void poll_wait_pipe(struct trace_iterator *iter)
  3145. {
  3146. set_current_state(TASK_INTERRUPTIBLE);
  3147. /* sleep for 100 msecs, and try again. */
  3148. schedule_timeout(HZ / 10);
  3149. }
  3150. /* Must be called with trace_types_lock mutex held. */
  3151. static int tracing_wait_pipe(struct file *filp)
  3152. {
  3153. struct trace_iterator *iter = filp->private_data;
  3154. while (trace_empty(iter)) {
  3155. if ((filp->f_flags & O_NONBLOCK)) {
  3156. return -EAGAIN;
  3157. }
  3158. mutex_unlock(&iter->mutex);
  3159. iter->trace->wait_pipe(iter);
  3160. mutex_lock(&iter->mutex);
  3161. if (signal_pending(current))
  3162. return -EINTR;
  3163. /*
  3164. * We block until we read something and tracing is disabled.
  3165. * We still block if tracing is disabled, but we have never
  3166. * read anything. This allows a user to cat this file, and
  3167. * then enable tracing. But after we have read something,
  3168. * we give an EOF when tracing is again disabled.
  3169. *
  3170. * iter->pos will be 0 if we haven't read anything.
  3171. */
  3172. if (!tracing_is_enabled() && iter->pos)
  3173. break;
  3174. }
  3175. return 1;
  3176. }
  3177. /*
  3178. * Consumer reader.
  3179. */
  3180. static ssize_t
  3181. tracing_read_pipe(struct file *filp, char __user *ubuf,
  3182. size_t cnt, loff_t *ppos)
  3183. {
  3184. struct trace_iterator *iter = filp->private_data;
  3185. struct trace_array *tr = iter->tr;
  3186. ssize_t sret;
  3187. /* return any leftover data */
  3188. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3189. if (sret != -EBUSY)
  3190. return sret;
  3191. trace_seq_init(&iter->seq);
  3192. /* copy the tracer to avoid using a global lock all around */
  3193. mutex_lock(&trace_types_lock);
  3194. if (unlikely(iter->trace->name != tr->current_trace->name))
  3195. *iter->trace = *tr->current_trace;
  3196. mutex_unlock(&trace_types_lock);
  3197. /*
  3198. * Avoid more than one consumer on a single file descriptor
  3199. * This is just a matter of traces coherency, the ring buffer itself
  3200. * is protected.
  3201. */
  3202. mutex_lock(&iter->mutex);
  3203. if (iter->trace->read) {
  3204. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  3205. if (sret)
  3206. goto out;
  3207. }
  3208. waitagain:
  3209. sret = tracing_wait_pipe(filp);
  3210. if (sret <= 0)
  3211. goto out;
  3212. /* stop when tracing is finished */
  3213. if (trace_empty(iter)) {
  3214. sret = 0;
  3215. goto out;
  3216. }
  3217. if (cnt >= PAGE_SIZE)
  3218. cnt = PAGE_SIZE - 1;
  3219. /* reset all but tr, trace, and overruns */
  3220. memset(&iter->seq, 0,
  3221. sizeof(struct trace_iterator) -
  3222. offsetof(struct trace_iterator, seq));
  3223. iter->pos = -1;
  3224. trace_event_read_lock();
  3225. trace_access_lock(iter->cpu_file);
  3226. while (trace_find_next_entry_inc(iter) != NULL) {
  3227. enum print_line_t ret;
  3228. int len = iter->seq.len;
  3229. ret = print_trace_line(iter);
  3230. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3231. /* don't print partial lines */
  3232. iter->seq.len = len;
  3233. break;
  3234. }
  3235. if (ret != TRACE_TYPE_NO_CONSUME)
  3236. trace_consume(iter);
  3237. if (iter->seq.len >= cnt)
  3238. break;
  3239. /*
  3240. * Setting the full flag means we reached the trace_seq buffer
  3241. * size and we should leave by partial output condition above.
  3242. * One of the trace_seq_* functions is not used properly.
  3243. */
  3244. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  3245. iter->ent->type);
  3246. }
  3247. trace_access_unlock(iter->cpu_file);
  3248. trace_event_read_unlock();
  3249. /* Now copy what we have to the user */
  3250. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3251. if (iter->seq.readpos >= iter->seq.len)
  3252. trace_seq_init(&iter->seq);
  3253. /*
  3254. * If there was nothing to send to user, in spite of consuming trace
  3255. * entries, go back to wait for more entries.
  3256. */
  3257. if (sret == -EBUSY)
  3258. goto waitagain;
  3259. out:
  3260. mutex_unlock(&iter->mutex);
  3261. return sret;
  3262. }
  3263. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  3264. struct pipe_buffer *buf)
  3265. {
  3266. __free_page(buf->page);
  3267. }
  3268. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  3269. unsigned int idx)
  3270. {
  3271. __free_page(spd->pages[idx]);
  3272. }
  3273. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  3274. .can_merge = 0,
  3275. .map = generic_pipe_buf_map,
  3276. .unmap = generic_pipe_buf_unmap,
  3277. .confirm = generic_pipe_buf_confirm,
  3278. .release = tracing_pipe_buf_release,
  3279. .steal = generic_pipe_buf_steal,
  3280. .get = generic_pipe_buf_get,
  3281. };
  3282. static size_t
  3283. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  3284. {
  3285. size_t count;
  3286. int ret;
  3287. /* Seq buffer is page-sized, exactly what we need. */
  3288. for (;;) {
  3289. count = iter->seq.len;
  3290. ret = print_trace_line(iter);
  3291. count = iter->seq.len - count;
  3292. if (rem < count) {
  3293. rem = 0;
  3294. iter->seq.len -= count;
  3295. break;
  3296. }
  3297. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3298. iter->seq.len -= count;
  3299. break;
  3300. }
  3301. if (ret != TRACE_TYPE_NO_CONSUME)
  3302. trace_consume(iter);
  3303. rem -= count;
  3304. if (!trace_find_next_entry_inc(iter)) {
  3305. rem = 0;
  3306. iter->ent = NULL;
  3307. break;
  3308. }
  3309. }
  3310. return rem;
  3311. }
  3312. static ssize_t tracing_splice_read_pipe(struct file *filp,
  3313. loff_t *ppos,
  3314. struct pipe_inode_info *pipe,
  3315. size_t len,
  3316. unsigned int flags)
  3317. {
  3318. struct page *pages_def[PIPE_DEF_BUFFERS];
  3319. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3320. struct trace_iterator *iter = filp->private_data;
  3321. struct splice_pipe_desc spd = {
  3322. .pages = pages_def,
  3323. .partial = partial_def,
  3324. .nr_pages = 0, /* This gets updated below. */
  3325. .nr_pages_max = PIPE_DEF_BUFFERS,
  3326. .flags = flags,
  3327. .ops = &tracing_pipe_buf_ops,
  3328. .spd_release = tracing_spd_release_pipe,
  3329. };
  3330. struct trace_array *tr = iter->tr;
  3331. ssize_t ret;
  3332. size_t rem;
  3333. unsigned int i;
  3334. if (splice_grow_spd(pipe, &spd))
  3335. return -ENOMEM;
  3336. /* copy the tracer to avoid using a global lock all around */
  3337. mutex_lock(&trace_types_lock);
  3338. if (unlikely(iter->trace->name != tr->current_trace->name))
  3339. *iter->trace = *tr->current_trace;
  3340. mutex_unlock(&trace_types_lock);
  3341. mutex_lock(&iter->mutex);
  3342. if (iter->trace->splice_read) {
  3343. ret = iter->trace->splice_read(iter, filp,
  3344. ppos, pipe, len, flags);
  3345. if (ret)
  3346. goto out_err;
  3347. }
  3348. ret = tracing_wait_pipe(filp);
  3349. if (ret <= 0)
  3350. goto out_err;
  3351. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  3352. ret = -EFAULT;
  3353. goto out_err;
  3354. }
  3355. trace_event_read_lock();
  3356. trace_access_lock(iter->cpu_file);
  3357. /* Fill as many pages as possible. */
  3358. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  3359. spd.pages[i] = alloc_page(GFP_KERNEL);
  3360. if (!spd.pages[i])
  3361. break;
  3362. rem = tracing_fill_pipe_page(rem, iter);
  3363. /* Copy the data into the page, so we can start over. */
  3364. ret = trace_seq_to_buffer(&iter->seq,
  3365. page_address(spd.pages[i]),
  3366. iter->seq.len);
  3367. if (ret < 0) {
  3368. __free_page(spd.pages[i]);
  3369. break;
  3370. }
  3371. spd.partial[i].offset = 0;
  3372. spd.partial[i].len = iter->seq.len;
  3373. trace_seq_init(&iter->seq);
  3374. }
  3375. trace_access_unlock(iter->cpu_file);
  3376. trace_event_read_unlock();
  3377. mutex_unlock(&iter->mutex);
  3378. spd.nr_pages = i;
  3379. ret = splice_to_pipe(pipe, &spd);
  3380. out:
  3381. splice_shrink_spd(&spd);
  3382. return ret;
  3383. out_err:
  3384. mutex_unlock(&iter->mutex);
  3385. goto out;
  3386. }
  3387. static ssize_t
  3388. tracing_entries_read(struct file *filp, char __user *ubuf,
  3389. size_t cnt, loff_t *ppos)
  3390. {
  3391. struct trace_cpu *tc = filp->private_data;
  3392. struct trace_array *tr = tc->tr;
  3393. char buf[64];
  3394. int r = 0;
  3395. ssize_t ret;
  3396. mutex_lock(&trace_types_lock);
  3397. if (tc->cpu == RING_BUFFER_ALL_CPUS) {
  3398. int cpu, buf_size_same;
  3399. unsigned long size;
  3400. size = 0;
  3401. buf_size_same = 1;
  3402. /* check if all cpu sizes are same */
  3403. for_each_tracing_cpu(cpu) {
  3404. /* fill in the size from first enabled cpu */
  3405. if (size == 0)
  3406. size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
  3407. if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
  3408. buf_size_same = 0;
  3409. break;
  3410. }
  3411. }
  3412. if (buf_size_same) {
  3413. if (!ring_buffer_expanded)
  3414. r = sprintf(buf, "%lu (expanded: %lu)\n",
  3415. size >> 10,
  3416. trace_buf_size >> 10);
  3417. else
  3418. r = sprintf(buf, "%lu\n", size >> 10);
  3419. } else
  3420. r = sprintf(buf, "X\n");
  3421. } else
  3422. r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
  3423. mutex_unlock(&trace_types_lock);
  3424. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3425. return ret;
  3426. }
  3427. static ssize_t
  3428. tracing_entries_write(struct file *filp, const char __user *ubuf,
  3429. size_t cnt, loff_t *ppos)
  3430. {
  3431. struct trace_cpu *tc = filp->private_data;
  3432. unsigned long val;
  3433. int ret;
  3434. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3435. if (ret)
  3436. return ret;
  3437. /* must have at least 1 entry */
  3438. if (!val)
  3439. return -EINVAL;
  3440. /* value is in KB */
  3441. val <<= 10;
  3442. ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
  3443. if (ret < 0)
  3444. return ret;
  3445. *ppos += cnt;
  3446. return cnt;
  3447. }
  3448. static ssize_t
  3449. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  3450. size_t cnt, loff_t *ppos)
  3451. {
  3452. struct trace_array *tr = filp->private_data;
  3453. char buf[64];
  3454. int r, cpu;
  3455. unsigned long size = 0, expanded_size = 0;
  3456. mutex_lock(&trace_types_lock);
  3457. for_each_tracing_cpu(cpu) {
  3458. size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
  3459. if (!ring_buffer_expanded)
  3460. expanded_size += trace_buf_size >> 10;
  3461. }
  3462. if (ring_buffer_expanded)
  3463. r = sprintf(buf, "%lu\n", size);
  3464. else
  3465. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  3466. mutex_unlock(&trace_types_lock);
  3467. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3468. }
  3469. static ssize_t
  3470. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  3471. size_t cnt, loff_t *ppos)
  3472. {
  3473. /*
  3474. * There is no need to read what the user has written, this function
  3475. * is just to make sure that there is no error when "echo" is used
  3476. */
  3477. *ppos += cnt;
  3478. return cnt;
  3479. }
  3480. static int
  3481. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  3482. {
  3483. struct trace_array *tr = inode->i_private;
  3484. /* disable tracing ? */
  3485. if (trace_flags & TRACE_ITER_STOP_ON_FREE)
  3486. tracing_off();
  3487. /* resize the ring buffer to 0 */
  3488. tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
  3489. return 0;
  3490. }
  3491. static ssize_t
  3492. tracing_mark_write(struct file *filp, const char __user *ubuf,
  3493. size_t cnt, loff_t *fpos)
  3494. {
  3495. unsigned long addr = (unsigned long)ubuf;
  3496. struct ring_buffer_event *event;
  3497. struct ring_buffer *buffer;
  3498. struct print_entry *entry;
  3499. unsigned long irq_flags;
  3500. struct page *pages[2];
  3501. void *map_page[2];
  3502. int nr_pages = 1;
  3503. ssize_t written;
  3504. int offset;
  3505. int size;
  3506. int len;
  3507. int ret;
  3508. int i;
  3509. if (tracing_disabled)
  3510. return -EINVAL;
  3511. if (!(trace_flags & TRACE_ITER_MARKERS))
  3512. return -EINVAL;
  3513. if (cnt > TRACE_BUF_SIZE)
  3514. cnt = TRACE_BUF_SIZE;
  3515. /*
  3516. * Userspace is injecting traces into the kernel trace buffer.
  3517. * We want to be as non intrusive as possible.
  3518. * To do so, we do not want to allocate any special buffers
  3519. * or take any locks, but instead write the userspace data
  3520. * straight into the ring buffer.
  3521. *
  3522. * First we need to pin the userspace buffer into memory,
  3523. * which, most likely it is, because it just referenced it.
  3524. * But there's no guarantee that it is. By using get_user_pages_fast()
  3525. * and kmap_atomic/kunmap_atomic() we can get access to the
  3526. * pages directly. We then write the data directly into the
  3527. * ring buffer.
  3528. */
  3529. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  3530. /* check if we cross pages */
  3531. if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
  3532. nr_pages = 2;
  3533. offset = addr & (PAGE_SIZE - 1);
  3534. addr &= PAGE_MASK;
  3535. ret = get_user_pages_fast(addr, nr_pages, 0, pages);
  3536. if (ret < nr_pages) {
  3537. while (--ret >= 0)
  3538. put_page(pages[ret]);
  3539. written = -EFAULT;
  3540. goto out;
  3541. }
  3542. for (i = 0; i < nr_pages; i++)
  3543. map_page[i] = kmap_atomic(pages[i]);
  3544. local_save_flags(irq_flags);
  3545. size = sizeof(*entry) + cnt + 2; /* possible \n added */
  3546. buffer = global_trace.trace_buffer.buffer;
  3547. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  3548. irq_flags, preempt_count());
  3549. if (!event) {
  3550. /* Ring buffer disabled, return as if not open for write */
  3551. written = -EBADF;
  3552. goto out_unlock;
  3553. }
  3554. entry = ring_buffer_event_data(event);
  3555. entry->ip = _THIS_IP_;
  3556. if (nr_pages == 2) {
  3557. len = PAGE_SIZE - offset;
  3558. memcpy(&entry->buf, map_page[0] + offset, len);
  3559. memcpy(&entry->buf[len], map_page[1], cnt - len);
  3560. } else
  3561. memcpy(&entry->buf, map_page[0] + offset, cnt);
  3562. if (entry->buf[cnt - 1] != '\n') {
  3563. entry->buf[cnt] = '\n';
  3564. entry->buf[cnt + 1] = '\0';
  3565. } else
  3566. entry->buf[cnt] = '\0';
  3567. __buffer_unlock_commit(buffer, event);
  3568. written = cnt;
  3569. *fpos += written;
  3570. out_unlock:
  3571. for (i = 0; i < nr_pages; i++){
  3572. kunmap_atomic(map_page[i]);
  3573. put_page(pages[i]);
  3574. }
  3575. out:
  3576. return written;
  3577. }
  3578. static int tracing_clock_show(struct seq_file *m, void *v)
  3579. {
  3580. struct trace_array *tr = m->private;
  3581. int i;
  3582. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  3583. seq_printf(m,
  3584. "%s%s%s%s", i ? " " : "",
  3585. i == tr->clock_id ? "[" : "", trace_clocks[i].name,
  3586. i == tr->clock_id ? "]" : "");
  3587. seq_putc(m, '\n');
  3588. return 0;
  3589. }
  3590. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  3591. size_t cnt, loff_t *fpos)
  3592. {
  3593. struct seq_file *m = filp->private_data;
  3594. struct trace_array *tr = m->private;
  3595. char buf[64];
  3596. const char *clockstr;
  3597. int i;
  3598. if (cnt >= sizeof(buf))
  3599. return -EINVAL;
  3600. if (copy_from_user(&buf, ubuf, cnt))
  3601. return -EFAULT;
  3602. buf[cnt] = 0;
  3603. clockstr = strstrip(buf);
  3604. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  3605. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  3606. break;
  3607. }
  3608. if (i == ARRAY_SIZE(trace_clocks))
  3609. return -EINVAL;
  3610. mutex_lock(&trace_types_lock);
  3611. tr->clock_id = i;
  3612. ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
  3613. /*
  3614. * New clock may not be consistent with the previous clock.
  3615. * Reset the buffer so that it doesn't have incomparable timestamps.
  3616. */
  3617. tracing_reset_online_cpus(&global_trace.trace_buffer);
  3618. #ifdef CONFIG_TRACER_MAX_TRACE
  3619. if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
  3620. ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
  3621. tracing_reset_online_cpus(&global_trace.max_buffer);
  3622. #endif
  3623. mutex_unlock(&trace_types_lock);
  3624. *fpos += cnt;
  3625. return cnt;
  3626. }
  3627. static int tracing_clock_open(struct inode *inode, struct file *file)
  3628. {
  3629. if (tracing_disabled)
  3630. return -ENODEV;
  3631. return single_open(file, tracing_clock_show, inode->i_private);
  3632. }
  3633. struct ftrace_buffer_info {
  3634. struct trace_iterator iter;
  3635. void *spare;
  3636. unsigned int read;
  3637. };
  3638. #ifdef CONFIG_TRACER_SNAPSHOT
  3639. static int tracing_snapshot_open(struct inode *inode, struct file *file)
  3640. {
  3641. struct trace_cpu *tc = inode->i_private;
  3642. struct trace_iterator *iter;
  3643. struct seq_file *m;
  3644. int ret = 0;
  3645. if (file->f_mode & FMODE_READ) {
  3646. iter = __tracing_open(inode, file, true);
  3647. if (IS_ERR(iter))
  3648. ret = PTR_ERR(iter);
  3649. } else {
  3650. /* Writes still need the seq_file to hold the private data */
  3651. m = kzalloc(sizeof(*m), GFP_KERNEL);
  3652. if (!m)
  3653. return -ENOMEM;
  3654. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3655. if (!iter) {
  3656. kfree(m);
  3657. return -ENOMEM;
  3658. }
  3659. iter->tr = tc->tr;
  3660. iter->trace_buffer = &tc->tr->max_buffer;
  3661. iter->cpu_file = tc->cpu;
  3662. m->private = iter;
  3663. file->private_data = m;
  3664. }
  3665. return ret;
  3666. }
  3667. static ssize_t
  3668. tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3669. loff_t *ppos)
  3670. {
  3671. struct seq_file *m = filp->private_data;
  3672. struct trace_iterator *iter = m->private;
  3673. struct trace_array *tr = iter->tr;
  3674. unsigned long val;
  3675. int ret;
  3676. ret = tracing_update_buffers();
  3677. if (ret < 0)
  3678. return ret;
  3679. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3680. if (ret)
  3681. return ret;
  3682. mutex_lock(&trace_types_lock);
  3683. if (tr->current_trace->use_max_tr) {
  3684. ret = -EBUSY;
  3685. goto out;
  3686. }
  3687. switch (val) {
  3688. case 0:
  3689. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3690. ret = -EINVAL;
  3691. break;
  3692. }
  3693. if (tr->allocated_snapshot)
  3694. free_snapshot(tr);
  3695. break;
  3696. case 1:
  3697. /* Only allow per-cpu swap if the ring buffer supports it */
  3698. #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
  3699. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3700. ret = -EINVAL;
  3701. break;
  3702. }
  3703. #endif
  3704. if (!tr->allocated_snapshot) {
  3705. ret = alloc_snapshot(tr);
  3706. if (ret < 0)
  3707. break;
  3708. }
  3709. local_irq_disable();
  3710. /* Now, we're going to swap */
  3711. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3712. update_max_tr(tr, current, smp_processor_id());
  3713. else
  3714. update_max_tr_single(tr, current, iter->cpu_file);
  3715. local_irq_enable();
  3716. break;
  3717. default:
  3718. if (tr->allocated_snapshot) {
  3719. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3720. tracing_reset_online_cpus(&tr->max_buffer);
  3721. else
  3722. tracing_reset(&tr->max_buffer, iter->cpu_file);
  3723. }
  3724. break;
  3725. }
  3726. if (ret >= 0) {
  3727. *ppos += cnt;
  3728. ret = cnt;
  3729. }
  3730. out:
  3731. mutex_unlock(&trace_types_lock);
  3732. return ret;
  3733. }
  3734. static int tracing_snapshot_release(struct inode *inode, struct file *file)
  3735. {
  3736. struct seq_file *m = file->private_data;
  3737. if (file->f_mode & FMODE_READ)
  3738. return tracing_release(inode, file);
  3739. /* If write only, the seq_file is just a stub */
  3740. if (m)
  3741. kfree(m->private);
  3742. kfree(m);
  3743. return 0;
  3744. }
  3745. static int tracing_buffers_open(struct inode *inode, struct file *filp);
  3746. static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
  3747. size_t count, loff_t *ppos);
  3748. static int tracing_buffers_release(struct inode *inode, struct file *file);
  3749. static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3750. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  3751. static int snapshot_raw_open(struct inode *inode, struct file *filp)
  3752. {
  3753. struct ftrace_buffer_info *info;
  3754. int ret;
  3755. ret = tracing_buffers_open(inode, filp);
  3756. if (ret < 0)
  3757. return ret;
  3758. info = filp->private_data;
  3759. if (info->iter.trace->use_max_tr) {
  3760. tracing_buffers_release(inode, filp);
  3761. return -EBUSY;
  3762. }
  3763. info->iter.snapshot = true;
  3764. info->iter.trace_buffer = &info->iter.tr->max_buffer;
  3765. return ret;
  3766. }
  3767. #endif /* CONFIG_TRACER_SNAPSHOT */
  3768. static const struct file_operations tracing_max_lat_fops = {
  3769. .open = tracing_open_generic,
  3770. .read = tracing_max_lat_read,
  3771. .write = tracing_max_lat_write,
  3772. .llseek = generic_file_llseek,
  3773. };
  3774. static const struct file_operations set_tracer_fops = {
  3775. .open = tracing_open_generic,
  3776. .read = tracing_set_trace_read,
  3777. .write = tracing_set_trace_write,
  3778. .llseek = generic_file_llseek,
  3779. };
  3780. static const struct file_operations tracing_pipe_fops = {
  3781. .open = tracing_open_pipe,
  3782. .poll = tracing_poll_pipe,
  3783. .read = tracing_read_pipe,
  3784. .splice_read = tracing_splice_read_pipe,
  3785. .release = tracing_release_pipe,
  3786. .llseek = no_llseek,
  3787. };
  3788. static const struct file_operations tracing_entries_fops = {
  3789. .open = tracing_open_generic,
  3790. .read = tracing_entries_read,
  3791. .write = tracing_entries_write,
  3792. .llseek = generic_file_llseek,
  3793. };
  3794. static const struct file_operations tracing_total_entries_fops = {
  3795. .open = tracing_open_generic,
  3796. .read = tracing_total_entries_read,
  3797. .llseek = generic_file_llseek,
  3798. };
  3799. static const struct file_operations tracing_free_buffer_fops = {
  3800. .write = tracing_free_buffer_write,
  3801. .release = tracing_free_buffer_release,
  3802. };
  3803. static const struct file_operations tracing_mark_fops = {
  3804. .open = tracing_open_generic,
  3805. .write = tracing_mark_write,
  3806. .llseek = generic_file_llseek,
  3807. };
  3808. static const struct file_operations trace_clock_fops = {
  3809. .open = tracing_clock_open,
  3810. .read = seq_read,
  3811. .llseek = seq_lseek,
  3812. .release = single_release,
  3813. .write = tracing_clock_write,
  3814. };
  3815. #ifdef CONFIG_TRACER_SNAPSHOT
  3816. static const struct file_operations snapshot_fops = {
  3817. .open = tracing_snapshot_open,
  3818. .read = seq_read,
  3819. .write = tracing_snapshot_write,
  3820. .llseek = tracing_seek,
  3821. .release = tracing_snapshot_release,
  3822. };
  3823. static const struct file_operations snapshot_raw_fops = {
  3824. .open = snapshot_raw_open,
  3825. .read = tracing_buffers_read,
  3826. .release = tracing_buffers_release,
  3827. .splice_read = tracing_buffers_splice_read,
  3828. .llseek = no_llseek,
  3829. };
  3830. #endif /* CONFIG_TRACER_SNAPSHOT */
  3831. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  3832. {
  3833. struct trace_cpu *tc = inode->i_private;
  3834. struct trace_array *tr = tc->tr;
  3835. struct ftrace_buffer_info *info;
  3836. if (tracing_disabled)
  3837. return -ENODEV;
  3838. info = kzalloc(sizeof(*info), GFP_KERNEL);
  3839. if (!info)
  3840. return -ENOMEM;
  3841. mutex_lock(&trace_types_lock);
  3842. tr->ref++;
  3843. info->iter.tr = tr;
  3844. info->iter.cpu_file = tc->cpu;
  3845. info->iter.trace = tr->current_trace;
  3846. info->iter.trace_buffer = &tr->trace_buffer;
  3847. info->spare = NULL;
  3848. /* Force reading ring buffer for first read */
  3849. info->read = (unsigned int)-1;
  3850. filp->private_data = info;
  3851. mutex_unlock(&trace_types_lock);
  3852. return nonseekable_open(inode, filp);
  3853. }
  3854. static unsigned int
  3855. tracing_buffers_poll(struct file *filp, poll_table *poll_table)
  3856. {
  3857. struct ftrace_buffer_info *info = filp->private_data;
  3858. struct trace_iterator *iter = &info->iter;
  3859. return trace_poll(iter, filp, poll_table);
  3860. }
  3861. static ssize_t
  3862. tracing_buffers_read(struct file *filp, char __user *ubuf,
  3863. size_t count, loff_t *ppos)
  3864. {
  3865. struct ftrace_buffer_info *info = filp->private_data;
  3866. struct trace_iterator *iter = &info->iter;
  3867. ssize_t ret;
  3868. ssize_t size;
  3869. if (!count)
  3870. return 0;
  3871. mutex_lock(&trace_types_lock);
  3872. #ifdef CONFIG_TRACER_MAX_TRACE
  3873. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  3874. size = -EBUSY;
  3875. goto out_unlock;
  3876. }
  3877. #endif
  3878. if (!info->spare)
  3879. info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
  3880. iter->cpu_file);
  3881. size = -ENOMEM;
  3882. if (!info->spare)
  3883. goto out_unlock;
  3884. /* Do we have previous read data to read? */
  3885. if (info->read < PAGE_SIZE)
  3886. goto read;
  3887. again:
  3888. trace_access_lock(iter->cpu_file);
  3889. ret = ring_buffer_read_page(iter->trace_buffer->buffer,
  3890. &info->spare,
  3891. count,
  3892. iter->cpu_file, 0);
  3893. trace_access_unlock(iter->cpu_file);
  3894. if (ret < 0) {
  3895. if (trace_empty(iter)) {
  3896. if ((filp->f_flags & O_NONBLOCK)) {
  3897. size = -EAGAIN;
  3898. goto out_unlock;
  3899. }
  3900. mutex_unlock(&trace_types_lock);
  3901. iter->trace->wait_pipe(iter);
  3902. mutex_lock(&trace_types_lock);
  3903. if (signal_pending(current)) {
  3904. size = -EINTR;
  3905. goto out_unlock;
  3906. }
  3907. goto again;
  3908. }
  3909. size = 0;
  3910. goto out_unlock;
  3911. }
  3912. info->read = 0;
  3913. read:
  3914. size = PAGE_SIZE - info->read;
  3915. if (size > count)
  3916. size = count;
  3917. ret = copy_to_user(ubuf, info->spare + info->read, size);
  3918. if (ret == size) {
  3919. size = -EFAULT;
  3920. goto out_unlock;
  3921. }
  3922. size -= ret;
  3923. *ppos += size;
  3924. info->read += size;
  3925. out_unlock:
  3926. mutex_unlock(&trace_types_lock);
  3927. return size;
  3928. }
  3929. static int tracing_buffers_release(struct inode *inode, struct file *file)
  3930. {
  3931. struct ftrace_buffer_info *info = file->private_data;
  3932. struct trace_iterator *iter = &info->iter;
  3933. mutex_lock(&trace_types_lock);
  3934. WARN_ON(!iter->tr->ref);
  3935. iter->tr->ref--;
  3936. if (info->spare)
  3937. ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
  3938. kfree(info);
  3939. mutex_unlock(&trace_types_lock);
  3940. return 0;
  3941. }
  3942. struct buffer_ref {
  3943. struct ring_buffer *buffer;
  3944. void *page;
  3945. int ref;
  3946. };
  3947. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  3948. struct pipe_buffer *buf)
  3949. {
  3950. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3951. if (--ref->ref)
  3952. return;
  3953. ring_buffer_free_read_page(ref->buffer, ref->page);
  3954. kfree(ref);
  3955. buf->private = 0;
  3956. }
  3957. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  3958. struct pipe_buffer *buf)
  3959. {
  3960. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3961. ref->ref++;
  3962. }
  3963. /* Pipe buffer operations for a buffer. */
  3964. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  3965. .can_merge = 0,
  3966. .map = generic_pipe_buf_map,
  3967. .unmap = generic_pipe_buf_unmap,
  3968. .confirm = generic_pipe_buf_confirm,
  3969. .release = buffer_pipe_buf_release,
  3970. .steal = generic_pipe_buf_steal,
  3971. .get = buffer_pipe_buf_get,
  3972. };
  3973. /*
  3974. * Callback from splice_to_pipe(), if we need to release some pages
  3975. * at the end of the spd in case we error'ed out in filling the pipe.
  3976. */
  3977. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  3978. {
  3979. struct buffer_ref *ref =
  3980. (struct buffer_ref *)spd->partial[i].private;
  3981. if (--ref->ref)
  3982. return;
  3983. ring_buffer_free_read_page(ref->buffer, ref->page);
  3984. kfree(ref);
  3985. spd->partial[i].private = 0;
  3986. }
  3987. static ssize_t
  3988. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3989. struct pipe_inode_info *pipe, size_t len,
  3990. unsigned int flags)
  3991. {
  3992. struct ftrace_buffer_info *info = file->private_data;
  3993. struct trace_iterator *iter = &info->iter;
  3994. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3995. struct page *pages_def[PIPE_DEF_BUFFERS];
  3996. struct splice_pipe_desc spd = {
  3997. .pages = pages_def,
  3998. .partial = partial_def,
  3999. .nr_pages_max = PIPE_DEF_BUFFERS,
  4000. .flags = flags,
  4001. .ops = &buffer_pipe_buf_ops,
  4002. .spd_release = buffer_spd_release,
  4003. };
  4004. struct buffer_ref *ref;
  4005. int entries, size, i;
  4006. ssize_t ret;
  4007. mutex_lock(&trace_types_lock);
  4008. #ifdef CONFIG_TRACER_MAX_TRACE
  4009. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  4010. ret = -EBUSY;
  4011. goto out;
  4012. }
  4013. #endif
  4014. if (splice_grow_spd(pipe, &spd)) {
  4015. ret = -ENOMEM;
  4016. goto out;
  4017. }
  4018. if (*ppos & (PAGE_SIZE - 1)) {
  4019. ret = -EINVAL;
  4020. goto out;
  4021. }
  4022. if (len & (PAGE_SIZE - 1)) {
  4023. if (len < PAGE_SIZE) {
  4024. ret = -EINVAL;
  4025. goto out;
  4026. }
  4027. len &= PAGE_MASK;
  4028. }
  4029. again:
  4030. trace_access_lock(iter->cpu_file);
  4031. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  4032. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  4033. struct page *page;
  4034. int r;
  4035. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  4036. if (!ref)
  4037. break;
  4038. ref->ref = 1;
  4039. ref->buffer = iter->trace_buffer->buffer;
  4040. ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
  4041. if (!ref->page) {
  4042. kfree(ref);
  4043. break;
  4044. }
  4045. r = ring_buffer_read_page(ref->buffer, &ref->page,
  4046. len, iter->cpu_file, 1);
  4047. if (r < 0) {
  4048. ring_buffer_free_read_page(ref->buffer, ref->page);
  4049. kfree(ref);
  4050. break;
  4051. }
  4052. /*
  4053. * zero out any left over data, this is going to
  4054. * user land.
  4055. */
  4056. size = ring_buffer_page_len(ref->page);
  4057. if (size < PAGE_SIZE)
  4058. memset(ref->page + size, 0, PAGE_SIZE - size);
  4059. page = virt_to_page(ref->page);
  4060. spd.pages[i] = page;
  4061. spd.partial[i].len = PAGE_SIZE;
  4062. spd.partial[i].offset = 0;
  4063. spd.partial[i].private = (unsigned long)ref;
  4064. spd.nr_pages++;
  4065. *ppos += PAGE_SIZE;
  4066. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  4067. }
  4068. trace_access_unlock(iter->cpu_file);
  4069. spd.nr_pages = i;
  4070. /* did we read anything? */
  4071. if (!spd.nr_pages) {
  4072. if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
  4073. ret = -EAGAIN;
  4074. goto out;
  4075. }
  4076. mutex_unlock(&trace_types_lock);
  4077. iter->trace->wait_pipe(iter);
  4078. mutex_lock(&trace_types_lock);
  4079. if (signal_pending(current)) {
  4080. ret = -EINTR;
  4081. goto out;
  4082. }
  4083. goto again;
  4084. }
  4085. ret = splice_to_pipe(pipe, &spd);
  4086. splice_shrink_spd(&spd);
  4087. out:
  4088. mutex_unlock(&trace_types_lock);
  4089. return ret;
  4090. }
  4091. static const struct file_operations tracing_buffers_fops = {
  4092. .open = tracing_buffers_open,
  4093. .read = tracing_buffers_read,
  4094. .poll = tracing_buffers_poll,
  4095. .release = tracing_buffers_release,
  4096. .splice_read = tracing_buffers_splice_read,
  4097. .llseek = no_llseek,
  4098. };
  4099. static ssize_t
  4100. tracing_stats_read(struct file *filp, char __user *ubuf,
  4101. size_t count, loff_t *ppos)
  4102. {
  4103. struct trace_cpu *tc = filp->private_data;
  4104. struct trace_array *tr = tc->tr;
  4105. struct trace_buffer *trace_buf = &tr->trace_buffer;
  4106. struct trace_seq *s;
  4107. unsigned long cnt;
  4108. unsigned long long t;
  4109. unsigned long usec_rem;
  4110. int cpu = tc->cpu;
  4111. s = kmalloc(sizeof(*s), GFP_KERNEL);
  4112. if (!s)
  4113. return -ENOMEM;
  4114. trace_seq_init(s);
  4115. cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
  4116. trace_seq_printf(s, "entries: %ld\n", cnt);
  4117. cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
  4118. trace_seq_printf(s, "overrun: %ld\n", cnt);
  4119. cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
  4120. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  4121. cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
  4122. trace_seq_printf(s, "bytes: %ld\n", cnt);
  4123. if (trace_clocks[trace_clock_id].in_ns) {
  4124. /* local or global for trace_clock */
  4125. t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4126. usec_rem = do_div(t, USEC_PER_SEC);
  4127. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
  4128. t, usec_rem);
  4129. t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4130. usec_rem = do_div(t, USEC_PER_SEC);
  4131. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  4132. } else {
  4133. /* counter or tsc mode for trace_clock */
  4134. trace_seq_printf(s, "oldest event ts: %llu\n",
  4135. ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4136. trace_seq_printf(s, "now ts: %llu\n",
  4137. ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4138. }
  4139. cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
  4140. trace_seq_printf(s, "dropped events: %ld\n", cnt);
  4141. cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
  4142. trace_seq_printf(s, "read events: %ld\n", cnt);
  4143. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  4144. kfree(s);
  4145. return count;
  4146. }
  4147. static const struct file_operations tracing_stats_fops = {
  4148. .open = tracing_open_generic,
  4149. .read = tracing_stats_read,
  4150. .llseek = generic_file_llseek,
  4151. };
  4152. #ifdef CONFIG_DYNAMIC_FTRACE
  4153. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  4154. {
  4155. return 0;
  4156. }
  4157. static ssize_t
  4158. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  4159. size_t cnt, loff_t *ppos)
  4160. {
  4161. static char ftrace_dyn_info_buffer[1024];
  4162. static DEFINE_MUTEX(dyn_info_mutex);
  4163. unsigned long *p = filp->private_data;
  4164. char *buf = ftrace_dyn_info_buffer;
  4165. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  4166. int r;
  4167. mutex_lock(&dyn_info_mutex);
  4168. r = sprintf(buf, "%ld ", *p);
  4169. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  4170. buf[r++] = '\n';
  4171. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4172. mutex_unlock(&dyn_info_mutex);
  4173. return r;
  4174. }
  4175. static const struct file_operations tracing_dyn_info_fops = {
  4176. .open = tracing_open_generic,
  4177. .read = tracing_read_dyn_info,
  4178. .llseek = generic_file_llseek,
  4179. };
  4180. #endif
  4181. struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
  4182. {
  4183. static int once;
  4184. if (tr->dir)
  4185. return tr->dir;
  4186. if (!debugfs_initialized())
  4187. return NULL;
  4188. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  4189. tr->dir = debugfs_create_dir("tracing", NULL);
  4190. if (!tr->dir && !once) {
  4191. once = 1;
  4192. pr_warning("Could not create debugfs directory 'tracing'\n");
  4193. return NULL;
  4194. }
  4195. return tr->dir;
  4196. }
  4197. struct dentry *tracing_init_dentry(void)
  4198. {
  4199. return tracing_init_dentry_tr(&global_trace);
  4200. }
  4201. static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
  4202. {
  4203. struct dentry *d_tracer;
  4204. if (tr->percpu_dir)
  4205. return tr->percpu_dir;
  4206. d_tracer = tracing_init_dentry_tr(tr);
  4207. if (!d_tracer)
  4208. return NULL;
  4209. tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
  4210. WARN_ONCE(!tr->percpu_dir,
  4211. "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
  4212. return tr->percpu_dir;
  4213. }
  4214. static void
  4215. tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
  4216. {
  4217. struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  4218. struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
  4219. struct dentry *d_cpu;
  4220. char cpu_dir[30]; /* 30 characters should be more than enough */
  4221. if (!d_percpu)
  4222. return;
  4223. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  4224. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  4225. if (!d_cpu) {
  4226. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  4227. return;
  4228. }
  4229. /* per cpu trace_pipe */
  4230. trace_create_file("trace_pipe", 0444, d_cpu,
  4231. (void *)&data->trace_cpu, &tracing_pipe_fops);
  4232. /* per cpu trace */
  4233. trace_create_file("trace", 0644, d_cpu,
  4234. (void *)&data->trace_cpu, &tracing_fops);
  4235. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  4236. (void *)&data->trace_cpu, &tracing_buffers_fops);
  4237. trace_create_file("stats", 0444, d_cpu,
  4238. (void *)&data->trace_cpu, &tracing_stats_fops);
  4239. trace_create_file("buffer_size_kb", 0444, d_cpu,
  4240. (void *)&data->trace_cpu, &tracing_entries_fops);
  4241. #ifdef CONFIG_TRACER_SNAPSHOT
  4242. trace_create_file("snapshot", 0644, d_cpu,
  4243. (void *)&data->trace_cpu, &snapshot_fops);
  4244. trace_create_file("snapshot_raw", 0444, d_cpu,
  4245. (void *)&data->trace_cpu, &snapshot_raw_fops);
  4246. #endif
  4247. }
  4248. #ifdef CONFIG_FTRACE_SELFTEST
  4249. /* Let selftest have access to static functions in this file */
  4250. #include "trace_selftest.c"
  4251. #endif
  4252. struct trace_option_dentry {
  4253. struct tracer_opt *opt;
  4254. struct tracer_flags *flags;
  4255. struct trace_array *tr;
  4256. struct dentry *entry;
  4257. };
  4258. static ssize_t
  4259. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  4260. loff_t *ppos)
  4261. {
  4262. struct trace_option_dentry *topt = filp->private_data;
  4263. char *buf;
  4264. if (topt->flags->val & topt->opt->bit)
  4265. buf = "1\n";
  4266. else
  4267. buf = "0\n";
  4268. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4269. }
  4270. static ssize_t
  4271. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4272. loff_t *ppos)
  4273. {
  4274. struct trace_option_dentry *topt = filp->private_data;
  4275. unsigned long val;
  4276. int ret;
  4277. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4278. if (ret)
  4279. return ret;
  4280. if (val != 0 && val != 1)
  4281. return -EINVAL;
  4282. if (!!(topt->flags->val & topt->opt->bit) != val) {
  4283. mutex_lock(&trace_types_lock);
  4284. ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
  4285. topt->opt, !val);
  4286. mutex_unlock(&trace_types_lock);
  4287. if (ret)
  4288. return ret;
  4289. }
  4290. *ppos += cnt;
  4291. return cnt;
  4292. }
  4293. static const struct file_operations trace_options_fops = {
  4294. .open = tracing_open_generic,
  4295. .read = trace_options_read,
  4296. .write = trace_options_write,
  4297. .llseek = generic_file_llseek,
  4298. };
  4299. static ssize_t
  4300. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  4301. loff_t *ppos)
  4302. {
  4303. long index = (long)filp->private_data;
  4304. char *buf;
  4305. if (trace_flags & (1 << index))
  4306. buf = "1\n";
  4307. else
  4308. buf = "0\n";
  4309. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4310. }
  4311. static ssize_t
  4312. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4313. loff_t *ppos)
  4314. {
  4315. struct trace_array *tr = &global_trace;
  4316. long index = (long)filp->private_data;
  4317. unsigned long val;
  4318. int ret;
  4319. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4320. if (ret)
  4321. return ret;
  4322. if (val != 0 && val != 1)
  4323. return -EINVAL;
  4324. mutex_lock(&trace_types_lock);
  4325. ret = set_tracer_flag(tr, 1 << index, val);
  4326. mutex_unlock(&trace_types_lock);
  4327. if (ret < 0)
  4328. return ret;
  4329. *ppos += cnt;
  4330. return cnt;
  4331. }
  4332. static const struct file_operations trace_options_core_fops = {
  4333. .open = tracing_open_generic,
  4334. .read = trace_options_core_read,
  4335. .write = trace_options_core_write,
  4336. .llseek = generic_file_llseek,
  4337. };
  4338. struct dentry *trace_create_file(const char *name,
  4339. umode_t mode,
  4340. struct dentry *parent,
  4341. void *data,
  4342. const struct file_operations *fops)
  4343. {
  4344. struct dentry *ret;
  4345. ret = debugfs_create_file(name, mode, parent, data, fops);
  4346. if (!ret)
  4347. pr_warning("Could not create debugfs '%s' entry\n", name);
  4348. return ret;
  4349. }
  4350. static struct dentry *trace_options_init_dentry(struct trace_array *tr)
  4351. {
  4352. struct dentry *d_tracer;
  4353. if (tr->options)
  4354. return tr->options;
  4355. d_tracer = tracing_init_dentry_tr(tr);
  4356. if (!d_tracer)
  4357. return NULL;
  4358. tr->options = debugfs_create_dir("options", d_tracer);
  4359. if (!tr->options) {
  4360. pr_warning("Could not create debugfs directory 'options'\n");
  4361. return NULL;
  4362. }
  4363. return tr->options;
  4364. }
  4365. static void
  4366. create_trace_option_file(struct trace_array *tr,
  4367. struct trace_option_dentry *topt,
  4368. struct tracer_flags *flags,
  4369. struct tracer_opt *opt)
  4370. {
  4371. struct dentry *t_options;
  4372. t_options = trace_options_init_dentry(tr);
  4373. if (!t_options)
  4374. return;
  4375. topt->flags = flags;
  4376. topt->opt = opt;
  4377. topt->tr = tr;
  4378. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  4379. &trace_options_fops);
  4380. }
  4381. static struct trace_option_dentry *
  4382. create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
  4383. {
  4384. struct trace_option_dentry *topts;
  4385. struct tracer_flags *flags;
  4386. struct tracer_opt *opts;
  4387. int cnt;
  4388. if (!tracer)
  4389. return NULL;
  4390. flags = tracer->flags;
  4391. if (!flags || !flags->opts)
  4392. return NULL;
  4393. opts = flags->opts;
  4394. for (cnt = 0; opts[cnt].name; cnt++)
  4395. ;
  4396. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  4397. if (!topts)
  4398. return NULL;
  4399. for (cnt = 0; opts[cnt].name; cnt++)
  4400. create_trace_option_file(tr, &topts[cnt], flags,
  4401. &opts[cnt]);
  4402. return topts;
  4403. }
  4404. static void
  4405. destroy_trace_option_files(struct trace_option_dentry *topts)
  4406. {
  4407. int cnt;
  4408. if (!topts)
  4409. return;
  4410. for (cnt = 0; topts[cnt].opt; cnt++) {
  4411. if (topts[cnt].entry)
  4412. debugfs_remove(topts[cnt].entry);
  4413. }
  4414. kfree(topts);
  4415. }
  4416. static struct dentry *
  4417. create_trace_option_core_file(struct trace_array *tr,
  4418. const char *option, long index)
  4419. {
  4420. struct dentry *t_options;
  4421. t_options = trace_options_init_dentry(tr);
  4422. if (!t_options)
  4423. return NULL;
  4424. return trace_create_file(option, 0644, t_options, (void *)index,
  4425. &trace_options_core_fops);
  4426. }
  4427. static __init void create_trace_options_dir(struct trace_array *tr)
  4428. {
  4429. struct dentry *t_options;
  4430. int i;
  4431. t_options = trace_options_init_dentry(tr);
  4432. if (!t_options)
  4433. return;
  4434. for (i = 0; trace_options[i]; i++)
  4435. create_trace_option_core_file(tr, trace_options[i], i);
  4436. }
  4437. static ssize_t
  4438. rb_simple_read(struct file *filp, char __user *ubuf,
  4439. size_t cnt, loff_t *ppos)
  4440. {
  4441. struct trace_array *tr = filp->private_data;
  4442. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4443. char buf[64];
  4444. int r;
  4445. if (buffer)
  4446. r = ring_buffer_record_is_on(buffer);
  4447. else
  4448. r = 0;
  4449. r = sprintf(buf, "%d\n", r);
  4450. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4451. }
  4452. static ssize_t
  4453. rb_simple_write(struct file *filp, const char __user *ubuf,
  4454. size_t cnt, loff_t *ppos)
  4455. {
  4456. struct trace_array *tr = filp->private_data;
  4457. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4458. unsigned long val;
  4459. int ret;
  4460. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4461. if (ret)
  4462. return ret;
  4463. if (buffer) {
  4464. mutex_lock(&trace_types_lock);
  4465. if (val) {
  4466. ring_buffer_record_on(buffer);
  4467. if (tr->current_trace->start)
  4468. tr->current_trace->start(tr);
  4469. } else {
  4470. ring_buffer_record_off(buffer);
  4471. if (tr->current_trace->stop)
  4472. tr->current_trace->stop(tr);
  4473. }
  4474. mutex_unlock(&trace_types_lock);
  4475. }
  4476. (*ppos)++;
  4477. return cnt;
  4478. }
  4479. static const struct file_operations rb_simple_fops = {
  4480. .open = tracing_open_generic,
  4481. .read = rb_simple_read,
  4482. .write = rb_simple_write,
  4483. .llseek = default_llseek,
  4484. };
  4485. struct dentry *trace_instance_dir;
  4486. static void
  4487. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
  4488. static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
  4489. {
  4490. int cpu;
  4491. for_each_tracing_cpu(cpu) {
  4492. memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
  4493. per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
  4494. per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
  4495. }
  4496. }
  4497. static int
  4498. allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
  4499. {
  4500. enum ring_buffer_flags rb_flags;
  4501. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  4502. buf->buffer = ring_buffer_alloc(size, rb_flags);
  4503. if (!buf->buffer)
  4504. return -ENOMEM;
  4505. buf->data = alloc_percpu(struct trace_array_cpu);
  4506. if (!buf->data) {
  4507. ring_buffer_free(buf->buffer);
  4508. return -ENOMEM;
  4509. }
  4510. init_trace_buffers(tr, buf);
  4511. /* Allocate the first page for all buffers */
  4512. set_buffer_entries(&tr->trace_buffer,
  4513. ring_buffer_size(tr->trace_buffer.buffer, 0));
  4514. return 0;
  4515. }
  4516. static int allocate_trace_buffers(struct trace_array *tr, int size)
  4517. {
  4518. int ret;
  4519. ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
  4520. if (ret)
  4521. return ret;
  4522. #ifdef CONFIG_TRACER_MAX_TRACE
  4523. ret = allocate_trace_buffer(tr, &tr->max_buffer,
  4524. allocate_snapshot ? size : 1);
  4525. if (WARN_ON(ret)) {
  4526. ring_buffer_free(tr->trace_buffer.buffer);
  4527. free_percpu(tr->trace_buffer.data);
  4528. return -ENOMEM;
  4529. }
  4530. tr->allocated_snapshot = allocate_snapshot;
  4531. /*
  4532. * Only the top level trace array gets its snapshot allocated
  4533. * from the kernel command line.
  4534. */
  4535. allocate_snapshot = false;
  4536. #endif
  4537. return 0;
  4538. }
  4539. static int new_instance_create(const char *name)
  4540. {
  4541. struct trace_array *tr;
  4542. int ret;
  4543. mutex_lock(&trace_types_lock);
  4544. ret = -EEXIST;
  4545. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4546. if (tr->name && strcmp(tr->name, name) == 0)
  4547. goto out_unlock;
  4548. }
  4549. ret = -ENOMEM;
  4550. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  4551. if (!tr)
  4552. goto out_unlock;
  4553. tr->name = kstrdup(name, GFP_KERNEL);
  4554. if (!tr->name)
  4555. goto out_free_tr;
  4556. raw_spin_lock_init(&tr->start_lock);
  4557. tr->current_trace = &nop_trace;
  4558. INIT_LIST_HEAD(&tr->systems);
  4559. INIT_LIST_HEAD(&tr->events);
  4560. if (allocate_trace_buffers(tr, trace_buf_size) < 0)
  4561. goto out_free_tr;
  4562. /* Holder for file callbacks */
  4563. tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
  4564. tr->trace_cpu.tr = tr;
  4565. tr->dir = debugfs_create_dir(name, trace_instance_dir);
  4566. if (!tr->dir)
  4567. goto out_free_tr;
  4568. ret = event_trace_add_tracer(tr->dir, tr);
  4569. if (ret)
  4570. goto out_free_tr;
  4571. init_tracer_debugfs(tr, tr->dir);
  4572. list_add(&tr->list, &ftrace_trace_arrays);
  4573. mutex_unlock(&trace_types_lock);
  4574. return 0;
  4575. out_free_tr:
  4576. if (tr->trace_buffer.buffer)
  4577. ring_buffer_free(tr->trace_buffer.buffer);
  4578. kfree(tr->name);
  4579. kfree(tr);
  4580. out_unlock:
  4581. mutex_unlock(&trace_types_lock);
  4582. return ret;
  4583. }
  4584. static int instance_delete(const char *name)
  4585. {
  4586. struct trace_array *tr;
  4587. int found = 0;
  4588. int ret;
  4589. mutex_lock(&trace_types_lock);
  4590. ret = -ENODEV;
  4591. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4592. if (tr->name && strcmp(tr->name, name) == 0) {
  4593. found = 1;
  4594. break;
  4595. }
  4596. }
  4597. if (!found)
  4598. goto out_unlock;
  4599. ret = -EBUSY;
  4600. if (tr->ref)
  4601. goto out_unlock;
  4602. list_del(&tr->list);
  4603. event_trace_del_tracer(tr);
  4604. debugfs_remove_recursive(tr->dir);
  4605. free_percpu(tr->trace_buffer.data);
  4606. ring_buffer_free(tr->trace_buffer.buffer);
  4607. kfree(tr->name);
  4608. kfree(tr);
  4609. ret = 0;
  4610. out_unlock:
  4611. mutex_unlock(&trace_types_lock);
  4612. return ret;
  4613. }
  4614. static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
  4615. {
  4616. struct dentry *parent;
  4617. int ret;
  4618. /* Paranoid: Make sure the parent is the "instances" directory */
  4619. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4620. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4621. return -ENOENT;
  4622. /*
  4623. * The inode mutex is locked, but debugfs_create_dir() will also
  4624. * take the mutex. As the instances directory can not be destroyed
  4625. * or changed in any other way, it is safe to unlock it, and
  4626. * let the dentry try. If two users try to make the same dir at
  4627. * the same time, then the new_instance_create() will determine the
  4628. * winner.
  4629. */
  4630. mutex_unlock(&inode->i_mutex);
  4631. ret = new_instance_create(dentry->d_iname);
  4632. mutex_lock(&inode->i_mutex);
  4633. return ret;
  4634. }
  4635. static int instance_rmdir(struct inode *inode, struct dentry *dentry)
  4636. {
  4637. struct dentry *parent;
  4638. int ret;
  4639. /* Paranoid: Make sure the parent is the "instances" directory */
  4640. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4641. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4642. return -ENOENT;
  4643. /* The caller did a dget() on dentry */
  4644. mutex_unlock(&dentry->d_inode->i_mutex);
  4645. /*
  4646. * The inode mutex is locked, but debugfs_create_dir() will also
  4647. * take the mutex. As the instances directory can not be destroyed
  4648. * or changed in any other way, it is safe to unlock it, and
  4649. * let the dentry try. If two users try to make the same dir at
  4650. * the same time, then the instance_delete() will determine the
  4651. * winner.
  4652. */
  4653. mutex_unlock(&inode->i_mutex);
  4654. ret = instance_delete(dentry->d_iname);
  4655. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  4656. mutex_lock(&dentry->d_inode->i_mutex);
  4657. return ret;
  4658. }
  4659. static const struct inode_operations instance_dir_inode_operations = {
  4660. .lookup = simple_lookup,
  4661. .mkdir = instance_mkdir,
  4662. .rmdir = instance_rmdir,
  4663. };
  4664. static __init void create_trace_instances(struct dentry *d_tracer)
  4665. {
  4666. trace_instance_dir = debugfs_create_dir("instances", d_tracer);
  4667. if (WARN_ON(!trace_instance_dir))
  4668. return;
  4669. /* Hijack the dir inode operations, to allow mkdir */
  4670. trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
  4671. }
  4672. static void
  4673. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
  4674. {
  4675. int cpu;
  4676. trace_create_file("trace_options", 0644, d_tracer,
  4677. tr, &tracing_iter_fops);
  4678. trace_create_file("trace", 0644, d_tracer,
  4679. (void *)&tr->trace_cpu, &tracing_fops);
  4680. trace_create_file("trace_pipe", 0444, d_tracer,
  4681. (void *)&tr->trace_cpu, &tracing_pipe_fops);
  4682. trace_create_file("buffer_size_kb", 0644, d_tracer,
  4683. (void *)&tr->trace_cpu, &tracing_entries_fops);
  4684. trace_create_file("buffer_total_size_kb", 0444, d_tracer,
  4685. tr, &tracing_total_entries_fops);
  4686. trace_create_file("free_buffer", 0644, d_tracer,
  4687. tr, &tracing_free_buffer_fops);
  4688. trace_create_file("trace_marker", 0220, d_tracer,
  4689. tr, &tracing_mark_fops);
  4690. trace_create_file("trace_clock", 0644, d_tracer, tr,
  4691. &trace_clock_fops);
  4692. trace_create_file("tracing_on", 0644, d_tracer,
  4693. tr, &rb_simple_fops);
  4694. #ifdef CONFIG_TRACER_SNAPSHOT
  4695. trace_create_file("snapshot", 0644, d_tracer,
  4696. (void *)&tr->trace_cpu, &snapshot_fops);
  4697. #endif
  4698. for_each_tracing_cpu(cpu)
  4699. tracing_init_debugfs_percpu(tr, cpu);
  4700. }
  4701. static __init int tracer_init_debugfs(void)
  4702. {
  4703. struct dentry *d_tracer;
  4704. trace_access_lock_init();
  4705. d_tracer = tracing_init_dentry();
  4706. init_tracer_debugfs(&global_trace, d_tracer);
  4707. trace_create_file("tracing_cpumask", 0644, d_tracer,
  4708. &global_trace, &tracing_cpumask_fops);
  4709. trace_create_file("available_tracers", 0444, d_tracer,
  4710. &global_trace, &show_traces_fops);
  4711. trace_create_file("current_tracer", 0644, d_tracer,
  4712. &global_trace, &set_tracer_fops);
  4713. #ifdef CONFIG_TRACER_MAX_TRACE
  4714. trace_create_file("tracing_max_latency", 0644, d_tracer,
  4715. &tracing_max_latency, &tracing_max_lat_fops);
  4716. #endif
  4717. trace_create_file("tracing_thresh", 0644, d_tracer,
  4718. &tracing_thresh, &tracing_max_lat_fops);
  4719. trace_create_file("README", 0444, d_tracer,
  4720. NULL, &tracing_readme_fops);
  4721. trace_create_file("saved_cmdlines", 0444, d_tracer,
  4722. NULL, &tracing_saved_cmdlines_fops);
  4723. #ifdef CONFIG_DYNAMIC_FTRACE
  4724. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  4725. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  4726. #endif
  4727. create_trace_instances(d_tracer);
  4728. create_trace_options_dir(&global_trace);
  4729. return 0;
  4730. }
  4731. static int trace_panic_handler(struct notifier_block *this,
  4732. unsigned long event, void *unused)
  4733. {
  4734. if (ftrace_dump_on_oops)
  4735. ftrace_dump(ftrace_dump_on_oops);
  4736. return NOTIFY_OK;
  4737. }
  4738. static struct notifier_block trace_panic_notifier = {
  4739. .notifier_call = trace_panic_handler,
  4740. .next = NULL,
  4741. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  4742. };
  4743. static int trace_die_handler(struct notifier_block *self,
  4744. unsigned long val,
  4745. void *data)
  4746. {
  4747. switch (val) {
  4748. case DIE_OOPS:
  4749. if (ftrace_dump_on_oops)
  4750. ftrace_dump(ftrace_dump_on_oops);
  4751. break;
  4752. default:
  4753. break;
  4754. }
  4755. return NOTIFY_OK;
  4756. }
  4757. static struct notifier_block trace_die_notifier = {
  4758. .notifier_call = trace_die_handler,
  4759. .priority = 200
  4760. };
  4761. /*
  4762. * printk is set to max of 1024, we really don't need it that big.
  4763. * Nothing should be printing 1000 characters anyway.
  4764. */
  4765. #define TRACE_MAX_PRINT 1000
  4766. /*
  4767. * Define here KERN_TRACE so that we have one place to modify
  4768. * it if we decide to change what log level the ftrace dump
  4769. * should be at.
  4770. */
  4771. #define KERN_TRACE KERN_EMERG
  4772. void
  4773. trace_printk_seq(struct trace_seq *s)
  4774. {
  4775. /* Probably should print a warning here. */
  4776. if (s->len >= 1000)
  4777. s->len = 1000;
  4778. /* should be zero ended, but we are paranoid. */
  4779. s->buffer[s->len] = 0;
  4780. printk(KERN_TRACE "%s", s->buffer);
  4781. trace_seq_init(s);
  4782. }
  4783. void trace_init_global_iter(struct trace_iterator *iter)
  4784. {
  4785. iter->tr = &global_trace;
  4786. iter->trace = iter->tr->current_trace;
  4787. iter->cpu_file = RING_BUFFER_ALL_CPUS;
  4788. iter->trace_buffer = &global_trace.trace_buffer;
  4789. }
  4790. static void
  4791. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
  4792. {
  4793. static arch_spinlock_t ftrace_dump_lock =
  4794. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  4795. /* use static because iter can be a bit big for the stack */
  4796. static struct trace_iterator iter;
  4797. unsigned int old_userobj;
  4798. static int dump_ran;
  4799. unsigned long flags;
  4800. int cnt = 0, cpu;
  4801. /* only one dump */
  4802. local_irq_save(flags);
  4803. arch_spin_lock(&ftrace_dump_lock);
  4804. if (dump_ran)
  4805. goto out;
  4806. dump_ran = 1;
  4807. tracing_off();
  4808. /* Did function tracer already get disabled? */
  4809. if (ftrace_is_dead()) {
  4810. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  4811. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  4812. }
  4813. if (disable_tracing)
  4814. ftrace_kill();
  4815. /* Simulate the iterator */
  4816. trace_init_global_iter(&iter);
  4817. for_each_tracing_cpu(cpu) {
  4818. atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
  4819. }
  4820. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  4821. /* don't look at user memory in panic mode */
  4822. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  4823. switch (oops_dump_mode) {
  4824. case DUMP_ALL:
  4825. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  4826. break;
  4827. case DUMP_ORIG:
  4828. iter.cpu_file = raw_smp_processor_id();
  4829. break;
  4830. case DUMP_NONE:
  4831. goto out_enable;
  4832. default:
  4833. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  4834. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  4835. }
  4836. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  4837. /*
  4838. * We need to stop all tracing on all CPUS to read the
  4839. * the next buffer. This is a bit expensive, but is
  4840. * not done often. We fill all what we can read,
  4841. * and then release the locks again.
  4842. */
  4843. while (!trace_empty(&iter)) {
  4844. if (!cnt)
  4845. printk(KERN_TRACE "---------------------------------\n");
  4846. cnt++;
  4847. /* reset all but tr, trace, and overruns */
  4848. memset(&iter.seq, 0,
  4849. sizeof(struct trace_iterator) -
  4850. offsetof(struct trace_iterator, seq));
  4851. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  4852. iter.pos = -1;
  4853. if (trace_find_next_entry_inc(&iter) != NULL) {
  4854. int ret;
  4855. ret = print_trace_line(&iter);
  4856. if (ret != TRACE_TYPE_NO_CONSUME)
  4857. trace_consume(&iter);
  4858. }
  4859. touch_nmi_watchdog();
  4860. trace_printk_seq(&iter.seq);
  4861. }
  4862. if (!cnt)
  4863. printk(KERN_TRACE " (ftrace buffer empty)\n");
  4864. else
  4865. printk(KERN_TRACE "---------------------------------\n");
  4866. out_enable:
  4867. /* Re-enable tracing if requested */
  4868. if (!disable_tracing) {
  4869. trace_flags |= old_userobj;
  4870. for_each_tracing_cpu(cpu) {
  4871. atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  4872. }
  4873. tracing_on();
  4874. }
  4875. out:
  4876. arch_spin_unlock(&ftrace_dump_lock);
  4877. local_irq_restore(flags);
  4878. }
  4879. /* By default: disable tracing after the dump */
  4880. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  4881. {
  4882. __ftrace_dump(true, oops_dump_mode);
  4883. }
  4884. EXPORT_SYMBOL_GPL(ftrace_dump);
  4885. __init static int tracer_alloc_buffers(void)
  4886. {
  4887. int ring_buf_size;
  4888. int ret = -ENOMEM;
  4889. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  4890. goto out;
  4891. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  4892. goto out_free_buffer_mask;
  4893. /* Only allocate trace_printk buffers if a trace_printk exists */
  4894. if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
  4895. /* Must be called before global_trace.buffer is allocated */
  4896. trace_printk_init_buffers();
  4897. /* To save memory, keep the ring buffer size to its minimum */
  4898. if (ring_buffer_expanded)
  4899. ring_buf_size = trace_buf_size;
  4900. else
  4901. ring_buf_size = 1;
  4902. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  4903. cpumask_copy(tracing_cpumask, cpu_all_mask);
  4904. raw_spin_lock_init(&global_trace.start_lock);
  4905. /* TODO: make the number of buffers hot pluggable with CPUS */
  4906. if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
  4907. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  4908. WARN_ON(1);
  4909. goto out_free_cpumask;
  4910. }
  4911. if (global_trace.buffer_disabled)
  4912. tracing_off();
  4913. trace_init_cmdlines();
  4914. register_tracer(&nop_trace);
  4915. global_trace.current_trace = &nop_trace;
  4916. /* All seems OK, enable tracing */
  4917. tracing_disabled = 0;
  4918. atomic_notifier_chain_register(&panic_notifier_list,
  4919. &trace_panic_notifier);
  4920. register_die_notifier(&trace_die_notifier);
  4921. global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
  4922. /* Holder for file callbacks */
  4923. global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
  4924. global_trace.trace_cpu.tr = &global_trace;
  4925. INIT_LIST_HEAD(&global_trace.systems);
  4926. INIT_LIST_HEAD(&global_trace.events);
  4927. list_add(&global_trace.list, &ftrace_trace_arrays);
  4928. while (trace_boot_options) {
  4929. char *option;
  4930. option = strsep(&trace_boot_options, ",");
  4931. trace_set_options(&global_trace, option);
  4932. }
  4933. return 0;
  4934. out_free_cpumask:
  4935. free_percpu(global_trace.trace_buffer.data);
  4936. #ifdef CONFIG_TRACER_MAX_TRACE
  4937. free_percpu(global_trace.max_buffer.data);
  4938. #endif
  4939. free_cpumask_var(tracing_cpumask);
  4940. out_free_buffer_mask:
  4941. free_cpumask_var(tracing_buffer_mask);
  4942. out:
  4943. return ret;
  4944. }
  4945. __init static int clear_boot_tracer(void)
  4946. {
  4947. /*
  4948. * The default tracer at boot buffer is an init section.
  4949. * This function is called in lateinit. If we did not
  4950. * find the boot tracer, then clear it out, to prevent
  4951. * later registration from accessing the buffer that is
  4952. * about to be freed.
  4953. */
  4954. if (!default_bootup_tracer)
  4955. return 0;
  4956. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  4957. default_bootup_tracer);
  4958. default_bootup_tracer = NULL;
  4959. return 0;
  4960. }
  4961. early_initcall(tracer_alloc_buffers);
  4962. fs_initcall(tracer_init_debugfs);
  4963. late_initcall(clear_boot_tracer);