trace_output.c 29 KB

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