trace.c 150 KB

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