trace.c 76 KB

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