trace_events.c 37 KB

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