trace_output.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * trace_output.c
  3. *
  4. * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mutex.h>
  9. #include <linux/ftrace.h>
  10. #include <linux/sched/clock.h>
  11. #include <linux/sched/mm.h>
  12. #include "trace_output.h"
  13. /* must be a power of 2 */
  14. #define EVENT_HASHSIZE 128
  15. DECLARE_RWSEM(trace_event_sem);
  16. static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
  17. static int next_event_type = __TRACE_LAST_TYPE + 1;
  18. enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
  19. {
  20. struct trace_seq *s = &iter->seq;
  21. struct trace_entry *entry = iter->ent;
  22. struct bputs_entry *field;
  23. trace_assign_type(field, entry);
  24. trace_seq_puts(s, field->str);
  25. return trace_handle_return(s);
  26. }
  27. enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
  28. {
  29. struct trace_seq *s = &iter->seq;
  30. struct trace_entry *entry = iter->ent;
  31. struct bprint_entry *field;
  32. trace_assign_type(field, entry);
  33. trace_seq_bprintf(s, field->fmt, field->buf);
  34. return trace_handle_return(s);
  35. }
  36. enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
  37. {
  38. struct trace_seq *s = &iter->seq;
  39. struct trace_entry *entry = iter->ent;
  40. struct print_entry *field;
  41. trace_assign_type(field, entry);
  42. trace_seq_puts(s, field->buf);
  43. return trace_handle_return(s);
  44. }
  45. const char *
  46. trace_print_flags_seq(struct trace_seq *p, const char *delim,
  47. unsigned long flags,
  48. const struct trace_print_flags *flag_array)
  49. {
  50. unsigned long mask;
  51. const char *str;
  52. const char *ret = trace_seq_buffer_ptr(p);
  53. int i, first = 1;
  54. for (i = 0; flag_array[i].name && flags; i++) {
  55. mask = flag_array[i].mask;
  56. if ((flags & mask) != mask)
  57. continue;
  58. str = flag_array[i].name;
  59. flags &= ~mask;
  60. if (!first && delim)
  61. trace_seq_puts(p, delim);
  62. else
  63. first = 0;
  64. trace_seq_puts(p, str);
  65. }
  66. /* check for left over flags */
  67. if (flags) {
  68. if (!first && delim)
  69. trace_seq_puts(p, delim);
  70. trace_seq_printf(p, "0x%lx", flags);
  71. }
  72. trace_seq_putc(p, 0);
  73. return ret;
  74. }
  75. EXPORT_SYMBOL(trace_print_flags_seq);
  76. const char *
  77. trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
  78. const struct trace_print_flags *symbol_array)
  79. {
  80. int i;
  81. const char *ret = trace_seq_buffer_ptr(p);
  82. for (i = 0; symbol_array[i].name; i++) {
  83. if (val != symbol_array[i].mask)
  84. continue;
  85. trace_seq_puts(p, symbol_array[i].name);
  86. break;
  87. }
  88. if (ret == (const char *)(trace_seq_buffer_ptr(p)))
  89. trace_seq_printf(p, "0x%lx", val);
  90. trace_seq_putc(p, 0);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(trace_print_symbols_seq);
  94. #if BITS_PER_LONG == 32
  95. const char *
  96. trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
  97. unsigned long long flags,
  98. const struct trace_print_flags_u64 *flag_array)
  99. {
  100. unsigned long long mask;
  101. const char *str;
  102. const char *ret = trace_seq_buffer_ptr(p);
  103. int i, first = 1;
  104. for (i = 0; flag_array[i].name && flags; i++) {
  105. mask = flag_array[i].mask;
  106. if ((flags & mask) != mask)
  107. continue;
  108. str = flag_array[i].name;
  109. flags &= ~mask;
  110. if (!first && delim)
  111. trace_seq_puts(p, delim);
  112. else
  113. first = 0;
  114. trace_seq_puts(p, str);
  115. }
  116. /* check for left over flags */
  117. if (flags) {
  118. if (!first && delim)
  119. trace_seq_puts(p, delim);
  120. trace_seq_printf(p, "0x%llx", flags);
  121. }
  122. trace_seq_putc(p, 0);
  123. return ret;
  124. }
  125. EXPORT_SYMBOL(trace_print_flags_seq_u64);
  126. const char *
  127. trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
  128. const struct trace_print_flags_u64 *symbol_array)
  129. {
  130. int i;
  131. const char *ret = trace_seq_buffer_ptr(p);
  132. for (i = 0; symbol_array[i].name; i++) {
  133. if (val != symbol_array[i].mask)
  134. continue;
  135. trace_seq_puts(p, symbol_array[i].name);
  136. break;
  137. }
  138. if (ret == (const char *)(trace_seq_buffer_ptr(p)))
  139. trace_seq_printf(p, "0x%llx", val);
  140. trace_seq_putc(p, 0);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(trace_print_symbols_seq_u64);
  144. #endif
  145. const char *
  146. trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
  147. unsigned int bitmask_size)
  148. {
  149. const char *ret = trace_seq_buffer_ptr(p);
  150. trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
  151. trace_seq_putc(p, 0);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
  155. /**
  156. * trace_print_hex_seq - print buffer as hex sequence
  157. * @p: trace seq struct to write to
  158. * @buf: The buffer to print
  159. * @buf_len: Length of @buf in bytes
  160. * @concatenate: Print @buf as single hex string or with spacing
  161. *
  162. * Prints the passed buffer as a hex sequence either as a whole,
  163. * single hex string if @concatenate is true or with spacing after
  164. * each byte in case @concatenate is false.
  165. */
  166. const char *
  167. trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
  168. bool concatenate)
  169. {
  170. int i;
  171. const char *ret = trace_seq_buffer_ptr(p);
  172. for (i = 0; i < buf_len; i++)
  173. trace_seq_printf(p, "%s%2.2x", concatenate || i == 0 ? "" : " ",
  174. buf[i]);
  175. trace_seq_putc(p, 0);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL(trace_print_hex_seq);
  179. const char *
  180. trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
  181. size_t el_size)
  182. {
  183. const char *ret = trace_seq_buffer_ptr(p);
  184. const char *prefix = "";
  185. void *ptr = (void *)buf;
  186. size_t buf_len = count * el_size;
  187. trace_seq_putc(p, '{');
  188. while (ptr < buf + buf_len) {
  189. switch (el_size) {
  190. case 1:
  191. trace_seq_printf(p, "%s0x%x", prefix,
  192. *(u8 *)ptr);
  193. break;
  194. case 2:
  195. trace_seq_printf(p, "%s0x%x", prefix,
  196. *(u16 *)ptr);
  197. break;
  198. case 4:
  199. trace_seq_printf(p, "%s0x%x", prefix,
  200. *(u32 *)ptr);
  201. break;
  202. case 8:
  203. trace_seq_printf(p, "%s0x%llx", prefix,
  204. *(u64 *)ptr);
  205. break;
  206. default:
  207. trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
  208. *(u8 *)ptr);
  209. el_size = 1;
  210. }
  211. prefix = ",";
  212. ptr += el_size;
  213. }
  214. trace_seq_putc(p, '}');
  215. trace_seq_putc(p, 0);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(trace_print_array_seq);
  219. int trace_raw_output_prep(struct trace_iterator *iter,
  220. struct trace_event *trace_event)
  221. {
  222. struct trace_event_call *event;
  223. struct trace_seq *s = &iter->seq;
  224. struct trace_seq *p = &iter->tmp_seq;
  225. struct trace_entry *entry;
  226. event = container_of(trace_event, struct trace_event_call, event);
  227. entry = iter->ent;
  228. if (entry->type != event->event.type) {
  229. WARN_ON_ONCE(1);
  230. return TRACE_TYPE_UNHANDLED;
  231. }
  232. trace_seq_init(p);
  233. trace_seq_printf(s, "%s: ", trace_event_name(event));
  234. return trace_handle_return(s);
  235. }
  236. EXPORT_SYMBOL(trace_raw_output_prep);
  237. static int trace_output_raw(struct trace_iterator *iter, char *name,
  238. char *fmt, va_list ap)
  239. {
  240. struct trace_seq *s = &iter->seq;
  241. trace_seq_printf(s, "%s: ", name);
  242. trace_seq_vprintf(s, fmt, ap);
  243. return trace_handle_return(s);
  244. }
  245. int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
  246. {
  247. va_list ap;
  248. int ret;
  249. va_start(ap, fmt);
  250. ret = trace_output_raw(iter, name, fmt, ap);
  251. va_end(ap);
  252. return ret;
  253. }
  254. EXPORT_SYMBOL_GPL(trace_output_call);
  255. #ifdef CONFIG_KRETPROBES
  256. static inline const char *kretprobed(const char *name)
  257. {
  258. static const char tramp_name[] = "kretprobe_trampoline";
  259. int size = sizeof(tramp_name);
  260. if (strncmp(tramp_name, name, size) == 0)
  261. return "[unknown/kretprobe'd]";
  262. return name;
  263. }
  264. #else
  265. static inline const char *kretprobed(const char *name)
  266. {
  267. return name;
  268. }
  269. #endif /* CONFIG_KRETPROBES */
  270. static void
  271. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  272. {
  273. #ifdef CONFIG_KALLSYMS
  274. char str[KSYM_SYMBOL_LEN];
  275. const char *name;
  276. kallsyms_lookup(address, NULL, NULL, NULL, str);
  277. name = kretprobed(str);
  278. trace_seq_printf(s, fmt, name);
  279. #endif
  280. }
  281. static void
  282. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  283. unsigned long address)
  284. {
  285. #ifdef CONFIG_KALLSYMS
  286. char str[KSYM_SYMBOL_LEN];
  287. const char *name;
  288. sprint_symbol(str, address);
  289. name = kretprobed(str);
  290. trace_seq_printf(s, fmt, name);
  291. #endif
  292. }
  293. #ifndef CONFIG_64BIT
  294. # define IP_FMT "%08lx"
  295. #else
  296. # define IP_FMT "%016lx"
  297. #endif
  298. static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
  299. unsigned long ip, unsigned long sym_flags)
  300. {
  301. struct file *file = NULL;
  302. unsigned long vmstart = 0;
  303. int ret = 1;
  304. if (s->full)
  305. return 0;
  306. if (mm) {
  307. const struct vm_area_struct *vma;
  308. down_read(&mm->mmap_sem);
  309. vma = find_vma(mm, ip);
  310. if (vma) {
  311. file = vma->vm_file;
  312. vmstart = vma->vm_start;
  313. }
  314. if (file) {
  315. ret = trace_seq_path(s, &file->f_path);
  316. if (ret)
  317. trace_seq_printf(s, "[+0x%lx]",
  318. ip - vmstart);
  319. }
  320. up_read(&mm->mmap_sem);
  321. }
  322. if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
  323. trace_seq_printf(s, " <" IP_FMT ">", ip);
  324. return !trace_seq_has_overflowed(s);
  325. }
  326. int
  327. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  328. {
  329. if (!ip) {
  330. trace_seq_putc(s, '0');
  331. goto out;
  332. }
  333. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  334. seq_print_sym_offset(s, "%s", ip);
  335. else
  336. seq_print_sym_short(s, "%s", ip);
  337. if (sym_flags & TRACE_ITER_SYM_ADDR)
  338. trace_seq_printf(s, " <" IP_FMT ">", ip);
  339. out:
  340. return !trace_seq_has_overflowed(s);
  341. }
  342. /**
  343. * trace_print_lat_fmt - print the irq, preempt and lockdep fields
  344. * @s: trace seq struct to write to
  345. * @entry: The trace entry field from the ring buffer
  346. *
  347. * Prints the generic fields of irqs off, in hard or softirq, preempt
  348. * count.
  349. */
  350. int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  351. {
  352. char hardsoft_irq;
  353. char need_resched;
  354. char irqs_off;
  355. int hardirq;
  356. int softirq;
  357. int nmi;
  358. nmi = entry->flags & TRACE_FLAG_NMI;
  359. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  360. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  361. irqs_off =
  362. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  363. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
  364. '.';
  365. switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
  366. TRACE_FLAG_PREEMPT_RESCHED)) {
  367. case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
  368. need_resched = 'N';
  369. break;
  370. case TRACE_FLAG_NEED_RESCHED:
  371. need_resched = 'n';
  372. break;
  373. case TRACE_FLAG_PREEMPT_RESCHED:
  374. need_resched = 'p';
  375. break;
  376. default:
  377. need_resched = '.';
  378. break;
  379. }
  380. hardsoft_irq =
  381. (nmi && hardirq) ? 'Z' :
  382. nmi ? 'z' :
  383. (hardirq && softirq) ? 'H' :
  384. hardirq ? 'h' :
  385. softirq ? 's' :
  386. '.' ;
  387. trace_seq_printf(s, "%c%c%c",
  388. irqs_off, need_resched, hardsoft_irq);
  389. if (entry->preempt_count)
  390. trace_seq_printf(s, "%x", entry->preempt_count);
  391. else
  392. trace_seq_putc(s, '.');
  393. return !trace_seq_has_overflowed(s);
  394. }
  395. static int
  396. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  397. {
  398. char comm[TASK_COMM_LEN];
  399. trace_find_cmdline(entry->pid, comm);
  400. trace_seq_printf(s, "%8.8s-%-5d %3d",
  401. comm, entry->pid, cpu);
  402. return trace_print_lat_fmt(s, entry);
  403. }
  404. #undef MARK
  405. #define MARK(v, s) {.val = v, .sym = s}
  406. /* trace overhead mark */
  407. static const struct trace_mark {
  408. unsigned long long val; /* unit: nsec */
  409. char sym;
  410. } mark[] = {
  411. MARK(1000000000ULL , '$'), /* 1 sec */
  412. MARK(100000000ULL , '@'), /* 100 msec */
  413. MARK(10000000ULL , '*'), /* 10 msec */
  414. MARK(1000000ULL , '#'), /* 1000 usecs */
  415. MARK(100000ULL , '!'), /* 100 usecs */
  416. MARK(10000ULL , '+'), /* 10 usecs */
  417. };
  418. #undef MARK
  419. char trace_find_mark(unsigned long long d)
  420. {
  421. int i;
  422. int size = ARRAY_SIZE(mark);
  423. for (i = 0; i < size; i++) {
  424. if (d > mark[i].val)
  425. break;
  426. }
  427. return (i == size) ? ' ' : mark[i].sym;
  428. }
  429. static int
  430. lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
  431. {
  432. struct trace_array *tr = iter->tr;
  433. unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
  434. unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
  435. unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
  436. unsigned long long rel_ts = next_ts - iter->ts;
  437. struct trace_seq *s = &iter->seq;
  438. if (in_ns) {
  439. abs_ts = ns2usecs(abs_ts);
  440. rel_ts = ns2usecs(rel_ts);
  441. }
  442. if (verbose && in_ns) {
  443. unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
  444. unsigned long abs_msec = (unsigned long)abs_ts;
  445. unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
  446. unsigned long rel_msec = (unsigned long)rel_ts;
  447. trace_seq_printf(
  448. s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
  449. ns2usecs(iter->ts),
  450. abs_msec, abs_usec,
  451. rel_msec, rel_usec);
  452. } else if (verbose && !in_ns) {
  453. trace_seq_printf(
  454. s, "[%016llx] %lld (+%lld): ",
  455. iter->ts, abs_ts, rel_ts);
  456. } else if (!verbose && in_ns) {
  457. trace_seq_printf(
  458. s, " %4lldus%c: ",
  459. abs_ts,
  460. trace_find_mark(rel_ts * NSEC_PER_USEC));
  461. } else { /* !verbose && !in_ns */
  462. trace_seq_printf(s, " %4lld: ", abs_ts);
  463. }
  464. return !trace_seq_has_overflowed(s);
  465. }
  466. int trace_print_context(struct trace_iterator *iter)
  467. {
  468. struct trace_array *tr = iter->tr;
  469. struct trace_seq *s = &iter->seq;
  470. struct trace_entry *entry = iter->ent;
  471. unsigned long long t;
  472. unsigned long secs, usec_rem;
  473. char comm[TASK_COMM_LEN];
  474. trace_find_cmdline(entry->pid, comm);
  475. trace_seq_printf(s, "%16s-%-5d [%03d] ",
  476. comm, entry->pid, iter->cpu);
  477. if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
  478. trace_print_lat_fmt(s, entry);
  479. if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
  480. t = ns2usecs(iter->ts);
  481. usec_rem = do_div(t, USEC_PER_SEC);
  482. secs = (unsigned long)t;
  483. trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
  484. } else
  485. trace_seq_printf(s, " %12llu: ", iter->ts);
  486. return !trace_seq_has_overflowed(s);
  487. }
  488. int trace_print_lat_context(struct trace_iterator *iter)
  489. {
  490. struct trace_array *tr = iter->tr;
  491. /* trace_find_next_entry will reset ent_size */
  492. int ent_size = iter->ent_size;
  493. struct trace_seq *s = &iter->seq;
  494. u64 next_ts;
  495. struct trace_entry *entry = iter->ent,
  496. *next_entry = trace_find_next_entry(iter, NULL,
  497. &next_ts);
  498. unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
  499. /* Restore the original ent_size */
  500. iter->ent_size = ent_size;
  501. if (!next_entry)
  502. next_ts = iter->ts;
  503. if (verbose) {
  504. char comm[TASK_COMM_LEN];
  505. trace_find_cmdline(entry->pid, comm);
  506. trace_seq_printf(
  507. s, "%16s %5d %3d %d %08x %08lx ",
  508. comm, entry->pid, iter->cpu, entry->flags,
  509. entry->preempt_count, iter->idx);
  510. } else {
  511. lat_print_generic(s, entry, iter->cpu);
  512. }
  513. lat_print_timestamp(iter, next_ts);
  514. return !trace_seq_has_overflowed(s);
  515. }
  516. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  517. static int task_state_char(unsigned long state)
  518. {
  519. int bit = state ? __ffs(state) + 1 : 0;
  520. return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
  521. }
  522. /**
  523. * ftrace_find_event - find a registered event
  524. * @type: the type of event to look for
  525. *
  526. * Returns an event of type @type otherwise NULL
  527. * Called with trace_event_read_lock() held.
  528. */
  529. struct trace_event *ftrace_find_event(int type)
  530. {
  531. struct trace_event *event;
  532. unsigned key;
  533. key = type & (EVENT_HASHSIZE - 1);
  534. hlist_for_each_entry(event, &event_hash[key], node) {
  535. if (event->type == type)
  536. return event;
  537. }
  538. return NULL;
  539. }
  540. static LIST_HEAD(ftrace_event_list);
  541. static int trace_search_list(struct list_head **list)
  542. {
  543. struct trace_event *e;
  544. int last = __TRACE_LAST_TYPE;
  545. if (list_empty(&ftrace_event_list)) {
  546. *list = &ftrace_event_list;
  547. return last + 1;
  548. }
  549. /*
  550. * We used up all possible max events,
  551. * lets see if somebody freed one.
  552. */
  553. list_for_each_entry(e, &ftrace_event_list, list) {
  554. if (e->type != last + 1)
  555. break;
  556. last++;
  557. }
  558. /* Did we used up all 65 thousand events??? */
  559. if ((last + 1) > TRACE_EVENT_TYPE_MAX)
  560. return 0;
  561. *list = &e->list;
  562. return last + 1;
  563. }
  564. void trace_event_read_lock(void)
  565. {
  566. down_read(&trace_event_sem);
  567. }
  568. void trace_event_read_unlock(void)
  569. {
  570. up_read(&trace_event_sem);
  571. }
  572. /**
  573. * register_trace_event - register output for an event type
  574. * @event: the event type to register
  575. *
  576. * Event types are stored in a hash and this hash is used to
  577. * find a way to print an event. If the @event->type is set
  578. * then it will use that type, otherwise it will assign a
  579. * type to use.
  580. *
  581. * If you assign your own type, please make sure it is added
  582. * to the trace_type enum in trace.h, to avoid collisions
  583. * with the dynamic types.
  584. *
  585. * Returns the event type number or zero on error.
  586. */
  587. int register_trace_event(struct trace_event *event)
  588. {
  589. unsigned key;
  590. int ret = 0;
  591. down_write(&trace_event_sem);
  592. if (WARN_ON(!event))
  593. goto out;
  594. if (WARN_ON(!event->funcs))
  595. goto out;
  596. INIT_LIST_HEAD(&event->list);
  597. if (!event->type) {
  598. struct list_head *list = NULL;
  599. if (next_event_type > TRACE_EVENT_TYPE_MAX) {
  600. event->type = trace_search_list(&list);
  601. if (!event->type)
  602. goto out;
  603. } else {
  604. event->type = next_event_type++;
  605. list = &ftrace_event_list;
  606. }
  607. if (WARN_ON(ftrace_find_event(event->type)))
  608. goto out;
  609. list_add_tail(&event->list, list);
  610. } else if (event->type > __TRACE_LAST_TYPE) {
  611. printk(KERN_WARNING "Need to add type to trace.h\n");
  612. WARN_ON(1);
  613. goto out;
  614. } else {
  615. /* Is this event already used */
  616. if (ftrace_find_event(event->type))
  617. goto out;
  618. }
  619. if (event->funcs->trace == NULL)
  620. event->funcs->trace = trace_nop_print;
  621. if (event->funcs->raw == NULL)
  622. event->funcs->raw = trace_nop_print;
  623. if (event->funcs->hex == NULL)
  624. event->funcs->hex = trace_nop_print;
  625. if (event->funcs->binary == NULL)
  626. event->funcs->binary = trace_nop_print;
  627. key = event->type & (EVENT_HASHSIZE - 1);
  628. hlist_add_head(&event->node, &event_hash[key]);
  629. ret = event->type;
  630. out:
  631. up_write(&trace_event_sem);
  632. return ret;
  633. }
  634. EXPORT_SYMBOL_GPL(register_trace_event);
  635. /*
  636. * Used by module code with the trace_event_sem held for write.
  637. */
  638. int __unregister_trace_event(struct trace_event *event)
  639. {
  640. hlist_del(&event->node);
  641. list_del(&event->list);
  642. return 0;
  643. }
  644. /**
  645. * unregister_trace_event - remove a no longer used event
  646. * @event: the event to remove
  647. */
  648. int unregister_trace_event(struct trace_event *event)
  649. {
  650. down_write(&trace_event_sem);
  651. __unregister_trace_event(event);
  652. up_write(&trace_event_sem);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(unregister_trace_event);
  656. /*
  657. * Standard events
  658. */
  659. enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
  660. struct trace_event *event)
  661. {
  662. trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
  663. return trace_handle_return(&iter->seq);
  664. }
  665. /* TRACE_FN */
  666. static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
  667. struct trace_event *event)
  668. {
  669. struct ftrace_entry *field;
  670. struct trace_seq *s = &iter->seq;
  671. trace_assign_type(field, iter->ent);
  672. seq_print_ip_sym(s, field->ip, flags);
  673. if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
  674. trace_seq_puts(s, " <-");
  675. seq_print_ip_sym(s, field->parent_ip, flags);
  676. }
  677. trace_seq_putc(s, '\n');
  678. return trace_handle_return(s);
  679. }
  680. static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
  681. struct trace_event *event)
  682. {
  683. struct ftrace_entry *field;
  684. trace_assign_type(field, iter->ent);
  685. trace_seq_printf(&iter->seq, "%lx %lx\n",
  686. field->ip,
  687. field->parent_ip);
  688. return trace_handle_return(&iter->seq);
  689. }
  690. static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
  691. struct trace_event *event)
  692. {
  693. struct ftrace_entry *field;
  694. struct trace_seq *s = &iter->seq;
  695. trace_assign_type(field, iter->ent);
  696. SEQ_PUT_HEX_FIELD(s, field->ip);
  697. SEQ_PUT_HEX_FIELD(s, field->parent_ip);
  698. return trace_handle_return(s);
  699. }
  700. static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
  701. struct trace_event *event)
  702. {
  703. struct ftrace_entry *field;
  704. struct trace_seq *s = &iter->seq;
  705. trace_assign_type(field, iter->ent);
  706. SEQ_PUT_FIELD(s, field->ip);
  707. SEQ_PUT_FIELD(s, field->parent_ip);
  708. return trace_handle_return(s);
  709. }
  710. static struct trace_event_functions trace_fn_funcs = {
  711. .trace = trace_fn_trace,
  712. .raw = trace_fn_raw,
  713. .hex = trace_fn_hex,
  714. .binary = trace_fn_bin,
  715. };
  716. static struct trace_event trace_fn_event = {
  717. .type = TRACE_FN,
  718. .funcs = &trace_fn_funcs,
  719. };
  720. /* TRACE_CTX an TRACE_WAKE */
  721. static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
  722. char *delim)
  723. {
  724. struct ctx_switch_entry *field;
  725. char comm[TASK_COMM_LEN];
  726. int S, T;
  727. trace_assign_type(field, iter->ent);
  728. T = task_state_char(field->next_state);
  729. S = task_state_char(field->prev_state);
  730. trace_find_cmdline(field->next_pid, comm);
  731. trace_seq_printf(&iter->seq,
  732. " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
  733. field->prev_pid,
  734. field->prev_prio,
  735. S, delim,
  736. field->next_cpu,
  737. field->next_pid,
  738. field->next_prio,
  739. T, comm);
  740. return trace_handle_return(&iter->seq);
  741. }
  742. static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
  743. struct trace_event *event)
  744. {
  745. return trace_ctxwake_print(iter, "==>");
  746. }
  747. static enum print_line_t trace_wake_print(struct trace_iterator *iter,
  748. int flags, struct trace_event *event)
  749. {
  750. return trace_ctxwake_print(iter, " +");
  751. }
  752. static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
  753. {
  754. struct ctx_switch_entry *field;
  755. int T;
  756. trace_assign_type(field, iter->ent);
  757. if (!S)
  758. S = task_state_char(field->prev_state);
  759. T = task_state_char(field->next_state);
  760. trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
  761. field->prev_pid,
  762. field->prev_prio,
  763. S,
  764. field->next_cpu,
  765. field->next_pid,
  766. field->next_prio,
  767. T);
  768. return trace_handle_return(&iter->seq);
  769. }
  770. static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
  771. struct trace_event *event)
  772. {
  773. return trace_ctxwake_raw(iter, 0);
  774. }
  775. static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
  776. struct trace_event *event)
  777. {
  778. return trace_ctxwake_raw(iter, '+');
  779. }
  780. static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
  781. {
  782. struct ctx_switch_entry *field;
  783. struct trace_seq *s = &iter->seq;
  784. int T;
  785. trace_assign_type(field, iter->ent);
  786. if (!S)
  787. S = task_state_char(field->prev_state);
  788. T = task_state_char(field->next_state);
  789. SEQ_PUT_HEX_FIELD(s, field->prev_pid);
  790. SEQ_PUT_HEX_FIELD(s, field->prev_prio);
  791. SEQ_PUT_HEX_FIELD(s, S);
  792. SEQ_PUT_HEX_FIELD(s, field->next_cpu);
  793. SEQ_PUT_HEX_FIELD(s, field->next_pid);
  794. SEQ_PUT_HEX_FIELD(s, field->next_prio);
  795. SEQ_PUT_HEX_FIELD(s, T);
  796. return trace_handle_return(s);
  797. }
  798. static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
  799. struct trace_event *event)
  800. {
  801. return trace_ctxwake_hex(iter, 0);
  802. }
  803. static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
  804. struct trace_event *event)
  805. {
  806. return trace_ctxwake_hex(iter, '+');
  807. }
  808. static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
  809. int flags, struct trace_event *event)
  810. {
  811. struct ctx_switch_entry *field;
  812. struct trace_seq *s = &iter->seq;
  813. trace_assign_type(field, iter->ent);
  814. SEQ_PUT_FIELD(s, field->prev_pid);
  815. SEQ_PUT_FIELD(s, field->prev_prio);
  816. SEQ_PUT_FIELD(s, field->prev_state);
  817. SEQ_PUT_FIELD(s, field->next_cpu);
  818. SEQ_PUT_FIELD(s, field->next_pid);
  819. SEQ_PUT_FIELD(s, field->next_prio);
  820. SEQ_PUT_FIELD(s, field->next_state);
  821. return trace_handle_return(s);
  822. }
  823. static struct trace_event_functions trace_ctx_funcs = {
  824. .trace = trace_ctx_print,
  825. .raw = trace_ctx_raw,
  826. .hex = trace_ctx_hex,
  827. .binary = trace_ctxwake_bin,
  828. };
  829. static struct trace_event trace_ctx_event = {
  830. .type = TRACE_CTX,
  831. .funcs = &trace_ctx_funcs,
  832. };
  833. static struct trace_event_functions trace_wake_funcs = {
  834. .trace = trace_wake_print,
  835. .raw = trace_wake_raw,
  836. .hex = trace_wake_hex,
  837. .binary = trace_ctxwake_bin,
  838. };
  839. static struct trace_event trace_wake_event = {
  840. .type = TRACE_WAKE,
  841. .funcs = &trace_wake_funcs,
  842. };
  843. /* TRACE_STACK */
  844. static enum print_line_t trace_stack_print(struct trace_iterator *iter,
  845. int flags, struct trace_event *event)
  846. {
  847. struct stack_entry *field;
  848. struct trace_seq *s = &iter->seq;
  849. unsigned long *p;
  850. unsigned long *end;
  851. trace_assign_type(field, iter->ent);
  852. end = (unsigned long *)((long)iter->ent + iter->ent_size);
  853. trace_seq_puts(s, "<stack trace>\n");
  854. for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
  855. if (trace_seq_has_overflowed(s))
  856. break;
  857. trace_seq_puts(s, " => ");
  858. seq_print_ip_sym(s, *p, flags);
  859. trace_seq_putc(s, '\n');
  860. }
  861. return trace_handle_return(s);
  862. }
  863. static struct trace_event_functions trace_stack_funcs = {
  864. .trace = trace_stack_print,
  865. };
  866. static struct trace_event trace_stack_event = {
  867. .type = TRACE_STACK,
  868. .funcs = &trace_stack_funcs,
  869. };
  870. /* TRACE_USER_STACK */
  871. static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
  872. int flags, struct trace_event *event)
  873. {
  874. struct trace_array *tr = iter->tr;
  875. struct userstack_entry *field;
  876. struct trace_seq *s = &iter->seq;
  877. struct mm_struct *mm = NULL;
  878. unsigned int i;
  879. trace_assign_type(field, iter->ent);
  880. trace_seq_puts(s, "<user stack trace>\n");
  881. if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
  882. struct task_struct *task;
  883. /*
  884. * we do the lookup on the thread group leader,
  885. * since individual threads might have already quit!
  886. */
  887. rcu_read_lock();
  888. task = find_task_by_vpid(field->tgid);
  889. if (task)
  890. mm = get_task_mm(task);
  891. rcu_read_unlock();
  892. }
  893. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  894. unsigned long ip = field->caller[i];
  895. if (ip == ULONG_MAX || trace_seq_has_overflowed(s))
  896. break;
  897. trace_seq_puts(s, " => ");
  898. if (!ip) {
  899. trace_seq_puts(s, "??");
  900. trace_seq_putc(s, '\n');
  901. continue;
  902. }
  903. seq_print_user_ip(s, mm, ip, flags);
  904. trace_seq_putc(s, '\n');
  905. }
  906. if (mm)
  907. mmput(mm);
  908. return trace_handle_return(s);
  909. }
  910. static struct trace_event_functions trace_user_stack_funcs = {
  911. .trace = trace_user_stack_print,
  912. };
  913. static struct trace_event trace_user_stack_event = {
  914. .type = TRACE_USER_STACK,
  915. .funcs = &trace_user_stack_funcs,
  916. };
  917. /* TRACE_HWLAT */
  918. static enum print_line_t
  919. trace_hwlat_print(struct trace_iterator *iter, int flags,
  920. struct trace_event *event)
  921. {
  922. struct trace_entry *entry = iter->ent;
  923. struct trace_seq *s = &iter->seq;
  924. struct hwlat_entry *field;
  925. trace_assign_type(field, entry);
  926. trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%ld.%09ld",
  927. field->seqnum,
  928. field->duration,
  929. field->outer_duration,
  930. field->timestamp.tv_sec,
  931. field->timestamp.tv_nsec);
  932. if (field->nmi_count) {
  933. /*
  934. * The generic sched_clock() is not NMI safe, thus
  935. * we only record the count and not the time.
  936. */
  937. if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
  938. trace_seq_printf(s, " nmi-total:%llu",
  939. field->nmi_total_ts);
  940. trace_seq_printf(s, " nmi-count:%u",
  941. field->nmi_count);
  942. }
  943. trace_seq_putc(s, '\n');
  944. return trace_handle_return(s);
  945. }
  946. static enum print_line_t
  947. trace_hwlat_raw(struct trace_iterator *iter, int flags,
  948. struct trace_event *event)
  949. {
  950. struct hwlat_entry *field;
  951. struct trace_seq *s = &iter->seq;
  952. trace_assign_type(field, iter->ent);
  953. trace_seq_printf(s, "%llu %lld %ld %09ld %u\n",
  954. field->duration,
  955. field->outer_duration,
  956. field->timestamp.tv_sec,
  957. field->timestamp.tv_nsec,
  958. field->seqnum);
  959. return trace_handle_return(s);
  960. }
  961. static struct trace_event_functions trace_hwlat_funcs = {
  962. .trace = trace_hwlat_print,
  963. .raw = trace_hwlat_raw,
  964. };
  965. static struct trace_event trace_hwlat_event = {
  966. .type = TRACE_HWLAT,
  967. .funcs = &trace_hwlat_funcs,
  968. };
  969. /* TRACE_BPUTS */
  970. static enum print_line_t
  971. trace_bputs_print(struct trace_iterator *iter, int flags,
  972. struct trace_event *event)
  973. {
  974. struct trace_entry *entry = iter->ent;
  975. struct trace_seq *s = &iter->seq;
  976. struct bputs_entry *field;
  977. trace_assign_type(field, entry);
  978. seq_print_ip_sym(s, field->ip, flags);
  979. trace_seq_puts(s, ": ");
  980. trace_seq_puts(s, field->str);
  981. return trace_handle_return(s);
  982. }
  983. static enum print_line_t
  984. trace_bputs_raw(struct trace_iterator *iter, int flags,
  985. struct trace_event *event)
  986. {
  987. struct bputs_entry *field;
  988. struct trace_seq *s = &iter->seq;
  989. trace_assign_type(field, iter->ent);
  990. trace_seq_printf(s, ": %lx : ", field->ip);
  991. trace_seq_puts(s, field->str);
  992. return trace_handle_return(s);
  993. }
  994. static struct trace_event_functions trace_bputs_funcs = {
  995. .trace = trace_bputs_print,
  996. .raw = trace_bputs_raw,
  997. };
  998. static struct trace_event trace_bputs_event = {
  999. .type = TRACE_BPUTS,
  1000. .funcs = &trace_bputs_funcs,
  1001. };
  1002. /* TRACE_BPRINT */
  1003. static enum print_line_t
  1004. trace_bprint_print(struct trace_iterator *iter, int flags,
  1005. struct trace_event *event)
  1006. {
  1007. struct trace_entry *entry = iter->ent;
  1008. struct trace_seq *s = &iter->seq;
  1009. struct bprint_entry *field;
  1010. trace_assign_type(field, entry);
  1011. seq_print_ip_sym(s, field->ip, flags);
  1012. trace_seq_puts(s, ": ");
  1013. trace_seq_bprintf(s, field->fmt, field->buf);
  1014. return trace_handle_return(s);
  1015. }
  1016. static enum print_line_t
  1017. trace_bprint_raw(struct trace_iterator *iter, int flags,
  1018. struct trace_event *event)
  1019. {
  1020. struct bprint_entry *field;
  1021. struct trace_seq *s = &iter->seq;
  1022. trace_assign_type(field, iter->ent);
  1023. trace_seq_printf(s, ": %lx : ", field->ip);
  1024. trace_seq_bprintf(s, field->fmt, field->buf);
  1025. return trace_handle_return(s);
  1026. }
  1027. static struct trace_event_functions trace_bprint_funcs = {
  1028. .trace = trace_bprint_print,
  1029. .raw = trace_bprint_raw,
  1030. };
  1031. static struct trace_event trace_bprint_event = {
  1032. .type = TRACE_BPRINT,
  1033. .funcs = &trace_bprint_funcs,
  1034. };
  1035. /* TRACE_PRINT */
  1036. static enum print_line_t trace_print_print(struct trace_iterator *iter,
  1037. int flags, struct trace_event *event)
  1038. {
  1039. struct print_entry *field;
  1040. struct trace_seq *s = &iter->seq;
  1041. trace_assign_type(field, iter->ent);
  1042. seq_print_ip_sym(s, field->ip, flags);
  1043. trace_seq_printf(s, ": %s", field->buf);
  1044. return trace_handle_return(s);
  1045. }
  1046. static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
  1047. struct trace_event *event)
  1048. {
  1049. struct print_entry *field;
  1050. trace_assign_type(field, iter->ent);
  1051. trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
  1052. return trace_handle_return(&iter->seq);
  1053. }
  1054. static struct trace_event_functions trace_print_funcs = {
  1055. .trace = trace_print_print,
  1056. .raw = trace_print_raw,
  1057. };
  1058. static struct trace_event trace_print_event = {
  1059. .type = TRACE_PRINT,
  1060. .funcs = &trace_print_funcs,
  1061. };
  1062. static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
  1063. struct trace_event *event)
  1064. {
  1065. struct raw_data_entry *field;
  1066. int i;
  1067. trace_assign_type(field, iter->ent);
  1068. trace_seq_printf(&iter->seq, "# %x buf:", field->id);
  1069. for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
  1070. trace_seq_printf(&iter->seq, " %02x",
  1071. (unsigned char)field->buf[i]);
  1072. trace_seq_putc(&iter->seq, '\n');
  1073. return trace_handle_return(&iter->seq);
  1074. }
  1075. static struct trace_event_functions trace_raw_data_funcs = {
  1076. .trace = trace_raw_data,
  1077. .raw = trace_raw_data,
  1078. };
  1079. static struct trace_event trace_raw_data_event = {
  1080. .type = TRACE_RAW_DATA,
  1081. .funcs = &trace_raw_data_funcs,
  1082. };
  1083. static struct trace_event *events[] __initdata = {
  1084. &trace_fn_event,
  1085. &trace_ctx_event,
  1086. &trace_wake_event,
  1087. &trace_stack_event,
  1088. &trace_user_stack_event,
  1089. &trace_bputs_event,
  1090. &trace_bprint_event,
  1091. &trace_print_event,
  1092. &trace_hwlat_event,
  1093. &trace_raw_data_event,
  1094. NULL
  1095. };
  1096. __init static int init_events(void)
  1097. {
  1098. struct trace_event *event;
  1099. int i, ret;
  1100. for (i = 0; events[i]; i++) {
  1101. event = events[i];
  1102. ret = register_trace_event(event);
  1103. if (!ret) {
  1104. printk(KERN_WARNING "event %d failed to register\n",
  1105. event->type);
  1106. WARN_ON_ONCE(1);
  1107. }
  1108. }
  1109. return 0;
  1110. }
  1111. early_initcall(init_events);