trace_events.c 38 KB

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