trace_events.c 79 KB

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