trace_events.c 28 KB

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