trace.c 148 KB

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