trace.c 110 KB

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