trace.c 148 KB

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