trace_events.c 62 KB

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