trace_events.c 37 KB

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