trace_events.c 36 KB

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