trace_events.c 60 KB

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