trace_events.c 62 KB

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