trace.c 70 KB

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