trace_events.c 37 KB

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