trace_events.c 59 KB

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