trace.c 71 KB

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