trace.c 70 KB

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