trace_output.c 27 KB

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