trace_events.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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/delay.h>
  18. #include "trace_output.h"
  19. #define TRACE_SYSTEM "TRACE_SYSTEM"
  20. DEFINE_MUTEX(event_mutex);
  21. LIST_HEAD(ftrace_events);
  22. int trace_define_field(struct ftrace_event_call *call, char *type,
  23. char *name, int offset, int size, int is_signed)
  24. {
  25. struct ftrace_event_field *field;
  26. field = kzalloc(sizeof(*field), GFP_KERNEL);
  27. if (!field)
  28. goto err;
  29. field->name = kstrdup(name, GFP_KERNEL);
  30. if (!field->name)
  31. goto err;
  32. field->type = kstrdup(type, GFP_KERNEL);
  33. if (!field->type)
  34. goto err;
  35. field->offset = offset;
  36. field->size = size;
  37. field->is_signed = is_signed;
  38. list_add(&field->link, &call->fields);
  39. return 0;
  40. err:
  41. if (field) {
  42. kfree(field->name);
  43. kfree(field->type);
  44. }
  45. kfree(field);
  46. return -ENOMEM;
  47. }
  48. EXPORT_SYMBOL_GPL(trace_define_field);
  49. #ifdef CONFIG_MODULES
  50. static void trace_destroy_fields(struct ftrace_event_call *call)
  51. {
  52. struct ftrace_event_field *field, *next;
  53. list_for_each_entry_safe(field, next, &call->fields, link) {
  54. list_del(&field->link);
  55. kfree(field->type);
  56. kfree(field->name);
  57. kfree(field);
  58. }
  59. }
  60. #endif /* CONFIG_MODULES */
  61. static void ftrace_event_enable_disable(struct ftrace_event_call *call,
  62. int enable)
  63. {
  64. switch (enable) {
  65. case 0:
  66. if (call->enabled) {
  67. call->enabled = 0;
  68. tracing_stop_cmdline_record();
  69. call->unregfunc();
  70. }
  71. break;
  72. case 1:
  73. if (!call->enabled) {
  74. call->enabled = 1;
  75. tracing_start_cmdline_record();
  76. call->regfunc();
  77. }
  78. break;
  79. }
  80. }
  81. static void ftrace_clear_events(void)
  82. {
  83. struct ftrace_event_call *call;
  84. mutex_lock(&event_mutex);
  85. list_for_each_entry(call, &ftrace_events, list) {
  86. ftrace_event_enable_disable(call, 0);
  87. }
  88. mutex_unlock(&event_mutex);
  89. }
  90. /*
  91. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  92. */
  93. static int __ftrace_set_clr_event(const char *match, const char *sub,
  94. const char *event, int set)
  95. {
  96. struct ftrace_event_call *call;
  97. int ret = -EINVAL;
  98. mutex_lock(&event_mutex);
  99. list_for_each_entry(call, &ftrace_events, list) {
  100. if (!call->name || !call->regfunc)
  101. continue;
  102. if (match &&
  103. strcmp(match, call->name) != 0 &&
  104. strcmp(match, call->system) != 0)
  105. continue;
  106. if (sub && strcmp(sub, call->system) != 0)
  107. continue;
  108. if (event && strcmp(event, call->name) != 0)
  109. continue;
  110. ftrace_event_enable_disable(call, set);
  111. ret = 0;
  112. }
  113. mutex_unlock(&event_mutex);
  114. return ret;
  115. }
  116. static int ftrace_set_clr_event(char *buf, int set)
  117. {
  118. char *event = NULL, *sub = NULL, *match;
  119. /*
  120. * The buf format can be <subsystem>:<event-name>
  121. * *:<event-name> means any event by that name.
  122. * :<event-name> is the same.
  123. *
  124. * <subsystem>:* means all events in that subsystem
  125. * <subsystem>: means the same.
  126. *
  127. * <name> (no ':') means all events in a subsystem with
  128. * the name <name> or any event that matches <name>
  129. */
  130. match = strsep(&buf, ":");
  131. if (buf) {
  132. sub = match;
  133. event = buf;
  134. match = NULL;
  135. if (!strlen(sub) || strcmp(sub, "*") == 0)
  136. sub = NULL;
  137. if (!strlen(event) || strcmp(event, "*") == 0)
  138. event = NULL;
  139. }
  140. return __ftrace_set_clr_event(match, sub, event, set);
  141. }
  142. /**
  143. * trace_set_clr_event - enable or disable an event
  144. * @system: system name to match (NULL for any system)
  145. * @event: event name to match (NULL for all events, within system)
  146. * @set: 1 to enable, 0 to disable
  147. *
  148. * This is a way for other parts of the kernel to enable or disable
  149. * event recording.
  150. *
  151. * Returns 0 on success, -EINVAL if the parameters do not match any
  152. * registered events.
  153. */
  154. int trace_set_clr_event(const char *system, const char *event, int set)
  155. {
  156. return __ftrace_set_clr_event(NULL, system, event, set);
  157. }
  158. /* 128 should be much more than enough */
  159. #define EVENT_BUF_SIZE 127
  160. static ssize_t
  161. ftrace_event_write(struct file *file, const char __user *ubuf,
  162. size_t cnt, loff_t *ppos)
  163. {
  164. size_t read = 0;
  165. int i, set = 1;
  166. ssize_t ret;
  167. char *buf;
  168. char ch;
  169. if (!cnt || cnt < 0)
  170. return 0;
  171. ret = tracing_update_buffers();
  172. if (ret < 0)
  173. return ret;
  174. ret = get_user(ch, ubuf++);
  175. if (ret)
  176. return ret;
  177. read++;
  178. cnt--;
  179. /* skip white space */
  180. while (cnt && isspace(ch)) {
  181. ret = get_user(ch, ubuf++);
  182. if (ret)
  183. return ret;
  184. read++;
  185. cnt--;
  186. }
  187. /* Only white space found? */
  188. if (isspace(ch)) {
  189. file->f_pos += read;
  190. ret = read;
  191. return ret;
  192. }
  193. buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
  194. if (!buf)
  195. return -ENOMEM;
  196. if (cnt > EVENT_BUF_SIZE)
  197. cnt = EVENT_BUF_SIZE;
  198. i = 0;
  199. while (cnt && !isspace(ch)) {
  200. if (!i && ch == '!')
  201. set = 0;
  202. else
  203. buf[i++] = ch;
  204. ret = get_user(ch, ubuf++);
  205. if (ret)
  206. goto out_free;
  207. read++;
  208. cnt--;
  209. }
  210. buf[i] = 0;
  211. file->f_pos += read;
  212. ret = ftrace_set_clr_event(buf, set);
  213. if (ret)
  214. goto out_free;
  215. ret = read;
  216. out_free:
  217. kfree(buf);
  218. return ret;
  219. }
  220. static void *
  221. t_next(struct seq_file *m, void *v, loff_t *pos)
  222. {
  223. struct list_head *list = m->private;
  224. struct ftrace_event_call *call;
  225. (*pos)++;
  226. for (;;) {
  227. if (list == &ftrace_events)
  228. return NULL;
  229. call = list_entry(list, struct ftrace_event_call, list);
  230. /*
  231. * The ftrace subsystem is for showing formats only.
  232. * They can not be enabled or disabled via the event files.
  233. */
  234. if (call->regfunc)
  235. break;
  236. list = list->next;
  237. }
  238. m->private = list->next;
  239. return call;
  240. }
  241. static void *t_start(struct seq_file *m, loff_t *pos)
  242. {
  243. mutex_lock(&event_mutex);
  244. if (*pos == 0)
  245. m->private = ftrace_events.next;
  246. return t_next(m, NULL, pos);
  247. }
  248. static void *
  249. s_next(struct seq_file *m, void *v, loff_t *pos)
  250. {
  251. struct list_head *list = m->private;
  252. struct ftrace_event_call *call;
  253. (*pos)++;
  254. retry:
  255. if (list == &ftrace_events)
  256. return NULL;
  257. call = list_entry(list, struct ftrace_event_call, list);
  258. if (!call->enabled) {
  259. list = list->next;
  260. goto retry;
  261. }
  262. m->private = list->next;
  263. return call;
  264. }
  265. static void *s_start(struct seq_file *m, loff_t *pos)
  266. {
  267. mutex_lock(&event_mutex);
  268. if (*pos == 0)
  269. m->private = ftrace_events.next;
  270. return s_next(m, NULL, pos);
  271. }
  272. static int t_show(struct seq_file *m, void *v)
  273. {
  274. struct ftrace_event_call *call = v;
  275. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  276. seq_printf(m, "%s:", call->system);
  277. seq_printf(m, "%s\n", call->name);
  278. return 0;
  279. }
  280. static void t_stop(struct seq_file *m, void *p)
  281. {
  282. mutex_unlock(&event_mutex);
  283. }
  284. static int
  285. ftrace_event_seq_open(struct inode *inode, struct file *file)
  286. {
  287. const struct seq_operations *seq_ops;
  288. if ((file->f_mode & FMODE_WRITE) &&
  289. !(file->f_flags & O_APPEND))
  290. ftrace_clear_events();
  291. seq_ops = inode->i_private;
  292. return seq_open(file, seq_ops);
  293. }
  294. static ssize_t
  295. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  296. loff_t *ppos)
  297. {
  298. struct ftrace_event_call *call = filp->private_data;
  299. char *buf;
  300. if (call->enabled)
  301. buf = "1\n";
  302. else
  303. buf = "0\n";
  304. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  305. }
  306. static ssize_t
  307. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  308. loff_t *ppos)
  309. {
  310. struct ftrace_event_call *call = filp->private_data;
  311. char buf[64];
  312. unsigned long val;
  313. int ret;
  314. if (cnt >= sizeof(buf))
  315. return -EINVAL;
  316. if (copy_from_user(&buf, ubuf, cnt))
  317. return -EFAULT;
  318. buf[cnt] = 0;
  319. ret = strict_strtoul(buf, 10, &val);
  320. if (ret < 0)
  321. return ret;
  322. ret = tracing_update_buffers();
  323. if (ret < 0)
  324. return ret;
  325. switch (val) {
  326. case 0:
  327. case 1:
  328. mutex_lock(&event_mutex);
  329. ftrace_event_enable_disable(call, val);
  330. mutex_unlock(&event_mutex);
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. *ppos += cnt;
  336. return cnt;
  337. }
  338. static ssize_t
  339. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  340. loff_t *ppos)
  341. {
  342. const char set_to_char[4] = { '?', '0', '1', 'X' };
  343. const char *system = filp->private_data;
  344. struct ftrace_event_call *call;
  345. char buf[2];
  346. int set = 0;
  347. int ret;
  348. mutex_lock(&event_mutex);
  349. list_for_each_entry(call, &ftrace_events, list) {
  350. if (!call->name || !call->regfunc)
  351. continue;
  352. if (system && strcmp(call->system, system) != 0)
  353. continue;
  354. /*
  355. * We need to find out if all the events are set
  356. * or if all events or cleared, or if we have
  357. * a mixture.
  358. */
  359. set |= (1 << !!call->enabled);
  360. /*
  361. * If we have a mixture, no need to look further.
  362. */
  363. if (set == 3)
  364. break;
  365. }
  366. mutex_unlock(&event_mutex);
  367. buf[0] = set_to_char[set];
  368. buf[1] = '\n';
  369. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  370. return ret;
  371. }
  372. static ssize_t
  373. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  374. loff_t *ppos)
  375. {
  376. const char *system = filp->private_data;
  377. unsigned long val;
  378. char buf[64];
  379. ssize_t ret;
  380. if (cnt >= sizeof(buf))
  381. return -EINVAL;
  382. if (copy_from_user(&buf, ubuf, cnt))
  383. return -EFAULT;
  384. buf[cnt] = 0;
  385. ret = strict_strtoul(buf, 10, &val);
  386. if (ret < 0)
  387. return ret;
  388. ret = tracing_update_buffers();
  389. if (ret < 0)
  390. return ret;
  391. if (val != 0 && val != 1)
  392. return -EINVAL;
  393. ret = __ftrace_set_clr_event(NULL, system, NULL, val);
  394. if (ret)
  395. goto out;
  396. ret = cnt;
  397. out:
  398. *ppos += cnt;
  399. return ret;
  400. }
  401. extern char *__bad_type_size(void);
  402. #undef FIELD
  403. #define FIELD(type, name) \
  404. sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
  405. #type, "common_" #name, offsetof(typeof(field), name), \
  406. sizeof(field.name)
  407. static int trace_write_header(struct trace_seq *s)
  408. {
  409. struct trace_entry field;
  410. /* struct trace_entry */
  411. return trace_seq_printf(s,
  412. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  413. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  414. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  415. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  416. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  417. "\n",
  418. FIELD(unsigned short, type),
  419. FIELD(unsigned char, flags),
  420. FIELD(unsigned char, preempt_count),
  421. FIELD(int, pid),
  422. FIELD(int, tgid));
  423. }
  424. static ssize_t
  425. event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
  426. loff_t *ppos)
  427. {
  428. struct ftrace_event_call *call = filp->private_data;
  429. struct trace_seq *s;
  430. char *buf;
  431. int r;
  432. if (*ppos)
  433. return 0;
  434. s = kmalloc(sizeof(*s), GFP_KERNEL);
  435. if (!s)
  436. return -ENOMEM;
  437. trace_seq_init(s);
  438. /* If any of the first writes fail, so will the show_format. */
  439. trace_seq_printf(s, "name: %s\n", call->name);
  440. trace_seq_printf(s, "ID: %d\n", call->id);
  441. trace_seq_printf(s, "format:\n");
  442. trace_write_header(s);
  443. r = call->show_format(s);
  444. if (!r) {
  445. /*
  446. * ug! The format output is bigger than a PAGE!!
  447. */
  448. buf = "FORMAT TOO BIG\n";
  449. r = simple_read_from_buffer(ubuf, cnt, ppos,
  450. buf, strlen(buf));
  451. goto out;
  452. }
  453. r = simple_read_from_buffer(ubuf, cnt, ppos,
  454. s->buffer, s->len);
  455. out:
  456. kfree(s);
  457. return r;
  458. }
  459. static ssize_t
  460. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  461. {
  462. struct ftrace_event_call *call = filp->private_data;
  463. struct trace_seq *s;
  464. int r;
  465. if (*ppos)
  466. return 0;
  467. s = kmalloc(sizeof(*s), GFP_KERNEL);
  468. if (!s)
  469. return -ENOMEM;
  470. trace_seq_init(s);
  471. trace_seq_printf(s, "%d\n", call->id);
  472. r = simple_read_from_buffer(ubuf, cnt, ppos,
  473. s->buffer, s->len);
  474. kfree(s);
  475. return r;
  476. }
  477. static ssize_t
  478. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  479. loff_t *ppos)
  480. {
  481. struct ftrace_event_call *call = filp->private_data;
  482. struct trace_seq *s;
  483. int r;
  484. if (*ppos)
  485. return 0;
  486. s = kmalloc(sizeof(*s), GFP_KERNEL);
  487. if (!s)
  488. return -ENOMEM;
  489. trace_seq_init(s);
  490. print_event_filter(call, s);
  491. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  492. kfree(s);
  493. return r;
  494. }
  495. static ssize_t
  496. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  497. loff_t *ppos)
  498. {
  499. struct ftrace_event_call *call = filp->private_data;
  500. char *buf;
  501. int err;
  502. if (cnt >= PAGE_SIZE)
  503. return -EINVAL;
  504. buf = (char *)__get_free_page(GFP_TEMPORARY);
  505. if (!buf)
  506. return -ENOMEM;
  507. if (copy_from_user(buf, ubuf, cnt)) {
  508. free_page((unsigned long) buf);
  509. return -EFAULT;
  510. }
  511. buf[cnt] = '\0';
  512. err = apply_event_filter(call, buf);
  513. free_page((unsigned long) buf);
  514. if (err < 0)
  515. return err;
  516. *ppos += cnt;
  517. return cnt;
  518. }
  519. static ssize_t
  520. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  521. loff_t *ppos)
  522. {
  523. struct event_subsystem *system = filp->private_data;
  524. struct trace_seq *s;
  525. int r;
  526. if (*ppos)
  527. return 0;
  528. s = kmalloc(sizeof(*s), GFP_KERNEL);
  529. if (!s)
  530. return -ENOMEM;
  531. trace_seq_init(s);
  532. print_subsystem_event_filter(system, s);
  533. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  534. kfree(s);
  535. return r;
  536. }
  537. static ssize_t
  538. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  539. loff_t *ppos)
  540. {
  541. struct event_subsystem *system = filp->private_data;
  542. char *buf;
  543. int err;
  544. if (cnt >= PAGE_SIZE)
  545. return -EINVAL;
  546. buf = (char *)__get_free_page(GFP_TEMPORARY);
  547. if (!buf)
  548. return -ENOMEM;
  549. if (copy_from_user(buf, ubuf, cnt)) {
  550. free_page((unsigned long) buf);
  551. return -EFAULT;
  552. }
  553. buf[cnt] = '\0';
  554. err = apply_subsystem_event_filter(system, buf);
  555. free_page((unsigned long) buf);
  556. if (err < 0)
  557. return err;
  558. *ppos += cnt;
  559. return cnt;
  560. }
  561. static ssize_t
  562. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  563. {
  564. int (*func)(struct trace_seq *s) = filp->private_data;
  565. struct trace_seq *s;
  566. int r;
  567. if (*ppos)
  568. return 0;
  569. s = kmalloc(sizeof(*s), GFP_KERNEL);
  570. if (!s)
  571. return -ENOMEM;
  572. trace_seq_init(s);
  573. func(s);
  574. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  575. kfree(s);
  576. return r;
  577. }
  578. static const struct seq_operations show_event_seq_ops = {
  579. .start = t_start,
  580. .next = t_next,
  581. .show = t_show,
  582. .stop = t_stop,
  583. };
  584. static const struct seq_operations show_set_event_seq_ops = {
  585. .start = s_start,
  586. .next = s_next,
  587. .show = t_show,
  588. .stop = t_stop,
  589. };
  590. static const struct file_operations ftrace_avail_fops = {
  591. .open = ftrace_event_seq_open,
  592. .read = seq_read,
  593. .llseek = seq_lseek,
  594. .release = seq_release,
  595. };
  596. static const struct file_operations ftrace_set_event_fops = {
  597. .open = ftrace_event_seq_open,
  598. .read = seq_read,
  599. .write = ftrace_event_write,
  600. .llseek = seq_lseek,
  601. .release = seq_release,
  602. };
  603. static const struct file_operations ftrace_enable_fops = {
  604. .open = tracing_open_generic,
  605. .read = event_enable_read,
  606. .write = event_enable_write,
  607. };
  608. static const struct file_operations ftrace_event_format_fops = {
  609. .open = tracing_open_generic,
  610. .read = event_format_read,
  611. };
  612. static const struct file_operations ftrace_event_id_fops = {
  613. .open = tracing_open_generic,
  614. .read = event_id_read,
  615. };
  616. static const struct file_operations ftrace_event_filter_fops = {
  617. .open = tracing_open_generic,
  618. .read = event_filter_read,
  619. .write = event_filter_write,
  620. };
  621. static const struct file_operations ftrace_subsystem_filter_fops = {
  622. .open = tracing_open_generic,
  623. .read = subsystem_filter_read,
  624. .write = subsystem_filter_write,
  625. };
  626. static const struct file_operations ftrace_system_enable_fops = {
  627. .open = tracing_open_generic,
  628. .read = system_enable_read,
  629. .write = system_enable_write,
  630. };
  631. static const struct file_operations ftrace_show_header_fops = {
  632. .open = tracing_open_generic,
  633. .read = show_header,
  634. };
  635. static struct dentry *event_trace_events_dir(void)
  636. {
  637. static struct dentry *d_tracer;
  638. static struct dentry *d_events;
  639. if (d_events)
  640. return d_events;
  641. d_tracer = tracing_init_dentry();
  642. if (!d_tracer)
  643. return NULL;
  644. d_events = debugfs_create_dir("events", d_tracer);
  645. if (!d_events)
  646. pr_warning("Could not create debugfs "
  647. "'events' directory\n");
  648. return d_events;
  649. }
  650. static LIST_HEAD(event_subsystems);
  651. static struct dentry *
  652. event_subsystem_dir(const char *name, struct dentry *d_events)
  653. {
  654. struct event_subsystem *system;
  655. struct dentry *entry;
  656. /* First see if we did not already create this dir */
  657. list_for_each_entry(system, &event_subsystems, list) {
  658. if (strcmp(system->name, name) == 0)
  659. return system->entry;
  660. }
  661. /* need to create new entry */
  662. system = kmalloc(sizeof(*system), GFP_KERNEL);
  663. if (!system) {
  664. pr_warning("No memory to create event subsystem %s\n",
  665. name);
  666. return d_events;
  667. }
  668. system->entry = debugfs_create_dir(name, d_events);
  669. if (!system->entry) {
  670. pr_warning("Could not create event subsystem %s\n",
  671. name);
  672. kfree(system);
  673. return d_events;
  674. }
  675. system->name = kstrdup(name, GFP_KERNEL);
  676. if (!system->name) {
  677. debugfs_remove(system->entry);
  678. kfree(system);
  679. return d_events;
  680. }
  681. list_add(&system->list, &event_subsystems);
  682. system->filter = NULL;
  683. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  684. if (!system->filter) {
  685. pr_warning("Could not allocate filter for subsystem "
  686. "'%s'\n", name);
  687. return system->entry;
  688. }
  689. entry = debugfs_create_file("filter", 0644, system->entry, system,
  690. &ftrace_subsystem_filter_fops);
  691. if (!entry) {
  692. kfree(system->filter);
  693. system->filter = NULL;
  694. pr_warning("Could not create debugfs "
  695. "'%s/filter' entry\n", name);
  696. }
  697. entry = trace_create_file("enable", 0644, system->entry,
  698. (void *)system->name,
  699. &ftrace_system_enable_fops);
  700. return system->entry;
  701. }
  702. static int
  703. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
  704. const struct file_operations *id,
  705. const struct file_operations *enable,
  706. const struct file_operations *filter,
  707. const struct file_operations *format)
  708. {
  709. struct dentry *entry;
  710. int ret;
  711. /*
  712. * If the trace point header did not define TRACE_SYSTEM
  713. * then the system would be called "TRACE_SYSTEM".
  714. */
  715. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  716. d_events = event_subsystem_dir(call->system, d_events);
  717. if (call->raw_init) {
  718. ret = call->raw_init();
  719. if (ret < 0) {
  720. pr_warning("Could not initialize trace point"
  721. " events/%s\n", call->name);
  722. return ret;
  723. }
  724. }
  725. call->dir = debugfs_create_dir(call->name, d_events);
  726. if (!call->dir) {
  727. pr_warning("Could not create debugfs "
  728. "'%s' directory\n", call->name);
  729. return -1;
  730. }
  731. if (call->regfunc)
  732. entry = trace_create_file("enable", 0644, call->dir, call,
  733. enable);
  734. if (call->id)
  735. entry = trace_create_file("id", 0444, call->dir, call,
  736. id);
  737. if (call->define_fields) {
  738. ret = call->define_fields();
  739. if (ret < 0) {
  740. pr_warning("Could not initialize trace point"
  741. " events/%s\n", call->name);
  742. return ret;
  743. }
  744. entry = trace_create_file("filter", 0644, call->dir, call,
  745. filter);
  746. }
  747. /* A trace may not want to export its format */
  748. if (!call->show_format)
  749. return 0;
  750. entry = trace_create_file("format", 0444, call->dir, call,
  751. format);
  752. return 0;
  753. }
  754. #define for_each_event(event, start, end) \
  755. for (event = start; \
  756. (unsigned long)event < (unsigned long)end; \
  757. event++)
  758. #ifdef CONFIG_MODULES
  759. static LIST_HEAD(ftrace_module_file_list);
  760. /*
  761. * Modules must own their file_operations to keep up with
  762. * reference counting.
  763. */
  764. struct ftrace_module_file_ops {
  765. struct list_head list;
  766. struct module *mod;
  767. struct file_operations id;
  768. struct file_operations enable;
  769. struct file_operations format;
  770. struct file_operations filter;
  771. };
  772. static struct ftrace_module_file_ops *
  773. trace_create_file_ops(struct module *mod)
  774. {
  775. struct ftrace_module_file_ops *file_ops;
  776. /*
  777. * This is a bit of a PITA. To allow for correct reference
  778. * counting, modules must "own" their file_operations.
  779. * To do this, we allocate the file operations that will be
  780. * used in the event directory.
  781. */
  782. file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
  783. if (!file_ops)
  784. return NULL;
  785. file_ops->mod = mod;
  786. file_ops->id = ftrace_event_id_fops;
  787. file_ops->id.owner = mod;
  788. file_ops->enable = ftrace_enable_fops;
  789. file_ops->enable.owner = mod;
  790. file_ops->filter = ftrace_event_filter_fops;
  791. file_ops->filter.owner = mod;
  792. file_ops->format = ftrace_event_format_fops;
  793. file_ops->format.owner = mod;
  794. list_add(&file_ops->list, &ftrace_module_file_list);
  795. return file_ops;
  796. }
  797. static void trace_module_add_events(struct module *mod)
  798. {
  799. struct ftrace_module_file_ops *file_ops = NULL;
  800. struct ftrace_event_call *call, *start, *end;
  801. struct dentry *d_events;
  802. start = mod->trace_events;
  803. end = mod->trace_events + mod->num_trace_events;
  804. if (start == end)
  805. return;
  806. d_events = event_trace_events_dir();
  807. if (!d_events)
  808. return;
  809. for_each_event(call, start, end) {
  810. /* The linker may leave blanks */
  811. if (!call->name)
  812. continue;
  813. /*
  814. * This module has events, create file ops for this module
  815. * if not already done.
  816. */
  817. if (!file_ops) {
  818. file_ops = trace_create_file_ops(mod);
  819. if (!file_ops)
  820. return;
  821. }
  822. call->mod = mod;
  823. list_add(&call->list, &ftrace_events);
  824. event_create_dir(call, d_events,
  825. &file_ops->id, &file_ops->enable,
  826. &file_ops->filter, &file_ops->format);
  827. }
  828. }
  829. static void trace_module_remove_events(struct module *mod)
  830. {
  831. struct ftrace_module_file_ops *file_ops;
  832. struct ftrace_event_call *call, *p;
  833. bool found = false;
  834. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  835. if (call->mod == mod) {
  836. found = true;
  837. ftrace_event_enable_disable(call, 0);
  838. if (call->event)
  839. unregister_ftrace_event(call->event);
  840. debugfs_remove_recursive(call->dir);
  841. list_del(&call->list);
  842. trace_destroy_fields(call);
  843. destroy_preds(call);
  844. }
  845. }
  846. /* Now free the file_operations */
  847. list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
  848. if (file_ops->mod == mod)
  849. break;
  850. }
  851. if (&file_ops->list != &ftrace_module_file_list) {
  852. list_del(&file_ops->list);
  853. kfree(file_ops);
  854. }
  855. /*
  856. * It is safest to reset the ring buffer if the module being unloaded
  857. * registered any events.
  858. */
  859. if (found)
  860. tracing_reset_current_online_cpus();
  861. }
  862. static int trace_module_notify(struct notifier_block *self,
  863. unsigned long val, void *data)
  864. {
  865. struct module *mod = data;
  866. mutex_lock(&event_mutex);
  867. switch (val) {
  868. case MODULE_STATE_COMING:
  869. trace_module_add_events(mod);
  870. break;
  871. case MODULE_STATE_GOING:
  872. trace_module_remove_events(mod);
  873. break;
  874. }
  875. mutex_unlock(&event_mutex);
  876. return 0;
  877. }
  878. #else
  879. static int trace_module_notify(struct notifier_block *self,
  880. unsigned long val, void *data)
  881. {
  882. return 0;
  883. }
  884. #endif /* CONFIG_MODULES */
  885. struct notifier_block trace_module_nb = {
  886. .notifier_call = trace_module_notify,
  887. .priority = 0,
  888. };
  889. extern struct ftrace_event_call __start_ftrace_events[];
  890. extern struct ftrace_event_call __stop_ftrace_events[];
  891. static __init int event_trace_init(void)
  892. {
  893. struct ftrace_event_call *call;
  894. struct dentry *d_tracer;
  895. struct dentry *entry;
  896. struct dentry *d_events;
  897. int ret;
  898. d_tracer = tracing_init_dentry();
  899. if (!d_tracer)
  900. return 0;
  901. entry = debugfs_create_file("available_events", 0444, d_tracer,
  902. (void *)&show_event_seq_ops,
  903. &ftrace_avail_fops);
  904. if (!entry)
  905. pr_warning("Could not create debugfs "
  906. "'available_events' entry\n");
  907. entry = debugfs_create_file("set_event", 0644, d_tracer,
  908. (void *)&show_set_event_seq_ops,
  909. &ftrace_set_event_fops);
  910. if (!entry)
  911. pr_warning("Could not create debugfs "
  912. "'set_event' entry\n");
  913. d_events = event_trace_events_dir();
  914. if (!d_events)
  915. return 0;
  916. /* ring buffer internal formats */
  917. trace_create_file("header_page", 0444, d_events,
  918. ring_buffer_print_page_header,
  919. &ftrace_show_header_fops);
  920. trace_create_file("header_event", 0444, d_events,
  921. ring_buffer_print_entry_header,
  922. &ftrace_show_header_fops);
  923. trace_create_file("enable", 0644, d_events,
  924. NULL, &ftrace_system_enable_fops);
  925. for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
  926. /* The linker may leave blanks */
  927. if (!call->name)
  928. continue;
  929. list_add(&call->list, &ftrace_events);
  930. event_create_dir(call, d_events, &ftrace_event_id_fops,
  931. &ftrace_enable_fops, &ftrace_event_filter_fops,
  932. &ftrace_event_format_fops);
  933. }
  934. ret = register_module_notifier(&trace_module_nb);
  935. if (ret)
  936. pr_warning("Failed to register trace events module notifier\n");
  937. return 0;
  938. }
  939. fs_initcall(event_trace_init);
  940. #ifdef CONFIG_FTRACE_STARTUP_TEST
  941. static DEFINE_SPINLOCK(test_spinlock);
  942. static DEFINE_SPINLOCK(test_spinlock_irq);
  943. static DEFINE_MUTEX(test_mutex);
  944. static __init void test_work(struct work_struct *dummy)
  945. {
  946. spin_lock(&test_spinlock);
  947. spin_lock_irq(&test_spinlock_irq);
  948. udelay(1);
  949. spin_unlock_irq(&test_spinlock_irq);
  950. spin_unlock(&test_spinlock);
  951. mutex_lock(&test_mutex);
  952. msleep(1);
  953. mutex_unlock(&test_mutex);
  954. }
  955. static __init int event_test_thread(void *unused)
  956. {
  957. void *test_malloc;
  958. test_malloc = kmalloc(1234, GFP_KERNEL);
  959. if (!test_malloc)
  960. pr_info("failed to kmalloc\n");
  961. schedule_on_each_cpu(test_work);
  962. kfree(test_malloc);
  963. set_current_state(TASK_INTERRUPTIBLE);
  964. while (!kthread_should_stop())
  965. schedule();
  966. return 0;
  967. }
  968. /*
  969. * Do various things that may trigger events.
  970. */
  971. static __init void event_test_stuff(void)
  972. {
  973. struct task_struct *test_thread;
  974. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  975. msleep(1);
  976. kthread_stop(test_thread);
  977. }
  978. /*
  979. * For every trace event defined, we will test each trace point separately,
  980. * and then by groups, and finally all trace points.
  981. */
  982. static __init void event_trace_self_tests(void)
  983. {
  984. struct ftrace_event_call *call;
  985. struct event_subsystem *system;
  986. int ret;
  987. pr_info("Running tests on trace events:\n");
  988. list_for_each_entry(call, &ftrace_events, list) {
  989. /* Only test those that have a regfunc */
  990. if (!call->regfunc)
  991. continue;
  992. pr_info("Testing event %s: ", call->name);
  993. /*
  994. * If an event is already enabled, someone is using
  995. * it and the self test should not be on.
  996. */
  997. if (call->enabled) {
  998. pr_warning("Enabled event during self test!\n");
  999. WARN_ON_ONCE(1);
  1000. continue;
  1001. }
  1002. ftrace_event_enable_disable(call, 1);
  1003. event_test_stuff();
  1004. ftrace_event_enable_disable(call, 0);
  1005. pr_cont("OK\n");
  1006. }
  1007. /* Now test at the sub system level */
  1008. pr_info("Running tests on trace event systems:\n");
  1009. list_for_each_entry(system, &event_subsystems, list) {
  1010. /* the ftrace system is special, skip it */
  1011. if (strcmp(system->name, "ftrace") == 0)
  1012. continue;
  1013. pr_info("Testing event system %s: ", system->name);
  1014. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
  1015. if (WARN_ON_ONCE(ret)) {
  1016. pr_warning("error enabling system %s\n",
  1017. system->name);
  1018. continue;
  1019. }
  1020. event_test_stuff();
  1021. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
  1022. if (WARN_ON_ONCE(ret))
  1023. pr_warning("error disabling system %s\n",
  1024. system->name);
  1025. pr_cont("OK\n");
  1026. }
  1027. /* Test with all events enabled */
  1028. pr_info("Running tests on all trace events:\n");
  1029. pr_info("Testing all events: ");
  1030. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
  1031. if (WARN_ON_ONCE(ret)) {
  1032. pr_warning("error enabling all events\n");
  1033. return;
  1034. }
  1035. event_test_stuff();
  1036. /* reset sysname */
  1037. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
  1038. if (WARN_ON_ONCE(ret)) {
  1039. pr_warning("error disabling all events\n");
  1040. return;
  1041. }
  1042. pr_cont("OK\n");
  1043. }
  1044. #ifdef CONFIG_FUNCTION_TRACER
  1045. static DEFINE_PER_CPU(atomic_t, test_event_disable);
  1046. static void
  1047. function_test_events_call(unsigned long ip, unsigned long parent_ip)
  1048. {
  1049. struct ring_buffer_event *event;
  1050. struct ftrace_entry *entry;
  1051. unsigned long flags;
  1052. long disabled;
  1053. int resched;
  1054. int cpu;
  1055. int pc;
  1056. pc = preempt_count();
  1057. resched = ftrace_preempt_disable();
  1058. cpu = raw_smp_processor_id();
  1059. disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu));
  1060. if (disabled != 1)
  1061. goto out;
  1062. local_save_flags(flags);
  1063. event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry),
  1064. flags, pc);
  1065. if (!event)
  1066. goto out;
  1067. entry = ring_buffer_event_data(event);
  1068. entry->ip = ip;
  1069. entry->parent_ip = parent_ip;
  1070. trace_nowake_buffer_unlock_commit(event, flags, pc);
  1071. out:
  1072. atomic_dec(&per_cpu(test_event_disable, cpu));
  1073. ftrace_preempt_enable(resched);
  1074. }
  1075. static struct ftrace_ops trace_ops __initdata =
  1076. {
  1077. .func = function_test_events_call,
  1078. };
  1079. static __init void event_trace_self_test_with_function(void)
  1080. {
  1081. register_ftrace_function(&trace_ops);
  1082. pr_info("Running tests again, along with the function tracer\n");
  1083. event_trace_self_tests();
  1084. unregister_ftrace_function(&trace_ops);
  1085. }
  1086. #else
  1087. static __init void event_trace_self_test_with_function(void)
  1088. {
  1089. }
  1090. #endif
  1091. static __init int event_trace_self_tests_init(void)
  1092. {
  1093. event_trace_self_tests();
  1094. event_trace_self_test_with_function();
  1095. return 0;
  1096. }
  1097. late_initcall(event_trace_self_tests_init);
  1098. #endif