trace.c 71 KB

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