trace_events.c 60 KB

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