trace_events.c 30 KB

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