trace_output.c 28 KB

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