trace_output.c 31 KB

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