trace.c 138 KB

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