trace_events.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. break;
  50. case 1:
  51. if (!call->enabled) {
  52. call->enabled = 1;
  53. call->regfunc();
  54. }
  55. break;
  56. }
  57. }
  58. static int ftrace_set_clr_event(char *buf, int set)
  59. {
  60. struct ftrace_event_call *call = __start_ftrace_events;
  61. char *event = NULL, *sub = NULL, *match;
  62. int ret = -EINVAL;
  63. /*
  64. * The buf format can be <subsystem>:<event-name>
  65. * *:<event-name> means any event by that name.
  66. * :<event-name> is the same.
  67. *
  68. * <subsystem>:* means all events in that subsystem
  69. * <subsystem>: means the same.
  70. *
  71. * <name> (no ':') means all events in a subsystem with
  72. * the name <name> or any event that matches <name>
  73. */
  74. match = strsep(&buf, ":");
  75. if (buf) {
  76. sub = match;
  77. event = buf;
  78. match = NULL;
  79. if (!strlen(sub) || strcmp(sub, "*") == 0)
  80. sub = NULL;
  81. if (!strlen(event) || strcmp(event, "*") == 0)
  82. event = NULL;
  83. }
  84. mutex_lock(&event_mutex);
  85. events_for_each(call) {
  86. if (!call->name)
  87. continue;
  88. if (match &&
  89. strcmp(match, call->name) != 0 &&
  90. strcmp(match, call->system) != 0)
  91. continue;
  92. if (sub && strcmp(sub, call->system) != 0)
  93. continue;
  94. if (event && strcmp(event, call->name) != 0)
  95. continue;
  96. ftrace_event_enable_disable(call, set);
  97. ret = 0;
  98. }
  99. mutex_unlock(&event_mutex);
  100. return ret;
  101. }
  102. /* 128 should be much more than enough */
  103. #define EVENT_BUF_SIZE 127
  104. static ssize_t
  105. ftrace_event_write(struct file *file, const char __user *ubuf,
  106. size_t cnt, loff_t *ppos)
  107. {
  108. size_t read = 0;
  109. int i, set = 1;
  110. ssize_t ret;
  111. char *buf;
  112. char ch;
  113. if (!cnt || cnt < 0)
  114. return 0;
  115. ret = get_user(ch, ubuf++);
  116. if (ret)
  117. return ret;
  118. read++;
  119. cnt--;
  120. /* skip white space */
  121. while (cnt && isspace(ch)) {
  122. ret = get_user(ch, ubuf++);
  123. if (ret)
  124. return ret;
  125. read++;
  126. cnt--;
  127. }
  128. /* Only white space found? */
  129. if (isspace(ch)) {
  130. file->f_pos += read;
  131. ret = read;
  132. return ret;
  133. }
  134. buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
  135. if (!buf)
  136. return -ENOMEM;
  137. if (cnt > EVENT_BUF_SIZE)
  138. cnt = EVENT_BUF_SIZE;
  139. i = 0;
  140. while (cnt && !isspace(ch)) {
  141. if (!i && ch == '!')
  142. set = 0;
  143. else
  144. buf[i++] = ch;
  145. ret = get_user(ch, ubuf++);
  146. if (ret)
  147. goto out_free;
  148. read++;
  149. cnt--;
  150. }
  151. buf[i] = 0;
  152. file->f_pos += read;
  153. ret = ftrace_set_clr_event(buf, set);
  154. if (ret)
  155. goto out_free;
  156. ret = read;
  157. out_free:
  158. kfree(buf);
  159. return ret;
  160. }
  161. static void *
  162. t_next(struct seq_file *m, void *v, loff_t *pos)
  163. {
  164. struct ftrace_event_call *call = m->private;
  165. struct ftrace_event_call *next = call;
  166. (*pos)++;
  167. if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
  168. return NULL;
  169. m->private = ++next;
  170. return call;
  171. }
  172. static void *t_start(struct seq_file *m, loff_t *pos)
  173. {
  174. return t_next(m, NULL, pos);
  175. }
  176. static void *
  177. s_next(struct seq_file *m, void *v, loff_t *pos)
  178. {
  179. struct ftrace_event_call *call = m->private;
  180. struct ftrace_event_call *next;
  181. (*pos)++;
  182. retry:
  183. if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
  184. return NULL;
  185. if (!call->enabled) {
  186. call++;
  187. goto retry;
  188. }
  189. next = call;
  190. m->private = ++next;
  191. return call;
  192. }
  193. static void *s_start(struct seq_file *m, loff_t *pos)
  194. {
  195. return s_next(m, NULL, pos);
  196. }
  197. static int t_show(struct seq_file *m, void *v)
  198. {
  199. struct ftrace_event_call *call = v;
  200. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  201. seq_printf(m, "%s:", call->system);
  202. seq_printf(m, "%s\n", call->name);
  203. return 0;
  204. }
  205. static void t_stop(struct seq_file *m, void *p)
  206. {
  207. }
  208. static int
  209. ftrace_event_seq_open(struct inode *inode, struct file *file)
  210. {
  211. int ret;
  212. const struct seq_operations *seq_ops;
  213. if ((file->f_mode & FMODE_WRITE) &&
  214. !(file->f_flags & O_APPEND))
  215. ftrace_clear_events();
  216. seq_ops = inode->i_private;
  217. ret = seq_open(file, seq_ops);
  218. if (!ret) {
  219. struct seq_file *m = file->private_data;
  220. m->private = __start_ftrace_events;
  221. }
  222. return ret;
  223. }
  224. static ssize_t
  225. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  226. loff_t *ppos)
  227. {
  228. struct ftrace_event_call *call = filp->private_data;
  229. char *buf;
  230. if (call->enabled)
  231. buf = "1\n";
  232. else
  233. buf = "0\n";
  234. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  235. }
  236. static ssize_t
  237. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  238. loff_t *ppos)
  239. {
  240. struct ftrace_event_call *call = filp->private_data;
  241. char buf[64];
  242. unsigned long val;
  243. int ret;
  244. if (cnt >= sizeof(buf))
  245. return -EINVAL;
  246. if (copy_from_user(&buf, ubuf, cnt))
  247. return -EFAULT;
  248. buf[cnt] = 0;
  249. ret = strict_strtoul(buf, 10, &val);
  250. if (ret < 0)
  251. return ret;
  252. switch (val) {
  253. case 0:
  254. case 1:
  255. mutex_lock(&event_mutex);
  256. ftrace_event_enable_disable(call, val);
  257. mutex_unlock(&event_mutex);
  258. break;
  259. default:
  260. return -EINVAL;
  261. }
  262. *ppos += cnt;
  263. return cnt;
  264. }
  265. #undef FIELD
  266. #define FIELD(type, name) \
  267. #type, #name, offsetof(typeof(field), name), sizeof(field.name)
  268. static int trace_write_header(struct trace_seq *s)
  269. {
  270. struct trace_entry field;
  271. /* struct trace_entry */
  272. return trace_seq_printf(s,
  273. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  274. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  275. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  276. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  277. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  278. "\n",
  279. FIELD(unsigned char, type),
  280. FIELD(unsigned char, flags),
  281. FIELD(unsigned char, preempt_count),
  282. FIELD(int, pid),
  283. FIELD(int, tgid));
  284. }
  285. static ssize_t
  286. event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
  287. loff_t *ppos)
  288. {
  289. struct ftrace_event_call *call = filp->private_data;
  290. struct trace_seq *s;
  291. char *buf;
  292. int r;
  293. s = kmalloc(sizeof(*s), GFP_KERNEL);
  294. if (!s)
  295. return -ENOMEM;
  296. trace_seq_init(s);
  297. if (*ppos)
  298. return 0;
  299. /* If any of the first writes fail, so will the show_format. */
  300. trace_seq_printf(s, "name: %s\n", call->name);
  301. trace_seq_printf(s, "ID: %d\n", call->id);
  302. trace_seq_printf(s, "format:\n");
  303. trace_write_header(s);
  304. r = call->show_format(s);
  305. if (!r) {
  306. /*
  307. * ug! The format output is bigger than a PAGE!!
  308. */
  309. buf = "FORMAT TOO BIG\n";
  310. r = simple_read_from_buffer(ubuf, cnt, ppos,
  311. buf, strlen(buf));
  312. goto out;
  313. }
  314. r = simple_read_from_buffer(ubuf, cnt, ppos,
  315. s->buffer, s->len);
  316. out:
  317. kfree(s);
  318. return r;
  319. }
  320. static const struct seq_operations show_event_seq_ops = {
  321. .start = t_start,
  322. .next = t_next,
  323. .show = t_show,
  324. .stop = t_stop,
  325. };
  326. static const struct seq_operations show_set_event_seq_ops = {
  327. .start = s_start,
  328. .next = s_next,
  329. .show = t_show,
  330. .stop = t_stop,
  331. };
  332. static const struct file_operations ftrace_set_event_fops = {
  333. .open = ftrace_event_seq_open,
  334. .read = seq_read,
  335. .write = ftrace_event_write,
  336. .llseek = seq_lseek,
  337. .release = seq_release,
  338. };
  339. static const struct file_operations ftrace_enable_fops = {
  340. .open = tracing_open_generic,
  341. .read = event_enable_read,
  342. .write = event_enable_write,
  343. };
  344. static const struct file_operations ftrace_event_format_fops = {
  345. .open = tracing_open_generic,
  346. .read = event_format_read,
  347. };
  348. static struct dentry *event_trace_events_dir(void)
  349. {
  350. static struct dentry *d_tracer;
  351. static struct dentry *d_events;
  352. if (d_events)
  353. return d_events;
  354. d_tracer = tracing_init_dentry();
  355. if (!d_tracer)
  356. return NULL;
  357. d_events = debugfs_create_dir("events", d_tracer);
  358. if (!d_events)
  359. pr_warning("Could not create debugfs "
  360. "'events' directory\n");
  361. return d_events;
  362. }
  363. struct event_subsystem {
  364. struct list_head list;
  365. const char *name;
  366. struct dentry *entry;
  367. };
  368. static LIST_HEAD(event_subsystems);
  369. static struct dentry *
  370. event_subsystem_dir(const char *name, struct dentry *d_events)
  371. {
  372. struct event_subsystem *system;
  373. /* First see if we did not already create this dir */
  374. list_for_each_entry(system, &event_subsystems, list) {
  375. if (strcmp(system->name, name) == 0)
  376. return system->entry;
  377. }
  378. /* need to create new entry */
  379. system = kmalloc(sizeof(*system), GFP_KERNEL);
  380. if (!system) {
  381. pr_warning("No memory to create event subsystem %s\n",
  382. name);
  383. return d_events;
  384. }
  385. system->entry = debugfs_create_dir(name, d_events);
  386. if (!system->entry) {
  387. pr_warning("Could not create event subsystem %s\n",
  388. name);
  389. kfree(system);
  390. return d_events;
  391. }
  392. system->name = name;
  393. list_add(&system->list, &event_subsystems);
  394. return system->entry;
  395. }
  396. static int
  397. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
  398. {
  399. struct dentry *entry;
  400. int ret;
  401. /*
  402. * If the trace point header did not define TRACE_SYSTEM
  403. * then the system would be called "TRACE_SYSTEM".
  404. */
  405. if (strcmp(call->system, "TRACE_SYSTEM") != 0)
  406. d_events = event_subsystem_dir(call->system, d_events);
  407. if (call->raw_init) {
  408. ret = call->raw_init();
  409. if (ret < 0) {
  410. pr_warning("Could not initialize trace point"
  411. " events/%s\n", call->name);
  412. return ret;
  413. }
  414. }
  415. call->dir = debugfs_create_dir(call->name, d_events);
  416. if (!call->dir) {
  417. pr_warning("Could not create debugfs "
  418. "'%s' directory\n", call->name);
  419. return -1;
  420. }
  421. if (call->regfunc) {
  422. entry = debugfs_create_file("enable", 0644, call->dir, call,
  423. &ftrace_enable_fops);
  424. if (!entry)
  425. pr_warning("Could not create debugfs "
  426. "'%s/enable' entry\n", call->name);
  427. }
  428. /* A trace may not want to export its format */
  429. if (!call->show_format)
  430. return 0;
  431. entry = debugfs_create_file("format", 0444, call->dir, call,
  432. &ftrace_event_format_fops);
  433. if (!entry)
  434. pr_warning("Could not create debugfs "
  435. "'%s/format' entry\n", call->name);
  436. return 0;
  437. }
  438. static __init int event_trace_init(void)
  439. {
  440. struct ftrace_event_call *call = __start_ftrace_events;
  441. struct dentry *d_tracer;
  442. struct dentry *entry;
  443. struct dentry *d_events;
  444. d_tracer = tracing_init_dentry();
  445. if (!d_tracer)
  446. return 0;
  447. entry = debugfs_create_file("set_event", 0644, d_tracer,
  448. (void *)&show_set_event_seq_ops,
  449. &ftrace_set_event_fops);
  450. if (!entry)
  451. pr_warning("Could not create debugfs "
  452. "'set_event' entry\n");
  453. d_events = event_trace_events_dir();
  454. if (!d_events)
  455. return 0;
  456. events_for_each(call) {
  457. /* The linker may leave blanks */
  458. if (!call->name)
  459. continue;
  460. event_create_dir(call, d_events);
  461. }
  462. return 0;
  463. }
  464. fs_initcall(event_trace_init);