trace_events.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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, (unsigned int)offsetof(typeof(field), name), \
  355. (unsigned int)sizeof(field.name)
  356. static int trace_write_header(struct trace_seq *s)
  357. {
  358. struct trace_entry field;
  359. /* struct trace_entry */
  360. return trace_seq_printf(s,
  361. "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
  362. "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
  363. "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
  364. "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
  365. "\tfield:%s %s;\toffset:%u;\tsize:%u;\n"
  366. "\n",
  367. FIELD(unsigned char, type),
  368. FIELD(unsigned char, flags),
  369. FIELD(unsigned char, preempt_count),
  370. FIELD(int, pid),
  371. FIELD(int, tgid));
  372. }
  373. static ssize_t
  374. event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
  375. loff_t *ppos)
  376. {
  377. struct ftrace_event_call *call = filp->private_data;
  378. struct trace_seq *s;
  379. char *buf;
  380. int r;
  381. s = kmalloc(sizeof(*s), GFP_KERNEL);
  382. if (!s)
  383. return -ENOMEM;
  384. trace_seq_init(s);
  385. if (*ppos)
  386. return 0;
  387. /* If any of the first writes fail, so will the show_format. */
  388. trace_seq_printf(s, "name: %s\n", call->name);
  389. trace_seq_printf(s, "ID: %d\n", call->id);
  390. trace_seq_printf(s, "format:\n");
  391. trace_write_header(s);
  392. r = call->show_format(s);
  393. if (!r) {
  394. /*
  395. * ug! The format output is bigger than a PAGE!!
  396. */
  397. buf = "FORMAT TOO BIG\n";
  398. r = simple_read_from_buffer(ubuf, cnt, ppos,
  399. buf, strlen(buf));
  400. goto out;
  401. }
  402. r = simple_read_from_buffer(ubuf, cnt, ppos,
  403. s->buffer, s->len);
  404. out:
  405. kfree(s);
  406. return r;
  407. }
  408. static const struct seq_operations show_event_seq_ops = {
  409. .start = t_start,
  410. .next = t_next,
  411. .show = t_show,
  412. .stop = t_stop,
  413. };
  414. static const struct seq_operations show_set_event_seq_ops = {
  415. .start = s_start,
  416. .next = s_next,
  417. .show = t_show,
  418. .stop = t_stop,
  419. };
  420. static const struct file_operations ftrace_avail_fops = {
  421. .open = ftrace_event_seq_open,
  422. .read = seq_read,
  423. .llseek = seq_lseek,
  424. .release = seq_release,
  425. };
  426. static const struct file_operations ftrace_set_event_fops = {
  427. .open = ftrace_event_seq_open,
  428. .read = seq_read,
  429. .write = ftrace_event_write,
  430. .llseek = seq_lseek,
  431. .release = seq_release,
  432. };
  433. static const struct file_operations ftrace_enable_fops = {
  434. .open = tracing_open_generic,
  435. .read = event_enable_read,
  436. .write = event_enable_write,
  437. };
  438. static const struct file_operations ftrace_type_fops = {
  439. .open = tracing_open_generic,
  440. .read = event_type_read,
  441. .write = event_type_write,
  442. };
  443. static const struct file_operations ftrace_available_types_fops = {
  444. .open = tracing_open_generic,
  445. .read = event_available_types_read,
  446. };
  447. static const struct file_operations ftrace_event_format_fops = {
  448. .open = tracing_open_generic,
  449. .read = event_format_read,
  450. };
  451. static struct dentry *event_trace_events_dir(void)
  452. {
  453. static struct dentry *d_tracer;
  454. static struct dentry *d_events;
  455. if (d_events)
  456. return d_events;
  457. d_tracer = tracing_init_dentry();
  458. if (!d_tracer)
  459. return NULL;
  460. d_events = debugfs_create_dir("events", d_tracer);
  461. if (!d_events)
  462. pr_warning("Could not create debugfs "
  463. "'events' directory\n");
  464. return d_events;
  465. }
  466. struct event_subsystem {
  467. struct list_head list;
  468. const char *name;
  469. struct dentry *entry;
  470. };
  471. static LIST_HEAD(event_subsystems);
  472. static struct dentry *
  473. event_subsystem_dir(const char *name, struct dentry *d_events)
  474. {
  475. struct event_subsystem *system;
  476. /* First see if we did not already create this dir */
  477. list_for_each_entry(system, &event_subsystems, list) {
  478. if (strcmp(system->name, name) == 0)
  479. return system->entry;
  480. }
  481. /* need to create new entry */
  482. system = kmalloc(sizeof(*system), GFP_KERNEL);
  483. if (!system) {
  484. pr_warning("No memory to create event subsystem %s\n",
  485. name);
  486. return d_events;
  487. }
  488. system->entry = debugfs_create_dir(name, d_events);
  489. if (!system->entry) {
  490. pr_warning("Could not create event subsystem %s\n",
  491. name);
  492. kfree(system);
  493. return d_events;
  494. }
  495. system->name = name;
  496. list_add(&system->list, &event_subsystems);
  497. return system->entry;
  498. }
  499. static int
  500. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
  501. {
  502. struct dentry *entry;
  503. int ret;
  504. /*
  505. * If the trace point header did not define TRACE_SYSTEM
  506. * then the system would be called "TRACE_SYSTEM".
  507. */
  508. if (strcmp(call->system, "TRACE_SYSTEM") != 0)
  509. d_events = event_subsystem_dir(call->system, d_events);
  510. if (call->raw_init) {
  511. ret = call->raw_init();
  512. if (ret < 0) {
  513. pr_warning("Could not initialize trace point"
  514. " events/%s\n", call->name);
  515. return ret;
  516. }
  517. }
  518. /* default the output to printf */
  519. call->type = TRACE_EVENT_TYPE_PRINTF;
  520. call->dir = debugfs_create_dir(call->name, d_events);
  521. if (!call->dir) {
  522. pr_warning("Could not create debugfs "
  523. "'%s' directory\n", call->name);
  524. return -1;
  525. }
  526. if (call->regfunc) {
  527. entry = debugfs_create_file("enable", 0644, call->dir, call,
  528. &ftrace_enable_fops);
  529. if (!entry)
  530. pr_warning("Could not create debugfs "
  531. "'%s/enable' entry\n", call->name);
  532. }
  533. /* Only let type be writable, if we can change it */
  534. entry = debugfs_create_file("type",
  535. call->raw_init ? 0644 : 0444,
  536. call->dir, call,
  537. &ftrace_type_fops);
  538. if (!entry)
  539. pr_warning("Could not create debugfs "
  540. "'%s/type' entry\n", call->name);
  541. entry = debugfs_create_file("available_types", 0444, call->dir, call,
  542. &ftrace_available_types_fops);
  543. if (!entry)
  544. pr_warning("Could not create debugfs "
  545. "'%s/available_types' entry\n", call->name);
  546. /* A trace may not want to export its format */
  547. if (!call->show_format)
  548. return 0;
  549. entry = debugfs_create_file("format", 0444, call->dir, call,
  550. &ftrace_event_format_fops);
  551. if (!entry)
  552. pr_warning("Could not create debugfs "
  553. "'%s/format' entry\n", call->name);
  554. return 0;
  555. }
  556. static __init int event_trace_init(void)
  557. {
  558. struct ftrace_event_call *call = __start_ftrace_events;
  559. struct dentry *d_tracer;
  560. struct dentry *entry;
  561. struct dentry *d_events;
  562. d_tracer = tracing_init_dentry();
  563. if (!d_tracer)
  564. return 0;
  565. entry = debugfs_create_file("available_events", 0444, d_tracer,
  566. (void *)&show_event_seq_ops,
  567. &ftrace_avail_fops);
  568. if (!entry)
  569. pr_warning("Could not create debugfs "
  570. "'available_events' entry\n");
  571. entry = debugfs_create_file("set_event", 0644, d_tracer,
  572. (void *)&show_set_event_seq_ops,
  573. &ftrace_set_event_fops);
  574. if (!entry)
  575. pr_warning("Could not create debugfs "
  576. "'set_event' entry\n");
  577. d_events = event_trace_events_dir();
  578. if (!d_events)
  579. return 0;
  580. events_for_each(call) {
  581. /* The linker may leave blanks */
  582. if (!call->name)
  583. continue;
  584. event_create_dir(call, d_events);
  585. }
  586. return 0;
  587. }
  588. fs_initcall(event_trace_init);