trace.c 71 KB

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