trace_events.c 62 KB

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