trace_events.c 77 KB

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