trace_events.c 59 KB

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