trace.c 152 KB

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