trace.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043
  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 next_size = 0;
  1413. int cpu;
  1414. /*
  1415. * If we are in a per_cpu trace file, don't bother by iterating over
  1416. * all cpu and peek directly.
  1417. */
  1418. if (cpu_file > TRACE_PIPE_ALL_CPU) {
  1419. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1420. return NULL;
  1421. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  1422. if (ent_cpu)
  1423. *ent_cpu = cpu_file;
  1424. return ent;
  1425. }
  1426. for_each_tracing_cpu(cpu) {
  1427. if (ring_buffer_empty_cpu(buffer, cpu))
  1428. continue;
  1429. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  1430. /*
  1431. * Pick the entry with the smallest timestamp:
  1432. */
  1433. if (ent && (!next || ts < next_ts)) {
  1434. next = ent;
  1435. next_cpu = cpu;
  1436. next_ts = ts;
  1437. next_lost = lost_events;
  1438. next_size = iter->ent_size;
  1439. }
  1440. }
  1441. iter->ent_size = next_size;
  1442. if (ent_cpu)
  1443. *ent_cpu = next_cpu;
  1444. if (ent_ts)
  1445. *ent_ts = next_ts;
  1446. if (missing_events)
  1447. *missing_events = next_lost;
  1448. return next;
  1449. }
  1450. /* Find the next real entry, without updating the iterator itself */
  1451. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1452. int *ent_cpu, u64 *ent_ts)
  1453. {
  1454. return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  1455. }
  1456. /* Find the next real entry, and increment the iterator to the next entry */
  1457. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  1458. {
  1459. iter->ent = __find_next_entry(iter, &iter->cpu,
  1460. &iter->lost_events, &iter->ts);
  1461. if (iter->ent)
  1462. trace_iterator_increment(iter);
  1463. return iter->ent ? iter : NULL;
  1464. }
  1465. static void trace_consume(struct trace_iterator *iter)
  1466. {
  1467. /* Don't allow ftrace to trace into the ring buffers */
  1468. ftrace_disable_cpu();
  1469. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
  1470. &iter->lost_events);
  1471. ftrace_enable_cpu();
  1472. }
  1473. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1474. {
  1475. struct trace_iterator *iter = m->private;
  1476. int i = (int)*pos;
  1477. void *ent;
  1478. WARN_ON_ONCE(iter->leftover);
  1479. (*pos)++;
  1480. /* can't go backwards */
  1481. if (iter->idx > i)
  1482. return NULL;
  1483. if (iter->idx < 0)
  1484. ent = trace_find_next_entry_inc(iter);
  1485. else
  1486. ent = iter;
  1487. while (ent && iter->idx < i)
  1488. ent = trace_find_next_entry_inc(iter);
  1489. iter->pos = *pos;
  1490. return ent;
  1491. }
  1492. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  1493. {
  1494. struct trace_array *tr = iter->tr;
  1495. struct ring_buffer_event *event;
  1496. struct ring_buffer_iter *buf_iter;
  1497. unsigned long entries = 0;
  1498. u64 ts;
  1499. tr->data[cpu]->skipped_entries = 0;
  1500. if (!iter->buffer_iter[cpu])
  1501. return;
  1502. buf_iter = iter->buffer_iter[cpu];
  1503. ring_buffer_iter_reset(buf_iter);
  1504. /*
  1505. * We could have the case with the max latency tracers
  1506. * that a reset never took place on a cpu. This is evident
  1507. * by the timestamp being before the start of the buffer.
  1508. */
  1509. while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
  1510. if (ts >= iter->tr->time_start)
  1511. break;
  1512. entries++;
  1513. ring_buffer_read(buf_iter, NULL);
  1514. }
  1515. tr->data[cpu]->skipped_entries = entries;
  1516. }
  1517. /*
  1518. * The current tracer is copied to avoid a global locking
  1519. * all around.
  1520. */
  1521. static void *s_start(struct seq_file *m, loff_t *pos)
  1522. {
  1523. struct trace_iterator *iter = m->private;
  1524. static struct tracer *old_tracer;
  1525. int cpu_file = iter->cpu_file;
  1526. void *p = NULL;
  1527. loff_t l = 0;
  1528. int cpu;
  1529. /* copy the tracer to avoid using a global lock all around */
  1530. mutex_lock(&trace_types_lock);
  1531. if (unlikely(old_tracer != current_trace && current_trace)) {
  1532. old_tracer = current_trace;
  1533. *iter->trace = *current_trace;
  1534. }
  1535. mutex_unlock(&trace_types_lock);
  1536. atomic_inc(&trace_record_cmdline_disabled);
  1537. if (*pos != iter->pos) {
  1538. iter->ent = NULL;
  1539. iter->cpu = 0;
  1540. iter->idx = -1;
  1541. ftrace_disable_cpu();
  1542. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  1543. for_each_tracing_cpu(cpu)
  1544. tracing_iter_reset(iter, cpu);
  1545. } else
  1546. tracing_iter_reset(iter, cpu_file);
  1547. ftrace_enable_cpu();
  1548. iter->leftover = 0;
  1549. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1550. ;
  1551. } else {
  1552. /*
  1553. * If we overflowed the seq_file before, then we want
  1554. * to just reuse the trace_seq buffer again.
  1555. */
  1556. if (iter->leftover)
  1557. p = iter;
  1558. else {
  1559. l = *pos - 1;
  1560. p = s_next(m, p, &l);
  1561. }
  1562. }
  1563. trace_event_read_lock();
  1564. trace_access_lock(cpu_file);
  1565. return p;
  1566. }
  1567. static void s_stop(struct seq_file *m, void *p)
  1568. {
  1569. struct trace_iterator *iter = m->private;
  1570. atomic_dec(&trace_record_cmdline_disabled);
  1571. trace_access_unlock(iter->cpu_file);
  1572. trace_event_read_unlock();
  1573. }
  1574. static void
  1575. get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
  1576. {
  1577. unsigned long count;
  1578. int cpu;
  1579. *total = 0;
  1580. *entries = 0;
  1581. for_each_tracing_cpu(cpu) {
  1582. count = ring_buffer_entries_cpu(tr->buffer, cpu);
  1583. /*
  1584. * If this buffer has skipped entries, then we hold all
  1585. * entries for the trace and we need to ignore the
  1586. * ones before the time stamp.
  1587. */
  1588. if (tr->data[cpu]->skipped_entries) {
  1589. count -= tr->data[cpu]->skipped_entries;
  1590. /* total is the same as the entries */
  1591. *total += count;
  1592. } else
  1593. *total += count +
  1594. ring_buffer_overrun_cpu(tr->buffer, cpu);
  1595. *entries += count;
  1596. }
  1597. }
  1598. static void print_lat_help_header(struct seq_file *m)
  1599. {
  1600. seq_puts(m, "# _------=> CPU# \n");
  1601. seq_puts(m, "# / _-----=> irqs-off \n");
  1602. seq_puts(m, "# | / _----=> need-resched \n");
  1603. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1604. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1605. seq_puts(m, "# |||| / delay \n");
  1606. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1607. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1608. }
  1609. static void print_event_info(struct trace_array *tr, struct seq_file *m)
  1610. {
  1611. unsigned long total;
  1612. unsigned long entries;
  1613. get_total_entries(tr, &total, &entries);
  1614. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  1615. entries, total, num_online_cpus());
  1616. seq_puts(m, "#\n");
  1617. }
  1618. static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
  1619. {
  1620. print_event_info(tr, m);
  1621. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1622. seq_puts(m, "# | | | | |\n");
  1623. }
  1624. static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
  1625. {
  1626. print_event_info(tr, m);
  1627. seq_puts(m, "# _-----=> irqs-off\n");
  1628. seq_puts(m, "# / _----=> need-resched\n");
  1629. seq_puts(m, "# | / _---=> hardirq/softirq\n");
  1630. seq_puts(m, "# || / _--=> preempt-depth\n");
  1631. seq_puts(m, "# ||| / delay\n");
  1632. seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
  1633. seq_puts(m, "# | | | |||| | |\n");
  1634. }
  1635. void
  1636. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1637. {
  1638. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1639. struct trace_array *tr = iter->tr;
  1640. struct trace_array_cpu *data = tr->data[tr->cpu];
  1641. struct tracer *type = current_trace;
  1642. unsigned long entries;
  1643. unsigned long total;
  1644. const char *name = "preemption";
  1645. if (type)
  1646. name = type->name;
  1647. get_total_entries(tr, &total, &entries);
  1648. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1649. name, UTS_RELEASE);
  1650. seq_puts(m, "# -----------------------------------"
  1651. "---------------------------------\n");
  1652. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1653. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1654. nsecs_to_usecs(data->saved_latency),
  1655. entries,
  1656. total,
  1657. tr->cpu,
  1658. #if defined(CONFIG_PREEMPT_NONE)
  1659. "server",
  1660. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1661. "desktop",
  1662. #elif defined(CONFIG_PREEMPT)
  1663. "preempt",
  1664. #else
  1665. "unknown",
  1666. #endif
  1667. /* These are reserved for later use */
  1668. 0, 0, 0, 0);
  1669. #ifdef CONFIG_SMP
  1670. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1671. #else
  1672. seq_puts(m, ")\n");
  1673. #endif
  1674. seq_puts(m, "# -----------------\n");
  1675. seq_printf(m, "# | task: %.16s-%d "
  1676. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1677. data->comm, data->pid, data->uid, data->nice,
  1678. data->policy, data->rt_priority);
  1679. seq_puts(m, "# -----------------\n");
  1680. if (data->critical_start) {
  1681. seq_puts(m, "# => started at: ");
  1682. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1683. trace_print_seq(m, &iter->seq);
  1684. seq_puts(m, "\n# => ended at: ");
  1685. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1686. trace_print_seq(m, &iter->seq);
  1687. seq_puts(m, "\n#\n");
  1688. }
  1689. seq_puts(m, "#\n");
  1690. }
  1691. static void test_cpu_buff_start(struct trace_iterator *iter)
  1692. {
  1693. struct trace_seq *s = &iter->seq;
  1694. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1695. return;
  1696. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1697. return;
  1698. if (cpumask_test_cpu(iter->cpu, iter->started))
  1699. return;
  1700. if (iter->tr->data[iter->cpu]->skipped_entries)
  1701. return;
  1702. cpumask_set_cpu(iter->cpu, iter->started);
  1703. /* Don't print started cpu buffer for the first entry of the trace */
  1704. if (iter->idx > 1)
  1705. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  1706. iter->cpu);
  1707. }
  1708. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1709. {
  1710. struct trace_seq *s = &iter->seq;
  1711. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1712. struct trace_entry *entry;
  1713. struct trace_event *event;
  1714. entry = iter->ent;
  1715. test_cpu_buff_start(iter);
  1716. event = ftrace_find_event(entry->type);
  1717. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1718. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1719. if (!trace_print_lat_context(iter))
  1720. goto partial;
  1721. } else {
  1722. if (!trace_print_context(iter))
  1723. goto partial;
  1724. }
  1725. }
  1726. if (event)
  1727. return event->funcs->trace(iter, sym_flags, event);
  1728. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  1729. goto partial;
  1730. return TRACE_TYPE_HANDLED;
  1731. partial:
  1732. return TRACE_TYPE_PARTIAL_LINE;
  1733. }
  1734. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1735. {
  1736. struct trace_seq *s = &iter->seq;
  1737. struct trace_entry *entry;
  1738. struct trace_event *event;
  1739. entry = iter->ent;
  1740. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1741. if (!trace_seq_printf(s, "%d %d %llu ",
  1742. entry->pid, iter->cpu, iter->ts))
  1743. goto partial;
  1744. }
  1745. event = ftrace_find_event(entry->type);
  1746. if (event)
  1747. return event->funcs->raw(iter, 0, event);
  1748. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  1749. goto partial;
  1750. return TRACE_TYPE_HANDLED;
  1751. partial:
  1752. return TRACE_TYPE_PARTIAL_LINE;
  1753. }
  1754. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1755. {
  1756. struct trace_seq *s = &iter->seq;
  1757. unsigned char newline = '\n';
  1758. struct trace_entry *entry;
  1759. struct trace_event *event;
  1760. entry = iter->ent;
  1761. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1762. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1763. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1764. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1765. }
  1766. event = ftrace_find_event(entry->type);
  1767. if (event) {
  1768. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  1769. if (ret != TRACE_TYPE_HANDLED)
  1770. return ret;
  1771. }
  1772. SEQ_PUT_FIELD_RET(s, newline);
  1773. return TRACE_TYPE_HANDLED;
  1774. }
  1775. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1776. {
  1777. struct trace_seq *s = &iter->seq;
  1778. struct trace_entry *entry;
  1779. struct trace_event *event;
  1780. entry = iter->ent;
  1781. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1782. SEQ_PUT_FIELD_RET(s, entry->pid);
  1783. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1784. SEQ_PUT_FIELD_RET(s, iter->ts);
  1785. }
  1786. event = ftrace_find_event(entry->type);
  1787. return event ? event->funcs->binary(iter, 0, event) :
  1788. TRACE_TYPE_HANDLED;
  1789. }
  1790. int trace_empty(struct trace_iterator *iter)
  1791. {
  1792. int cpu;
  1793. /* If we are looking at one CPU buffer, only check that one */
  1794. if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
  1795. cpu = iter->cpu_file;
  1796. if (iter->buffer_iter[cpu]) {
  1797. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1798. return 0;
  1799. } else {
  1800. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1801. return 0;
  1802. }
  1803. return 1;
  1804. }
  1805. for_each_tracing_cpu(cpu) {
  1806. if (iter->buffer_iter[cpu]) {
  1807. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1808. return 0;
  1809. } else {
  1810. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1811. return 0;
  1812. }
  1813. }
  1814. return 1;
  1815. }
  1816. /* Called with trace_event_read_lock() held. */
  1817. enum print_line_t print_trace_line(struct trace_iterator *iter)
  1818. {
  1819. enum print_line_t ret;
  1820. if (iter->lost_events &&
  1821. !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  1822. iter->cpu, iter->lost_events))
  1823. return TRACE_TYPE_PARTIAL_LINE;
  1824. if (iter->trace && iter->trace->print_line) {
  1825. ret = iter->trace->print_line(iter);
  1826. if (ret != TRACE_TYPE_UNHANDLED)
  1827. return ret;
  1828. }
  1829. if (iter->ent->type == TRACE_BPRINT &&
  1830. trace_flags & TRACE_ITER_PRINTK &&
  1831. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1832. return trace_print_bprintk_msg_only(iter);
  1833. if (iter->ent->type == TRACE_PRINT &&
  1834. trace_flags & TRACE_ITER_PRINTK &&
  1835. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1836. return trace_print_printk_msg_only(iter);
  1837. if (trace_flags & TRACE_ITER_BIN)
  1838. return print_bin_fmt(iter);
  1839. if (trace_flags & TRACE_ITER_HEX)
  1840. return print_hex_fmt(iter);
  1841. if (trace_flags & TRACE_ITER_RAW)
  1842. return print_raw_fmt(iter);
  1843. return print_trace_fmt(iter);
  1844. }
  1845. void trace_latency_header(struct seq_file *m)
  1846. {
  1847. struct trace_iterator *iter = m->private;
  1848. /* print nothing if the buffers are empty */
  1849. if (trace_empty(iter))
  1850. return;
  1851. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1852. print_trace_header(m, iter);
  1853. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1854. print_lat_help_header(m);
  1855. }
  1856. void trace_default_header(struct seq_file *m)
  1857. {
  1858. struct trace_iterator *iter = m->private;
  1859. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  1860. return;
  1861. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1862. /* print nothing if the buffers are empty */
  1863. if (trace_empty(iter))
  1864. return;
  1865. print_trace_header(m, iter);
  1866. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1867. print_lat_help_header(m);
  1868. } else {
  1869. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  1870. if (trace_flags & TRACE_ITER_IRQ_INFO)
  1871. print_func_help_header_irq(iter->tr, m);
  1872. else
  1873. print_func_help_header(iter->tr, m);
  1874. }
  1875. }
  1876. }
  1877. static void test_ftrace_alive(struct seq_file *m)
  1878. {
  1879. if (!ftrace_is_dead())
  1880. return;
  1881. seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  1882. seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
  1883. }
  1884. static int s_show(struct seq_file *m, void *v)
  1885. {
  1886. struct trace_iterator *iter = v;
  1887. int ret;
  1888. if (iter->ent == NULL) {
  1889. if (iter->tr) {
  1890. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1891. seq_puts(m, "#\n");
  1892. test_ftrace_alive(m);
  1893. }
  1894. if (iter->trace && iter->trace->print_header)
  1895. iter->trace->print_header(m);
  1896. else
  1897. trace_default_header(m);
  1898. } else if (iter->leftover) {
  1899. /*
  1900. * If we filled the seq_file buffer earlier, we
  1901. * want to just show it now.
  1902. */
  1903. ret = trace_print_seq(m, &iter->seq);
  1904. /* ret should this time be zero, but you never know */
  1905. iter->leftover = ret;
  1906. } else {
  1907. print_trace_line(iter);
  1908. ret = trace_print_seq(m, &iter->seq);
  1909. /*
  1910. * If we overflow the seq_file buffer, then it will
  1911. * ask us for this data again at start up.
  1912. * Use that instead.
  1913. * ret is 0 if seq_file write succeeded.
  1914. * -1 otherwise.
  1915. */
  1916. iter->leftover = ret;
  1917. }
  1918. return 0;
  1919. }
  1920. static const struct seq_operations tracer_seq_ops = {
  1921. .start = s_start,
  1922. .next = s_next,
  1923. .stop = s_stop,
  1924. .show = s_show,
  1925. };
  1926. static struct trace_iterator *
  1927. __tracing_open(struct inode *inode, struct file *file)
  1928. {
  1929. long cpu_file = (long) inode->i_private;
  1930. void *fail_ret = ERR_PTR(-ENOMEM);
  1931. struct trace_iterator *iter;
  1932. struct seq_file *m;
  1933. int cpu, ret;
  1934. if (tracing_disabled)
  1935. return ERR_PTR(-ENODEV);
  1936. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1937. if (!iter)
  1938. return ERR_PTR(-ENOMEM);
  1939. /*
  1940. * We make a copy of the current tracer to avoid concurrent
  1941. * changes on it while we are reading.
  1942. */
  1943. mutex_lock(&trace_types_lock);
  1944. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  1945. if (!iter->trace)
  1946. goto fail;
  1947. if (current_trace)
  1948. *iter->trace = *current_trace;
  1949. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  1950. goto fail;
  1951. if (current_trace && current_trace->print_max)
  1952. iter->tr = &max_tr;
  1953. else
  1954. iter->tr = &global_trace;
  1955. iter->pos = -1;
  1956. mutex_init(&iter->mutex);
  1957. iter->cpu_file = cpu_file;
  1958. /* Notify the tracer early; before we stop tracing. */
  1959. if (iter->trace && iter->trace->open)
  1960. iter->trace->open(iter);
  1961. /* Annotate start of buffers if we had overruns */
  1962. if (ring_buffer_overruns(iter->tr->buffer))
  1963. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1964. /* stop the trace while dumping */
  1965. tracing_stop();
  1966. if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
  1967. for_each_tracing_cpu(cpu) {
  1968. iter->buffer_iter[cpu] =
  1969. ring_buffer_read_prepare(iter->tr->buffer, cpu);
  1970. }
  1971. ring_buffer_read_prepare_sync();
  1972. for_each_tracing_cpu(cpu) {
  1973. ring_buffer_read_start(iter->buffer_iter[cpu]);
  1974. tracing_iter_reset(iter, cpu);
  1975. }
  1976. } else {
  1977. cpu = iter->cpu_file;
  1978. iter->buffer_iter[cpu] =
  1979. ring_buffer_read_prepare(iter->tr->buffer, cpu);
  1980. ring_buffer_read_prepare_sync();
  1981. ring_buffer_read_start(iter->buffer_iter[cpu]);
  1982. tracing_iter_reset(iter, cpu);
  1983. }
  1984. ret = seq_open(file, &tracer_seq_ops);
  1985. if (ret < 0) {
  1986. fail_ret = ERR_PTR(ret);
  1987. goto fail_buffer;
  1988. }
  1989. m = file->private_data;
  1990. m->private = iter;
  1991. mutex_unlock(&trace_types_lock);
  1992. return iter;
  1993. fail_buffer:
  1994. for_each_tracing_cpu(cpu) {
  1995. if (iter->buffer_iter[cpu])
  1996. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1997. }
  1998. free_cpumask_var(iter->started);
  1999. tracing_start();
  2000. fail:
  2001. mutex_unlock(&trace_types_lock);
  2002. kfree(iter->trace);
  2003. kfree(iter);
  2004. return fail_ret;
  2005. }
  2006. int tracing_open_generic(struct inode *inode, struct file *filp)
  2007. {
  2008. if (tracing_disabled)
  2009. return -ENODEV;
  2010. filp->private_data = inode->i_private;
  2011. return 0;
  2012. }
  2013. static int tracing_release(struct inode *inode, struct file *file)
  2014. {
  2015. struct seq_file *m = file->private_data;
  2016. struct trace_iterator *iter;
  2017. int cpu;
  2018. if (!(file->f_mode & FMODE_READ))
  2019. return 0;
  2020. iter = m->private;
  2021. mutex_lock(&trace_types_lock);
  2022. for_each_tracing_cpu(cpu) {
  2023. if (iter->buffer_iter[cpu])
  2024. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2025. }
  2026. if (iter->trace && iter->trace->close)
  2027. iter->trace->close(iter);
  2028. /* reenable tracing if it was previously enabled */
  2029. tracing_start();
  2030. mutex_unlock(&trace_types_lock);
  2031. seq_release(inode, file);
  2032. mutex_destroy(&iter->mutex);
  2033. free_cpumask_var(iter->started);
  2034. kfree(iter->trace);
  2035. kfree(iter);
  2036. return 0;
  2037. }
  2038. static int tracing_open(struct inode *inode, struct file *file)
  2039. {
  2040. struct trace_iterator *iter;
  2041. int ret = 0;
  2042. /* If this file was open for write, then erase contents */
  2043. if ((file->f_mode & FMODE_WRITE) &&
  2044. (file->f_flags & O_TRUNC)) {
  2045. long cpu = (long) inode->i_private;
  2046. if (cpu == TRACE_PIPE_ALL_CPU)
  2047. tracing_reset_online_cpus(&global_trace);
  2048. else
  2049. tracing_reset(&global_trace, cpu);
  2050. }
  2051. if (file->f_mode & FMODE_READ) {
  2052. iter = __tracing_open(inode, file);
  2053. if (IS_ERR(iter))
  2054. ret = PTR_ERR(iter);
  2055. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2056. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2057. }
  2058. return ret;
  2059. }
  2060. static void *
  2061. t_next(struct seq_file *m, void *v, loff_t *pos)
  2062. {
  2063. struct tracer *t = v;
  2064. (*pos)++;
  2065. if (t)
  2066. t = t->next;
  2067. return t;
  2068. }
  2069. static void *t_start(struct seq_file *m, loff_t *pos)
  2070. {
  2071. struct tracer *t;
  2072. loff_t l = 0;
  2073. mutex_lock(&trace_types_lock);
  2074. for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
  2075. ;
  2076. return t;
  2077. }
  2078. static void t_stop(struct seq_file *m, void *p)
  2079. {
  2080. mutex_unlock(&trace_types_lock);
  2081. }
  2082. static int t_show(struct seq_file *m, void *v)
  2083. {
  2084. struct tracer *t = v;
  2085. if (!t)
  2086. return 0;
  2087. seq_printf(m, "%s", t->name);
  2088. if (t->next)
  2089. seq_putc(m, ' ');
  2090. else
  2091. seq_putc(m, '\n');
  2092. return 0;
  2093. }
  2094. static const struct seq_operations show_traces_seq_ops = {
  2095. .start = t_start,
  2096. .next = t_next,
  2097. .stop = t_stop,
  2098. .show = t_show,
  2099. };
  2100. static int show_traces_open(struct inode *inode, struct file *file)
  2101. {
  2102. if (tracing_disabled)
  2103. return -ENODEV;
  2104. return seq_open(file, &show_traces_seq_ops);
  2105. }
  2106. static ssize_t
  2107. tracing_write_stub(struct file *filp, const char __user *ubuf,
  2108. size_t count, loff_t *ppos)
  2109. {
  2110. return count;
  2111. }
  2112. static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
  2113. {
  2114. if (file->f_mode & FMODE_READ)
  2115. return seq_lseek(file, offset, origin);
  2116. else
  2117. return 0;
  2118. }
  2119. static const struct file_operations tracing_fops = {
  2120. .open = tracing_open,
  2121. .read = seq_read,
  2122. .write = tracing_write_stub,
  2123. .llseek = tracing_seek,
  2124. .release = tracing_release,
  2125. };
  2126. static const struct file_operations show_traces_fops = {
  2127. .open = show_traces_open,
  2128. .read = seq_read,
  2129. .release = seq_release,
  2130. .llseek = seq_lseek,
  2131. };
  2132. /*
  2133. * Only trace on a CPU if the bitmask is set:
  2134. */
  2135. static cpumask_var_t tracing_cpumask;
  2136. /*
  2137. * The tracer itself will not take this lock, but still we want
  2138. * to provide a consistent cpumask to user-space:
  2139. */
  2140. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2141. /*
  2142. * Temporary storage for the character representation of the
  2143. * CPU bitmask (and one more byte for the newline):
  2144. */
  2145. static char mask_str[NR_CPUS + 1];
  2146. static ssize_t
  2147. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2148. size_t count, loff_t *ppos)
  2149. {
  2150. int len;
  2151. mutex_lock(&tracing_cpumask_update_lock);
  2152. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2153. if (count - len < 2) {
  2154. count = -EINVAL;
  2155. goto out_err;
  2156. }
  2157. len += sprintf(mask_str + len, "\n");
  2158. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2159. out_err:
  2160. mutex_unlock(&tracing_cpumask_update_lock);
  2161. return count;
  2162. }
  2163. static ssize_t
  2164. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2165. size_t count, loff_t *ppos)
  2166. {
  2167. int err, cpu;
  2168. cpumask_var_t tracing_cpumask_new;
  2169. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  2170. return -ENOMEM;
  2171. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2172. if (err)
  2173. goto err_unlock;
  2174. mutex_lock(&tracing_cpumask_update_lock);
  2175. local_irq_disable();
  2176. arch_spin_lock(&ftrace_max_lock);
  2177. for_each_tracing_cpu(cpu) {
  2178. /*
  2179. * Increase/decrease the disabled counter if we are
  2180. * about to flip a bit in the cpumask:
  2181. */
  2182. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  2183. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2184. atomic_inc(&global_trace.data[cpu]->disabled);
  2185. }
  2186. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  2187. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2188. atomic_dec(&global_trace.data[cpu]->disabled);
  2189. }
  2190. }
  2191. arch_spin_unlock(&ftrace_max_lock);
  2192. local_irq_enable();
  2193. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  2194. mutex_unlock(&tracing_cpumask_update_lock);
  2195. free_cpumask_var(tracing_cpumask_new);
  2196. return count;
  2197. err_unlock:
  2198. free_cpumask_var(tracing_cpumask_new);
  2199. return err;
  2200. }
  2201. static const struct file_operations tracing_cpumask_fops = {
  2202. .open = tracing_open_generic,
  2203. .read = tracing_cpumask_read,
  2204. .write = tracing_cpumask_write,
  2205. .llseek = generic_file_llseek,
  2206. };
  2207. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2208. {
  2209. struct tracer_opt *trace_opts;
  2210. u32 tracer_flags;
  2211. int i;
  2212. mutex_lock(&trace_types_lock);
  2213. tracer_flags = current_trace->flags->val;
  2214. trace_opts = current_trace->flags->opts;
  2215. for (i = 0; trace_options[i]; i++) {
  2216. if (trace_flags & (1 << i))
  2217. seq_printf(m, "%s\n", trace_options[i]);
  2218. else
  2219. seq_printf(m, "no%s\n", trace_options[i]);
  2220. }
  2221. for (i = 0; trace_opts[i].name; i++) {
  2222. if (tracer_flags & trace_opts[i].bit)
  2223. seq_printf(m, "%s\n", trace_opts[i].name);
  2224. else
  2225. seq_printf(m, "no%s\n", trace_opts[i].name);
  2226. }
  2227. mutex_unlock(&trace_types_lock);
  2228. return 0;
  2229. }
  2230. static int __set_tracer_option(struct tracer *trace,
  2231. struct tracer_flags *tracer_flags,
  2232. struct tracer_opt *opts, int neg)
  2233. {
  2234. int ret;
  2235. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2236. if (ret)
  2237. return ret;
  2238. if (neg)
  2239. tracer_flags->val &= ~opts->bit;
  2240. else
  2241. tracer_flags->val |= opts->bit;
  2242. return 0;
  2243. }
  2244. /* Try to assign a tracer specific option */
  2245. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2246. {
  2247. struct tracer_flags *tracer_flags = trace->flags;
  2248. struct tracer_opt *opts = NULL;
  2249. int i;
  2250. for (i = 0; tracer_flags->opts[i].name; i++) {
  2251. opts = &tracer_flags->opts[i];
  2252. if (strcmp(cmp, opts->name) == 0)
  2253. return __set_tracer_option(trace, trace->flags,
  2254. opts, neg);
  2255. }
  2256. return -EINVAL;
  2257. }
  2258. static void set_tracer_flags(unsigned int mask, int enabled)
  2259. {
  2260. /* do nothing if flag is already set */
  2261. if (!!(trace_flags & mask) == !!enabled)
  2262. return;
  2263. if (enabled)
  2264. trace_flags |= mask;
  2265. else
  2266. trace_flags &= ~mask;
  2267. if (mask == TRACE_ITER_RECORD_CMD)
  2268. trace_event_enable_cmd_record(enabled);
  2269. if (mask == TRACE_ITER_OVERWRITE)
  2270. ring_buffer_change_overwrite(global_trace.buffer, enabled);
  2271. }
  2272. static ssize_t
  2273. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2274. size_t cnt, loff_t *ppos)
  2275. {
  2276. char buf[64];
  2277. char *cmp;
  2278. int neg = 0;
  2279. int ret;
  2280. int i;
  2281. if (cnt >= sizeof(buf))
  2282. return -EINVAL;
  2283. if (copy_from_user(&buf, ubuf, cnt))
  2284. return -EFAULT;
  2285. buf[cnt] = 0;
  2286. cmp = strstrip(buf);
  2287. if (strncmp(cmp, "no", 2) == 0) {
  2288. neg = 1;
  2289. cmp += 2;
  2290. }
  2291. for (i = 0; trace_options[i]; i++) {
  2292. if (strcmp(cmp, trace_options[i]) == 0) {
  2293. set_tracer_flags(1 << i, !neg);
  2294. break;
  2295. }
  2296. }
  2297. /* If no option could be set, test the specific tracer options */
  2298. if (!trace_options[i]) {
  2299. mutex_lock(&trace_types_lock);
  2300. ret = set_tracer_option(current_trace, cmp, neg);
  2301. mutex_unlock(&trace_types_lock);
  2302. if (ret)
  2303. return ret;
  2304. }
  2305. *ppos += cnt;
  2306. return cnt;
  2307. }
  2308. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2309. {
  2310. if (tracing_disabled)
  2311. return -ENODEV;
  2312. return single_open(file, tracing_trace_options_show, NULL);
  2313. }
  2314. static const struct file_operations tracing_iter_fops = {
  2315. .open = tracing_trace_options_open,
  2316. .read = seq_read,
  2317. .llseek = seq_lseek,
  2318. .release = single_release,
  2319. .write = tracing_trace_options_write,
  2320. };
  2321. static const char readme_msg[] =
  2322. "tracing mini-HOWTO:\n\n"
  2323. "# mount -t debugfs nodev /sys/kernel/debug\n\n"
  2324. "# cat /sys/kernel/debug/tracing/available_tracers\n"
  2325. "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
  2326. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2327. "nop\n"
  2328. "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
  2329. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2330. "wakeup\n"
  2331. "# cat /sys/kernel/debug/tracing/trace_options\n"
  2332. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2333. "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
  2334. "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
  2335. "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
  2336. "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
  2337. ;
  2338. static ssize_t
  2339. tracing_readme_read(struct file *filp, char __user *ubuf,
  2340. size_t cnt, loff_t *ppos)
  2341. {
  2342. return simple_read_from_buffer(ubuf, cnt, ppos,
  2343. readme_msg, strlen(readme_msg));
  2344. }
  2345. static const struct file_operations tracing_readme_fops = {
  2346. .open = tracing_open_generic,
  2347. .read = tracing_readme_read,
  2348. .llseek = generic_file_llseek,
  2349. };
  2350. static ssize_t
  2351. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2352. size_t cnt, loff_t *ppos)
  2353. {
  2354. char *buf_comm;
  2355. char *file_buf;
  2356. char *buf;
  2357. int len = 0;
  2358. int pid;
  2359. int i;
  2360. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2361. if (!file_buf)
  2362. return -ENOMEM;
  2363. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2364. if (!buf_comm) {
  2365. kfree(file_buf);
  2366. return -ENOMEM;
  2367. }
  2368. buf = file_buf;
  2369. for (i = 0; i < SAVED_CMDLINES; i++) {
  2370. int r;
  2371. pid = map_cmdline_to_pid[i];
  2372. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2373. continue;
  2374. trace_find_cmdline(pid, buf_comm);
  2375. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2376. buf += r;
  2377. len += r;
  2378. }
  2379. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2380. file_buf, len);
  2381. kfree(file_buf);
  2382. kfree(buf_comm);
  2383. return len;
  2384. }
  2385. static const struct file_operations tracing_saved_cmdlines_fops = {
  2386. .open = tracing_open_generic,
  2387. .read = tracing_saved_cmdlines_read,
  2388. .llseek = generic_file_llseek,
  2389. };
  2390. static ssize_t
  2391. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2392. size_t cnt, loff_t *ppos)
  2393. {
  2394. char buf[64];
  2395. int r;
  2396. r = sprintf(buf, "%u\n", tracer_enabled);
  2397. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2398. }
  2399. static ssize_t
  2400. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2401. size_t cnt, loff_t *ppos)
  2402. {
  2403. struct trace_array *tr = filp->private_data;
  2404. unsigned long val;
  2405. int ret;
  2406. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  2407. if (ret)
  2408. return ret;
  2409. val = !!val;
  2410. mutex_lock(&trace_types_lock);
  2411. if (tracer_enabled ^ val) {
  2412. /* Only need to warn if this is used to change the state */
  2413. WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
  2414. if (val) {
  2415. tracer_enabled = 1;
  2416. if (current_trace->start)
  2417. current_trace->start(tr);
  2418. tracing_start();
  2419. } else {
  2420. tracer_enabled = 0;
  2421. tracing_stop();
  2422. if (current_trace->stop)
  2423. current_trace->stop(tr);
  2424. }
  2425. }
  2426. mutex_unlock(&trace_types_lock);
  2427. *ppos += cnt;
  2428. return cnt;
  2429. }
  2430. static ssize_t
  2431. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2432. size_t cnt, loff_t *ppos)
  2433. {
  2434. char buf[MAX_TRACER_SIZE+2];
  2435. int r;
  2436. mutex_lock(&trace_types_lock);
  2437. if (current_trace)
  2438. r = sprintf(buf, "%s\n", current_trace->name);
  2439. else
  2440. r = sprintf(buf, "\n");
  2441. mutex_unlock(&trace_types_lock);
  2442. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2443. }
  2444. int tracer_init(struct tracer *t, struct trace_array *tr)
  2445. {
  2446. tracing_reset_online_cpus(tr);
  2447. return t->init(tr);
  2448. }
  2449. static int __tracing_resize_ring_buffer(unsigned long size)
  2450. {
  2451. int ret;
  2452. /*
  2453. * If kernel or user changes the size of the ring buffer
  2454. * we use the size that was given, and we can forget about
  2455. * expanding it later.
  2456. */
  2457. ring_buffer_expanded = 1;
  2458. ret = ring_buffer_resize(global_trace.buffer, size);
  2459. if (ret < 0)
  2460. return ret;
  2461. if (!current_trace->use_max_tr)
  2462. goto out;
  2463. ret = ring_buffer_resize(max_tr.buffer, size);
  2464. if (ret < 0) {
  2465. int r;
  2466. r = ring_buffer_resize(global_trace.buffer,
  2467. global_trace.entries);
  2468. if (r < 0) {
  2469. /*
  2470. * AARGH! We are left with different
  2471. * size max buffer!!!!
  2472. * The max buffer is our "snapshot" buffer.
  2473. * When a tracer needs a snapshot (one of the
  2474. * latency tracers), it swaps the max buffer
  2475. * with the saved snap shot. We succeeded to
  2476. * update the size of the main buffer, but failed to
  2477. * update the size of the max buffer. But when we tried
  2478. * to reset the main buffer to the original size, we
  2479. * failed there too. This is very unlikely to
  2480. * happen, but if it does, warn and kill all
  2481. * tracing.
  2482. */
  2483. WARN_ON(1);
  2484. tracing_disabled = 1;
  2485. }
  2486. return ret;
  2487. }
  2488. max_tr.entries = size;
  2489. out:
  2490. global_trace.entries = size;
  2491. return ret;
  2492. }
  2493. static ssize_t tracing_resize_ring_buffer(unsigned long size)
  2494. {
  2495. int cpu, ret = size;
  2496. mutex_lock(&trace_types_lock);
  2497. tracing_stop();
  2498. /* disable all cpu buffers */
  2499. for_each_tracing_cpu(cpu) {
  2500. if (global_trace.data[cpu])
  2501. atomic_inc(&global_trace.data[cpu]->disabled);
  2502. if (max_tr.data[cpu])
  2503. atomic_inc(&max_tr.data[cpu]->disabled);
  2504. }
  2505. if (size != global_trace.entries)
  2506. ret = __tracing_resize_ring_buffer(size);
  2507. if (ret < 0)
  2508. ret = -ENOMEM;
  2509. for_each_tracing_cpu(cpu) {
  2510. if (global_trace.data[cpu])
  2511. atomic_dec(&global_trace.data[cpu]->disabled);
  2512. if (max_tr.data[cpu])
  2513. atomic_dec(&max_tr.data[cpu]->disabled);
  2514. }
  2515. tracing_start();
  2516. mutex_unlock(&trace_types_lock);
  2517. return ret;
  2518. }
  2519. /**
  2520. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2521. *
  2522. * To save on memory when the tracing is never used on a system with it
  2523. * configured in. The ring buffers are set to a minimum size. But once
  2524. * a user starts to use the tracing facility, then they need to grow
  2525. * to their default size.
  2526. *
  2527. * This function is to be called when a tracer is about to be used.
  2528. */
  2529. int tracing_update_buffers(void)
  2530. {
  2531. int ret = 0;
  2532. mutex_lock(&trace_types_lock);
  2533. if (!ring_buffer_expanded)
  2534. ret = __tracing_resize_ring_buffer(trace_buf_size);
  2535. mutex_unlock(&trace_types_lock);
  2536. return ret;
  2537. }
  2538. struct trace_option_dentry;
  2539. static struct trace_option_dentry *
  2540. create_trace_option_files(struct tracer *tracer);
  2541. static void
  2542. destroy_trace_option_files(struct trace_option_dentry *topts);
  2543. static int tracing_set_tracer(const char *buf)
  2544. {
  2545. static struct trace_option_dentry *topts;
  2546. struct trace_array *tr = &global_trace;
  2547. struct tracer *t;
  2548. int ret = 0;
  2549. mutex_lock(&trace_types_lock);
  2550. if (!ring_buffer_expanded) {
  2551. ret = __tracing_resize_ring_buffer(trace_buf_size);
  2552. if (ret < 0)
  2553. goto out;
  2554. ret = 0;
  2555. }
  2556. for (t = trace_types; t; t = t->next) {
  2557. if (strcmp(t->name, buf) == 0)
  2558. break;
  2559. }
  2560. if (!t) {
  2561. ret = -EINVAL;
  2562. goto out;
  2563. }
  2564. if (t == current_trace)
  2565. goto out;
  2566. trace_branch_disable();
  2567. if (current_trace && current_trace->reset)
  2568. current_trace->reset(tr);
  2569. if (current_trace && current_trace->use_max_tr) {
  2570. /*
  2571. * We don't free the ring buffer. instead, resize it because
  2572. * The max_tr ring buffer has some state (e.g. ring->clock) and
  2573. * we want preserve it.
  2574. */
  2575. ring_buffer_resize(max_tr.buffer, 1);
  2576. max_tr.entries = 1;
  2577. }
  2578. destroy_trace_option_files(topts);
  2579. current_trace = t;
  2580. topts = create_trace_option_files(current_trace);
  2581. if (current_trace->use_max_tr) {
  2582. ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
  2583. if (ret < 0)
  2584. goto out;
  2585. max_tr.entries = global_trace.entries;
  2586. }
  2587. if (t->init) {
  2588. ret = tracer_init(t, tr);
  2589. if (ret)
  2590. goto out;
  2591. }
  2592. trace_branch_enable(tr);
  2593. out:
  2594. mutex_unlock(&trace_types_lock);
  2595. return ret;
  2596. }
  2597. static ssize_t
  2598. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2599. size_t cnt, loff_t *ppos)
  2600. {
  2601. char buf[MAX_TRACER_SIZE+1];
  2602. int i;
  2603. size_t ret;
  2604. int err;
  2605. ret = cnt;
  2606. if (cnt > MAX_TRACER_SIZE)
  2607. cnt = MAX_TRACER_SIZE;
  2608. if (copy_from_user(&buf, ubuf, cnt))
  2609. return -EFAULT;
  2610. buf[cnt] = 0;
  2611. /* strip ending whitespace. */
  2612. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2613. buf[i] = 0;
  2614. err = tracing_set_tracer(buf);
  2615. if (err)
  2616. return err;
  2617. *ppos += ret;
  2618. return ret;
  2619. }
  2620. static ssize_t
  2621. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2622. size_t cnt, loff_t *ppos)
  2623. {
  2624. unsigned long *ptr = filp->private_data;
  2625. char buf[64];
  2626. int r;
  2627. r = snprintf(buf, sizeof(buf), "%ld\n",
  2628. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2629. if (r > sizeof(buf))
  2630. r = sizeof(buf);
  2631. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2632. }
  2633. static ssize_t
  2634. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2635. size_t cnt, loff_t *ppos)
  2636. {
  2637. unsigned long *ptr = filp->private_data;
  2638. unsigned long val;
  2639. int ret;
  2640. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  2641. if (ret)
  2642. return ret;
  2643. *ptr = val * 1000;
  2644. return cnt;
  2645. }
  2646. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2647. {
  2648. long cpu_file = (long) inode->i_private;
  2649. struct trace_iterator *iter;
  2650. int ret = 0;
  2651. if (tracing_disabled)
  2652. return -ENODEV;
  2653. mutex_lock(&trace_types_lock);
  2654. /* create a buffer to store the information to pass to userspace */
  2655. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2656. if (!iter) {
  2657. ret = -ENOMEM;
  2658. goto out;
  2659. }
  2660. /*
  2661. * We make a copy of the current tracer to avoid concurrent
  2662. * changes on it while we are reading.
  2663. */
  2664. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2665. if (!iter->trace) {
  2666. ret = -ENOMEM;
  2667. goto fail;
  2668. }
  2669. if (current_trace)
  2670. *iter->trace = *current_trace;
  2671. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2672. ret = -ENOMEM;
  2673. goto fail;
  2674. }
  2675. /* trace pipe does not show start of buffer */
  2676. cpumask_setall(iter->started);
  2677. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2678. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2679. iter->cpu_file = cpu_file;
  2680. iter->tr = &global_trace;
  2681. mutex_init(&iter->mutex);
  2682. filp->private_data = iter;
  2683. if (iter->trace->pipe_open)
  2684. iter->trace->pipe_open(iter);
  2685. nonseekable_open(inode, filp);
  2686. out:
  2687. mutex_unlock(&trace_types_lock);
  2688. return ret;
  2689. fail:
  2690. kfree(iter->trace);
  2691. kfree(iter);
  2692. mutex_unlock(&trace_types_lock);
  2693. return ret;
  2694. }
  2695. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2696. {
  2697. struct trace_iterator *iter = file->private_data;
  2698. mutex_lock(&trace_types_lock);
  2699. if (iter->trace->pipe_close)
  2700. iter->trace->pipe_close(iter);
  2701. mutex_unlock(&trace_types_lock);
  2702. free_cpumask_var(iter->started);
  2703. mutex_destroy(&iter->mutex);
  2704. kfree(iter->trace);
  2705. kfree(iter);
  2706. return 0;
  2707. }
  2708. static unsigned int
  2709. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2710. {
  2711. struct trace_iterator *iter = filp->private_data;
  2712. if (trace_flags & TRACE_ITER_BLOCK) {
  2713. /*
  2714. * Always select as readable when in blocking mode
  2715. */
  2716. return POLLIN | POLLRDNORM;
  2717. } else {
  2718. if (!trace_empty(iter))
  2719. return POLLIN | POLLRDNORM;
  2720. poll_wait(filp, &trace_wait, poll_table);
  2721. if (!trace_empty(iter))
  2722. return POLLIN | POLLRDNORM;
  2723. return 0;
  2724. }
  2725. }
  2726. void default_wait_pipe(struct trace_iterator *iter)
  2727. {
  2728. DEFINE_WAIT(wait);
  2729. prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
  2730. if (trace_empty(iter))
  2731. schedule();
  2732. finish_wait(&trace_wait, &wait);
  2733. }
  2734. /*
  2735. * This is a make-shift waitqueue.
  2736. * A tracer might use this callback on some rare cases:
  2737. *
  2738. * 1) the current tracer might hold the runqueue lock when it wakes up
  2739. * a reader, hence a deadlock (sched, function, and function graph tracers)
  2740. * 2) the function tracers, trace all functions, we don't want
  2741. * the overhead of calling wake_up and friends
  2742. * (and tracing them too)
  2743. *
  2744. * Anyway, this is really very primitive wakeup.
  2745. */
  2746. void poll_wait_pipe(struct trace_iterator *iter)
  2747. {
  2748. set_current_state(TASK_INTERRUPTIBLE);
  2749. /* sleep for 100 msecs, and try again. */
  2750. schedule_timeout(HZ / 10);
  2751. }
  2752. /* Must be called with trace_types_lock mutex held. */
  2753. static int tracing_wait_pipe(struct file *filp)
  2754. {
  2755. struct trace_iterator *iter = filp->private_data;
  2756. while (trace_empty(iter)) {
  2757. if ((filp->f_flags & O_NONBLOCK)) {
  2758. return -EAGAIN;
  2759. }
  2760. mutex_unlock(&iter->mutex);
  2761. iter->trace->wait_pipe(iter);
  2762. mutex_lock(&iter->mutex);
  2763. if (signal_pending(current))
  2764. return -EINTR;
  2765. /*
  2766. * We block until we read something and tracing is disabled.
  2767. * We still block if tracing is disabled, but we have never
  2768. * read anything. This allows a user to cat this file, and
  2769. * then enable tracing. But after we have read something,
  2770. * we give an EOF when tracing is again disabled.
  2771. *
  2772. * iter->pos will be 0 if we haven't read anything.
  2773. */
  2774. if (!tracer_enabled && iter->pos)
  2775. break;
  2776. }
  2777. return 1;
  2778. }
  2779. /*
  2780. * Consumer reader.
  2781. */
  2782. static ssize_t
  2783. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2784. size_t cnt, loff_t *ppos)
  2785. {
  2786. struct trace_iterator *iter = filp->private_data;
  2787. static struct tracer *old_tracer;
  2788. ssize_t sret;
  2789. /* return any leftover data */
  2790. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2791. if (sret != -EBUSY)
  2792. return sret;
  2793. trace_seq_init(&iter->seq);
  2794. /* copy the tracer to avoid using a global lock all around */
  2795. mutex_lock(&trace_types_lock);
  2796. if (unlikely(old_tracer != current_trace && current_trace)) {
  2797. old_tracer = current_trace;
  2798. *iter->trace = *current_trace;
  2799. }
  2800. mutex_unlock(&trace_types_lock);
  2801. /*
  2802. * Avoid more than one consumer on a single file descriptor
  2803. * This is just a matter of traces coherency, the ring buffer itself
  2804. * is protected.
  2805. */
  2806. mutex_lock(&iter->mutex);
  2807. if (iter->trace->read) {
  2808. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2809. if (sret)
  2810. goto out;
  2811. }
  2812. waitagain:
  2813. sret = tracing_wait_pipe(filp);
  2814. if (sret <= 0)
  2815. goto out;
  2816. /* stop when tracing is finished */
  2817. if (trace_empty(iter)) {
  2818. sret = 0;
  2819. goto out;
  2820. }
  2821. if (cnt >= PAGE_SIZE)
  2822. cnt = PAGE_SIZE - 1;
  2823. /* reset all but tr, trace, and overruns */
  2824. memset(&iter->seq, 0,
  2825. sizeof(struct trace_iterator) -
  2826. offsetof(struct trace_iterator, seq));
  2827. iter->pos = -1;
  2828. trace_event_read_lock();
  2829. trace_access_lock(iter->cpu_file);
  2830. while (trace_find_next_entry_inc(iter) != NULL) {
  2831. enum print_line_t ret;
  2832. int len = iter->seq.len;
  2833. ret = print_trace_line(iter);
  2834. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2835. /* don't print partial lines */
  2836. iter->seq.len = len;
  2837. break;
  2838. }
  2839. if (ret != TRACE_TYPE_NO_CONSUME)
  2840. trace_consume(iter);
  2841. if (iter->seq.len >= cnt)
  2842. break;
  2843. /*
  2844. * Setting the full flag means we reached the trace_seq buffer
  2845. * size and we should leave by partial output condition above.
  2846. * One of the trace_seq_* functions is not used properly.
  2847. */
  2848. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  2849. iter->ent->type);
  2850. }
  2851. trace_access_unlock(iter->cpu_file);
  2852. trace_event_read_unlock();
  2853. /* Now copy what we have to the user */
  2854. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2855. if (iter->seq.readpos >= iter->seq.len)
  2856. trace_seq_init(&iter->seq);
  2857. /*
  2858. * If there was nothing to send to user, in spite of consuming trace
  2859. * entries, go back to wait for more entries.
  2860. */
  2861. if (sret == -EBUSY)
  2862. goto waitagain;
  2863. out:
  2864. mutex_unlock(&iter->mutex);
  2865. return sret;
  2866. }
  2867. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  2868. struct pipe_buffer *buf)
  2869. {
  2870. __free_page(buf->page);
  2871. }
  2872. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  2873. unsigned int idx)
  2874. {
  2875. __free_page(spd->pages[idx]);
  2876. }
  2877. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  2878. .can_merge = 0,
  2879. .map = generic_pipe_buf_map,
  2880. .unmap = generic_pipe_buf_unmap,
  2881. .confirm = generic_pipe_buf_confirm,
  2882. .release = tracing_pipe_buf_release,
  2883. .steal = generic_pipe_buf_steal,
  2884. .get = generic_pipe_buf_get,
  2885. };
  2886. static size_t
  2887. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  2888. {
  2889. size_t count;
  2890. int ret;
  2891. /* Seq buffer is page-sized, exactly what we need. */
  2892. for (;;) {
  2893. count = iter->seq.len;
  2894. ret = print_trace_line(iter);
  2895. count = iter->seq.len - count;
  2896. if (rem < count) {
  2897. rem = 0;
  2898. iter->seq.len -= count;
  2899. break;
  2900. }
  2901. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2902. iter->seq.len -= count;
  2903. break;
  2904. }
  2905. if (ret != TRACE_TYPE_NO_CONSUME)
  2906. trace_consume(iter);
  2907. rem -= count;
  2908. if (!trace_find_next_entry_inc(iter)) {
  2909. rem = 0;
  2910. iter->ent = NULL;
  2911. break;
  2912. }
  2913. }
  2914. return rem;
  2915. }
  2916. static ssize_t tracing_splice_read_pipe(struct file *filp,
  2917. loff_t *ppos,
  2918. struct pipe_inode_info *pipe,
  2919. size_t len,
  2920. unsigned int flags)
  2921. {
  2922. struct page *pages_def[PIPE_DEF_BUFFERS];
  2923. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  2924. struct trace_iterator *iter = filp->private_data;
  2925. struct splice_pipe_desc spd = {
  2926. .pages = pages_def,
  2927. .partial = partial_def,
  2928. .nr_pages = 0, /* This gets updated below. */
  2929. .flags = flags,
  2930. .ops = &tracing_pipe_buf_ops,
  2931. .spd_release = tracing_spd_release_pipe,
  2932. };
  2933. static struct tracer *old_tracer;
  2934. ssize_t ret;
  2935. size_t rem;
  2936. unsigned int i;
  2937. if (splice_grow_spd(pipe, &spd))
  2938. return -ENOMEM;
  2939. /* copy the tracer to avoid using a global lock all around */
  2940. mutex_lock(&trace_types_lock);
  2941. if (unlikely(old_tracer != current_trace && current_trace)) {
  2942. old_tracer = current_trace;
  2943. *iter->trace = *current_trace;
  2944. }
  2945. mutex_unlock(&trace_types_lock);
  2946. mutex_lock(&iter->mutex);
  2947. if (iter->trace->splice_read) {
  2948. ret = iter->trace->splice_read(iter, filp,
  2949. ppos, pipe, len, flags);
  2950. if (ret)
  2951. goto out_err;
  2952. }
  2953. ret = tracing_wait_pipe(filp);
  2954. if (ret <= 0)
  2955. goto out_err;
  2956. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  2957. ret = -EFAULT;
  2958. goto out_err;
  2959. }
  2960. trace_event_read_lock();
  2961. trace_access_lock(iter->cpu_file);
  2962. /* Fill as many pages as possible. */
  2963. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  2964. spd.pages[i] = alloc_page(GFP_KERNEL);
  2965. if (!spd.pages[i])
  2966. break;
  2967. rem = tracing_fill_pipe_page(rem, iter);
  2968. /* Copy the data into the page, so we can start over. */
  2969. ret = trace_seq_to_buffer(&iter->seq,
  2970. page_address(spd.pages[i]),
  2971. iter->seq.len);
  2972. if (ret < 0) {
  2973. __free_page(spd.pages[i]);
  2974. break;
  2975. }
  2976. spd.partial[i].offset = 0;
  2977. spd.partial[i].len = iter->seq.len;
  2978. trace_seq_init(&iter->seq);
  2979. }
  2980. trace_access_unlock(iter->cpu_file);
  2981. trace_event_read_unlock();
  2982. mutex_unlock(&iter->mutex);
  2983. spd.nr_pages = i;
  2984. ret = splice_to_pipe(pipe, &spd);
  2985. out:
  2986. splice_shrink_spd(pipe, &spd);
  2987. return ret;
  2988. out_err:
  2989. mutex_unlock(&iter->mutex);
  2990. goto out;
  2991. }
  2992. static ssize_t
  2993. tracing_entries_read(struct file *filp, char __user *ubuf,
  2994. size_t cnt, loff_t *ppos)
  2995. {
  2996. struct trace_array *tr = filp->private_data;
  2997. char buf[96];
  2998. int r;
  2999. mutex_lock(&trace_types_lock);
  3000. if (!ring_buffer_expanded)
  3001. r = sprintf(buf, "%lu (expanded: %lu)\n",
  3002. tr->entries >> 10,
  3003. trace_buf_size >> 10);
  3004. else
  3005. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  3006. mutex_unlock(&trace_types_lock);
  3007. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3008. }
  3009. static ssize_t
  3010. tracing_entries_write(struct file *filp, const char __user *ubuf,
  3011. size_t cnt, loff_t *ppos)
  3012. {
  3013. unsigned long val;
  3014. int ret;
  3015. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3016. if (ret)
  3017. return ret;
  3018. /* must have at least 1 entry */
  3019. if (!val)
  3020. return -EINVAL;
  3021. /* value is in KB */
  3022. val <<= 10;
  3023. ret = tracing_resize_ring_buffer(val);
  3024. if (ret < 0)
  3025. return ret;
  3026. *ppos += cnt;
  3027. return cnt;
  3028. }
  3029. static ssize_t
  3030. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  3031. size_t cnt, loff_t *ppos)
  3032. {
  3033. struct trace_array *tr = filp->private_data;
  3034. char buf[64];
  3035. int r, cpu;
  3036. unsigned long size = 0, expanded_size = 0;
  3037. mutex_lock(&trace_types_lock);
  3038. for_each_tracing_cpu(cpu) {
  3039. size += tr->entries >> 10;
  3040. if (!ring_buffer_expanded)
  3041. expanded_size += trace_buf_size >> 10;
  3042. }
  3043. if (ring_buffer_expanded)
  3044. r = sprintf(buf, "%lu\n", size);
  3045. else
  3046. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  3047. mutex_unlock(&trace_types_lock);
  3048. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3049. }
  3050. static ssize_t
  3051. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  3052. size_t cnt, loff_t *ppos)
  3053. {
  3054. /*
  3055. * There is no need to read what the user has written, this function
  3056. * is just to make sure that there is no error when "echo" is used
  3057. */
  3058. *ppos += cnt;
  3059. return cnt;
  3060. }
  3061. static int
  3062. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  3063. {
  3064. /* disable tracing ? */
  3065. if (trace_flags & TRACE_ITER_STOP_ON_FREE)
  3066. tracing_off();
  3067. /* resize the ring buffer to 0 */
  3068. tracing_resize_ring_buffer(0);
  3069. return 0;
  3070. }
  3071. static ssize_t
  3072. tracing_mark_write(struct file *filp, const char __user *ubuf,
  3073. size_t cnt, loff_t *fpos)
  3074. {
  3075. unsigned long addr = (unsigned long)ubuf;
  3076. struct ring_buffer_event *event;
  3077. struct ring_buffer *buffer;
  3078. struct print_entry *entry;
  3079. unsigned long irq_flags;
  3080. struct page *pages[2];
  3081. int nr_pages = 1;
  3082. ssize_t written;
  3083. void *page1;
  3084. void *page2;
  3085. int offset;
  3086. int size;
  3087. int len;
  3088. int ret;
  3089. if (tracing_disabled)
  3090. return -EINVAL;
  3091. if (cnt > TRACE_BUF_SIZE)
  3092. cnt = TRACE_BUF_SIZE;
  3093. /*
  3094. * Userspace is injecting traces into the kernel trace buffer.
  3095. * We want to be as non intrusive as possible.
  3096. * To do so, we do not want to allocate any special buffers
  3097. * or take any locks, but instead write the userspace data
  3098. * straight into the ring buffer.
  3099. *
  3100. * First we need to pin the userspace buffer into memory,
  3101. * which, most likely it is, because it just referenced it.
  3102. * But there's no guarantee that it is. By using get_user_pages_fast()
  3103. * and kmap_atomic/kunmap_atomic() we can get access to the
  3104. * pages directly. We then write the data directly into the
  3105. * ring buffer.
  3106. */
  3107. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  3108. /* check if we cross pages */
  3109. if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
  3110. nr_pages = 2;
  3111. offset = addr & (PAGE_SIZE - 1);
  3112. addr &= PAGE_MASK;
  3113. ret = get_user_pages_fast(addr, nr_pages, 0, pages);
  3114. if (ret < nr_pages) {
  3115. while (--ret >= 0)
  3116. put_page(pages[ret]);
  3117. written = -EFAULT;
  3118. goto out;
  3119. }
  3120. page1 = kmap_atomic(pages[0]);
  3121. if (nr_pages == 2)
  3122. page2 = kmap_atomic(pages[1]);
  3123. local_save_flags(irq_flags);
  3124. size = sizeof(*entry) + cnt + 2; /* possible \n added */
  3125. buffer = global_trace.buffer;
  3126. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  3127. irq_flags, preempt_count());
  3128. if (!event) {
  3129. /* Ring buffer disabled, return as if not open for write */
  3130. written = -EBADF;
  3131. goto out_unlock;
  3132. }
  3133. entry = ring_buffer_event_data(event);
  3134. entry->ip = _THIS_IP_;
  3135. if (nr_pages == 2) {
  3136. len = PAGE_SIZE - offset;
  3137. memcpy(&entry->buf, page1 + offset, len);
  3138. memcpy(&entry->buf[len], page2, cnt - len);
  3139. } else
  3140. memcpy(&entry->buf, page1 + offset, cnt);
  3141. if (entry->buf[cnt - 1] != '\n') {
  3142. entry->buf[cnt] = '\n';
  3143. entry->buf[cnt + 1] = '\0';
  3144. } else
  3145. entry->buf[cnt] = '\0';
  3146. ring_buffer_unlock_commit(buffer, event);
  3147. written = cnt;
  3148. *fpos += written;
  3149. out_unlock:
  3150. if (nr_pages == 2)
  3151. kunmap_atomic(page2);
  3152. kunmap_atomic(page1);
  3153. while (nr_pages > 0)
  3154. put_page(pages[--nr_pages]);
  3155. out:
  3156. return written;
  3157. }
  3158. static int tracing_clock_show(struct seq_file *m, void *v)
  3159. {
  3160. int i;
  3161. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  3162. seq_printf(m,
  3163. "%s%s%s%s", i ? " " : "",
  3164. i == trace_clock_id ? "[" : "", trace_clocks[i].name,
  3165. i == trace_clock_id ? "]" : "");
  3166. seq_putc(m, '\n');
  3167. return 0;
  3168. }
  3169. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  3170. size_t cnt, loff_t *fpos)
  3171. {
  3172. char buf[64];
  3173. const char *clockstr;
  3174. int i;
  3175. if (cnt >= sizeof(buf))
  3176. return -EINVAL;
  3177. if (copy_from_user(&buf, ubuf, cnt))
  3178. return -EFAULT;
  3179. buf[cnt] = 0;
  3180. clockstr = strstrip(buf);
  3181. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  3182. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  3183. break;
  3184. }
  3185. if (i == ARRAY_SIZE(trace_clocks))
  3186. return -EINVAL;
  3187. trace_clock_id = i;
  3188. mutex_lock(&trace_types_lock);
  3189. ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
  3190. if (max_tr.buffer)
  3191. ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
  3192. mutex_unlock(&trace_types_lock);
  3193. *fpos += cnt;
  3194. return cnt;
  3195. }
  3196. static int tracing_clock_open(struct inode *inode, struct file *file)
  3197. {
  3198. if (tracing_disabled)
  3199. return -ENODEV;
  3200. return single_open(file, tracing_clock_show, NULL);
  3201. }
  3202. static const struct file_operations tracing_max_lat_fops = {
  3203. .open = tracing_open_generic,
  3204. .read = tracing_max_lat_read,
  3205. .write = tracing_max_lat_write,
  3206. .llseek = generic_file_llseek,
  3207. };
  3208. static const struct file_operations tracing_ctrl_fops = {
  3209. .open = tracing_open_generic,
  3210. .read = tracing_ctrl_read,
  3211. .write = tracing_ctrl_write,
  3212. .llseek = generic_file_llseek,
  3213. };
  3214. static const struct file_operations set_tracer_fops = {
  3215. .open = tracing_open_generic,
  3216. .read = tracing_set_trace_read,
  3217. .write = tracing_set_trace_write,
  3218. .llseek = generic_file_llseek,
  3219. };
  3220. static const struct file_operations tracing_pipe_fops = {
  3221. .open = tracing_open_pipe,
  3222. .poll = tracing_poll_pipe,
  3223. .read = tracing_read_pipe,
  3224. .splice_read = tracing_splice_read_pipe,
  3225. .release = tracing_release_pipe,
  3226. .llseek = no_llseek,
  3227. };
  3228. static const struct file_operations tracing_entries_fops = {
  3229. .open = tracing_open_generic,
  3230. .read = tracing_entries_read,
  3231. .write = tracing_entries_write,
  3232. .llseek = generic_file_llseek,
  3233. };
  3234. static const struct file_operations tracing_total_entries_fops = {
  3235. .open = tracing_open_generic,
  3236. .read = tracing_total_entries_read,
  3237. .llseek = generic_file_llseek,
  3238. };
  3239. static const struct file_operations tracing_free_buffer_fops = {
  3240. .write = tracing_free_buffer_write,
  3241. .release = tracing_free_buffer_release,
  3242. };
  3243. static const struct file_operations tracing_mark_fops = {
  3244. .open = tracing_open_generic,
  3245. .write = tracing_mark_write,
  3246. .llseek = generic_file_llseek,
  3247. };
  3248. static const struct file_operations trace_clock_fops = {
  3249. .open = tracing_clock_open,
  3250. .read = seq_read,
  3251. .llseek = seq_lseek,
  3252. .release = single_release,
  3253. .write = tracing_clock_write,
  3254. };
  3255. struct ftrace_buffer_info {
  3256. struct trace_array *tr;
  3257. void *spare;
  3258. int cpu;
  3259. unsigned int read;
  3260. };
  3261. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  3262. {
  3263. int cpu = (int)(long)inode->i_private;
  3264. struct ftrace_buffer_info *info;
  3265. if (tracing_disabled)
  3266. return -ENODEV;
  3267. info = kzalloc(sizeof(*info), GFP_KERNEL);
  3268. if (!info)
  3269. return -ENOMEM;
  3270. info->tr = &global_trace;
  3271. info->cpu = cpu;
  3272. info->spare = NULL;
  3273. /* Force reading ring buffer for first read */
  3274. info->read = (unsigned int)-1;
  3275. filp->private_data = info;
  3276. return nonseekable_open(inode, filp);
  3277. }
  3278. static ssize_t
  3279. tracing_buffers_read(struct file *filp, char __user *ubuf,
  3280. size_t count, loff_t *ppos)
  3281. {
  3282. struct ftrace_buffer_info *info = filp->private_data;
  3283. ssize_t ret;
  3284. size_t size;
  3285. if (!count)
  3286. return 0;
  3287. if (!info->spare)
  3288. info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
  3289. if (!info->spare)
  3290. return -ENOMEM;
  3291. /* Do we have previous read data to read? */
  3292. if (info->read < PAGE_SIZE)
  3293. goto read;
  3294. trace_access_lock(info->cpu);
  3295. ret = ring_buffer_read_page(info->tr->buffer,
  3296. &info->spare,
  3297. count,
  3298. info->cpu, 0);
  3299. trace_access_unlock(info->cpu);
  3300. if (ret < 0)
  3301. return 0;
  3302. info->read = 0;
  3303. read:
  3304. size = PAGE_SIZE - info->read;
  3305. if (size > count)
  3306. size = count;
  3307. ret = copy_to_user(ubuf, info->spare + info->read, size);
  3308. if (ret == size)
  3309. return -EFAULT;
  3310. size -= ret;
  3311. *ppos += size;
  3312. info->read += size;
  3313. return size;
  3314. }
  3315. static int tracing_buffers_release(struct inode *inode, struct file *file)
  3316. {
  3317. struct ftrace_buffer_info *info = file->private_data;
  3318. if (info->spare)
  3319. ring_buffer_free_read_page(info->tr->buffer, info->spare);
  3320. kfree(info);
  3321. return 0;
  3322. }
  3323. struct buffer_ref {
  3324. struct ring_buffer *buffer;
  3325. void *page;
  3326. int ref;
  3327. };
  3328. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  3329. struct pipe_buffer *buf)
  3330. {
  3331. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3332. if (--ref->ref)
  3333. return;
  3334. ring_buffer_free_read_page(ref->buffer, ref->page);
  3335. kfree(ref);
  3336. buf->private = 0;
  3337. }
  3338. static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
  3339. struct pipe_buffer *buf)
  3340. {
  3341. return 1;
  3342. }
  3343. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  3344. struct pipe_buffer *buf)
  3345. {
  3346. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3347. ref->ref++;
  3348. }
  3349. /* Pipe buffer operations for a buffer. */
  3350. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  3351. .can_merge = 0,
  3352. .map = generic_pipe_buf_map,
  3353. .unmap = generic_pipe_buf_unmap,
  3354. .confirm = generic_pipe_buf_confirm,
  3355. .release = buffer_pipe_buf_release,
  3356. .steal = buffer_pipe_buf_steal,
  3357. .get = buffer_pipe_buf_get,
  3358. };
  3359. /*
  3360. * Callback from splice_to_pipe(), if we need to release some pages
  3361. * at the end of the spd in case we error'ed out in filling the pipe.
  3362. */
  3363. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  3364. {
  3365. struct buffer_ref *ref =
  3366. (struct buffer_ref *)spd->partial[i].private;
  3367. if (--ref->ref)
  3368. return;
  3369. ring_buffer_free_read_page(ref->buffer, ref->page);
  3370. kfree(ref);
  3371. spd->partial[i].private = 0;
  3372. }
  3373. static ssize_t
  3374. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3375. struct pipe_inode_info *pipe, size_t len,
  3376. unsigned int flags)
  3377. {
  3378. struct ftrace_buffer_info *info = file->private_data;
  3379. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3380. struct page *pages_def[PIPE_DEF_BUFFERS];
  3381. struct splice_pipe_desc spd = {
  3382. .pages = pages_def,
  3383. .partial = partial_def,
  3384. .flags = flags,
  3385. .ops = &buffer_pipe_buf_ops,
  3386. .spd_release = buffer_spd_release,
  3387. };
  3388. struct buffer_ref *ref;
  3389. int entries, size, i;
  3390. size_t ret;
  3391. if (splice_grow_spd(pipe, &spd))
  3392. return -ENOMEM;
  3393. if (*ppos & (PAGE_SIZE - 1)) {
  3394. WARN_ONCE(1, "Ftrace: previous read must page-align\n");
  3395. ret = -EINVAL;
  3396. goto out;
  3397. }
  3398. if (len & (PAGE_SIZE - 1)) {
  3399. WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
  3400. if (len < PAGE_SIZE) {
  3401. ret = -EINVAL;
  3402. goto out;
  3403. }
  3404. len &= PAGE_MASK;
  3405. }
  3406. trace_access_lock(info->cpu);
  3407. entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
  3408. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  3409. struct page *page;
  3410. int r;
  3411. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  3412. if (!ref)
  3413. break;
  3414. ref->ref = 1;
  3415. ref->buffer = info->tr->buffer;
  3416. ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
  3417. if (!ref->page) {
  3418. kfree(ref);
  3419. break;
  3420. }
  3421. r = ring_buffer_read_page(ref->buffer, &ref->page,
  3422. len, info->cpu, 1);
  3423. if (r < 0) {
  3424. ring_buffer_free_read_page(ref->buffer, ref->page);
  3425. kfree(ref);
  3426. break;
  3427. }
  3428. /*
  3429. * zero out any left over data, this is going to
  3430. * user land.
  3431. */
  3432. size = ring_buffer_page_len(ref->page);
  3433. if (size < PAGE_SIZE)
  3434. memset(ref->page + size, 0, PAGE_SIZE - size);
  3435. page = virt_to_page(ref->page);
  3436. spd.pages[i] = page;
  3437. spd.partial[i].len = PAGE_SIZE;
  3438. spd.partial[i].offset = 0;
  3439. spd.partial[i].private = (unsigned long)ref;
  3440. spd.nr_pages++;
  3441. *ppos += PAGE_SIZE;
  3442. entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
  3443. }
  3444. trace_access_unlock(info->cpu);
  3445. spd.nr_pages = i;
  3446. /* did we read anything? */
  3447. if (!spd.nr_pages) {
  3448. if (flags & SPLICE_F_NONBLOCK)
  3449. ret = -EAGAIN;
  3450. else
  3451. ret = 0;
  3452. /* TODO: block */
  3453. goto out;
  3454. }
  3455. ret = splice_to_pipe(pipe, &spd);
  3456. splice_shrink_spd(pipe, &spd);
  3457. out:
  3458. return ret;
  3459. }
  3460. static const struct file_operations tracing_buffers_fops = {
  3461. .open = tracing_buffers_open,
  3462. .read = tracing_buffers_read,
  3463. .release = tracing_buffers_release,
  3464. .splice_read = tracing_buffers_splice_read,
  3465. .llseek = no_llseek,
  3466. };
  3467. static ssize_t
  3468. tracing_stats_read(struct file *filp, char __user *ubuf,
  3469. size_t count, loff_t *ppos)
  3470. {
  3471. unsigned long cpu = (unsigned long)filp->private_data;
  3472. struct trace_array *tr = &global_trace;
  3473. struct trace_seq *s;
  3474. unsigned long cnt;
  3475. unsigned long long t;
  3476. unsigned long usec_rem;
  3477. s = kmalloc(sizeof(*s), GFP_KERNEL);
  3478. if (!s)
  3479. return -ENOMEM;
  3480. trace_seq_init(s);
  3481. cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
  3482. trace_seq_printf(s, "entries: %ld\n", cnt);
  3483. cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
  3484. trace_seq_printf(s, "overrun: %ld\n", cnt);
  3485. cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
  3486. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  3487. cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
  3488. trace_seq_printf(s, "bytes: %ld\n", cnt);
  3489. t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
  3490. usec_rem = do_div(t, USEC_PER_SEC);
  3491. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
  3492. t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
  3493. usec_rem = do_div(t, USEC_PER_SEC);
  3494. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  3495. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  3496. kfree(s);
  3497. return count;
  3498. }
  3499. static const struct file_operations tracing_stats_fops = {
  3500. .open = tracing_open_generic,
  3501. .read = tracing_stats_read,
  3502. .llseek = generic_file_llseek,
  3503. };
  3504. #ifdef CONFIG_DYNAMIC_FTRACE
  3505. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  3506. {
  3507. return 0;
  3508. }
  3509. static ssize_t
  3510. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  3511. size_t cnt, loff_t *ppos)
  3512. {
  3513. static char ftrace_dyn_info_buffer[1024];
  3514. static DEFINE_MUTEX(dyn_info_mutex);
  3515. unsigned long *p = filp->private_data;
  3516. char *buf = ftrace_dyn_info_buffer;
  3517. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  3518. int r;
  3519. mutex_lock(&dyn_info_mutex);
  3520. r = sprintf(buf, "%ld ", *p);
  3521. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  3522. buf[r++] = '\n';
  3523. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3524. mutex_unlock(&dyn_info_mutex);
  3525. return r;
  3526. }
  3527. static const struct file_operations tracing_dyn_info_fops = {
  3528. .open = tracing_open_generic,
  3529. .read = tracing_read_dyn_info,
  3530. .llseek = generic_file_llseek,
  3531. };
  3532. #endif
  3533. static struct dentry *d_tracer;
  3534. struct dentry *tracing_init_dentry(void)
  3535. {
  3536. static int once;
  3537. if (d_tracer)
  3538. return d_tracer;
  3539. if (!debugfs_initialized())
  3540. return NULL;
  3541. d_tracer = debugfs_create_dir("tracing", NULL);
  3542. if (!d_tracer && !once) {
  3543. once = 1;
  3544. pr_warning("Could not create debugfs directory 'tracing'\n");
  3545. return NULL;
  3546. }
  3547. return d_tracer;
  3548. }
  3549. static struct dentry *d_percpu;
  3550. struct dentry *tracing_dentry_percpu(void)
  3551. {
  3552. static int once;
  3553. struct dentry *d_tracer;
  3554. if (d_percpu)
  3555. return d_percpu;
  3556. d_tracer = tracing_init_dentry();
  3557. if (!d_tracer)
  3558. return NULL;
  3559. d_percpu = debugfs_create_dir("per_cpu", d_tracer);
  3560. if (!d_percpu && !once) {
  3561. once = 1;
  3562. pr_warning("Could not create debugfs directory 'per_cpu'\n");
  3563. return NULL;
  3564. }
  3565. return d_percpu;
  3566. }
  3567. static void tracing_init_debugfs_percpu(long cpu)
  3568. {
  3569. struct dentry *d_percpu = tracing_dentry_percpu();
  3570. struct dentry *d_cpu;
  3571. char cpu_dir[30]; /* 30 characters should be more than enough */
  3572. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  3573. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  3574. if (!d_cpu) {
  3575. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  3576. return;
  3577. }
  3578. /* per cpu trace_pipe */
  3579. trace_create_file("trace_pipe", 0444, d_cpu,
  3580. (void *) cpu, &tracing_pipe_fops);
  3581. /* per cpu trace */
  3582. trace_create_file("trace", 0644, d_cpu,
  3583. (void *) cpu, &tracing_fops);
  3584. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  3585. (void *) cpu, &tracing_buffers_fops);
  3586. trace_create_file("stats", 0444, d_cpu,
  3587. (void *) cpu, &tracing_stats_fops);
  3588. }
  3589. #ifdef CONFIG_FTRACE_SELFTEST
  3590. /* Let selftest have access to static functions in this file */
  3591. #include "trace_selftest.c"
  3592. #endif
  3593. struct trace_option_dentry {
  3594. struct tracer_opt *opt;
  3595. struct tracer_flags *flags;
  3596. struct dentry *entry;
  3597. };
  3598. static ssize_t
  3599. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  3600. loff_t *ppos)
  3601. {
  3602. struct trace_option_dentry *topt = filp->private_data;
  3603. char *buf;
  3604. if (topt->flags->val & topt->opt->bit)
  3605. buf = "1\n";
  3606. else
  3607. buf = "0\n";
  3608. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3609. }
  3610. static ssize_t
  3611. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3612. loff_t *ppos)
  3613. {
  3614. struct trace_option_dentry *topt = filp->private_data;
  3615. unsigned long val;
  3616. int ret;
  3617. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3618. if (ret)
  3619. return ret;
  3620. if (val != 0 && val != 1)
  3621. return -EINVAL;
  3622. if (!!(topt->flags->val & topt->opt->bit) != val) {
  3623. mutex_lock(&trace_types_lock);
  3624. ret = __set_tracer_option(current_trace, topt->flags,
  3625. topt->opt, !val);
  3626. mutex_unlock(&trace_types_lock);
  3627. if (ret)
  3628. return ret;
  3629. }
  3630. *ppos += cnt;
  3631. return cnt;
  3632. }
  3633. static const struct file_operations trace_options_fops = {
  3634. .open = tracing_open_generic,
  3635. .read = trace_options_read,
  3636. .write = trace_options_write,
  3637. .llseek = generic_file_llseek,
  3638. };
  3639. static ssize_t
  3640. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  3641. loff_t *ppos)
  3642. {
  3643. long index = (long)filp->private_data;
  3644. char *buf;
  3645. if (trace_flags & (1 << index))
  3646. buf = "1\n";
  3647. else
  3648. buf = "0\n";
  3649. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3650. }
  3651. static ssize_t
  3652. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3653. loff_t *ppos)
  3654. {
  3655. long index = (long)filp->private_data;
  3656. unsigned long val;
  3657. int ret;
  3658. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3659. if (ret)
  3660. return ret;
  3661. if (val != 0 && val != 1)
  3662. return -EINVAL;
  3663. set_tracer_flags(1 << index, val);
  3664. *ppos += cnt;
  3665. return cnt;
  3666. }
  3667. static const struct file_operations trace_options_core_fops = {
  3668. .open = tracing_open_generic,
  3669. .read = trace_options_core_read,
  3670. .write = trace_options_core_write,
  3671. .llseek = generic_file_llseek,
  3672. };
  3673. struct dentry *trace_create_file(const char *name,
  3674. umode_t mode,
  3675. struct dentry *parent,
  3676. void *data,
  3677. const struct file_operations *fops)
  3678. {
  3679. struct dentry *ret;
  3680. ret = debugfs_create_file(name, mode, parent, data, fops);
  3681. if (!ret)
  3682. pr_warning("Could not create debugfs '%s' entry\n", name);
  3683. return ret;
  3684. }
  3685. static struct dentry *trace_options_init_dentry(void)
  3686. {
  3687. struct dentry *d_tracer;
  3688. static struct dentry *t_options;
  3689. if (t_options)
  3690. return t_options;
  3691. d_tracer = tracing_init_dentry();
  3692. if (!d_tracer)
  3693. return NULL;
  3694. t_options = debugfs_create_dir("options", d_tracer);
  3695. if (!t_options) {
  3696. pr_warning("Could not create debugfs directory 'options'\n");
  3697. return NULL;
  3698. }
  3699. return t_options;
  3700. }
  3701. static void
  3702. create_trace_option_file(struct trace_option_dentry *topt,
  3703. struct tracer_flags *flags,
  3704. struct tracer_opt *opt)
  3705. {
  3706. struct dentry *t_options;
  3707. t_options = trace_options_init_dentry();
  3708. if (!t_options)
  3709. return;
  3710. topt->flags = flags;
  3711. topt->opt = opt;
  3712. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  3713. &trace_options_fops);
  3714. }
  3715. static struct trace_option_dentry *
  3716. create_trace_option_files(struct tracer *tracer)
  3717. {
  3718. struct trace_option_dentry *topts;
  3719. struct tracer_flags *flags;
  3720. struct tracer_opt *opts;
  3721. int cnt;
  3722. if (!tracer)
  3723. return NULL;
  3724. flags = tracer->flags;
  3725. if (!flags || !flags->opts)
  3726. return NULL;
  3727. opts = flags->opts;
  3728. for (cnt = 0; opts[cnt].name; cnt++)
  3729. ;
  3730. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  3731. if (!topts)
  3732. return NULL;
  3733. for (cnt = 0; opts[cnt].name; cnt++)
  3734. create_trace_option_file(&topts[cnt], flags,
  3735. &opts[cnt]);
  3736. return topts;
  3737. }
  3738. static void
  3739. destroy_trace_option_files(struct trace_option_dentry *topts)
  3740. {
  3741. int cnt;
  3742. if (!topts)
  3743. return;
  3744. for (cnt = 0; topts[cnt].opt; cnt++) {
  3745. if (topts[cnt].entry)
  3746. debugfs_remove(topts[cnt].entry);
  3747. }
  3748. kfree(topts);
  3749. }
  3750. static struct dentry *
  3751. create_trace_option_core_file(const char *option, long index)
  3752. {
  3753. struct dentry *t_options;
  3754. t_options = trace_options_init_dentry();
  3755. if (!t_options)
  3756. return NULL;
  3757. return trace_create_file(option, 0644, t_options, (void *)index,
  3758. &trace_options_core_fops);
  3759. }
  3760. static __init void create_trace_options_dir(void)
  3761. {
  3762. struct dentry *t_options;
  3763. int i;
  3764. t_options = trace_options_init_dentry();
  3765. if (!t_options)
  3766. return;
  3767. for (i = 0; trace_options[i]; i++)
  3768. create_trace_option_core_file(trace_options[i], i);
  3769. }
  3770. static ssize_t
  3771. rb_simple_read(struct file *filp, char __user *ubuf,
  3772. size_t cnt, loff_t *ppos)
  3773. {
  3774. struct trace_array *tr = filp->private_data;
  3775. struct ring_buffer *buffer = tr->buffer;
  3776. char buf[64];
  3777. int r;
  3778. if (buffer)
  3779. r = ring_buffer_record_is_on(buffer);
  3780. else
  3781. r = 0;
  3782. r = sprintf(buf, "%d\n", r);
  3783. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3784. }
  3785. static ssize_t
  3786. rb_simple_write(struct file *filp, const char __user *ubuf,
  3787. size_t cnt, loff_t *ppos)
  3788. {
  3789. struct trace_array *tr = filp->private_data;
  3790. struct ring_buffer *buffer = tr->buffer;
  3791. unsigned long val;
  3792. int ret;
  3793. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3794. if (ret)
  3795. return ret;
  3796. if (buffer) {
  3797. if (val)
  3798. ring_buffer_record_on(buffer);
  3799. else
  3800. ring_buffer_record_off(buffer);
  3801. }
  3802. (*ppos)++;
  3803. return cnt;
  3804. }
  3805. static const struct file_operations rb_simple_fops = {
  3806. .open = tracing_open_generic,
  3807. .read = rb_simple_read,
  3808. .write = rb_simple_write,
  3809. .llseek = default_llseek,
  3810. };
  3811. static __init int tracer_init_debugfs(void)
  3812. {
  3813. struct dentry *d_tracer;
  3814. int cpu;
  3815. trace_access_lock_init();
  3816. d_tracer = tracing_init_dentry();
  3817. trace_create_file("tracing_enabled", 0644, d_tracer,
  3818. &global_trace, &tracing_ctrl_fops);
  3819. trace_create_file("trace_options", 0644, d_tracer,
  3820. NULL, &tracing_iter_fops);
  3821. trace_create_file("tracing_cpumask", 0644, d_tracer,
  3822. NULL, &tracing_cpumask_fops);
  3823. trace_create_file("trace", 0644, d_tracer,
  3824. (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
  3825. trace_create_file("available_tracers", 0444, d_tracer,
  3826. &global_trace, &show_traces_fops);
  3827. trace_create_file("current_tracer", 0644, d_tracer,
  3828. &global_trace, &set_tracer_fops);
  3829. #ifdef CONFIG_TRACER_MAX_TRACE
  3830. trace_create_file("tracing_max_latency", 0644, d_tracer,
  3831. &tracing_max_latency, &tracing_max_lat_fops);
  3832. #endif
  3833. trace_create_file("tracing_thresh", 0644, d_tracer,
  3834. &tracing_thresh, &tracing_max_lat_fops);
  3835. trace_create_file("README", 0444, d_tracer,
  3836. NULL, &tracing_readme_fops);
  3837. trace_create_file("trace_pipe", 0444, d_tracer,
  3838. (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
  3839. trace_create_file("buffer_size_kb", 0644, d_tracer,
  3840. &global_trace, &tracing_entries_fops);
  3841. trace_create_file("buffer_total_size_kb", 0444, d_tracer,
  3842. &global_trace, &tracing_total_entries_fops);
  3843. trace_create_file("free_buffer", 0644, d_tracer,
  3844. &global_trace, &tracing_free_buffer_fops);
  3845. trace_create_file("trace_marker", 0220, d_tracer,
  3846. NULL, &tracing_mark_fops);
  3847. trace_create_file("saved_cmdlines", 0444, d_tracer,
  3848. NULL, &tracing_saved_cmdlines_fops);
  3849. trace_create_file("trace_clock", 0644, d_tracer, NULL,
  3850. &trace_clock_fops);
  3851. trace_create_file("tracing_on", 0644, d_tracer,
  3852. &global_trace, &rb_simple_fops);
  3853. #ifdef CONFIG_DYNAMIC_FTRACE
  3854. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  3855. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  3856. #endif
  3857. create_trace_options_dir();
  3858. for_each_tracing_cpu(cpu)
  3859. tracing_init_debugfs_percpu(cpu);
  3860. return 0;
  3861. }
  3862. static int trace_panic_handler(struct notifier_block *this,
  3863. unsigned long event, void *unused)
  3864. {
  3865. if (ftrace_dump_on_oops)
  3866. ftrace_dump(ftrace_dump_on_oops);
  3867. return NOTIFY_OK;
  3868. }
  3869. static struct notifier_block trace_panic_notifier = {
  3870. .notifier_call = trace_panic_handler,
  3871. .next = NULL,
  3872. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  3873. };
  3874. static int trace_die_handler(struct notifier_block *self,
  3875. unsigned long val,
  3876. void *data)
  3877. {
  3878. switch (val) {
  3879. case DIE_OOPS:
  3880. if (ftrace_dump_on_oops)
  3881. ftrace_dump(ftrace_dump_on_oops);
  3882. break;
  3883. default:
  3884. break;
  3885. }
  3886. return NOTIFY_OK;
  3887. }
  3888. static struct notifier_block trace_die_notifier = {
  3889. .notifier_call = trace_die_handler,
  3890. .priority = 200
  3891. };
  3892. /*
  3893. * printk is set to max of 1024, we really don't need it that big.
  3894. * Nothing should be printing 1000 characters anyway.
  3895. */
  3896. #define TRACE_MAX_PRINT 1000
  3897. /*
  3898. * Define here KERN_TRACE so that we have one place to modify
  3899. * it if we decide to change what log level the ftrace dump
  3900. * should be at.
  3901. */
  3902. #define KERN_TRACE KERN_EMERG
  3903. void
  3904. trace_printk_seq(struct trace_seq *s)
  3905. {
  3906. /* Probably should print a warning here. */
  3907. if (s->len >= 1000)
  3908. s->len = 1000;
  3909. /* should be zero ended, but we are paranoid. */
  3910. s->buffer[s->len] = 0;
  3911. printk(KERN_TRACE "%s", s->buffer);
  3912. trace_seq_init(s);
  3913. }
  3914. void trace_init_global_iter(struct trace_iterator *iter)
  3915. {
  3916. iter->tr = &global_trace;
  3917. iter->trace = current_trace;
  3918. iter->cpu_file = TRACE_PIPE_ALL_CPU;
  3919. }
  3920. static void
  3921. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
  3922. {
  3923. static arch_spinlock_t ftrace_dump_lock =
  3924. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  3925. /* use static because iter can be a bit big for the stack */
  3926. static struct trace_iterator iter;
  3927. unsigned int old_userobj;
  3928. static int dump_ran;
  3929. unsigned long flags;
  3930. int cnt = 0, cpu;
  3931. /* only one dump */
  3932. local_irq_save(flags);
  3933. arch_spin_lock(&ftrace_dump_lock);
  3934. if (dump_ran)
  3935. goto out;
  3936. dump_ran = 1;
  3937. tracing_off();
  3938. /* Did function tracer already get disabled? */
  3939. if (ftrace_is_dead()) {
  3940. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  3941. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  3942. }
  3943. if (disable_tracing)
  3944. ftrace_kill();
  3945. trace_init_global_iter(&iter);
  3946. for_each_tracing_cpu(cpu) {
  3947. atomic_inc(&iter.tr->data[cpu]->disabled);
  3948. }
  3949. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  3950. /* don't look at user memory in panic mode */
  3951. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  3952. /* Simulate the iterator */
  3953. iter.tr = &global_trace;
  3954. iter.trace = current_trace;
  3955. switch (oops_dump_mode) {
  3956. case DUMP_ALL:
  3957. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3958. break;
  3959. case DUMP_ORIG:
  3960. iter.cpu_file = raw_smp_processor_id();
  3961. break;
  3962. case DUMP_NONE:
  3963. goto out_enable;
  3964. default:
  3965. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  3966. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3967. }
  3968. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  3969. /*
  3970. * We need to stop all tracing on all CPUS to read the
  3971. * the next buffer. This is a bit expensive, but is
  3972. * not done often. We fill all what we can read,
  3973. * and then release the locks again.
  3974. */
  3975. while (!trace_empty(&iter)) {
  3976. if (!cnt)
  3977. printk(KERN_TRACE "---------------------------------\n");
  3978. cnt++;
  3979. /* reset all but tr, trace, and overruns */
  3980. memset(&iter.seq, 0,
  3981. sizeof(struct trace_iterator) -
  3982. offsetof(struct trace_iterator, seq));
  3983. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3984. iter.pos = -1;
  3985. if (trace_find_next_entry_inc(&iter) != NULL) {
  3986. int ret;
  3987. ret = print_trace_line(&iter);
  3988. if (ret != TRACE_TYPE_NO_CONSUME)
  3989. trace_consume(&iter);
  3990. }
  3991. touch_nmi_watchdog();
  3992. trace_printk_seq(&iter.seq);
  3993. }
  3994. if (!cnt)
  3995. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3996. else
  3997. printk(KERN_TRACE "---------------------------------\n");
  3998. out_enable:
  3999. /* Re-enable tracing if requested */
  4000. if (!disable_tracing) {
  4001. trace_flags |= old_userobj;
  4002. for_each_tracing_cpu(cpu) {
  4003. atomic_dec(&iter.tr->data[cpu]->disabled);
  4004. }
  4005. tracing_on();
  4006. }
  4007. out:
  4008. arch_spin_unlock(&ftrace_dump_lock);
  4009. local_irq_restore(flags);
  4010. }
  4011. /* By default: disable tracing after the dump */
  4012. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  4013. {
  4014. __ftrace_dump(true, oops_dump_mode);
  4015. }
  4016. EXPORT_SYMBOL_GPL(ftrace_dump);
  4017. __init static int tracer_alloc_buffers(void)
  4018. {
  4019. int ring_buf_size;
  4020. enum ring_buffer_flags rb_flags;
  4021. int i;
  4022. int ret = -ENOMEM;
  4023. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  4024. goto out;
  4025. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  4026. goto out_free_buffer_mask;
  4027. /* To save memory, keep the ring buffer size to its minimum */
  4028. if (ring_buffer_expanded)
  4029. ring_buf_size = trace_buf_size;
  4030. else
  4031. ring_buf_size = 1;
  4032. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  4033. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  4034. cpumask_copy(tracing_cpumask, cpu_all_mask);
  4035. /* TODO: make the number of buffers hot pluggable with CPUS */
  4036. global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
  4037. if (!global_trace.buffer) {
  4038. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  4039. WARN_ON(1);
  4040. goto out_free_cpumask;
  4041. }
  4042. global_trace.entries = ring_buffer_size(global_trace.buffer);
  4043. if (global_trace.buffer_disabled)
  4044. tracing_off();
  4045. #ifdef CONFIG_TRACER_MAX_TRACE
  4046. max_tr.buffer = ring_buffer_alloc(1, rb_flags);
  4047. if (!max_tr.buffer) {
  4048. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  4049. WARN_ON(1);
  4050. ring_buffer_free(global_trace.buffer);
  4051. goto out_free_cpumask;
  4052. }
  4053. max_tr.entries = 1;
  4054. #endif
  4055. /* Allocate the first page for all buffers */
  4056. for_each_tracing_cpu(i) {
  4057. global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  4058. max_tr.data[i] = &per_cpu(max_tr_data, i);
  4059. }
  4060. trace_init_cmdlines();
  4061. register_tracer(&nop_trace);
  4062. current_trace = &nop_trace;
  4063. /* All seems OK, enable tracing */
  4064. tracing_disabled = 0;
  4065. atomic_notifier_chain_register(&panic_notifier_list,
  4066. &trace_panic_notifier);
  4067. register_die_notifier(&trace_die_notifier);
  4068. return 0;
  4069. out_free_cpumask:
  4070. free_cpumask_var(tracing_cpumask);
  4071. out_free_buffer_mask:
  4072. free_cpumask_var(tracing_buffer_mask);
  4073. out:
  4074. return ret;
  4075. }
  4076. __init static int clear_boot_tracer(void)
  4077. {
  4078. /*
  4079. * The default tracer at boot buffer is an init section.
  4080. * This function is called in lateinit. If we did not
  4081. * find the boot tracer, then clear it out, to prevent
  4082. * later registration from accessing the buffer that is
  4083. * about to be freed.
  4084. */
  4085. if (!default_bootup_tracer)
  4086. return 0;
  4087. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  4088. default_bootup_tracer);
  4089. default_bootup_tracer = NULL;
  4090. return 0;
  4091. }
  4092. early_initcall(tracer_alloc_buffers);
  4093. fs_initcall(tracer_init_debugfs);
  4094. late_initcall(clear_boot_tracer);