trace_events.c 36 KB

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