trace_events.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * event tracer
  3. *
  4. * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * - Added format output of fields of the trace point.
  7. * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
  8. *
  9. */
  10. #include <linux/debugfs.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/module.h>
  13. #include <linux/ctype.h>
  14. #include "trace_output.h"
  15. #define TRACE_SYSTEM "TRACE_SYSTEM"
  16. static DEFINE_MUTEX(event_mutex);
  17. #define events_for_each(event) \
  18. for (event = __start_ftrace_events; \
  19. (unsigned long)event < (unsigned long)__stop_ftrace_events; \
  20. event++)
  21. void event_trace_printk(unsigned long ip, const char *fmt, ...)
  22. {
  23. va_list ap;
  24. va_start(ap, fmt);
  25. tracing_record_cmdline(current);
  26. trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
  27. va_end(ap);
  28. }
  29. static void ftrace_clear_events(void)
  30. {
  31. struct ftrace_event_call *call = (void *)__start_ftrace_events;
  32. while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
  33. if (call->enabled) {
  34. call->enabled = 0;
  35. call->unregfunc();
  36. }
  37. call++;
  38. }
  39. }
  40. static void ftrace_event_enable_disable(struct ftrace_event_call *call,
  41. int enable)
  42. {
  43. switch (enable) {
  44. case 0:
  45. if (call->enabled) {
  46. call->enabled = 0;
  47. call->unregfunc();
  48. }
  49. if (call->raw_enabled) {
  50. call->raw_enabled = 0;
  51. call->raw_unreg();
  52. }
  53. break;
  54. case 1:
  55. if (!call->enabled &&
  56. (call->type & TRACE_EVENT_TYPE_PRINTF)) {
  57. call->enabled = 1;
  58. call->regfunc();
  59. }
  60. if (!call->raw_enabled &&
  61. (call->type & TRACE_EVENT_TYPE_RAW)) {
  62. call->raw_enabled = 1;
  63. call->raw_reg();
  64. }
  65. break;
  66. }
  67. }
  68. static int ftrace_set_clr_event(char *buf, int set)
  69. {
  70. struct ftrace_event_call *call = __start_ftrace_events;
  71. char *event = NULL, *sub = NULL, *match;
  72. int ret = -EINVAL;
  73. /*
  74. * The buf format can be <subsystem>:<event-name>
  75. * *:<event-name> means any event by that name.
  76. * :<event-name> is the same.
  77. *
  78. * <subsystem>:* means all events in that subsystem
  79. * <subsystem>: means the same.
  80. *
  81. * <name> (no ':') means all events in a subsystem with
  82. * the name <name> or any event that matches <name>
  83. */
  84. match = strsep(&buf, ":");
  85. if (buf) {
  86. sub = match;
  87. event = buf;
  88. match = NULL;
  89. if (!strlen(sub) || strcmp(sub, "*") == 0)
  90. sub = NULL;
  91. if (!strlen(event) || strcmp(event, "*") == 0)
  92. event = NULL;
  93. }
  94. mutex_lock(&event_mutex);
  95. events_for_each(call) {
  96. if (!call->name)
  97. continue;
  98. if (match &&
  99. strcmp(match, call->name) != 0 &&
  100. strcmp(match, call->system) != 0)
  101. continue;
  102. if (sub && strcmp(sub, call->system) != 0)
  103. continue;
  104. if (event && strcmp(event, call->name) != 0)
  105. continue;
  106. ftrace_event_enable_disable(call, set);
  107. ret = 0;
  108. }
  109. mutex_unlock(&event_mutex);
  110. return ret;
  111. }
  112. /* 128 should be much more than enough */
  113. #define EVENT_BUF_SIZE 127
  114. static ssize_t
  115. ftrace_event_write(struct file *file, const char __user *ubuf,
  116. size_t cnt, loff_t *ppos)
  117. {
  118. size_t read = 0;
  119. int i, set = 1;
  120. ssize_t ret;
  121. char *buf;
  122. char ch;
  123. if (!cnt || cnt < 0)
  124. return 0;
  125. ret = get_user(ch, ubuf++);
  126. if (ret)
  127. return ret;
  128. read++;
  129. cnt--;
  130. /* skip white space */
  131. while (cnt && isspace(ch)) {
  132. ret = get_user(ch, ubuf++);
  133. if (ret)
  134. return ret;
  135. read++;
  136. cnt--;
  137. }
  138. /* Only white space found? */
  139. if (isspace(ch)) {
  140. file->f_pos += read;
  141. ret = read;
  142. return ret;
  143. }
  144. buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
  145. if (!buf)
  146. return -ENOMEM;
  147. if (cnt > EVENT_BUF_SIZE)
  148. cnt = EVENT_BUF_SIZE;
  149. i = 0;
  150. while (cnt && !isspace(ch)) {
  151. if (!i && ch == '!')
  152. set = 0;
  153. else
  154. buf[i++] = ch;
  155. ret = get_user(ch, ubuf++);
  156. if (ret)
  157. goto out_free;
  158. read++;
  159. cnt--;
  160. }
  161. buf[i] = 0;
  162. file->f_pos += read;
  163. ret = ftrace_set_clr_event(buf, set);
  164. if (ret)
  165. goto out_free;
  166. ret = read;
  167. out_free:
  168. kfree(buf);
  169. return ret;
  170. }
  171. static void *
  172. t_next(struct seq_file *m, void *v, loff_t *pos)
  173. {
  174. struct ftrace_event_call *call = m->private;
  175. struct ftrace_event_call *next = call;
  176. (*pos)++;
  177. if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
  178. return NULL;
  179. m->private = ++next;
  180. return call;
  181. }
  182. static void *t_start(struct seq_file *m, loff_t *pos)
  183. {
  184. return t_next(m, NULL, pos);
  185. }
  186. static void *
  187. s_next(struct seq_file *m, void *v, loff_t *pos)
  188. {
  189. struct ftrace_event_call *call = m->private;
  190. struct ftrace_event_call *next;
  191. (*pos)++;
  192. retry:
  193. if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
  194. return NULL;
  195. if (!call->enabled) {
  196. call++;
  197. goto retry;
  198. }
  199. next = call;
  200. m->private = ++next;
  201. return call;
  202. }
  203. static void *s_start(struct seq_file *m, loff_t *pos)
  204. {
  205. return s_next(m, NULL, pos);
  206. }
  207. static int t_show(struct seq_file *m, void *v)
  208. {
  209. struct ftrace_event_call *call = v;
  210. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  211. seq_printf(m, "%s:", call->system);
  212. seq_printf(m, "%s\n", call->name);
  213. return 0;
  214. }
  215. static void t_stop(struct seq_file *m, void *p)
  216. {
  217. }
  218. static int
  219. ftrace_event_seq_open(struct inode *inode, struct file *file)
  220. {
  221. int ret;
  222. const struct seq_operations *seq_ops;
  223. if ((file->f_mode & FMODE_WRITE) &&
  224. !(file->f_flags & O_APPEND))
  225. ftrace_clear_events();
  226. seq_ops = inode->i_private;
  227. ret = seq_open(file, seq_ops);
  228. if (!ret) {
  229. struct seq_file *m = file->private_data;
  230. m->private = __start_ftrace_events;
  231. }
  232. return ret;
  233. }
  234. static ssize_t
  235. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  236. loff_t *ppos)
  237. {
  238. struct ftrace_event_call *call = filp->private_data;
  239. char *buf;
  240. if (call->enabled || call->raw_enabled)
  241. buf = "1\n";
  242. else
  243. buf = "0\n";
  244. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  245. }
  246. static ssize_t
  247. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  248. loff_t *ppos)
  249. {
  250. struct ftrace_event_call *call = filp->private_data;
  251. char buf[64];
  252. unsigned long val;
  253. int ret;
  254. if (cnt >= sizeof(buf))
  255. return -EINVAL;
  256. if (copy_from_user(&buf, ubuf, cnt))
  257. return -EFAULT;
  258. buf[cnt] = 0;
  259. ret = strict_strtoul(buf, 10, &val);
  260. if (ret < 0)
  261. return ret;
  262. switch (val) {
  263. case 0:
  264. case 1:
  265. mutex_lock(&event_mutex);
  266. ftrace_event_enable_disable(call, val);
  267. mutex_unlock(&event_mutex);
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. *ppos += cnt;
  273. return cnt;
  274. }
  275. static ssize_t
  276. event_type_read(struct file *filp, char __user *ubuf, size_t cnt,
  277. loff_t *ppos)
  278. {
  279. struct ftrace_event_call *call = filp->private_data;
  280. char buf[16];
  281. int r = 0;
  282. if (call->type & TRACE_EVENT_TYPE_PRINTF)
  283. r += sprintf(buf, "printf\n");
  284. if (call->type & TRACE_EVENT_TYPE_RAW)
  285. r += sprintf(buf+r, "raw\n");
  286. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  287. }
  288. static ssize_t
  289. event_type_write(struct file *filp, const char __user *ubuf, size_t cnt,
  290. loff_t *ppos)
  291. {
  292. struct ftrace_event_call *call = filp->private_data;
  293. char buf[64];
  294. /*
  295. * If there's only one type, we can't change it.
  296. * And currently we always have printf type, and we
  297. * may or may not have raw type.
  298. *
  299. * This is a redundant check, the file should be read
  300. * only if this is the case anyway.
  301. */
  302. if (!call->raw_init)
  303. return -EPERM;
  304. if (cnt >= sizeof(buf))
  305. return -EINVAL;
  306. if (copy_from_user(&buf, ubuf, cnt))
  307. return -EFAULT;
  308. buf[cnt] = 0;
  309. if (!strncmp(buf, "printf", 6) &&
  310. (!buf[6] || isspace(buf[6]))) {
  311. call->type = TRACE_EVENT_TYPE_PRINTF;
  312. /*
  313. * If raw enabled, the disable it and enable
  314. * printf type.
  315. */
  316. if (call->raw_enabled) {
  317. call->raw_enabled = 0;
  318. call->raw_unreg();
  319. call->enabled = 1;
  320. call->regfunc();
  321. }
  322. } else if (!strncmp(buf, "raw", 3) &&
  323. (!buf[3] || isspace(buf[3]))) {
  324. call->type = TRACE_EVENT_TYPE_RAW;
  325. /*
  326. * If printf enabled, the disable it and enable
  327. * raw type.
  328. */
  329. if (call->enabled) {
  330. call->enabled = 0;
  331. call->unregfunc();
  332. call->raw_enabled = 1;
  333. call->raw_reg();
  334. }
  335. } else
  336. return -EINVAL;
  337. *ppos += cnt;
  338. return cnt;
  339. }
  340. static ssize_t
  341. event_available_types_read(struct file *filp, char __user *ubuf, size_t cnt,
  342. loff_t *ppos)
  343. {
  344. struct ftrace_event_call *call = filp->private_data;
  345. char buf[16];
  346. int r = 0;
  347. r += sprintf(buf, "printf\n");
  348. if (call->raw_init)
  349. r += sprintf(buf+r, "raw\n");
  350. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  351. }
  352. #undef FIELD
  353. #define FIELD(type, name) \
  354. #type, #name, offsetof(typeof(field), name), sizeof(field.name)
  355. static int trace_write_header(struct trace_seq *s)
  356. {
  357. struct trace_entry field;
  358. /* struct trace_entry */
  359. return trace_seq_printf(s,
  360. "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
  361. "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
  362. "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
  363. "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
  364. "\tfield:%s %s;\toffset:%lu;\tsize:%lu;\n"
  365. "\n",
  366. FIELD(unsigned char, type),
  367. FIELD(unsigned char, flags),
  368. FIELD(unsigned char, preempt_count),
  369. FIELD(int, pid),
  370. FIELD(int, tgid));
  371. }
  372. static ssize_t
  373. event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
  374. loff_t *ppos)
  375. {
  376. struct ftrace_event_call *call = filp->private_data;
  377. struct trace_seq *s;
  378. char *buf;
  379. int r;
  380. s = kmalloc(sizeof(*s), GFP_KERNEL);
  381. if (!s)
  382. return -ENOMEM;
  383. trace_seq_init(s);
  384. if (*ppos)
  385. return 0;
  386. /* If any of the first writes fail, so will the show_format. */
  387. trace_seq_printf(s, "name: %s\n", call->name);
  388. trace_seq_printf(s, "ID: %d\n", call->id);
  389. trace_seq_printf(s, "format:\n");
  390. trace_write_header(s);
  391. r = call->show_format(s);
  392. if (!r) {
  393. /*
  394. * ug! The format output is bigger than a PAGE!!
  395. */
  396. buf = "FORMAT TOO BIG\n";
  397. r = simple_read_from_buffer(ubuf, cnt, ppos,
  398. buf, strlen(buf));
  399. goto out;
  400. }
  401. r = simple_read_from_buffer(ubuf, cnt, ppos,
  402. s->buffer, s->len);
  403. out:
  404. kfree(s);
  405. return r;
  406. }
  407. static const struct seq_operations show_event_seq_ops = {
  408. .start = t_start,
  409. .next = t_next,
  410. .show = t_show,
  411. .stop = t_stop,
  412. };
  413. static const struct seq_operations show_set_event_seq_ops = {
  414. .start = s_start,
  415. .next = s_next,
  416. .show = t_show,
  417. .stop = t_stop,
  418. };
  419. static const struct file_operations ftrace_avail_fops = {
  420. .open = ftrace_event_seq_open,
  421. .read = seq_read,
  422. .llseek = seq_lseek,
  423. .release = seq_release,
  424. };
  425. static const struct file_operations ftrace_set_event_fops = {
  426. .open = ftrace_event_seq_open,
  427. .read = seq_read,
  428. .write = ftrace_event_write,
  429. .llseek = seq_lseek,
  430. .release = seq_release,
  431. };
  432. static const struct file_operations ftrace_enable_fops = {
  433. .open = tracing_open_generic,
  434. .read = event_enable_read,
  435. .write = event_enable_write,
  436. };
  437. static const struct file_operations ftrace_type_fops = {
  438. .open = tracing_open_generic,
  439. .read = event_type_read,
  440. .write = event_type_write,
  441. };
  442. static const struct file_operations ftrace_available_types_fops = {
  443. .open = tracing_open_generic,
  444. .read = event_available_types_read,
  445. };
  446. static const struct file_operations ftrace_event_format_fops = {
  447. .open = tracing_open_generic,
  448. .read = event_format_read,
  449. };
  450. static struct dentry *event_trace_events_dir(void)
  451. {
  452. static struct dentry *d_tracer;
  453. static struct dentry *d_events;
  454. if (d_events)
  455. return d_events;
  456. d_tracer = tracing_init_dentry();
  457. if (!d_tracer)
  458. return NULL;
  459. d_events = debugfs_create_dir("events", d_tracer);
  460. if (!d_events)
  461. pr_warning("Could not create debugfs "
  462. "'events' directory\n");
  463. return d_events;
  464. }
  465. struct event_subsystem {
  466. struct list_head list;
  467. const char *name;
  468. struct dentry *entry;
  469. };
  470. static LIST_HEAD(event_subsystems);
  471. static struct dentry *
  472. event_subsystem_dir(const char *name, struct dentry *d_events)
  473. {
  474. struct event_subsystem *system;
  475. /* First see if we did not already create this dir */
  476. list_for_each_entry(system, &event_subsystems, list) {
  477. if (strcmp(system->name, name) == 0)
  478. return system->entry;
  479. }
  480. /* need to create new entry */
  481. system = kmalloc(sizeof(*system), GFP_KERNEL);
  482. if (!system) {
  483. pr_warning("No memory to create event subsystem %s\n",
  484. name);
  485. return d_events;
  486. }
  487. system->entry = debugfs_create_dir(name, d_events);
  488. if (!system->entry) {
  489. pr_warning("Could not create event subsystem %s\n",
  490. name);
  491. kfree(system);
  492. return d_events;
  493. }
  494. system->name = name;
  495. list_add(&system->list, &event_subsystems);
  496. return system->entry;
  497. }
  498. static int
  499. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
  500. {
  501. struct dentry *entry;
  502. int ret;
  503. /*
  504. * If the trace point header did not define TRACE_SYSTEM
  505. * then the system would be called "TRACE_SYSTEM".
  506. */
  507. if (strcmp(call->system, "TRACE_SYSTEM") != 0)
  508. d_events = event_subsystem_dir(call->system, d_events);
  509. if (call->raw_init) {
  510. ret = call->raw_init();
  511. if (ret < 0) {
  512. pr_warning("Could not initialize trace point"
  513. " events/%s\n", call->name);
  514. return ret;
  515. }
  516. }
  517. /* default the output to printf */
  518. call->type = TRACE_EVENT_TYPE_PRINTF;
  519. call->dir = debugfs_create_dir(call->name, d_events);
  520. if (!call->dir) {
  521. pr_warning("Could not create debugfs "
  522. "'%s' directory\n", call->name);
  523. return -1;
  524. }
  525. entry = debugfs_create_file("enable", 0644, call->dir, call,
  526. &ftrace_enable_fops);
  527. if (!entry)
  528. pr_warning("Could not create debugfs "
  529. "'%s/enable' entry\n", call->name);
  530. /* Only let type be writable, if we can change it */
  531. entry = debugfs_create_file("type",
  532. call->raw_init ? 0644 : 0444,
  533. call->dir, call,
  534. &ftrace_type_fops);
  535. if (!entry)
  536. pr_warning("Could not create debugfs "
  537. "'%s/type' entry\n", call->name);
  538. entry = debugfs_create_file("available_types", 0444, call->dir, call,
  539. &ftrace_available_types_fops);
  540. if (!entry)
  541. pr_warning("Could not create debugfs "
  542. "'%s/available_types' entry\n", call->name);
  543. /* A trace may not want to export its format */
  544. if (!call->show_format)
  545. return 0;
  546. entry = debugfs_create_file("format", 0444, call->dir, call,
  547. &ftrace_event_format_fops);
  548. if (!entry)
  549. pr_warning("Could not create debugfs "
  550. "'%s/format' entry\n", call->name);
  551. return 0;
  552. }
  553. static __init int event_trace_init(void)
  554. {
  555. struct ftrace_event_call *call = __start_ftrace_events;
  556. struct dentry *d_tracer;
  557. struct dentry *entry;
  558. struct dentry *d_events;
  559. d_tracer = tracing_init_dentry();
  560. if (!d_tracer)
  561. return 0;
  562. entry = debugfs_create_file("available_events", 0444, d_tracer,
  563. (void *)&show_event_seq_ops,
  564. &ftrace_avail_fops);
  565. if (!entry)
  566. pr_warning("Could not create debugfs "
  567. "'available_events' entry\n");
  568. entry = debugfs_create_file("set_event", 0644, d_tracer,
  569. (void *)&show_set_event_seq_ops,
  570. &ftrace_set_event_fops);
  571. if (!entry)
  572. pr_warning("Could not create debugfs "
  573. "'set_event' entry\n");
  574. d_events = event_trace_events_dir();
  575. if (!d_events)
  576. return 0;
  577. events_for_each(call) {
  578. /* The linker may leave blanks */
  579. if (!call->name)
  580. continue;
  581. event_create_dir(call, d_events);
  582. }
  583. return 0;
  584. }
  585. fs_initcall(event_trace_init);