trace_output.c 27 KB

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