trace_events.c 59 KB

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