trace_events.c 12 KB

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