trace.c 95 KB

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