trace.c 73 KB

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