trace_events.c 59 KB

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