trace_events.c 66 KB

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