trace_events.c 31 KB

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