trace_output.c 27 KB

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