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