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