trace_events.c 58 KB

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