trace_events.c 59 KB

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