trace_events.c 32 KB

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