trace_events.c 37 KB

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