trace.c 71 KB

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