trace_events.c 38 KB

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