trace_events.c 30 KB

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