trace_events.c 36 KB

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