trace_output.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. char str[KSYM_SYMBOL_LEN];
  274. #ifdef CONFIG_KALLSYMS
  275. const char *name;
  276. kallsyms_lookup(address, NULL, NULL, NULL, str);
  277. name = kretprobed(str);
  278. if (name && strlen(name)) {
  279. trace_seq_printf(s, fmt, name);
  280. return;
  281. }
  282. #endif
  283. snprintf(str, KSYM_SYMBOL_LEN, "0x%08lx", address);
  284. trace_seq_printf(s, fmt, str);
  285. }
  286. static void
  287. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  288. unsigned long address)
  289. {
  290. char str[KSYM_SYMBOL_LEN];
  291. #ifdef CONFIG_KALLSYMS
  292. const char *name;
  293. sprint_symbol(str, address);
  294. name = kretprobed(str);
  295. if (name && strlen(name)) {
  296. trace_seq_printf(s, fmt, name);
  297. return;
  298. }
  299. #endif
  300. snprintf(str, KSYM_SYMBOL_LEN, "0x%08lx", address);
  301. trace_seq_printf(s, fmt, str);
  302. }
  303. #ifndef CONFIG_64BIT
  304. # define IP_FMT "%08lx"
  305. #else
  306. # define IP_FMT "%016lx"
  307. #endif
  308. static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
  309. unsigned long ip, unsigned long sym_flags)
  310. {
  311. struct file *file = NULL;
  312. unsigned long vmstart = 0;
  313. int ret = 1;
  314. if (s->full)
  315. return 0;
  316. if (mm) {
  317. const struct vm_area_struct *vma;
  318. down_read(&mm->mmap_sem);
  319. vma = find_vma(mm, ip);
  320. if (vma) {
  321. file = vma->vm_file;
  322. vmstart = vma->vm_start;
  323. }
  324. if (file) {
  325. ret = trace_seq_path(s, &file->f_path);
  326. if (ret)
  327. trace_seq_printf(s, "[+0x%lx]",
  328. ip - vmstart);
  329. }
  330. up_read(&mm->mmap_sem);
  331. }
  332. if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
  333. trace_seq_printf(s, " <" IP_FMT ">", ip);
  334. return !trace_seq_has_overflowed(s);
  335. }
  336. int
  337. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  338. {
  339. if (!ip) {
  340. trace_seq_putc(s, '0');
  341. goto out;
  342. }
  343. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  344. seq_print_sym_offset(s, "%s", ip);
  345. else
  346. seq_print_sym_short(s, "%s", ip);
  347. if (sym_flags & TRACE_ITER_SYM_ADDR)
  348. trace_seq_printf(s, " <" IP_FMT ">", ip);
  349. out:
  350. return !trace_seq_has_overflowed(s);
  351. }
  352. /**
  353. * trace_print_lat_fmt - print the irq, preempt and lockdep fields
  354. * @s: trace seq struct to write to
  355. * @entry: The trace entry field from the ring buffer
  356. *
  357. * Prints the generic fields of irqs off, in hard or softirq, preempt
  358. * count.
  359. */
  360. int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  361. {
  362. char hardsoft_irq;
  363. char need_resched;
  364. char irqs_off;
  365. int hardirq;
  366. int softirq;
  367. int nmi;
  368. nmi = entry->flags & TRACE_FLAG_NMI;
  369. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  370. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  371. irqs_off =
  372. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  373. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
  374. '.';
  375. switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
  376. TRACE_FLAG_PREEMPT_RESCHED)) {
  377. case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
  378. need_resched = 'N';
  379. break;
  380. case TRACE_FLAG_NEED_RESCHED:
  381. need_resched = 'n';
  382. break;
  383. case TRACE_FLAG_PREEMPT_RESCHED:
  384. need_resched = 'p';
  385. break;
  386. default:
  387. need_resched = '.';
  388. break;
  389. }
  390. hardsoft_irq =
  391. (nmi && hardirq) ? 'Z' :
  392. nmi ? 'z' :
  393. (hardirq && softirq) ? 'H' :
  394. hardirq ? 'h' :
  395. softirq ? 's' :
  396. '.' ;
  397. trace_seq_printf(s, "%c%c%c",
  398. irqs_off, need_resched, hardsoft_irq);
  399. if (entry->preempt_count)
  400. trace_seq_printf(s, "%x", entry->preempt_count);
  401. else
  402. trace_seq_putc(s, '.');
  403. return !trace_seq_has_overflowed(s);
  404. }
  405. static int
  406. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  407. {
  408. char comm[TASK_COMM_LEN];
  409. trace_find_cmdline(entry->pid, comm);
  410. trace_seq_printf(s, "%8.8s-%-5d %3d",
  411. comm, entry->pid, cpu);
  412. return trace_print_lat_fmt(s, entry);
  413. }
  414. #undef MARK
  415. #define MARK(v, s) {.val = v, .sym = s}
  416. /* trace overhead mark */
  417. static const struct trace_mark {
  418. unsigned long long val; /* unit: nsec */
  419. char sym;
  420. } mark[] = {
  421. MARK(1000000000ULL , '$'), /* 1 sec */
  422. MARK(100000000ULL , '@'), /* 100 msec */
  423. MARK(10000000ULL , '*'), /* 10 msec */
  424. MARK(1000000ULL , '#'), /* 1000 usecs */
  425. MARK(100000ULL , '!'), /* 100 usecs */
  426. MARK(10000ULL , '+'), /* 10 usecs */
  427. };
  428. #undef MARK
  429. char trace_find_mark(unsigned long long d)
  430. {
  431. int i;
  432. int size = ARRAY_SIZE(mark);
  433. for (i = 0; i < size; i++) {
  434. if (d > mark[i].val)
  435. break;
  436. }
  437. return (i == size) ? ' ' : mark[i].sym;
  438. }
  439. static int
  440. lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
  441. {
  442. struct trace_array *tr = iter->tr;
  443. unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
  444. unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
  445. unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
  446. unsigned long long rel_ts = next_ts - iter->ts;
  447. struct trace_seq *s = &iter->seq;
  448. if (in_ns) {
  449. abs_ts = ns2usecs(abs_ts);
  450. rel_ts = ns2usecs(rel_ts);
  451. }
  452. if (verbose && in_ns) {
  453. unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
  454. unsigned long abs_msec = (unsigned long)abs_ts;
  455. unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
  456. unsigned long rel_msec = (unsigned long)rel_ts;
  457. trace_seq_printf(
  458. s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
  459. ns2usecs(iter->ts),
  460. abs_msec, abs_usec,
  461. rel_msec, rel_usec);
  462. } else if (verbose && !in_ns) {
  463. trace_seq_printf(
  464. s, "[%016llx] %lld (+%lld): ",
  465. iter->ts, abs_ts, rel_ts);
  466. } else if (!verbose && in_ns) {
  467. trace_seq_printf(
  468. s, " %4lldus%c: ",
  469. abs_ts,
  470. trace_find_mark(rel_ts * NSEC_PER_USEC));
  471. } else { /* !verbose && !in_ns */
  472. trace_seq_printf(s, " %4lld: ", abs_ts);
  473. }
  474. return !trace_seq_has_overflowed(s);
  475. }
  476. int trace_print_context(struct trace_iterator *iter)
  477. {
  478. struct trace_array *tr = iter->tr;
  479. struct trace_seq *s = &iter->seq;
  480. struct trace_entry *entry = iter->ent;
  481. unsigned long long t;
  482. unsigned long secs, usec_rem;
  483. char comm[TASK_COMM_LEN];
  484. trace_find_cmdline(entry->pid, comm);
  485. trace_seq_printf(s, "%16s-%-5d [%03d] ",
  486. comm, entry->pid, iter->cpu);
  487. if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
  488. unsigned int tgid = trace_find_tgid(entry->pid);
  489. if (!tgid)
  490. trace_seq_printf(s, "(-----) ");
  491. else
  492. trace_seq_printf(s, "(%5d) ", tgid);
  493. }
  494. if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
  495. trace_print_lat_fmt(s, entry);
  496. if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
  497. t = ns2usecs(iter->ts);
  498. usec_rem = do_div(t, USEC_PER_SEC);
  499. secs = (unsigned long)t;
  500. trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
  501. } else
  502. trace_seq_printf(s, " %12llu: ", iter->ts);
  503. return !trace_seq_has_overflowed(s);
  504. }
  505. int trace_print_lat_context(struct trace_iterator *iter)
  506. {
  507. struct trace_array *tr = iter->tr;
  508. /* trace_find_next_entry will reset ent_size */
  509. int ent_size = iter->ent_size;
  510. struct trace_seq *s = &iter->seq;
  511. u64 next_ts;
  512. struct trace_entry *entry = iter->ent,
  513. *next_entry = trace_find_next_entry(iter, NULL,
  514. &next_ts);
  515. unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
  516. /* Restore the original ent_size */
  517. iter->ent_size = ent_size;
  518. if (!next_entry)
  519. next_ts = iter->ts;
  520. if (verbose) {
  521. char comm[TASK_COMM_LEN];
  522. trace_find_cmdline(entry->pid, comm);
  523. trace_seq_printf(
  524. s, "%16s %5d %3d %d %08x %08lx ",
  525. comm, entry->pid, iter->cpu, entry->flags,
  526. entry->preempt_count, iter->idx);
  527. } else {
  528. lat_print_generic(s, entry, iter->cpu);
  529. }
  530. lat_print_timestamp(iter, next_ts);
  531. return !trace_seq_has_overflowed(s);
  532. }
  533. /**
  534. * ftrace_find_event - find a registered event
  535. * @type: the type of event to look for
  536. *
  537. * Returns an event of type @type otherwise NULL
  538. * Called with trace_event_read_lock() held.
  539. */
  540. struct trace_event *ftrace_find_event(int type)
  541. {
  542. struct trace_event *event;
  543. unsigned key;
  544. key = type & (EVENT_HASHSIZE - 1);
  545. hlist_for_each_entry(event, &event_hash[key], node) {
  546. if (event->type == type)
  547. return event;
  548. }
  549. return NULL;
  550. }
  551. static LIST_HEAD(ftrace_event_list);
  552. static int trace_search_list(struct list_head **list)
  553. {
  554. struct trace_event *e;
  555. int last = __TRACE_LAST_TYPE;
  556. if (list_empty(&ftrace_event_list)) {
  557. *list = &ftrace_event_list;
  558. return last + 1;
  559. }
  560. /*
  561. * We used up all possible max events,
  562. * lets see if somebody freed one.
  563. */
  564. list_for_each_entry(e, &ftrace_event_list, list) {
  565. if (e->type != last + 1)
  566. break;
  567. last++;
  568. }
  569. /* Did we used up all 65 thousand events??? */
  570. if ((last + 1) > TRACE_EVENT_TYPE_MAX)
  571. return 0;
  572. *list = &e->list;
  573. return last + 1;
  574. }
  575. void trace_event_read_lock(void)
  576. {
  577. down_read(&trace_event_sem);
  578. }
  579. void trace_event_read_unlock(void)
  580. {
  581. up_read(&trace_event_sem);
  582. }
  583. /**
  584. * register_trace_event - register output for an event type
  585. * @event: the event type to register
  586. *
  587. * Event types are stored in a hash and this hash is used to
  588. * find a way to print an event. If the @event->type is set
  589. * then it will use that type, otherwise it will assign a
  590. * type to use.
  591. *
  592. * If you assign your own type, please make sure it is added
  593. * to the trace_type enum in trace.h, to avoid collisions
  594. * with the dynamic types.
  595. *
  596. * Returns the event type number or zero on error.
  597. */
  598. int register_trace_event(struct trace_event *event)
  599. {
  600. unsigned key;
  601. int ret = 0;
  602. down_write(&trace_event_sem);
  603. if (WARN_ON(!event))
  604. goto out;
  605. if (WARN_ON(!event->funcs))
  606. goto out;
  607. INIT_LIST_HEAD(&event->list);
  608. if (!event->type) {
  609. struct list_head *list = NULL;
  610. if (next_event_type > TRACE_EVENT_TYPE_MAX) {
  611. event->type = trace_search_list(&list);
  612. if (!event->type)
  613. goto out;
  614. } else {
  615. event->type = next_event_type++;
  616. list = &ftrace_event_list;
  617. }
  618. if (WARN_ON(ftrace_find_event(event->type)))
  619. goto out;
  620. list_add_tail(&event->list, list);
  621. } else if (event->type > __TRACE_LAST_TYPE) {
  622. printk(KERN_WARNING "Need to add type to trace.h\n");
  623. WARN_ON(1);
  624. goto out;
  625. } else {
  626. /* Is this event already used */
  627. if (ftrace_find_event(event->type))
  628. goto out;
  629. }
  630. if (event->funcs->trace == NULL)
  631. event->funcs->trace = trace_nop_print;
  632. if (event->funcs->raw == NULL)
  633. event->funcs->raw = trace_nop_print;
  634. if (event->funcs->hex == NULL)
  635. event->funcs->hex = trace_nop_print;
  636. if (event->funcs->binary == NULL)
  637. event->funcs->binary = trace_nop_print;
  638. key = event->type & (EVENT_HASHSIZE - 1);
  639. hlist_add_head(&event->node, &event_hash[key]);
  640. ret = event->type;
  641. out:
  642. up_write(&trace_event_sem);
  643. return ret;
  644. }
  645. EXPORT_SYMBOL_GPL(register_trace_event);
  646. /*
  647. * Used by module code with the trace_event_sem held for write.
  648. */
  649. int __unregister_trace_event(struct trace_event *event)
  650. {
  651. hlist_del(&event->node);
  652. list_del(&event->list);
  653. return 0;
  654. }
  655. /**
  656. * unregister_trace_event - remove a no longer used event
  657. * @event: the event to remove
  658. */
  659. int unregister_trace_event(struct trace_event *event)
  660. {
  661. down_write(&trace_event_sem);
  662. __unregister_trace_event(event);
  663. up_write(&trace_event_sem);
  664. return 0;
  665. }
  666. EXPORT_SYMBOL_GPL(unregister_trace_event);
  667. /*
  668. * Standard events
  669. */
  670. enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
  671. struct trace_event *event)
  672. {
  673. trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
  674. return trace_handle_return(&iter->seq);
  675. }
  676. /* TRACE_FN */
  677. static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
  678. struct trace_event *event)
  679. {
  680. struct ftrace_entry *field;
  681. struct trace_seq *s = &iter->seq;
  682. trace_assign_type(field, iter->ent);
  683. seq_print_ip_sym(s, field->ip, flags);
  684. if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
  685. trace_seq_puts(s, " <-");
  686. seq_print_ip_sym(s, field->parent_ip, flags);
  687. }
  688. trace_seq_putc(s, '\n');
  689. return trace_handle_return(s);
  690. }
  691. static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
  692. struct trace_event *event)
  693. {
  694. struct ftrace_entry *field;
  695. trace_assign_type(field, iter->ent);
  696. trace_seq_printf(&iter->seq, "%lx %lx\n",
  697. field->ip,
  698. field->parent_ip);
  699. return trace_handle_return(&iter->seq);
  700. }
  701. static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
  702. struct trace_event *event)
  703. {
  704. struct ftrace_entry *field;
  705. struct trace_seq *s = &iter->seq;
  706. trace_assign_type(field, iter->ent);
  707. SEQ_PUT_HEX_FIELD(s, field->ip);
  708. SEQ_PUT_HEX_FIELD(s, field->parent_ip);
  709. return trace_handle_return(s);
  710. }
  711. static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
  712. struct trace_event *event)
  713. {
  714. struct ftrace_entry *field;
  715. struct trace_seq *s = &iter->seq;
  716. trace_assign_type(field, iter->ent);
  717. SEQ_PUT_FIELD(s, field->ip);
  718. SEQ_PUT_FIELD(s, field->parent_ip);
  719. return trace_handle_return(s);
  720. }
  721. static struct trace_event_functions trace_fn_funcs = {
  722. .trace = trace_fn_trace,
  723. .raw = trace_fn_raw,
  724. .hex = trace_fn_hex,
  725. .binary = trace_fn_bin,
  726. };
  727. static struct trace_event trace_fn_event = {
  728. .type = TRACE_FN,
  729. .funcs = &trace_fn_funcs,
  730. };
  731. /* TRACE_CTX an TRACE_WAKE */
  732. static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
  733. char *delim)
  734. {
  735. struct ctx_switch_entry *field;
  736. char comm[TASK_COMM_LEN];
  737. int S, T;
  738. trace_assign_type(field, iter->ent);
  739. T = task_index_to_char(field->next_state);
  740. S = task_index_to_char(field->prev_state);
  741. trace_find_cmdline(field->next_pid, comm);
  742. trace_seq_printf(&iter->seq,
  743. " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
  744. field->prev_pid,
  745. field->prev_prio,
  746. S, delim,
  747. field->next_cpu,
  748. field->next_pid,
  749. field->next_prio,
  750. T, comm);
  751. return trace_handle_return(&iter->seq);
  752. }
  753. static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
  754. struct trace_event *event)
  755. {
  756. return trace_ctxwake_print(iter, "==>");
  757. }
  758. static enum print_line_t trace_wake_print(struct trace_iterator *iter,
  759. int flags, struct trace_event *event)
  760. {
  761. return trace_ctxwake_print(iter, " +");
  762. }
  763. static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
  764. {
  765. struct ctx_switch_entry *field;
  766. int T;
  767. trace_assign_type(field, iter->ent);
  768. if (!S)
  769. S = task_index_to_char(field->prev_state);
  770. T = task_index_to_char(field->next_state);
  771. trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
  772. field->prev_pid,
  773. field->prev_prio,
  774. S,
  775. field->next_cpu,
  776. field->next_pid,
  777. field->next_prio,
  778. T);
  779. return trace_handle_return(&iter->seq);
  780. }
  781. static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
  782. struct trace_event *event)
  783. {
  784. return trace_ctxwake_raw(iter, 0);
  785. }
  786. static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
  787. struct trace_event *event)
  788. {
  789. return trace_ctxwake_raw(iter, '+');
  790. }
  791. static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
  792. {
  793. struct ctx_switch_entry *field;
  794. struct trace_seq *s = &iter->seq;
  795. int T;
  796. trace_assign_type(field, iter->ent);
  797. if (!S)
  798. S = task_index_to_char(field->prev_state);
  799. T = task_index_to_char(field->next_state);
  800. SEQ_PUT_HEX_FIELD(s, field->prev_pid);
  801. SEQ_PUT_HEX_FIELD(s, field->prev_prio);
  802. SEQ_PUT_HEX_FIELD(s, S);
  803. SEQ_PUT_HEX_FIELD(s, field->next_cpu);
  804. SEQ_PUT_HEX_FIELD(s, field->next_pid);
  805. SEQ_PUT_HEX_FIELD(s, field->next_prio);
  806. SEQ_PUT_HEX_FIELD(s, T);
  807. return trace_handle_return(s);
  808. }
  809. static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
  810. struct trace_event *event)
  811. {
  812. return trace_ctxwake_hex(iter, 0);
  813. }
  814. static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
  815. struct trace_event *event)
  816. {
  817. return trace_ctxwake_hex(iter, '+');
  818. }
  819. static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
  820. int flags, struct trace_event *event)
  821. {
  822. struct ctx_switch_entry *field;
  823. struct trace_seq *s = &iter->seq;
  824. trace_assign_type(field, iter->ent);
  825. SEQ_PUT_FIELD(s, field->prev_pid);
  826. SEQ_PUT_FIELD(s, field->prev_prio);
  827. SEQ_PUT_FIELD(s, field->prev_state);
  828. SEQ_PUT_FIELD(s, field->next_cpu);
  829. SEQ_PUT_FIELD(s, field->next_pid);
  830. SEQ_PUT_FIELD(s, field->next_prio);
  831. SEQ_PUT_FIELD(s, field->next_state);
  832. return trace_handle_return(s);
  833. }
  834. static struct trace_event_functions trace_ctx_funcs = {
  835. .trace = trace_ctx_print,
  836. .raw = trace_ctx_raw,
  837. .hex = trace_ctx_hex,
  838. .binary = trace_ctxwake_bin,
  839. };
  840. static struct trace_event trace_ctx_event = {
  841. .type = TRACE_CTX,
  842. .funcs = &trace_ctx_funcs,
  843. };
  844. static struct trace_event_functions trace_wake_funcs = {
  845. .trace = trace_wake_print,
  846. .raw = trace_wake_raw,
  847. .hex = trace_wake_hex,
  848. .binary = trace_ctxwake_bin,
  849. };
  850. static struct trace_event trace_wake_event = {
  851. .type = TRACE_WAKE,
  852. .funcs = &trace_wake_funcs,
  853. };
  854. /* TRACE_STACK */
  855. static enum print_line_t trace_stack_print(struct trace_iterator *iter,
  856. int flags, struct trace_event *event)
  857. {
  858. struct stack_entry *field;
  859. struct trace_seq *s = &iter->seq;
  860. unsigned long *p;
  861. unsigned long *end;
  862. trace_assign_type(field, iter->ent);
  863. end = (unsigned long *)((long)iter->ent + iter->ent_size);
  864. trace_seq_puts(s, "<stack trace>\n");
  865. for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
  866. if (trace_seq_has_overflowed(s))
  867. break;
  868. trace_seq_puts(s, " => ");
  869. seq_print_ip_sym(s, *p, flags);
  870. trace_seq_putc(s, '\n');
  871. }
  872. return trace_handle_return(s);
  873. }
  874. static struct trace_event_functions trace_stack_funcs = {
  875. .trace = trace_stack_print,
  876. };
  877. static struct trace_event trace_stack_event = {
  878. .type = TRACE_STACK,
  879. .funcs = &trace_stack_funcs,
  880. };
  881. /* TRACE_USER_STACK */
  882. static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
  883. int flags, struct trace_event *event)
  884. {
  885. struct trace_array *tr = iter->tr;
  886. struct userstack_entry *field;
  887. struct trace_seq *s = &iter->seq;
  888. struct mm_struct *mm = NULL;
  889. unsigned int i;
  890. trace_assign_type(field, iter->ent);
  891. trace_seq_puts(s, "<user stack trace>\n");
  892. if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
  893. struct task_struct *task;
  894. /*
  895. * we do the lookup on the thread group leader,
  896. * since individual threads might have already quit!
  897. */
  898. rcu_read_lock();
  899. task = find_task_by_vpid(field->tgid);
  900. if (task)
  901. mm = get_task_mm(task);
  902. rcu_read_unlock();
  903. }
  904. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  905. unsigned long ip = field->caller[i];
  906. if (ip == ULONG_MAX || trace_seq_has_overflowed(s))
  907. break;
  908. trace_seq_puts(s, " => ");
  909. if (!ip) {
  910. trace_seq_puts(s, "??");
  911. trace_seq_putc(s, '\n');
  912. continue;
  913. }
  914. seq_print_user_ip(s, mm, ip, flags);
  915. trace_seq_putc(s, '\n');
  916. }
  917. if (mm)
  918. mmput(mm);
  919. return trace_handle_return(s);
  920. }
  921. static struct trace_event_functions trace_user_stack_funcs = {
  922. .trace = trace_user_stack_print,
  923. };
  924. static struct trace_event trace_user_stack_event = {
  925. .type = TRACE_USER_STACK,
  926. .funcs = &trace_user_stack_funcs,
  927. };
  928. /* TRACE_HWLAT */
  929. static enum print_line_t
  930. trace_hwlat_print(struct trace_iterator *iter, int flags,
  931. struct trace_event *event)
  932. {
  933. struct trace_entry *entry = iter->ent;
  934. struct trace_seq *s = &iter->seq;
  935. struct hwlat_entry *field;
  936. trace_assign_type(field, entry);
  937. trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%lld.%09ld",
  938. field->seqnum,
  939. field->duration,
  940. field->outer_duration,
  941. (long long)field->timestamp.tv_sec,
  942. field->timestamp.tv_nsec);
  943. if (field->nmi_count) {
  944. /*
  945. * The generic sched_clock() is not NMI safe, thus
  946. * we only record the count and not the time.
  947. */
  948. if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
  949. trace_seq_printf(s, " nmi-total:%llu",
  950. field->nmi_total_ts);
  951. trace_seq_printf(s, " nmi-count:%u",
  952. field->nmi_count);
  953. }
  954. trace_seq_putc(s, '\n');
  955. return trace_handle_return(s);
  956. }
  957. static enum print_line_t
  958. trace_hwlat_raw(struct trace_iterator *iter, int flags,
  959. struct trace_event *event)
  960. {
  961. struct hwlat_entry *field;
  962. struct trace_seq *s = &iter->seq;
  963. trace_assign_type(field, iter->ent);
  964. trace_seq_printf(s, "%llu %lld %lld %09ld %u\n",
  965. field->duration,
  966. field->outer_duration,
  967. (long long)field->timestamp.tv_sec,
  968. field->timestamp.tv_nsec,
  969. field->seqnum);
  970. return trace_handle_return(s);
  971. }
  972. static struct trace_event_functions trace_hwlat_funcs = {
  973. .trace = trace_hwlat_print,
  974. .raw = trace_hwlat_raw,
  975. };
  976. static struct trace_event trace_hwlat_event = {
  977. .type = TRACE_HWLAT,
  978. .funcs = &trace_hwlat_funcs,
  979. };
  980. /* TRACE_BPUTS */
  981. static enum print_line_t
  982. trace_bputs_print(struct trace_iterator *iter, int flags,
  983. struct trace_event *event)
  984. {
  985. struct trace_entry *entry = iter->ent;
  986. struct trace_seq *s = &iter->seq;
  987. struct bputs_entry *field;
  988. trace_assign_type(field, entry);
  989. seq_print_ip_sym(s, field->ip, flags);
  990. trace_seq_puts(s, ": ");
  991. trace_seq_puts(s, field->str);
  992. return trace_handle_return(s);
  993. }
  994. static enum print_line_t
  995. trace_bputs_raw(struct trace_iterator *iter, int flags,
  996. struct trace_event *event)
  997. {
  998. struct bputs_entry *field;
  999. struct trace_seq *s = &iter->seq;
  1000. trace_assign_type(field, iter->ent);
  1001. trace_seq_printf(s, ": %lx : ", field->ip);
  1002. trace_seq_puts(s, field->str);
  1003. return trace_handle_return(s);
  1004. }
  1005. static struct trace_event_functions trace_bputs_funcs = {
  1006. .trace = trace_bputs_print,
  1007. .raw = trace_bputs_raw,
  1008. };
  1009. static struct trace_event trace_bputs_event = {
  1010. .type = TRACE_BPUTS,
  1011. .funcs = &trace_bputs_funcs,
  1012. };
  1013. /* TRACE_BPRINT */
  1014. static enum print_line_t
  1015. trace_bprint_print(struct trace_iterator *iter, int flags,
  1016. struct trace_event *event)
  1017. {
  1018. struct trace_entry *entry = iter->ent;
  1019. struct trace_seq *s = &iter->seq;
  1020. struct bprint_entry *field;
  1021. trace_assign_type(field, entry);
  1022. seq_print_ip_sym(s, field->ip, flags);
  1023. trace_seq_puts(s, ": ");
  1024. trace_seq_bprintf(s, field->fmt, field->buf);
  1025. return trace_handle_return(s);
  1026. }
  1027. static enum print_line_t
  1028. trace_bprint_raw(struct trace_iterator *iter, int flags,
  1029. struct trace_event *event)
  1030. {
  1031. struct bprint_entry *field;
  1032. struct trace_seq *s = &iter->seq;
  1033. trace_assign_type(field, iter->ent);
  1034. trace_seq_printf(s, ": %lx : ", field->ip);
  1035. trace_seq_bprintf(s, field->fmt, field->buf);
  1036. return trace_handle_return(s);
  1037. }
  1038. static struct trace_event_functions trace_bprint_funcs = {
  1039. .trace = trace_bprint_print,
  1040. .raw = trace_bprint_raw,
  1041. };
  1042. static struct trace_event trace_bprint_event = {
  1043. .type = TRACE_BPRINT,
  1044. .funcs = &trace_bprint_funcs,
  1045. };
  1046. /* TRACE_PRINT */
  1047. static enum print_line_t trace_print_print(struct trace_iterator *iter,
  1048. int flags, struct trace_event *event)
  1049. {
  1050. struct print_entry *field;
  1051. struct trace_seq *s = &iter->seq;
  1052. trace_assign_type(field, iter->ent);
  1053. seq_print_ip_sym(s, field->ip, flags);
  1054. trace_seq_printf(s, ": %s", field->buf);
  1055. return trace_handle_return(s);
  1056. }
  1057. static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
  1058. struct trace_event *event)
  1059. {
  1060. struct print_entry *field;
  1061. trace_assign_type(field, iter->ent);
  1062. trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
  1063. return trace_handle_return(&iter->seq);
  1064. }
  1065. static struct trace_event_functions trace_print_funcs = {
  1066. .trace = trace_print_print,
  1067. .raw = trace_print_raw,
  1068. };
  1069. static struct trace_event trace_print_event = {
  1070. .type = TRACE_PRINT,
  1071. .funcs = &trace_print_funcs,
  1072. };
  1073. static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
  1074. struct trace_event *event)
  1075. {
  1076. struct raw_data_entry *field;
  1077. int i;
  1078. trace_assign_type(field, iter->ent);
  1079. trace_seq_printf(&iter->seq, "# %x buf:", field->id);
  1080. for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
  1081. trace_seq_printf(&iter->seq, " %02x",
  1082. (unsigned char)field->buf[i]);
  1083. trace_seq_putc(&iter->seq, '\n');
  1084. return trace_handle_return(&iter->seq);
  1085. }
  1086. static struct trace_event_functions trace_raw_data_funcs = {
  1087. .trace = trace_raw_data,
  1088. .raw = trace_raw_data,
  1089. };
  1090. static struct trace_event trace_raw_data_event = {
  1091. .type = TRACE_RAW_DATA,
  1092. .funcs = &trace_raw_data_funcs,
  1093. };
  1094. static struct trace_event *events[] __initdata = {
  1095. &trace_fn_event,
  1096. &trace_ctx_event,
  1097. &trace_wake_event,
  1098. &trace_stack_event,
  1099. &trace_user_stack_event,
  1100. &trace_bputs_event,
  1101. &trace_bprint_event,
  1102. &trace_print_event,
  1103. &trace_hwlat_event,
  1104. &trace_raw_data_event,
  1105. NULL
  1106. };
  1107. __init static int init_events(void)
  1108. {
  1109. struct trace_event *event;
  1110. int i, ret;
  1111. for (i = 0; events[i]; i++) {
  1112. event = events[i];
  1113. ret = register_trace_event(event);
  1114. if (!ret) {
  1115. printk(KERN_WARNING "event %d failed to register\n",
  1116. event->type);
  1117. WARN_ON_ONCE(1);
  1118. }
  1119. }
  1120. return 0;
  1121. }
  1122. early_initcall(init_events);