trace_events.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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. #undef TRACE_SYSTEM
  21. #define TRACE_SYSTEM "TRACE_SYSTEM"
  22. DEFINE_MUTEX(event_mutex);
  23. LIST_HEAD(ftrace_events);
  24. int trace_define_field(struct ftrace_event_call *call, const char *type,
  25. const char *name, int offset, int size, int is_signed,
  26. int filter_type)
  27. {
  28. struct ftrace_event_field *field;
  29. field = kzalloc(sizeof(*field), GFP_KERNEL);
  30. if (!field)
  31. goto err;
  32. field->name = kstrdup(name, GFP_KERNEL);
  33. if (!field->name)
  34. goto err;
  35. field->type = kstrdup(type, GFP_KERNEL);
  36. if (!field->type)
  37. goto err;
  38. if (filter_type == FILTER_OTHER)
  39. field->filter_type = filter_assign_type(type);
  40. else
  41. field->filter_type = filter_type;
  42. field->offset = offset;
  43. field->size = size;
  44. field->is_signed = is_signed;
  45. list_add(&field->link, &call->fields);
  46. return 0;
  47. err:
  48. if (field) {
  49. kfree(field->name);
  50. kfree(field->type);
  51. }
  52. kfree(field);
  53. return -ENOMEM;
  54. }
  55. EXPORT_SYMBOL_GPL(trace_define_field);
  56. #define __common_field(type, item) \
  57. ret = trace_define_field(call, #type, "common_" #item, \
  58. offsetof(typeof(ent), item), \
  59. sizeof(ent.item), \
  60. is_signed_type(type), FILTER_OTHER); \
  61. if (ret) \
  62. return ret;
  63. int trace_define_common_fields(struct ftrace_event_call *call)
  64. {
  65. int ret;
  66. struct trace_entry ent;
  67. __common_field(unsigned short, type);
  68. __common_field(unsigned char, flags);
  69. __common_field(unsigned char, preempt_count);
  70. __common_field(int, pid);
  71. __common_field(int, lock_depth);
  72. return ret;
  73. }
  74. EXPORT_SYMBOL_GPL(trace_define_common_fields);
  75. #ifdef CONFIG_MODULES
  76. static void trace_destroy_fields(struct ftrace_event_call *call)
  77. {
  78. struct ftrace_event_field *field, *next;
  79. list_for_each_entry_safe(field, next, &call->fields, link) {
  80. list_del(&field->link);
  81. kfree(field->type);
  82. kfree(field->name);
  83. kfree(field);
  84. }
  85. }
  86. #endif /* CONFIG_MODULES */
  87. static void ftrace_event_enable_disable(struct ftrace_event_call *call,
  88. int enable)
  89. {
  90. switch (enable) {
  91. case 0:
  92. if (call->enabled) {
  93. call->enabled = 0;
  94. tracing_stop_cmdline_record();
  95. call->unregfunc(call->data);
  96. }
  97. break;
  98. case 1:
  99. if (!call->enabled) {
  100. call->enabled = 1;
  101. tracing_start_cmdline_record();
  102. call->regfunc(call->data);
  103. }
  104. break;
  105. }
  106. }
  107. static void ftrace_clear_events(void)
  108. {
  109. struct ftrace_event_call *call;
  110. mutex_lock(&event_mutex);
  111. list_for_each_entry(call, &ftrace_events, list) {
  112. ftrace_event_enable_disable(call, 0);
  113. }
  114. mutex_unlock(&event_mutex);
  115. }
  116. /*
  117. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  118. */
  119. static int __ftrace_set_clr_event(const char *match, const char *sub,
  120. const char *event, int set)
  121. {
  122. struct ftrace_event_call *call;
  123. int ret = -EINVAL;
  124. mutex_lock(&event_mutex);
  125. list_for_each_entry(call, &ftrace_events, list) {
  126. if (!call->name || !call->regfunc)
  127. continue;
  128. if (match &&
  129. strcmp(match, call->name) != 0 &&
  130. strcmp(match, call->system) != 0)
  131. continue;
  132. if (sub && strcmp(sub, call->system) != 0)
  133. continue;
  134. if (event && strcmp(event, call->name) != 0)
  135. continue;
  136. ftrace_event_enable_disable(call, set);
  137. ret = 0;
  138. }
  139. mutex_unlock(&event_mutex);
  140. return ret;
  141. }
  142. static int ftrace_set_clr_event(char *buf, int set)
  143. {
  144. char *event = NULL, *sub = NULL, *match;
  145. /*
  146. * The buf format can be <subsystem>:<event-name>
  147. * *:<event-name> means any event by that name.
  148. * :<event-name> is the same.
  149. *
  150. * <subsystem>:* means all events in that subsystem
  151. * <subsystem>: means the same.
  152. *
  153. * <name> (no ':') means all events in a subsystem with
  154. * the name <name> or any event that matches <name>
  155. */
  156. match = strsep(&buf, ":");
  157. if (buf) {
  158. sub = match;
  159. event = buf;
  160. match = NULL;
  161. if (!strlen(sub) || strcmp(sub, "*") == 0)
  162. sub = NULL;
  163. if (!strlen(event) || strcmp(event, "*") == 0)
  164. event = NULL;
  165. }
  166. return __ftrace_set_clr_event(match, sub, event, set);
  167. }
  168. /**
  169. * trace_set_clr_event - enable or disable an event
  170. * @system: system name to match (NULL for any system)
  171. * @event: event name to match (NULL for all events, within system)
  172. * @set: 1 to enable, 0 to disable
  173. *
  174. * This is a way for other parts of the kernel to enable or disable
  175. * event recording.
  176. *
  177. * Returns 0 on success, -EINVAL if the parameters do not match any
  178. * registered events.
  179. */
  180. int trace_set_clr_event(const char *system, const char *event, int set)
  181. {
  182. return __ftrace_set_clr_event(NULL, system, event, set);
  183. }
  184. /* 128 should be much more than enough */
  185. #define EVENT_BUF_SIZE 127
  186. static ssize_t
  187. ftrace_event_write(struct file *file, const char __user *ubuf,
  188. size_t cnt, loff_t *ppos)
  189. {
  190. struct trace_parser parser;
  191. size_t read = 0;
  192. ssize_t ret;
  193. if (!cnt || cnt < 0)
  194. return 0;
  195. ret = tracing_update_buffers();
  196. if (ret < 0)
  197. return ret;
  198. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  199. return -ENOMEM;
  200. read = trace_get_user(&parser, ubuf, cnt, ppos);
  201. if (trace_parser_loaded((&parser))) {
  202. int set = 1;
  203. if (*parser.buffer == '!')
  204. set = 0;
  205. parser.buffer[parser.idx] = 0;
  206. ret = ftrace_set_clr_event(parser.buffer + !set, set);
  207. if (ret)
  208. goto out_put;
  209. }
  210. ret = read;
  211. out_put:
  212. trace_parser_put(&parser);
  213. return ret;
  214. }
  215. static void *
  216. t_next(struct seq_file *m, void *v, loff_t *pos)
  217. {
  218. struct list_head *list = m->private;
  219. struct ftrace_event_call *call;
  220. (*pos)++;
  221. for (;;) {
  222. if (list == &ftrace_events)
  223. return NULL;
  224. call = list_entry(list, struct ftrace_event_call, list);
  225. /*
  226. * The ftrace subsystem is for showing formats only.
  227. * They can not be enabled or disabled via the event files.
  228. */
  229. if (call->regfunc)
  230. break;
  231. list = list->next;
  232. }
  233. m->private = list->next;
  234. return call;
  235. }
  236. static void *t_start(struct seq_file *m, loff_t *pos)
  237. {
  238. struct ftrace_event_call *call = NULL;
  239. loff_t l;
  240. mutex_lock(&event_mutex);
  241. m->private = ftrace_events.next;
  242. for (l = 0; l <= *pos; ) {
  243. call = t_next(m, NULL, &l);
  244. if (!call)
  245. break;
  246. }
  247. return call;
  248. }
  249. static void *
  250. s_next(struct seq_file *m, void *v, loff_t *pos)
  251. {
  252. struct list_head *list = m->private;
  253. struct ftrace_event_call *call;
  254. (*pos)++;
  255. retry:
  256. if (list == &ftrace_events)
  257. return NULL;
  258. call = list_entry(list, struct ftrace_event_call, list);
  259. if (!call->enabled) {
  260. list = list->next;
  261. goto retry;
  262. }
  263. m->private = list->next;
  264. return call;
  265. }
  266. static void *s_start(struct seq_file *m, loff_t *pos)
  267. {
  268. struct ftrace_event_call *call = NULL;
  269. loff_t l;
  270. mutex_lock(&event_mutex);
  271. m->private = ftrace_events.next;
  272. for (l = 0; l <= *pos; ) {
  273. call = s_next(m, NULL, &l);
  274. if (!call)
  275. break;
  276. }
  277. return call;
  278. }
  279. static int t_show(struct seq_file *m, void *v)
  280. {
  281. struct ftrace_event_call *call = v;
  282. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  283. seq_printf(m, "%s:", call->system);
  284. seq_printf(m, "%s\n", call->name);
  285. return 0;
  286. }
  287. static void t_stop(struct seq_file *m, void *p)
  288. {
  289. mutex_unlock(&event_mutex);
  290. }
  291. static int
  292. ftrace_event_seq_open(struct inode *inode, struct file *file)
  293. {
  294. const struct seq_operations *seq_ops;
  295. if ((file->f_mode & FMODE_WRITE) &&
  296. (file->f_flags & O_TRUNC))
  297. ftrace_clear_events();
  298. seq_ops = inode->i_private;
  299. return seq_open(file, seq_ops);
  300. }
  301. static ssize_t
  302. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  303. loff_t *ppos)
  304. {
  305. struct ftrace_event_call *call = filp->private_data;
  306. char *buf;
  307. if (call->enabled)
  308. buf = "1\n";
  309. else
  310. buf = "0\n";
  311. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  312. }
  313. static ssize_t
  314. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  315. loff_t *ppos)
  316. {
  317. struct ftrace_event_call *call = filp->private_data;
  318. char buf[64];
  319. unsigned long val;
  320. int ret;
  321. if (cnt >= sizeof(buf))
  322. return -EINVAL;
  323. if (copy_from_user(&buf, ubuf, cnt))
  324. return -EFAULT;
  325. buf[cnt] = 0;
  326. ret = strict_strtoul(buf, 10, &val);
  327. if (ret < 0)
  328. return ret;
  329. ret = tracing_update_buffers();
  330. if (ret < 0)
  331. return ret;
  332. switch (val) {
  333. case 0:
  334. case 1:
  335. mutex_lock(&event_mutex);
  336. ftrace_event_enable_disable(call, val);
  337. mutex_unlock(&event_mutex);
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. *ppos += cnt;
  343. return cnt;
  344. }
  345. static ssize_t
  346. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  347. loff_t *ppos)
  348. {
  349. const char set_to_char[4] = { '?', '0', '1', 'X' };
  350. const char *system = filp->private_data;
  351. struct ftrace_event_call *call;
  352. char buf[2];
  353. int set = 0;
  354. int ret;
  355. mutex_lock(&event_mutex);
  356. list_for_each_entry(call, &ftrace_events, list) {
  357. if (!call->name || !call->regfunc)
  358. continue;
  359. if (system && strcmp(call->system, system) != 0)
  360. continue;
  361. /*
  362. * We need to find out if all the events are set
  363. * or if all events or cleared, or if we have
  364. * a mixture.
  365. */
  366. set |= (1 << !!call->enabled);
  367. /*
  368. * If we have a mixture, no need to look further.
  369. */
  370. if (set == 3)
  371. break;
  372. }
  373. mutex_unlock(&event_mutex);
  374. buf[0] = set_to_char[set];
  375. buf[1] = '\n';
  376. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  377. return ret;
  378. }
  379. static ssize_t
  380. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  381. loff_t *ppos)
  382. {
  383. const char *system = filp->private_data;
  384. unsigned long val;
  385. char buf[64];
  386. ssize_t ret;
  387. if (cnt >= sizeof(buf))
  388. return -EINVAL;
  389. if (copy_from_user(&buf, ubuf, cnt))
  390. return -EFAULT;
  391. buf[cnt] = 0;
  392. ret = strict_strtoul(buf, 10, &val);
  393. if (ret < 0)
  394. return ret;
  395. ret = tracing_update_buffers();
  396. if (ret < 0)
  397. return ret;
  398. if (val != 0 && val != 1)
  399. return -EINVAL;
  400. ret = __ftrace_set_clr_event(NULL, system, NULL, val);
  401. if (ret)
  402. goto out;
  403. ret = cnt;
  404. out:
  405. *ppos += cnt;
  406. return ret;
  407. }
  408. extern char *__bad_type_size(void);
  409. #undef FIELD
  410. #define FIELD(type, name) \
  411. sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
  412. #type, "common_" #name, offsetof(typeof(field), name), \
  413. sizeof(field.name)
  414. static int trace_write_header(struct trace_seq *s)
  415. {
  416. struct trace_entry field;
  417. /* struct trace_entry */
  418. return trace_seq_printf(s,
  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. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  424. "\n",
  425. FIELD(unsigned short, type),
  426. FIELD(unsigned char, flags),
  427. FIELD(unsigned char, preempt_count),
  428. FIELD(int, pid),
  429. FIELD(int, lock_depth));
  430. }
  431. static ssize_t
  432. event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
  433. loff_t *ppos)
  434. {
  435. struct ftrace_event_call *call = filp->private_data;
  436. struct trace_seq *s;
  437. char *buf;
  438. int r;
  439. if (*ppos)
  440. return 0;
  441. s = kmalloc(sizeof(*s), GFP_KERNEL);
  442. if (!s)
  443. return -ENOMEM;
  444. trace_seq_init(s);
  445. /* If any of the first writes fail, so will the show_format. */
  446. trace_seq_printf(s, "name: %s\n", call->name);
  447. trace_seq_printf(s, "ID: %d\n", call->id);
  448. trace_seq_printf(s, "format:\n");
  449. trace_write_header(s);
  450. r = call->show_format(call, s);
  451. if (!r) {
  452. /*
  453. * ug! The format output is bigger than a PAGE!!
  454. */
  455. buf = "FORMAT TOO BIG\n";
  456. r = simple_read_from_buffer(ubuf, cnt, ppos,
  457. buf, strlen(buf));
  458. goto out;
  459. }
  460. r = simple_read_from_buffer(ubuf, cnt, ppos,
  461. s->buffer, s->len);
  462. out:
  463. kfree(s);
  464. return r;
  465. }
  466. static ssize_t
  467. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  468. {
  469. struct ftrace_event_call *call = filp->private_data;
  470. struct trace_seq *s;
  471. int r;
  472. if (*ppos)
  473. return 0;
  474. s = kmalloc(sizeof(*s), GFP_KERNEL);
  475. if (!s)
  476. return -ENOMEM;
  477. trace_seq_init(s);
  478. trace_seq_printf(s, "%d\n", call->id);
  479. r = simple_read_from_buffer(ubuf, cnt, ppos,
  480. s->buffer, s->len);
  481. kfree(s);
  482. return r;
  483. }
  484. static ssize_t
  485. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  486. loff_t *ppos)
  487. {
  488. struct ftrace_event_call *call = filp->private_data;
  489. struct trace_seq *s;
  490. int r;
  491. if (*ppos)
  492. return 0;
  493. s = kmalloc(sizeof(*s), GFP_KERNEL);
  494. if (!s)
  495. return -ENOMEM;
  496. trace_seq_init(s);
  497. print_event_filter(call, s);
  498. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  499. kfree(s);
  500. return r;
  501. }
  502. static ssize_t
  503. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  504. loff_t *ppos)
  505. {
  506. struct ftrace_event_call *call = filp->private_data;
  507. char *buf;
  508. int err;
  509. if (cnt >= PAGE_SIZE)
  510. return -EINVAL;
  511. buf = (char *)__get_free_page(GFP_TEMPORARY);
  512. if (!buf)
  513. return -ENOMEM;
  514. if (copy_from_user(buf, ubuf, cnt)) {
  515. free_page((unsigned long) buf);
  516. return -EFAULT;
  517. }
  518. buf[cnt] = '\0';
  519. err = apply_event_filter(call, buf);
  520. free_page((unsigned long) buf);
  521. if (err < 0)
  522. return err;
  523. *ppos += cnt;
  524. return cnt;
  525. }
  526. static ssize_t
  527. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  528. loff_t *ppos)
  529. {
  530. struct event_subsystem *system = filp->private_data;
  531. struct trace_seq *s;
  532. int r;
  533. if (*ppos)
  534. return 0;
  535. s = kmalloc(sizeof(*s), GFP_KERNEL);
  536. if (!s)
  537. return -ENOMEM;
  538. trace_seq_init(s);
  539. print_subsystem_event_filter(system, s);
  540. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  541. kfree(s);
  542. return r;
  543. }
  544. static ssize_t
  545. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  546. loff_t *ppos)
  547. {
  548. struct event_subsystem *system = filp->private_data;
  549. char *buf;
  550. int err;
  551. if (cnt >= PAGE_SIZE)
  552. return -EINVAL;
  553. buf = (char *)__get_free_page(GFP_TEMPORARY);
  554. if (!buf)
  555. return -ENOMEM;
  556. if (copy_from_user(buf, ubuf, cnt)) {
  557. free_page((unsigned long) buf);
  558. return -EFAULT;
  559. }
  560. buf[cnt] = '\0';
  561. err = apply_subsystem_event_filter(system, buf);
  562. free_page((unsigned long) buf);
  563. if (err < 0)
  564. return err;
  565. *ppos += cnt;
  566. return cnt;
  567. }
  568. static ssize_t
  569. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  570. {
  571. int (*func)(struct trace_seq *s) = filp->private_data;
  572. struct trace_seq *s;
  573. int r;
  574. if (*ppos)
  575. return 0;
  576. s = kmalloc(sizeof(*s), GFP_KERNEL);
  577. if (!s)
  578. return -ENOMEM;
  579. trace_seq_init(s);
  580. func(s);
  581. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  582. kfree(s);
  583. return r;
  584. }
  585. static const struct seq_operations show_event_seq_ops = {
  586. .start = t_start,
  587. .next = t_next,
  588. .show = t_show,
  589. .stop = t_stop,
  590. };
  591. static const struct seq_operations show_set_event_seq_ops = {
  592. .start = s_start,
  593. .next = s_next,
  594. .show = t_show,
  595. .stop = t_stop,
  596. };
  597. static const struct file_operations ftrace_avail_fops = {
  598. .open = ftrace_event_seq_open,
  599. .read = seq_read,
  600. .llseek = seq_lseek,
  601. .release = seq_release,
  602. };
  603. static const struct file_operations ftrace_set_event_fops = {
  604. .open = ftrace_event_seq_open,
  605. .read = seq_read,
  606. .write = ftrace_event_write,
  607. .llseek = seq_lseek,
  608. .release = seq_release,
  609. };
  610. static const struct file_operations ftrace_enable_fops = {
  611. .open = tracing_open_generic,
  612. .read = event_enable_read,
  613. .write = event_enable_write,
  614. };
  615. static const struct file_operations ftrace_event_format_fops = {
  616. .open = tracing_open_generic,
  617. .read = event_format_read,
  618. };
  619. static const struct file_operations ftrace_event_id_fops = {
  620. .open = tracing_open_generic,
  621. .read = event_id_read,
  622. };
  623. static const struct file_operations ftrace_event_filter_fops = {
  624. .open = tracing_open_generic,
  625. .read = event_filter_read,
  626. .write = event_filter_write,
  627. };
  628. static const struct file_operations ftrace_subsystem_filter_fops = {
  629. .open = tracing_open_generic,
  630. .read = subsystem_filter_read,
  631. .write = subsystem_filter_write,
  632. };
  633. static const struct file_operations ftrace_system_enable_fops = {
  634. .open = tracing_open_generic,
  635. .read = system_enable_read,
  636. .write = system_enable_write,
  637. };
  638. static const struct file_operations ftrace_show_header_fops = {
  639. .open = tracing_open_generic,
  640. .read = show_header,
  641. };
  642. static struct dentry *event_trace_events_dir(void)
  643. {
  644. static struct dentry *d_tracer;
  645. static struct dentry *d_events;
  646. if (d_events)
  647. return d_events;
  648. d_tracer = tracing_init_dentry();
  649. if (!d_tracer)
  650. return NULL;
  651. d_events = debugfs_create_dir("events", d_tracer);
  652. if (!d_events)
  653. pr_warning("Could not create debugfs "
  654. "'events' directory\n");
  655. return d_events;
  656. }
  657. static LIST_HEAD(event_subsystems);
  658. static struct dentry *
  659. event_subsystem_dir(const char *name, struct dentry *d_events)
  660. {
  661. struct event_subsystem *system;
  662. struct dentry *entry;
  663. /* First see if we did not already create this dir */
  664. list_for_each_entry(system, &event_subsystems, list) {
  665. if (strcmp(system->name, name) == 0) {
  666. system->nr_events++;
  667. return system->entry;
  668. }
  669. }
  670. /* need to create new entry */
  671. system = kmalloc(sizeof(*system), GFP_KERNEL);
  672. if (!system) {
  673. pr_warning("No memory to create event subsystem %s\n",
  674. name);
  675. return d_events;
  676. }
  677. system->entry = debugfs_create_dir(name, d_events);
  678. if (!system->entry) {
  679. pr_warning("Could not create event subsystem %s\n",
  680. name);
  681. kfree(system);
  682. return d_events;
  683. }
  684. system->nr_events = 1;
  685. system->name = kstrdup(name, GFP_KERNEL);
  686. if (!system->name) {
  687. debugfs_remove(system->entry);
  688. kfree(system);
  689. return d_events;
  690. }
  691. list_add(&system->list, &event_subsystems);
  692. system->filter = NULL;
  693. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  694. if (!system->filter) {
  695. pr_warning("Could not allocate filter for subsystem "
  696. "'%s'\n", name);
  697. return system->entry;
  698. }
  699. entry = debugfs_create_file("filter", 0644, system->entry, system,
  700. &ftrace_subsystem_filter_fops);
  701. if (!entry) {
  702. kfree(system->filter);
  703. system->filter = NULL;
  704. pr_warning("Could not create debugfs "
  705. "'%s/filter' entry\n", name);
  706. }
  707. trace_create_file("enable", 0644, system->entry,
  708. (void *)system->name,
  709. &ftrace_system_enable_fops);
  710. return system->entry;
  711. }
  712. static int
  713. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
  714. const struct file_operations *id,
  715. const struct file_operations *enable,
  716. const struct file_operations *filter,
  717. const struct file_operations *format)
  718. {
  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. trace_create_file("enable", 0644, call->dir, call,
  734. enable);
  735. if (call->id && call->profile_enable)
  736. 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. 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. 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. static 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. /*
  1059. * Testing syscall events here is pretty useless, but
  1060. * we still do it if configured. But this is time consuming.
  1061. * What we really need is a user thread to perform the
  1062. * syscalls as we test.
  1063. */
  1064. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  1065. if (call->system &&
  1066. strcmp(call->system, "syscalls") == 0)
  1067. continue;
  1068. #endif
  1069. pr_info("Testing event %s: ", call->name);
  1070. /*
  1071. * If an event is already enabled, someone is using
  1072. * it and the self test should not be on.
  1073. */
  1074. if (call->enabled) {
  1075. pr_warning("Enabled event during self test!\n");
  1076. WARN_ON_ONCE(1);
  1077. continue;
  1078. }
  1079. ftrace_event_enable_disable(call, 1);
  1080. event_test_stuff();
  1081. ftrace_event_enable_disable(call, 0);
  1082. pr_cont("OK\n");
  1083. }
  1084. /* Now test at the sub system level */
  1085. pr_info("Running tests on trace event systems:\n");
  1086. list_for_each_entry(system, &event_subsystems, list) {
  1087. /* the ftrace system is special, skip it */
  1088. if (strcmp(system->name, "ftrace") == 0)
  1089. continue;
  1090. pr_info("Testing event system %s: ", system->name);
  1091. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
  1092. if (WARN_ON_ONCE(ret)) {
  1093. pr_warning("error enabling system %s\n",
  1094. system->name);
  1095. continue;
  1096. }
  1097. event_test_stuff();
  1098. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
  1099. if (WARN_ON_ONCE(ret))
  1100. pr_warning("error disabling system %s\n",
  1101. system->name);
  1102. pr_cont("OK\n");
  1103. }
  1104. /* Test with all events enabled */
  1105. pr_info("Running tests on all trace events:\n");
  1106. pr_info("Testing all events: ");
  1107. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
  1108. if (WARN_ON_ONCE(ret)) {
  1109. pr_warning("error enabling all events\n");
  1110. return;
  1111. }
  1112. event_test_stuff();
  1113. /* reset sysname */
  1114. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
  1115. if (WARN_ON_ONCE(ret)) {
  1116. pr_warning("error disabling all events\n");
  1117. return;
  1118. }
  1119. pr_cont("OK\n");
  1120. }
  1121. #ifdef CONFIG_FUNCTION_TRACER
  1122. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  1123. static void
  1124. function_test_events_call(unsigned long ip, unsigned long parent_ip)
  1125. {
  1126. struct ring_buffer_event *event;
  1127. struct ring_buffer *buffer;
  1128. struct ftrace_entry *entry;
  1129. unsigned long flags;
  1130. long disabled;
  1131. int resched;
  1132. int cpu;
  1133. int pc;
  1134. pc = preempt_count();
  1135. resched = ftrace_preempt_disable();
  1136. cpu = raw_smp_processor_id();
  1137. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  1138. if (disabled != 1)
  1139. goto out;
  1140. local_save_flags(flags);
  1141. event = trace_current_buffer_lock_reserve(&buffer,
  1142. TRACE_FN, sizeof(*entry),
  1143. flags, pc);
  1144. if (!event)
  1145. goto out;
  1146. entry = ring_buffer_event_data(event);
  1147. entry->ip = ip;
  1148. entry->parent_ip = parent_ip;
  1149. trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
  1150. out:
  1151. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  1152. ftrace_preempt_enable(resched);
  1153. }
  1154. static struct ftrace_ops trace_ops __initdata =
  1155. {
  1156. .func = function_test_events_call,
  1157. };
  1158. static __init void event_trace_self_test_with_function(void)
  1159. {
  1160. register_ftrace_function(&trace_ops);
  1161. pr_info("Running tests again, along with the function tracer\n");
  1162. event_trace_self_tests();
  1163. unregister_ftrace_function(&trace_ops);
  1164. }
  1165. #else
  1166. static __init void event_trace_self_test_with_function(void)
  1167. {
  1168. }
  1169. #endif
  1170. static __init int event_trace_self_tests_init(void)
  1171. {
  1172. if (!tracing_selftest_disabled) {
  1173. event_trace_self_tests();
  1174. event_trace_self_test_with_function();
  1175. }
  1176. return 0;
  1177. }
  1178. late_initcall(event_trace_self_tests_init);
  1179. #endif