trace_events.c 37 KB

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