trace_events.c 30 KB

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