trace_events.c 62 KB

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