trace_events.c 58 KB

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