trace_events.c 35 KB

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