trace_events.c 77 KB

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