trace_events.c 78 KB

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