trace.c 95 KB

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