trace_output.c 27 KB

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