trace_events.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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. .llseek = default_llseek,
  741. };
  742. static const struct file_operations ftrace_event_format_fops = {
  743. .open = trace_format_open,
  744. .read = seq_read,
  745. .llseek = seq_lseek,
  746. .release = seq_release,
  747. };
  748. static const struct file_operations ftrace_event_id_fops = {
  749. .open = tracing_open_generic,
  750. .read = event_id_read,
  751. .llseek = default_llseek,
  752. };
  753. static const struct file_operations ftrace_event_filter_fops = {
  754. .open = tracing_open_generic,
  755. .read = event_filter_read,
  756. .write = event_filter_write,
  757. .llseek = default_llseek,
  758. };
  759. static const struct file_operations ftrace_subsystem_filter_fops = {
  760. .open = tracing_open_generic,
  761. .read = subsystem_filter_read,
  762. .write = subsystem_filter_write,
  763. .llseek = default_llseek,
  764. };
  765. static const struct file_operations ftrace_system_enable_fops = {
  766. .open = tracing_open_generic,
  767. .read = system_enable_read,
  768. .write = system_enable_write,
  769. .llseek = default_llseek,
  770. };
  771. static const struct file_operations ftrace_show_header_fops = {
  772. .open = tracing_open_generic,
  773. .read = show_header,
  774. .llseek = default_llseek,
  775. };
  776. static struct dentry *event_trace_events_dir(void)
  777. {
  778. static struct dentry *d_tracer;
  779. static struct dentry *d_events;
  780. if (d_events)
  781. return d_events;
  782. d_tracer = tracing_init_dentry();
  783. if (!d_tracer)
  784. return NULL;
  785. d_events = debugfs_create_dir("events", d_tracer);
  786. if (!d_events)
  787. pr_warning("Could not create debugfs "
  788. "'events' directory\n");
  789. return d_events;
  790. }
  791. static LIST_HEAD(event_subsystems);
  792. static struct dentry *
  793. event_subsystem_dir(const char *name, struct dentry *d_events)
  794. {
  795. struct event_subsystem *system;
  796. struct dentry *entry;
  797. /* First see if we did not already create this dir */
  798. list_for_each_entry(system, &event_subsystems, list) {
  799. if (strcmp(system->name, name) == 0) {
  800. system->nr_events++;
  801. return system->entry;
  802. }
  803. }
  804. /* need to create new entry */
  805. system = kmalloc(sizeof(*system), GFP_KERNEL);
  806. if (!system) {
  807. pr_warning("No memory to create event subsystem %s\n",
  808. name);
  809. return d_events;
  810. }
  811. system->entry = debugfs_create_dir(name, d_events);
  812. if (!system->entry) {
  813. pr_warning("Could not create event subsystem %s\n",
  814. name);
  815. kfree(system);
  816. return d_events;
  817. }
  818. system->nr_events = 1;
  819. system->name = kstrdup(name, GFP_KERNEL);
  820. if (!system->name) {
  821. debugfs_remove(system->entry);
  822. kfree(system);
  823. return d_events;
  824. }
  825. list_add(&system->list, &event_subsystems);
  826. system->filter = NULL;
  827. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  828. if (!system->filter) {
  829. pr_warning("Could not allocate filter for subsystem "
  830. "'%s'\n", name);
  831. return system->entry;
  832. }
  833. entry = debugfs_create_file("filter", 0644, system->entry, system,
  834. &ftrace_subsystem_filter_fops);
  835. if (!entry) {
  836. kfree(system->filter);
  837. system->filter = NULL;
  838. pr_warning("Could not create debugfs "
  839. "'%s/filter' entry\n", name);
  840. }
  841. trace_create_file("enable", 0644, system->entry,
  842. (void *)system->name,
  843. &ftrace_system_enable_fops);
  844. return system->entry;
  845. }
  846. static int
  847. event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
  848. const struct file_operations *id,
  849. const struct file_operations *enable,
  850. const struct file_operations *filter,
  851. const struct file_operations *format)
  852. {
  853. struct list_head *head;
  854. int ret;
  855. /*
  856. * If the trace point header did not define TRACE_SYSTEM
  857. * then the system would be called "TRACE_SYSTEM".
  858. */
  859. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  860. d_events = event_subsystem_dir(call->class->system, d_events);
  861. call->dir = debugfs_create_dir(call->name, d_events);
  862. if (!call->dir) {
  863. pr_warning("Could not create debugfs "
  864. "'%s' directory\n", call->name);
  865. return -1;
  866. }
  867. if (call->class->reg)
  868. trace_create_file("enable", 0644, call->dir, call,
  869. enable);
  870. #ifdef CONFIG_PERF_EVENTS
  871. if (call->event.type && call->class->reg)
  872. trace_create_file("id", 0444, call->dir, call,
  873. id);
  874. #endif
  875. /*
  876. * Other events may have the same class. Only update
  877. * the fields if they are not already defined.
  878. */
  879. head = trace_get_fields(call);
  880. if (list_empty(head)) {
  881. ret = call->class->define_fields(call);
  882. if (ret < 0) {
  883. pr_warning("Could not initialize trace point"
  884. " events/%s\n", call->name);
  885. return ret;
  886. }
  887. }
  888. trace_create_file("filter", 0644, call->dir, call,
  889. filter);
  890. trace_create_file("format", 0444, call->dir, call,
  891. format);
  892. return 0;
  893. }
  894. static int
  895. __trace_add_event_call(struct ftrace_event_call *call, struct module *mod,
  896. const struct file_operations *id,
  897. const struct file_operations *enable,
  898. const struct file_operations *filter,
  899. const struct file_operations *format)
  900. {
  901. struct dentry *d_events;
  902. int ret;
  903. /* The linker may leave blanks */
  904. if (!call->name)
  905. return -EINVAL;
  906. if (call->class->raw_init) {
  907. ret = call->class->raw_init(call);
  908. if (ret < 0) {
  909. if (ret != -ENOSYS)
  910. pr_warning("Could not initialize trace events/%s\n",
  911. call->name);
  912. return ret;
  913. }
  914. }
  915. d_events = event_trace_events_dir();
  916. if (!d_events)
  917. return -ENOENT;
  918. ret = event_create_dir(call, d_events, id, enable, filter, format);
  919. if (!ret)
  920. list_add(&call->list, &ftrace_events);
  921. call->mod = mod;
  922. return ret;
  923. }
  924. /* Add an additional event_call dynamically */
  925. int trace_add_event_call(struct ftrace_event_call *call)
  926. {
  927. int ret;
  928. mutex_lock(&event_mutex);
  929. ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
  930. &ftrace_enable_fops,
  931. &ftrace_event_filter_fops,
  932. &ftrace_event_format_fops);
  933. mutex_unlock(&event_mutex);
  934. return ret;
  935. }
  936. static void remove_subsystem_dir(const char *name)
  937. {
  938. struct event_subsystem *system;
  939. if (strcmp(name, TRACE_SYSTEM) == 0)
  940. return;
  941. list_for_each_entry(system, &event_subsystems, list) {
  942. if (strcmp(system->name, name) == 0) {
  943. if (!--system->nr_events) {
  944. struct event_filter *filter = system->filter;
  945. debugfs_remove_recursive(system->entry);
  946. list_del(&system->list);
  947. if (filter) {
  948. kfree(filter->filter_string);
  949. kfree(filter);
  950. }
  951. kfree(system->name);
  952. kfree(system);
  953. }
  954. break;
  955. }
  956. }
  957. }
  958. /*
  959. * Must be called under locking both of event_mutex and trace_event_mutex.
  960. */
  961. static void __trace_remove_event_call(struct ftrace_event_call *call)
  962. {
  963. ftrace_event_enable_disable(call, 0);
  964. if (call->event.funcs)
  965. __unregister_ftrace_event(&call->event);
  966. debugfs_remove_recursive(call->dir);
  967. list_del(&call->list);
  968. trace_destroy_fields(call);
  969. destroy_preds(call);
  970. remove_subsystem_dir(call->class->system);
  971. }
  972. /* Remove an event_call */
  973. void trace_remove_event_call(struct ftrace_event_call *call)
  974. {
  975. mutex_lock(&event_mutex);
  976. down_write(&trace_event_mutex);
  977. __trace_remove_event_call(call);
  978. up_write(&trace_event_mutex);
  979. mutex_unlock(&event_mutex);
  980. }
  981. #define for_each_event(event, start, end) \
  982. for (event = start; \
  983. (unsigned long)event < (unsigned long)end; \
  984. event++)
  985. #ifdef CONFIG_MODULES
  986. static LIST_HEAD(ftrace_module_file_list);
  987. /*
  988. * Modules must own their file_operations to keep up with
  989. * reference counting.
  990. */
  991. struct ftrace_module_file_ops {
  992. struct list_head list;
  993. struct module *mod;
  994. struct file_operations id;
  995. struct file_operations enable;
  996. struct file_operations format;
  997. struct file_operations filter;
  998. };
  999. static struct ftrace_module_file_ops *
  1000. trace_create_file_ops(struct module *mod)
  1001. {
  1002. struct ftrace_module_file_ops *file_ops;
  1003. /*
  1004. * This is a bit of a PITA. To allow for correct reference
  1005. * counting, modules must "own" their file_operations.
  1006. * To do this, we allocate the file operations that will be
  1007. * used in the event directory.
  1008. */
  1009. file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
  1010. if (!file_ops)
  1011. return NULL;
  1012. file_ops->mod = mod;
  1013. file_ops->id = ftrace_event_id_fops;
  1014. file_ops->id.owner = mod;
  1015. file_ops->enable = ftrace_enable_fops;
  1016. file_ops->enable.owner = mod;
  1017. file_ops->filter = ftrace_event_filter_fops;
  1018. file_ops->filter.owner = mod;
  1019. file_ops->format = ftrace_event_format_fops;
  1020. file_ops->format.owner = mod;
  1021. list_add(&file_ops->list, &ftrace_module_file_list);
  1022. return file_ops;
  1023. }
  1024. static void trace_module_add_events(struct module *mod)
  1025. {
  1026. struct ftrace_module_file_ops *file_ops = NULL;
  1027. struct ftrace_event_call *call, *start, *end;
  1028. start = mod->trace_events;
  1029. end = mod->trace_events + mod->num_trace_events;
  1030. if (start == end)
  1031. return;
  1032. file_ops = trace_create_file_ops(mod);
  1033. if (!file_ops)
  1034. return;
  1035. for_each_event(call, start, end) {
  1036. __trace_add_event_call(call, mod,
  1037. &file_ops->id, &file_ops->enable,
  1038. &file_ops->filter, &file_ops->format);
  1039. }
  1040. }
  1041. static void trace_module_remove_events(struct module *mod)
  1042. {
  1043. struct ftrace_module_file_ops *file_ops;
  1044. struct ftrace_event_call *call, *p;
  1045. bool found = false;
  1046. down_write(&trace_event_mutex);
  1047. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  1048. if (call->mod == mod) {
  1049. found = true;
  1050. __trace_remove_event_call(call);
  1051. }
  1052. }
  1053. /* Now free the file_operations */
  1054. list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
  1055. if (file_ops->mod == mod)
  1056. break;
  1057. }
  1058. if (&file_ops->list != &ftrace_module_file_list) {
  1059. list_del(&file_ops->list);
  1060. kfree(file_ops);
  1061. }
  1062. /*
  1063. * It is safest to reset the ring buffer if the module being unloaded
  1064. * registered any events.
  1065. */
  1066. if (found)
  1067. tracing_reset_current_online_cpus();
  1068. up_write(&trace_event_mutex);
  1069. }
  1070. static int trace_module_notify(struct notifier_block *self,
  1071. unsigned long val, void *data)
  1072. {
  1073. struct module *mod = data;
  1074. mutex_lock(&event_mutex);
  1075. switch (val) {
  1076. case MODULE_STATE_COMING:
  1077. trace_module_add_events(mod);
  1078. break;
  1079. case MODULE_STATE_GOING:
  1080. trace_module_remove_events(mod);
  1081. break;
  1082. }
  1083. mutex_unlock(&event_mutex);
  1084. return 0;
  1085. }
  1086. #else
  1087. static int trace_module_notify(struct notifier_block *self,
  1088. unsigned long val, void *data)
  1089. {
  1090. return 0;
  1091. }
  1092. #endif /* CONFIG_MODULES */
  1093. static struct notifier_block trace_module_nb = {
  1094. .notifier_call = trace_module_notify,
  1095. .priority = 0,
  1096. };
  1097. extern struct ftrace_event_call __start_ftrace_events[];
  1098. extern struct ftrace_event_call __stop_ftrace_events[];
  1099. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  1100. static __init int setup_trace_event(char *str)
  1101. {
  1102. strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  1103. ring_buffer_expanded = 1;
  1104. tracing_selftest_disabled = 1;
  1105. return 1;
  1106. }
  1107. __setup("trace_event=", setup_trace_event);
  1108. static __init int event_trace_init(void)
  1109. {
  1110. struct ftrace_event_call *call;
  1111. struct dentry *d_tracer;
  1112. struct dentry *entry;
  1113. struct dentry *d_events;
  1114. int ret;
  1115. char *buf = bootup_event_buf;
  1116. char *token;
  1117. d_tracer = tracing_init_dentry();
  1118. if (!d_tracer)
  1119. return 0;
  1120. entry = debugfs_create_file("available_events", 0444, d_tracer,
  1121. (void *)&show_event_seq_ops,
  1122. &ftrace_avail_fops);
  1123. if (!entry)
  1124. pr_warning("Could not create debugfs "
  1125. "'available_events' entry\n");
  1126. entry = debugfs_create_file("set_event", 0644, d_tracer,
  1127. (void *)&show_set_event_seq_ops,
  1128. &ftrace_set_event_fops);
  1129. if (!entry)
  1130. pr_warning("Could not create debugfs "
  1131. "'set_event' entry\n");
  1132. d_events = event_trace_events_dir();
  1133. if (!d_events)
  1134. return 0;
  1135. /* ring buffer internal formats */
  1136. trace_create_file("header_page", 0444, d_events,
  1137. ring_buffer_print_page_header,
  1138. &ftrace_show_header_fops);
  1139. trace_create_file("header_event", 0444, d_events,
  1140. ring_buffer_print_entry_header,
  1141. &ftrace_show_header_fops);
  1142. trace_create_file("enable", 0644, d_events,
  1143. NULL, &ftrace_system_enable_fops);
  1144. if (trace_define_common_fields())
  1145. pr_warning("tracing: Failed to allocate common fields");
  1146. for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
  1147. __trace_add_event_call(call, NULL, &ftrace_event_id_fops,
  1148. &ftrace_enable_fops,
  1149. &ftrace_event_filter_fops,
  1150. &ftrace_event_format_fops);
  1151. }
  1152. while (true) {
  1153. token = strsep(&buf, ",");
  1154. if (!token)
  1155. break;
  1156. if (!*token)
  1157. continue;
  1158. ret = ftrace_set_clr_event(token, 1);
  1159. if (ret)
  1160. pr_warning("Failed to enable trace event: %s\n", token);
  1161. }
  1162. ret = register_module_notifier(&trace_module_nb);
  1163. if (ret)
  1164. pr_warning("Failed to register trace events module notifier\n");
  1165. return 0;
  1166. }
  1167. fs_initcall(event_trace_init);
  1168. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1169. static DEFINE_SPINLOCK(test_spinlock);
  1170. static DEFINE_SPINLOCK(test_spinlock_irq);
  1171. static DEFINE_MUTEX(test_mutex);
  1172. static __init void test_work(struct work_struct *dummy)
  1173. {
  1174. spin_lock(&test_spinlock);
  1175. spin_lock_irq(&test_spinlock_irq);
  1176. udelay(1);
  1177. spin_unlock_irq(&test_spinlock_irq);
  1178. spin_unlock(&test_spinlock);
  1179. mutex_lock(&test_mutex);
  1180. msleep(1);
  1181. mutex_unlock(&test_mutex);
  1182. }
  1183. static __init int event_test_thread(void *unused)
  1184. {
  1185. void *test_malloc;
  1186. test_malloc = kmalloc(1234, GFP_KERNEL);
  1187. if (!test_malloc)
  1188. pr_info("failed to kmalloc\n");
  1189. schedule_on_each_cpu(test_work);
  1190. kfree(test_malloc);
  1191. set_current_state(TASK_INTERRUPTIBLE);
  1192. while (!kthread_should_stop())
  1193. schedule();
  1194. return 0;
  1195. }
  1196. /*
  1197. * Do various things that may trigger events.
  1198. */
  1199. static __init void event_test_stuff(void)
  1200. {
  1201. struct task_struct *test_thread;
  1202. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  1203. msleep(1);
  1204. kthread_stop(test_thread);
  1205. }
  1206. /*
  1207. * For every trace event defined, we will test each trace point separately,
  1208. * and then by groups, and finally all trace points.
  1209. */
  1210. static __init void event_trace_self_tests(void)
  1211. {
  1212. struct ftrace_event_call *call;
  1213. struct event_subsystem *system;
  1214. int ret;
  1215. pr_info("Running tests on trace events:\n");
  1216. list_for_each_entry(call, &ftrace_events, list) {
  1217. /* Only test those that have a probe */
  1218. if (!call->class || !call->class->probe)
  1219. continue;
  1220. /*
  1221. * Testing syscall events here is pretty useless, but
  1222. * we still do it if configured. But this is time consuming.
  1223. * What we really need is a user thread to perform the
  1224. * syscalls as we test.
  1225. */
  1226. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  1227. if (call->class->system &&
  1228. strcmp(call->class->system, "syscalls") == 0)
  1229. continue;
  1230. #endif
  1231. pr_info("Testing event %s: ", call->name);
  1232. /*
  1233. * If an event is already enabled, someone is using
  1234. * it and the self test should not be on.
  1235. */
  1236. if (call->flags & TRACE_EVENT_FL_ENABLED) {
  1237. pr_warning("Enabled event during self test!\n");
  1238. WARN_ON_ONCE(1);
  1239. continue;
  1240. }
  1241. ftrace_event_enable_disable(call, 1);
  1242. event_test_stuff();
  1243. ftrace_event_enable_disable(call, 0);
  1244. pr_cont("OK\n");
  1245. }
  1246. /* Now test at the sub system level */
  1247. pr_info("Running tests on trace event systems:\n");
  1248. list_for_each_entry(system, &event_subsystems, list) {
  1249. /* the ftrace system is special, skip it */
  1250. if (strcmp(system->name, "ftrace") == 0)
  1251. continue;
  1252. pr_info("Testing event system %s: ", system->name);
  1253. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
  1254. if (WARN_ON_ONCE(ret)) {
  1255. pr_warning("error enabling system %s\n",
  1256. system->name);
  1257. continue;
  1258. }
  1259. event_test_stuff();
  1260. ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
  1261. if (WARN_ON_ONCE(ret))
  1262. pr_warning("error disabling system %s\n",
  1263. system->name);
  1264. pr_cont("OK\n");
  1265. }
  1266. /* Test with all events enabled */
  1267. pr_info("Running tests on all trace events:\n");
  1268. pr_info("Testing all events: ");
  1269. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
  1270. if (WARN_ON_ONCE(ret)) {
  1271. pr_warning("error enabling all events\n");
  1272. return;
  1273. }
  1274. event_test_stuff();
  1275. /* reset sysname */
  1276. ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
  1277. if (WARN_ON_ONCE(ret)) {
  1278. pr_warning("error disabling all events\n");
  1279. return;
  1280. }
  1281. pr_cont("OK\n");
  1282. }
  1283. #ifdef CONFIG_FUNCTION_TRACER
  1284. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  1285. static void
  1286. function_test_events_call(unsigned long ip, unsigned long parent_ip)
  1287. {
  1288. struct ring_buffer_event *event;
  1289. struct ring_buffer *buffer;
  1290. struct ftrace_entry *entry;
  1291. unsigned long flags;
  1292. long disabled;
  1293. int cpu;
  1294. int pc;
  1295. pc = preempt_count();
  1296. preempt_disable_notrace();
  1297. cpu = raw_smp_processor_id();
  1298. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  1299. if (disabled != 1)
  1300. goto out;
  1301. local_save_flags(flags);
  1302. event = trace_current_buffer_lock_reserve(&buffer,
  1303. TRACE_FN, sizeof(*entry),
  1304. flags, pc);
  1305. if (!event)
  1306. goto out;
  1307. entry = ring_buffer_event_data(event);
  1308. entry->ip = ip;
  1309. entry->parent_ip = parent_ip;
  1310. trace_nowake_buffer_unlock_commit(buffer, event, flags, pc);
  1311. out:
  1312. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  1313. preempt_enable_notrace();
  1314. }
  1315. static struct ftrace_ops trace_ops __initdata =
  1316. {
  1317. .func = function_test_events_call,
  1318. };
  1319. static __init void event_trace_self_test_with_function(void)
  1320. {
  1321. register_ftrace_function(&trace_ops);
  1322. pr_info("Running tests again, along with the function tracer\n");
  1323. event_trace_self_tests();
  1324. unregister_ftrace_function(&trace_ops);
  1325. }
  1326. #else
  1327. static __init void event_trace_self_test_with_function(void)
  1328. {
  1329. }
  1330. #endif
  1331. static __init int event_trace_self_tests_init(void)
  1332. {
  1333. if (!tracing_selftest_disabled) {
  1334. event_trace_self_tests();
  1335. event_trace_self_test_with_function();
  1336. }
  1337. return 0;
  1338. }
  1339. late_initcall(event_trace_self_tests_init);
  1340. #endif