trace_output.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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);