trace_events.c 62 KB

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