trace.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235
  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. #ifdef CONFIG_STACKTRACE
  591. struct ring_buffer_event *event;
  592. struct stack_entry *entry;
  593. struct stack_trace trace;
  594. unsigned long irq_flags;
  595. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  596. return;
  597. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  598. &irq_flags);
  599. if (!event)
  600. return;
  601. entry = ring_buffer_event_data(event);
  602. tracing_generic_entry_update(&entry->ent, flags, pc);
  603. entry->ent.type = TRACE_STACK;
  604. memset(&entry->caller, 0, sizeof(entry->caller));
  605. trace.nr_entries = 0;
  606. trace.max_entries = FTRACE_STACK_ENTRIES;
  607. trace.skip = skip;
  608. trace.entries = entry->caller;
  609. save_stack_trace(&trace);
  610. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  611. #endif
  612. }
  613. void __trace_stack(struct trace_array *tr,
  614. struct trace_array_cpu *data,
  615. unsigned long flags,
  616. int skip)
  617. {
  618. ftrace_trace_stack(tr, data, flags, skip, preempt_count());
  619. }
  620. static void
  621. ftrace_trace_special(void *__tr, void *__data,
  622. unsigned long arg1, unsigned long arg2, unsigned long arg3,
  623. int pc)
  624. {
  625. struct ring_buffer_event *event;
  626. struct trace_array_cpu *data = __data;
  627. struct trace_array *tr = __tr;
  628. struct special_entry *entry;
  629. unsigned long irq_flags;
  630. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  631. &irq_flags);
  632. if (!event)
  633. return;
  634. entry = ring_buffer_event_data(event);
  635. tracing_generic_entry_update(&entry->ent, 0, pc);
  636. entry->ent.type = TRACE_SPECIAL;
  637. entry->arg1 = arg1;
  638. entry->arg2 = arg2;
  639. entry->arg3 = arg3;
  640. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  641. ftrace_trace_stack(tr, data, irq_flags, 4, pc);
  642. trace_wake_up();
  643. }
  644. void
  645. __trace_special(void *__tr, void *__data,
  646. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  647. {
  648. ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
  649. }
  650. void
  651. tracing_sched_switch_trace(struct trace_array *tr,
  652. struct trace_array_cpu *data,
  653. struct task_struct *prev,
  654. struct task_struct *next,
  655. unsigned long flags, int pc)
  656. {
  657. struct ring_buffer_event *event;
  658. struct ctx_switch_entry *entry;
  659. unsigned long irq_flags;
  660. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  661. &irq_flags);
  662. if (!event)
  663. return;
  664. entry = ring_buffer_event_data(event);
  665. tracing_generic_entry_update(&entry->ent, flags, pc);
  666. entry->ent.type = TRACE_CTX;
  667. entry->prev_pid = prev->pid;
  668. entry->prev_prio = prev->prio;
  669. entry->prev_state = prev->state;
  670. entry->next_pid = next->pid;
  671. entry->next_prio = next->prio;
  672. entry->next_state = next->state;
  673. entry->next_cpu = task_cpu(next);
  674. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  675. ftrace_trace_stack(tr, data, flags, 5, pc);
  676. }
  677. void
  678. tracing_sched_wakeup_trace(struct trace_array *tr,
  679. struct trace_array_cpu *data,
  680. struct task_struct *wakee,
  681. struct task_struct *curr,
  682. unsigned long flags, int pc)
  683. {
  684. struct ring_buffer_event *event;
  685. struct ctx_switch_entry *entry;
  686. unsigned long irq_flags;
  687. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  688. &irq_flags);
  689. if (!event)
  690. return;
  691. entry = ring_buffer_event_data(event);
  692. tracing_generic_entry_update(&entry->ent, flags, pc);
  693. entry->ent.type = TRACE_WAKE;
  694. entry->prev_pid = curr->pid;
  695. entry->prev_prio = curr->prio;
  696. entry->prev_state = curr->state;
  697. entry->next_pid = wakee->pid;
  698. entry->next_prio = wakee->prio;
  699. entry->next_state = wakee->state;
  700. entry->next_cpu = task_cpu(wakee);
  701. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  702. ftrace_trace_stack(tr, data, flags, 6, pc);
  703. trace_wake_up();
  704. }
  705. void
  706. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
  707. {
  708. struct trace_array *tr = &global_trace;
  709. struct trace_array_cpu *data;
  710. int cpu;
  711. int pc;
  712. if (tracing_disabled || !tr->ctrl)
  713. return;
  714. pc = preempt_count();
  715. preempt_disable_notrace();
  716. cpu = raw_smp_processor_id();
  717. data = tr->data[cpu];
  718. if (likely(!atomic_read(&data->disabled)))
  719. ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
  720. preempt_enable_notrace();
  721. }
  722. #ifdef CONFIG_FUNCTION_TRACER
  723. static void
  724. function_trace_call(unsigned long ip, unsigned long parent_ip)
  725. {
  726. struct trace_array *tr = &global_trace;
  727. struct trace_array_cpu *data;
  728. unsigned long flags;
  729. long disabled;
  730. int cpu, resched;
  731. int pc;
  732. if (unlikely(!ftrace_function_enabled))
  733. return;
  734. pc = preempt_count();
  735. resched = need_resched();
  736. preempt_disable_notrace();
  737. local_save_flags(flags);
  738. cpu = raw_smp_processor_id();
  739. data = tr->data[cpu];
  740. disabled = atomic_inc_return(&data->disabled);
  741. if (likely(disabled == 1))
  742. trace_function(tr, data, ip, parent_ip, flags, pc);
  743. atomic_dec(&data->disabled);
  744. if (resched)
  745. preempt_enable_no_resched_notrace();
  746. else
  747. preempt_enable_notrace();
  748. }
  749. static struct ftrace_ops trace_ops __read_mostly =
  750. {
  751. .func = function_trace_call,
  752. };
  753. void tracing_start_function_trace(void)
  754. {
  755. ftrace_function_enabled = 0;
  756. register_ftrace_function(&trace_ops);
  757. if (tracer_enabled)
  758. ftrace_function_enabled = 1;
  759. }
  760. void tracing_stop_function_trace(void)
  761. {
  762. ftrace_function_enabled = 0;
  763. unregister_ftrace_function(&trace_ops);
  764. }
  765. #endif
  766. enum trace_file_type {
  767. TRACE_FILE_LAT_FMT = 1,
  768. };
  769. static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
  770. {
  771. /* Don't allow ftrace to trace into the ring buffers */
  772. ftrace_disable_cpu();
  773. iter->idx++;
  774. if (iter->buffer_iter[iter->cpu])
  775. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  776. ftrace_enable_cpu();
  777. }
  778. static struct trace_entry *
  779. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
  780. {
  781. struct ring_buffer_event *event;
  782. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  783. /* Don't allow ftrace to trace into the ring buffers */
  784. ftrace_disable_cpu();
  785. if (buf_iter)
  786. event = ring_buffer_iter_peek(buf_iter, ts);
  787. else
  788. event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
  789. ftrace_enable_cpu();
  790. return event ? ring_buffer_event_data(event) : NULL;
  791. }
  792. static struct trace_entry *
  793. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  794. {
  795. struct ring_buffer *buffer = iter->tr->buffer;
  796. struct trace_entry *ent, *next = NULL;
  797. u64 next_ts = 0, ts;
  798. int next_cpu = -1;
  799. int cpu;
  800. for_each_tracing_cpu(cpu) {
  801. if (ring_buffer_empty_cpu(buffer, cpu))
  802. continue;
  803. ent = peek_next_entry(iter, cpu, &ts);
  804. /*
  805. * Pick the entry with the smallest timestamp:
  806. */
  807. if (ent && (!next || ts < next_ts)) {
  808. next = ent;
  809. next_cpu = cpu;
  810. next_ts = ts;
  811. }
  812. }
  813. if (ent_cpu)
  814. *ent_cpu = next_cpu;
  815. if (ent_ts)
  816. *ent_ts = next_ts;
  817. return next;
  818. }
  819. /* Find the next real entry, without updating the iterator itself */
  820. static struct trace_entry *
  821. find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  822. {
  823. return __find_next_entry(iter, ent_cpu, ent_ts);
  824. }
  825. /* Find the next real entry, and increment the iterator to the next entry */
  826. static void *find_next_entry_inc(struct trace_iterator *iter)
  827. {
  828. iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
  829. if (iter->ent)
  830. trace_iterator_increment(iter, iter->cpu);
  831. return iter->ent ? iter : NULL;
  832. }
  833. static void trace_consume(struct trace_iterator *iter)
  834. {
  835. /* Don't allow ftrace to trace into the ring buffers */
  836. ftrace_disable_cpu();
  837. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
  838. ftrace_enable_cpu();
  839. }
  840. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  841. {
  842. struct trace_iterator *iter = m->private;
  843. int i = (int)*pos;
  844. void *ent;
  845. (*pos)++;
  846. /* can't go backwards */
  847. if (iter->idx > i)
  848. return NULL;
  849. if (iter->idx < 0)
  850. ent = find_next_entry_inc(iter);
  851. else
  852. ent = iter;
  853. while (ent && iter->idx < i)
  854. ent = find_next_entry_inc(iter);
  855. iter->pos = *pos;
  856. return ent;
  857. }
  858. static void *s_start(struct seq_file *m, loff_t *pos)
  859. {
  860. struct trace_iterator *iter = m->private;
  861. void *p = NULL;
  862. loff_t l = 0;
  863. int cpu;
  864. mutex_lock(&trace_types_lock);
  865. if (!current_trace || current_trace != iter->trace) {
  866. mutex_unlock(&trace_types_lock);
  867. return NULL;
  868. }
  869. atomic_inc(&trace_record_cmdline_disabled);
  870. /* let the tracer grab locks here if needed */
  871. if (current_trace->start)
  872. current_trace->start(iter);
  873. if (*pos != iter->pos) {
  874. iter->ent = NULL;
  875. iter->cpu = 0;
  876. iter->idx = -1;
  877. ftrace_disable_cpu();
  878. for_each_tracing_cpu(cpu) {
  879. ring_buffer_iter_reset(iter->buffer_iter[cpu]);
  880. }
  881. ftrace_enable_cpu();
  882. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  883. ;
  884. } else {
  885. l = *pos - 1;
  886. p = s_next(m, p, &l);
  887. }
  888. return p;
  889. }
  890. static void s_stop(struct seq_file *m, void *p)
  891. {
  892. struct trace_iterator *iter = m->private;
  893. atomic_dec(&trace_record_cmdline_disabled);
  894. /* let the tracer release locks here if needed */
  895. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  896. iter->trace->stop(iter);
  897. mutex_unlock(&trace_types_lock);
  898. }
  899. #ifdef CONFIG_KRETPROBES
  900. static inline const char *kretprobed(const char *name)
  901. {
  902. static const char tramp_name[] = "kretprobe_trampoline";
  903. int size = sizeof(tramp_name);
  904. if (strncmp(tramp_name, name, size) == 0)
  905. return "[unknown/kretprobe'd]";
  906. return name;
  907. }
  908. #else
  909. static inline const char *kretprobed(const char *name)
  910. {
  911. return name;
  912. }
  913. #endif /* CONFIG_KRETPROBES */
  914. static int
  915. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  916. {
  917. #ifdef CONFIG_KALLSYMS
  918. char str[KSYM_SYMBOL_LEN];
  919. const char *name;
  920. kallsyms_lookup(address, NULL, NULL, NULL, str);
  921. name = kretprobed(str);
  922. return trace_seq_printf(s, fmt, name);
  923. #endif
  924. return 1;
  925. }
  926. static int
  927. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  928. unsigned long address)
  929. {
  930. #ifdef CONFIG_KALLSYMS
  931. char str[KSYM_SYMBOL_LEN];
  932. const char *name;
  933. sprint_symbol(str, address);
  934. name = kretprobed(str);
  935. return trace_seq_printf(s, fmt, name);
  936. #endif
  937. return 1;
  938. }
  939. #ifndef CONFIG_64BIT
  940. # define IP_FMT "%08lx"
  941. #else
  942. # define IP_FMT "%016lx"
  943. #endif
  944. static int
  945. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  946. {
  947. int ret;
  948. if (!ip)
  949. return trace_seq_printf(s, "0");
  950. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  951. ret = seq_print_sym_offset(s, "%s", ip);
  952. else
  953. ret = seq_print_sym_short(s, "%s", ip);
  954. if (!ret)
  955. return 0;
  956. if (sym_flags & TRACE_ITER_SYM_ADDR)
  957. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  958. return ret;
  959. }
  960. static void print_lat_help_header(struct seq_file *m)
  961. {
  962. seq_puts(m, "# _------=> CPU# \n");
  963. seq_puts(m, "# / _-----=> irqs-off \n");
  964. seq_puts(m, "# | / _----=> need-resched \n");
  965. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  966. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  967. seq_puts(m, "# |||| / \n");
  968. seq_puts(m, "# ||||| delay \n");
  969. seq_puts(m, "# cmd pid ||||| time | caller \n");
  970. seq_puts(m, "# \\ / ||||| \\ | / \n");
  971. }
  972. static void print_func_help_header(struct seq_file *m)
  973. {
  974. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  975. seq_puts(m, "# | | | | |\n");
  976. }
  977. static void
  978. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  979. {
  980. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  981. struct trace_array *tr = iter->tr;
  982. struct trace_array_cpu *data = tr->data[tr->cpu];
  983. struct tracer *type = current_trace;
  984. unsigned long total;
  985. unsigned long entries;
  986. const char *name = "preemption";
  987. if (type)
  988. name = type->name;
  989. entries = ring_buffer_entries(iter->tr->buffer);
  990. total = entries +
  991. ring_buffer_overruns(iter->tr->buffer);
  992. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  993. name, UTS_RELEASE);
  994. seq_puts(m, "-----------------------------------"
  995. "---------------------------------\n");
  996. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  997. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  998. nsecs_to_usecs(data->saved_latency),
  999. entries,
  1000. total,
  1001. tr->cpu,
  1002. #if defined(CONFIG_PREEMPT_NONE)
  1003. "server",
  1004. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1005. "desktop",
  1006. #elif defined(CONFIG_PREEMPT)
  1007. "preempt",
  1008. #else
  1009. "unknown",
  1010. #endif
  1011. /* These are reserved for later use */
  1012. 0, 0, 0, 0);
  1013. #ifdef CONFIG_SMP
  1014. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1015. #else
  1016. seq_puts(m, ")\n");
  1017. #endif
  1018. seq_puts(m, " -----------------\n");
  1019. seq_printf(m, " | task: %.16s-%d "
  1020. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1021. data->comm, data->pid, data->uid, data->nice,
  1022. data->policy, data->rt_priority);
  1023. seq_puts(m, " -----------------\n");
  1024. if (data->critical_start) {
  1025. seq_puts(m, " => started at: ");
  1026. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1027. trace_print_seq(m, &iter->seq);
  1028. seq_puts(m, "\n => ended at: ");
  1029. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1030. trace_print_seq(m, &iter->seq);
  1031. seq_puts(m, "\n");
  1032. }
  1033. seq_puts(m, "\n");
  1034. }
  1035. static void
  1036. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1037. {
  1038. int hardirq, softirq;
  1039. char *comm;
  1040. comm = trace_find_cmdline(entry->pid);
  1041. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1042. trace_seq_printf(s, "%3d", cpu);
  1043. trace_seq_printf(s, "%c%c",
  1044. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  1045. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
  1046. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1047. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1048. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1049. if (hardirq && softirq) {
  1050. trace_seq_putc(s, 'H');
  1051. } else {
  1052. if (hardirq) {
  1053. trace_seq_putc(s, 'h');
  1054. } else {
  1055. if (softirq)
  1056. trace_seq_putc(s, 's');
  1057. else
  1058. trace_seq_putc(s, '.');
  1059. }
  1060. }
  1061. if (entry->preempt_count)
  1062. trace_seq_printf(s, "%x", entry->preempt_count);
  1063. else
  1064. trace_seq_puts(s, ".");
  1065. }
  1066. unsigned long preempt_mark_thresh = 100;
  1067. static void
  1068. lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
  1069. unsigned long rel_usecs)
  1070. {
  1071. trace_seq_printf(s, " %4lldus", abs_usecs);
  1072. if (rel_usecs > preempt_mark_thresh)
  1073. trace_seq_puts(s, "!: ");
  1074. else if (rel_usecs > 1)
  1075. trace_seq_puts(s, "+: ");
  1076. else
  1077. trace_seq_puts(s, " : ");
  1078. }
  1079. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1080. /*
  1081. * The message is supposed to contain an ending newline.
  1082. * If the printing stops prematurely, try to add a newline of our own.
  1083. */
  1084. void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
  1085. {
  1086. struct trace_entry *ent;
  1087. struct trace_field_cont *cont;
  1088. bool ok = true;
  1089. ent = peek_next_entry(iter, iter->cpu, NULL);
  1090. if (!ent || ent->type != TRACE_CONT) {
  1091. trace_seq_putc(s, '\n');
  1092. return;
  1093. }
  1094. do {
  1095. cont = (struct trace_field_cont *)ent;
  1096. if (ok)
  1097. ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
  1098. ftrace_disable_cpu();
  1099. if (iter->buffer_iter[iter->cpu])
  1100. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1101. else
  1102. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  1103. ftrace_enable_cpu();
  1104. ent = peek_next_entry(iter, iter->cpu, NULL);
  1105. } while (ent && ent->type == TRACE_CONT);
  1106. if (!ok)
  1107. trace_seq_putc(s, '\n');
  1108. }
  1109. static enum print_line_t
  1110. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1111. {
  1112. struct trace_seq *s = &iter->seq;
  1113. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1114. struct trace_entry *next_entry;
  1115. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1116. struct trace_entry *entry = iter->ent;
  1117. unsigned long abs_usecs;
  1118. unsigned long rel_usecs;
  1119. u64 next_ts;
  1120. char *comm;
  1121. int S, T;
  1122. int i;
  1123. unsigned state;
  1124. if (entry->type == TRACE_CONT)
  1125. return TRACE_TYPE_HANDLED;
  1126. next_entry = find_next_entry(iter, NULL, &next_ts);
  1127. if (!next_entry)
  1128. next_ts = iter->ts;
  1129. rel_usecs = ns2usecs(next_ts - iter->ts);
  1130. abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
  1131. if (verbose) {
  1132. comm = trace_find_cmdline(entry->pid);
  1133. trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
  1134. " %ld.%03ldms (+%ld.%03ldms): ",
  1135. comm,
  1136. entry->pid, cpu, entry->flags,
  1137. entry->preempt_count, trace_idx,
  1138. ns2usecs(iter->ts),
  1139. abs_usecs/1000,
  1140. abs_usecs % 1000, rel_usecs/1000,
  1141. rel_usecs % 1000);
  1142. } else {
  1143. lat_print_generic(s, entry, cpu);
  1144. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1145. }
  1146. switch (entry->type) {
  1147. case TRACE_FN: {
  1148. struct ftrace_entry *field;
  1149. trace_assign_type(field, entry);
  1150. seq_print_ip_sym(s, field->ip, sym_flags);
  1151. trace_seq_puts(s, " (");
  1152. seq_print_ip_sym(s, field->parent_ip, sym_flags);
  1153. trace_seq_puts(s, ")\n");
  1154. break;
  1155. }
  1156. case TRACE_CTX:
  1157. case TRACE_WAKE: {
  1158. struct ctx_switch_entry *field;
  1159. trace_assign_type(field, entry);
  1160. T = field->next_state < sizeof(state_to_char) ?
  1161. state_to_char[field->next_state] : 'X';
  1162. state = field->prev_state ?
  1163. __ffs(field->prev_state) + 1 : 0;
  1164. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1165. comm = trace_find_cmdline(field->next_pid);
  1166. trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
  1167. field->prev_pid,
  1168. field->prev_prio,
  1169. S, entry->type == TRACE_CTX ? "==>" : " +",
  1170. field->next_cpu,
  1171. field->next_pid,
  1172. field->next_prio,
  1173. T, comm);
  1174. break;
  1175. }
  1176. case TRACE_SPECIAL: {
  1177. struct special_entry *field;
  1178. trace_assign_type(field, entry);
  1179. trace_seq_printf(s, "# %ld %ld %ld\n",
  1180. field->arg1,
  1181. field->arg2,
  1182. field->arg3);
  1183. break;
  1184. }
  1185. case TRACE_STACK: {
  1186. struct stack_entry *field;
  1187. trace_assign_type(field, entry);
  1188. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1189. if (i)
  1190. trace_seq_puts(s, " <= ");
  1191. seq_print_ip_sym(s, field->caller[i], sym_flags);
  1192. }
  1193. trace_seq_puts(s, "\n");
  1194. break;
  1195. }
  1196. case TRACE_PRINT: {
  1197. struct print_entry *field;
  1198. trace_assign_type(field, entry);
  1199. seq_print_ip_sym(s, field->ip, sym_flags);
  1200. trace_seq_printf(s, ": %s", field->buf);
  1201. if (entry->flags & TRACE_FLAG_CONT)
  1202. trace_seq_print_cont(s, iter);
  1203. break;
  1204. }
  1205. default:
  1206. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1207. }
  1208. return TRACE_TYPE_HANDLED;
  1209. }
  1210. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1211. {
  1212. struct trace_seq *s = &iter->seq;
  1213. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1214. struct trace_entry *entry;
  1215. unsigned long usec_rem;
  1216. unsigned long long t;
  1217. unsigned long secs;
  1218. char *comm;
  1219. int ret;
  1220. int S, T;
  1221. int i;
  1222. entry = iter->ent;
  1223. if (entry->type == TRACE_CONT)
  1224. return TRACE_TYPE_HANDLED;
  1225. comm = trace_find_cmdline(iter->ent->pid);
  1226. t = ns2usecs(iter->ts);
  1227. usec_rem = do_div(t, 1000000ULL);
  1228. secs = (unsigned long)t;
  1229. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1230. if (!ret)
  1231. return TRACE_TYPE_PARTIAL_LINE;
  1232. ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
  1233. if (!ret)
  1234. return TRACE_TYPE_PARTIAL_LINE;
  1235. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1236. if (!ret)
  1237. return TRACE_TYPE_PARTIAL_LINE;
  1238. switch (entry->type) {
  1239. case TRACE_FN: {
  1240. struct ftrace_entry *field;
  1241. trace_assign_type(field, entry);
  1242. ret = seq_print_ip_sym(s, field->ip, sym_flags);
  1243. if (!ret)
  1244. return TRACE_TYPE_PARTIAL_LINE;
  1245. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1246. field->parent_ip) {
  1247. ret = trace_seq_printf(s, " <-");
  1248. if (!ret)
  1249. return TRACE_TYPE_PARTIAL_LINE;
  1250. ret = seq_print_ip_sym(s,
  1251. field->parent_ip,
  1252. sym_flags);
  1253. if (!ret)
  1254. return TRACE_TYPE_PARTIAL_LINE;
  1255. }
  1256. ret = trace_seq_printf(s, "\n");
  1257. if (!ret)
  1258. return TRACE_TYPE_PARTIAL_LINE;
  1259. break;
  1260. }
  1261. case TRACE_CTX:
  1262. case TRACE_WAKE: {
  1263. struct ctx_switch_entry *field;
  1264. trace_assign_type(field, entry);
  1265. S = field->prev_state < sizeof(state_to_char) ?
  1266. state_to_char[field->prev_state] : 'X';
  1267. T = field->next_state < sizeof(state_to_char) ?
  1268. state_to_char[field->next_state] : 'X';
  1269. ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
  1270. field->prev_pid,
  1271. field->prev_prio,
  1272. S,
  1273. entry->type == TRACE_CTX ? "==>" : " +",
  1274. field->next_cpu,
  1275. field->next_pid,
  1276. field->next_prio,
  1277. T);
  1278. if (!ret)
  1279. return TRACE_TYPE_PARTIAL_LINE;
  1280. break;
  1281. }
  1282. case TRACE_SPECIAL: {
  1283. struct special_entry *field;
  1284. trace_assign_type(field, entry);
  1285. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1286. field->arg1,
  1287. field->arg2,
  1288. field->arg3);
  1289. if (!ret)
  1290. return TRACE_TYPE_PARTIAL_LINE;
  1291. break;
  1292. }
  1293. case TRACE_STACK: {
  1294. struct stack_entry *field;
  1295. trace_assign_type(field, entry);
  1296. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1297. if (i) {
  1298. ret = trace_seq_puts(s, " <= ");
  1299. if (!ret)
  1300. return TRACE_TYPE_PARTIAL_LINE;
  1301. }
  1302. ret = seq_print_ip_sym(s, field->caller[i],
  1303. sym_flags);
  1304. if (!ret)
  1305. return TRACE_TYPE_PARTIAL_LINE;
  1306. }
  1307. ret = trace_seq_puts(s, "\n");
  1308. if (!ret)
  1309. return TRACE_TYPE_PARTIAL_LINE;
  1310. break;
  1311. }
  1312. case TRACE_PRINT: {
  1313. struct print_entry *field;
  1314. trace_assign_type(field, entry);
  1315. seq_print_ip_sym(s, field->ip, sym_flags);
  1316. trace_seq_printf(s, ": %s", field->buf);
  1317. if (entry->flags & TRACE_FLAG_CONT)
  1318. trace_seq_print_cont(s, iter);
  1319. break;
  1320. }
  1321. }
  1322. return TRACE_TYPE_HANDLED;
  1323. }
  1324. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1325. {
  1326. struct trace_seq *s = &iter->seq;
  1327. struct trace_entry *entry;
  1328. int ret;
  1329. int S, T;
  1330. entry = iter->ent;
  1331. if (entry->type == TRACE_CONT)
  1332. return TRACE_TYPE_HANDLED;
  1333. ret = trace_seq_printf(s, "%d %d %llu ",
  1334. entry->pid, iter->cpu, iter->ts);
  1335. if (!ret)
  1336. return TRACE_TYPE_PARTIAL_LINE;
  1337. switch (entry->type) {
  1338. case TRACE_FN: {
  1339. struct ftrace_entry *field;
  1340. trace_assign_type(field, entry);
  1341. ret = trace_seq_printf(s, "%x %x\n",
  1342. field->ip,
  1343. field->parent_ip);
  1344. if (!ret)
  1345. return TRACE_TYPE_PARTIAL_LINE;
  1346. break;
  1347. }
  1348. case TRACE_CTX:
  1349. case TRACE_WAKE: {
  1350. struct ctx_switch_entry *field;
  1351. trace_assign_type(field, entry);
  1352. S = field->prev_state < sizeof(state_to_char) ?
  1353. state_to_char[field->prev_state] : 'X';
  1354. T = field->next_state < sizeof(state_to_char) ?
  1355. state_to_char[field->next_state] : 'X';
  1356. if (entry->type == TRACE_WAKE)
  1357. S = '+';
  1358. ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
  1359. field->prev_pid,
  1360. field->prev_prio,
  1361. S,
  1362. field->next_cpu,
  1363. field->next_pid,
  1364. field->next_prio,
  1365. T);
  1366. if (!ret)
  1367. return TRACE_TYPE_PARTIAL_LINE;
  1368. break;
  1369. }
  1370. case TRACE_SPECIAL:
  1371. case TRACE_STACK: {
  1372. struct special_entry *field;
  1373. trace_assign_type(field, entry);
  1374. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1375. field->arg1,
  1376. field->arg2,
  1377. field->arg3);
  1378. if (!ret)
  1379. return TRACE_TYPE_PARTIAL_LINE;
  1380. break;
  1381. }
  1382. case TRACE_PRINT: {
  1383. struct print_entry *field;
  1384. trace_assign_type(field, entry);
  1385. trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
  1386. if (entry->flags & TRACE_FLAG_CONT)
  1387. trace_seq_print_cont(s, iter);
  1388. break;
  1389. }
  1390. }
  1391. return TRACE_TYPE_HANDLED;
  1392. }
  1393. #define SEQ_PUT_FIELD_RET(s, x) \
  1394. do { \
  1395. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1396. return 0; \
  1397. } while (0)
  1398. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1399. do { \
  1400. BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
  1401. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1402. return 0; \
  1403. } while (0)
  1404. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1405. {
  1406. struct trace_seq *s = &iter->seq;
  1407. unsigned char newline = '\n';
  1408. struct trace_entry *entry;
  1409. int S, T;
  1410. entry = iter->ent;
  1411. if (entry->type == TRACE_CONT)
  1412. return TRACE_TYPE_HANDLED;
  1413. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1414. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1415. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1416. switch (entry->type) {
  1417. case TRACE_FN: {
  1418. struct ftrace_entry *field;
  1419. trace_assign_type(field, entry);
  1420. SEQ_PUT_HEX_FIELD_RET(s, field->ip);
  1421. SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
  1422. break;
  1423. }
  1424. case TRACE_CTX:
  1425. case TRACE_WAKE: {
  1426. struct ctx_switch_entry *field;
  1427. trace_assign_type(field, entry);
  1428. S = field->prev_state < sizeof(state_to_char) ?
  1429. state_to_char[field->prev_state] : 'X';
  1430. T = field->next_state < sizeof(state_to_char) ?
  1431. state_to_char[field->next_state] : 'X';
  1432. if (entry->type == TRACE_WAKE)
  1433. S = '+';
  1434. SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
  1435. SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
  1436. SEQ_PUT_HEX_FIELD_RET(s, S);
  1437. SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
  1438. SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
  1439. SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
  1440. SEQ_PUT_HEX_FIELD_RET(s, T);
  1441. break;
  1442. }
  1443. case TRACE_SPECIAL:
  1444. case TRACE_STACK: {
  1445. struct special_entry *field;
  1446. trace_assign_type(field, entry);
  1447. SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
  1448. SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
  1449. SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
  1450. break;
  1451. }
  1452. }
  1453. SEQ_PUT_FIELD_RET(s, newline);
  1454. return TRACE_TYPE_HANDLED;
  1455. }
  1456. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1457. {
  1458. struct trace_seq *s = &iter->seq;
  1459. struct trace_entry *entry;
  1460. entry = iter->ent;
  1461. if (entry->type == TRACE_CONT)
  1462. return TRACE_TYPE_HANDLED;
  1463. SEQ_PUT_FIELD_RET(s, entry->pid);
  1464. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1465. SEQ_PUT_FIELD_RET(s, iter->ts);
  1466. switch (entry->type) {
  1467. case TRACE_FN: {
  1468. struct ftrace_entry *field;
  1469. trace_assign_type(field, entry);
  1470. SEQ_PUT_FIELD_RET(s, field->ip);
  1471. SEQ_PUT_FIELD_RET(s, field->parent_ip);
  1472. break;
  1473. }
  1474. case TRACE_CTX: {
  1475. struct ctx_switch_entry *field;
  1476. trace_assign_type(field, entry);
  1477. SEQ_PUT_FIELD_RET(s, field->prev_pid);
  1478. SEQ_PUT_FIELD_RET(s, field->prev_prio);
  1479. SEQ_PUT_FIELD_RET(s, field->prev_state);
  1480. SEQ_PUT_FIELD_RET(s, field->next_pid);
  1481. SEQ_PUT_FIELD_RET(s, field->next_prio);
  1482. SEQ_PUT_FIELD_RET(s, field->next_state);
  1483. break;
  1484. }
  1485. case TRACE_SPECIAL:
  1486. case TRACE_STACK: {
  1487. struct special_entry *field;
  1488. trace_assign_type(field, entry);
  1489. SEQ_PUT_FIELD_RET(s, field->arg1);
  1490. SEQ_PUT_FIELD_RET(s, field->arg2);
  1491. SEQ_PUT_FIELD_RET(s, field->arg3);
  1492. break;
  1493. }
  1494. }
  1495. return 1;
  1496. }
  1497. static int trace_empty(struct trace_iterator *iter)
  1498. {
  1499. int cpu;
  1500. for_each_tracing_cpu(cpu) {
  1501. if (iter->buffer_iter[cpu]) {
  1502. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1503. return 0;
  1504. } else {
  1505. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1506. return 0;
  1507. }
  1508. }
  1509. return 1;
  1510. }
  1511. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1512. {
  1513. enum print_line_t ret;
  1514. if (iter->trace && iter->trace->print_line) {
  1515. ret = iter->trace->print_line(iter);
  1516. if (ret != TRACE_TYPE_UNHANDLED)
  1517. return ret;
  1518. }
  1519. if (trace_flags & TRACE_ITER_BIN)
  1520. return print_bin_fmt(iter);
  1521. if (trace_flags & TRACE_ITER_HEX)
  1522. return print_hex_fmt(iter);
  1523. if (trace_flags & TRACE_ITER_RAW)
  1524. return print_raw_fmt(iter);
  1525. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1526. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1527. return print_trace_fmt(iter);
  1528. }
  1529. static int s_show(struct seq_file *m, void *v)
  1530. {
  1531. struct trace_iterator *iter = v;
  1532. if (iter->ent == NULL) {
  1533. if (iter->tr) {
  1534. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1535. seq_puts(m, "#\n");
  1536. }
  1537. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1538. /* print nothing if the buffers are empty */
  1539. if (trace_empty(iter))
  1540. return 0;
  1541. print_trace_header(m, iter);
  1542. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1543. print_lat_help_header(m);
  1544. } else {
  1545. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1546. print_func_help_header(m);
  1547. }
  1548. } else {
  1549. print_trace_line(iter);
  1550. trace_print_seq(m, &iter->seq);
  1551. }
  1552. return 0;
  1553. }
  1554. static struct seq_operations tracer_seq_ops = {
  1555. .start = s_start,
  1556. .next = s_next,
  1557. .stop = s_stop,
  1558. .show = s_show,
  1559. };
  1560. static struct trace_iterator *
  1561. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1562. {
  1563. struct trace_iterator *iter;
  1564. struct seq_file *m;
  1565. int cpu;
  1566. if (tracing_disabled) {
  1567. *ret = -ENODEV;
  1568. return NULL;
  1569. }
  1570. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1571. if (!iter) {
  1572. *ret = -ENOMEM;
  1573. goto out;
  1574. }
  1575. mutex_lock(&trace_types_lock);
  1576. if (current_trace && current_trace->print_max)
  1577. iter->tr = &max_tr;
  1578. else
  1579. iter->tr = inode->i_private;
  1580. iter->trace = current_trace;
  1581. iter->pos = -1;
  1582. for_each_tracing_cpu(cpu) {
  1583. iter->buffer_iter[cpu] =
  1584. ring_buffer_read_start(iter->tr->buffer, cpu);
  1585. if (!iter->buffer_iter[cpu])
  1586. goto fail_buffer;
  1587. }
  1588. /* TODO stop tracer */
  1589. *ret = seq_open(file, &tracer_seq_ops);
  1590. if (*ret)
  1591. goto fail_buffer;
  1592. m = file->private_data;
  1593. m->private = iter;
  1594. /* stop the trace while dumping */
  1595. if (iter->tr->ctrl) {
  1596. tracer_enabled = 0;
  1597. ftrace_function_enabled = 0;
  1598. }
  1599. if (iter->trace && iter->trace->open)
  1600. iter->trace->open(iter);
  1601. mutex_unlock(&trace_types_lock);
  1602. out:
  1603. return iter;
  1604. fail_buffer:
  1605. for_each_tracing_cpu(cpu) {
  1606. if (iter->buffer_iter[cpu])
  1607. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1608. }
  1609. mutex_unlock(&trace_types_lock);
  1610. kfree(iter);
  1611. return ERR_PTR(-ENOMEM);
  1612. }
  1613. int tracing_open_generic(struct inode *inode, struct file *filp)
  1614. {
  1615. if (tracing_disabled)
  1616. return -ENODEV;
  1617. filp->private_data = inode->i_private;
  1618. return 0;
  1619. }
  1620. int tracing_release(struct inode *inode, struct file *file)
  1621. {
  1622. struct seq_file *m = (struct seq_file *)file->private_data;
  1623. struct trace_iterator *iter = m->private;
  1624. int cpu;
  1625. mutex_lock(&trace_types_lock);
  1626. for_each_tracing_cpu(cpu) {
  1627. if (iter->buffer_iter[cpu])
  1628. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1629. }
  1630. if (iter->trace && iter->trace->close)
  1631. iter->trace->close(iter);
  1632. /* reenable tracing if it was previously enabled */
  1633. if (iter->tr->ctrl) {
  1634. tracer_enabled = 1;
  1635. /*
  1636. * It is safe to enable function tracing even if it
  1637. * isn't used
  1638. */
  1639. ftrace_function_enabled = 1;
  1640. }
  1641. mutex_unlock(&trace_types_lock);
  1642. seq_release(inode, file);
  1643. kfree(iter);
  1644. return 0;
  1645. }
  1646. static int tracing_open(struct inode *inode, struct file *file)
  1647. {
  1648. int ret;
  1649. __tracing_open(inode, file, &ret);
  1650. return ret;
  1651. }
  1652. static int tracing_lt_open(struct inode *inode, struct file *file)
  1653. {
  1654. struct trace_iterator *iter;
  1655. int ret;
  1656. iter = __tracing_open(inode, file, &ret);
  1657. if (!ret)
  1658. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1659. return ret;
  1660. }
  1661. static void *
  1662. t_next(struct seq_file *m, void *v, loff_t *pos)
  1663. {
  1664. struct tracer *t = m->private;
  1665. (*pos)++;
  1666. if (t)
  1667. t = t->next;
  1668. m->private = t;
  1669. return t;
  1670. }
  1671. static void *t_start(struct seq_file *m, loff_t *pos)
  1672. {
  1673. struct tracer *t = m->private;
  1674. loff_t l = 0;
  1675. mutex_lock(&trace_types_lock);
  1676. for (; t && l < *pos; t = t_next(m, t, &l))
  1677. ;
  1678. return t;
  1679. }
  1680. static void t_stop(struct seq_file *m, void *p)
  1681. {
  1682. mutex_unlock(&trace_types_lock);
  1683. }
  1684. static int t_show(struct seq_file *m, void *v)
  1685. {
  1686. struct tracer *t = v;
  1687. if (!t)
  1688. return 0;
  1689. seq_printf(m, "%s", t->name);
  1690. if (t->next)
  1691. seq_putc(m, ' ');
  1692. else
  1693. seq_putc(m, '\n');
  1694. return 0;
  1695. }
  1696. static struct seq_operations show_traces_seq_ops = {
  1697. .start = t_start,
  1698. .next = t_next,
  1699. .stop = t_stop,
  1700. .show = t_show,
  1701. };
  1702. static int show_traces_open(struct inode *inode, struct file *file)
  1703. {
  1704. int ret;
  1705. if (tracing_disabled)
  1706. return -ENODEV;
  1707. ret = seq_open(file, &show_traces_seq_ops);
  1708. if (!ret) {
  1709. struct seq_file *m = file->private_data;
  1710. m->private = trace_types;
  1711. }
  1712. return ret;
  1713. }
  1714. static struct file_operations tracing_fops = {
  1715. .open = tracing_open,
  1716. .read = seq_read,
  1717. .llseek = seq_lseek,
  1718. .release = tracing_release,
  1719. };
  1720. static struct file_operations tracing_lt_fops = {
  1721. .open = tracing_lt_open,
  1722. .read = seq_read,
  1723. .llseek = seq_lseek,
  1724. .release = tracing_release,
  1725. };
  1726. static struct file_operations show_traces_fops = {
  1727. .open = show_traces_open,
  1728. .read = seq_read,
  1729. .release = seq_release,
  1730. };
  1731. /*
  1732. * Only trace on a CPU if the bitmask is set:
  1733. */
  1734. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1735. /*
  1736. * When tracing/tracing_cpu_mask is modified then this holds
  1737. * the new bitmask we are about to install:
  1738. */
  1739. static cpumask_t tracing_cpumask_new;
  1740. /*
  1741. * The tracer itself will not take this lock, but still we want
  1742. * to provide a consistent cpumask to user-space:
  1743. */
  1744. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1745. /*
  1746. * Temporary storage for the character representation of the
  1747. * CPU bitmask (and one more byte for the newline):
  1748. */
  1749. static char mask_str[NR_CPUS + 1];
  1750. static ssize_t
  1751. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1752. size_t count, loff_t *ppos)
  1753. {
  1754. int len;
  1755. mutex_lock(&tracing_cpumask_update_lock);
  1756. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1757. if (count - len < 2) {
  1758. count = -EINVAL;
  1759. goto out_err;
  1760. }
  1761. len += sprintf(mask_str + len, "\n");
  1762. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1763. out_err:
  1764. mutex_unlock(&tracing_cpumask_update_lock);
  1765. return count;
  1766. }
  1767. static ssize_t
  1768. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1769. size_t count, loff_t *ppos)
  1770. {
  1771. int err, cpu;
  1772. mutex_lock(&tracing_cpumask_update_lock);
  1773. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1774. if (err)
  1775. goto err_unlock;
  1776. raw_local_irq_disable();
  1777. __raw_spin_lock(&ftrace_max_lock);
  1778. for_each_tracing_cpu(cpu) {
  1779. /*
  1780. * Increase/decrease the disabled counter if we are
  1781. * about to flip a bit in the cpumask:
  1782. */
  1783. if (cpu_isset(cpu, tracing_cpumask) &&
  1784. !cpu_isset(cpu, tracing_cpumask_new)) {
  1785. atomic_inc(&global_trace.data[cpu]->disabled);
  1786. }
  1787. if (!cpu_isset(cpu, tracing_cpumask) &&
  1788. cpu_isset(cpu, tracing_cpumask_new)) {
  1789. atomic_dec(&global_trace.data[cpu]->disabled);
  1790. }
  1791. }
  1792. __raw_spin_unlock(&ftrace_max_lock);
  1793. raw_local_irq_enable();
  1794. tracing_cpumask = tracing_cpumask_new;
  1795. mutex_unlock(&tracing_cpumask_update_lock);
  1796. return count;
  1797. err_unlock:
  1798. mutex_unlock(&tracing_cpumask_update_lock);
  1799. return err;
  1800. }
  1801. static struct file_operations tracing_cpumask_fops = {
  1802. .open = tracing_open_generic,
  1803. .read = tracing_cpumask_read,
  1804. .write = tracing_cpumask_write,
  1805. };
  1806. static ssize_t
  1807. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1808. size_t cnt, loff_t *ppos)
  1809. {
  1810. char *buf;
  1811. int r = 0;
  1812. int len = 0;
  1813. int i;
  1814. /* calulate max size */
  1815. for (i = 0; trace_options[i]; i++) {
  1816. len += strlen(trace_options[i]);
  1817. len += 3; /* "no" and space */
  1818. }
  1819. /* +2 for \n and \0 */
  1820. buf = kmalloc(len + 2, GFP_KERNEL);
  1821. if (!buf)
  1822. return -ENOMEM;
  1823. for (i = 0; trace_options[i]; i++) {
  1824. if (trace_flags & (1 << i))
  1825. r += sprintf(buf + r, "%s ", trace_options[i]);
  1826. else
  1827. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1828. }
  1829. r += sprintf(buf + r, "\n");
  1830. WARN_ON(r >= len + 2);
  1831. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1832. kfree(buf);
  1833. return r;
  1834. }
  1835. static ssize_t
  1836. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1837. size_t cnt, loff_t *ppos)
  1838. {
  1839. char buf[64];
  1840. char *cmp = buf;
  1841. int neg = 0;
  1842. int i;
  1843. if (cnt >= sizeof(buf))
  1844. return -EINVAL;
  1845. if (copy_from_user(&buf, ubuf, cnt))
  1846. return -EFAULT;
  1847. buf[cnt] = 0;
  1848. if (strncmp(buf, "no", 2) == 0) {
  1849. neg = 1;
  1850. cmp += 2;
  1851. }
  1852. for (i = 0; trace_options[i]; i++) {
  1853. int len = strlen(trace_options[i]);
  1854. if (strncmp(cmp, trace_options[i], len) == 0) {
  1855. if (neg)
  1856. trace_flags &= ~(1 << i);
  1857. else
  1858. trace_flags |= (1 << i);
  1859. break;
  1860. }
  1861. }
  1862. /*
  1863. * If no option could be set, return an error:
  1864. */
  1865. if (!trace_options[i])
  1866. return -EINVAL;
  1867. filp->f_pos += cnt;
  1868. return cnt;
  1869. }
  1870. static struct file_operations tracing_iter_fops = {
  1871. .open = tracing_open_generic,
  1872. .read = tracing_iter_ctrl_read,
  1873. .write = tracing_iter_ctrl_write,
  1874. };
  1875. static const char readme_msg[] =
  1876. "tracing mini-HOWTO:\n\n"
  1877. "# mkdir /debug\n"
  1878. "# mount -t debugfs nodev /debug\n\n"
  1879. "# cat /debug/tracing/available_tracers\n"
  1880. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1881. "# cat /debug/tracing/current_tracer\n"
  1882. "none\n"
  1883. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1884. "# cat /debug/tracing/current_tracer\n"
  1885. "sched_switch\n"
  1886. "# cat /debug/tracing/iter_ctrl\n"
  1887. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1888. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1889. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1890. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1891. "echo 0 > /debug/tracing/tracing_enabled\n"
  1892. ;
  1893. static ssize_t
  1894. tracing_readme_read(struct file *filp, char __user *ubuf,
  1895. size_t cnt, loff_t *ppos)
  1896. {
  1897. return simple_read_from_buffer(ubuf, cnt, ppos,
  1898. readme_msg, strlen(readme_msg));
  1899. }
  1900. static struct file_operations tracing_readme_fops = {
  1901. .open = tracing_open_generic,
  1902. .read = tracing_readme_read,
  1903. };
  1904. static ssize_t
  1905. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1906. size_t cnt, loff_t *ppos)
  1907. {
  1908. struct trace_array *tr = filp->private_data;
  1909. char buf[64];
  1910. int r;
  1911. r = sprintf(buf, "%ld\n", tr->ctrl);
  1912. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1913. }
  1914. static ssize_t
  1915. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1916. size_t cnt, loff_t *ppos)
  1917. {
  1918. struct trace_array *tr = filp->private_data;
  1919. char buf[64];
  1920. long val;
  1921. int ret;
  1922. if (cnt >= sizeof(buf))
  1923. return -EINVAL;
  1924. if (copy_from_user(&buf, ubuf, cnt))
  1925. return -EFAULT;
  1926. buf[cnt] = 0;
  1927. ret = strict_strtoul(buf, 10, &val);
  1928. if (ret < 0)
  1929. return ret;
  1930. val = !!val;
  1931. mutex_lock(&trace_types_lock);
  1932. if (tr->ctrl ^ val) {
  1933. if (val)
  1934. tracer_enabled = 1;
  1935. else
  1936. tracer_enabled = 0;
  1937. tr->ctrl = val;
  1938. if (current_trace && current_trace->ctrl_update)
  1939. current_trace->ctrl_update(tr);
  1940. }
  1941. mutex_unlock(&trace_types_lock);
  1942. filp->f_pos += cnt;
  1943. return cnt;
  1944. }
  1945. static ssize_t
  1946. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1947. size_t cnt, loff_t *ppos)
  1948. {
  1949. char buf[max_tracer_type_len+2];
  1950. int r;
  1951. mutex_lock(&trace_types_lock);
  1952. if (current_trace)
  1953. r = sprintf(buf, "%s\n", current_trace->name);
  1954. else
  1955. r = sprintf(buf, "\n");
  1956. mutex_unlock(&trace_types_lock);
  1957. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1958. }
  1959. static ssize_t
  1960. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1961. size_t cnt, loff_t *ppos)
  1962. {
  1963. struct trace_array *tr = &global_trace;
  1964. struct tracer *t;
  1965. char buf[max_tracer_type_len+1];
  1966. int i;
  1967. size_t ret;
  1968. ret = cnt;
  1969. if (cnt > max_tracer_type_len)
  1970. cnt = max_tracer_type_len;
  1971. if (copy_from_user(&buf, ubuf, cnt))
  1972. return -EFAULT;
  1973. buf[cnt] = 0;
  1974. /* strip ending whitespace. */
  1975. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1976. buf[i] = 0;
  1977. mutex_lock(&trace_types_lock);
  1978. for (t = trace_types; t; t = t->next) {
  1979. if (strcmp(t->name, buf) == 0)
  1980. break;
  1981. }
  1982. if (!t) {
  1983. ret = -EINVAL;
  1984. goto out;
  1985. }
  1986. if (t == current_trace)
  1987. goto out;
  1988. if (current_trace && current_trace->reset)
  1989. current_trace->reset(tr);
  1990. current_trace = t;
  1991. if (t->init)
  1992. t->init(tr);
  1993. out:
  1994. mutex_unlock(&trace_types_lock);
  1995. if (ret > 0)
  1996. filp->f_pos += ret;
  1997. return ret;
  1998. }
  1999. static ssize_t
  2000. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2001. size_t cnt, loff_t *ppos)
  2002. {
  2003. unsigned long *ptr = filp->private_data;
  2004. char buf[64];
  2005. int r;
  2006. r = snprintf(buf, sizeof(buf), "%ld\n",
  2007. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2008. if (r > sizeof(buf))
  2009. r = sizeof(buf);
  2010. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2011. }
  2012. static ssize_t
  2013. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2014. size_t cnt, loff_t *ppos)
  2015. {
  2016. long *ptr = filp->private_data;
  2017. char buf[64];
  2018. long val;
  2019. int ret;
  2020. if (cnt >= sizeof(buf))
  2021. return -EINVAL;
  2022. if (copy_from_user(&buf, ubuf, cnt))
  2023. return -EFAULT;
  2024. buf[cnt] = 0;
  2025. ret = strict_strtoul(buf, 10, &val);
  2026. if (ret < 0)
  2027. return ret;
  2028. *ptr = val * 1000;
  2029. return cnt;
  2030. }
  2031. static atomic_t tracing_reader;
  2032. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2033. {
  2034. struct trace_iterator *iter;
  2035. if (tracing_disabled)
  2036. return -ENODEV;
  2037. /* We only allow for reader of the pipe */
  2038. if (atomic_inc_return(&tracing_reader) != 1) {
  2039. atomic_dec(&tracing_reader);
  2040. return -EBUSY;
  2041. }
  2042. /* create a buffer to store the information to pass to userspace */
  2043. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2044. if (!iter)
  2045. return -ENOMEM;
  2046. mutex_lock(&trace_types_lock);
  2047. iter->tr = &global_trace;
  2048. iter->trace = current_trace;
  2049. filp->private_data = iter;
  2050. if (iter->trace->pipe_open)
  2051. iter->trace->pipe_open(iter);
  2052. mutex_unlock(&trace_types_lock);
  2053. return 0;
  2054. }
  2055. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2056. {
  2057. struct trace_iterator *iter = file->private_data;
  2058. kfree(iter);
  2059. atomic_dec(&tracing_reader);
  2060. return 0;
  2061. }
  2062. static unsigned int
  2063. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2064. {
  2065. struct trace_iterator *iter = filp->private_data;
  2066. if (trace_flags & TRACE_ITER_BLOCK) {
  2067. /*
  2068. * Always select as readable when in blocking mode
  2069. */
  2070. return POLLIN | POLLRDNORM;
  2071. } else {
  2072. if (!trace_empty(iter))
  2073. return POLLIN | POLLRDNORM;
  2074. poll_wait(filp, &trace_wait, poll_table);
  2075. if (!trace_empty(iter))
  2076. return POLLIN | POLLRDNORM;
  2077. return 0;
  2078. }
  2079. }
  2080. /*
  2081. * Consumer reader.
  2082. */
  2083. static ssize_t
  2084. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2085. size_t cnt, loff_t *ppos)
  2086. {
  2087. struct trace_iterator *iter = filp->private_data;
  2088. ssize_t sret;
  2089. /* return any leftover data */
  2090. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2091. if (sret != -EBUSY)
  2092. return sret;
  2093. trace_seq_reset(&iter->seq);
  2094. mutex_lock(&trace_types_lock);
  2095. if (iter->trace->read) {
  2096. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2097. if (sret)
  2098. goto out;
  2099. }
  2100. waitagain:
  2101. sret = 0;
  2102. while (trace_empty(iter)) {
  2103. if ((filp->f_flags & O_NONBLOCK)) {
  2104. sret = -EAGAIN;
  2105. goto out;
  2106. }
  2107. /*
  2108. * This is a make-shift waitqueue. The reason we don't use
  2109. * an actual wait queue is because:
  2110. * 1) we only ever have one waiter
  2111. * 2) the tracing, traces all functions, we don't want
  2112. * the overhead of calling wake_up and friends
  2113. * (and tracing them too)
  2114. * Anyway, this is really very primitive wakeup.
  2115. */
  2116. set_current_state(TASK_INTERRUPTIBLE);
  2117. iter->tr->waiter = current;
  2118. mutex_unlock(&trace_types_lock);
  2119. /* sleep for 100 msecs, and try again. */
  2120. schedule_timeout(HZ/10);
  2121. mutex_lock(&trace_types_lock);
  2122. iter->tr->waiter = NULL;
  2123. if (signal_pending(current)) {
  2124. sret = -EINTR;
  2125. goto out;
  2126. }
  2127. if (iter->trace != current_trace)
  2128. goto out;
  2129. /*
  2130. * We block until we read something and tracing is disabled.
  2131. * We still block if tracing is disabled, but we have never
  2132. * read anything. This allows a user to cat this file, and
  2133. * then enable tracing. But after we have read something,
  2134. * we give an EOF when tracing is again disabled.
  2135. *
  2136. * iter->pos will be 0 if we haven't read anything.
  2137. */
  2138. if (!tracer_enabled && iter->pos)
  2139. break;
  2140. continue;
  2141. }
  2142. /* stop when tracing is finished */
  2143. if (trace_empty(iter))
  2144. goto out;
  2145. if (cnt >= PAGE_SIZE)
  2146. cnt = PAGE_SIZE - 1;
  2147. /* reset all but tr, trace, and overruns */
  2148. memset(&iter->seq, 0,
  2149. sizeof(struct trace_iterator) -
  2150. offsetof(struct trace_iterator, seq));
  2151. iter->pos = -1;
  2152. while (find_next_entry_inc(iter) != NULL) {
  2153. enum print_line_t ret;
  2154. int len = iter->seq.len;
  2155. ret = print_trace_line(iter);
  2156. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2157. /* don't print partial lines */
  2158. iter->seq.len = len;
  2159. break;
  2160. }
  2161. trace_consume(iter);
  2162. if (iter->seq.len >= cnt)
  2163. break;
  2164. }
  2165. /* Now copy what we have to the user */
  2166. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2167. if (iter->seq.readpos >= iter->seq.len)
  2168. trace_seq_reset(&iter->seq);
  2169. /*
  2170. * If there was nothing to send to user, inspite of consuming trace
  2171. * entries, go back to wait for more entries.
  2172. */
  2173. if (sret == -EBUSY)
  2174. goto waitagain;
  2175. out:
  2176. mutex_unlock(&trace_types_lock);
  2177. return sret;
  2178. }
  2179. static ssize_t
  2180. tracing_entries_read(struct file *filp, char __user *ubuf,
  2181. size_t cnt, loff_t *ppos)
  2182. {
  2183. struct trace_array *tr = filp->private_data;
  2184. char buf[64];
  2185. int r;
  2186. r = sprintf(buf, "%lu\n", tr->entries);
  2187. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2188. }
  2189. static ssize_t
  2190. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2191. size_t cnt, loff_t *ppos)
  2192. {
  2193. unsigned long val;
  2194. char buf[64];
  2195. int ret, cpu;
  2196. struct trace_array *tr = filp->private_data;
  2197. if (cnt >= sizeof(buf))
  2198. return -EINVAL;
  2199. if (copy_from_user(&buf, ubuf, cnt))
  2200. return -EFAULT;
  2201. buf[cnt] = 0;
  2202. ret = strict_strtoul(buf, 10, &val);
  2203. if (ret < 0)
  2204. return ret;
  2205. /* must have at least 1 entry */
  2206. if (!val)
  2207. return -EINVAL;
  2208. mutex_lock(&trace_types_lock);
  2209. if (tr->ctrl) {
  2210. cnt = -EBUSY;
  2211. pr_info("ftrace: please disable tracing"
  2212. " before modifying buffer size\n");
  2213. goto out;
  2214. }
  2215. /* disable all cpu buffers */
  2216. for_each_tracing_cpu(cpu) {
  2217. if (global_trace.data[cpu])
  2218. atomic_inc(&global_trace.data[cpu]->disabled);
  2219. if (max_tr.data[cpu])
  2220. atomic_inc(&max_tr.data[cpu]->disabled);
  2221. }
  2222. if (val != global_trace.entries) {
  2223. ret = ring_buffer_resize(global_trace.buffer, val);
  2224. if (ret < 0) {
  2225. cnt = ret;
  2226. goto out;
  2227. }
  2228. ret = ring_buffer_resize(max_tr.buffer, val);
  2229. if (ret < 0) {
  2230. int r;
  2231. cnt = ret;
  2232. r = ring_buffer_resize(global_trace.buffer,
  2233. global_trace.entries);
  2234. if (r < 0) {
  2235. /* AARGH! We are left with different
  2236. * size max buffer!!!! */
  2237. WARN_ON(1);
  2238. tracing_disabled = 1;
  2239. }
  2240. goto out;
  2241. }
  2242. global_trace.entries = val;
  2243. }
  2244. filp->f_pos += cnt;
  2245. /* If check pages failed, return ENOMEM */
  2246. if (tracing_disabled)
  2247. cnt = -ENOMEM;
  2248. out:
  2249. for_each_tracing_cpu(cpu) {
  2250. if (global_trace.data[cpu])
  2251. atomic_dec(&global_trace.data[cpu]->disabled);
  2252. if (max_tr.data[cpu])
  2253. atomic_dec(&max_tr.data[cpu]->disabled);
  2254. }
  2255. max_tr.entries = global_trace.entries;
  2256. mutex_unlock(&trace_types_lock);
  2257. return cnt;
  2258. }
  2259. static int mark_printk(const char *fmt, ...)
  2260. {
  2261. int ret;
  2262. va_list args;
  2263. va_start(args, fmt);
  2264. ret = trace_vprintk(0, fmt, args);
  2265. va_end(args);
  2266. return ret;
  2267. }
  2268. static ssize_t
  2269. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2270. size_t cnt, loff_t *fpos)
  2271. {
  2272. char *buf;
  2273. char *end;
  2274. struct trace_array *tr = &global_trace;
  2275. if (!tr->ctrl || tracing_disabled)
  2276. return -EINVAL;
  2277. if (cnt > TRACE_BUF_SIZE)
  2278. cnt = TRACE_BUF_SIZE;
  2279. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2280. if (buf == NULL)
  2281. return -ENOMEM;
  2282. if (copy_from_user(buf, ubuf, cnt)) {
  2283. kfree(buf);
  2284. return -EFAULT;
  2285. }
  2286. /* Cut from the first nil or newline. */
  2287. buf[cnt] = '\0';
  2288. end = strchr(buf, '\n');
  2289. if (end)
  2290. *end = '\0';
  2291. cnt = mark_printk("%s\n", buf);
  2292. kfree(buf);
  2293. *fpos += cnt;
  2294. return cnt;
  2295. }
  2296. static struct file_operations tracing_max_lat_fops = {
  2297. .open = tracing_open_generic,
  2298. .read = tracing_max_lat_read,
  2299. .write = tracing_max_lat_write,
  2300. };
  2301. static struct file_operations tracing_ctrl_fops = {
  2302. .open = tracing_open_generic,
  2303. .read = tracing_ctrl_read,
  2304. .write = tracing_ctrl_write,
  2305. };
  2306. static struct file_operations set_tracer_fops = {
  2307. .open = tracing_open_generic,
  2308. .read = tracing_set_trace_read,
  2309. .write = tracing_set_trace_write,
  2310. };
  2311. static struct file_operations tracing_pipe_fops = {
  2312. .open = tracing_open_pipe,
  2313. .poll = tracing_poll_pipe,
  2314. .read = tracing_read_pipe,
  2315. .release = tracing_release_pipe,
  2316. };
  2317. static struct file_operations tracing_entries_fops = {
  2318. .open = tracing_open_generic,
  2319. .read = tracing_entries_read,
  2320. .write = tracing_entries_write,
  2321. };
  2322. static struct file_operations tracing_mark_fops = {
  2323. .open = tracing_open_generic,
  2324. .write = tracing_mark_write,
  2325. };
  2326. #ifdef CONFIG_DYNAMIC_FTRACE
  2327. static ssize_t
  2328. tracing_read_long(struct file *filp, char __user *ubuf,
  2329. size_t cnt, loff_t *ppos)
  2330. {
  2331. unsigned long *p = filp->private_data;
  2332. char buf[64];
  2333. int r;
  2334. r = sprintf(buf, "%ld\n", *p);
  2335. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2336. }
  2337. static struct file_operations tracing_read_long_fops = {
  2338. .open = tracing_open_generic,
  2339. .read = tracing_read_long,
  2340. };
  2341. #endif
  2342. static struct dentry *d_tracer;
  2343. struct dentry *tracing_init_dentry(void)
  2344. {
  2345. static int once;
  2346. if (d_tracer)
  2347. return d_tracer;
  2348. d_tracer = debugfs_create_dir("tracing", NULL);
  2349. if (!d_tracer && !once) {
  2350. once = 1;
  2351. pr_warning("Could not create debugfs directory 'tracing'\n");
  2352. return NULL;
  2353. }
  2354. return d_tracer;
  2355. }
  2356. #ifdef CONFIG_FTRACE_SELFTEST
  2357. /* Let selftest have access to static functions in this file */
  2358. #include "trace_selftest.c"
  2359. #endif
  2360. static __init int tracer_init_debugfs(void)
  2361. {
  2362. struct dentry *d_tracer;
  2363. struct dentry *entry;
  2364. d_tracer = tracing_init_dentry();
  2365. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2366. &global_trace, &tracing_ctrl_fops);
  2367. if (!entry)
  2368. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2369. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2370. NULL, &tracing_iter_fops);
  2371. if (!entry)
  2372. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2373. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2374. NULL, &tracing_cpumask_fops);
  2375. if (!entry)
  2376. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2377. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2378. &global_trace, &tracing_lt_fops);
  2379. if (!entry)
  2380. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2381. entry = debugfs_create_file("trace", 0444, d_tracer,
  2382. &global_trace, &tracing_fops);
  2383. if (!entry)
  2384. pr_warning("Could not create debugfs 'trace' entry\n");
  2385. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2386. &global_trace, &show_traces_fops);
  2387. if (!entry)
  2388. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  2389. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2390. &global_trace, &set_tracer_fops);
  2391. if (!entry)
  2392. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  2393. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2394. &tracing_max_latency,
  2395. &tracing_max_lat_fops);
  2396. if (!entry)
  2397. pr_warning("Could not create debugfs "
  2398. "'tracing_max_latency' entry\n");
  2399. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2400. &tracing_thresh, &tracing_max_lat_fops);
  2401. if (!entry)
  2402. pr_warning("Could not create debugfs "
  2403. "'tracing_thresh' entry\n");
  2404. entry = debugfs_create_file("README", 0644, d_tracer,
  2405. NULL, &tracing_readme_fops);
  2406. if (!entry)
  2407. pr_warning("Could not create debugfs 'README' entry\n");
  2408. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2409. NULL, &tracing_pipe_fops);
  2410. if (!entry)
  2411. pr_warning("Could not create debugfs "
  2412. "'trace_pipe' entry\n");
  2413. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2414. &global_trace, &tracing_entries_fops);
  2415. if (!entry)
  2416. pr_warning("Could not create debugfs "
  2417. "'trace_entries' entry\n");
  2418. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  2419. NULL, &tracing_mark_fops);
  2420. if (!entry)
  2421. pr_warning("Could not create debugfs "
  2422. "'trace_marker' entry\n");
  2423. #ifdef CONFIG_DYNAMIC_FTRACE
  2424. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2425. &ftrace_update_tot_cnt,
  2426. &tracing_read_long_fops);
  2427. if (!entry)
  2428. pr_warning("Could not create debugfs "
  2429. "'dyn_ftrace_total_info' entry\n");
  2430. #endif
  2431. #ifdef CONFIG_SYSPROF_TRACER
  2432. init_tracer_sysprof_debugfs(d_tracer);
  2433. #endif
  2434. return 0;
  2435. }
  2436. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  2437. {
  2438. static DEFINE_SPINLOCK(trace_buf_lock);
  2439. static char trace_buf[TRACE_BUF_SIZE];
  2440. struct ring_buffer_event *event;
  2441. struct trace_array *tr = &global_trace;
  2442. struct trace_array_cpu *data;
  2443. struct print_entry *entry;
  2444. unsigned long flags, irq_flags;
  2445. int cpu, len = 0, size, pc;
  2446. if (!tr->ctrl || tracing_disabled)
  2447. return 0;
  2448. pc = preempt_count();
  2449. preempt_disable_notrace();
  2450. cpu = raw_smp_processor_id();
  2451. data = tr->data[cpu];
  2452. if (unlikely(atomic_read(&data->disabled)))
  2453. goto out;
  2454. spin_lock_irqsave(&trace_buf_lock, flags);
  2455. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  2456. len = min(len, TRACE_BUF_SIZE-1);
  2457. trace_buf[len] = 0;
  2458. size = sizeof(*entry) + len + 1;
  2459. event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
  2460. if (!event)
  2461. goto out_unlock;
  2462. entry = ring_buffer_event_data(event);
  2463. tracing_generic_entry_update(&entry->ent, flags, pc);
  2464. entry->ent.type = TRACE_PRINT;
  2465. entry->ip = ip;
  2466. memcpy(&entry->buf, trace_buf, len);
  2467. entry->buf[len] = 0;
  2468. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  2469. out_unlock:
  2470. spin_unlock_irqrestore(&trace_buf_lock, flags);
  2471. out:
  2472. preempt_enable_notrace();
  2473. return len;
  2474. }
  2475. EXPORT_SYMBOL_GPL(trace_vprintk);
  2476. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2477. {
  2478. int ret;
  2479. va_list ap;
  2480. if (!(trace_flags & TRACE_ITER_PRINTK))
  2481. return 0;
  2482. va_start(ap, fmt);
  2483. ret = trace_vprintk(ip, fmt, ap);
  2484. va_end(ap);
  2485. return ret;
  2486. }
  2487. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2488. static int trace_panic_handler(struct notifier_block *this,
  2489. unsigned long event, void *unused)
  2490. {
  2491. ftrace_dump();
  2492. return NOTIFY_OK;
  2493. }
  2494. static struct notifier_block trace_panic_notifier = {
  2495. .notifier_call = trace_panic_handler,
  2496. .next = NULL,
  2497. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  2498. };
  2499. static int trace_die_handler(struct notifier_block *self,
  2500. unsigned long val,
  2501. void *data)
  2502. {
  2503. switch (val) {
  2504. case DIE_OOPS:
  2505. ftrace_dump();
  2506. break;
  2507. default:
  2508. break;
  2509. }
  2510. return NOTIFY_OK;
  2511. }
  2512. static struct notifier_block trace_die_notifier = {
  2513. .notifier_call = trace_die_handler,
  2514. .priority = 200
  2515. };
  2516. /*
  2517. * printk is set to max of 1024, we really don't need it that big.
  2518. * Nothing should be printing 1000 characters anyway.
  2519. */
  2520. #define TRACE_MAX_PRINT 1000
  2521. /*
  2522. * Define here KERN_TRACE so that we have one place to modify
  2523. * it if we decide to change what log level the ftrace dump
  2524. * should be at.
  2525. */
  2526. #define KERN_TRACE KERN_INFO
  2527. static void
  2528. trace_printk_seq(struct trace_seq *s)
  2529. {
  2530. /* Probably should print a warning here. */
  2531. if (s->len >= 1000)
  2532. s->len = 1000;
  2533. /* should be zero ended, but we are paranoid. */
  2534. s->buffer[s->len] = 0;
  2535. printk(KERN_TRACE "%s", s->buffer);
  2536. trace_seq_reset(s);
  2537. }
  2538. void ftrace_dump(void)
  2539. {
  2540. static DEFINE_SPINLOCK(ftrace_dump_lock);
  2541. /* use static because iter can be a bit big for the stack */
  2542. static struct trace_iterator iter;
  2543. static cpumask_t mask;
  2544. static int dump_ran;
  2545. unsigned long flags;
  2546. int cnt = 0, cpu;
  2547. /* only one dump */
  2548. spin_lock_irqsave(&ftrace_dump_lock, flags);
  2549. if (dump_ran)
  2550. goto out;
  2551. dump_ran = 1;
  2552. /* No turning back! */
  2553. ftrace_kill();
  2554. for_each_tracing_cpu(cpu) {
  2555. atomic_inc(&global_trace.data[cpu]->disabled);
  2556. }
  2557. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  2558. iter.tr = &global_trace;
  2559. iter.trace = current_trace;
  2560. /*
  2561. * We need to stop all tracing on all CPUS to read the
  2562. * the next buffer. This is a bit expensive, but is
  2563. * not done often. We fill all what we can read,
  2564. * and then release the locks again.
  2565. */
  2566. cpus_clear(mask);
  2567. while (!trace_empty(&iter)) {
  2568. if (!cnt)
  2569. printk(KERN_TRACE "---------------------------------\n");
  2570. cnt++;
  2571. /* reset all but tr, trace, and overruns */
  2572. memset(&iter.seq, 0,
  2573. sizeof(struct trace_iterator) -
  2574. offsetof(struct trace_iterator, seq));
  2575. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  2576. iter.pos = -1;
  2577. if (find_next_entry_inc(&iter) != NULL) {
  2578. print_trace_line(&iter);
  2579. trace_consume(&iter);
  2580. }
  2581. trace_printk_seq(&iter.seq);
  2582. }
  2583. if (!cnt)
  2584. printk(KERN_TRACE " (ftrace buffer empty)\n");
  2585. else
  2586. printk(KERN_TRACE "---------------------------------\n");
  2587. out:
  2588. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  2589. }
  2590. __init static int tracer_alloc_buffers(void)
  2591. {
  2592. struct trace_array_cpu *data;
  2593. int i;
  2594. /* TODO: make the number of buffers hot pluggable with CPUS */
  2595. tracing_buffer_mask = cpu_possible_map;
  2596. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  2597. TRACE_BUFFER_FLAGS);
  2598. if (!global_trace.buffer) {
  2599. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  2600. WARN_ON(1);
  2601. return 0;
  2602. }
  2603. global_trace.entries = ring_buffer_size(global_trace.buffer);
  2604. #ifdef CONFIG_TRACER_MAX_TRACE
  2605. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  2606. TRACE_BUFFER_FLAGS);
  2607. if (!max_tr.buffer) {
  2608. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  2609. WARN_ON(1);
  2610. ring_buffer_free(global_trace.buffer);
  2611. return 0;
  2612. }
  2613. max_tr.entries = ring_buffer_size(max_tr.buffer);
  2614. WARN_ON(max_tr.entries != global_trace.entries);
  2615. #endif
  2616. /* Allocate the first page for all buffers */
  2617. for_each_tracing_cpu(i) {
  2618. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2619. max_tr.data[i] = &per_cpu(max_data, i);
  2620. }
  2621. trace_init_cmdlines();
  2622. register_tracer(&nop_trace);
  2623. #ifdef CONFIG_BOOT_TRACER
  2624. register_tracer(&boot_tracer);
  2625. current_trace = &boot_tracer;
  2626. current_trace->init(&global_trace);
  2627. #else
  2628. current_trace = &nop_trace;
  2629. #endif
  2630. /* All seems OK, enable tracing */
  2631. global_trace.ctrl = tracer_enabled;
  2632. tracing_disabled = 0;
  2633. atomic_notifier_chain_register(&panic_notifier_list,
  2634. &trace_panic_notifier);
  2635. register_die_notifier(&trace_die_notifier);
  2636. return 0;
  2637. }
  2638. early_initcall(tracer_alloc_buffers);
  2639. fs_initcall(tracer_init_debugfs);