trace_events.c 31 KB

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