trace_events.c 34 KB

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