trace_events.c 77 KB

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