trace_events.c 32 KB

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