trace_events.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  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. #define pr_fmt(fmt) fmt
  11. #include <linux/workqueue.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/kthread.h>
  14. #include <linux/tracefs.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/module.h>
  17. #include <linux/ctype.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <asm/setup.h>
  21. #include "trace_output.h"
  22. #undef TRACE_SYSTEM
  23. #define TRACE_SYSTEM "TRACE_SYSTEM"
  24. DEFINE_MUTEX(event_mutex);
  25. LIST_HEAD(ftrace_events);
  26. static LIST_HEAD(ftrace_common_fields);
  27. #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
  28. static struct kmem_cache *field_cachep;
  29. static struct kmem_cache *file_cachep;
  30. #define SYSTEM_FL_FREE_NAME (1 << 31)
  31. static inline int system_refcount(struct event_subsystem *system)
  32. {
  33. return system->ref_count & ~SYSTEM_FL_FREE_NAME;
  34. }
  35. static int system_refcount_inc(struct event_subsystem *system)
  36. {
  37. return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
  38. }
  39. static int system_refcount_dec(struct event_subsystem *system)
  40. {
  41. return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
  42. }
  43. /* Double loops, do not use break, only goto's work */
  44. #define do_for_each_event_file(tr, file) \
  45. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  46. list_for_each_entry(file, &tr->events, list)
  47. #define do_for_each_event_file_safe(tr, file) \
  48. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  49. struct trace_event_file *___n; \
  50. list_for_each_entry_safe(file, ___n, &tr->events, list)
  51. #define while_for_each_event_file() \
  52. }
  53. static struct list_head *
  54. trace_get_fields(struct trace_event_call *event_call)
  55. {
  56. if (!event_call->class->get_fields)
  57. return &event_call->class->fields;
  58. return event_call->class->get_fields(event_call);
  59. }
  60. static struct ftrace_event_field *
  61. __find_event_field(struct list_head *head, char *name)
  62. {
  63. struct ftrace_event_field *field;
  64. list_for_each_entry(field, head, link) {
  65. if (!strcmp(field->name, name))
  66. return field;
  67. }
  68. return NULL;
  69. }
  70. struct ftrace_event_field *
  71. trace_find_event_field(struct trace_event_call *call, char *name)
  72. {
  73. struct ftrace_event_field *field;
  74. struct list_head *head;
  75. field = __find_event_field(&ftrace_common_fields, name);
  76. if (field)
  77. return field;
  78. head = trace_get_fields(call);
  79. return __find_event_field(head, name);
  80. }
  81. static int __trace_define_field(struct list_head *head, const char *type,
  82. const char *name, int offset, int size,
  83. int is_signed, int filter_type)
  84. {
  85. struct ftrace_event_field *field;
  86. field = kmem_cache_alloc(field_cachep, GFP_TRACE);
  87. if (!field)
  88. return -ENOMEM;
  89. field->name = name;
  90. field->type = type;
  91. if (filter_type == FILTER_OTHER)
  92. field->filter_type = filter_assign_type(type);
  93. else
  94. field->filter_type = filter_type;
  95. field->offset = offset;
  96. field->size = size;
  97. field->is_signed = is_signed;
  98. list_add(&field->link, head);
  99. return 0;
  100. }
  101. int trace_define_field(struct trace_event_call *call, const char *type,
  102. const char *name, int offset, int size, int is_signed,
  103. int filter_type)
  104. {
  105. struct list_head *head;
  106. if (WARN_ON(!call->class))
  107. return 0;
  108. head = trace_get_fields(call);
  109. return __trace_define_field(head, type, name, offset, size,
  110. is_signed, filter_type);
  111. }
  112. EXPORT_SYMBOL_GPL(trace_define_field);
  113. #define __common_field(type, item) \
  114. ret = __trace_define_field(&ftrace_common_fields, #type, \
  115. "common_" #item, \
  116. offsetof(typeof(ent), item), \
  117. sizeof(ent.item), \
  118. is_signed_type(type), FILTER_OTHER); \
  119. if (ret) \
  120. return ret;
  121. static int trace_define_common_fields(void)
  122. {
  123. int ret;
  124. struct trace_entry ent;
  125. __common_field(unsigned short, type);
  126. __common_field(unsigned char, flags);
  127. __common_field(unsigned char, preempt_count);
  128. __common_field(int, pid);
  129. return ret;
  130. }
  131. static void trace_destroy_fields(struct trace_event_call *call)
  132. {
  133. struct ftrace_event_field *field, *next;
  134. struct list_head *head;
  135. head = trace_get_fields(call);
  136. list_for_each_entry_safe(field, next, head, link) {
  137. list_del(&field->link);
  138. kmem_cache_free(field_cachep, field);
  139. }
  140. }
  141. int trace_event_raw_init(struct trace_event_call *call)
  142. {
  143. int id;
  144. id = register_trace_event(&call->event);
  145. if (!id)
  146. return -ENODEV;
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(trace_event_raw_init);
  150. void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  151. struct trace_event_file *trace_file,
  152. unsigned long len)
  153. {
  154. struct trace_event_call *event_call = trace_file->event_call;
  155. local_save_flags(fbuffer->flags);
  156. fbuffer->pc = preempt_count();
  157. fbuffer->trace_file = trace_file;
  158. fbuffer->event =
  159. trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
  160. event_call->event.type, len,
  161. fbuffer->flags, fbuffer->pc);
  162. if (!fbuffer->event)
  163. return NULL;
  164. fbuffer->entry = ring_buffer_event_data(fbuffer->event);
  165. return fbuffer->entry;
  166. }
  167. EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
  168. static DEFINE_SPINLOCK(tracepoint_iter_lock);
  169. static void output_printk(struct trace_event_buffer *fbuffer)
  170. {
  171. struct trace_event_call *event_call;
  172. struct trace_event *event;
  173. unsigned long flags;
  174. struct trace_iterator *iter = tracepoint_print_iter;
  175. if (!iter)
  176. return;
  177. event_call = fbuffer->trace_file->event_call;
  178. if (!event_call || !event_call->event.funcs ||
  179. !event_call->event.funcs->trace)
  180. return;
  181. event = &fbuffer->trace_file->event_call->event;
  182. spin_lock_irqsave(&tracepoint_iter_lock, flags);
  183. trace_seq_init(&iter->seq);
  184. iter->ent = fbuffer->entry;
  185. event_call->event.funcs->trace(iter, 0, event);
  186. trace_seq_putc(&iter->seq, 0);
  187. printk("%s", iter->seq.buffer);
  188. spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
  189. }
  190. void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
  191. {
  192. if (tracepoint_printk)
  193. output_printk(fbuffer);
  194. event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
  195. fbuffer->event, fbuffer->entry,
  196. fbuffer->flags, fbuffer->pc);
  197. }
  198. EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
  199. int trace_event_reg(struct trace_event_call *call,
  200. enum trace_reg type, void *data)
  201. {
  202. struct trace_event_file *file = data;
  203. WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
  204. switch (type) {
  205. case TRACE_REG_REGISTER:
  206. return tracepoint_probe_register(call->tp,
  207. call->class->probe,
  208. file);
  209. case TRACE_REG_UNREGISTER:
  210. tracepoint_probe_unregister(call->tp,
  211. call->class->probe,
  212. file);
  213. return 0;
  214. #ifdef CONFIG_PERF_EVENTS
  215. case TRACE_REG_PERF_REGISTER:
  216. return tracepoint_probe_register(call->tp,
  217. call->class->perf_probe,
  218. call);
  219. case TRACE_REG_PERF_UNREGISTER:
  220. tracepoint_probe_unregister(call->tp,
  221. call->class->perf_probe,
  222. call);
  223. return 0;
  224. case TRACE_REG_PERF_OPEN:
  225. case TRACE_REG_PERF_CLOSE:
  226. case TRACE_REG_PERF_ADD:
  227. case TRACE_REG_PERF_DEL:
  228. return 0;
  229. #endif
  230. }
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(trace_event_reg);
  234. void trace_event_enable_cmd_record(bool enable)
  235. {
  236. struct trace_event_file *file;
  237. struct trace_array *tr;
  238. mutex_lock(&event_mutex);
  239. do_for_each_event_file(tr, file) {
  240. if (!(file->flags & EVENT_FILE_FL_ENABLED))
  241. continue;
  242. if (enable) {
  243. tracing_start_cmdline_record();
  244. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  245. } else {
  246. tracing_stop_cmdline_record();
  247. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  248. }
  249. } while_for_each_event_file();
  250. mutex_unlock(&event_mutex);
  251. }
  252. static int __ftrace_event_enable_disable(struct trace_event_file *file,
  253. int enable, int soft_disable)
  254. {
  255. struct trace_event_call *call = file->event_call;
  256. int ret = 0;
  257. int disable;
  258. switch (enable) {
  259. case 0:
  260. /*
  261. * When soft_disable is set and enable is cleared, the sm_ref
  262. * reference counter is decremented. If it reaches 0, we want
  263. * to clear the SOFT_DISABLED flag but leave the event in the
  264. * state that it was. That is, if the event was enabled and
  265. * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
  266. * is set we do not want the event to be enabled before we
  267. * clear the bit.
  268. *
  269. * When soft_disable is not set but the SOFT_MODE flag is,
  270. * we do nothing. Do not disable the tracepoint, otherwise
  271. * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
  272. */
  273. if (soft_disable) {
  274. if (atomic_dec_return(&file->sm_ref) > 0)
  275. break;
  276. disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
  277. clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
  278. } else
  279. disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
  280. if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
  281. clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  282. if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
  283. tracing_stop_cmdline_record();
  284. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  285. }
  286. call->class->reg(call, TRACE_REG_UNREGISTER, file);
  287. }
  288. /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
  289. if (file->flags & EVENT_FILE_FL_SOFT_MODE)
  290. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  291. else
  292. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  293. break;
  294. case 1:
  295. /*
  296. * When soft_disable is set and enable is set, we want to
  297. * register the tracepoint for the event, but leave the event
  298. * as is. That means, if the event was already enabled, we do
  299. * nothing (but set SOFT_MODE). If the event is disabled, we
  300. * set SOFT_DISABLED before enabling the event tracepoint, so
  301. * it still seems to be disabled.
  302. */
  303. if (!soft_disable)
  304. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  305. else {
  306. if (atomic_inc_return(&file->sm_ref) > 1)
  307. break;
  308. set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
  309. }
  310. if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
  311. /* Keep the event disabled, when going to SOFT_MODE. */
  312. if (soft_disable)
  313. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  314. if (trace_flags & TRACE_ITER_RECORD_CMD) {
  315. tracing_start_cmdline_record();
  316. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  317. }
  318. ret = call->class->reg(call, TRACE_REG_REGISTER, file);
  319. if (ret) {
  320. tracing_stop_cmdline_record();
  321. pr_info("event trace: Could not enable event "
  322. "%s\n", trace_event_name(call));
  323. break;
  324. }
  325. set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  326. /* WAS_ENABLED gets set but never cleared. */
  327. call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
  328. }
  329. break;
  330. }
  331. return ret;
  332. }
  333. int trace_event_enable_disable(struct trace_event_file *file,
  334. int enable, int soft_disable)
  335. {
  336. return __ftrace_event_enable_disable(file, enable, soft_disable);
  337. }
  338. static int ftrace_event_enable_disable(struct trace_event_file *file,
  339. int enable)
  340. {
  341. return __ftrace_event_enable_disable(file, enable, 0);
  342. }
  343. static void ftrace_clear_events(struct trace_array *tr)
  344. {
  345. struct trace_event_file *file;
  346. mutex_lock(&event_mutex);
  347. list_for_each_entry(file, &tr->events, list) {
  348. ftrace_event_enable_disable(file, 0);
  349. }
  350. mutex_unlock(&event_mutex);
  351. }
  352. static void __put_system(struct event_subsystem *system)
  353. {
  354. struct event_filter *filter = system->filter;
  355. WARN_ON_ONCE(system_refcount(system) == 0);
  356. if (system_refcount_dec(system))
  357. return;
  358. list_del(&system->list);
  359. if (filter) {
  360. kfree(filter->filter_string);
  361. kfree(filter);
  362. }
  363. if (system->ref_count & SYSTEM_FL_FREE_NAME)
  364. kfree(system->name);
  365. kfree(system);
  366. }
  367. static void __get_system(struct event_subsystem *system)
  368. {
  369. WARN_ON_ONCE(system_refcount(system) == 0);
  370. system_refcount_inc(system);
  371. }
  372. static void __get_system_dir(struct trace_subsystem_dir *dir)
  373. {
  374. WARN_ON_ONCE(dir->ref_count == 0);
  375. dir->ref_count++;
  376. __get_system(dir->subsystem);
  377. }
  378. static void __put_system_dir(struct trace_subsystem_dir *dir)
  379. {
  380. WARN_ON_ONCE(dir->ref_count == 0);
  381. /* If the subsystem is about to be freed, the dir must be too */
  382. WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
  383. __put_system(dir->subsystem);
  384. if (!--dir->ref_count)
  385. kfree(dir);
  386. }
  387. static void put_system(struct trace_subsystem_dir *dir)
  388. {
  389. mutex_lock(&event_mutex);
  390. __put_system_dir(dir);
  391. mutex_unlock(&event_mutex);
  392. }
  393. static void remove_subsystem(struct trace_subsystem_dir *dir)
  394. {
  395. if (!dir)
  396. return;
  397. if (!--dir->nr_events) {
  398. tracefs_remove_recursive(dir->entry);
  399. list_del(&dir->list);
  400. __put_system_dir(dir);
  401. }
  402. }
  403. static void remove_event_file_dir(struct trace_event_file *file)
  404. {
  405. struct dentry *dir = file->dir;
  406. struct dentry *child;
  407. if (dir) {
  408. spin_lock(&dir->d_lock); /* probably unneeded */
  409. list_for_each_entry(child, &dir->d_subdirs, d_child) {
  410. if (d_really_is_positive(child)) /* probably unneeded */
  411. d_inode(child)->i_private = NULL;
  412. }
  413. spin_unlock(&dir->d_lock);
  414. tracefs_remove_recursive(dir);
  415. }
  416. list_del(&file->list);
  417. remove_subsystem(file->system);
  418. free_event_filter(file->filter);
  419. kmem_cache_free(file_cachep, file);
  420. }
  421. /*
  422. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  423. */
  424. static int
  425. __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
  426. const char *sub, const char *event, int set)
  427. {
  428. struct trace_event_file *file;
  429. struct trace_event_call *call;
  430. const char *name;
  431. int ret = -EINVAL;
  432. list_for_each_entry(file, &tr->events, list) {
  433. call = file->event_call;
  434. name = trace_event_name(call);
  435. if (!name || !call->class || !call->class->reg)
  436. continue;
  437. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  438. continue;
  439. if (match &&
  440. strcmp(match, name) != 0 &&
  441. strcmp(match, call->class->system) != 0)
  442. continue;
  443. if (sub && strcmp(sub, call->class->system) != 0)
  444. continue;
  445. if (event && strcmp(event, name) != 0)
  446. continue;
  447. ftrace_event_enable_disable(file, set);
  448. ret = 0;
  449. }
  450. return ret;
  451. }
  452. static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
  453. const char *sub, const char *event, int set)
  454. {
  455. int ret;
  456. mutex_lock(&event_mutex);
  457. ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
  458. mutex_unlock(&event_mutex);
  459. return ret;
  460. }
  461. static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
  462. {
  463. char *event = NULL, *sub = NULL, *match;
  464. int ret;
  465. /*
  466. * The buf format can be <subsystem>:<event-name>
  467. * *:<event-name> means any event by that name.
  468. * :<event-name> is the same.
  469. *
  470. * <subsystem>:* means all events in that subsystem
  471. * <subsystem>: means the same.
  472. *
  473. * <name> (no ':') means all events in a subsystem with
  474. * the name <name> or any event that matches <name>
  475. */
  476. match = strsep(&buf, ":");
  477. if (buf) {
  478. sub = match;
  479. event = buf;
  480. match = NULL;
  481. if (!strlen(sub) || strcmp(sub, "*") == 0)
  482. sub = NULL;
  483. if (!strlen(event) || strcmp(event, "*") == 0)
  484. event = NULL;
  485. }
  486. ret = __ftrace_set_clr_event(tr, match, sub, event, set);
  487. /* Put back the colon to allow this to be called again */
  488. if (buf)
  489. *(buf - 1) = ':';
  490. return ret;
  491. }
  492. /**
  493. * trace_set_clr_event - enable or disable an event
  494. * @system: system name to match (NULL for any system)
  495. * @event: event name to match (NULL for all events, within system)
  496. * @set: 1 to enable, 0 to disable
  497. *
  498. * This is a way for other parts of the kernel to enable or disable
  499. * event recording.
  500. *
  501. * Returns 0 on success, -EINVAL if the parameters do not match any
  502. * registered events.
  503. */
  504. int trace_set_clr_event(const char *system, const char *event, int set)
  505. {
  506. struct trace_array *tr = top_trace_array();
  507. if (!tr)
  508. return -ENODEV;
  509. return __ftrace_set_clr_event(tr, NULL, system, event, set);
  510. }
  511. EXPORT_SYMBOL_GPL(trace_set_clr_event);
  512. /* 128 should be much more than enough */
  513. #define EVENT_BUF_SIZE 127
  514. static ssize_t
  515. ftrace_event_write(struct file *file, const char __user *ubuf,
  516. size_t cnt, loff_t *ppos)
  517. {
  518. struct trace_parser parser;
  519. struct seq_file *m = file->private_data;
  520. struct trace_array *tr = m->private;
  521. ssize_t read, ret;
  522. if (!cnt)
  523. return 0;
  524. ret = tracing_update_buffers();
  525. if (ret < 0)
  526. return ret;
  527. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  528. return -ENOMEM;
  529. read = trace_get_user(&parser, ubuf, cnt, ppos);
  530. if (read >= 0 && trace_parser_loaded((&parser))) {
  531. int set = 1;
  532. if (*parser.buffer == '!')
  533. set = 0;
  534. parser.buffer[parser.idx] = 0;
  535. ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
  536. if (ret)
  537. goto out_put;
  538. }
  539. ret = read;
  540. out_put:
  541. trace_parser_put(&parser);
  542. return ret;
  543. }
  544. static void *
  545. t_next(struct seq_file *m, void *v, loff_t *pos)
  546. {
  547. struct trace_event_file *file = v;
  548. struct trace_event_call *call;
  549. struct trace_array *tr = m->private;
  550. (*pos)++;
  551. list_for_each_entry_continue(file, &tr->events, list) {
  552. call = file->event_call;
  553. /*
  554. * The ftrace subsystem is for showing formats only.
  555. * They can not be enabled or disabled via the event files.
  556. */
  557. if (call->class && call->class->reg)
  558. return file;
  559. }
  560. return NULL;
  561. }
  562. static void *t_start(struct seq_file *m, loff_t *pos)
  563. {
  564. struct trace_event_file *file;
  565. struct trace_array *tr = m->private;
  566. loff_t l;
  567. mutex_lock(&event_mutex);
  568. file = list_entry(&tr->events, struct trace_event_file, list);
  569. for (l = 0; l <= *pos; ) {
  570. file = t_next(m, file, &l);
  571. if (!file)
  572. break;
  573. }
  574. return file;
  575. }
  576. static void *
  577. s_next(struct seq_file *m, void *v, loff_t *pos)
  578. {
  579. struct trace_event_file *file = v;
  580. struct trace_array *tr = m->private;
  581. (*pos)++;
  582. list_for_each_entry_continue(file, &tr->events, list) {
  583. if (file->flags & EVENT_FILE_FL_ENABLED)
  584. return file;
  585. }
  586. return NULL;
  587. }
  588. static void *s_start(struct seq_file *m, loff_t *pos)
  589. {
  590. struct trace_event_file *file;
  591. struct trace_array *tr = m->private;
  592. loff_t l;
  593. mutex_lock(&event_mutex);
  594. file = list_entry(&tr->events, struct trace_event_file, list);
  595. for (l = 0; l <= *pos; ) {
  596. file = s_next(m, file, &l);
  597. if (!file)
  598. break;
  599. }
  600. return file;
  601. }
  602. static int t_show(struct seq_file *m, void *v)
  603. {
  604. struct trace_event_file *file = v;
  605. struct trace_event_call *call = file->event_call;
  606. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  607. seq_printf(m, "%s:", call->class->system);
  608. seq_printf(m, "%s\n", trace_event_name(call));
  609. return 0;
  610. }
  611. static void t_stop(struct seq_file *m, void *p)
  612. {
  613. mutex_unlock(&event_mutex);
  614. }
  615. static ssize_t
  616. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  617. loff_t *ppos)
  618. {
  619. struct trace_event_file *file;
  620. unsigned long flags;
  621. char buf[4] = "0";
  622. mutex_lock(&event_mutex);
  623. file = event_file_data(filp);
  624. if (likely(file))
  625. flags = file->flags;
  626. mutex_unlock(&event_mutex);
  627. if (!file)
  628. return -ENODEV;
  629. if (flags & EVENT_FILE_FL_ENABLED &&
  630. !(flags & EVENT_FILE_FL_SOFT_DISABLED))
  631. strcpy(buf, "1");
  632. if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
  633. flags & EVENT_FILE_FL_SOFT_MODE)
  634. strcat(buf, "*");
  635. strcat(buf, "\n");
  636. return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
  637. }
  638. static ssize_t
  639. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  640. loff_t *ppos)
  641. {
  642. struct trace_event_file *file;
  643. unsigned long val;
  644. int ret;
  645. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  646. if (ret)
  647. return ret;
  648. ret = tracing_update_buffers();
  649. if (ret < 0)
  650. return ret;
  651. switch (val) {
  652. case 0:
  653. case 1:
  654. ret = -ENODEV;
  655. mutex_lock(&event_mutex);
  656. file = event_file_data(filp);
  657. if (likely(file))
  658. ret = ftrace_event_enable_disable(file, val);
  659. mutex_unlock(&event_mutex);
  660. break;
  661. default:
  662. return -EINVAL;
  663. }
  664. *ppos += cnt;
  665. return ret ? ret : cnt;
  666. }
  667. static ssize_t
  668. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  669. loff_t *ppos)
  670. {
  671. const char set_to_char[4] = { '?', '0', '1', 'X' };
  672. struct trace_subsystem_dir *dir = filp->private_data;
  673. struct event_subsystem *system = dir->subsystem;
  674. struct trace_event_call *call;
  675. struct trace_event_file *file;
  676. struct trace_array *tr = dir->tr;
  677. char buf[2];
  678. int set = 0;
  679. int ret;
  680. mutex_lock(&event_mutex);
  681. list_for_each_entry(file, &tr->events, list) {
  682. call = file->event_call;
  683. if (!trace_event_name(call) || !call->class || !call->class->reg)
  684. continue;
  685. if (system && strcmp(call->class->system, system->name) != 0)
  686. continue;
  687. /*
  688. * We need to find out if all the events are set
  689. * or if all events or cleared, or if we have
  690. * a mixture.
  691. */
  692. set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
  693. /*
  694. * If we have a mixture, no need to look further.
  695. */
  696. if (set == 3)
  697. break;
  698. }
  699. mutex_unlock(&event_mutex);
  700. buf[0] = set_to_char[set];
  701. buf[1] = '\n';
  702. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  703. return ret;
  704. }
  705. static ssize_t
  706. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  707. loff_t *ppos)
  708. {
  709. struct trace_subsystem_dir *dir = filp->private_data;
  710. struct event_subsystem *system = dir->subsystem;
  711. const char *name = NULL;
  712. unsigned long val;
  713. ssize_t ret;
  714. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  715. if (ret)
  716. return ret;
  717. ret = tracing_update_buffers();
  718. if (ret < 0)
  719. return ret;
  720. if (val != 0 && val != 1)
  721. return -EINVAL;
  722. /*
  723. * Opening of "enable" adds a ref count to system,
  724. * so the name is safe to use.
  725. */
  726. if (system)
  727. name = system->name;
  728. ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
  729. if (ret)
  730. goto out;
  731. ret = cnt;
  732. out:
  733. *ppos += cnt;
  734. return ret;
  735. }
  736. enum {
  737. FORMAT_HEADER = 1,
  738. FORMAT_FIELD_SEPERATOR = 2,
  739. FORMAT_PRINTFMT = 3,
  740. };
  741. static void *f_next(struct seq_file *m, void *v, loff_t *pos)
  742. {
  743. struct trace_event_call *call = event_file_data(m->private);
  744. struct list_head *common_head = &ftrace_common_fields;
  745. struct list_head *head = trace_get_fields(call);
  746. struct list_head *node = v;
  747. (*pos)++;
  748. switch ((unsigned long)v) {
  749. case FORMAT_HEADER:
  750. node = common_head;
  751. break;
  752. case FORMAT_FIELD_SEPERATOR:
  753. node = head;
  754. break;
  755. case FORMAT_PRINTFMT:
  756. /* all done */
  757. return NULL;
  758. }
  759. node = node->prev;
  760. if (node == common_head)
  761. return (void *)FORMAT_FIELD_SEPERATOR;
  762. else if (node == head)
  763. return (void *)FORMAT_PRINTFMT;
  764. else
  765. return node;
  766. }
  767. static int f_show(struct seq_file *m, void *v)
  768. {
  769. struct trace_event_call *call = event_file_data(m->private);
  770. struct ftrace_event_field *field;
  771. const char *array_descriptor;
  772. switch ((unsigned long)v) {
  773. case FORMAT_HEADER:
  774. seq_printf(m, "name: %s\n", trace_event_name(call));
  775. seq_printf(m, "ID: %d\n", call->event.type);
  776. seq_puts(m, "format:\n");
  777. return 0;
  778. case FORMAT_FIELD_SEPERATOR:
  779. seq_putc(m, '\n');
  780. return 0;
  781. case FORMAT_PRINTFMT:
  782. seq_printf(m, "\nprint fmt: %s\n",
  783. call->print_fmt);
  784. return 0;
  785. }
  786. field = list_entry(v, struct ftrace_event_field, link);
  787. /*
  788. * Smartly shows the array type(except dynamic array).
  789. * Normal:
  790. * field:TYPE VAR
  791. * If TYPE := TYPE[LEN], it is shown:
  792. * field:TYPE VAR[LEN]
  793. */
  794. array_descriptor = strchr(field->type, '[');
  795. if (!strncmp(field->type, "__data_loc", 10))
  796. array_descriptor = NULL;
  797. if (!array_descriptor)
  798. seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  799. field->type, field->name, field->offset,
  800. field->size, !!field->is_signed);
  801. else
  802. seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  803. (int)(array_descriptor - field->type),
  804. field->type, field->name,
  805. array_descriptor, field->offset,
  806. field->size, !!field->is_signed);
  807. return 0;
  808. }
  809. static void *f_start(struct seq_file *m, loff_t *pos)
  810. {
  811. void *p = (void *)FORMAT_HEADER;
  812. loff_t l = 0;
  813. /* ->stop() is called even if ->start() fails */
  814. mutex_lock(&event_mutex);
  815. if (!event_file_data(m->private))
  816. return ERR_PTR(-ENODEV);
  817. while (l < *pos && p)
  818. p = f_next(m, p, &l);
  819. return p;
  820. }
  821. static void f_stop(struct seq_file *m, void *p)
  822. {
  823. mutex_unlock(&event_mutex);
  824. }
  825. static const struct seq_operations trace_format_seq_ops = {
  826. .start = f_start,
  827. .next = f_next,
  828. .stop = f_stop,
  829. .show = f_show,
  830. };
  831. static int trace_format_open(struct inode *inode, struct file *file)
  832. {
  833. struct seq_file *m;
  834. int ret;
  835. ret = seq_open(file, &trace_format_seq_ops);
  836. if (ret < 0)
  837. return ret;
  838. m = file->private_data;
  839. m->private = file;
  840. return 0;
  841. }
  842. static ssize_t
  843. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  844. {
  845. int id = (long)event_file_data(filp);
  846. char buf[32];
  847. int len;
  848. if (*ppos)
  849. return 0;
  850. if (unlikely(!id))
  851. return -ENODEV;
  852. len = sprintf(buf, "%d\n", id);
  853. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  854. }
  855. static ssize_t
  856. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  857. loff_t *ppos)
  858. {
  859. struct trace_event_file *file;
  860. struct trace_seq *s;
  861. int r = -ENODEV;
  862. if (*ppos)
  863. return 0;
  864. s = kmalloc(sizeof(*s), GFP_KERNEL);
  865. if (!s)
  866. return -ENOMEM;
  867. trace_seq_init(s);
  868. mutex_lock(&event_mutex);
  869. file = event_file_data(filp);
  870. if (file)
  871. print_event_filter(file, s);
  872. mutex_unlock(&event_mutex);
  873. if (file)
  874. r = simple_read_from_buffer(ubuf, cnt, ppos,
  875. s->buffer, trace_seq_used(s));
  876. kfree(s);
  877. return r;
  878. }
  879. static ssize_t
  880. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  881. loff_t *ppos)
  882. {
  883. struct trace_event_file *file;
  884. char *buf;
  885. int err = -ENODEV;
  886. if (cnt >= PAGE_SIZE)
  887. return -EINVAL;
  888. buf = (char *)__get_free_page(GFP_TEMPORARY);
  889. if (!buf)
  890. return -ENOMEM;
  891. if (copy_from_user(buf, ubuf, cnt)) {
  892. free_page((unsigned long) buf);
  893. return -EFAULT;
  894. }
  895. buf[cnt] = '\0';
  896. mutex_lock(&event_mutex);
  897. file = event_file_data(filp);
  898. if (file)
  899. err = apply_event_filter(file, buf);
  900. mutex_unlock(&event_mutex);
  901. free_page((unsigned long) buf);
  902. if (err < 0)
  903. return err;
  904. *ppos += cnt;
  905. return cnt;
  906. }
  907. static LIST_HEAD(event_subsystems);
  908. static int subsystem_open(struct inode *inode, struct file *filp)
  909. {
  910. struct event_subsystem *system = NULL;
  911. struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
  912. struct trace_array *tr;
  913. int ret;
  914. if (tracing_is_disabled())
  915. return -ENODEV;
  916. /* Make sure the system still exists */
  917. mutex_lock(&trace_types_lock);
  918. mutex_lock(&event_mutex);
  919. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  920. list_for_each_entry(dir, &tr->systems, list) {
  921. if (dir == inode->i_private) {
  922. /* Don't open systems with no events */
  923. if (dir->nr_events) {
  924. __get_system_dir(dir);
  925. system = dir->subsystem;
  926. }
  927. goto exit_loop;
  928. }
  929. }
  930. }
  931. exit_loop:
  932. mutex_unlock(&event_mutex);
  933. mutex_unlock(&trace_types_lock);
  934. if (!system)
  935. return -ENODEV;
  936. /* Some versions of gcc think dir can be uninitialized here */
  937. WARN_ON(!dir);
  938. /* Still need to increment the ref count of the system */
  939. if (trace_array_get(tr) < 0) {
  940. put_system(dir);
  941. return -ENODEV;
  942. }
  943. ret = tracing_open_generic(inode, filp);
  944. if (ret < 0) {
  945. trace_array_put(tr);
  946. put_system(dir);
  947. }
  948. return ret;
  949. }
  950. static int system_tr_open(struct inode *inode, struct file *filp)
  951. {
  952. struct trace_subsystem_dir *dir;
  953. struct trace_array *tr = inode->i_private;
  954. int ret;
  955. if (tracing_is_disabled())
  956. return -ENODEV;
  957. if (trace_array_get(tr) < 0)
  958. return -ENODEV;
  959. /* Make a temporary dir that has no system but points to tr */
  960. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  961. if (!dir) {
  962. trace_array_put(tr);
  963. return -ENOMEM;
  964. }
  965. dir->tr = tr;
  966. ret = tracing_open_generic(inode, filp);
  967. if (ret < 0) {
  968. trace_array_put(tr);
  969. kfree(dir);
  970. return ret;
  971. }
  972. filp->private_data = dir;
  973. return 0;
  974. }
  975. static int subsystem_release(struct inode *inode, struct file *file)
  976. {
  977. struct trace_subsystem_dir *dir = file->private_data;
  978. trace_array_put(dir->tr);
  979. /*
  980. * If dir->subsystem is NULL, then this is a temporary
  981. * descriptor that was made for a trace_array to enable
  982. * all subsystems.
  983. */
  984. if (dir->subsystem)
  985. put_system(dir);
  986. else
  987. kfree(dir);
  988. return 0;
  989. }
  990. static ssize_t
  991. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  992. loff_t *ppos)
  993. {
  994. struct trace_subsystem_dir *dir = filp->private_data;
  995. struct event_subsystem *system = dir->subsystem;
  996. struct trace_seq *s;
  997. int r;
  998. if (*ppos)
  999. return 0;
  1000. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1001. if (!s)
  1002. return -ENOMEM;
  1003. trace_seq_init(s);
  1004. print_subsystem_event_filter(system, s);
  1005. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1006. s->buffer, trace_seq_used(s));
  1007. kfree(s);
  1008. return r;
  1009. }
  1010. static ssize_t
  1011. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1012. loff_t *ppos)
  1013. {
  1014. struct trace_subsystem_dir *dir = filp->private_data;
  1015. char *buf;
  1016. int err;
  1017. if (cnt >= PAGE_SIZE)
  1018. return -EINVAL;
  1019. buf = (char *)__get_free_page(GFP_TEMPORARY);
  1020. if (!buf)
  1021. return -ENOMEM;
  1022. if (copy_from_user(buf, ubuf, cnt)) {
  1023. free_page((unsigned long) buf);
  1024. return -EFAULT;
  1025. }
  1026. buf[cnt] = '\0';
  1027. err = apply_subsystem_event_filter(dir, buf);
  1028. free_page((unsigned long) buf);
  1029. if (err < 0)
  1030. return err;
  1031. *ppos += cnt;
  1032. return cnt;
  1033. }
  1034. static ssize_t
  1035. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1036. {
  1037. int (*func)(struct trace_seq *s) = filp->private_data;
  1038. struct trace_seq *s;
  1039. int r;
  1040. if (*ppos)
  1041. return 0;
  1042. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1043. if (!s)
  1044. return -ENOMEM;
  1045. trace_seq_init(s);
  1046. func(s);
  1047. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1048. s->buffer, trace_seq_used(s));
  1049. kfree(s);
  1050. return r;
  1051. }
  1052. static int ftrace_event_avail_open(struct inode *inode, struct file *file);
  1053. static int ftrace_event_set_open(struct inode *inode, struct file *file);
  1054. static int ftrace_event_release(struct inode *inode, struct file *file);
  1055. static const struct seq_operations show_event_seq_ops = {
  1056. .start = t_start,
  1057. .next = t_next,
  1058. .show = t_show,
  1059. .stop = t_stop,
  1060. };
  1061. static const struct seq_operations show_set_event_seq_ops = {
  1062. .start = s_start,
  1063. .next = s_next,
  1064. .show = t_show,
  1065. .stop = t_stop,
  1066. };
  1067. static const struct file_operations ftrace_avail_fops = {
  1068. .open = ftrace_event_avail_open,
  1069. .read = seq_read,
  1070. .llseek = seq_lseek,
  1071. .release = seq_release,
  1072. };
  1073. static const struct file_operations ftrace_set_event_fops = {
  1074. .open = ftrace_event_set_open,
  1075. .read = seq_read,
  1076. .write = ftrace_event_write,
  1077. .llseek = seq_lseek,
  1078. .release = ftrace_event_release,
  1079. };
  1080. static const struct file_operations ftrace_enable_fops = {
  1081. .open = tracing_open_generic,
  1082. .read = event_enable_read,
  1083. .write = event_enable_write,
  1084. .llseek = default_llseek,
  1085. };
  1086. static const struct file_operations ftrace_event_format_fops = {
  1087. .open = trace_format_open,
  1088. .read = seq_read,
  1089. .llseek = seq_lseek,
  1090. .release = seq_release,
  1091. };
  1092. static const struct file_operations ftrace_event_id_fops = {
  1093. .read = event_id_read,
  1094. .llseek = default_llseek,
  1095. };
  1096. static const struct file_operations ftrace_event_filter_fops = {
  1097. .open = tracing_open_generic,
  1098. .read = event_filter_read,
  1099. .write = event_filter_write,
  1100. .llseek = default_llseek,
  1101. };
  1102. static const struct file_operations ftrace_subsystem_filter_fops = {
  1103. .open = subsystem_open,
  1104. .read = subsystem_filter_read,
  1105. .write = subsystem_filter_write,
  1106. .llseek = default_llseek,
  1107. .release = subsystem_release,
  1108. };
  1109. static const struct file_operations ftrace_system_enable_fops = {
  1110. .open = subsystem_open,
  1111. .read = system_enable_read,
  1112. .write = system_enable_write,
  1113. .llseek = default_llseek,
  1114. .release = subsystem_release,
  1115. };
  1116. static const struct file_operations ftrace_tr_enable_fops = {
  1117. .open = system_tr_open,
  1118. .read = system_enable_read,
  1119. .write = system_enable_write,
  1120. .llseek = default_llseek,
  1121. .release = subsystem_release,
  1122. };
  1123. static const struct file_operations ftrace_show_header_fops = {
  1124. .open = tracing_open_generic,
  1125. .read = show_header,
  1126. .llseek = default_llseek,
  1127. };
  1128. static int
  1129. ftrace_event_open(struct inode *inode, struct file *file,
  1130. const struct seq_operations *seq_ops)
  1131. {
  1132. struct seq_file *m;
  1133. int ret;
  1134. ret = seq_open(file, seq_ops);
  1135. if (ret < 0)
  1136. return ret;
  1137. m = file->private_data;
  1138. /* copy tr over to seq ops */
  1139. m->private = inode->i_private;
  1140. return ret;
  1141. }
  1142. static int ftrace_event_release(struct inode *inode, struct file *file)
  1143. {
  1144. struct trace_array *tr = inode->i_private;
  1145. trace_array_put(tr);
  1146. return seq_release(inode, file);
  1147. }
  1148. static int
  1149. ftrace_event_avail_open(struct inode *inode, struct file *file)
  1150. {
  1151. const struct seq_operations *seq_ops = &show_event_seq_ops;
  1152. return ftrace_event_open(inode, file, seq_ops);
  1153. }
  1154. static int
  1155. ftrace_event_set_open(struct inode *inode, struct file *file)
  1156. {
  1157. const struct seq_operations *seq_ops = &show_set_event_seq_ops;
  1158. struct trace_array *tr = inode->i_private;
  1159. int ret;
  1160. if (trace_array_get(tr) < 0)
  1161. return -ENODEV;
  1162. if ((file->f_mode & FMODE_WRITE) &&
  1163. (file->f_flags & O_TRUNC))
  1164. ftrace_clear_events(tr);
  1165. ret = ftrace_event_open(inode, file, seq_ops);
  1166. if (ret < 0)
  1167. trace_array_put(tr);
  1168. return ret;
  1169. }
  1170. static struct event_subsystem *
  1171. create_new_subsystem(const char *name)
  1172. {
  1173. struct event_subsystem *system;
  1174. /* need to create new entry */
  1175. system = kmalloc(sizeof(*system), GFP_KERNEL);
  1176. if (!system)
  1177. return NULL;
  1178. system->ref_count = 1;
  1179. /* Only allocate if dynamic (kprobes and modules) */
  1180. if (!core_kernel_data((unsigned long)name)) {
  1181. system->ref_count |= SYSTEM_FL_FREE_NAME;
  1182. system->name = kstrdup(name, GFP_KERNEL);
  1183. if (!system->name)
  1184. goto out_free;
  1185. } else
  1186. system->name = name;
  1187. system->filter = NULL;
  1188. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  1189. if (!system->filter)
  1190. goto out_free;
  1191. list_add(&system->list, &event_subsystems);
  1192. return system;
  1193. out_free:
  1194. if (system->ref_count & SYSTEM_FL_FREE_NAME)
  1195. kfree(system->name);
  1196. kfree(system);
  1197. return NULL;
  1198. }
  1199. static struct dentry *
  1200. event_subsystem_dir(struct trace_array *tr, const char *name,
  1201. struct trace_event_file *file, struct dentry *parent)
  1202. {
  1203. struct trace_subsystem_dir *dir;
  1204. struct event_subsystem *system;
  1205. struct dentry *entry;
  1206. /* First see if we did not already create this dir */
  1207. list_for_each_entry(dir, &tr->systems, list) {
  1208. system = dir->subsystem;
  1209. if (strcmp(system->name, name) == 0) {
  1210. dir->nr_events++;
  1211. file->system = dir;
  1212. return dir->entry;
  1213. }
  1214. }
  1215. /* Now see if the system itself exists. */
  1216. list_for_each_entry(system, &event_subsystems, list) {
  1217. if (strcmp(system->name, name) == 0)
  1218. break;
  1219. }
  1220. /* Reset system variable when not found */
  1221. if (&system->list == &event_subsystems)
  1222. system = NULL;
  1223. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  1224. if (!dir)
  1225. goto out_fail;
  1226. if (!system) {
  1227. system = create_new_subsystem(name);
  1228. if (!system)
  1229. goto out_free;
  1230. } else
  1231. __get_system(system);
  1232. dir->entry = tracefs_create_dir(name, parent);
  1233. if (!dir->entry) {
  1234. pr_warn("Failed to create system directory %s\n", name);
  1235. __put_system(system);
  1236. goto out_free;
  1237. }
  1238. dir->tr = tr;
  1239. dir->ref_count = 1;
  1240. dir->nr_events = 1;
  1241. dir->subsystem = system;
  1242. file->system = dir;
  1243. entry = tracefs_create_file("filter", 0644, dir->entry, dir,
  1244. &ftrace_subsystem_filter_fops);
  1245. if (!entry) {
  1246. kfree(system->filter);
  1247. system->filter = NULL;
  1248. pr_warn("Could not create tracefs '%s/filter' entry\n", name);
  1249. }
  1250. trace_create_file("enable", 0644, dir->entry, dir,
  1251. &ftrace_system_enable_fops);
  1252. list_add(&dir->list, &tr->systems);
  1253. return dir->entry;
  1254. out_free:
  1255. kfree(dir);
  1256. out_fail:
  1257. /* Only print this message if failed on memory allocation */
  1258. if (!dir || !system)
  1259. pr_warn("No memory to create event subsystem %s\n", name);
  1260. return NULL;
  1261. }
  1262. static int
  1263. event_create_dir(struct dentry *parent, struct trace_event_file *file)
  1264. {
  1265. struct trace_event_call *call = file->event_call;
  1266. struct trace_array *tr = file->tr;
  1267. struct list_head *head;
  1268. struct dentry *d_events;
  1269. const char *name;
  1270. int ret;
  1271. /*
  1272. * If the trace point header did not define TRACE_SYSTEM
  1273. * then the system would be called "TRACE_SYSTEM".
  1274. */
  1275. if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
  1276. d_events = event_subsystem_dir(tr, call->class->system, file, parent);
  1277. if (!d_events)
  1278. return -ENOMEM;
  1279. } else
  1280. d_events = parent;
  1281. name = trace_event_name(call);
  1282. file->dir = tracefs_create_dir(name, d_events);
  1283. if (!file->dir) {
  1284. pr_warn("Could not create tracefs '%s' directory\n", name);
  1285. return -1;
  1286. }
  1287. if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
  1288. trace_create_file("enable", 0644, file->dir, file,
  1289. &ftrace_enable_fops);
  1290. #ifdef CONFIG_PERF_EVENTS
  1291. if (call->event.type && call->class->reg)
  1292. trace_create_file("id", 0444, file->dir,
  1293. (void *)(long)call->event.type,
  1294. &ftrace_event_id_fops);
  1295. #endif
  1296. /*
  1297. * Other events may have the same class. Only update
  1298. * the fields if they are not already defined.
  1299. */
  1300. head = trace_get_fields(call);
  1301. if (list_empty(head)) {
  1302. ret = call->class->define_fields(call);
  1303. if (ret < 0) {
  1304. pr_warn("Could not initialize trace point events/%s\n",
  1305. name);
  1306. return -1;
  1307. }
  1308. }
  1309. trace_create_file("filter", 0644, file->dir, file,
  1310. &ftrace_event_filter_fops);
  1311. trace_create_file("trigger", 0644, file->dir, file,
  1312. &event_trigger_fops);
  1313. trace_create_file("format", 0444, file->dir, call,
  1314. &ftrace_event_format_fops);
  1315. return 0;
  1316. }
  1317. static void remove_event_from_tracers(struct trace_event_call *call)
  1318. {
  1319. struct trace_event_file *file;
  1320. struct trace_array *tr;
  1321. do_for_each_event_file_safe(tr, file) {
  1322. if (file->event_call != call)
  1323. continue;
  1324. remove_event_file_dir(file);
  1325. /*
  1326. * The do_for_each_event_file_safe() is
  1327. * a double loop. After finding the call for this
  1328. * trace_array, we use break to jump to the next
  1329. * trace_array.
  1330. */
  1331. break;
  1332. } while_for_each_event_file();
  1333. }
  1334. static void event_remove(struct trace_event_call *call)
  1335. {
  1336. struct trace_array *tr;
  1337. struct trace_event_file *file;
  1338. do_for_each_event_file(tr, file) {
  1339. if (file->event_call != call)
  1340. continue;
  1341. ftrace_event_enable_disable(file, 0);
  1342. /*
  1343. * The do_for_each_event_file() is
  1344. * a double loop. After finding the call for this
  1345. * trace_array, we use break to jump to the next
  1346. * trace_array.
  1347. */
  1348. break;
  1349. } while_for_each_event_file();
  1350. if (call->event.funcs)
  1351. __unregister_trace_event(&call->event);
  1352. remove_event_from_tracers(call);
  1353. list_del(&call->list);
  1354. }
  1355. static int event_init(struct trace_event_call *call)
  1356. {
  1357. int ret = 0;
  1358. const char *name;
  1359. name = trace_event_name(call);
  1360. if (WARN_ON(!name))
  1361. return -EINVAL;
  1362. if (call->class->raw_init) {
  1363. ret = call->class->raw_init(call);
  1364. if (ret < 0 && ret != -ENOSYS)
  1365. pr_warn("Could not initialize trace events/%s\n", name);
  1366. }
  1367. return ret;
  1368. }
  1369. static int
  1370. __register_event(struct trace_event_call *call, struct module *mod)
  1371. {
  1372. int ret;
  1373. ret = event_init(call);
  1374. if (ret < 0)
  1375. return ret;
  1376. list_add(&call->list, &ftrace_events);
  1377. call->mod = mod;
  1378. return 0;
  1379. }
  1380. static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
  1381. {
  1382. int rlen;
  1383. int elen;
  1384. /* Find the length of the enum value as a string */
  1385. elen = snprintf(ptr, 0, "%ld", map->enum_value);
  1386. /* Make sure there's enough room to replace the string with the value */
  1387. if (len < elen)
  1388. return NULL;
  1389. snprintf(ptr, elen + 1, "%ld", map->enum_value);
  1390. /* Get the rest of the string of ptr */
  1391. rlen = strlen(ptr + len);
  1392. memmove(ptr + elen, ptr + len, rlen);
  1393. /* Make sure we end the new string */
  1394. ptr[elen + rlen] = 0;
  1395. return ptr + elen;
  1396. }
  1397. static void update_event_printk(struct trace_event_call *call,
  1398. struct trace_enum_map *map)
  1399. {
  1400. char *ptr;
  1401. int quote = 0;
  1402. int len = strlen(map->enum_string);
  1403. for (ptr = call->print_fmt; *ptr; ptr++) {
  1404. if (*ptr == '\\') {
  1405. ptr++;
  1406. /* paranoid */
  1407. if (!*ptr)
  1408. break;
  1409. continue;
  1410. }
  1411. if (*ptr == '"') {
  1412. quote ^= 1;
  1413. continue;
  1414. }
  1415. if (quote)
  1416. continue;
  1417. if (isdigit(*ptr)) {
  1418. /* skip numbers */
  1419. do {
  1420. ptr++;
  1421. /* Check for alpha chars like ULL */
  1422. } while (isalnum(*ptr));
  1423. if (!*ptr)
  1424. break;
  1425. /*
  1426. * A number must have some kind of delimiter after
  1427. * it, and we can ignore that too.
  1428. */
  1429. continue;
  1430. }
  1431. if (isalpha(*ptr) || *ptr == '_') {
  1432. if (strncmp(map->enum_string, ptr, len) == 0 &&
  1433. !isalnum(ptr[len]) && ptr[len] != '_') {
  1434. ptr = enum_replace(ptr, map, len);
  1435. /* Hmm, enum string smaller than value */
  1436. if (WARN_ON_ONCE(!ptr))
  1437. return;
  1438. /*
  1439. * No need to decrement here, as enum_replace()
  1440. * returns the pointer to the character passed
  1441. * the enum, and two enums can not be placed
  1442. * back to back without something in between.
  1443. * We can skip that something in between.
  1444. */
  1445. continue;
  1446. }
  1447. skip_more:
  1448. do {
  1449. ptr++;
  1450. } while (isalnum(*ptr) || *ptr == '_');
  1451. if (!*ptr)
  1452. break;
  1453. /*
  1454. * If what comes after this variable is a '.' or
  1455. * '->' then we can continue to ignore that string.
  1456. */
  1457. if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
  1458. ptr += *ptr == '.' ? 1 : 2;
  1459. if (!*ptr)
  1460. break;
  1461. goto skip_more;
  1462. }
  1463. /*
  1464. * Once again, we can skip the delimiter that came
  1465. * after the string.
  1466. */
  1467. continue;
  1468. }
  1469. }
  1470. }
  1471. void trace_event_enum_update(struct trace_enum_map **map, int len)
  1472. {
  1473. struct trace_event_call *call, *p;
  1474. const char *last_system = NULL;
  1475. int last_i;
  1476. int i;
  1477. down_write(&trace_event_sem);
  1478. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  1479. /* events are usually grouped together with systems */
  1480. if (!last_system || call->class->system != last_system) {
  1481. last_i = 0;
  1482. last_system = call->class->system;
  1483. }
  1484. for (i = last_i; i < len; i++) {
  1485. if (call->class->system == map[i]->system) {
  1486. /* Save the first system if need be */
  1487. if (!last_i)
  1488. last_i = i;
  1489. update_event_printk(call, map[i]);
  1490. }
  1491. }
  1492. }
  1493. up_write(&trace_event_sem);
  1494. }
  1495. static struct trace_event_file *
  1496. trace_create_new_event(struct trace_event_call *call,
  1497. struct trace_array *tr)
  1498. {
  1499. struct trace_event_file *file;
  1500. file = kmem_cache_alloc(file_cachep, GFP_TRACE);
  1501. if (!file)
  1502. return NULL;
  1503. file->event_call = call;
  1504. file->tr = tr;
  1505. atomic_set(&file->sm_ref, 0);
  1506. atomic_set(&file->tm_ref, 0);
  1507. INIT_LIST_HEAD(&file->triggers);
  1508. list_add(&file->list, &tr->events);
  1509. return file;
  1510. }
  1511. /* Add an event to a trace directory */
  1512. static int
  1513. __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
  1514. {
  1515. struct trace_event_file *file;
  1516. file = trace_create_new_event(call, tr);
  1517. if (!file)
  1518. return -ENOMEM;
  1519. return event_create_dir(tr->event_dir, file);
  1520. }
  1521. /*
  1522. * Just create a decriptor for early init. A descriptor is required
  1523. * for enabling events at boot. We want to enable events before
  1524. * the filesystem is initialized.
  1525. */
  1526. static __init int
  1527. __trace_early_add_new_event(struct trace_event_call *call,
  1528. struct trace_array *tr)
  1529. {
  1530. struct trace_event_file *file;
  1531. file = trace_create_new_event(call, tr);
  1532. if (!file)
  1533. return -ENOMEM;
  1534. return 0;
  1535. }
  1536. struct ftrace_module_file_ops;
  1537. static void __add_event_to_tracers(struct trace_event_call *call);
  1538. /* Add an additional event_call dynamically */
  1539. int trace_add_event_call(struct trace_event_call *call)
  1540. {
  1541. int ret;
  1542. mutex_lock(&trace_types_lock);
  1543. mutex_lock(&event_mutex);
  1544. ret = __register_event(call, NULL);
  1545. if (ret >= 0)
  1546. __add_event_to_tracers(call);
  1547. mutex_unlock(&event_mutex);
  1548. mutex_unlock(&trace_types_lock);
  1549. return ret;
  1550. }
  1551. /*
  1552. * Must be called under locking of trace_types_lock, event_mutex and
  1553. * trace_event_sem.
  1554. */
  1555. static void __trace_remove_event_call(struct trace_event_call *call)
  1556. {
  1557. event_remove(call);
  1558. trace_destroy_fields(call);
  1559. free_event_filter(call->filter);
  1560. call->filter = NULL;
  1561. }
  1562. static int probe_remove_event_call(struct trace_event_call *call)
  1563. {
  1564. struct trace_array *tr;
  1565. struct trace_event_file *file;
  1566. #ifdef CONFIG_PERF_EVENTS
  1567. if (call->perf_refcount)
  1568. return -EBUSY;
  1569. #endif
  1570. do_for_each_event_file(tr, file) {
  1571. if (file->event_call != call)
  1572. continue;
  1573. /*
  1574. * We can't rely on ftrace_event_enable_disable(enable => 0)
  1575. * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
  1576. * TRACE_REG_UNREGISTER.
  1577. */
  1578. if (file->flags & EVENT_FILE_FL_ENABLED)
  1579. return -EBUSY;
  1580. /*
  1581. * The do_for_each_event_file_safe() is
  1582. * a double loop. After finding the call for this
  1583. * trace_array, we use break to jump to the next
  1584. * trace_array.
  1585. */
  1586. break;
  1587. } while_for_each_event_file();
  1588. __trace_remove_event_call(call);
  1589. return 0;
  1590. }
  1591. /* Remove an event_call */
  1592. int trace_remove_event_call(struct trace_event_call *call)
  1593. {
  1594. int ret;
  1595. mutex_lock(&trace_types_lock);
  1596. mutex_lock(&event_mutex);
  1597. down_write(&trace_event_sem);
  1598. ret = probe_remove_event_call(call);
  1599. up_write(&trace_event_sem);
  1600. mutex_unlock(&event_mutex);
  1601. mutex_unlock(&trace_types_lock);
  1602. return ret;
  1603. }
  1604. #define for_each_event(event, start, end) \
  1605. for (event = start; \
  1606. (unsigned long)event < (unsigned long)end; \
  1607. event++)
  1608. #ifdef CONFIG_MODULES
  1609. static void trace_module_add_events(struct module *mod)
  1610. {
  1611. struct trace_event_call **call, **start, **end;
  1612. if (!mod->num_trace_events)
  1613. return;
  1614. /* Don't add infrastructure for mods without tracepoints */
  1615. if (trace_module_has_bad_taint(mod)) {
  1616. pr_err("%s: module has bad taint, not creating trace events\n",
  1617. mod->name);
  1618. return;
  1619. }
  1620. start = mod->trace_events;
  1621. end = mod->trace_events + mod->num_trace_events;
  1622. for_each_event(call, start, end) {
  1623. __register_event(*call, mod);
  1624. __add_event_to_tracers(*call);
  1625. }
  1626. }
  1627. static void trace_module_remove_events(struct module *mod)
  1628. {
  1629. struct trace_event_call *call, *p;
  1630. bool clear_trace = false;
  1631. down_write(&trace_event_sem);
  1632. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  1633. if (call->mod == mod) {
  1634. if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
  1635. clear_trace = true;
  1636. __trace_remove_event_call(call);
  1637. }
  1638. }
  1639. up_write(&trace_event_sem);
  1640. /*
  1641. * It is safest to reset the ring buffer if the module being unloaded
  1642. * registered any events that were used. The only worry is if
  1643. * a new module gets loaded, and takes on the same id as the events
  1644. * of this module. When printing out the buffer, traced events left
  1645. * over from this module may be passed to the new module events and
  1646. * unexpected results may occur.
  1647. */
  1648. if (clear_trace)
  1649. tracing_reset_all_online_cpus();
  1650. }
  1651. static int trace_module_notify(struct notifier_block *self,
  1652. unsigned long val, void *data)
  1653. {
  1654. struct module *mod = data;
  1655. mutex_lock(&trace_types_lock);
  1656. mutex_lock(&event_mutex);
  1657. switch (val) {
  1658. case MODULE_STATE_COMING:
  1659. trace_module_add_events(mod);
  1660. break;
  1661. case MODULE_STATE_GOING:
  1662. trace_module_remove_events(mod);
  1663. break;
  1664. }
  1665. mutex_unlock(&event_mutex);
  1666. mutex_unlock(&trace_types_lock);
  1667. return 0;
  1668. }
  1669. static struct notifier_block trace_module_nb = {
  1670. .notifier_call = trace_module_notify,
  1671. .priority = 1, /* higher than trace.c module notify */
  1672. };
  1673. #endif /* CONFIG_MODULES */
  1674. /* Create a new event directory structure for a trace directory. */
  1675. static void
  1676. __trace_add_event_dirs(struct trace_array *tr)
  1677. {
  1678. struct trace_event_call *call;
  1679. int ret;
  1680. list_for_each_entry(call, &ftrace_events, list) {
  1681. ret = __trace_add_new_event(call, tr);
  1682. if (ret < 0)
  1683. pr_warn("Could not create directory for event %s\n",
  1684. trace_event_name(call));
  1685. }
  1686. }
  1687. struct trace_event_file *
  1688. find_event_file(struct trace_array *tr, const char *system, const char *event)
  1689. {
  1690. struct trace_event_file *file;
  1691. struct trace_event_call *call;
  1692. const char *name;
  1693. list_for_each_entry(file, &tr->events, list) {
  1694. call = file->event_call;
  1695. name = trace_event_name(call);
  1696. if (!name || !call->class || !call->class->reg)
  1697. continue;
  1698. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  1699. continue;
  1700. if (strcmp(event, name) == 0 &&
  1701. strcmp(system, call->class->system) == 0)
  1702. return file;
  1703. }
  1704. return NULL;
  1705. }
  1706. #ifdef CONFIG_DYNAMIC_FTRACE
  1707. /* Avoid typos */
  1708. #define ENABLE_EVENT_STR "enable_event"
  1709. #define DISABLE_EVENT_STR "disable_event"
  1710. struct event_probe_data {
  1711. struct trace_event_file *file;
  1712. unsigned long count;
  1713. int ref;
  1714. bool enable;
  1715. };
  1716. static void
  1717. event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
  1718. {
  1719. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  1720. struct event_probe_data *data = *pdata;
  1721. if (!data)
  1722. return;
  1723. if (data->enable)
  1724. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  1725. else
  1726. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  1727. }
  1728. static void
  1729. event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
  1730. {
  1731. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  1732. struct event_probe_data *data = *pdata;
  1733. if (!data)
  1734. return;
  1735. if (!data->count)
  1736. return;
  1737. /* Skip if the event is in a state we want to switch to */
  1738. if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  1739. return;
  1740. if (data->count != -1)
  1741. (data->count)--;
  1742. event_enable_probe(ip, parent_ip, _data);
  1743. }
  1744. static int
  1745. event_enable_print(struct seq_file *m, unsigned long ip,
  1746. struct ftrace_probe_ops *ops, void *_data)
  1747. {
  1748. struct event_probe_data *data = _data;
  1749. seq_printf(m, "%ps:", (void *)ip);
  1750. seq_printf(m, "%s:%s:%s",
  1751. data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
  1752. data->file->event_call->class->system,
  1753. trace_event_name(data->file->event_call));
  1754. if (data->count == -1)
  1755. seq_puts(m, ":unlimited\n");
  1756. else
  1757. seq_printf(m, ":count=%ld\n", data->count);
  1758. return 0;
  1759. }
  1760. static int
  1761. event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
  1762. void **_data)
  1763. {
  1764. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  1765. struct event_probe_data *data = *pdata;
  1766. data->ref++;
  1767. return 0;
  1768. }
  1769. static void
  1770. event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
  1771. void **_data)
  1772. {
  1773. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  1774. struct event_probe_data *data = *pdata;
  1775. if (WARN_ON_ONCE(data->ref <= 0))
  1776. return;
  1777. data->ref--;
  1778. if (!data->ref) {
  1779. /* Remove the SOFT_MODE flag */
  1780. __ftrace_event_enable_disable(data->file, 0, 1);
  1781. module_put(data->file->event_call->mod);
  1782. kfree(data);
  1783. }
  1784. *pdata = NULL;
  1785. }
  1786. static struct ftrace_probe_ops event_enable_probe_ops = {
  1787. .func = event_enable_probe,
  1788. .print = event_enable_print,
  1789. .init = event_enable_init,
  1790. .free = event_enable_free,
  1791. };
  1792. static struct ftrace_probe_ops event_enable_count_probe_ops = {
  1793. .func = event_enable_count_probe,
  1794. .print = event_enable_print,
  1795. .init = event_enable_init,
  1796. .free = event_enable_free,
  1797. };
  1798. static struct ftrace_probe_ops event_disable_probe_ops = {
  1799. .func = event_enable_probe,
  1800. .print = event_enable_print,
  1801. .init = event_enable_init,
  1802. .free = event_enable_free,
  1803. };
  1804. static struct ftrace_probe_ops event_disable_count_probe_ops = {
  1805. .func = event_enable_count_probe,
  1806. .print = event_enable_print,
  1807. .init = event_enable_init,
  1808. .free = event_enable_free,
  1809. };
  1810. static int
  1811. event_enable_func(struct ftrace_hash *hash,
  1812. char *glob, char *cmd, char *param, int enabled)
  1813. {
  1814. struct trace_array *tr = top_trace_array();
  1815. struct trace_event_file *file;
  1816. struct ftrace_probe_ops *ops;
  1817. struct event_probe_data *data;
  1818. const char *system;
  1819. const char *event;
  1820. char *number;
  1821. bool enable;
  1822. int ret;
  1823. if (!tr)
  1824. return -ENODEV;
  1825. /* hash funcs only work with set_ftrace_filter */
  1826. if (!enabled || !param)
  1827. return -EINVAL;
  1828. system = strsep(&param, ":");
  1829. if (!param)
  1830. return -EINVAL;
  1831. event = strsep(&param, ":");
  1832. mutex_lock(&event_mutex);
  1833. ret = -EINVAL;
  1834. file = find_event_file(tr, system, event);
  1835. if (!file)
  1836. goto out;
  1837. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  1838. if (enable)
  1839. ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
  1840. else
  1841. ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
  1842. if (glob[0] == '!') {
  1843. unregister_ftrace_function_probe_func(glob+1, ops);
  1844. ret = 0;
  1845. goto out;
  1846. }
  1847. ret = -ENOMEM;
  1848. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1849. if (!data)
  1850. goto out;
  1851. data->enable = enable;
  1852. data->count = -1;
  1853. data->file = file;
  1854. if (!param)
  1855. goto out_reg;
  1856. number = strsep(&param, ":");
  1857. ret = -EINVAL;
  1858. if (!strlen(number))
  1859. goto out_free;
  1860. /*
  1861. * We use the callback data field (which is a pointer)
  1862. * as our counter.
  1863. */
  1864. ret = kstrtoul(number, 0, &data->count);
  1865. if (ret)
  1866. goto out_free;
  1867. out_reg:
  1868. /* Don't let event modules unload while probe registered */
  1869. ret = try_module_get(file->event_call->mod);
  1870. if (!ret) {
  1871. ret = -EBUSY;
  1872. goto out_free;
  1873. }
  1874. ret = __ftrace_event_enable_disable(file, 1, 1);
  1875. if (ret < 0)
  1876. goto out_put;
  1877. ret = register_ftrace_function_probe(glob, ops, data);
  1878. /*
  1879. * The above returns on success the # of functions enabled,
  1880. * but if it didn't find any functions it returns zero.
  1881. * Consider no functions a failure too.
  1882. */
  1883. if (!ret) {
  1884. ret = -ENOENT;
  1885. goto out_disable;
  1886. } else if (ret < 0)
  1887. goto out_disable;
  1888. /* Just return zero, not the number of enabled functions */
  1889. ret = 0;
  1890. out:
  1891. mutex_unlock(&event_mutex);
  1892. return ret;
  1893. out_disable:
  1894. __ftrace_event_enable_disable(file, 0, 1);
  1895. out_put:
  1896. module_put(file->event_call->mod);
  1897. out_free:
  1898. kfree(data);
  1899. goto out;
  1900. }
  1901. static struct ftrace_func_command event_enable_cmd = {
  1902. .name = ENABLE_EVENT_STR,
  1903. .func = event_enable_func,
  1904. };
  1905. static struct ftrace_func_command event_disable_cmd = {
  1906. .name = DISABLE_EVENT_STR,
  1907. .func = event_enable_func,
  1908. };
  1909. static __init int register_event_cmds(void)
  1910. {
  1911. int ret;
  1912. ret = register_ftrace_command(&event_enable_cmd);
  1913. if (WARN_ON(ret < 0))
  1914. return ret;
  1915. ret = register_ftrace_command(&event_disable_cmd);
  1916. if (WARN_ON(ret < 0))
  1917. unregister_ftrace_command(&event_enable_cmd);
  1918. return ret;
  1919. }
  1920. #else
  1921. static inline int register_event_cmds(void) { return 0; }
  1922. #endif /* CONFIG_DYNAMIC_FTRACE */
  1923. /*
  1924. * The top level array has already had its trace_event_file
  1925. * descriptors created in order to allow for early events to
  1926. * be recorded. This function is called after the tracefs has been
  1927. * initialized, and we now have to create the files associated
  1928. * to the events.
  1929. */
  1930. static __init void
  1931. __trace_early_add_event_dirs(struct trace_array *tr)
  1932. {
  1933. struct trace_event_file *file;
  1934. int ret;
  1935. list_for_each_entry(file, &tr->events, list) {
  1936. ret = event_create_dir(tr->event_dir, file);
  1937. if (ret < 0)
  1938. pr_warn("Could not create directory for event %s\n",
  1939. trace_event_name(file->event_call));
  1940. }
  1941. }
  1942. /*
  1943. * For early boot up, the top trace array requires to have
  1944. * a list of events that can be enabled. This must be done before
  1945. * the filesystem is set up in order to allow events to be traced
  1946. * early.
  1947. */
  1948. static __init void
  1949. __trace_early_add_events(struct trace_array *tr)
  1950. {
  1951. struct trace_event_call *call;
  1952. int ret;
  1953. list_for_each_entry(call, &ftrace_events, list) {
  1954. /* Early boot up should not have any modules loaded */
  1955. if (WARN_ON_ONCE(call->mod))
  1956. continue;
  1957. ret = __trace_early_add_new_event(call, tr);
  1958. if (ret < 0)
  1959. pr_warn("Could not create early event %s\n",
  1960. trace_event_name(call));
  1961. }
  1962. }
  1963. /* Remove the event directory structure for a trace directory. */
  1964. static void
  1965. __trace_remove_event_dirs(struct trace_array *tr)
  1966. {
  1967. struct trace_event_file *file, *next;
  1968. list_for_each_entry_safe(file, next, &tr->events, list)
  1969. remove_event_file_dir(file);
  1970. }
  1971. static void __add_event_to_tracers(struct trace_event_call *call)
  1972. {
  1973. struct trace_array *tr;
  1974. list_for_each_entry(tr, &ftrace_trace_arrays, list)
  1975. __trace_add_new_event(call, tr);
  1976. }
  1977. extern struct trace_event_call *__start_ftrace_events[];
  1978. extern struct trace_event_call *__stop_ftrace_events[];
  1979. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  1980. static __init int setup_trace_event(char *str)
  1981. {
  1982. strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  1983. ring_buffer_expanded = true;
  1984. tracing_selftest_disabled = true;
  1985. return 1;
  1986. }
  1987. __setup("trace_event=", setup_trace_event);
  1988. /* Expects to have event_mutex held when called */
  1989. static int
  1990. create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
  1991. {
  1992. struct dentry *d_events;
  1993. struct dentry *entry;
  1994. entry = tracefs_create_file("set_event", 0644, parent,
  1995. tr, &ftrace_set_event_fops);
  1996. if (!entry) {
  1997. pr_warn("Could not create tracefs 'set_event' entry\n");
  1998. return -ENOMEM;
  1999. }
  2000. d_events = tracefs_create_dir("events", parent);
  2001. if (!d_events) {
  2002. pr_warn("Could not create tracefs 'events' directory\n");
  2003. return -ENOMEM;
  2004. }
  2005. /* ring buffer internal formats */
  2006. trace_create_file("header_page", 0444, d_events,
  2007. ring_buffer_print_page_header,
  2008. &ftrace_show_header_fops);
  2009. trace_create_file("header_event", 0444, d_events,
  2010. ring_buffer_print_entry_header,
  2011. &ftrace_show_header_fops);
  2012. trace_create_file("enable", 0644, d_events,
  2013. tr, &ftrace_tr_enable_fops);
  2014. tr->event_dir = d_events;
  2015. return 0;
  2016. }
  2017. /**
  2018. * event_trace_add_tracer - add a instance of a trace_array to events
  2019. * @parent: The parent dentry to place the files/directories for events in
  2020. * @tr: The trace array associated with these events
  2021. *
  2022. * When a new instance is created, it needs to set up its events
  2023. * directory, as well as other files associated with events. It also
  2024. * creates the event hierachry in the @parent/events directory.
  2025. *
  2026. * Returns 0 on success.
  2027. */
  2028. int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
  2029. {
  2030. int ret;
  2031. mutex_lock(&event_mutex);
  2032. ret = create_event_toplevel_files(parent, tr);
  2033. if (ret)
  2034. goto out_unlock;
  2035. down_write(&trace_event_sem);
  2036. __trace_add_event_dirs(tr);
  2037. up_write(&trace_event_sem);
  2038. out_unlock:
  2039. mutex_unlock(&event_mutex);
  2040. return ret;
  2041. }
  2042. /*
  2043. * The top trace array already had its file descriptors created.
  2044. * Now the files themselves need to be created.
  2045. */
  2046. static __init int
  2047. early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
  2048. {
  2049. int ret;
  2050. mutex_lock(&event_mutex);
  2051. ret = create_event_toplevel_files(parent, tr);
  2052. if (ret)
  2053. goto out_unlock;
  2054. down_write(&trace_event_sem);
  2055. __trace_early_add_event_dirs(tr);
  2056. up_write(&trace_event_sem);
  2057. out_unlock:
  2058. mutex_unlock(&event_mutex);
  2059. return ret;
  2060. }
  2061. int event_trace_del_tracer(struct trace_array *tr)
  2062. {
  2063. mutex_lock(&event_mutex);
  2064. /* Disable any event triggers and associated soft-disabled events */
  2065. clear_event_triggers(tr);
  2066. /* Disable any running events */
  2067. __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
  2068. /* Access to events are within rcu_read_lock_sched() */
  2069. synchronize_sched();
  2070. down_write(&trace_event_sem);
  2071. __trace_remove_event_dirs(tr);
  2072. tracefs_remove_recursive(tr->event_dir);
  2073. up_write(&trace_event_sem);
  2074. tr->event_dir = NULL;
  2075. mutex_unlock(&event_mutex);
  2076. return 0;
  2077. }
  2078. static __init int event_trace_memsetup(void)
  2079. {
  2080. field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
  2081. file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
  2082. return 0;
  2083. }
  2084. static __init void
  2085. early_enable_events(struct trace_array *tr, bool disable_first)
  2086. {
  2087. char *buf = bootup_event_buf;
  2088. char *token;
  2089. int ret;
  2090. while (true) {
  2091. token = strsep(&buf, ",");
  2092. if (!token)
  2093. break;
  2094. if (!*token)
  2095. continue;
  2096. /* Restarting syscalls requires that we stop them first */
  2097. if (disable_first)
  2098. ftrace_set_clr_event(tr, token, 0);
  2099. ret = ftrace_set_clr_event(tr, token, 1);
  2100. if (ret)
  2101. pr_warn("Failed to enable trace event: %s\n", token);
  2102. /* Put back the comma to allow this to be called again */
  2103. if (buf)
  2104. *(buf - 1) = ',';
  2105. }
  2106. }
  2107. static __init int event_trace_enable(void)
  2108. {
  2109. struct trace_array *tr = top_trace_array();
  2110. struct trace_event_call **iter, *call;
  2111. int ret;
  2112. if (!tr)
  2113. return -ENODEV;
  2114. for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
  2115. call = *iter;
  2116. ret = event_init(call);
  2117. if (!ret)
  2118. list_add(&call->list, &ftrace_events);
  2119. }
  2120. /*
  2121. * We need the top trace array to have a working set of trace
  2122. * points at early init, before the debug files and directories
  2123. * are created. Create the file entries now, and attach them
  2124. * to the actual file dentries later.
  2125. */
  2126. __trace_early_add_events(tr);
  2127. early_enable_events(tr, false);
  2128. trace_printk_start_comm();
  2129. register_event_cmds();
  2130. register_trigger_cmds();
  2131. return 0;
  2132. }
  2133. /*
  2134. * event_trace_enable() is called from trace_event_init() first to
  2135. * initialize events and perhaps start any events that are on the
  2136. * command line. Unfortunately, there are some events that will not
  2137. * start this early, like the system call tracepoints that need
  2138. * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
  2139. * is called before pid 1 starts, and this flag is never set, making
  2140. * the syscall tracepoint never get reached, but the event is enabled
  2141. * regardless (and not doing anything).
  2142. */
  2143. static __init int event_trace_enable_again(void)
  2144. {
  2145. struct trace_array *tr;
  2146. tr = top_trace_array();
  2147. if (!tr)
  2148. return -ENODEV;
  2149. early_enable_events(tr, true);
  2150. return 0;
  2151. }
  2152. early_initcall(event_trace_enable_again);
  2153. static __init int event_trace_init(void)
  2154. {
  2155. struct trace_array *tr;
  2156. struct dentry *d_tracer;
  2157. struct dentry *entry;
  2158. int ret;
  2159. tr = top_trace_array();
  2160. if (!tr)
  2161. return -ENODEV;
  2162. d_tracer = tracing_init_dentry();
  2163. if (IS_ERR(d_tracer))
  2164. return 0;
  2165. entry = tracefs_create_file("available_events", 0444, d_tracer,
  2166. tr, &ftrace_avail_fops);
  2167. if (!entry)
  2168. pr_warn("Could not create tracefs 'available_events' entry\n");
  2169. if (trace_define_common_fields())
  2170. pr_warn("tracing: Failed to allocate common fields");
  2171. ret = early_event_add_tracer(d_tracer, tr);
  2172. if (ret)
  2173. return ret;
  2174. #ifdef CONFIG_MODULES
  2175. ret = register_module_notifier(&trace_module_nb);
  2176. if (ret)
  2177. pr_warn("Failed to register trace events module notifier\n");
  2178. #endif
  2179. return 0;
  2180. }
  2181. void __init trace_event_init(void)
  2182. {
  2183. event_trace_memsetup();
  2184. init_ftrace_syscalls();
  2185. event_trace_enable();
  2186. }
  2187. fs_initcall(event_trace_init);
  2188. #ifdef CONFIG_FTRACE_STARTUP_TEST
  2189. static DEFINE_SPINLOCK(test_spinlock);
  2190. static DEFINE_SPINLOCK(test_spinlock_irq);
  2191. static DEFINE_MUTEX(test_mutex);
  2192. static __init void test_work(struct work_struct *dummy)
  2193. {
  2194. spin_lock(&test_spinlock);
  2195. spin_lock_irq(&test_spinlock_irq);
  2196. udelay(1);
  2197. spin_unlock_irq(&test_spinlock_irq);
  2198. spin_unlock(&test_spinlock);
  2199. mutex_lock(&test_mutex);
  2200. msleep(1);
  2201. mutex_unlock(&test_mutex);
  2202. }
  2203. static __init int event_test_thread(void *unused)
  2204. {
  2205. void *test_malloc;
  2206. test_malloc = kmalloc(1234, GFP_KERNEL);
  2207. if (!test_malloc)
  2208. pr_info("failed to kmalloc\n");
  2209. schedule_on_each_cpu(test_work);
  2210. kfree(test_malloc);
  2211. set_current_state(TASK_INTERRUPTIBLE);
  2212. while (!kthread_should_stop()) {
  2213. schedule();
  2214. set_current_state(TASK_INTERRUPTIBLE);
  2215. }
  2216. __set_current_state(TASK_RUNNING);
  2217. return 0;
  2218. }
  2219. /*
  2220. * Do various things that may trigger events.
  2221. */
  2222. static __init void event_test_stuff(void)
  2223. {
  2224. struct task_struct *test_thread;
  2225. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  2226. msleep(1);
  2227. kthread_stop(test_thread);
  2228. }
  2229. /*
  2230. * For every trace event defined, we will test each trace point separately,
  2231. * and then by groups, and finally all trace points.
  2232. */
  2233. static __init void event_trace_self_tests(void)
  2234. {
  2235. struct trace_subsystem_dir *dir;
  2236. struct trace_event_file *file;
  2237. struct trace_event_call *call;
  2238. struct event_subsystem *system;
  2239. struct trace_array *tr;
  2240. int ret;
  2241. tr = top_trace_array();
  2242. if (!tr)
  2243. return;
  2244. pr_info("Running tests on trace events:\n");
  2245. list_for_each_entry(file, &tr->events, list) {
  2246. call = file->event_call;
  2247. /* Only test those that have a probe */
  2248. if (!call->class || !call->class->probe)
  2249. continue;
  2250. /*
  2251. * Testing syscall events here is pretty useless, but
  2252. * we still do it if configured. But this is time consuming.
  2253. * What we really need is a user thread to perform the
  2254. * syscalls as we test.
  2255. */
  2256. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  2257. if (call->class->system &&
  2258. strcmp(call->class->system, "syscalls") == 0)
  2259. continue;
  2260. #endif
  2261. pr_info("Testing event %s: ", trace_event_name(call));
  2262. /*
  2263. * If an event is already enabled, someone is using
  2264. * it and the self test should not be on.
  2265. */
  2266. if (file->flags & EVENT_FILE_FL_ENABLED) {
  2267. pr_warn("Enabled event during self test!\n");
  2268. WARN_ON_ONCE(1);
  2269. continue;
  2270. }
  2271. ftrace_event_enable_disable(file, 1);
  2272. event_test_stuff();
  2273. ftrace_event_enable_disable(file, 0);
  2274. pr_cont("OK\n");
  2275. }
  2276. /* Now test at the sub system level */
  2277. pr_info("Running tests on trace event systems:\n");
  2278. list_for_each_entry(dir, &tr->systems, list) {
  2279. system = dir->subsystem;
  2280. /* the ftrace system is special, skip it */
  2281. if (strcmp(system->name, "ftrace") == 0)
  2282. continue;
  2283. pr_info("Testing event system %s: ", system->name);
  2284. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
  2285. if (WARN_ON_ONCE(ret)) {
  2286. pr_warn("error enabling system %s\n",
  2287. system->name);
  2288. continue;
  2289. }
  2290. event_test_stuff();
  2291. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
  2292. if (WARN_ON_ONCE(ret)) {
  2293. pr_warn("error disabling system %s\n",
  2294. system->name);
  2295. continue;
  2296. }
  2297. pr_cont("OK\n");
  2298. }
  2299. /* Test with all events enabled */
  2300. pr_info("Running tests on all trace events:\n");
  2301. pr_info("Testing all events: ");
  2302. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
  2303. if (WARN_ON_ONCE(ret)) {
  2304. pr_warn("error enabling all events\n");
  2305. return;
  2306. }
  2307. event_test_stuff();
  2308. /* reset sysname */
  2309. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
  2310. if (WARN_ON_ONCE(ret)) {
  2311. pr_warn("error disabling all events\n");
  2312. return;
  2313. }
  2314. pr_cont("OK\n");
  2315. }
  2316. #ifdef CONFIG_FUNCTION_TRACER
  2317. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  2318. static void
  2319. function_test_events_call(unsigned long ip, unsigned long parent_ip,
  2320. struct ftrace_ops *op, struct pt_regs *pt_regs)
  2321. {
  2322. struct ring_buffer_event *event;
  2323. struct ring_buffer *buffer;
  2324. struct ftrace_entry *entry;
  2325. unsigned long flags;
  2326. long disabled;
  2327. int cpu;
  2328. int pc;
  2329. pc = preempt_count();
  2330. preempt_disable_notrace();
  2331. cpu = raw_smp_processor_id();
  2332. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  2333. if (disabled != 1)
  2334. goto out;
  2335. local_save_flags(flags);
  2336. event = trace_current_buffer_lock_reserve(&buffer,
  2337. TRACE_FN, sizeof(*entry),
  2338. flags, pc);
  2339. if (!event)
  2340. goto out;
  2341. entry = ring_buffer_event_data(event);
  2342. entry->ip = ip;
  2343. entry->parent_ip = parent_ip;
  2344. trace_buffer_unlock_commit(buffer, event, flags, pc);
  2345. out:
  2346. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  2347. preempt_enable_notrace();
  2348. }
  2349. static struct ftrace_ops trace_ops __initdata =
  2350. {
  2351. .func = function_test_events_call,
  2352. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  2353. };
  2354. static __init void event_trace_self_test_with_function(void)
  2355. {
  2356. int ret;
  2357. ret = register_ftrace_function(&trace_ops);
  2358. if (WARN_ON(ret < 0)) {
  2359. pr_info("Failed to enable function tracer for event tests\n");
  2360. return;
  2361. }
  2362. pr_info("Running tests again, along with the function tracer\n");
  2363. event_trace_self_tests();
  2364. unregister_ftrace_function(&trace_ops);
  2365. }
  2366. #else
  2367. static __init void event_trace_self_test_with_function(void)
  2368. {
  2369. }
  2370. #endif
  2371. static __init int event_trace_self_tests_init(void)
  2372. {
  2373. if (!tracing_selftest_disabled) {
  2374. event_trace_self_tests();
  2375. event_trace_self_test_with_function();
  2376. }
  2377. return 0;
  2378. }
  2379. late_initcall(event_trace_self_tests_init);
  2380. #endif