trace_events.c 59 KB

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