trace.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126
  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 && event->latency_trace)
  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 && event->trace)
  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 && event->raw)
  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 && event->hex) {
  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. if (event && event->binary)
  1278. return event->binary(iter, 0);
  1279. return TRACE_TYPE_HANDLED;
  1280. }
  1281. static int trace_empty(struct trace_iterator *iter)
  1282. {
  1283. int cpu;
  1284. for_each_tracing_cpu(cpu) {
  1285. if (iter->buffer_iter[cpu]) {
  1286. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1287. return 0;
  1288. } else {
  1289. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1290. return 0;
  1291. }
  1292. }
  1293. return 1;
  1294. }
  1295. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1296. {
  1297. enum print_line_t ret;
  1298. if (iter->trace && iter->trace->print_line) {
  1299. ret = iter->trace->print_line(iter);
  1300. if (ret != TRACE_TYPE_UNHANDLED)
  1301. return ret;
  1302. }
  1303. if (iter->ent->type == TRACE_PRINT &&
  1304. trace_flags & TRACE_ITER_PRINTK &&
  1305. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1306. return print_printk_msg_only(iter);
  1307. if (trace_flags & TRACE_ITER_BIN)
  1308. return print_bin_fmt(iter);
  1309. if (trace_flags & TRACE_ITER_HEX)
  1310. return print_hex_fmt(iter);
  1311. if (trace_flags & TRACE_ITER_RAW)
  1312. return print_raw_fmt(iter);
  1313. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1314. return print_lat_fmt(iter);
  1315. return print_trace_fmt(iter);
  1316. }
  1317. static int s_show(struct seq_file *m, void *v)
  1318. {
  1319. struct trace_iterator *iter = v;
  1320. if (iter->ent == NULL) {
  1321. if (iter->tr) {
  1322. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1323. seq_puts(m, "#\n");
  1324. }
  1325. if (iter->trace && iter->trace->print_header)
  1326. iter->trace->print_header(m);
  1327. else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1328. /* print nothing if the buffers are empty */
  1329. if (trace_empty(iter))
  1330. return 0;
  1331. print_trace_header(m, iter);
  1332. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1333. print_lat_help_header(m);
  1334. } else {
  1335. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1336. print_func_help_header(m);
  1337. }
  1338. } else {
  1339. print_trace_line(iter);
  1340. trace_print_seq(m, &iter->seq);
  1341. }
  1342. return 0;
  1343. }
  1344. static struct seq_operations tracer_seq_ops = {
  1345. .start = s_start,
  1346. .next = s_next,
  1347. .stop = s_stop,
  1348. .show = s_show,
  1349. };
  1350. static struct trace_iterator *
  1351. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1352. {
  1353. struct trace_iterator *iter;
  1354. struct seq_file *m;
  1355. int cpu;
  1356. if (tracing_disabled) {
  1357. *ret = -ENODEV;
  1358. return NULL;
  1359. }
  1360. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1361. if (!iter) {
  1362. *ret = -ENOMEM;
  1363. goto out;
  1364. }
  1365. mutex_lock(&trace_types_lock);
  1366. if (current_trace && current_trace->print_max)
  1367. iter->tr = &max_tr;
  1368. else
  1369. iter->tr = inode->i_private;
  1370. iter->trace = current_trace;
  1371. iter->pos = -1;
  1372. /* Notify the tracer early; before we stop tracing. */
  1373. if (iter->trace && iter->trace->open)
  1374. iter->trace->open(iter);
  1375. /* Annotate start of buffers if we had overruns */
  1376. if (ring_buffer_overruns(iter->tr->buffer))
  1377. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1378. for_each_tracing_cpu(cpu) {
  1379. iter->buffer_iter[cpu] =
  1380. ring_buffer_read_start(iter->tr->buffer, cpu);
  1381. if (!iter->buffer_iter[cpu])
  1382. goto fail_buffer;
  1383. }
  1384. /* TODO stop tracer */
  1385. *ret = seq_open(file, &tracer_seq_ops);
  1386. if (*ret)
  1387. goto fail_buffer;
  1388. m = file->private_data;
  1389. m->private = iter;
  1390. /* stop the trace while dumping */
  1391. tracing_stop();
  1392. mutex_unlock(&trace_types_lock);
  1393. out:
  1394. return iter;
  1395. fail_buffer:
  1396. for_each_tracing_cpu(cpu) {
  1397. if (iter->buffer_iter[cpu])
  1398. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1399. }
  1400. mutex_unlock(&trace_types_lock);
  1401. kfree(iter);
  1402. return ERR_PTR(-ENOMEM);
  1403. }
  1404. int tracing_open_generic(struct inode *inode, struct file *filp)
  1405. {
  1406. if (tracing_disabled)
  1407. return -ENODEV;
  1408. filp->private_data = inode->i_private;
  1409. return 0;
  1410. }
  1411. int tracing_release(struct inode *inode, struct file *file)
  1412. {
  1413. struct seq_file *m = (struct seq_file *)file->private_data;
  1414. struct trace_iterator *iter = m->private;
  1415. int cpu;
  1416. mutex_lock(&trace_types_lock);
  1417. for_each_tracing_cpu(cpu) {
  1418. if (iter->buffer_iter[cpu])
  1419. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1420. }
  1421. if (iter->trace && iter->trace->close)
  1422. iter->trace->close(iter);
  1423. /* reenable tracing if it was previously enabled */
  1424. tracing_start();
  1425. mutex_unlock(&trace_types_lock);
  1426. seq_release(inode, file);
  1427. kfree(iter);
  1428. return 0;
  1429. }
  1430. static int tracing_open(struct inode *inode, struct file *file)
  1431. {
  1432. int ret;
  1433. __tracing_open(inode, file, &ret);
  1434. return ret;
  1435. }
  1436. static int tracing_lt_open(struct inode *inode, struct file *file)
  1437. {
  1438. struct trace_iterator *iter;
  1439. int ret;
  1440. iter = __tracing_open(inode, file, &ret);
  1441. if (!ret)
  1442. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1443. return ret;
  1444. }
  1445. static void *
  1446. t_next(struct seq_file *m, void *v, loff_t *pos)
  1447. {
  1448. struct tracer *t = m->private;
  1449. (*pos)++;
  1450. if (t)
  1451. t = t->next;
  1452. m->private = t;
  1453. return t;
  1454. }
  1455. static void *t_start(struct seq_file *m, loff_t *pos)
  1456. {
  1457. struct tracer *t = m->private;
  1458. loff_t l = 0;
  1459. mutex_lock(&trace_types_lock);
  1460. for (; t && l < *pos; t = t_next(m, t, &l))
  1461. ;
  1462. return t;
  1463. }
  1464. static void t_stop(struct seq_file *m, void *p)
  1465. {
  1466. mutex_unlock(&trace_types_lock);
  1467. }
  1468. static int t_show(struct seq_file *m, void *v)
  1469. {
  1470. struct tracer *t = v;
  1471. if (!t)
  1472. return 0;
  1473. seq_printf(m, "%s", t->name);
  1474. if (t->next)
  1475. seq_putc(m, ' ');
  1476. else
  1477. seq_putc(m, '\n');
  1478. return 0;
  1479. }
  1480. static struct seq_operations show_traces_seq_ops = {
  1481. .start = t_start,
  1482. .next = t_next,
  1483. .stop = t_stop,
  1484. .show = t_show,
  1485. };
  1486. static int show_traces_open(struct inode *inode, struct file *file)
  1487. {
  1488. int ret;
  1489. if (tracing_disabled)
  1490. return -ENODEV;
  1491. ret = seq_open(file, &show_traces_seq_ops);
  1492. if (!ret) {
  1493. struct seq_file *m = file->private_data;
  1494. m->private = trace_types;
  1495. }
  1496. return ret;
  1497. }
  1498. static struct file_operations tracing_fops = {
  1499. .open = tracing_open,
  1500. .read = seq_read,
  1501. .llseek = seq_lseek,
  1502. .release = tracing_release,
  1503. };
  1504. static struct file_operations tracing_lt_fops = {
  1505. .open = tracing_lt_open,
  1506. .read = seq_read,
  1507. .llseek = seq_lseek,
  1508. .release = tracing_release,
  1509. };
  1510. static struct file_operations show_traces_fops = {
  1511. .open = show_traces_open,
  1512. .read = seq_read,
  1513. .release = seq_release,
  1514. };
  1515. /*
  1516. * Only trace on a CPU if the bitmask is set:
  1517. */
  1518. static cpumask_var_t tracing_cpumask;
  1519. /*
  1520. * The tracer itself will not take this lock, but still we want
  1521. * to provide a consistent cpumask to user-space:
  1522. */
  1523. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1524. /*
  1525. * Temporary storage for the character representation of the
  1526. * CPU bitmask (and one more byte for the newline):
  1527. */
  1528. static char mask_str[NR_CPUS + 1];
  1529. static ssize_t
  1530. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1531. size_t count, loff_t *ppos)
  1532. {
  1533. int len;
  1534. mutex_lock(&tracing_cpumask_update_lock);
  1535. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1536. if (count - len < 2) {
  1537. count = -EINVAL;
  1538. goto out_err;
  1539. }
  1540. len += sprintf(mask_str + len, "\n");
  1541. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1542. out_err:
  1543. mutex_unlock(&tracing_cpumask_update_lock);
  1544. return count;
  1545. }
  1546. static ssize_t
  1547. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1548. size_t count, loff_t *ppos)
  1549. {
  1550. int err, cpu;
  1551. cpumask_var_t tracing_cpumask_new;
  1552. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  1553. return -ENOMEM;
  1554. mutex_lock(&tracing_cpumask_update_lock);
  1555. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1556. if (err)
  1557. goto err_unlock;
  1558. local_irq_disable();
  1559. __raw_spin_lock(&ftrace_max_lock);
  1560. for_each_tracing_cpu(cpu) {
  1561. /*
  1562. * Increase/decrease the disabled counter if we are
  1563. * about to flip a bit in the cpumask:
  1564. */
  1565. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  1566. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1567. atomic_inc(&global_trace.data[cpu]->disabled);
  1568. }
  1569. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  1570. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1571. atomic_dec(&global_trace.data[cpu]->disabled);
  1572. }
  1573. }
  1574. __raw_spin_unlock(&ftrace_max_lock);
  1575. local_irq_enable();
  1576. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  1577. mutex_unlock(&tracing_cpumask_update_lock);
  1578. free_cpumask_var(tracing_cpumask_new);
  1579. return count;
  1580. err_unlock:
  1581. mutex_unlock(&tracing_cpumask_update_lock);
  1582. free_cpumask_var(tracing_cpumask);
  1583. return err;
  1584. }
  1585. static struct file_operations tracing_cpumask_fops = {
  1586. .open = tracing_open_generic,
  1587. .read = tracing_cpumask_read,
  1588. .write = tracing_cpumask_write,
  1589. };
  1590. static ssize_t
  1591. tracing_trace_options_read(struct file *filp, char __user *ubuf,
  1592. size_t cnt, loff_t *ppos)
  1593. {
  1594. int i;
  1595. char *buf;
  1596. int r = 0;
  1597. int len = 0;
  1598. u32 tracer_flags = current_trace->flags->val;
  1599. struct tracer_opt *trace_opts = current_trace->flags->opts;
  1600. /* calulate max size */
  1601. for (i = 0; trace_options[i]; i++) {
  1602. len += strlen(trace_options[i]);
  1603. len += 3; /* "no" and space */
  1604. }
  1605. /*
  1606. * Increase the size with names of options specific
  1607. * of the current tracer.
  1608. */
  1609. for (i = 0; trace_opts[i].name; i++) {
  1610. len += strlen(trace_opts[i].name);
  1611. len += 3; /* "no" and space */
  1612. }
  1613. /* +2 for \n and \0 */
  1614. buf = kmalloc(len + 2, GFP_KERNEL);
  1615. if (!buf)
  1616. return -ENOMEM;
  1617. for (i = 0; trace_options[i]; i++) {
  1618. if (trace_flags & (1 << i))
  1619. r += sprintf(buf + r, "%s ", trace_options[i]);
  1620. else
  1621. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1622. }
  1623. for (i = 0; trace_opts[i].name; i++) {
  1624. if (tracer_flags & trace_opts[i].bit)
  1625. r += sprintf(buf + r, "%s ",
  1626. trace_opts[i].name);
  1627. else
  1628. r += sprintf(buf + r, "no%s ",
  1629. trace_opts[i].name);
  1630. }
  1631. r += sprintf(buf + r, "\n");
  1632. WARN_ON(r >= len + 2);
  1633. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1634. kfree(buf);
  1635. return r;
  1636. }
  1637. /* Try to assign a tracer specific option */
  1638. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  1639. {
  1640. struct tracer_flags *trace_flags = trace->flags;
  1641. struct tracer_opt *opts = NULL;
  1642. int ret = 0, i = 0;
  1643. int len;
  1644. for (i = 0; trace_flags->opts[i].name; i++) {
  1645. opts = &trace_flags->opts[i];
  1646. len = strlen(opts->name);
  1647. if (strncmp(cmp, opts->name, len) == 0) {
  1648. ret = trace->set_flag(trace_flags->val,
  1649. opts->bit, !neg);
  1650. break;
  1651. }
  1652. }
  1653. /* Not found */
  1654. if (!trace_flags->opts[i].name)
  1655. return -EINVAL;
  1656. /* Refused to handle */
  1657. if (ret)
  1658. return ret;
  1659. if (neg)
  1660. trace_flags->val &= ~opts->bit;
  1661. else
  1662. trace_flags->val |= opts->bit;
  1663. return 0;
  1664. }
  1665. static ssize_t
  1666. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  1667. size_t cnt, loff_t *ppos)
  1668. {
  1669. char buf[64];
  1670. char *cmp = buf;
  1671. int neg = 0;
  1672. int ret;
  1673. int i;
  1674. if (cnt >= sizeof(buf))
  1675. return -EINVAL;
  1676. if (copy_from_user(&buf, ubuf, cnt))
  1677. return -EFAULT;
  1678. buf[cnt] = 0;
  1679. if (strncmp(buf, "no", 2) == 0) {
  1680. neg = 1;
  1681. cmp += 2;
  1682. }
  1683. for (i = 0; trace_options[i]; i++) {
  1684. int len = strlen(trace_options[i]);
  1685. if (strncmp(cmp, trace_options[i], len) == 0) {
  1686. if (neg)
  1687. trace_flags &= ~(1 << i);
  1688. else
  1689. trace_flags |= (1 << i);
  1690. break;
  1691. }
  1692. }
  1693. /* If no option could be set, test the specific tracer options */
  1694. if (!trace_options[i]) {
  1695. ret = set_tracer_option(current_trace, cmp, neg);
  1696. if (ret)
  1697. return ret;
  1698. }
  1699. filp->f_pos += cnt;
  1700. return cnt;
  1701. }
  1702. static struct file_operations tracing_iter_fops = {
  1703. .open = tracing_open_generic,
  1704. .read = tracing_trace_options_read,
  1705. .write = tracing_trace_options_write,
  1706. };
  1707. static const char readme_msg[] =
  1708. "tracing mini-HOWTO:\n\n"
  1709. "# mkdir /debug\n"
  1710. "# mount -t debugfs nodev /debug\n\n"
  1711. "# cat /debug/tracing/available_tracers\n"
  1712. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1713. "# cat /debug/tracing/current_tracer\n"
  1714. "none\n"
  1715. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1716. "# cat /debug/tracing/current_tracer\n"
  1717. "sched_switch\n"
  1718. "# cat /debug/tracing/trace_options\n"
  1719. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1720. "# echo print-parent > /debug/tracing/trace_options\n"
  1721. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1722. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1723. "echo 0 > /debug/tracing/tracing_enabled\n"
  1724. ;
  1725. static ssize_t
  1726. tracing_readme_read(struct file *filp, char __user *ubuf,
  1727. size_t cnt, loff_t *ppos)
  1728. {
  1729. return simple_read_from_buffer(ubuf, cnt, ppos,
  1730. readme_msg, strlen(readme_msg));
  1731. }
  1732. static struct file_operations tracing_readme_fops = {
  1733. .open = tracing_open_generic,
  1734. .read = tracing_readme_read,
  1735. };
  1736. static ssize_t
  1737. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1738. size_t cnt, loff_t *ppos)
  1739. {
  1740. char buf[64];
  1741. int r;
  1742. r = sprintf(buf, "%u\n", tracer_enabled);
  1743. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1744. }
  1745. static ssize_t
  1746. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1747. size_t cnt, loff_t *ppos)
  1748. {
  1749. struct trace_array *tr = filp->private_data;
  1750. char buf[64];
  1751. long val;
  1752. int ret;
  1753. if (cnt >= sizeof(buf))
  1754. return -EINVAL;
  1755. if (copy_from_user(&buf, ubuf, cnt))
  1756. return -EFAULT;
  1757. buf[cnt] = 0;
  1758. ret = strict_strtoul(buf, 10, &val);
  1759. if (ret < 0)
  1760. return ret;
  1761. val = !!val;
  1762. mutex_lock(&trace_types_lock);
  1763. if (tracer_enabled ^ val) {
  1764. if (val) {
  1765. tracer_enabled = 1;
  1766. if (current_trace->start)
  1767. current_trace->start(tr);
  1768. tracing_start();
  1769. } else {
  1770. tracer_enabled = 0;
  1771. tracing_stop();
  1772. if (current_trace->stop)
  1773. current_trace->stop(tr);
  1774. }
  1775. }
  1776. mutex_unlock(&trace_types_lock);
  1777. filp->f_pos += cnt;
  1778. return cnt;
  1779. }
  1780. static ssize_t
  1781. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1782. size_t cnt, loff_t *ppos)
  1783. {
  1784. char buf[max_tracer_type_len+2];
  1785. int r;
  1786. mutex_lock(&trace_types_lock);
  1787. if (current_trace)
  1788. r = sprintf(buf, "%s\n", current_trace->name);
  1789. else
  1790. r = sprintf(buf, "\n");
  1791. mutex_unlock(&trace_types_lock);
  1792. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1793. }
  1794. static int tracing_set_tracer(const char *buf)
  1795. {
  1796. struct trace_array *tr = &global_trace;
  1797. struct tracer *t;
  1798. int ret = 0;
  1799. mutex_lock(&trace_types_lock);
  1800. for (t = trace_types; t; t = t->next) {
  1801. if (strcmp(t->name, buf) == 0)
  1802. break;
  1803. }
  1804. if (!t) {
  1805. ret = -EINVAL;
  1806. goto out;
  1807. }
  1808. if (t == current_trace)
  1809. goto out;
  1810. trace_branch_disable();
  1811. if (current_trace && current_trace->reset)
  1812. current_trace->reset(tr);
  1813. current_trace = t;
  1814. if (t->init) {
  1815. ret = t->init(tr);
  1816. if (ret)
  1817. goto out;
  1818. }
  1819. trace_branch_enable(tr);
  1820. out:
  1821. mutex_unlock(&trace_types_lock);
  1822. return ret;
  1823. }
  1824. static ssize_t
  1825. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1826. size_t cnt, loff_t *ppos)
  1827. {
  1828. char buf[max_tracer_type_len+1];
  1829. int i;
  1830. size_t ret;
  1831. int err;
  1832. ret = cnt;
  1833. if (cnt > max_tracer_type_len)
  1834. cnt = max_tracer_type_len;
  1835. if (copy_from_user(&buf, ubuf, cnt))
  1836. return -EFAULT;
  1837. buf[cnt] = 0;
  1838. /* strip ending whitespace. */
  1839. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1840. buf[i] = 0;
  1841. err = tracing_set_tracer(buf);
  1842. if (err)
  1843. return err;
  1844. filp->f_pos += ret;
  1845. return ret;
  1846. }
  1847. static ssize_t
  1848. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1849. size_t cnt, loff_t *ppos)
  1850. {
  1851. unsigned long *ptr = filp->private_data;
  1852. char buf[64];
  1853. int r;
  1854. r = snprintf(buf, sizeof(buf), "%ld\n",
  1855. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1856. if (r > sizeof(buf))
  1857. r = sizeof(buf);
  1858. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1859. }
  1860. static ssize_t
  1861. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1862. size_t cnt, loff_t *ppos)
  1863. {
  1864. long *ptr = filp->private_data;
  1865. char buf[64];
  1866. long val;
  1867. int ret;
  1868. if (cnt >= sizeof(buf))
  1869. return -EINVAL;
  1870. if (copy_from_user(&buf, ubuf, cnt))
  1871. return -EFAULT;
  1872. buf[cnt] = 0;
  1873. ret = strict_strtoul(buf, 10, &val);
  1874. if (ret < 0)
  1875. return ret;
  1876. *ptr = val * 1000;
  1877. return cnt;
  1878. }
  1879. static atomic_t tracing_reader;
  1880. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1881. {
  1882. struct trace_iterator *iter;
  1883. if (tracing_disabled)
  1884. return -ENODEV;
  1885. /* We only allow for reader of the pipe */
  1886. if (atomic_inc_return(&tracing_reader) != 1) {
  1887. atomic_dec(&tracing_reader);
  1888. return -EBUSY;
  1889. }
  1890. /* create a buffer to store the information to pass to userspace */
  1891. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1892. if (!iter)
  1893. return -ENOMEM;
  1894. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  1895. kfree(iter);
  1896. return -ENOMEM;
  1897. }
  1898. mutex_lock(&trace_types_lock);
  1899. /* trace pipe does not show start of buffer */
  1900. cpumask_setall(iter->started);
  1901. iter->tr = &global_trace;
  1902. iter->trace = current_trace;
  1903. filp->private_data = iter;
  1904. if (iter->trace->pipe_open)
  1905. iter->trace->pipe_open(iter);
  1906. mutex_unlock(&trace_types_lock);
  1907. return 0;
  1908. }
  1909. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1910. {
  1911. struct trace_iterator *iter = file->private_data;
  1912. free_cpumask_var(iter->started);
  1913. kfree(iter);
  1914. atomic_dec(&tracing_reader);
  1915. return 0;
  1916. }
  1917. static unsigned int
  1918. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1919. {
  1920. struct trace_iterator *iter = filp->private_data;
  1921. if (trace_flags & TRACE_ITER_BLOCK) {
  1922. /*
  1923. * Always select as readable when in blocking mode
  1924. */
  1925. return POLLIN | POLLRDNORM;
  1926. } else {
  1927. if (!trace_empty(iter))
  1928. return POLLIN | POLLRDNORM;
  1929. poll_wait(filp, &trace_wait, poll_table);
  1930. if (!trace_empty(iter))
  1931. return POLLIN | POLLRDNORM;
  1932. return 0;
  1933. }
  1934. }
  1935. /*
  1936. * Consumer reader.
  1937. */
  1938. static ssize_t
  1939. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1940. size_t cnt, loff_t *ppos)
  1941. {
  1942. struct trace_iterator *iter = filp->private_data;
  1943. ssize_t sret;
  1944. /* return any leftover data */
  1945. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  1946. if (sret != -EBUSY)
  1947. return sret;
  1948. trace_seq_reset(&iter->seq);
  1949. mutex_lock(&trace_types_lock);
  1950. if (iter->trace->read) {
  1951. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  1952. if (sret)
  1953. goto out;
  1954. }
  1955. waitagain:
  1956. sret = 0;
  1957. while (trace_empty(iter)) {
  1958. if ((filp->f_flags & O_NONBLOCK)) {
  1959. sret = -EAGAIN;
  1960. goto out;
  1961. }
  1962. /*
  1963. * This is a make-shift waitqueue. The reason we don't use
  1964. * an actual wait queue is because:
  1965. * 1) we only ever have one waiter
  1966. * 2) the tracing, traces all functions, we don't want
  1967. * the overhead of calling wake_up and friends
  1968. * (and tracing them too)
  1969. * Anyway, this is really very primitive wakeup.
  1970. */
  1971. set_current_state(TASK_INTERRUPTIBLE);
  1972. iter->tr->waiter = current;
  1973. mutex_unlock(&trace_types_lock);
  1974. /* sleep for 100 msecs, and try again. */
  1975. schedule_timeout(HZ/10);
  1976. mutex_lock(&trace_types_lock);
  1977. iter->tr->waiter = NULL;
  1978. if (signal_pending(current)) {
  1979. sret = -EINTR;
  1980. goto out;
  1981. }
  1982. if (iter->trace != current_trace)
  1983. goto out;
  1984. /*
  1985. * We block until we read something and tracing is disabled.
  1986. * We still block if tracing is disabled, but we have never
  1987. * read anything. This allows a user to cat this file, and
  1988. * then enable tracing. But after we have read something,
  1989. * we give an EOF when tracing is again disabled.
  1990. *
  1991. * iter->pos will be 0 if we haven't read anything.
  1992. */
  1993. if (!tracer_enabled && iter->pos)
  1994. break;
  1995. continue;
  1996. }
  1997. /* stop when tracing is finished */
  1998. if (trace_empty(iter))
  1999. goto out;
  2000. if (cnt >= PAGE_SIZE)
  2001. cnt = PAGE_SIZE - 1;
  2002. /* reset all but tr, trace, and overruns */
  2003. memset(&iter->seq, 0,
  2004. sizeof(struct trace_iterator) -
  2005. offsetof(struct trace_iterator, seq));
  2006. iter->pos = -1;
  2007. while (find_next_entry_inc(iter) != NULL) {
  2008. enum print_line_t ret;
  2009. int len = iter->seq.len;
  2010. ret = print_trace_line(iter);
  2011. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2012. /* don't print partial lines */
  2013. iter->seq.len = len;
  2014. break;
  2015. }
  2016. trace_consume(iter);
  2017. if (iter->seq.len >= cnt)
  2018. break;
  2019. }
  2020. /* Now copy what we have to the user */
  2021. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2022. if (iter->seq.readpos >= iter->seq.len)
  2023. trace_seq_reset(&iter->seq);
  2024. /*
  2025. * If there was nothing to send to user, inspite of consuming trace
  2026. * entries, go back to wait for more entries.
  2027. */
  2028. if (sret == -EBUSY)
  2029. goto waitagain;
  2030. out:
  2031. mutex_unlock(&trace_types_lock);
  2032. return sret;
  2033. }
  2034. static ssize_t
  2035. tracing_entries_read(struct file *filp, char __user *ubuf,
  2036. size_t cnt, loff_t *ppos)
  2037. {
  2038. struct trace_array *tr = filp->private_data;
  2039. char buf[64];
  2040. int r;
  2041. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  2042. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2043. }
  2044. static ssize_t
  2045. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2046. size_t cnt, loff_t *ppos)
  2047. {
  2048. unsigned long val;
  2049. char buf[64];
  2050. int ret, cpu;
  2051. if (cnt >= sizeof(buf))
  2052. return -EINVAL;
  2053. if (copy_from_user(&buf, ubuf, cnt))
  2054. return -EFAULT;
  2055. buf[cnt] = 0;
  2056. ret = strict_strtoul(buf, 10, &val);
  2057. if (ret < 0)
  2058. return ret;
  2059. /* must have at least 1 entry */
  2060. if (!val)
  2061. return -EINVAL;
  2062. mutex_lock(&trace_types_lock);
  2063. tracing_stop();
  2064. /* disable all cpu buffers */
  2065. for_each_tracing_cpu(cpu) {
  2066. if (global_trace.data[cpu])
  2067. atomic_inc(&global_trace.data[cpu]->disabled);
  2068. if (max_tr.data[cpu])
  2069. atomic_inc(&max_tr.data[cpu]->disabled);
  2070. }
  2071. /* value is in KB */
  2072. val <<= 10;
  2073. if (val != global_trace.entries) {
  2074. ret = ring_buffer_resize(global_trace.buffer, val);
  2075. if (ret < 0) {
  2076. cnt = ret;
  2077. goto out;
  2078. }
  2079. ret = ring_buffer_resize(max_tr.buffer, val);
  2080. if (ret < 0) {
  2081. int r;
  2082. cnt = ret;
  2083. r = ring_buffer_resize(global_trace.buffer,
  2084. global_trace.entries);
  2085. if (r < 0) {
  2086. /* AARGH! We are left with different
  2087. * size max buffer!!!! */
  2088. WARN_ON(1);
  2089. tracing_disabled = 1;
  2090. }
  2091. goto out;
  2092. }
  2093. global_trace.entries = val;
  2094. }
  2095. filp->f_pos += cnt;
  2096. /* If check pages failed, return ENOMEM */
  2097. if (tracing_disabled)
  2098. cnt = -ENOMEM;
  2099. out:
  2100. for_each_tracing_cpu(cpu) {
  2101. if (global_trace.data[cpu])
  2102. atomic_dec(&global_trace.data[cpu]->disabled);
  2103. if (max_tr.data[cpu])
  2104. atomic_dec(&max_tr.data[cpu]->disabled);
  2105. }
  2106. tracing_start();
  2107. max_tr.entries = global_trace.entries;
  2108. mutex_unlock(&trace_types_lock);
  2109. return cnt;
  2110. }
  2111. static int mark_printk(const char *fmt, ...)
  2112. {
  2113. int ret;
  2114. va_list args;
  2115. va_start(args, fmt);
  2116. ret = trace_vprintk(0, -1, fmt, args);
  2117. va_end(args);
  2118. return ret;
  2119. }
  2120. static ssize_t
  2121. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2122. size_t cnt, loff_t *fpos)
  2123. {
  2124. char *buf;
  2125. char *end;
  2126. if (tracing_disabled)
  2127. return -EINVAL;
  2128. if (cnt > TRACE_BUF_SIZE)
  2129. cnt = TRACE_BUF_SIZE;
  2130. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2131. if (buf == NULL)
  2132. return -ENOMEM;
  2133. if (copy_from_user(buf, ubuf, cnt)) {
  2134. kfree(buf);
  2135. return -EFAULT;
  2136. }
  2137. /* Cut from the first nil or newline. */
  2138. buf[cnt] = '\0';
  2139. end = strchr(buf, '\n');
  2140. if (end)
  2141. *end = '\0';
  2142. cnt = mark_printk("%s\n", buf);
  2143. kfree(buf);
  2144. *fpos += cnt;
  2145. return cnt;
  2146. }
  2147. static struct file_operations tracing_max_lat_fops = {
  2148. .open = tracing_open_generic,
  2149. .read = tracing_max_lat_read,
  2150. .write = tracing_max_lat_write,
  2151. };
  2152. static struct file_operations tracing_ctrl_fops = {
  2153. .open = tracing_open_generic,
  2154. .read = tracing_ctrl_read,
  2155. .write = tracing_ctrl_write,
  2156. };
  2157. static struct file_operations set_tracer_fops = {
  2158. .open = tracing_open_generic,
  2159. .read = tracing_set_trace_read,
  2160. .write = tracing_set_trace_write,
  2161. };
  2162. static struct file_operations tracing_pipe_fops = {
  2163. .open = tracing_open_pipe,
  2164. .poll = tracing_poll_pipe,
  2165. .read = tracing_read_pipe,
  2166. .release = tracing_release_pipe,
  2167. };
  2168. static struct file_operations tracing_entries_fops = {
  2169. .open = tracing_open_generic,
  2170. .read = tracing_entries_read,
  2171. .write = tracing_entries_write,
  2172. };
  2173. static struct file_operations tracing_mark_fops = {
  2174. .open = tracing_open_generic,
  2175. .write = tracing_mark_write,
  2176. };
  2177. #ifdef CONFIG_DYNAMIC_FTRACE
  2178. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2179. {
  2180. return 0;
  2181. }
  2182. static ssize_t
  2183. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2184. size_t cnt, loff_t *ppos)
  2185. {
  2186. static char ftrace_dyn_info_buffer[1024];
  2187. static DEFINE_MUTEX(dyn_info_mutex);
  2188. unsigned long *p = filp->private_data;
  2189. char *buf = ftrace_dyn_info_buffer;
  2190. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2191. int r;
  2192. mutex_lock(&dyn_info_mutex);
  2193. r = sprintf(buf, "%ld ", *p);
  2194. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2195. buf[r++] = '\n';
  2196. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2197. mutex_unlock(&dyn_info_mutex);
  2198. return r;
  2199. }
  2200. static struct file_operations tracing_dyn_info_fops = {
  2201. .open = tracing_open_generic,
  2202. .read = tracing_read_dyn_info,
  2203. };
  2204. #endif
  2205. static struct dentry *d_tracer;
  2206. struct dentry *tracing_init_dentry(void)
  2207. {
  2208. static int once;
  2209. if (d_tracer)
  2210. return d_tracer;
  2211. d_tracer = debugfs_create_dir("tracing", NULL);
  2212. if (!d_tracer && !once) {
  2213. once = 1;
  2214. pr_warning("Could not create debugfs directory 'tracing'\n");
  2215. return NULL;
  2216. }
  2217. return d_tracer;
  2218. }
  2219. #ifdef CONFIG_FTRACE_SELFTEST
  2220. /* Let selftest have access to static functions in this file */
  2221. #include "trace_selftest.c"
  2222. #endif
  2223. static __init int tracer_init_debugfs(void)
  2224. {
  2225. struct dentry *d_tracer;
  2226. struct dentry *entry;
  2227. d_tracer = tracing_init_dentry();
  2228. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2229. &global_trace, &tracing_ctrl_fops);
  2230. if (!entry)
  2231. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2232. entry = debugfs_create_file("trace_options", 0644, d_tracer,
  2233. NULL, &tracing_iter_fops);
  2234. if (!entry)
  2235. pr_warning("Could not create debugfs 'trace_options' entry\n");
  2236. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2237. NULL, &tracing_cpumask_fops);
  2238. if (!entry)
  2239. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2240. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2241. &global_trace, &tracing_lt_fops);
  2242. if (!entry)
  2243. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2244. entry = debugfs_create_file("trace", 0444, d_tracer,
  2245. &global_trace, &tracing_fops);
  2246. if (!entry)
  2247. pr_warning("Could not create debugfs 'trace' entry\n");
  2248. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2249. &global_trace, &show_traces_fops);
  2250. if (!entry)
  2251. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  2252. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2253. &global_trace, &set_tracer_fops);
  2254. if (!entry)
  2255. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  2256. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2257. &tracing_max_latency,
  2258. &tracing_max_lat_fops);
  2259. if (!entry)
  2260. pr_warning("Could not create debugfs "
  2261. "'tracing_max_latency' entry\n");
  2262. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2263. &tracing_thresh, &tracing_max_lat_fops);
  2264. if (!entry)
  2265. pr_warning("Could not create debugfs "
  2266. "'tracing_thresh' entry\n");
  2267. entry = debugfs_create_file("README", 0644, d_tracer,
  2268. NULL, &tracing_readme_fops);
  2269. if (!entry)
  2270. pr_warning("Could not create debugfs 'README' entry\n");
  2271. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2272. NULL, &tracing_pipe_fops);
  2273. if (!entry)
  2274. pr_warning("Could not create debugfs "
  2275. "'trace_pipe' entry\n");
  2276. entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
  2277. &global_trace, &tracing_entries_fops);
  2278. if (!entry)
  2279. pr_warning("Could not create debugfs "
  2280. "'buffer_size_kb' entry\n");
  2281. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  2282. NULL, &tracing_mark_fops);
  2283. if (!entry)
  2284. pr_warning("Could not create debugfs "
  2285. "'trace_marker' entry\n");
  2286. #ifdef CONFIG_DYNAMIC_FTRACE
  2287. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2288. &ftrace_update_tot_cnt,
  2289. &tracing_dyn_info_fops);
  2290. if (!entry)
  2291. pr_warning("Could not create debugfs "
  2292. "'dyn_ftrace_total_info' entry\n");
  2293. #endif
  2294. #ifdef CONFIG_SYSPROF_TRACER
  2295. init_tracer_sysprof_debugfs(d_tracer);
  2296. #endif
  2297. return 0;
  2298. }
  2299. int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
  2300. {
  2301. static DEFINE_SPINLOCK(trace_buf_lock);
  2302. static char trace_buf[TRACE_BUF_SIZE];
  2303. struct ring_buffer_event *event;
  2304. struct trace_array *tr = &global_trace;
  2305. struct trace_array_cpu *data;
  2306. int cpu, len = 0, size, pc;
  2307. struct print_entry *entry;
  2308. unsigned long irq_flags;
  2309. if (tracing_disabled || tracing_selftest_running)
  2310. return 0;
  2311. pc = preempt_count();
  2312. preempt_disable_notrace();
  2313. cpu = raw_smp_processor_id();
  2314. data = tr->data[cpu];
  2315. if (unlikely(atomic_read(&data->disabled)))
  2316. goto out;
  2317. pause_graph_tracing();
  2318. spin_lock_irqsave(&trace_buf_lock, irq_flags);
  2319. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  2320. len = min(len, TRACE_BUF_SIZE-1);
  2321. trace_buf[len] = 0;
  2322. size = sizeof(*entry) + len + 1;
  2323. event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
  2324. if (!event)
  2325. goto out_unlock;
  2326. entry = ring_buffer_event_data(event);
  2327. tracing_generic_entry_update(&entry->ent, irq_flags, pc);
  2328. entry->ent.type = TRACE_PRINT;
  2329. entry->ip = ip;
  2330. entry->depth = depth;
  2331. memcpy(&entry->buf, trace_buf, len);
  2332. entry->buf[len] = 0;
  2333. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  2334. out_unlock:
  2335. spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
  2336. unpause_graph_tracing();
  2337. out:
  2338. preempt_enable_notrace();
  2339. return len;
  2340. }
  2341. EXPORT_SYMBOL_GPL(trace_vprintk);
  2342. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2343. {
  2344. int ret;
  2345. va_list ap;
  2346. if (!(trace_flags & TRACE_ITER_PRINTK))
  2347. return 0;
  2348. va_start(ap, fmt);
  2349. ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
  2350. va_end(ap);
  2351. return ret;
  2352. }
  2353. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2354. int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
  2355. {
  2356. if (!(trace_flags & TRACE_ITER_PRINTK))
  2357. return 0;
  2358. return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
  2359. }
  2360. EXPORT_SYMBOL_GPL(__ftrace_vprintk);
  2361. static int trace_panic_handler(struct notifier_block *this,
  2362. unsigned long event, void *unused)
  2363. {
  2364. if (ftrace_dump_on_oops)
  2365. ftrace_dump();
  2366. return NOTIFY_OK;
  2367. }
  2368. static struct notifier_block trace_panic_notifier = {
  2369. .notifier_call = trace_panic_handler,
  2370. .next = NULL,
  2371. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  2372. };
  2373. static int trace_die_handler(struct notifier_block *self,
  2374. unsigned long val,
  2375. void *data)
  2376. {
  2377. switch (val) {
  2378. case DIE_OOPS:
  2379. if (ftrace_dump_on_oops)
  2380. ftrace_dump();
  2381. break;
  2382. default:
  2383. break;
  2384. }
  2385. return NOTIFY_OK;
  2386. }
  2387. static struct notifier_block trace_die_notifier = {
  2388. .notifier_call = trace_die_handler,
  2389. .priority = 200
  2390. };
  2391. /*
  2392. * printk is set to max of 1024, we really don't need it that big.
  2393. * Nothing should be printing 1000 characters anyway.
  2394. */
  2395. #define TRACE_MAX_PRINT 1000
  2396. /*
  2397. * Define here KERN_TRACE so that we have one place to modify
  2398. * it if we decide to change what log level the ftrace dump
  2399. * should be at.
  2400. */
  2401. #define KERN_TRACE KERN_EMERG
  2402. static void
  2403. trace_printk_seq(struct trace_seq *s)
  2404. {
  2405. /* Probably should print a warning here. */
  2406. if (s->len >= 1000)
  2407. s->len = 1000;
  2408. /* should be zero ended, but we are paranoid. */
  2409. s->buffer[s->len] = 0;
  2410. printk(KERN_TRACE "%s", s->buffer);
  2411. trace_seq_reset(s);
  2412. }
  2413. void ftrace_dump(void)
  2414. {
  2415. static DEFINE_SPINLOCK(ftrace_dump_lock);
  2416. /* use static because iter can be a bit big for the stack */
  2417. static struct trace_iterator iter;
  2418. static int dump_ran;
  2419. unsigned long flags;
  2420. int cnt = 0, cpu;
  2421. /* only one dump */
  2422. spin_lock_irqsave(&ftrace_dump_lock, flags);
  2423. if (dump_ran)
  2424. goto out;
  2425. dump_ran = 1;
  2426. /* No turning back! */
  2427. tracing_off();
  2428. ftrace_kill();
  2429. for_each_tracing_cpu(cpu) {
  2430. atomic_inc(&global_trace.data[cpu]->disabled);
  2431. }
  2432. /* don't look at user memory in panic mode */
  2433. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  2434. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  2435. iter.tr = &global_trace;
  2436. iter.trace = current_trace;
  2437. /*
  2438. * We need to stop all tracing on all CPUS to read the
  2439. * the next buffer. This is a bit expensive, but is
  2440. * not done often. We fill all what we can read,
  2441. * and then release the locks again.
  2442. */
  2443. while (!trace_empty(&iter)) {
  2444. if (!cnt)
  2445. printk(KERN_TRACE "---------------------------------\n");
  2446. cnt++;
  2447. /* reset all but tr, trace, and overruns */
  2448. memset(&iter.seq, 0,
  2449. sizeof(struct trace_iterator) -
  2450. offsetof(struct trace_iterator, seq));
  2451. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  2452. iter.pos = -1;
  2453. if (find_next_entry_inc(&iter) != NULL) {
  2454. print_trace_line(&iter);
  2455. trace_consume(&iter);
  2456. }
  2457. trace_printk_seq(&iter.seq);
  2458. }
  2459. if (!cnt)
  2460. printk(KERN_TRACE " (ftrace buffer empty)\n");
  2461. else
  2462. printk(KERN_TRACE "---------------------------------\n");
  2463. out:
  2464. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  2465. }
  2466. __init static int tracer_alloc_buffers(void)
  2467. {
  2468. struct trace_array_cpu *data;
  2469. int i;
  2470. int ret = -ENOMEM;
  2471. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  2472. goto out;
  2473. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  2474. goto out_free_buffer_mask;
  2475. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  2476. cpumask_copy(tracing_cpumask, cpu_all_mask);
  2477. /* TODO: make the number of buffers hot pluggable with CPUS */
  2478. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  2479. TRACE_BUFFER_FLAGS);
  2480. if (!global_trace.buffer) {
  2481. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  2482. WARN_ON(1);
  2483. goto out_free_cpumask;
  2484. }
  2485. global_trace.entries = ring_buffer_size(global_trace.buffer);
  2486. #ifdef CONFIG_TRACER_MAX_TRACE
  2487. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  2488. TRACE_BUFFER_FLAGS);
  2489. if (!max_tr.buffer) {
  2490. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  2491. WARN_ON(1);
  2492. ring_buffer_free(global_trace.buffer);
  2493. goto out_free_cpumask;
  2494. }
  2495. max_tr.entries = ring_buffer_size(max_tr.buffer);
  2496. WARN_ON(max_tr.entries != global_trace.entries);
  2497. #endif
  2498. /* Allocate the first page for all buffers */
  2499. for_each_tracing_cpu(i) {
  2500. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2501. max_tr.data[i] = &per_cpu(max_data, i);
  2502. }
  2503. trace_init_cmdlines();
  2504. register_tracer(&nop_trace);
  2505. current_trace = &nop_trace;
  2506. #ifdef CONFIG_BOOT_TRACER
  2507. register_tracer(&boot_tracer);
  2508. #endif
  2509. /* All seems OK, enable tracing */
  2510. tracing_disabled = 0;
  2511. atomic_notifier_chain_register(&panic_notifier_list,
  2512. &trace_panic_notifier);
  2513. register_die_notifier(&trace_die_notifier);
  2514. ret = 0;
  2515. out_free_cpumask:
  2516. free_cpumask_var(tracing_cpumask);
  2517. out_free_buffer_mask:
  2518. free_cpumask_var(tracing_buffer_mask);
  2519. out:
  2520. return ret;
  2521. }
  2522. __init static int clear_boot_tracer(void)
  2523. {
  2524. /*
  2525. * The default tracer at boot buffer is an init section.
  2526. * This function is called in lateinit. If we did not
  2527. * find the boot tracer, then clear it out, to prevent
  2528. * later registration from accessing the buffer that is
  2529. * about to be freed.
  2530. */
  2531. if (!default_bootup_tracer)
  2532. return 0;
  2533. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  2534. default_bootup_tracer);
  2535. default_bootup_tracer = NULL;
  2536. return 0;
  2537. }
  2538. early_initcall(tracer_alloc_buffers);
  2539. fs_initcall(tracer_init_debugfs);
  2540. late_initcall(clear_boot_tracer);