trace_events.c 50 KB

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