trace.c 153 KB

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