trace.c 124 KB

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