trace_events.c 37 KB

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