trace_output.c 27 KB

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