trace.c 132 KB

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