trace_events.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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 ftrace_event_call *call = v;
  219. (*pos)++;
  220. list_for_each_entry_continue(call, &ftrace_events, list) {
  221. /*
  222. * The ftrace subsystem is for showing formats only.
  223. * They can not be enabled or disabled via the event files.
  224. */
  225. if (call->regfunc)
  226. return call;
  227. }
  228. return NULL;
  229. }
  230. static void *t_start(struct seq_file *m, loff_t *pos)
  231. {
  232. struct ftrace_event_call *call;
  233. loff_t l;
  234. mutex_lock(&event_mutex);
  235. call = list_entry(&ftrace_events, struct ftrace_event_call, list);
  236. for (l = 0; l <= *pos; ) {
  237. call = t_next(m, call, &l);
  238. if (!call)
  239. break;
  240. }
  241. return call;
  242. }
  243. static void *
  244. s_next(struct seq_file *m, void *v, loff_t *pos)
  245. {
  246. struct ftrace_event_call *call = v;
  247. (*pos)++;
  248. list_for_each_entry_continue(call, &ftrace_events, list) {
  249. if (call->enabled)
  250. return call;
  251. }
  252. return NULL;
  253. }
  254. static void *s_start(struct seq_file *m, loff_t *pos)
  255. {
  256. struct ftrace_event_call *call;
  257. loff_t l;
  258. mutex_lock(&event_mutex);
  259. call = list_entry(&ftrace_events, struct ftrace_event_call, list);
  260. for (l = 0; l <= *pos; ) {
  261. call = s_next(m, call, &l);
  262. if (!call)
  263. break;
  264. }
  265. return call;
  266. }
  267. static int t_show(struct seq_file *m, void *v)
  268. {
  269. struct ftrace_event_call *call = v;
  270. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  271. seq_printf(m, "%s:", call->system);
  272. seq_printf(m, "%s\n", call->name);
  273. return 0;
  274. }
  275. static void t_stop(struct seq_file *m, void *p)
  276. {
  277. mutex_unlock(&event_mutex);
  278. }
  279. static int
  280. ftrace_event_seq_open(struct inode *inode, struct file *file)
  281. {
  282. const struct seq_operations *seq_ops;
  283. if ((file->f_mode & FMODE_WRITE) &&
  284. (file->f_flags & O_TRUNC))
  285. ftrace_clear_events();
  286. seq_ops = inode->i_private;
  287. return seq_open(file, seq_ops);
  288. }
  289. static ssize_t
  290. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  291. loff_t *ppos)
  292. {
  293. struct ftrace_event_call *call = filp->private_data;
  294. char *buf;
  295. if (call->enabled)
  296. buf = "1\n";
  297. else
  298. buf = "0\n";
  299. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  300. }
  301. static ssize_t
  302. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  303. loff_t *ppos)
  304. {
  305. struct ftrace_event_call *call = filp->private_data;
  306. char buf[64];
  307. unsigned long val;
  308. int ret;
  309. if (cnt >= sizeof(buf))
  310. return -EINVAL;
  311. if (copy_from_user(&buf, ubuf, cnt))
  312. return -EFAULT;
  313. buf[cnt] = 0;
  314. ret = strict_strtoul(buf, 10, &val);
  315. if (ret < 0)
  316. return ret;
  317. ret = tracing_update_buffers();
  318. if (ret < 0)
  319. return ret;
  320. switch (val) {
  321. case 0:
  322. case 1:
  323. mutex_lock(&event_mutex);
  324. ftrace_event_enable_disable(call, val);
  325. mutex_unlock(&event_mutex);
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. *ppos += cnt;
  331. return cnt;
  332. }
  333. static ssize_t
  334. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  335. loff_t *ppos)
  336. {
  337. const char set_to_char[4] = { '?', '0', '1', 'X' };
  338. const char *system = filp->private_data;
  339. struct ftrace_event_call *call;
  340. char buf[2];
  341. int set = 0;
  342. int ret;
  343. mutex_lock(&event_mutex);
  344. list_for_each_entry(call, &ftrace_events, list) {
  345. if (!call->name || !call->regfunc)
  346. continue;
  347. if (system && strcmp(call->system, system) != 0)
  348. continue;
  349. /*
  350. * We need to find out if all the events are set
  351. * or if all events or cleared, or if we have
  352. * a mixture.
  353. */
  354. set |= (1 << !!call->enabled);
  355. /*
  356. * If we have a mixture, no need to look further.
  357. */
  358. if (set == 3)
  359. break;
  360. }
  361. mutex_unlock(&event_mutex);
  362. buf[0] = set_to_char[set];
  363. buf[1] = '\n';
  364. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  365. return ret;
  366. }
  367. static ssize_t
  368. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  369. loff_t *ppos)
  370. {
  371. const char *system = filp->private_data;
  372. unsigned long val;
  373. char buf[64];
  374. ssize_t ret;
  375. if (cnt >= sizeof(buf))
  376. return -EINVAL;
  377. if (copy_from_user(&buf, ubuf, cnt))
  378. return -EFAULT;
  379. buf[cnt] = 0;
  380. ret = strict_strtoul(buf, 10, &val);
  381. if (ret < 0)
  382. return ret;
  383. ret = tracing_update_buffers();
  384. if (ret < 0)
  385. return ret;
  386. if (val != 0 && val != 1)
  387. return -EINVAL;
  388. ret = __ftrace_set_clr_event(NULL, system, NULL, val);
  389. if (ret)
  390. goto out;
  391. ret = cnt;
  392. out:
  393. *ppos += cnt;
  394. return ret;
  395. }
  396. extern char *__bad_type_size(void);
  397. #undef FIELD
  398. #define FIELD(type, name) \
  399. sizeof(type) != sizeof(field.name) ? __bad_type_size() : \
  400. #type, "common_" #name, offsetof(typeof(field), name), \
  401. sizeof(field.name)
  402. static int trace_write_header(struct trace_seq *s)
  403. {
  404. struct trace_entry field;
  405. /* struct trace_entry */
  406. return trace_seq_printf(s,
  407. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  408. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  409. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  410. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  411. "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
  412. "\n",
  413. FIELD(unsigned short, type),
  414. FIELD(unsigned char, flags),
  415. FIELD(unsigned char, preempt_count),
  416. FIELD(int, pid),
  417. FIELD(int, lock_depth));
  418. }
  419. static ssize_t
  420. event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
  421. loff_t *ppos)
  422. {
  423. struct ftrace_event_call *call = filp->private_data;
  424. struct trace_seq *s;
  425. char *buf;
  426. int r;
  427. if (*ppos)
  428. return 0;
  429. s = kmalloc(sizeof(*s), GFP_KERNEL);
  430. if (!s)
  431. return -ENOMEM;
  432. trace_seq_init(s);
  433. /* If any of the first writes fail, so will the show_format. */
  434. trace_seq_printf(s, "name: %s\n", call->name);
  435. trace_seq_printf(s, "ID: %d\n", call->id);
  436. trace_seq_printf(s, "format:\n");
  437. trace_write_header(s);
  438. r = call->show_format(call, s);
  439. if (!r) {
  440. /*
  441. * ug! The format output is bigger than a PAGE!!
  442. */
  443. buf = "FORMAT TOO BIG\n";
  444. r = simple_read_from_buffer(ubuf, cnt, ppos,
  445. buf, strlen(buf));
  446. goto out;
  447. }
  448. r = simple_read_from_buffer(ubuf, cnt, ppos,
  449. s->buffer, s->len);
  450. out:
  451. kfree(s);
  452. return r;
  453. }
  454. static ssize_t
  455. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  456. {
  457. struct ftrace_event_call *call = filp->private_data;
  458. struct trace_seq *s;
  459. int r;
  460. if (*ppos)
  461. return 0;
  462. s = kmalloc(sizeof(*s), GFP_KERNEL);
  463. if (!s)
  464. return -ENOMEM;
  465. trace_seq_init(s);
  466. trace_seq_printf(s, "%d\n", call->id);
  467. r = simple_read_from_buffer(ubuf, cnt, ppos,
  468. s->buffer, s->len);
  469. kfree(s);
  470. return r;
  471. }
  472. static ssize_t
  473. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  474. loff_t *ppos)
  475. {
  476. struct ftrace_event_call *call = filp->private_data;
  477. struct trace_seq *s;
  478. int r;
  479. if (*ppos)
  480. return 0;
  481. s = kmalloc(sizeof(*s), GFP_KERNEL);
  482. if (!s)
  483. return -ENOMEM;
  484. trace_seq_init(s);
  485. print_event_filter(call, s);
  486. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  487. kfree(s);
  488. return r;
  489. }
  490. static ssize_t
  491. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  492. loff_t *ppos)
  493. {
  494. struct ftrace_event_call *call = filp->private_data;
  495. char *buf;
  496. int err;
  497. if (cnt >= PAGE_SIZE)
  498. return -EINVAL;
  499. buf = (char *)__get_free_page(GFP_TEMPORARY);
  500. if (!buf)
  501. return -ENOMEM;
  502. if (copy_from_user(buf, ubuf, cnt)) {
  503. free_page((unsigned long) buf);
  504. return -EFAULT;
  505. }
  506. buf[cnt] = '\0';
  507. err = apply_event_filter(call, buf);
  508. free_page((unsigned long) buf);
  509. if (err < 0)
  510. return err;
  511. *ppos += cnt;
  512. return cnt;
  513. }
  514. static ssize_t
  515. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  516. loff_t *ppos)
  517. {
  518. struct event_subsystem *system = filp->private_data;
  519. struct trace_seq *s;
  520. int r;
  521. if (*ppos)
  522. return 0;
  523. s = kmalloc(sizeof(*s), GFP_KERNEL);
  524. if (!s)
  525. return -ENOMEM;
  526. trace_seq_init(s);
  527. print_subsystem_event_filter(system, s);
  528. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  529. kfree(s);
  530. return r;
  531. }
  532. static ssize_t
  533. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  534. loff_t *ppos)
  535. {
  536. struct event_subsystem *system = filp->private_data;
  537. char *buf;
  538. int err;
  539. if (cnt >= PAGE_SIZE)
  540. return -EINVAL;
  541. buf = (char *)__get_free_page(GFP_TEMPORARY);
  542. if (!buf)
  543. return -ENOMEM;
  544. if (copy_from_user(buf, ubuf, cnt)) {
  545. free_page((unsigned long) buf);
  546. return -EFAULT;
  547. }
  548. buf[cnt] = '\0';
  549. err = apply_subsystem_event_filter(system, buf);
  550. free_page((unsigned long) buf);
  551. if (err < 0)
  552. return err;
  553. *ppos += cnt;
  554. return cnt;
  555. }
  556. static ssize_t
  557. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  558. {
  559. int (*func)(struct trace_seq *s) = filp->private_data;
  560. struct trace_seq *s;
  561. int r;
  562. if (*ppos)
  563. return 0;
  564. s = kmalloc(sizeof(*s), GFP_KERNEL);
  565. if (!s)
  566. return -ENOMEM;
  567. trace_seq_init(s);
  568. func(s);
  569. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  570. kfree(s);
  571. return r;
  572. }
  573. static const struct seq_operations show_event_seq_ops = {
  574. .start = t_start,
  575. .next = t_next,
  576. .show = t_show,
  577. .stop = t_stop,
  578. };
  579. static const struct seq_operations show_set_event_seq_ops = {
  580. .start = s_start,
  581. .next = s_next,
  582. .show = t_show,
  583. .stop = t_stop,
  584. };
  585. static const struct file_operations ftrace_avail_fops = {
  586. .open = ftrace_event_seq_open,
  587. .read = seq_read,
  588. .llseek = seq_lseek,
  589. .release = seq_release,
  590. };
  591. static const struct file_operations ftrace_set_event_fops = {
  592. .open = ftrace_event_seq_open,
  593. .read = seq_read,
  594. .write = ftrace_event_write,
  595. .llseek = seq_lseek,
  596. .release = seq_release,
  597. };
  598. static const struct file_operations ftrace_enable_fops = {
  599. .open = tracing_open_generic,
  600. .read = event_enable_read,
  601. .write = event_enable_write,
  602. };
  603. static const struct file_operations ftrace_event_format_fops = {
  604. .open = tracing_open_generic,
  605. .read = event_format_read,
  606. };
  607. static const struct file_operations ftrace_event_id_fops = {
  608. .open = tracing_open_generic,
  609. .read = event_id_read,
  610. };
  611. static const struct file_operations ftrace_event_filter_fops = {
  612. .open = tracing_open_generic,
  613. .read = event_filter_read,
  614. .write = event_filter_write,
  615. };
  616. static const struct file_operations ftrace_subsystem_filter_fops = {
  617. .open = tracing_open_generic,
  618. .read = subsystem_filter_read,
  619. .write = subsystem_filter_write,
  620. };
  621. static const struct file_operations ftrace_system_enable_fops = {
  622. .open = tracing_open_generic,
  623. .read = system_enable_read,
  624. .write = system_enable_write,
  625. };
  626. static const struct file_operations ftrace_show_header_fops = {
  627. .open = tracing_open_generic,
  628. .read = show_header,
  629. };
  630. static struct dentry *event_trace_events_dir(void)
  631. {
  632. static struct dentry *d_tracer;
  633. static struct dentry *d_events;
  634. if (d_events)
  635. return d_events;
  636. d_tracer = tracing_init_dentry();
  637. if (!d_tracer)
  638. return NULL;
  639. d_events = debugfs_create_dir("events", d_tracer);
  640. if (!d_events)
  641. pr_warning("Could not create debugfs "
  642. "'events' directory\n");
  643. return d_events;
  644. }
  645. static LIST_HEAD(event_subsystems);
  646. static struct dentry *
  647. event_subsystem_dir(const char *name, struct dentry *d_events)
  648. {
  649. struct event_subsystem *system;
  650. struct dentry *entry;
  651. /* First see if we did not already create this dir */
  652. list_for_each_entry(system, &event_subsystems, list) {
  653. if (strcmp(system->name, name) == 0) {
  654. system->nr_events++;
  655. return system->entry;
  656. }
  657. }
  658. /* need to create new entry */
  659. system = kmalloc(sizeof(*system), GFP_KERNEL);
  660. if (!system) {
  661. pr_warning("No memory to create event subsystem %s\n",
  662. name);
  663. return d_events;
  664. }
  665. system->entry = debugfs_create_dir(name, d_events);
  666. if (!system->entry) {
  667. pr_warning("Could not create event subsystem %s\n",
  668. name);
  669. kfree(system);
  670. return d_events;
  671. }
  672. system->nr_events = 1;
  673. system->name = kstrdup(name, GFP_KERNEL);
  674. if (!system->name) {
  675. debugfs_remove(system->entry);
  676. kfree(system);
  677. return d_events;
  678. }
  679. list_add(&system->list, &event_subsystems);
  680. system->filter = NULL;
  681. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  682. if (!system->filter) {
  683. pr_warning("Could not allocate filter for subsystem "
  684. "'%s'\n", name);
  685. return system->entry;
  686. }
  687. entry = debugfs_create_file("filter", 0644, system->entry, system,
  688. &ftrace_subsystem_filter_fops);
  689. if (!entry) {
  690. kfree(system->filter);
  691. system->filter = NULL;
  692. pr_warning("Could not create debugfs "
  693. "'%s/filter' entry\n", name);
  694. }
  695. entry = trace_create_file("enable", 0644, system->entry,
  696. (void *)system->name,
  697. &ftrace_system_enable_fops);
  698. return system->entry;
  699. }
  700. static int
  701. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
  702. const struct file_operations *id,
  703. const struct file_operations *enable,
  704. const struct file_operations *filter,
  705. const struct file_operations *format)
  706. {
  707. struct dentry *entry;
  708. int ret;
  709. /*
  710. * If the trace point header did not define TRACE_SYSTEM
  711. * then the system would be called "TRACE_SYSTEM".
  712. */
  713. if (strcmp(call->system, TRACE_SYSTEM) != 0)
  714. d_events = event_subsystem_dir(call->system, d_events);
  715. call->dir = debugfs_create_dir(call->name, d_events);
  716. if (!call->dir) {
  717. pr_warning("Could not create debugfs "
  718. "'%s' directory\n", call->name);
  719. return -1;
  720. }
  721. if (call->regfunc)
  722. entry = trace_create_file("enable", 0644, call->dir, call,
  723. enable);
  724. if (call->id && call->profile_enable)
  725. entry = trace_create_file("id", 0444, call->dir, call,
  726. id);
  727. if (call->define_fields) {
  728. ret = call->define_fields(call);
  729. if (ret < 0) {
  730. pr_warning("Could not initialize trace point"
  731. " events/%s\n", call->name);
  732. return ret;
  733. }
  734. entry = trace_create_file("filter", 0644, call->dir, call,
  735. filter);
  736. }
  737. /* A trace may not want to export its format */
  738. if (!call->show_format)
  739. return 0;
  740. entry = trace_create_file("format", 0444, call->dir, call,
  741. format);
  742. return 0;
  743. }
  744. #define for_each_event(event, start, end) \
  745. for (event = start; \
  746. (unsigned long)event < (unsigned long)end; \
  747. event++)
  748. #ifdef CONFIG_MODULES
  749. static LIST_HEAD(ftrace_module_file_list);
  750. /*
  751. * Modules must own their file_operations to keep up with
  752. * reference counting.
  753. */
  754. struct ftrace_module_file_ops {
  755. struct list_head list;
  756. struct module *mod;
  757. struct file_operations id;
  758. struct file_operations enable;
  759. struct file_operations format;
  760. struct file_operations filter;
  761. };
  762. static void remove_subsystem_dir(const char *name)
  763. {
  764. struct event_subsystem *system;
  765. if (strcmp(name, TRACE_SYSTEM) == 0)
  766. return;
  767. list_for_each_entry(system, &event_subsystems, list) {
  768. if (strcmp(system->name, name) == 0) {
  769. if (!--system->nr_events) {
  770. struct event_filter *filter = system->filter;
  771. debugfs_remove_recursive(system->entry);
  772. list_del(&system->list);
  773. if (filter) {
  774. kfree(filter->filter_string);
  775. kfree(filter);
  776. }
  777. kfree(system->name);
  778. kfree(system);
  779. }
  780. break;
  781. }
  782. }
  783. }
  784. static struct ftrace_module_file_ops *
  785. trace_create_file_ops(struct module *mod)
  786. {
  787. struct ftrace_module_file_ops *file_ops;
  788. /*
  789. * This is a bit of a PITA. To allow for correct reference
  790. * counting, modules must "own" their file_operations.
  791. * To do this, we allocate the file operations that will be
  792. * used in the event directory.
  793. */
  794. file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
  795. if (!file_ops)
  796. return NULL;
  797. file_ops->mod = mod;
  798. file_ops->id = ftrace_event_id_fops;
  799. file_ops->id.owner = mod;
  800. file_ops->enable = ftrace_enable_fops;
  801. file_ops->enable.owner = mod;
  802. file_ops->filter = ftrace_event_filter_fops;
  803. file_ops->filter.owner = mod;
  804. file_ops->format = ftrace_event_format_fops;
  805. file_ops->format.owner = mod;
  806. list_add(&file_ops->list, &ftrace_module_file_list);
  807. return file_ops;
  808. }
  809. static void trace_module_add_events(struct module *mod)
  810. {
  811. struct ftrace_module_file_ops *file_ops = NULL;
  812. struct ftrace_event_call *call, *start, *end;
  813. struct dentry *d_events;
  814. int ret;
  815. start = mod->trace_events;
  816. end = mod->trace_events + mod->num_trace_events;
  817. if (start == end)
  818. return;
  819. d_events = event_trace_events_dir();
  820. if (!d_events)
  821. return;
  822. for_each_event(call, start, end) {
  823. /* The linker may leave blanks */
  824. if (!call->name)
  825. continue;
  826. if (call->raw_init) {
  827. ret = call->raw_init();
  828. if (ret < 0) {
  829. if (ret != -ENOSYS)
  830. pr_warning("Could not initialize trace "
  831. "point events/%s\n", call->name);
  832. continue;
  833. }
  834. }
  835. /*
  836. * This module has events, create file ops for this module
  837. * if not already done.
  838. */
  839. if (!file_ops) {
  840. file_ops = trace_create_file_ops(mod);
  841. if (!file_ops)
  842. return;
  843. }
  844. call->mod = mod;
  845. list_add(&call->list, &ftrace_events);
  846. event_create_dir(call, d_events,
  847. &file_ops->id, &file_ops->enable,
  848. &file_ops->filter, &file_ops->format);
  849. }
  850. }
  851. static void trace_module_remove_events(struct module *mod)
  852. {
  853. struct ftrace_module_file_ops *file_ops;
  854. struct ftrace_event_call *call, *p;
  855. bool found = false;
  856. down_write(&trace_event_mutex);
  857. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  858. if (call->mod == mod) {
  859. found = true;
  860. ftrace_event_enable_disable(call, 0);
  861. if (call->event)
  862. __unregister_ftrace_event(call->event);
  863. debugfs_remove_recursive(call->dir);
  864. list_del(&call->list);
  865. trace_destroy_fields(call);
  866. destroy_preds(call);
  867. remove_subsystem_dir(call->system);
  868. }
  869. }
  870. /* Now free the file_operations */
  871. list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
  872. if (file_ops->mod == mod)
  873. break;
  874. }
  875. if (&file_ops->list != &ftrace_module_file_list) {
  876. list_del(&file_ops->list);
  877. kfree(file_ops);
  878. }
  879. /*
  880. * It is safest to reset the ring buffer if the module being unloaded
  881. * registered any events.
  882. */
  883. if (found)
  884. tracing_reset_current_online_cpus();
  885. up_write(&trace_event_mutex);
  886. }
  887. static int trace_module_notify(struct notifier_block *self,
  888. unsigned long val, void *data)
  889. {
  890. struct module *mod = data;
  891. mutex_lock(&event_mutex);
  892. switch (val) {
  893. case MODULE_STATE_COMING:
  894. trace_module_add_events(mod);
  895. break;
  896. case MODULE_STATE_GOING:
  897. trace_module_remove_events(mod);
  898. break;
  899. }
  900. mutex_unlock(&event_mutex);
  901. return 0;
  902. }
  903. #else
  904. static int trace_module_notify(struct notifier_block *self,
  905. unsigned long val, void *data)
  906. {
  907. return 0;
  908. }
  909. #endif /* CONFIG_MODULES */
  910. static struct notifier_block trace_module_nb = {
  911. .notifier_call = trace_module_notify,
  912. .priority = 0,
  913. };
  914. extern struct ftrace_event_call __start_ftrace_events[];
  915. extern struct ftrace_event_call __stop_ftrace_events[];
  916. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  917. static __init int setup_trace_event(char *str)
  918. {
  919. strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  920. ring_buffer_expanded = 1;
  921. tracing_selftest_disabled = 1;
  922. return 1;
  923. }
  924. __setup("trace_event=", setup_trace_event);
  925. static __init int event_trace_init(void)
  926. {
  927. struct ftrace_event_call *call;
  928. struct dentry *d_tracer;
  929. struct dentry *entry;
  930. struct dentry *d_events;
  931. int ret;
  932. char *buf = bootup_event_buf;
  933. char *token;
  934. d_tracer = tracing_init_dentry();
  935. if (!d_tracer)
  936. return 0;
  937. entry = debugfs_create_file("available_events", 0444, d_tracer,
  938. (void *)&show_event_seq_ops,
  939. &ftrace_avail_fops);
  940. if (!entry)
  941. pr_warning("Could not create debugfs "
  942. "'available_events' entry\n");
  943. entry = debugfs_create_file("set_event", 0644, d_tracer,
  944. (void *)&show_set_event_seq_ops,
  945. &ftrace_set_event_fops);
  946. if (!entry)
  947. pr_warning("Could not create debugfs "
  948. "'set_event' entry\n");
  949. d_events = event_trace_events_dir();
  950. if (!d_events)
  951. return 0;
  952. /* ring buffer internal formats */
  953. trace_create_file("header_page", 0444, d_events,
  954. ring_buffer_print_page_header,
  955. &ftrace_show_header_fops);
  956. trace_create_file("header_event", 0444, d_events,
  957. ring_buffer_print_entry_header,
  958. &ftrace_show_header_fops);
  959. trace_create_file("enable", 0644, d_events,
  960. NULL, &ftrace_system_enable_fops);
  961. for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
  962. /* The linker may leave blanks */
  963. if (!call->name)
  964. continue;
  965. if (call->raw_init) {
  966. ret = call->raw_init();
  967. if (ret < 0) {
  968. if (ret != -ENOSYS)
  969. pr_warning("Could not initialize trace "
  970. "point events/%s\n", call->name);
  971. continue;
  972. }
  973. }
  974. list_add(&call->list, &ftrace_events);
  975. event_create_dir(call, d_events, &ftrace_event_id_fops,
  976. &ftrace_enable_fops, &ftrace_event_filter_fops,
  977. &ftrace_event_format_fops);
  978. }
  979. while (true) {
  980. token = strsep(&buf, ",");
  981. if (!token)
  982. break;
  983. if (!*token)
  984. continue;
  985. ret = ftrace_set_clr_event(token, 1);
  986. if (ret)
  987. pr_warning("Failed to enable trace event: %s\n", token);
  988. }
  989. ret = register_module_notifier(&trace_module_nb);
  990. if (ret)
  991. pr_warning("Failed to register trace events module notifier\n");
  992. return 0;
  993. }
  994. fs_initcall(event_trace_init);
  995. #ifdef CONFIG_FTRACE_STARTUP_TEST
  996. static DEFINE_SPINLOCK(test_spinlock);
  997. static DEFINE_SPINLOCK(test_spinlock_irq);
  998. static DEFINE_MUTEX(test_mutex);
  999. static __init void test_work(struct work_struct *dummy)
  1000. {
  1001. spin_lock(&test_spinlock);
  1002. spin_lock_irq(&test_spinlock_irq);
  1003. udelay(1);
  1004. spin_unlock_irq(&test_spinlock_irq);
  1005. spin_unlock(&test_spinlock);
  1006. mutex_lock(&test_mutex);
  1007. msleep(1);
  1008. mutex_unlock(&test_mutex);
  1009. }
  1010. static __init int event_test_thread(void *unused)
  1011. {
  1012. void *test_malloc;
  1013. test_malloc = kmalloc(1234, GFP_KERNEL);
  1014. if (!test_malloc)
  1015. pr_info("failed to kmalloc\n");
  1016. schedule_on_each_cpu(test_work);
  1017. kfree(test_malloc);
  1018. set_current_state(TASK_INTERRUPTIBLE);
  1019. while (!kthread_should_stop())
  1020. schedule();
  1021. return 0;
  1022. }
  1023. /*
  1024. * Do various things that may trigger events.
  1025. */
  1026. static __init void event_test_stuff(void)
  1027. {
  1028. struct task_struct *test_thread;
  1029. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  1030. msleep(1);
  1031. kthread_stop(test_thread);
  1032. }
  1033. /*
  1034. * For every trace event defined, we will test each trace point separately,
  1035. * and then by groups, and finally all trace points.
  1036. */
  1037. static __init void event_trace_self_tests(void)
  1038. {
  1039. struct ftrace_event_call *call;
  1040. struct event_subsystem *system;
  1041. int ret;
  1042. pr_info("Running tests on trace events:\n");
  1043. list_for_each_entry(call, &ftrace_events, list) {
  1044. /* Only test those that have a regfunc */
  1045. if (!call->regfunc)
  1046. continue;
  1047. /*
  1048. * Testing syscall events here is pretty useless, but
  1049. * we still do it if configured. But this is time consuming.
  1050. * What we really need is a user thread to perform the
  1051. * syscalls as we test.
  1052. */
  1053. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  1054. if (call->system &&
  1055. strcmp(call->system, "syscalls") == 0)
  1056. continue;
  1057. #endif
  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, ftrace_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(ftrace_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(ftrace_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