trace_events.c 32 KB

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