trace.c 179 KB

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