trace_events.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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. #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
  31. static struct kmem_cache *field_cachep;
  32. static struct kmem_cache *file_cachep;
  33. /* Double loops, do not use break, only goto's work */
  34. #define do_for_each_event_file(tr, file) \
  35. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  36. list_for_each_entry(file, &tr->events, list)
  37. #define do_for_each_event_file_safe(tr, file) \
  38. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  39. struct ftrace_event_file *___n; \
  40. list_for_each_entry_safe(file, ___n, &tr->events, list)
  41. #define while_for_each_event_file() \
  42. }
  43. struct list_head *
  44. trace_get_fields(struct ftrace_event_call *event_call)
  45. {
  46. if (!event_call->class->get_fields)
  47. return &event_call->class->fields;
  48. return event_call->class->get_fields(event_call);
  49. }
  50. static int __trace_define_field(struct list_head *head, const char *type,
  51. const char *name, int offset, int size,
  52. int is_signed, int filter_type)
  53. {
  54. struct ftrace_event_field *field;
  55. field = kmem_cache_alloc(field_cachep, GFP_TRACE);
  56. if (!field)
  57. goto err;
  58. field->name = name;
  59. field->type = type;
  60. if (filter_type == FILTER_OTHER)
  61. field->filter_type = filter_assign_type(type);
  62. else
  63. field->filter_type = filter_type;
  64. field->offset = offset;
  65. field->size = size;
  66. field->is_signed = is_signed;
  67. list_add(&field->link, head);
  68. return 0;
  69. err:
  70. kmem_cache_free(field_cachep, field);
  71. return -ENOMEM;
  72. }
  73. int trace_define_field(struct ftrace_event_call *call, const char *type,
  74. const char *name, int offset, int size, int is_signed,
  75. int filter_type)
  76. {
  77. struct list_head *head;
  78. if (WARN_ON(!call->class))
  79. return 0;
  80. head = trace_get_fields(call);
  81. return __trace_define_field(head, type, name, offset, size,
  82. is_signed, filter_type);
  83. }
  84. EXPORT_SYMBOL_GPL(trace_define_field);
  85. #define __common_field(type, item) \
  86. ret = __trace_define_field(&ftrace_common_fields, #type, \
  87. "common_" #item, \
  88. offsetof(typeof(ent), item), \
  89. sizeof(ent.item), \
  90. is_signed_type(type), FILTER_OTHER); \
  91. if (ret) \
  92. return ret;
  93. static int trace_define_common_fields(void)
  94. {
  95. int ret;
  96. struct trace_entry ent;
  97. __common_field(unsigned short, type);
  98. __common_field(unsigned char, flags);
  99. __common_field(unsigned char, preempt_count);
  100. __common_field(int, pid);
  101. return ret;
  102. }
  103. void trace_destroy_fields(struct ftrace_event_call *call)
  104. {
  105. struct ftrace_event_field *field, *next;
  106. struct list_head *head;
  107. head = trace_get_fields(call);
  108. list_for_each_entry_safe(field, next, head, link) {
  109. list_del(&field->link);
  110. kmem_cache_free(field_cachep, field);
  111. }
  112. }
  113. int trace_event_raw_init(struct ftrace_event_call *call)
  114. {
  115. int id;
  116. id = register_ftrace_event(&call->event);
  117. if (!id)
  118. return -ENODEV;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL_GPL(trace_event_raw_init);
  122. int ftrace_event_reg(struct ftrace_event_call *call,
  123. enum trace_reg type, void *data)
  124. {
  125. struct ftrace_event_file *file = data;
  126. switch (type) {
  127. case TRACE_REG_REGISTER:
  128. return tracepoint_probe_register(call->name,
  129. call->class->probe,
  130. file);
  131. case TRACE_REG_UNREGISTER:
  132. tracepoint_probe_unregister(call->name,
  133. call->class->probe,
  134. file);
  135. return 0;
  136. #ifdef CONFIG_PERF_EVENTS
  137. case TRACE_REG_PERF_REGISTER:
  138. return tracepoint_probe_register(call->name,
  139. call->class->perf_probe,
  140. call);
  141. case TRACE_REG_PERF_UNREGISTER:
  142. tracepoint_probe_unregister(call->name,
  143. call->class->perf_probe,
  144. call);
  145. return 0;
  146. case TRACE_REG_PERF_OPEN:
  147. case TRACE_REG_PERF_CLOSE:
  148. case TRACE_REG_PERF_ADD:
  149. case TRACE_REG_PERF_DEL:
  150. return 0;
  151. #endif
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(ftrace_event_reg);
  156. void trace_event_enable_cmd_record(bool enable)
  157. {
  158. struct ftrace_event_file *file;
  159. struct trace_array *tr;
  160. mutex_lock(&event_mutex);
  161. do_for_each_event_file(tr, file) {
  162. if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
  163. continue;
  164. if (enable) {
  165. tracing_start_cmdline_record();
  166. file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
  167. } else {
  168. tracing_stop_cmdline_record();
  169. file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
  170. }
  171. } while_for_each_event_file();
  172. mutex_unlock(&event_mutex);
  173. }
  174. static int ftrace_event_enable_disable(struct ftrace_event_file *file,
  175. int enable)
  176. {
  177. struct ftrace_event_call *call = file->event_call;
  178. int ret = 0;
  179. switch (enable) {
  180. case 0:
  181. if (file->flags & FTRACE_EVENT_FL_ENABLED) {
  182. file->flags &= ~FTRACE_EVENT_FL_ENABLED;
  183. if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
  184. tracing_stop_cmdline_record();
  185. file->flags &= ~FTRACE_EVENT_FL_RECORDED_CMD;
  186. }
  187. call->class->reg(call, TRACE_REG_UNREGISTER, file);
  188. }
  189. break;
  190. case 1:
  191. if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
  192. if (trace_flags & TRACE_ITER_RECORD_CMD) {
  193. tracing_start_cmdline_record();
  194. file->flags |= FTRACE_EVENT_FL_RECORDED_CMD;
  195. }
  196. ret = call->class->reg(call, TRACE_REG_REGISTER, file);
  197. if (ret) {
  198. tracing_stop_cmdline_record();
  199. pr_info("event trace: Could not enable event "
  200. "%s\n", call->name);
  201. break;
  202. }
  203. file->flags |= FTRACE_EVENT_FL_ENABLED;
  204. /* WAS_ENABLED gets set but never cleared. */
  205. call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
  206. }
  207. break;
  208. }
  209. return ret;
  210. }
  211. static void ftrace_clear_events(struct trace_array *tr)
  212. {
  213. struct ftrace_event_file *file;
  214. mutex_lock(&event_mutex);
  215. list_for_each_entry(file, &tr->events, list) {
  216. ftrace_event_enable_disable(file, 0);
  217. }
  218. mutex_unlock(&event_mutex);
  219. }
  220. static void __put_system(struct event_subsystem *system)
  221. {
  222. struct event_filter *filter = system->filter;
  223. WARN_ON_ONCE(system->ref_count == 0);
  224. if (--system->ref_count)
  225. return;
  226. list_del(&system->list);
  227. if (filter) {
  228. kfree(filter->filter_string);
  229. kfree(filter);
  230. }
  231. kfree(system);
  232. }
  233. static void __get_system(struct event_subsystem *system)
  234. {
  235. WARN_ON_ONCE(system->ref_count == 0);
  236. system->ref_count++;
  237. }
  238. static void __get_system_dir(struct ftrace_subsystem_dir *dir)
  239. {
  240. WARN_ON_ONCE(dir->ref_count == 0);
  241. dir->ref_count++;
  242. __get_system(dir->subsystem);
  243. }
  244. static void __put_system_dir(struct ftrace_subsystem_dir *dir)
  245. {
  246. WARN_ON_ONCE(dir->ref_count == 0);
  247. /* If the subsystem is about to be freed, the dir must be too */
  248. WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1);
  249. __put_system(dir->subsystem);
  250. if (!--dir->ref_count)
  251. kfree(dir);
  252. }
  253. static void put_system(struct ftrace_subsystem_dir *dir)
  254. {
  255. mutex_lock(&event_mutex);
  256. __put_system_dir(dir);
  257. mutex_unlock(&event_mutex);
  258. }
  259. /*
  260. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  261. */
  262. static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
  263. const char *sub, const char *event, int set)
  264. {
  265. struct ftrace_event_file *file;
  266. struct ftrace_event_call *call;
  267. int ret = -EINVAL;
  268. mutex_lock(&event_mutex);
  269. list_for_each_entry(file, &tr->events, list) {
  270. call = file->event_call;
  271. if (!call->name || !call->class || !call->class->reg)
  272. continue;
  273. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  274. continue;
  275. if (match &&
  276. strcmp(match, call->name) != 0 &&
  277. strcmp(match, call->class->system) != 0)
  278. continue;
  279. if (sub && strcmp(sub, call->class->system) != 0)
  280. continue;
  281. if (event && strcmp(event, call->name) != 0)
  282. continue;
  283. ftrace_event_enable_disable(file, set);
  284. ret = 0;
  285. }
  286. mutex_unlock(&event_mutex);
  287. return ret;
  288. }
  289. static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
  290. {
  291. char *event = NULL, *sub = NULL, *match;
  292. /*
  293. * The buf format can be <subsystem>:<event-name>
  294. * *:<event-name> means any event by that name.
  295. * :<event-name> is the same.
  296. *
  297. * <subsystem>:* means all events in that subsystem
  298. * <subsystem>: means the same.
  299. *
  300. * <name> (no ':') means all events in a subsystem with
  301. * the name <name> or any event that matches <name>
  302. */
  303. match = strsep(&buf, ":");
  304. if (buf) {
  305. sub = match;
  306. event = buf;
  307. match = NULL;
  308. if (!strlen(sub) || strcmp(sub, "*") == 0)
  309. sub = NULL;
  310. if (!strlen(event) || strcmp(event, "*") == 0)
  311. event = NULL;
  312. }
  313. return __ftrace_set_clr_event(tr, match, sub, event, set);
  314. }
  315. /**
  316. * trace_set_clr_event - enable or disable an event
  317. * @system: system name to match (NULL for any system)
  318. * @event: event name to match (NULL for all events, within system)
  319. * @set: 1 to enable, 0 to disable
  320. *
  321. * This is a way for other parts of the kernel to enable or disable
  322. * event recording.
  323. *
  324. * Returns 0 on success, -EINVAL if the parameters do not match any
  325. * registered events.
  326. */
  327. int trace_set_clr_event(const char *system, const char *event, int set)
  328. {
  329. struct trace_array *tr = top_trace_array();
  330. return __ftrace_set_clr_event(tr, NULL, system, event, set);
  331. }
  332. EXPORT_SYMBOL_GPL(trace_set_clr_event);
  333. /* 128 should be much more than enough */
  334. #define EVENT_BUF_SIZE 127
  335. static ssize_t
  336. ftrace_event_write(struct file *file, const char __user *ubuf,
  337. size_t cnt, loff_t *ppos)
  338. {
  339. struct trace_parser parser;
  340. struct seq_file *m = file->private_data;
  341. struct trace_array *tr = m->private;
  342. ssize_t read, ret;
  343. if (!cnt)
  344. return 0;
  345. ret = tracing_update_buffers();
  346. if (ret < 0)
  347. return ret;
  348. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  349. return -ENOMEM;
  350. read = trace_get_user(&parser, ubuf, cnt, ppos);
  351. if (read >= 0 && trace_parser_loaded((&parser))) {
  352. int set = 1;
  353. if (*parser.buffer == '!')
  354. set = 0;
  355. parser.buffer[parser.idx] = 0;
  356. ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
  357. if (ret)
  358. goto out_put;
  359. }
  360. ret = read;
  361. out_put:
  362. trace_parser_put(&parser);
  363. return ret;
  364. }
  365. static void *
  366. t_next(struct seq_file *m, void *v, loff_t *pos)
  367. {
  368. struct ftrace_event_file *file = v;
  369. struct ftrace_event_call *call;
  370. struct trace_array *tr = m->private;
  371. (*pos)++;
  372. list_for_each_entry_continue(file, &tr->events, list) {
  373. call = file->event_call;
  374. /*
  375. * The ftrace subsystem is for showing formats only.
  376. * They can not be enabled or disabled via the event files.
  377. */
  378. if (call->class && call->class->reg)
  379. return file;
  380. }
  381. return NULL;
  382. }
  383. static void *t_start(struct seq_file *m, loff_t *pos)
  384. {
  385. struct ftrace_event_file *file;
  386. struct trace_array *tr = m->private;
  387. loff_t l;
  388. mutex_lock(&event_mutex);
  389. file = list_entry(&tr->events, struct ftrace_event_file, list);
  390. for (l = 0; l <= *pos; ) {
  391. file = t_next(m, file, &l);
  392. if (!file)
  393. break;
  394. }
  395. return file;
  396. }
  397. static void *
  398. s_next(struct seq_file *m, void *v, loff_t *pos)
  399. {
  400. struct ftrace_event_file *file = v;
  401. struct trace_array *tr = m->private;
  402. (*pos)++;
  403. list_for_each_entry_continue(file, &tr->events, list) {
  404. if (file->flags & FTRACE_EVENT_FL_ENABLED)
  405. return file;
  406. }
  407. return NULL;
  408. }
  409. static void *s_start(struct seq_file *m, loff_t *pos)
  410. {
  411. struct ftrace_event_file *file;
  412. struct trace_array *tr = m->private;
  413. loff_t l;
  414. mutex_lock(&event_mutex);
  415. file = list_entry(&tr->events, struct ftrace_event_file, list);
  416. for (l = 0; l <= *pos; ) {
  417. file = s_next(m, file, &l);
  418. if (!file)
  419. break;
  420. }
  421. return file;
  422. }
  423. static int t_show(struct seq_file *m, void *v)
  424. {
  425. struct ftrace_event_file *file = v;
  426. struct ftrace_event_call *call = file->event_call;
  427. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  428. seq_printf(m, "%s:", call->class->system);
  429. seq_printf(m, "%s\n", call->name);
  430. return 0;
  431. }
  432. static void t_stop(struct seq_file *m, void *p)
  433. {
  434. mutex_unlock(&event_mutex);
  435. }
  436. static ssize_t
  437. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  438. loff_t *ppos)
  439. {
  440. struct ftrace_event_file *file = filp->private_data;
  441. char *buf;
  442. if (file->flags & FTRACE_EVENT_FL_ENABLED)
  443. buf = "1\n";
  444. else
  445. buf = "0\n";
  446. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  447. }
  448. static ssize_t
  449. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  450. loff_t *ppos)
  451. {
  452. struct ftrace_event_file *file = filp->private_data;
  453. unsigned long val;
  454. int ret;
  455. if (!file)
  456. return -EINVAL;
  457. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  458. if (ret)
  459. return ret;
  460. ret = tracing_update_buffers();
  461. if (ret < 0)
  462. return ret;
  463. switch (val) {
  464. case 0:
  465. case 1:
  466. mutex_lock(&event_mutex);
  467. ret = ftrace_event_enable_disable(file, val);
  468. mutex_unlock(&event_mutex);
  469. break;
  470. default:
  471. return -EINVAL;
  472. }
  473. *ppos += cnt;
  474. return ret ? ret : cnt;
  475. }
  476. static ssize_t
  477. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  478. loff_t *ppos)
  479. {
  480. const char set_to_char[4] = { '?', '0', '1', 'X' };
  481. struct ftrace_subsystem_dir *dir = filp->private_data;
  482. struct event_subsystem *system = dir->subsystem;
  483. struct ftrace_event_call *call;
  484. struct ftrace_event_file *file;
  485. struct trace_array *tr = dir->tr;
  486. char buf[2];
  487. int set = 0;
  488. int ret;
  489. mutex_lock(&event_mutex);
  490. list_for_each_entry(file, &tr->events, list) {
  491. call = file->event_call;
  492. if (!call->name || !call->class || !call->class->reg)
  493. continue;
  494. if (system && strcmp(call->class->system, system->name) != 0)
  495. continue;
  496. /*
  497. * We need to find out if all the events are set
  498. * or if all events or cleared, or if we have
  499. * a mixture.
  500. */
  501. set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
  502. /*
  503. * If we have a mixture, no need to look further.
  504. */
  505. if (set == 3)
  506. break;
  507. }
  508. mutex_unlock(&event_mutex);
  509. buf[0] = set_to_char[set];
  510. buf[1] = '\n';
  511. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  512. return ret;
  513. }
  514. static ssize_t
  515. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  516. loff_t *ppos)
  517. {
  518. struct ftrace_subsystem_dir *dir = filp->private_data;
  519. struct event_subsystem *system = dir->subsystem;
  520. const char *name = NULL;
  521. unsigned long val;
  522. ssize_t ret;
  523. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  524. if (ret)
  525. return ret;
  526. ret = tracing_update_buffers();
  527. if (ret < 0)
  528. return ret;
  529. if (val != 0 && val != 1)
  530. return -EINVAL;
  531. /*
  532. * Opening of "enable" adds a ref count to system,
  533. * so the name is safe to use.
  534. */
  535. if (system)
  536. name = system->name;
  537. ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
  538. if (ret)
  539. goto out;
  540. ret = cnt;
  541. out:
  542. *ppos += cnt;
  543. return ret;
  544. }
  545. enum {
  546. FORMAT_HEADER = 1,
  547. FORMAT_FIELD_SEPERATOR = 2,
  548. FORMAT_PRINTFMT = 3,
  549. };
  550. static void *f_next(struct seq_file *m, void *v, loff_t *pos)
  551. {
  552. struct ftrace_event_call *call = m->private;
  553. struct ftrace_event_field *field;
  554. struct list_head *common_head = &ftrace_common_fields;
  555. struct list_head *head = trace_get_fields(call);
  556. (*pos)++;
  557. switch ((unsigned long)v) {
  558. case FORMAT_HEADER:
  559. if (unlikely(list_empty(common_head)))
  560. return NULL;
  561. field = list_entry(common_head->prev,
  562. struct ftrace_event_field, link);
  563. return field;
  564. case FORMAT_FIELD_SEPERATOR:
  565. if (unlikely(list_empty(head)))
  566. return NULL;
  567. field = list_entry(head->prev, struct ftrace_event_field, link);
  568. return field;
  569. case FORMAT_PRINTFMT:
  570. /* all done */
  571. return NULL;
  572. }
  573. field = v;
  574. if (field->link.prev == common_head)
  575. return (void *)FORMAT_FIELD_SEPERATOR;
  576. else if (field->link.prev == head)
  577. return (void *)FORMAT_PRINTFMT;
  578. field = list_entry(field->link.prev, struct ftrace_event_field, link);
  579. return field;
  580. }
  581. static void *f_start(struct seq_file *m, loff_t *pos)
  582. {
  583. loff_t l = 0;
  584. void *p;
  585. /* Start by showing the header */
  586. if (!*pos)
  587. return (void *)FORMAT_HEADER;
  588. p = (void *)FORMAT_HEADER;
  589. do {
  590. p = f_next(m, p, &l);
  591. } while (p && l < *pos);
  592. return p;
  593. }
  594. static int f_show(struct seq_file *m, void *v)
  595. {
  596. struct ftrace_event_call *call = m->private;
  597. struct ftrace_event_field *field;
  598. const char *array_descriptor;
  599. switch ((unsigned long)v) {
  600. case FORMAT_HEADER:
  601. seq_printf(m, "name: %s\n", call->name);
  602. seq_printf(m, "ID: %d\n", call->event.type);
  603. seq_printf(m, "format:\n");
  604. return 0;
  605. case FORMAT_FIELD_SEPERATOR:
  606. seq_putc(m, '\n');
  607. return 0;
  608. case FORMAT_PRINTFMT:
  609. seq_printf(m, "\nprint fmt: %s\n",
  610. call->print_fmt);
  611. return 0;
  612. }
  613. field = v;
  614. /*
  615. * Smartly shows the array type(except dynamic array).
  616. * Normal:
  617. * field:TYPE VAR
  618. * If TYPE := TYPE[LEN], it is shown:
  619. * field:TYPE VAR[LEN]
  620. */
  621. array_descriptor = strchr(field->type, '[');
  622. if (!strncmp(field->type, "__data_loc", 10))
  623. array_descriptor = NULL;
  624. if (!array_descriptor)
  625. seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  626. field->type, field->name, field->offset,
  627. field->size, !!field->is_signed);
  628. else
  629. seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  630. (int)(array_descriptor - field->type),
  631. field->type, field->name,
  632. array_descriptor, field->offset,
  633. field->size, !!field->is_signed);
  634. return 0;
  635. }
  636. static void f_stop(struct seq_file *m, void *p)
  637. {
  638. }
  639. static const struct seq_operations trace_format_seq_ops = {
  640. .start = f_start,
  641. .next = f_next,
  642. .stop = f_stop,
  643. .show = f_show,
  644. };
  645. static int trace_format_open(struct inode *inode, struct file *file)
  646. {
  647. struct ftrace_event_call *call = inode->i_private;
  648. struct seq_file *m;
  649. int ret;
  650. ret = seq_open(file, &trace_format_seq_ops);
  651. if (ret < 0)
  652. return ret;
  653. m = file->private_data;
  654. m->private = call;
  655. return 0;
  656. }
  657. static ssize_t
  658. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  659. {
  660. struct ftrace_event_call *call = filp->private_data;
  661. struct trace_seq *s;
  662. int r;
  663. if (*ppos)
  664. return 0;
  665. s = kmalloc(sizeof(*s), GFP_KERNEL);
  666. if (!s)
  667. return -ENOMEM;
  668. trace_seq_init(s);
  669. trace_seq_printf(s, "%d\n", call->event.type);
  670. r = simple_read_from_buffer(ubuf, cnt, ppos,
  671. s->buffer, s->len);
  672. kfree(s);
  673. return r;
  674. }
  675. static ssize_t
  676. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  677. loff_t *ppos)
  678. {
  679. struct ftrace_event_call *call = filp->private_data;
  680. struct trace_seq *s;
  681. int r;
  682. if (*ppos)
  683. return 0;
  684. s = kmalloc(sizeof(*s), GFP_KERNEL);
  685. if (!s)
  686. return -ENOMEM;
  687. trace_seq_init(s);
  688. print_event_filter(call, s);
  689. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  690. kfree(s);
  691. return r;
  692. }
  693. static ssize_t
  694. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  695. loff_t *ppos)
  696. {
  697. struct ftrace_event_call *call = filp->private_data;
  698. char *buf;
  699. int err;
  700. if (cnt >= PAGE_SIZE)
  701. return -EINVAL;
  702. buf = (char *)__get_free_page(GFP_TEMPORARY);
  703. if (!buf)
  704. return -ENOMEM;
  705. if (copy_from_user(buf, ubuf, cnt)) {
  706. free_page((unsigned long) buf);
  707. return -EFAULT;
  708. }
  709. buf[cnt] = '\0';
  710. err = apply_event_filter(call, buf);
  711. free_page((unsigned long) buf);
  712. if (err < 0)
  713. return err;
  714. *ppos += cnt;
  715. return cnt;
  716. }
  717. static LIST_HEAD(event_subsystems);
  718. static int subsystem_open(struct inode *inode, struct file *filp)
  719. {
  720. struct event_subsystem *system = NULL;
  721. struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
  722. struct trace_array *tr;
  723. int ret;
  724. /* Make sure the system still exists */
  725. mutex_lock(&event_mutex);
  726. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  727. list_for_each_entry(dir, &tr->systems, list) {
  728. if (dir == inode->i_private) {
  729. /* Don't open systems with no events */
  730. if (dir->nr_events) {
  731. __get_system_dir(dir);
  732. system = dir->subsystem;
  733. }
  734. goto exit_loop;
  735. }
  736. }
  737. }
  738. exit_loop:
  739. mutex_unlock(&event_mutex);
  740. if (!system)
  741. return -ENODEV;
  742. /* Some versions of gcc think dir can be uninitialized here */
  743. WARN_ON(!dir);
  744. ret = tracing_open_generic(inode, filp);
  745. if (ret < 0)
  746. put_system(dir);
  747. return ret;
  748. }
  749. static int system_tr_open(struct inode *inode, struct file *filp)
  750. {
  751. struct ftrace_subsystem_dir *dir;
  752. struct trace_array *tr = inode->i_private;
  753. int ret;
  754. /* Make a temporary dir that has no system but points to tr */
  755. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  756. if (!dir)
  757. return -ENOMEM;
  758. dir->tr = tr;
  759. ret = tracing_open_generic(inode, filp);
  760. if (ret < 0)
  761. kfree(dir);
  762. filp->private_data = dir;
  763. return ret;
  764. }
  765. static int subsystem_release(struct inode *inode, struct file *file)
  766. {
  767. struct ftrace_subsystem_dir *dir = file->private_data;
  768. /*
  769. * If dir->subsystem is NULL, then this is a temporary
  770. * descriptor that was made for a trace_array to enable
  771. * all subsystems.
  772. */
  773. if (dir->subsystem)
  774. put_system(dir);
  775. else
  776. kfree(dir);
  777. return 0;
  778. }
  779. static ssize_t
  780. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  781. loff_t *ppos)
  782. {
  783. struct ftrace_subsystem_dir *dir = filp->private_data;
  784. struct event_subsystem *system = dir->subsystem;
  785. struct trace_seq *s;
  786. int r;
  787. if (*ppos)
  788. return 0;
  789. s = kmalloc(sizeof(*s), GFP_KERNEL);
  790. if (!s)
  791. return -ENOMEM;
  792. trace_seq_init(s);
  793. print_subsystem_event_filter(system, s);
  794. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  795. kfree(s);
  796. return r;
  797. }
  798. static ssize_t
  799. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  800. loff_t *ppos)
  801. {
  802. struct ftrace_subsystem_dir *dir = filp->private_data;
  803. char *buf;
  804. int err;
  805. if (cnt >= PAGE_SIZE)
  806. return -EINVAL;
  807. buf = (char *)__get_free_page(GFP_TEMPORARY);
  808. if (!buf)
  809. return -ENOMEM;
  810. if (copy_from_user(buf, ubuf, cnt)) {
  811. free_page((unsigned long) buf);
  812. return -EFAULT;
  813. }
  814. buf[cnt] = '\0';
  815. err = apply_subsystem_event_filter(dir, buf);
  816. free_page((unsigned long) buf);
  817. if (err < 0)
  818. return err;
  819. *ppos += cnt;
  820. return cnt;
  821. }
  822. static ssize_t
  823. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  824. {
  825. int (*func)(struct trace_seq *s) = filp->private_data;
  826. struct trace_seq *s;
  827. int r;
  828. if (*ppos)
  829. return 0;
  830. s = kmalloc(sizeof(*s), GFP_KERNEL);
  831. if (!s)
  832. return -ENOMEM;
  833. trace_seq_init(s);
  834. func(s);
  835. r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
  836. kfree(s);
  837. return r;
  838. }
  839. static int ftrace_event_avail_open(struct inode *inode, struct file *file);
  840. static int ftrace_event_set_open(struct inode *inode, struct file *file);
  841. static const struct seq_operations show_event_seq_ops = {
  842. .start = t_start,
  843. .next = t_next,
  844. .show = t_show,
  845. .stop = t_stop,
  846. };
  847. static const struct seq_operations show_set_event_seq_ops = {
  848. .start = s_start,
  849. .next = s_next,
  850. .show = t_show,
  851. .stop = t_stop,
  852. };
  853. static const struct file_operations ftrace_avail_fops = {
  854. .open = ftrace_event_avail_open,
  855. .read = seq_read,
  856. .llseek = seq_lseek,
  857. .release = seq_release,
  858. };
  859. static const struct file_operations ftrace_set_event_fops = {
  860. .open = ftrace_event_set_open,
  861. .read = seq_read,
  862. .write = ftrace_event_write,
  863. .llseek = seq_lseek,
  864. .release = seq_release,
  865. };
  866. static const struct file_operations ftrace_enable_fops = {
  867. .open = tracing_open_generic,
  868. .read = event_enable_read,
  869. .write = event_enable_write,
  870. .llseek = default_llseek,
  871. };
  872. static const struct file_operations ftrace_event_format_fops = {
  873. .open = trace_format_open,
  874. .read = seq_read,
  875. .llseek = seq_lseek,
  876. .release = seq_release,
  877. };
  878. static const struct file_operations ftrace_event_id_fops = {
  879. .open = tracing_open_generic,
  880. .read = event_id_read,
  881. .llseek = default_llseek,
  882. };
  883. static const struct file_operations ftrace_event_filter_fops = {
  884. .open = tracing_open_generic,
  885. .read = event_filter_read,
  886. .write = event_filter_write,
  887. .llseek = default_llseek,
  888. };
  889. static const struct file_operations ftrace_subsystem_filter_fops = {
  890. .open = subsystem_open,
  891. .read = subsystem_filter_read,
  892. .write = subsystem_filter_write,
  893. .llseek = default_llseek,
  894. .release = subsystem_release,
  895. };
  896. static const struct file_operations ftrace_system_enable_fops = {
  897. .open = subsystem_open,
  898. .read = system_enable_read,
  899. .write = system_enable_write,
  900. .llseek = default_llseek,
  901. .release = subsystem_release,
  902. };
  903. static const struct file_operations ftrace_tr_enable_fops = {
  904. .open = system_tr_open,
  905. .read = system_enable_read,
  906. .write = system_enable_write,
  907. .llseek = default_llseek,
  908. .release = subsystem_release,
  909. };
  910. static const struct file_operations ftrace_show_header_fops = {
  911. .open = tracing_open_generic,
  912. .read = show_header,
  913. .llseek = default_llseek,
  914. };
  915. static int
  916. ftrace_event_open(struct inode *inode, struct file *file,
  917. const struct seq_operations *seq_ops)
  918. {
  919. struct seq_file *m;
  920. int ret;
  921. ret = seq_open(file, seq_ops);
  922. if (ret < 0)
  923. return ret;
  924. m = file->private_data;
  925. /* copy tr over to seq ops */
  926. m->private = inode->i_private;
  927. return ret;
  928. }
  929. static int
  930. ftrace_event_avail_open(struct inode *inode, struct file *file)
  931. {
  932. const struct seq_operations *seq_ops = &show_event_seq_ops;
  933. return ftrace_event_open(inode, file, seq_ops);
  934. }
  935. static int
  936. ftrace_event_set_open(struct inode *inode, struct file *file)
  937. {
  938. const struct seq_operations *seq_ops = &show_set_event_seq_ops;
  939. struct trace_array *tr = inode->i_private;
  940. if ((file->f_mode & FMODE_WRITE) &&
  941. (file->f_flags & O_TRUNC))
  942. ftrace_clear_events(tr);
  943. return ftrace_event_open(inode, file, seq_ops);
  944. }
  945. static struct event_subsystem *
  946. create_new_subsystem(const char *name)
  947. {
  948. struct event_subsystem *system;
  949. /* need to create new entry */
  950. system = kmalloc(sizeof(*system), GFP_KERNEL);
  951. if (!system)
  952. return NULL;
  953. system->ref_count = 1;
  954. system->name = name;
  955. system->filter = NULL;
  956. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  957. if (!system->filter)
  958. goto out_free;
  959. list_add(&system->list, &event_subsystems);
  960. return system;
  961. out_free:
  962. kfree(system);
  963. return NULL;
  964. }
  965. static struct dentry *
  966. event_subsystem_dir(struct trace_array *tr, const char *name,
  967. struct ftrace_event_file *file, struct dentry *parent)
  968. {
  969. struct ftrace_subsystem_dir *dir;
  970. struct event_subsystem *system;
  971. struct dentry *entry;
  972. /* First see if we did not already create this dir */
  973. list_for_each_entry(dir, &tr->systems, list) {
  974. system = dir->subsystem;
  975. if (strcmp(system->name, name) == 0) {
  976. dir->nr_events++;
  977. file->system = dir;
  978. return dir->entry;
  979. }
  980. }
  981. /* Now see if the system itself exists. */
  982. list_for_each_entry(system, &event_subsystems, list) {
  983. if (strcmp(system->name, name) == 0)
  984. break;
  985. }
  986. /* Reset system variable when not found */
  987. if (&system->list == &event_subsystems)
  988. system = NULL;
  989. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  990. if (!dir)
  991. goto out_fail;
  992. if (!system) {
  993. system = create_new_subsystem(name);
  994. if (!system)
  995. goto out_free;
  996. } else
  997. __get_system(system);
  998. dir->entry = debugfs_create_dir(name, parent);
  999. if (!dir->entry) {
  1000. pr_warning("Failed to create system directory %s\n", name);
  1001. __put_system(system);
  1002. goto out_free;
  1003. }
  1004. dir->tr = tr;
  1005. dir->ref_count = 1;
  1006. dir->nr_events = 1;
  1007. dir->subsystem = system;
  1008. file->system = dir;
  1009. entry = debugfs_create_file("filter", 0644, dir->entry, dir,
  1010. &ftrace_subsystem_filter_fops);
  1011. if (!entry) {
  1012. kfree(system->filter);
  1013. system->filter = NULL;
  1014. pr_warning("Could not create debugfs '%s/filter' entry\n", name);
  1015. }
  1016. trace_create_file("enable", 0644, dir->entry, dir,
  1017. &ftrace_system_enable_fops);
  1018. list_add(&dir->list, &tr->systems);
  1019. return dir->entry;
  1020. out_free:
  1021. kfree(dir);
  1022. out_fail:
  1023. /* Only print this message if failed on memory allocation */
  1024. if (!dir || !system)
  1025. pr_warning("No memory to create event subsystem %s\n",
  1026. name);
  1027. return NULL;
  1028. }
  1029. static int
  1030. event_create_dir(struct dentry *parent,
  1031. struct ftrace_event_file *file,
  1032. const struct file_operations *id,
  1033. const struct file_operations *enable,
  1034. const struct file_operations *filter,
  1035. const struct file_operations *format)
  1036. {
  1037. struct ftrace_event_call *call = file->event_call;
  1038. struct trace_array *tr = file->tr;
  1039. struct list_head *head;
  1040. struct dentry *d_events;
  1041. int ret;
  1042. /*
  1043. * If the trace point header did not define TRACE_SYSTEM
  1044. * then the system would be called "TRACE_SYSTEM".
  1045. */
  1046. if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
  1047. d_events = event_subsystem_dir(tr, call->class->system, file, parent);
  1048. if (!d_events)
  1049. return -ENOMEM;
  1050. } else
  1051. d_events = parent;
  1052. file->dir = debugfs_create_dir(call->name, d_events);
  1053. if (!file->dir) {
  1054. pr_warning("Could not create debugfs '%s' directory\n",
  1055. call->name);
  1056. return -1;
  1057. }
  1058. if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
  1059. trace_create_file("enable", 0644, file->dir, file,
  1060. enable);
  1061. #ifdef CONFIG_PERF_EVENTS
  1062. if (call->event.type && call->class->reg)
  1063. trace_create_file("id", 0444, file->dir, call,
  1064. id);
  1065. #endif
  1066. /*
  1067. * Other events may have the same class. Only update
  1068. * the fields if they are not already defined.
  1069. */
  1070. head = trace_get_fields(call);
  1071. if (list_empty(head)) {
  1072. ret = call->class->define_fields(call);
  1073. if (ret < 0) {
  1074. pr_warning("Could not initialize trace point"
  1075. " events/%s\n", call->name);
  1076. return -1;
  1077. }
  1078. }
  1079. trace_create_file("filter", 0644, file->dir, call,
  1080. filter);
  1081. trace_create_file("format", 0444, file->dir, call,
  1082. format);
  1083. return 0;
  1084. }
  1085. static void remove_subsystem(struct ftrace_subsystem_dir *dir)
  1086. {
  1087. if (!dir)
  1088. return;
  1089. if (!--dir->nr_events) {
  1090. debugfs_remove_recursive(dir->entry);
  1091. list_del(&dir->list);
  1092. __put_system_dir(dir);
  1093. }
  1094. }
  1095. static void remove_event_from_tracers(struct ftrace_event_call *call)
  1096. {
  1097. struct ftrace_event_file *file;
  1098. struct trace_array *tr;
  1099. do_for_each_event_file_safe(tr, file) {
  1100. if (file->event_call != call)
  1101. continue;
  1102. list_del(&file->list);
  1103. debugfs_remove_recursive(file->dir);
  1104. remove_subsystem(file->system);
  1105. kmem_cache_free(file_cachep, file);
  1106. /*
  1107. * The do_for_each_event_file_safe() is
  1108. * a double loop. After finding the call for this
  1109. * trace_array, we use break to jump to the next
  1110. * trace_array.
  1111. */
  1112. break;
  1113. } while_for_each_event_file();
  1114. }
  1115. static void event_remove(struct ftrace_event_call *call)
  1116. {
  1117. struct trace_array *tr;
  1118. struct ftrace_event_file *file;
  1119. do_for_each_event_file(tr, file) {
  1120. if (file->event_call != call)
  1121. continue;
  1122. ftrace_event_enable_disable(file, 0);
  1123. /*
  1124. * The do_for_each_event_file() is
  1125. * a double loop. After finding the call for this
  1126. * trace_array, we use break to jump to the next
  1127. * trace_array.
  1128. */
  1129. break;
  1130. } while_for_each_event_file();
  1131. if (call->event.funcs)
  1132. __unregister_ftrace_event(&call->event);
  1133. remove_event_from_tracers(call);
  1134. list_del(&call->list);
  1135. }
  1136. static int event_init(struct ftrace_event_call *call)
  1137. {
  1138. int ret = 0;
  1139. if (WARN_ON(!call->name))
  1140. return -EINVAL;
  1141. if (call->class->raw_init) {
  1142. ret = call->class->raw_init(call);
  1143. if (ret < 0 && ret != -ENOSYS)
  1144. pr_warn("Could not initialize trace events/%s\n",
  1145. call->name);
  1146. }
  1147. return ret;
  1148. }
  1149. static int
  1150. __register_event(struct ftrace_event_call *call, struct module *mod)
  1151. {
  1152. int ret;
  1153. ret = event_init(call);
  1154. if (ret < 0)
  1155. return ret;
  1156. list_add(&call->list, &ftrace_events);
  1157. call->mod = mod;
  1158. return 0;
  1159. }
  1160. /* Add an event to a trace directory */
  1161. static int
  1162. __trace_add_new_event(struct ftrace_event_call *call,
  1163. struct trace_array *tr,
  1164. const struct file_operations *id,
  1165. const struct file_operations *enable,
  1166. const struct file_operations *filter,
  1167. const struct file_operations *format)
  1168. {
  1169. struct ftrace_event_file *file;
  1170. file = kmem_cache_alloc(file_cachep, GFP_TRACE);
  1171. if (!file)
  1172. return -ENOMEM;
  1173. file->event_call = call;
  1174. file->tr = tr;
  1175. list_add(&file->list, &tr->events);
  1176. return event_create_dir(tr->event_dir, file, id, enable, filter, format);
  1177. }
  1178. /*
  1179. * Just create a decriptor for early init. A descriptor is required
  1180. * for enabling events at boot. We want to enable events before
  1181. * the filesystem is initialized.
  1182. */
  1183. static __init int
  1184. __trace_early_add_new_event(struct ftrace_event_call *call,
  1185. struct trace_array *tr)
  1186. {
  1187. struct ftrace_event_file *file;
  1188. file = kmem_cache_alloc(file_cachep, GFP_TRACE);
  1189. if (!file)
  1190. return -ENOMEM;
  1191. file->event_call = call;
  1192. file->tr = tr;
  1193. list_add(&file->list, &tr->events);
  1194. return 0;
  1195. }
  1196. struct ftrace_module_file_ops;
  1197. static void __add_event_to_tracers(struct ftrace_event_call *call,
  1198. struct ftrace_module_file_ops *file_ops);
  1199. /* Add an additional event_call dynamically */
  1200. int trace_add_event_call(struct ftrace_event_call *call)
  1201. {
  1202. int ret;
  1203. mutex_lock(&event_mutex);
  1204. ret = __register_event(call, NULL);
  1205. if (ret >= 0)
  1206. __add_event_to_tracers(call, NULL);
  1207. mutex_unlock(&event_mutex);
  1208. return ret;
  1209. }
  1210. /*
  1211. * Must be called under locking both of event_mutex and trace_event_mutex.
  1212. */
  1213. static void __trace_remove_event_call(struct ftrace_event_call *call)
  1214. {
  1215. event_remove(call);
  1216. trace_destroy_fields(call);
  1217. destroy_preds(call);
  1218. }
  1219. /* Remove an event_call */
  1220. void trace_remove_event_call(struct ftrace_event_call *call)
  1221. {
  1222. mutex_lock(&event_mutex);
  1223. down_write(&trace_event_mutex);
  1224. __trace_remove_event_call(call);
  1225. up_write(&trace_event_mutex);
  1226. mutex_unlock(&event_mutex);
  1227. }
  1228. #define for_each_event(event, start, end) \
  1229. for (event = start; \
  1230. (unsigned long)event < (unsigned long)end; \
  1231. event++)
  1232. #ifdef CONFIG_MODULES
  1233. static LIST_HEAD(ftrace_module_file_list);
  1234. /*
  1235. * Modules must own their file_operations to keep up with
  1236. * reference counting.
  1237. */
  1238. struct ftrace_module_file_ops {
  1239. struct list_head list;
  1240. struct module *mod;
  1241. struct file_operations id;
  1242. struct file_operations enable;
  1243. struct file_operations format;
  1244. struct file_operations filter;
  1245. };
  1246. static struct ftrace_module_file_ops *
  1247. find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
  1248. {
  1249. /*
  1250. * As event_calls are added in groups by module,
  1251. * when we find one file_ops, we don't need to search for
  1252. * each call in that module, as the rest should be the
  1253. * same. Only search for a new one if the last one did
  1254. * not match.
  1255. */
  1256. if (file_ops && mod == file_ops->mod)
  1257. return file_ops;
  1258. list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
  1259. if (file_ops->mod == mod)
  1260. return file_ops;
  1261. }
  1262. return NULL;
  1263. }
  1264. static struct ftrace_module_file_ops *
  1265. trace_create_file_ops(struct module *mod)
  1266. {
  1267. struct ftrace_module_file_ops *file_ops;
  1268. /*
  1269. * This is a bit of a PITA. To allow for correct reference
  1270. * counting, modules must "own" their file_operations.
  1271. * To do this, we allocate the file operations that will be
  1272. * used in the event directory.
  1273. */
  1274. file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
  1275. if (!file_ops)
  1276. return NULL;
  1277. file_ops->mod = mod;
  1278. file_ops->id = ftrace_event_id_fops;
  1279. file_ops->id.owner = mod;
  1280. file_ops->enable = ftrace_enable_fops;
  1281. file_ops->enable.owner = mod;
  1282. file_ops->filter = ftrace_event_filter_fops;
  1283. file_ops->filter.owner = mod;
  1284. file_ops->format = ftrace_event_format_fops;
  1285. file_ops->format.owner = mod;
  1286. list_add(&file_ops->list, &ftrace_module_file_list);
  1287. return file_ops;
  1288. }
  1289. static void trace_module_add_events(struct module *mod)
  1290. {
  1291. struct ftrace_module_file_ops *file_ops = NULL;
  1292. struct ftrace_event_call **call, **start, **end;
  1293. start = mod->trace_events;
  1294. end = mod->trace_events + mod->num_trace_events;
  1295. if (start == end)
  1296. return;
  1297. file_ops = trace_create_file_ops(mod);
  1298. if (!file_ops)
  1299. return;
  1300. for_each_event(call, start, end) {
  1301. __register_event(*call, mod);
  1302. __add_event_to_tracers(*call, file_ops);
  1303. }
  1304. }
  1305. static void trace_module_remove_events(struct module *mod)
  1306. {
  1307. struct ftrace_module_file_ops *file_ops;
  1308. struct ftrace_event_call *call, *p;
  1309. bool clear_trace = false;
  1310. down_write(&trace_event_mutex);
  1311. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  1312. if (call->mod == mod) {
  1313. if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
  1314. clear_trace = true;
  1315. __trace_remove_event_call(call);
  1316. }
  1317. }
  1318. /* Now free the file_operations */
  1319. list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
  1320. if (file_ops->mod == mod)
  1321. break;
  1322. }
  1323. if (&file_ops->list != &ftrace_module_file_list) {
  1324. list_del(&file_ops->list);
  1325. kfree(file_ops);
  1326. }
  1327. /*
  1328. * It is safest to reset the ring buffer if the module being unloaded
  1329. * registered any events that were used.
  1330. */
  1331. if (clear_trace)
  1332. tracing_reset_current_online_cpus();
  1333. up_write(&trace_event_mutex);
  1334. }
  1335. static int trace_module_notify(struct notifier_block *self,
  1336. unsigned long val, void *data)
  1337. {
  1338. struct module *mod = data;
  1339. mutex_lock(&event_mutex);
  1340. switch (val) {
  1341. case MODULE_STATE_COMING:
  1342. trace_module_add_events(mod);
  1343. break;
  1344. case MODULE_STATE_GOING:
  1345. trace_module_remove_events(mod);
  1346. break;
  1347. }
  1348. mutex_unlock(&event_mutex);
  1349. return 0;
  1350. }
  1351. static int
  1352. __trace_add_new_mod_event(struct ftrace_event_call *call,
  1353. struct trace_array *tr,
  1354. struct ftrace_module_file_ops *file_ops)
  1355. {
  1356. return __trace_add_new_event(call, tr,
  1357. &file_ops->id, &file_ops->enable,
  1358. &file_ops->filter, &file_ops->format);
  1359. }
  1360. #else
  1361. static inline struct ftrace_module_file_ops *
  1362. find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
  1363. {
  1364. return NULL;
  1365. }
  1366. static inline int trace_module_notify(struct notifier_block *self,
  1367. unsigned long val, void *data)
  1368. {
  1369. return 0;
  1370. }
  1371. static inline int
  1372. __trace_add_new_mod_event(struct ftrace_event_call *call,
  1373. struct trace_array *tr,
  1374. struct ftrace_module_file_ops *file_ops)
  1375. {
  1376. return -ENODEV;
  1377. }
  1378. #endif /* CONFIG_MODULES */
  1379. /* Create a new event directory structure for a trace directory. */
  1380. static void
  1381. __trace_add_event_dirs(struct trace_array *tr)
  1382. {
  1383. struct ftrace_module_file_ops *file_ops = NULL;
  1384. struct ftrace_event_call *call;
  1385. int ret;
  1386. list_for_each_entry(call, &ftrace_events, list) {
  1387. if (call->mod) {
  1388. /*
  1389. * Directories for events by modules need to
  1390. * keep module ref counts when opened (as we don't
  1391. * want the module to disappear when reading one
  1392. * of these files). The file_ops keep account of
  1393. * the module ref count.
  1394. */
  1395. file_ops = find_ftrace_file_ops(file_ops, call->mod);
  1396. if (!file_ops)
  1397. continue; /* Warn? */
  1398. ret = __trace_add_new_mod_event(call, tr, file_ops);
  1399. if (ret < 0)
  1400. pr_warning("Could not create directory for event %s\n",
  1401. call->name);
  1402. continue;
  1403. }
  1404. ret = __trace_add_new_event(call, tr,
  1405. &ftrace_event_id_fops,
  1406. &ftrace_enable_fops,
  1407. &ftrace_event_filter_fops,
  1408. &ftrace_event_format_fops);
  1409. if (ret < 0)
  1410. pr_warning("Could not create directory for event %s\n",
  1411. call->name);
  1412. }
  1413. }
  1414. /*
  1415. * The top level array has already had its ftrace_event_file
  1416. * descriptors created in order to allow for early events to
  1417. * be recorded. This function is called after the debugfs has been
  1418. * initialized, and we now have to create the files associated
  1419. * to the events.
  1420. */
  1421. static __init void
  1422. __trace_early_add_event_dirs(struct trace_array *tr)
  1423. {
  1424. struct ftrace_event_file *file;
  1425. int ret;
  1426. list_for_each_entry(file, &tr->events, list) {
  1427. ret = event_create_dir(tr->event_dir, file,
  1428. &ftrace_event_id_fops,
  1429. &ftrace_enable_fops,
  1430. &ftrace_event_filter_fops,
  1431. &ftrace_event_format_fops);
  1432. if (ret < 0)
  1433. pr_warning("Could not create directory for event %s\n",
  1434. file->event_call->name);
  1435. }
  1436. }
  1437. /*
  1438. * For early boot up, the top trace array requires to have
  1439. * a list of events that can be enabled. This must be done before
  1440. * the filesystem is set up in order to allow events to be traced
  1441. * early.
  1442. */
  1443. static __init void
  1444. __trace_early_add_events(struct trace_array *tr)
  1445. {
  1446. struct ftrace_event_call *call;
  1447. int ret;
  1448. list_for_each_entry(call, &ftrace_events, list) {
  1449. /* Early boot up should not have any modules loaded */
  1450. if (WARN_ON_ONCE(call->mod))
  1451. continue;
  1452. ret = __trace_early_add_new_event(call, tr);
  1453. if (ret < 0)
  1454. pr_warning("Could not create early event %s\n",
  1455. call->name);
  1456. }
  1457. }
  1458. /* Remove the event directory structure for a trace directory. */
  1459. static void
  1460. __trace_remove_event_dirs(struct trace_array *tr)
  1461. {
  1462. struct ftrace_event_file *file, *next;
  1463. list_for_each_entry_safe(file, next, &tr->events, list) {
  1464. list_del(&file->list);
  1465. debugfs_remove_recursive(file->dir);
  1466. remove_subsystem(file->system);
  1467. kmem_cache_free(file_cachep, file);
  1468. }
  1469. }
  1470. static void
  1471. __add_event_to_tracers(struct ftrace_event_call *call,
  1472. struct ftrace_module_file_ops *file_ops)
  1473. {
  1474. struct trace_array *tr;
  1475. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  1476. if (file_ops)
  1477. __trace_add_new_mod_event(call, tr, file_ops);
  1478. else
  1479. __trace_add_new_event(call, tr,
  1480. &ftrace_event_id_fops,
  1481. &ftrace_enable_fops,
  1482. &ftrace_event_filter_fops,
  1483. &ftrace_event_format_fops);
  1484. }
  1485. }
  1486. static struct notifier_block trace_module_nb = {
  1487. .notifier_call = trace_module_notify,
  1488. .priority = 0,
  1489. };
  1490. extern struct ftrace_event_call *__start_ftrace_events[];
  1491. extern struct ftrace_event_call *__stop_ftrace_events[];
  1492. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  1493. static __init int setup_trace_event(char *str)
  1494. {
  1495. strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  1496. ring_buffer_expanded = 1;
  1497. tracing_selftest_disabled = 1;
  1498. return 1;
  1499. }
  1500. __setup("trace_event=", setup_trace_event);
  1501. /* Expects to have event_mutex held when called */
  1502. static int
  1503. create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
  1504. {
  1505. struct dentry *d_events;
  1506. struct dentry *entry;
  1507. entry = debugfs_create_file("set_event", 0644, parent,
  1508. tr, &ftrace_set_event_fops);
  1509. if (!entry) {
  1510. pr_warning("Could not create debugfs 'set_event' entry\n");
  1511. return -ENOMEM;
  1512. }
  1513. d_events = debugfs_create_dir("events", parent);
  1514. if (!d_events) {
  1515. pr_warning("Could not create debugfs 'events' directory\n");
  1516. return -ENOMEM;
  1517. }
  1518. /* ring buffer internal formats */
  1519. trace_create_file("header_page", 0444, d_events,
  1520. ring_buffer_print_page_header,
  1521. &ftrace_show_header_fops);
  1522. trace_create_file("header_event", 0444, d_events,
  1523. ring_buffer_print_entry_header,
  1524. &ftrace_show_header_fops);
  1525. trace_create_file("enable", 0644, d_events,
  1526. tr, &ftrace_tr_enable_fops);
  1527. tr->event_dir = d_events;
  1528. return 0;
  1529. }
  1530. /**
  1531. * event_trace_add_tracer - add a instance of a trace_array to events
  1532. * @parent: The parent dentry to place the files/directories for events in
  1533. * @tr: The trace array associated with these events
  1534. *
  1535. * When a new instance is created, it needs to set up its events
  1536. * directory, as well as other files associated with events. It also
  1537. * creates the event hierachry in the @parent/events directory.
  1538. *
  1539. * Returns 0 on success.
  1540. */
  1541. int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
  1542. {
  1543. int ret;
  1544. mutex_lock(&event_mutex);
  1545. ret = create_event_toplevel_files(parent, tr);
  1546. if (ret)
  1547. goto out_unlock;
  1548. down_write(&trace_event_mutex);
  1549. __trace_add_event_dirs(tr);
  1550. up_write(&trace_event_mutex);
  1551. out_unlock:
  1552. mutex_unlock(&event_mutex);
  1553. return ret;
  1554. }
  1555. /*
  1556. * The top trace array already had its file descriptors created.
  1557. * Now the files themselves need to be created.
  1558. */
  1559. static __init int
  1560. early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
  1561. {
  1562. int ret;
  1563. mutex_lock(&event_mutex);
  1564. ret = create_event_toplevel_files(parent, tr);
  1565. if (ret)
  1566. goto out_unlock;
  1567. down_write(&trace_event_mutex);
  1568. __trace_early_add_event_dirs(tr);
  1569. up_write(&trace_event_mutex);
  1570. out_unlock:
  1571. mutex_unlock(&event_mutex);
  1572. return ret;
  1573. }
  1574. int event_trace_del_tracer(struct trace_array *tr)
  1575. {
  1576. /* Disable any running events */
  1577. __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
  1578. mutex_lock(&event_mutex);
  1579. down_write(&trace_event_mutex);
  1580. __trace_remove_event_dirs(tr);
  1581. debugfs_remove_recursive(tr->event_dir);
  1582. up_write(&trace_event_mutex);
  1583. tr->event_dir = NULL;
  1584. mutex_unlock(&event_mutex);
  1585. return 0;
  1586. }
  1587. static __init int event_trace_memsetup(void)
  1588. {
  1589. field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
  1590. file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
  1591. return 0;
  1592. }
  1593. static __init int event_trace_enable(void)
  1594. {
  1595. struct trace_array *tr = top_trace_array();
  1596. struct ftrace_event_call **iter, *call;
  1597. char *buf = bootup_event_buf;
  1598. char *token;
  1599. int ret;
  1600. for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
  1601. call = *iter;
  1602. ret = event_init(call);
  1603. if (!ret)
  1604. list_add(&call->list, &ftrace_events);
  1605. }
  1606. /*
  1607. * We need the top trace array to have a working set of trace
  1608. * points at early init, before the debug files and directories
  1609. * are created. Create the file entries now, and attach them
  1610. * to the actual file dentries later.
  1611. */
  1612. __trace_early_add_events(tr);
  1613. while (true) {
  1614. token = strsep(&buf, ",");
  1615. if (!token)
  1616. break;
  1617. if (!*token)
  1618. continue;
  1619. ret = ftrace_set_clr_event(tr, token, 1);
  1620. if (ret)
  1621. pr_warn("Failed to enable trace event: %s\n", token);
  1622. }
  1623. trace_printk_start_comm();
  1624. return 0;
  1625. }
  1626. static __init int event_trace_init(void)
  1627. {
  1628. struct trace_array *tr;
  1629. struct dentry *d_tracer;
  1630. struct dentry *entry;
  1631. int ret;
  1632. tr = top_trace_array();
  1633. d_tracer = tracing_init_dentry();
  1634. if (!d_tracer)
  1635. return 0;
  1636. entry = debugfs_create_file("available_events", 0444, d_tracer,
  1637. tr, &ftrace_avail_fops);
  1638. if (!entry)
  1639. pr_warning("Could not create debugfs "
  1640. "'available_events' entry\n");
  1641. if (trace_define_common_fields())
  1642. pr_warning("tracing: Failed to allocate common fields");
  1643. ret = early_event_add_tracer(d_tracer, tr);
  1644. if (ret)
  1645. return ret;
  1646. ret = register_module_notifier(&trace_module_nb);
  1647. if (ret)
  1648. pr_warning("Failed to register trace events module notifier\n");
  1649. return 0;
  1650. }
  1651. early_initcall(event_trace_memsetup);
  1652. core_initcall(event_trace_enable);
  1653. fs_initcall(event_trace_init);
  1654. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1655. static DEFINE_SPINLOCK(test_spinlock);
  1656. static DEFINE_SPINLOCK(test_spinlock_irq);
  1657. static DEFINE_MUTEX(test_mutex);
  1658. static __init void test_work(struct work_struct *dummy)
  1659. {
  1660. spin_lock(&test_spinlock);
  1661. spin_lock_irq(&test_spinlock_irq);
  1662. udelay(1);
  1663. spin_unlock_irq(&test_spinlock_irq);
  1664. spin_unlock(&test_spinlock);
  1665. mutex_lock(&test_mutex);
  1666. msleep(1);
  1667. mutex_unlock(&test_mutex);
  1668. }
  1669. static __init int event_test_thread(void *unused)
  1670. {
  1671. void *test_malloc;
  1672. test_malloc = kmalloc(1234, GFP_KERNEL);
  1673. if (!test_malloc)
  1674. pr_info("failed to kmalloc\n");
  1675. schedule_on_each_cpu(test_work);
  1676. kfree(test_malloc);
  1677. set_current_state(TASK_INTERRUPTIBLE);
  1678. while (!kthread_should_stop())
  1679. schedule();
  1680. return 0;
  1681. }
  1682. /*
  1683. * Do various things that may trigger events.
  1684. */
  1685. static __init void event_test_stuff(void)
  1686. {
  1687. struct task_struct *test_thread;
  1688. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  1689. msleep(1);
  1690. kthread_stop(test_thread);
  1691. }
  1692. /*
  1693. * For every trace event defined, we will test each trace point separately,
  1694. * and then by groups, and finally all trace points.
  1695. */
  1696. static __init void event_trace_self_tests(void)
  1697. {
  1698. struct ftrace_subsystem_dir *dir;
  1699. struct ftrace_event_file *file;
  1700. struct ftrace_event_call *call;
  1701. struct event_subsystem *system;
  1702. struct trace_array *tr;
  1703. int ret;
  1704. tr = top_trace_array();
  1705. pr_info("Running tests on trace events:\n");
  1706. list_for_each_entry(file, &tr->events, list) {
  1707. call = file->event_call;
  1708. /* Only test those that have a probe */
  1709. if (!call->class || !call->class->probe)
  1710. continue;
  1711. /*
  1712. * Testing syscall events here is pretty useless, but
  1713. * we still do it if configured. But this is time consuming.
  1714. * What we really need is a user thread to perform the
  1715. * syscalls as we test.
  1716. */
  1717. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  1718. if (call->class->system &&
  1719. strcmp(call->class->system, "syscalls") == 0)
  1720. continue;
  1721. #endif
  1722. pr_info("Testing event %s: ", call->name);
  1723. /*
  1724. * If an event is already enabled, someone is using
  1725. * it and the self test should not be on.
  1726. */
  1727. if (file->flags & FTRACE_EVENT_FL_ENABLED) {
  1728. pr_warning("Enabled event during self test!\n");
  1729. WARN_ON_ONCE(1);
  1730. continue;
  1731. }
  1732. ftrace_event_enable_disable(file, 1);
  1733. event_test_stuff();
  1734. ftrace_event_enable_disable(file, 0);
  1735. pr_cont("OK\n");
  1736. }
  1737. /* Now test at the sub system level */
  1738. pr_info("Running tests on trace event systems:\n");
  1739. list_for_each_entry(dir, &tr->systems, list) {
  1740. system = dir->subsystem;
  1741. /* the ftrace system is special, skip it */
  1742. if (strcmp(system->name, "ftrace") == 0)
  1743. continue;
  1744. pr_info("Testing event system %s: ", system->name);
  1745. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
  1746. if (WARN_ON_ONCE(ret)) {
  1747. pr_warning("error enabling system %s\n",
  1748. system->name);
  1749. continue;
  1750. }
  1751. event_test_stuff();
  1752. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
  1753. if (WARN_ON_ONCE(ret)) {
  1754. pr_warning("error disabling system %s\n",
  1755. system->name);
  1756. continue;
  1757. }
  1758. pr_cont("OK\n");
  1759. }
  1760. /* Test with all events enabled */
  1761. pr_info("Running tests on all trace events:\n");
  1762. pr_info("Testing all events: ");
  1763. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
  1764. if (WARN_ON_ONCE(ret)) {
  1765. pr_warning("error enabling all events\n");
  1766. return;
  1767. }
  1768. event_test_stuff();
  1769. /* reset sysname */
  1770. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
  1771. if (WARN_ON_ONCE(ret)) {
  1772. pr_warning("error disabling all events\n");
  1773. return;
  1774. }
  1775. pr_cont("OK\n");
  1776. }
  1777. #ifdef CONFIG_FUNCTION_TRACER
  1778. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  1779. static void
  1780. function_test_events_call(unsigned long ip, unsigned long parent_ip,
  1781. struct ftrace_ops *op, struct pt_regs *pt_regs)
  1782. {
  1783. struct ring_buffer_event *event;
  1784. struct ring_buffer *buffer;
  1785. struct ftrace_entry *entry;
  1786. unsigned long flags;
  1787. long disabled;
  1788. int cpu;
  1789. int pc;
  1790. pc = preempt_count();
  1791. preempt_disable_notrace();
  1792. cpu = raw_smp_processor_id();
  1793. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  1794. if (disabled != 1)
  1795. goto out;
  1796. local_save_flags(flags);
  1797. event = trace_current_buffer_lock_reserve(&buffer,
  1798. TRACE_FN, sizeof(*entry),
  1799. flags, pc);
  1800. if (!event)
  1801. goto out;
  1802. entry = ring_buffer_event_data(event);
  1803. entry->ip = ip;
  1804. entry->parent_ip = parent_ip;
  1805. trace_buffer_unlock_commit(buffer, event, flags, pc);
  1806. out:
  1807. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  1808. preempt_enable_notrace();
  1809. }
  1810. static struct ftrace_ops trace_ops __initdata =
  1811. {
  1812. .func = function_test_events_call,
  1813. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  1814. };
  1815. static __init void event_trace_self_test_with_function(void)
  1816. {
  1817. int ret;
  1818. ret = register_ftrace_function(&trace_ops);
  1819. if (WARN_ON(ret < 0)) {
  1820. pr_info("Failed to enable function tracer for event tests\n");
  1821. return;
  1822. }
  1823. pr_info("Running tests again, along with the function tracer\n");
  1824. event_trace_self_tests();
  1825. unregister_ftrace_function(&trace_ops);
  1826. }
  1827. #else
  1828. static __init void event_trace_self_test_with_function(void)
  1829. {
  1830. }
  1831. #endif
  1832. static __init int event_trace_self_tests_init(void)
  1833. {
  1834. if (!tracing_selftest_disabled) {
  1835. event_trace_self_tests();
  1836. event_trace_self_test_with_function();
  1837. }
  1838. return 0;
  1839. }
  1840. late_initcall(event_trace_self_tests_init);
  1841. #endif