PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/trace/trace_events.c

https://github.com/mstsirkin/kvm
C | 1747 lines | 1284 code | 346 blank | 117 comment | 193 complexity | ca25c708615ccc90fed907050219bd74 MD5 | raw file
  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/workqueue.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/kthread.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/module.h>
  16. #include <linux/ctype.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <asm/setup.h>
  20. #include "trace_output.h"
  21. #undef TRACE_SYSTEM
  22. #define TRACE_SYSTEM "TRACE_SYSTEM"
  23. DEFINE_MUTEX(event_mutex);
  24. DEFINE_MUTEX(event_storage_mutex);
  25. EXPORT_SYMBOL_GPL(event_storage_mutex);
  26. char event_storage[EVENT_STORAGE_SIZE];
  27. EXPORT_SYMBOL_GPL(event_storage);
  28. LIST_HEAD(ftrace_events);
  29. LIST_HEAD(ftrace_common_fields);
  30. struct list_head *
  31. trace_get_fields(struct ftrace_event_call *event_call)
  32. {
  33. if (!event_call->class->get_fields)
  34. return &event_call->class->fields;
  35. return event_call->class->get_fields(event_call);
  36. }
  37. static int __trace_define_field(struct list_head *head, const char *type,
  38. const char *name, int offset, int size,
  39. int is_signed, int filter_type)
  40. {
  41. struct ftrace_event_field *field;
  42. field = kzalloc(sizeof(*field), GFP_KERNEL);
  43. if (!field)
  44. goto err;
  45. field->name = kstrdup(name, GFP_KERNEL);
  46. if (!field->name)
  47. goto err;
  48. field->type = kstrdup(type, GFP_KERNEL);
  49. if (!field->type)
  50. goto err;
  51. if (filter_type == FILTER_OTHER)
  52. field->filter_type = filter_assign_type(type);
  53. else
  54. field->filter_type = filter_type;
  55. field->offset = offset;
  56. field->size = size;
  57. field->is_signed = is_signed;
  58. list_add(&field->link, head);
  59. return 0;
  60. err:
  61. if (field)
  62. kfree(field->name);
  63. kfree(field);
  64. return -ENOMEM;
  65. }
  66. int trace_define_field(struct ftrace_event_call *call, const char *type,
  67. const char *name, int offset, int size, int is_signed,
  68. int filter_type)
  69. {
  70. struct list_head *head;
  71. if (WARN_ON(!call->class))
  72. return 0;
  73. head = trace_get_fields(call);
  74. return __trace_define_field(head, type, name, offset, size,
  75. is_signed, filter_type);
  76. }
  77. EXPORT_SYMBOL_GPL(trace_define_field);
  78. #define __common_field(type, item) \
  79. ret = __trace_define_field(&ftrace_common_fields, #type, \
  80. "common_" #item, \
  81. offsetof(typeof(ent), item), \
  82. sizeof(ent.item), \
  83. is_signed_type(type), FILTER_OTHER); \
  84. if (ret) \
  85. return ret;
  86. static int trace_define_common_fields(void)
  87. {
  88. int ret;
  89. struct trace_entry ent;
  90. __common_field(unsigned short, type);
  91. __common_field(unsigned char, flags);
  92. __common_field(unsigned char, preempt_count);
  93. __common_field(int, pid);
  94. __common_field(int, padding);
  95. return ret;
  96. }
  97. void trace_destroy_fields(struct ftrace_event_call *call)
  98. {
  99. struct ftrace_event_field *field, *next;
  100. struct list_head *head;
  101. head = trace_get_fields(call);
  102. list_for_each_entry_safe(field, next, head, link) {
  103. list_del(&field->link);
  104. kfree(field->type);
  105. kfree(field->name);
  106. kfree(field);
  107. }
  108. }
  109. int trace_event_raw_init(struct ftrace_event_call *call)
  110. {
  111. int id;
  112. id = register_ftrace_event(&call->event);
  113. if (!id)
  114. return -ENODEV;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(trace_event_raw_init);
  118. int ftrace_event_reg(struct ftrace_event_call *call, enum trace_reg type)
  119. {
  120. switch (type) {
  121. case TRACE_REG_REGISTER:
  122. return tracepoint_probe_register(call->name,
  123. call->class->probe,
  124. call);
  125. case TRACE_REG_UNREGISTER:
  126. tracepoint_probe_unregister(call->name,
  127. call->class->probe,
  128. call);
  129. return 0;
  130. #ifdef CONFIG_PERF_EVENTS
  131. case TRACE_REG_PERF_REGISTER:
  132. return tracepoint_probe_register(call->name,
  133. call->class->perf_probe,
  134. call);
  135. case TRACE_REG_PERF_UNREGISTER:
  136. tracepoint_probe_unregister(call->name,
  137. call->class->perf_probe,
  138. call);
  139. return 0;
  140. #endif
  141. }
  142. return 0;
  143. }
  144. EXPORT_SYMBOL_GPL(ftrace_event_reg);
  145. void trace_event_enable_cmd_record(bool enable)
  146. {
  147. struct ftrace_event_call *call;
  148. mutex_lock(&event_mutex);
  149. list_for_each_entry(call, &ftrace_events, list) {
  150. if (!(call->flags & TRACE_EVENT_FL_ENABLED))
  151. continue;
  152. if (enable) {
  153. tracing_start_cmdline_record();
  154. call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
  155. } else {
  156. tracing_stop_cmdline_record();
  157. call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
  158. }
  159. }
  160. mutex_unlock(&event_mutex);
  161. }
  162. static int ftrace_event_enable_disable(struct ftrace_event_call *call,
  163. int enable)
  164. {
  165. int ret = 0;
  166. switch (enable) {
  167. case 0:
  168. if (call->flags & TRACE_EVENT_FL_ENABLED) {
  169. call->flags &= ~TRACE_EVENT_FL_ENABLED;
  170. if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) {
  171. tracing_stop_cmdline_record();
  172. call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD;
  173. }
  174. call->class->reg(call, TRACE_REG_UNREGISTER);
  175. }
  176. break;
  177. case 1:
  178. if (!(call->flags & TRACE_EVENT_FL_ENABLED)) {
  179. if (trace_flags & TRACE_ITER_RECORD_CMD) {
  180. tracing_start_cmdline_record();
  181. call->flags |= TRACE_EVENT_FL_RECORDED_CMD;
  182. }
  183. ret = call->class->reg(call, TRACE_REG_REGISTER);
  184. if (ret) {
  185. tracing_stop_cmdline_record();
  186. pr_info("event trace: Could not enable event "
  187. "%s\n", call->name);
  188. break;
  189. }
  190. call->flags |= TRACE_EVENT_FL_ENABLED;
  191. }
  192. break;
  193. }
  194. return ret;
  195. }
  196. static void ftrace_clear_events(void)
  197. {
  198. struct ftrace_event_call *call;
  199. mutex_lock(&event_mutex);
  200. list_for_each_entry(call, &ftrace_events, list) {
  201. ftrace_event_enable_disable(call, 0);
  202. }
  203. mutex_unlock(&event_mutex);
  204. }
  205. static void __put_system(struct event_subsystem *system)
  206. {
  207. struct event_filter *filter = system->filter;
  208. WARN_ON_ONCE(system->ref_count == 0);
  209. if (--system->ref_count)
  210. return;
  211. if (filter) {
  212. kfree(filter->filter_string);
  213. kfree(filter);
  214. }
  215. kfree(system->name);
  216. kfree(system);
  217. }
  218. static void __get_system(struct event_subsystem *system)
  219. {
  220. WARN_ON_ONCE(system->ref_count == 0);
  221. system->ref_count++;
  222. }
  223. static void put_system(struct event_subsystem *system)
  224. {
  225. mutex_lock(&event_mutex);
  226. __put_system(system);
  227. mutex_unlock(&event_mutex);
  228. }
  229. /*
  230. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  231. */
  232. static int __ftrace_set_clr_event(const char *match, const char *sub,
  233. const char *event, int set)
  234. {
  235. struct ftrace_event_call *call;
  236. int ret = -EINVAL;
  237. mutex_lock(&event_mutex);
  238. list_for_each_entry(call, &ftrace_events, list) {
  239. if (!call->name || !call->class || !call->class->reg)
  240. continue;
  241. if (match &&
  242. strcmp(match, call->name) != 0 &&
  243. strcmp(match, call->class->system) != 0)
  244. continue;
  245. if (sub && strcmp(sub, call->class->system) != 0)
  246. continue;
  247. if (event && strcmp(event, call->name) != 0)
  248. continue;
  249. ftrace_event_enable_disable(call, set);
  250. ret = 0;
  251. }
  252. mutex_unlock(&event_mutex);
  253. return ret;
  254. }
  255. static int ftrace_set_clr_event(char *buf, int set)
  256. {
  257. char *event = NULL, *sub = NULL, *match;
  258. /*
  259. * The buf format can be <subsystem>:<event-name>
  260. * *:<event-name> means any event by that name.
  261. * :<event-name> is the same.
  262. *
  263. * <subsystem>:* means all events in that subsystem
  264. * <subsystem>: means the same.
  265. *
  266. * <name> (no ':') means all events in a subsystem with
  267. * the name <name> or any event that matches <name>
  268. */
  269. match = strsep(&buf, ":");
  270. if (buf) {
  271. sub = match;
  272. event = buf;
  273. match = NULL;
  274. if (!strlen(sub) || strcmp(sub, "*") == 0)
  275. sub = NULL;
  276. if (!strlen(event) || strcmp(event, "*") == 0)
  277. event = NULL;
  278. }
  279. return __ftrace_set_clr_event(match, sub, event, set);
  280. }
  281. /**
  282. * trace_set_clr_event - enable or disable an event
  283. * @system: system name to match (NULL for any system)
  284. * @event: event name to match (NULL for all events, within system)
  285. * @set: 1 to enable, 0 to disable
  286. *
  287. * This is a way for other parts of the kernel to enable or disable
  288. * event recording.
  289. *
  290. * Returns 0 on success, -EINVAL if the parameters do not match any
  291. * registered events.
  292. */
  293. int trace_set_clr_event(const char *system, const char *event, int set)
  294. {
  295. return __ftrace_set_clr_event(NULL, system, event, set);
  296. }
  297. EXPORT_SYMBOL_GPL(trace_set_clr_event);
  298. /* 128 should be much more than enough */
  299. #define EVENT_BUF_SIZE 127
  300. static ssize_t
  301. ftrace_event_write(struct file *file, const char __user *ubuf,
  302. size_t cnt, loff_t *ppos)
  303. {
  304. struct trace_parser parser;
  305. ssize_t read, ret;
  306. if (!cnt)
  307. return 0;
  308. ret = tracing_update_buffers();
  309. if (ret < 0)
  310. return ret;
  311. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  312. return -ENOMEM;
  313. read = trace_get_user(&parser, ubuf, cnt, ppos);
  314. if (read >= 0 && trace_parser_loaded((&parser))) {
  315. int set = 1;
  316. if (*parser.buffer == '!')
  317. set = 0;
  318. parser.buffer[parser.idx] = 0;
  319. ret = ftrace_set_clr_event(parser.buffer + !set, set);
  320. if (ret)
  321. goto out_put;
  322. }
  323. ret = read;
  324. out_put:
  325. trace_parser_put(&parser);
  326. return ret;
  327. }
  328. static void *
  329. t_next(struct seq_file *m, void *v, loff_t *pos)
  330. {
  331. struct ftrace_event_call *call = v;
  332. (*pos)++;
  333. list_for_each_entry_continue(call, &ftrace_events, list) {
  334. /*
  335. * The ftrace subsystem is for showing formats only.
  336. * They can not be enabled or disabled via the event files.
  337. */
  338. if (call->class && call->class->reg)
  339. return call;
  340. }
  341. return NULL;
  342. }
  343. static void *t_start(struct seq_file *m, loff_t *pos)
  344. {
  345. struct ftrace_event_call *call;
  346. loff_t l;
  347. mutex_lock(&event_mutex);
  348. call = list_entry(&ftrace_events, struct ftrace_event_call, list);
  349. for (l = 0; l <= *pos; ) {
  350. call = t_next(m, call, &l);
  351. if (!call)
  352. break;
  353. }
  354. return call;
  355. }
  356. static void *
  357. s_next(struct seq_file *m, void *v, loff_t *pos)
  358. {
  359. struct ftrace_event_call *call = v;
  360. (*pos)++;
  361. list_for_each_entry_continue(call, &ftrace_events, list) {
  362. if (call->flags & TRACE_EVENT_FL_ENABLED)
  363. return call;
  364. }
  365. return NULL;
  366. }
  367. static void *s_start(struct seq_file *m, loff_t *pos)
  368. {
  369. struct ftrace_event_call *call;
  370. loff_t l;
  371. mutex_lock(&event_mutex);
  372. call = list_entry(&ftrace_events, struct ftrace_event_call, list);
  373. for (l = 0; l <= *pos; ) {
  374. call = s_next(m, call, &l);
  375. if (!call)
  376. break;
  377. }
  378. return call;
  379. }
  380. static int t_show(struct seq_file *m, void *v)
  381. {
  382. struct ftrace_event_call *call = v;
  383. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  384. seq_printf(m, "%s:", call->class->system);
  385. seq_printf(m, "%s\n", call->name);
  386. return 0;
  387. }
  388. static void t_stop(struct seq_file *m, void *p)
  389. {
  390. mutex_unlock(&event_mutex);
  391. }
  392. static int
  393. ftrace_event_seq_open(struct inode *inode, struct file *file)
  394. {
  395. const struct seq_operations *seq_ops;
  396. if ((file->f_mode & FMODE_WRITE) &&
  397. (file->f_flags & O_TRUNC))
  398. ftrace_clear_events();
  399. seq_ops = inode->i_private;
  400. return seq_open(file, seq_ops);
  401. }
  402. static ssize_t
  403. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  404. loff_t *ppos)
  405. {
  406. struct ftrace_event_call *call = filp->private_data;
  407. char *buf;
  408. if (call->flags & TRACE_EVENT_FL_ENABLED)
  409. buf = "1\n";
  410. else
  411. buf = "0\n";
  412. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  413. }
  414. static ssize_t
  415. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  416. loff_t *ppos)
  417. {
  418. struct ftrace_event_call *call = filp->private_data;
  419. unsigned long val;
  420. int ret;
  421. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  422. if (ret)
  423. return ret;
  424. ret = tracing_update_buffers();
  425. if (ret < 0)
  426. return ret;
  427. switch (val) {
  428. case 0:
  429. case 1:
  430. mutex_lock(&event_mutex);
  431. ret = ftrace_event_enable_disable(call, val);
  432. mutex_unlock(&event_mutex);
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. *ppos += cnt;
  438. return ret ? ret : cnt;
  439. }
  440. static ssize_t
  441. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  442. loff_t *ppos)
  443. {
  444. const char set_to_char[4] = { '?', '0', '1', 'X' };
  445. struct event_subsystem *system = filp->private_data;
  446. struct ftrace_event_call *call;
  447. char buf[2];
  448. int set = 0;
  449. int ret;
  450. mutex_lock(&event_mutex);
  451. list_for_each_entry(call, &ftrace_events, list) {
  452. if (!call->name || !call->class || !call->class->reg)
  453. continue;
  454. if (system && strcmp(call->class->system, system->name) != 0)
  455. continue;
  456. /*
  457. * We need to find out if all the events are set
  458. * or if all events or cleared, or if we have
  459. * a mixture.
  460. */
  461. set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED));
  462. /*
  463. * If we have a mixture, no need to look further.
  464. */
  465. if (set == 3)
  466. break;
  467. }
  468. mutex_unlock(&event_mutex);
  469. buf[0] = set_to_char[set];
  470. buf[1] = '\n';
  471. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  472. return ret;
  473. }
  474. static ssize_t
  475. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  476. loff_t *ppos)
  477. {
  478. struct event_subsystem *system = filp->private_data;
  479. const char *name = NULL;
  480. unsigned long val;
  481. ssize_t ret;
  482. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  483. if (ret)
  484. return ret;
  485. ret = tracing_update_buffers();
  486. if (ret < 0)
  487. return ret;
  488. if (val != 0 && val != 1)
  489. return -EINVAL;
  490. /*
  491. * Opening of "enable" adds a ref count to system,
  492. * so the name is safe to use.
  493. */
  494. if (system)
  495. name = system->name;
  496. ret = __ftrace_set_clr_event(NULL, name, NULL, val);
  497. if (ret)
  498. goto out;
  499. ret = cnt;
  500. out:
  501. *ppos += cnt;
  502. return ret;
  503. }
  504. enum {
  505. FORMAT_HEADER = 1,
  506. FORMAT_FIELD_SEPERATOR = 2,
  507. FORMAT_PRINTFMT = 3,
  508. };
  509. static void *f_next(struct seq_file *m, void *v, loff_t *pos)
  510. {
  511. struct ftrace_event_call *call = m->private;
  512. struct ftrace_event_field *field;
  513. struct list_head *common_head = &ftrace_common_fields;
  514. struct list_head *head = trace_get_fields(call);
  515. (*pos)++;
  516. switch ((unsigned long)v) {
  517. case FORMAT_HEADER:
  518. if (unlikely(list_empty(common_head)))
  519. return NULL;
  520. field = list_entry(common_head->prev,
  521. struct ftrace_event_field, link);
  522. return field;
  523. case FORMAT_FIELD_SEPERATOR:
  524. if (unlikely(list_empty(head)))
  525. return NULL;
  526. field = list_entry(head->prev, struct ftrace_event_field, link);
  527. return field;
  528. case FORMAT_PRINTFMT:
  529. /* all done */
  530. return NULL;
  531. }
  532. field = v;
  533. if (field->link.prev == common_head)
  534. return (void *)FORMAT_FIELD_SEPERATOR;
  535. else if (field->link.prev == head)
  536. return (void *)FORMAT_PRINTFMT;
  537. field = list_entry(field->link.prev, struct ftrace_event_field, link);
  538. return field;
  539. }
  540. static void *f_start(struct seq_file *m, loff_t *pos)
  541. {
  542. loff_t l = 0;
  543. void *p;
  544. /* Start by showing the header */
  545. if (!*pos)
  546. return (void *)FORMAT_HEADER;
  547. p = (void *)FORMAT_HEADER;
  548. do {
  549. p = f_next(m, p, &l);
  550. } while (p && l < *pos);
  551. return p;
  552. }
  553. static int f_show(struct seq_file *m, void *v)
  554. {
  555. struct ftrace_event_call *call = m->private;
  556. struct ftrace_event_field *field;
  557. const char *array_descriptor;
  558. switch ((unsigned long)v) {
  559. case FORMAT_HEADER:
  560. seq_printf(m, "name: %s\n", call->name);
  561. seq_printf(m, "ID: %d\n", call->event.type);
  562. seq_printf(m, "format:\n");
  563. return 0;
  564. case FORMAT_FIELD_SEPERATOR:
  565. seq_putc(m, '\n');
  566. return 0;
  567. case FORMAT_PRINTFMT:
  568. seq_printf(m, "\nprint fmt: %s\n",
  569. call->print_fmt);
  570. return 0;
  571. }
  572. field = v;
  573. /*
  574. * Smartly shows the array type(except dynamic array).
  575. * Normal:
  576. * field:TYPE VAR
  577. * If TYPE := TYPE[LEN], it is shown:
  578. * field:TYPE VAR[LEN]
  579. */
  580. array_descriptor = strchr(field->type, '[');
  581. if (!strncmp(field->type, "__data_loc", 10))
  582. array_descriptor = NULL;
  583. if (!array_descriptor)
  584. seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  585. field->type, field->name, field->offset,
  586. field->size, !!field->is_signed);
  587. else
  588. seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  589. (int)(array_descriptor - field->type),
  590. field->type, field->name,
  591. array_descriptor, field->offset,
  592. field->size, !!field->is_signed);
  593. return 0;
  594. }
  595. static void f_stop(struct seq_file *m, void *p)
  596. {
  597. }
  598. static const struct seq_operations trace_format_seq_ops = {
  599. .start = f_start,
  600. .next = f_next,
  601. .stop = f_stop,
  602. .show = f_show,
  603. };
  604. static int trace_format_open(struct inode *inode, struct file *file)
  605. {
  606. struct ftrace_event_call *call = inode->i_private;
  607. struct seq_file *m;
  608. int ret;
  609. ret = seq_open(file, &trace_format_seq_ops);
  610. if (ret < 0)
  611. return ret;
  612. m = file->private_data;
  613. m->private = call;
  614. return 0;
  615. }
  616. static ssize_t
  617. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  618. {
  619. struct ftrace_event_call *call = filp->private_data;
  620. struct trace_seq *s;
  621. int r;
  622. if (*ppos)
  623. return 0;
  624. s = kmalloc(sizeof(*s), GFP_KERNEL);
  625. if (!s)
  626. return -ENOMEM;
  627. trace_seq_init(s);
  628. trace_seq_printf(s, "%d\n", call->event.type);
  629. r = simple_read_from_buffer(ubuf, cnt, ppos,
  630. s->buffer, s->len);
  631. kfree(s);
  632. return r;
  633. }
  634. static ssize_t
  635. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  636. loff_t *ppos)
  637. {
  638. struct ftrace_event_call *call = filp->private_data;
  639. struct trace_seq *s;
  640. int r;
  641. if (*ppos)
  642. return 0;
  643. s = kmalloc(sizeof(*s), GFP_KERNEL);
  644. if (!s)
  645. return -ENOMEM;
  646. trace_seq_init(s);
  647. print_event_filter(call, s);
  648. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  649. kfree(s);
  650. return r;
  651. }
  652. static ssize_t
  653. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  654. loff_t *ppos)
  655. {
  656. struct ftrace_event_call *call = filp->private_data;
  657. char *buf;
  658. int err;
  659. if (cnt >= PAGE_SIZE)
  660. return -EINVAL;
  661. buf = (char *)__get_free_page(GFP_TEMPORARY);
  662. if (!buf)
  663. return -ENOMEM;
  664. if (copy_from_user(buf, ubuf, cnt)) {
  665. free_page((unsigned long) buf);
  666. return -EFAULT;
  667. }
  668. buf[cnt] = '\0';
  669. err = apply_event_filter(call, buf);
  670. free_page((unsigned long) buf);
  671. if (err < 0)
  672. return err;
  673. *ppos += cnt;
  674. return cnt;
  675. }
  676. static LIST_HEAD(event_subsystems);
  677. static int subsystem_open(struct inode *inode, struct file *filp)
  678. {
  679. struct event_subsystem *system = NULL;
  680. int ret;
  681. if (!inode->i_private)
  682. goto skip_search;
  683. /* Make sure the system still exists */
  684. mutex_lock(&event_mutex);
  685. list_for_each_entry(system, &event_subsystems, list) {
  686. if (system == inode->i_private) {
  687. /* Don't open systems with no events */
  688. if (!system->nr_events) {
  689. system = NULL;
  690. break;
  691. }
  692. __get_system(system);
  693. break;
  694. }
  695. }
  696. mutex_unlock(&event_mutex);
  697. if (system != inode->i_private)
  698. return -ENODEV;
  699. skip_search:
  700. ret = tracing_open_generic(inode, filp);
  701. if (ret < 0 && system)
  702. put_system(system);
  703. return ret;
  704. }
  705. static int subsystem_release(struct inode *inode, struct file *file)
  706. {
  707. struct event_subsystem *system = inode->i_private;
  708. if (system)
  709. put_system(system);
  710. return 0;
  711. }
  712. static ssize_t
  713. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  714. loff_t *ppos)
  715. {
  716. struct event_subsystem *system = filp->private_data;
  717. struct trace_seq *s;
  718. int r;
  719. if (*ppos)
  720. return 0;
  721. s = kmalloc(sizeof(*s), GFP_KERNEL);
  722. if (!s)
  723. return -ENOMEM;
  724. trace_seq_init(s);
  725. print_subsystem_event_filter(system, s);
  726. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  727. kfree(s);
  728. return r;
  729. }
  730. static ssize_t
  731. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  732. loff_t *ppos)
  733. {
  734. struct event_subsystem *system = filp->private_data;
  735. char *buf;
  736. int err;
  737. if (cnt >= PAGE_SIZE)
  738. return -EINVAL;
  739. buf = (char *)__get_free_page(GFP_TEMPORARY);
  740. if (!buf)
  741. return -ENOMEM;
  742. if (copy_from_user(buf, ubuf, cnt)) {
  743. free_page((unsigned long) buf);
  744. return -EFAULT;
  745. }
  746. buf[cnt] = '\0';
  747. err = apply_subsystem_event_filter(system, buf);
  748. free_page((unsigned long) buf);
  749. if (err < 0)
  750. return err;
  751. *ppos += cnt;
  752. return cnt;
  753. }
  754. static ssize_t
  755. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  756. {
  757. int (*func)(struct trace_seq *s) = filp->private_data;
  758. struct trace_seq *s;
  759. int r;
  760. if (*ppos)
  761. return 0;
  762. s = kmalloc(sizeof(*s), GFP_KERNEL);
  763. if (!s)
  764. return -ENOMEM;
  765. trace_seq_init(s);
  766. func(s);
  767. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  768. kfree(s);
  769. return r;
  770. }
  771. static const struct seq_operations show_event_seq_ops = {
  772. .start = t_start,
  773. .next = t_next,
  774. .show = t_show,
  775. .stop = t_stop,
  776. };
  777. static const struct seq_operations show_set_event_seq_ops = {
  778. .start = s_start,
  779. .next = s_next,
  780. .show = t_show,
  781. .stop = t_stop,
  782. };
  783. static const struct file_operations ftrace_avail_fops = {
  784. .open = ftrace_event_seq_open,
  785. .read = seq_read,
  786. .llseek = seq_lseek,
  787. .release = seq_release,
  788. };
  789. static const struct file_operations ftrace_set_event_fops = {
  790. .open = ftrace_event_seq_open,
  791. .read = seq_read,
  792. .write = ftrace_event_write,
  793. .llseek = seq_lseek,
  794. .release = seq_release,
  795. };
  796. static const struct file_operations ftrace_enable_fops = {
  797. .open = tracing_open_generic,
  798. .read = event_enable_read,
  799. .write = event_enable_write,
  800. .llseek = default_llseek,
  801. };
  802. static const struct file_operations ftrace_event_format_fops = {
  803. .open = trace_format_open,
  804. .read = seq_read,
  805. .llseek = seq_lseek,
  806. .release = seq_release,
  807. };
  808. static const struct file_operations ftrace_event_id_fops = {
  809. .open = tracing_open_generic,
  810. .read = event_id_read,
  811. .llseek = default_llseek,
  812. };
  813. static const struct file_operations ftrace_event_filter_fops = {
  814. .open = tracing_open_generic,
  815. .read = event_filter_read,
  816. .write = event_filter_write,
  817. .llseek = default_llseek,
  818. };
  819. static const struct file_operations ftrace_subsystem_filter_fops = {
  820. .open = subsystem_open,
  821. .read = subsystem_filter_read,
  822. .write = subsystem_filter_write,
  823. .llseek = default_llseek,
  824. .release = subsystem_release,
  825. };
  826. static const struct file_operations ftrace_system_enable_fops = {
  827. .open = subsystem_open,
  828. .read = system_enable_read,
  829. .write = system_enable_write,
  830. .llseek = default_llseek,
  831. .release = subsystem_release,
  832. };
  833. static const struct file_operations ftrace_show_header_fops = {
  834. .open = tracing_open_generic,
  835. .read = show_header,
  836. .llseek = default_llseek,
  837. };
  838. static struct dentry *event_trace_events_dir(void)
  839. {
  840. static struct dentry *d_tracer;
  841. static struct dentry *d_events;
  842. if (d_events)
  843. return d_events;
  844. d_tracer = tracing_init_dentry();
  845. if (!d_tracer)
  846. return NULL;
  847. d_events = debugfs_create_dir("events", d_tracer);
  848. if (!d_events)
  849. pr_warning("Could not create debugfs "
  850. "'events' directory\n");
  851. return d_events;
  852. }
  853. static struct dentry *
  854. event_subsystem_dir(const char *name, struct dentry *d_events)
  855. {
  856. struct event_subsystem *system;
  857. struct dentry *entry;
  858. /* First see if we did not already create this dir */
  859. list_for_each_entry(system, &event_subsystems, list) {
  860. if (strcmp(system->name, name) == 0) {
  861. __get_system(system);
  862. system->nr_events++;
  863. return system->entry;
  864. }
  865. }
  866. /* need to create new entry */
  867. system = kmalloc(sizeof(*system), GFP_KERNEL);
  868. if (!system) {
  869. pr_warning("No memory to create event subsystem %s\n",
  870. name);
  871. return d_events;
  872. }
  873. system->entry = debugfs_create_dir(name, d_events);
  874. if (!system->entry) {
  875. pr_warning("Could not create event subsystem %s\n",
  876. name);
  877. kfree(system);
  878. return d_events;
  879. }
  880. system->nr_events = 1;
  881. system->ref_count = 1;
  882. system->name = kstrdup(name, GFP_KERNEL);
  883. if (!system->name) {
  884. debugfs_remove(system->entry);
  885. kfree(system);
  886. return d_events;
  887. }
  888. list_add(&system->list, &event_subsystems);
  889. system->filter = NULL;
  890. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  891. if (!system->filter) {
  892. pr_warning("Could not allocate filter for subsystem "
  893. "'%s'\n", name);
  894. return system->entry;
  895. }
  896. entry = debugfs_create_file("filter", 0644, system->entry, system,
  897. &ftrace_subsystem_filter_fops);
  898. if (!entry) {
  899. kfree(system->filter);
  900. system->filter = NULL;
  901. pr_warning("Could not create debugfs "
  902. "'%s/filter' entry\n", name);
  903. }
  904. trace_create_file("enable", 0644, system->entry, system,
  905. &ftrace_system_enable_fops);
  906. return system->entry;
  907. }
  908. static int
  909. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
  910. const struct file_operations *id,
  911. const struct file_operations *enable,
  912. const struct file_operations *filter,
  913. const struct file_operations *format)
  914. {
  915. struct list_head *head;
  916. int ret;
  917. /*
  918. * If the trace point header did not define TRACE_SYSTEM
  919. * then the system would be called "TRACE_SYSTEM".
  920. */
  921. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  922. d_events = event_subsystem_dir(call->class->system, d_events);
  923. call->dir = debugfs_create_dir(call->name, d_events);
  924. if (!call->dir) {
  925. pr_warning("Could not create debugfs "
  926. "'%s' directory\n", call->name);
  927. return -1;
  928. }
  929. if (call->class->reg)
  930. trace_create_file("enable", 0644, call->dir, call,
  931. enable);
  932. #ifdef CONFIG_PERF_EVENTS
  933. if (call->event.type && call->class->reg)
  934. trace_create_file("id", 0444, call->dir, call,
  935. id);
  936. #endif
  937. /*
  938. * Other events may have the same class. Only update
  939. * the fields if they are not already defined.
  940. */
  941. head = trace_get_fields(call);
  942. if (list_empty(head)) {
  943. ret = call->class->define_fields(call);
  944. if (ret < 0) {
  945. pr_warning("Could not initialize trace point"
  946. " events/%s\n", call->name);
  947. return ret;
  948. }
  949. }
  950. trace_create_file("filter", 0644, call->dir, call,
  951. filter);
  952. trace_create_file("format", 0444, call->dir, call,
  953. format);
  954. return 0;
  955. }
  956. static int
  957. __trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
  958. const struct file_operations *id,
  959. const struct file_operations *enable,
  960. const struct file_operations *filter,
  961. const struct file_operations *format)
  962. {
  963. struct dentry *d_events;
  964. int ret;
  965. /* The linker may leave blanks */
  966. if (!call->name)
  967. return -EINVAL;
  968. if (call->class->raw_init) {
  969. ret = call->class->raw_init(call);
  970. if (ret < 0) {
  971. if (ret != -ENOSYS)
  972. pr_warning("Could not initialize trace events/%s\n",
  973. call->name);
  974. return ret;
  975. }
  976. }
  977. d_events = event_trace_events_dir();
  978. if (!d_events)
  979. return -ENOENT;
  980. ret = event_create_dir(call, d_events, id, enable, filter, format);
  981. if (!ret)
  982. list_add(&call->list, &ftrace_events);
  983. call->mod = mod;
  984. return ret;
  985. }
  986. /* Add an additional event_call dynamically */
  987. int trace_add_event_call(struct ftrace_event_call *call)
  988. {
  989. int ret;
  990. mutex_lock(&event_mutex);
  991. ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
  992. &ftrace_enable_fops,
  993. &ftrace_event_filter_fops,
  994. &ftrace_event_format_fops);
  995. mutex_unlock(&event_mutex);
  996. return ret;
  997. }
  998. static void remove_subsystem_dir(const char *name)
  999. {
  1000. struct event_subsystem *system;
  1001. if (strcmp(name, TRACE_SYSTEM) == 0)
  1002. return;
  1003. list_for_each_entry(system, &event_subsystems, list) {
  1004. if (strcmp(system->name, name) == 0) {
  1005. if (!--system->nr_events) {
  1006. debugfs_remove_recursive(system->entry);
  1007. list_del(&system->list);
  1008. __put_system(system);
  1009. }
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. /*
  1015. * Must be called under locking both of event_mutex and trace_event_mutex.
  1016. */
  1017. static void __trace_remove_event_call(struct ftrace_event_call *call)
  1018. {
  1019. ftrace_event_enable_disable(call, 0);
  1020. if (call->event.funcs)
  1021. __unregister_ftrace_event(&call->event);
  1022. debugfs_remove_recursive(call->dir);
  1023. list_del(&call->list);
  1024. trace_destroy_fields(call);
  1025. destroy_preds(call);
  1026. remove_subsystem_dir(call->class->system);
  1027. }
  1028. /* Remove an event_call */
  1029. void trace_remove_event_call(struct ftrace_event_call *call)
  1030. {
  1031. mutex_lock(&event_mutex);
  1032. down_write(&trace_event_mutex);
  1033. __trace_remove_event_call(call);
  1034. up_write(&trace_event_mutex);
  1035. mutex_unlock(&event_mutex);
  1036. }
  1037. #define for_each_event(event, start, end) \
  1038. for (event = start; \
  1039. (unsigned long)event < (unsigned long)end; \
  1040. event++)
  1041. #ifdef CONFIG_MODULES
  1042. static LIST_HEAD(ftrace_module_file_list);
  1043. /*
  1044. * Modules must own their file_operations to keep up with
  1045. * reference counting.
  1046. */
  1047. struct ftrace_module_file_ops {
  1048. struct list_head list;
  1049. struct module *mod;
  1050. struct file_operations id;
  1051. struct file_operations enable;
  1052. struct file_operations format;
  1053. struct file_operations filter;
  1054. };
  1055. static struct ftrace_module_file_ops *
  1056. trace_create_file_ops(struct module *mod)
  1057. {
  1058. struct ftrace_module_file_ops *file_ops;
  1059. /*
  1060. * This is a bit of a PITA. To allow for correct reference
  1061. * counting, modules must "own" their file_operations.
  1062. * To do this, we allocate the file operations that will be
  1063. * used in the event directory.
  1064. */
  1065. file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
  1066. if (!file_ops)
  1067. return NULL;
  1068. file_ops->mod = mod;
  1069. file_ops->id = ftrace_event_id_fops;
  1070. file_ops->id.owner = mod;
  1071. file_ops->enable = ftrace_enable_fops;
  1072. file_ops->enable.owner = mod;
  1073. file_ops->filter = ftrace_event_filter_fops;
  1074. file_ops->filter.owner = mod;
  1075. file_ops->format = ftrace_event_format_fops;
  1076. file_ops->format.owner = mod;
  1077. list_add(&file_ops->list, &ftrace_module_file_list);
  1078. return file_ops;
  1079. }
  1080. static void trace_module_add_events(struct module *mod)
  1081. {
  1082. struct ftrace_module_file_ops *file_ops = NULL;
  1083. struct ftrace_event_call **call, **start, **end;
  1084. start = mod->trace_events;
  1085. end = mod->trace_events + mod->num_trace_events;
  1086. if (start == end)
  1087. return;
  1088. file_ops = trace_create_file_ops(mod);
  1089. if (!file_ops)
  1090. return;
  1091. for_each_event(call, start, end) {
  1092. __trace_add_event_call(*call, mod,
  1093. &file_ops->id, &file_ops->enable,
  1094. &file_ops->filter, &file_ops->format);
  1095. }
  1096. }
  1097. static void trace_module_remove_events(struct module *mod)
  1098. {
  1099. struct ftrace_module_file_ops *file_ops;
  1100. struct ftrace_event_call *call, *p;
  1101. bool found = false;
  1102. down_write(&trace_event_mutex);
  1103. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  1104. if (call->mod == mod) {
  1105. found = true;
  1106. __trace_remove_event_call(call);
  1107. }
  1108. }
  1109. /* Now free the file_operations */
  1110. list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
  1111. if (file_ops->mod == mod)
  1112. break;
  1113. }
  1114. if (&file_ops->list != &ftrace_module_file_list) {
  1115. list_del(&file_ops->list);
  1116. kfree(file_ops);
  1117. }
  1118. /*
  1119. * It is safest to reset the ring buffer if the module being unloaded
  1120. * registered any events.
  1121. */
  1122. if (found)
  1123. tracing_reset_current_online_cpus();
  1124. up_write(&trace_event_mutex);
  1125. }
  1126. static int trace_module_notify(struct notifier_block *self,
  1127. unsigned long val, void *data)
  1128. {
  1129. struct module *mod = data;
  1130. mutex_lock(&event_mutex);
  1131. switch (val) {
  1132. case MODULE_STATE_COMING:
  1133. trace_module_add_events(mod);
  1134. break;
  1135. case MODULE_STATE_GOING:
  1136. trace_module_remove_events(mod);
  1137. break;
  1138. }
  1139. mutex_unlock(&event_mutex);
  1140. return 0;
  1141. }
  1142. #else
  1143. static int trace_module_notify(struct notifier_block *self,
  1144. unsigned long val, void *data)
  1145. {
  1146. return 0;
  1147. }
  1148. #endif /* CONFIG_MODULES */
  1149. static struct notifier_block trace_module_nb = {
  1150. .notifier_call = trace_module_notify,
  1151. .priority = 0,
  1152. };
  1153. extern struct ftrace_event_call *__start_ftrace_events[];
  1154. extern struct ftrace_event_call *__stop_ftrace_events[];
  1155. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  1156. static __init int setup_trace_event(char *str)
  1157. {
  1158. strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  1159. ring_buffer_expanded = 1;
  1160. tracing_selftest_disabled = 1;
  1161. return 1;
  1162. }
  1163. __setup("trace_event=", setup_trace_event);
  1164. static __init int event_trace_init(void)
  1165. {
  1166. struct ftrace_event_call **call;
  1167. struct dentry *d_tracer;
  1168. struct dentry *entry;
  1169. struct dentry *d_events;
  1170. int ret;
  1171. char *buf = bootup_event_buf;
  1172. char *token;
  1173. d_tracer = tracing_init_dentry();
  1174. if (!d_tracer)
  1175. return 0;
  1176. entry = debugfs_create_file("available_events", 0444, d_tracer,
  1177. (void *)&show_event_seq_ops,
  1178. &ftrace_avail_fops);
  1179. if (!entry)
  1180. pr_warning("Could not create debugfs "
  1181. "'available_events' entry\n");
  1182. entry = debugfs_create_file("set_event", 0644, d_tracer,
  1183. (void *)&show_set_event_seq_ops,
  1184. &ftrace_set_event_fops);
  1185. if (!entry)
  1186. pr_warning("Could not create debugfs "
  1187. "'set_event' entry\n");
  1188. d_events = event_trace_events_dir();
  1189. if (!d_events)
  1190. return 0;
  1191. /* ring buffer internal formats */
  1192. trace_create_file("header_page", 0444, d_events,
  1193. ring_buffer_print_page_header,
  1194. &ftrace_show_header_fops);
  1195. trace_create_file("header_event", 0444, d_events,
  1196. ring_buffer_print_entry_header,
  1197. &ftrace_show_header_fops);
  1198. trace_create_file("enable", 0644, d_events,
  1199. NULL, &ftrace_system_enable_fops);
  1200. if (trace_define_common_fields())
  1201. pr_warning("tracing: Failed to allocate common fields");
  1202. for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
  1203. __trace_add_event_call(*call, NULL, &ftrace_event_id_fops,
  1204. &ftrace_enable_fops,
  1205. &ftrace_event_filter_fops,
  1206. &ftrace_event_format_fops);
  1207. }
  1208. while (true) {
  1209. token = strsep(&buf, ",");
  1210. if (!token)
  1211. break;
  1212. if (!*token)
  1213. continue;
  1214. ret = ftrace_set_clr_event(token, 1);
  1215. if (ret)
  1216. pr_warning("Failed to enable trace event: %s\n", token);
  1217. }
  1218. ret = register_module_notifier(&trace_module_nb);
  1219. if (ret)
  1220. pr_warning("Failed to register trace events module notifier\n");
  1221. return 0;
  1222. }
  1223. fs_initcall(event_trace_init);
  1224. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1225. static DEFINE_SPINLOCK(test_spinlock);
  1226. static DEFINE_SPINLOCK(test_spinlock_irq);
  1227. static DEFINE_MUTEX(test_mutex);
  1228. static __init void test_work(struct work_struct *dummy)
  1229. {
  1230. spin_lock(&test_spinlock);
  1231. spin_lock_irq(&test_spinlock_irq);
  1232. udelay(1);
  1233. spin_unlock_irq(&test_spinlock_irq);
  1234. spin_unlock(&test_spinlock);
  1235. mutex_lock(&test_mutex);
  1236. msleep(1);
  1237. mutex_unlock(&test_mutex);
  1238. }
  1239. static __init int event_test_thread(void *unused)
  1240. {
  1241. void *test_malloc;
  1242. test_malloc = kmalloc(1234, GFP_KERNEL);
  1243. if (!test_malloc)
  1244. pr_info("failed to kmalloc\n");
  1245. schedule_on_each_cpu(test_work);
  1246. kfree(test_malloc);
  1247. set_current_state(TASK_INTERRUPTIBLE);
  1248. while (!kthread_should_stop())
  1249. schedule();
  1250. return 0;
  1251. }
  1252. /*
  1253. * Do various things that may trigger events.
  1254. */
  1255. static __init void event_test_stuff(void)
  1256. {
  1257. struct task_struct *test_thread;
  1258. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  1259. msleep(1);
  1260. kthread_stop(test_thread);
  1261. }
  1262. /*
  1263. * For every trace event defined, we will test each trace point separately,
  1264. * and then by groups, and finally all trace points.
  1265. */
  1266. static __init void event_trace_self_tests(void)
  1267. {
  1268. struct ftrace_event_call *call;
  1269. struct event_subsystem *system;
  1270. int ret;
  1271. pr_info("Running tests on trace events:\n");
  1272. list_for_each_entry(call, &ftrace_events, list) {
  1273. /* Only test those that have a probe */
  1274. if (!call->class || !call->class->probe)
  1275. continue;
  1276. /*
  1277. * Testing syscall events here is pretty useless, but
  1278. * we still do it if configured. But this is time consuming.
  1279. * What we really need is a user thread to perform the
  1280. * syscalls as we test.
  1281. */
  1282. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  1283. if (call->class->system &&
  1284. strcmp(call->class->system, "syscalls") == 0)
  1285. continue;
  1286. #endif
  1287. pr_info("Testing event %s: ", call->name);
  1288. /*
  1289. * If an event is already enabled, someone is using
  1290. * it and the self test should not be on.
  1291. */
  1292. if (call->flags & TRACE_EVENT_FL_ENABLED) {
  1293. pr_warning("Enabled event during self test!\n");
  1294. WARN_ON_ONCE(1);
  1295. continue;
  1296. }
  1297. ftrace_event_enable_disable(call, 1);
  1298. event_test_stuff();
  1299. ftrace_event_enable_disable(call, 0);
  1300. pr_cont("OK\n");
  1301. }
  1302. /* Now test at the sub system level */
  1303. pr_info("Running tests on trace event systems:\n");
  1304. list_for_each_entry(system, &event_subsystems, list) {
  1305. /* the ftrace system is special, skip it */
  1306. if (strcmp(system->name, "ftrace") == 0)
  1307. continue;
  1308. pr_info("Testing event system %s: ", system->name);
  1309. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
  1310. if (WARN_ON_ONCE(ret)) {
  1311. pr_warning("error enabling system %s\n",
  1312. system->name);
  1313. continue;
  1314. }
  1315. event_test_stuff();
  1316. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
  1317. if (WARN_ON_ONCE(ret))
  1318. pr_warning("error disabling system %s\n",
  1319. system->name);
  1320. pr_cont("OK\n");
  1321. }
  1322. /* Test with all events enabled */
  1323. pr_info("Running tests on all trace events:\n");
  1324. pr_info("Testing all events: ");
  1325. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
  1326. if (WARN_ON_ONCE(ret)) {
  1327. pr_warning("error enabling all events\n");
  1328. return;
  1329. }
  1330. event_test_stuff();
  1331. /* reset sysname */
  1332. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
  1333. if (WARN_ON_ONCE(ret)) {
  1334. pr_warning("error disabling all events\n");
  1335. return;
  1336. }
  1337. pr_cont("OK\n");
  1338. }
  1339. #ifdef CONFIG_FUNCTION_TRACER
  1340. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  1341. static void
  1342. function_test_events_call(unsigned long ip, unsigned long parent_ip)
  1343. {
  1344. struct ring_buffer_event *event;
  1345. struct ring_buffer *buffer;
  1346. struct ftrace_entry *entry;
  1347. unsigned long flags;
  1348. long disabled;
  1349. int cpu;
  1350. int pc;
  1351. pc = preempt_count();
  1352. preempt_disable_notrace();
  1353. cpu = raw_smp_processor_id();
  1354. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  1355. if (disabled != 1)
  1356. goto out;
  1357. local_save_flags(flags);
  1358. event = trace_current_buffer_lock_reserve(&buffer,
  1359. TRACE_FN, sizeof(*entry),
  1360. flags, pc);
  1361. if (!event)
  1362. goto out;
  1363. entry = ring_buffer_event_data(event);
  1364. entry->ip = ip;
  1365. entry->parent_ip = parent_ip;
  1366. trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
  1367. out:
  1368. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  1369. preempt_enable_notrace();
  1370. }
  1371. static struct ftrace_ops trace_ops __initdata =
  1372. {
  1373. .func = function_test_events_call,
  1374. };
  1375. static __init void event_trace_self_test_with_function(void)
  1376. {
  1377. int ret;
  1378. ret = register_ftrace_function(&trace_ops);
  1379. if (WARN_ON(ret < 0)) {
  1380. pr_info("Failed to enable function tracer for event tests\n");
  1381. return;
  1382. }
  1383. pr_info("Running tests again, along with the function tracer\n");
  1384. event_trace_self_tests();
  1385. unregister_ftrace_function(&trace_ops);
  1386. }
  1387. #else
  1388. static __init void event_trace_self_test_with_function(void)
  1389. {
  1390. }
  1391. #endif
  1392. static __init int event_trace_self_tests_init(void)
  1393. {
  1394. if (!tracing_selftest_disabled) {
  1395. event_trace_self_tests();
  1396. event_trace_self_test_with_function();
  1397. }
  1398. return 0;
  1399. }
  1400. late_initcall(event_trace_self_tests_init);
  1401. #endif