trace_events.c 60 KB

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