trace_events.c 63 KB

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