trace.c 114 KB

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