trace_events.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * event tracer
  4. *
  5. * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  6. *
  7. * - Added format output of fields of the trace point.
  8. * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
  9. *
  10. */
  11. #define pr_fmt(fmt) fmt
  12. #include <linux/workqueue.h>
  13. #include <linux/security.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/kthread.h>
  16. #include <linux/tracefs.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/module.h>
  19. #include <linux/ctype.h>
  20. #include <linux/sort.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <trace/events/sched.h>
  24. #include <trace/syscall.h>
  25. #include <asm/setup.h>
  26. #include "trace_output.h"
  27. #undef TRACE_SYSTEM
  28. #define TRACE_SYSTEM "TRACE_SYSTEM"
  29. DEFINE_MUTEX(event_mutex);
  30. LIST_HEAD(ftrace_events);
  31. static LIST_HEAD(ftrace_generic_fields);
  32. static LIST_HEAD(ftrace_common_fields);
  33. static bool eventdir_initialized;
  34. static LIST_HEAD(module_strings);
  35. struct module_string {
  36. struct list_head next;
  37. struct module *module;
  38. char *str;
  39. };
  40. #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
  41. static struct kmem_cache *field_cachep;
  42. static struct kmem_cache *file_cachep;
  43. static inline int system_refcount(struct event_subsystem *system)
  44. {
  45. return system->ref_count;
  46. }
  47. static int system_refcount_inc(struct event_subsystem *system)
  48. {
  49. return system->ref_count++;
  50. }
  51. static int system_refcount_dec(struct event_subsystem *system)
  52. {
  53. return --system->ref_count;
  54. }
  55. /* Double loops, do not use break, only goto's work */
  56. #define do_for_each_event_file(tr, file) \
  57. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  58. list_for_each_entry(file, &tr->events, list)
  59. #define do_for_each_event_file_safe(tr, file) \
  60. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  61. struct trace_event_file *___n; \
  62. list_for_each_entry_safe(file, ___n, &tr->events, list)
  63. #define while_for_each_event_file() \
  64. }
  65. static struct ftrace_event_field *
  66. __find_event_field(struct list_head *head, const char *name)
  67. {
  68. struct ftrace_event_field *field;
  69. list_for_each_entry(field, head, link) {
  70. if (!strcmp(field->name, name))
  71. return field;
  72. }
  73. return NULL;
  74. }
  75. struct ftrace_event_field *
  76. trace_find_event_field(struct trace_event_call *call, char *name)
  77. {
  78. struct ftrace_event_field *field;
  79. struct list_head *head;
  80. head = trace_get_fields(call);
  81. field = __find_event_field(head, name);
  82. if (field)
  83. return field;
  84. field = __find_event_field(&ftrace_generic_fields, name);
  85. if (field)
  86. return field;
  87. return __find_event_field(&ftrace_common_fields, name);
  88. }
  89. static int __trace_define_field(struct list_head *head, const char *type,
  90. const char *name, int offset, int size,
  91. int is_signed, int filter_type, int len,
  92. int need_test)
  93. {
  94. struct ftrace_event_field *field;
  95. field = kmem_cache_alloc(field_cachep, GFP_TRACE);
  96. if (!field)
  97. return -ENOMEM;
  98. field->name = name;
  99. field->type = type;
  100. if (filter_type == FILTER_OTHER)
  101. field->filter_type = filter_assign_type(type);
  102. else
  103. field->filter_type = filter_type;
  104. field->offset = offset;
  105. field->size = size;
  106. field->is_signed = is_signed;
  107. field->needs_test = need_test;
  108. field->len = len;
  109. list_add(&field->link, head);
  110. return 0;
  111. }
  112. int trace_define_field(struct trace_event_call *call, const char *type,
  113. const char *name, int offset, int size, int is_signed,
  114. int filter_type)
  115. {
  116. struct list_head *head;
  117. if (WARN_ON(!call->class))
  118. return 0;
  119. head = trace_get_fields(call);
  120. return __trace_define_field(head, type, name, offset, size,
  121. is_signed, filter_type, 0, 0);
  122. }
  123. EXPORT_SYMBOL_GPL(trace_define_field);
  124. static int trace_define_field_ext(struct trace_event_call *call, const char *type,
  125. const char *name, int offset, int size, int is_signed,
  126. int filter_type, int len, int need_test)
  127. {
  128. struct list_head *head;
  129. if (WARN_ON(!call->class))
  130. return 0;
  131. head = trace_get_fields(call);
  132. return __trace_define_field(head, type, name, offset, size,
  133. is_signed, filter_type, len, need_test);
  134. }
  135. #define __generic_field(type, item, filter_type) \
  136. ret = __trace_define_field(&ftrace_generic_fields, #type, \
  137. #item, 0, 0, is_signed_type(type), \
  138. filter_type, 0, 0); \
  139. if (ret) \
  140. return ret;
  141. #define __common_field(type, item) \
  142. ret = __trace_define_field(&ftrace_common_fields, #type, \
  143. "common_" #item, \
  144. offsetof(typeof(ent), item), \
  145. sizeof(ent.item), \
  146. is_signed_type(type), FILTER_OTHER, \
  147. 0, 0); \
  148. if (ret) \
  149. return ret;
  150. static int trace_define_generic_fields(void)
  151. {
  152. int ret;
  153. __generic_field(int, CPU, FILTER_CPU);
  154. __generic_field(int, cpu, FILTER_CPU);
  155. __generic_field(int, common_cpu, FILTER_CPU);
  156. __generic_field(char *, COMM, FILTER_COMM);
  157. __generic_field(char *, comm, FILTER_COMM);
  158. __generic_field(char *, stacktrace, FILTER_STACKTRACE);
  159. __generic_field(char *, STACKTRACE, FILTER_STACKTRACE);
  160. return ret;
  161. }
  162. static int trace_define_common_fields(void)
  163. {
  164. int ret;
  165. struct trace_entry ent;
  166. __common_field(unsigned short, type);
  167. __common_field(unsigned char, flags);
  168. /* Holds both preempt_count and migrate_disable */
  169. __common_field(unsigned char, preempt_count);
  170. __common_field(int, pid);
  171. return ret;
  172. }
  173. static void trace_destroy_fields(struct trace_event_call *call)
  174. {
  175. struct ftrace_event_field *field, *next;
  176. struct list_head *head;
  177. head = trace_get_fields(call);
  178. list_for_each_entry_safe(field, next, head, link) {
  179. list_del(&field->link);
  180. kmem_cache_free(field_cachep, field);
  181. }
  182. }
  183. /*
  184. * run-time version of trace_event_get_offsets_<call>() that returns the last
  185. * accessible offset of trace fields excluding __dynamic_array bytes
  186. */
  187. int trace_event_get_offsets(struct trace_event_call *call)
  188. {
  189. struct ftrace_event_field *tail;
  190. struct list_head *head;
  191. head = trace_get_fields(call);
  192. /*
  193. * head->next points to the last field with the largest offset,
  194. * since it was added last by trace_define_field()
  195. */
  196. tail = list_first_entry(head, struct ftrace_event_field, link);
  197. return tail->offset + tail->size;
  198. }
  199. static struct trace_event_fields *find_event_field(const char *fmt,
  200. struct trace_event_call *call)
  201. {
  202. struct trace_event_fields *field = call->class->fields_array;
  203. const char *p = fmt;
  204. int len;
  205. if (!(len = str_has_prefix(fmt, "REC->")))
  206. return NULL;
  207. fmt += len;
  208. for (p = fmt; *p; p++) {
  209. if (!isalnum(*p) && *p != '_')
  210. break;
  211. }
  212. len = p - fmt;
  213. for (; field->type; field++) {
  214. if (strncmp(field->name, fmt, len) || field->name[len])
  215. continue;
  216. return field;
  217. }
  218. return NULL;
  219. }
  220. /*
  221. * Check if the referenced field is an array and return true,
  222. * as arrays are OK to dereference.
  223. */
  224. static bool test_field(const char *fmt, struct trace_event_call *call)
  225. {
  226. struct trace_event_fields *field;
  227. field = find_event_field(fmt, call);
  228. if (!field)
  229. return false;
  230. /* This is an array and is OK to dereference. */
  231. return strchr(field->type, '[') != NULL;
  232. }
  233. /* Look for a string within an argument */
  234. static bool find_print_string(const char *arg, const char *str, const char *end)
  235. {
  236. const char *r;
  237. r = strstr(arg, str);
  238. return r && r < end;
  239. }
  240. /* Return true if the argument pointer is safe */
  241. static bool process_pointer(const char *fmt, int len, struct trace_event_call *call)
  242. {
  243. const char *r, *e, *a;
  244. e = fmt + len;
  245. /* Find the REC-> in the argument */
  246. r = strstr(fmt, "REC->");
  247. if (r && r < e) {
  248. /*
  249. * Addresses of events on the buffer, or an array on the buffer is
  250. * OK to dereference. There's ways to fool this, but
  251. * this is to catch common mistakes, not malicious code.
  252. */
  253. a = strchr(fmt, '&');
  254. if ((a && (a < r)) || test_field(r, call))
  255. return true;
  256. } else if (find_print_string(fmt, "__get_dynamic_array(", e)) {
  257. return true;
  258. } else if (find_print_string(fmt, "__get_rel_dynamic_array(", e)) {
  259. return true;
  260. } else if (find_print_string(fmt, "__get_dynamic_array_len(", e)) {
  261. return true;
  262. } else if (find_print_string(fmt, "__get_rel_dynamic_array_len(", e)) {
  263. return true;
  264. } else if (find_print_string(fmt, "__get_sockaddr(", e)) {
  265. return true;
  266. } else if (find_print_string(fmt, "__get_rel_sockaddr(", e)) {
  267. return true;
  268. }
  269. return false;
  270. }
  271. /* Return true if the string is safe */
  272. static bool process_string(const char *fmt, int len, struct trace_event_call *call)
  273. {
  274. struct trace_event_fields *field;
  275. const char *r, *e, *s;
  276. e = fmt + len;
  277. /*
  278. * There are several helper functions that return strings.
  279. * If the argument contains a function, then assume its field is valid.
  280. * It is considered that the argument has a function if it has:
  281. * alphanumeric or '_' before a parenthesis.
  282. */
  283. s = fmt;
  284. do {
  285. r = strstr(s, "(");
  286. if (!r || r >= e)
  287. break;
  288. for (int i = 1; r - i >= s; i++) {
  289. char ch = *(r - i);
  290. if (isspace(ch))
  291. continue;
  292. if (isalnum(ch) || ch == '_')
  293. return true;
  294. /* Anything else, this isn't a function */
  295. break;
  296. }
  297. /* A function could be wrapped in parethesis, try the next one */
  298. s = r + 1;
  299. } while (s < e);
  300. /*
  301. * Check for arrays. If the argument has: foo[REC->val]
  302. * then it is very likely that foo is an array of strings
  303. * that are safe to use.
  304. */
  305. r = strstr(s, "[");
  306. if (r && r < e) {
  307. r = strstr(r, "REC->");
  308. if (r && r < e)
  309. return true;
  310. }
  311. /*
  312. * If there's any strings in the argument consider this arg OK as it
  313. * could be: REC->field ? "foo" : "bar" and we don't want to get into
  314. * verifying that logic here.
  315. */
  316. if (find_print_string(fmt, "\"", e))
  317. return true;
  318. /* Dereferenced strings are also valid like any other pointer */
  319. if (process_pointer(fmt, len, call))
  320. return true;
  321. /* Make sure the field is found */
  322. field = find_event_field(fmt, call);
  323. if (!field)
  324. return false;
  325. /* Test this field's string before printing the event */
  326. call->flags |= TRACE_EVENT_FL_TEST_STR;
  327. field->needs_test = 1;
  328. return true;
  329. }
  330. /*
  331. * Examine the print fmt of the event looking for unsafe dereference
  332. * pointers using %p* that could be recorded in the trace event and
  333. * much later referenced after the pointer was freed. Dereferencing
  334. * pointers are OK, if it is dereferenced into the event itself.
  335. */
  336. static void test_event_printk(struct trace_event_call *call)
  337. {
  338. u64 dereference_flags = 0;
  339. u64 string_flags = 0;
  340. bool first = true;
  341. const char *fmt;
  342. int parens = 0;
  343. char in_quote = 0;
  344. int start_arg = 0;
  345. int arg = 0;
  346. int i, e;
  347. fmt = call->print_fmt;
  348. if (!fmt)
  349. return;
  350. for (i = 0; fmt[i]; i++) {
  351. switch (fmt[i]) {
  352. case '\\':
  353. i++;
  354. if (!fmt[i])
  355. return;
  356. continue;
  357. case '"':
  358. case '\'':
  359. /*
  360. * The print fmt starts with a string that
  361. * is processed first to find %p* usage,
  362. * then after the first string, the print fmt
  363. * contains arguments that are used to check
  364. * if the dereferenced %p* usage is safe.
  365. */
  366. if (first) {
  367. if (fmt[i] == '\'')
  368. continue;
  369. if (in_quote) {
  370. arg = 0;
  371. first = false;
  372. /*
  373. * If there was no %p* uses
  374. * the fmt is OK.
  375. */
  376. if (!dereference_flags)
  377. return;
  378. }
  379. }
  380. if (in_quote) {
  381. if (in_quote == fmt[i])
  382. in_quote = 0;
  383. } else {
  384. in_quote = fmt[i];
  385. }
  386. continue;
  387. case '%':
  388. if (!first || !in_quote)
  389. continue;
  390. i++;
  391. if (!fmt[i])
  392. return;
  393. switch (fmt[i]) {
  394. case '%':
  395. continue;
  396. case 'p':
  397. do_pointer:
  398. /* Find dereferencing fields */
  399. switch (fmt[i + 1]) {
  400. case 'B': case 'R': case 'r':
  401. case 'b': case 'M': case 'm':
  402. case 'I': case 'i': case 'E':
  403. case 'U': case 'V': case 'N':
  404. case 'a': case 'd': case 'D':
  405. case 'g': case 't': case 'C':
  406. case 'O': case 'f':
  407. if (WARN_ONCE(arg == 63,
  408. "Too many args for event: %s",
  409. trace_event_name(call)))
  410. return;
  411. dereference_flags |= 1ULL << arg;
  412. }
  413. break;
  414. default:
  415. {
  416. bool star = false;
  417. int j;
  418. /* Increment arg if %*s exists. */
  419. for (j = 0; fmt[i + j]; j++) {
  420. if (isdigit(fmt[i + j]) ||
  421. fmt[i + j] == '.')
  422. continue;
  423. if (fmt[i + j] == '*') {
  424. star = true;
  425. /* Handle %*pbl case */
  426. if (!j && fmt[i + 1] == 'p') {
  427. arg++;
  428. i++;
  429. goto do_pointer;
  430. }
  431. continue;
  432. }
  433. if ((fmt[i + j] == 's')) {
  434. if (star)
  435. arg++;
  436. if (WARN_ONCE(arg == 63,
  437. "Too many args for event: %s",
  438. trace_event_name(call)))
  439. return;
  440. dereference_flags |= 1ULL << arg;
  441. string_flags |= 1ULL << arg;
  442. }
  443. break;
  444. }
  445. break;
  446. } /* default */
  447. } /* switch */
  448. arg++;
  449. continue;
  450. case '(':
  451. if (in_quote)
  452. continue;
  453. parens++;
  454. continue;
  455. case ')':
  456. if (in_quote)
  457. continue;
  458. parens--;
  459. if (WARN_ONCE(parens < 0,
  460. "Paren mismatch for event: %s\narg='%s'\n%*s",
  461. trace_event_name(call),
  462. fmt + start_arg,
  463. (i - start_arg) + 5, "^"))
  464. return;
  465. continue;
  466. case ',':
  467. if (in_quote || parens)
  468. continue;
  469. e = i;
  470. i++;
  471. while (isspace(fmt[i]))
  472. i++;
  473. /*
  474. * If start_arg is zero, then this is the start of the
  475. * first argument. The processing of the argument happens
  476. * when the end of the argument is found, as it needs to
  477. * handle paranthesis and such.
  478. */
  479. if (!start_arg) {
  480. start_arg = i;
  481. /* Balance out the i++ in the for loop */
  482. i--;
  483. continue;
  484. }
  485. if (dereference_flags & (1ULL << arg)) {
  486. if (string_flags & (1ULL << arg)) {
  487. if (process_string(fmt + start_arg, e - start_arg, call))
  488. dereference_flags &= ~(1ULL << arg);
  489. } else if (process_pointer(fmt + start_arg, e - start_arg, call))
  490. dereference_flags &= ~(1ULL << arg);
  491. }
  492. start_arg = i;
  493. arg++;
  494. /* Balance out the i++ in the for loop */
  495. i--;
  496. }
  497. }
  498. if (dereference_flags & (1ULL << arg)) {
  499. if (string_flags & (1ULL << arg)) {
  500. if (process_string(fmt + start_arg, i - start_arg, call))
  501. dereference_flags &= ~(1ULL << arg);
  502. } else if (process_pointer(fmt + start_arg, i - start_arg, call))
  503. dereference_flags &= ~(1ULL << arg);
  504. }
  505. /*
  506. * If you triggered the below warning, the trace event reported
  507. * uses an unsafe dereference pointer %p*. As the data stored
  508. * at the trace event time may no longer exist when the trace
  509. * event is printed, dereferencing to the original source is
  510. * unsafe. The source of the dereference must be copied into the
  511. * event itself, and the dereference must access the copy instead.
  512. */
  513. if (WARN_ON_ONCE(dereference_flags)) {
  514. arg = 1;
  515. while (!(dereference_flags & 1)) {
  516. dereference_flags >>= 1;
  517. arg++;
  518. }
  519. pr_warn("event %s has unsafe dereference of argument %d\n",
  520. trace_event_name(call), arg);
  521. pr_warn("print_fmt: %s\n", fmt);
  522. }
  523. }
  524. int trace_event_raw_init(struct trace_event_call *call)
  525. {
  526. int id;
  527. id = register_trace_event(&call->event);
  528. if (!id)
  529. return -ENODEV;
  530. test_event_printk(call);
  531. return 0;
  532. }
  533. EXPORT_SYMBOL_GPL(trace_event_raw_init);
  534. bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
  535. {
  536. struct trace_array *tr = trace_file->tr;
  537. struct trace_array_cpu *data;
  538. struct trace_pid_list *no_pid_list;
  539. struct trace_pid_list *pid_list;
  540. pid_list = rcu_dereference_raw(tr->filtered_pids);
  541. no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
  542. if (!pid_list && !no_pid_list)
  543. return false;
  544. data = this_cpu_ptr(tr->array_buffer.data);
  545. return data->ignore_pid;
  546. }
  547. EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
  548. void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  549. struct trace_event_file *trace_file,
  550. unsigned long len)
  551. {
  552. struct trace_event_call *event_call = trace_file->event_call;
  553. if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
  554. trace_event_ignore_this_pid(trace_file))
  555. return NULL;
  556. /*
  557. * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
  558. * preemption (adding one to the preempt_count). Since we are
  559. * interested in the preempt_count at the time the tracepoint was
  560. * hit, we need to subtract one to offset the increment.
  561. */
  562. fbuffer->trace_ctx = tracing_gen_ctx_dec();
  563. fbuffer->trace_file = trace_file;
  564. fbuffer->event =
  565. trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
  566. event_call->event.type, len,
  567. fbuffer->trace_ctx);
  568. if (!fbuffer->event)
  569. return NULL;
  570. fbuffer->regs = NULL;
  571. fbuffer->entry = ring_buffer_event_data(fbuffer->event);
  572. return fbuffer->entry;
  573. }
  574. EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
  575. int trace_event_reg(struct trace_event_call *call,
  576. enum trace_reg type, void *data)
  577. {
  578. struct trace_event_file *file = data;
  579. WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
  580. switch (type) {
  581. case TRACE_REG_REGISTER:
  582. return tracepoint_probe_register(call->tp,
  583. call->class->probe,
  584. file);
  585. case TRACE_REG_UNREGISTER:
  586. tracepoint_probe_unregister(call->tp,
  587. call->class->probe,
  588. file);
  589. return 0;
  590. #ifdef CONFIG_PERF_EVENTS
  591. case TRACE_REG_PERF_REGISTER:
  592. return tracepoint_probe_register(call->tp,
  593. call->class->perf_probe,
  594. call);
  595. case TRACE_REG_PERF_UNREGISTER:
  596. tracepoint_probe_unregister(call->tp,
  597. call->class->perf_probe,
  598. call);
  599. return 0;
  600. case TRACE_REG_PERF_OPEN:
  601. case TRACE_REG_PERF_CLOSE:
  602. case TRACE_REG_PERF_ADD:
  603. case TRACE_REG_PERF_DEL:
  604. return 0;
  605. #endif
  606. }
  607. return 0;
  608. }
  609. EXPORT_SYMBOL_GPL(trace_event_reg);
  610. void trace_event_enable_cmd_record(bool enable)
  611. {
  612. struct trace_event_file *file;
  613. struct trace_array *tr;
  614. lockdep_assert_held(&event_mutex);
  615. do_for_each_event_file(tr, file) {
  616. if (!(file->flags & EVENT_FILE_FL_ENABLED))
  617. continue;
  618. if (enable) {
  619. tracing_start_cmdline_record();
  620. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  621. } else {
  622. tracing_stop_cmdline_record();
  623. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  624. }
  625. } while_for_each_event_file();
  626. }
  627. void trace_event_enable_tgid_record(bool enable)
  628. {
  629. struct trace_event_file *file;
  630. struct trace_array *tr;
  631. lockdep_assert_held(&event_mutex);
  632. do_for_each_event_file(tr, file) {
  633. if (!(file->flags & EVENT_FILE_FL_ENABLED))
  634. continue;
  635. if (enable) {
  636. tracing_start_tgid_record();
  637. set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
  638. } else {
  639. tracing_stop_tgid_record();
  640. clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
  641. &file->flags);
  642. }
  643. } while_for_each_event_file();
  644. }
  645. static int __ftrace_event_enable_disable(struct trace_event_file *file,
  646. int enable, int soft_disable)
  647. {
  648. struct trace_event_call *call = file->event_call;
  649. struct trace_array *tr = file->tr;
  650. int ret = 0;
  651. int disable;
  652. switch (enable) {
  653. case 0:
  654. /*
  655. * When soft_disable is set and enable is cleared, the sm_ref
  656. * reference counter is decremented. If it reaches 0, we want
  657. * to clear the SOFT_DISABLED flag but leave the event in the
  658. * state that it was. That is, if the event was enabled and
  659. * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
  660. * is set we do not want the event to be enabled before we
  661. * clear the bit.
  662. *
  663. * When soft_disable is not set but the SOFT_MODE flag is,
  664. * we do nothing. Do not disable the tracepoint, otherwise
  665. * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
  666. */
  667. if (soft_disable) {
  668. if (atomic_dec_return(&file->sm_ref) > 0)
  669. break;
  670. disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
  671. clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
  672. /* Disable use of trace_buffered_event */
  673. trace_buffered_event_disable();
  674. } else
  675. disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
  676. if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
  677. clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  678. if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
  679. tracing_stop_cmdline_record();
  680. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  681. }
  682. if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
  683. tracing_stop_tgid_record();
  684. clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
  685. }
  686. ret = call->class->reg(call, TRACE_REG_UNREGISTER, file);
  687. WARN_ON_ONCE(ret);
  688. }
  689. /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
  690. if (file->flags & EVENT_FILE_FL_SOFT_MODE)
  691. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  692. else
  693. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  694. break;
  695. case 1:
  696. /*
  697. * When soft_disable is set and enable is set, we want to
  698. * register the tracepoint for the event, but leave the event
  699. * as is. That means, if the event was already enabled, we do
  700. * nothing (but set SOFT_MODE). If the event is disabled, we
  701. * set SOFT_DISABLED before enabling the event tracepoint, so
  702. * it still seems to be disabled.
  703. */
  704. if (!soft_disable)
  705. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  706. else {
  707. if (atomic_inc_return(&file->sm_ref) > 1)
  708. break;
  709. set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
  710. /* Enable use of trace_buffered_event */
  711. trace_buffered_event_enable();
  712. }
  713. if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
  714. bool cmd = false, tgid = false;
  715. /* Keep the event disabled, when going to SOFT_MODE. */
  716. if (soft_disable)
  717. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  718. if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
  719. cmd = true;
  720. tracing_start_cmdline_record();
  721. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  722. }
  723. if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
  724. tgid = true;
  725. tracing_start_tgid_record();
  726. set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
  727. }
  728. ret = call->class->reg(call, TRACE_REG_REGISTER, file);
  729. if (ret) {
  730. if (cmd)
  731. tracing_stop_cmdline_record();
  732. if (tgid)
  733. tracing_stop_tgid_record();
  734. pr_info("event trace: Could not enable event "
  735. "%s\n", trace_event_name(call));
  736. break;
  737. }
  738. set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  739. /* WAS_ENABLED gets set but never cleared. */
  740. set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
  741. }
  742. break;
  743. }
  744. return ret;
  745. }
  746. int trace_event_enable_disable(struct trace_event_file *file,
  747. int enable, int soft_disable)
  748. {
  749. return __ftrace_event_enable_disable(file, enable, soft_disable);
  750. }
  751. static int ftrace_event_enable_disable(struct trace_event_file *file,
  752. int enable)
  753. {
  754. return __ftrace_event_enable_disable(file, enable, 0);
  755. }
  756. static void ftrace_clear_events(struct trace_array *tr)
  757. {
  758. struct trace_event_file *file;
  759. mutex_lock(&event_mutex);
  760. list_for_each_entry(file, &tr->events, list) {
  761. ftrace_event_enable_disable(file, 0);
  762. }
  763. mutex_unlock(&event_mutex);
  764. }
  765. static void
  766. event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
  767. {
  768. struct trace_pid_list *pid_list;
  769. struct trace_array *tr = data;
  770. pid_list = rcu_dereference_raw(tr->filtered_pids);
  771. trace_filter_add_remove_task(pid_list, NULL, task);
  772. pid_list = rcu_dereference_raw(tr->filtered_no_pids);
  773. trace_filter_add_remove_task(pid_list, NULL, task);
  774. }
  775. static void
  776. event_filter_pid_sched_process_fork(void *data,
  777. struct task_struct *self,
  778. struct task_struct *task)
  779. {
  780. struct trace_pid_list *pid_list;
  781. struct trace_array *tr = data;
  782. pid_list = rcu_dereference_sched(tr->filtered_pids);
  783. trace_filter_add_remove_task(pid_list, self, task);
  784. pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  785. trace_filter_add_remove_task(pid_list, self, task);
  786. }
  787. void trace_event_follow_fork(struct trace_array *tr, bool enable)
  788. {
  789. if (enable) {
  790. register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
  791. tr, INT_MIN);
  792. register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
  793. tr, INT_MAX);
  794. } else {
  795. unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
  796. tr);
  797. unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
  798. tr);
  799. }
  800. }
  801. static void
  802. event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
  803. struct task_struct *prev,
  804. struct task_struct *next,
  805. unsigned int prev_state)
  806. {
  807. struct trace_array *tr = data;
  808. struct trace_pid_list *no_pid_list;
  809. struct trace_pid_list *pid_list;
  810. bool ret;
  811. pid_list = rcu_dereference_sched(tr->filtered_pids);
  812. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  813. /*
  814. * Sched switch is funny, as we only want to ignore it
  815. * in the notrace case if both prev and next should be ignored.
  816. */
  817. ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
  818. trace_ignore_this_task(NULL, no_pid_list, next);
  819. this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
  820. (trace_ignore_this_task(pid_list, NULL, prev) &&
  821. trace_ignore_this_task(pid_list, NULL, next)));
  822. }
  823. static void
  824. event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
  825. struct task_struct *prev,
  826. struct task_struct *next,
  827. unsigned int prev_state)
  828. {
  829. struct trace_array *tr = data;
  830. struct trace_pid_list *no_pid_list;
  831. struct trace_pid_list *pid_list;
  832. pid_list = rcu_dereference_sched(tr->filtered_pids);
  833. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  834. this_cpu_write(tr->array_buffer.data->ignore_pid,
  835. trace_ignore_this_task(pid_list, no_pid_list, next));
  836. }
  837. static void
  838. event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
  839. {
  840. struct trace_array *tr = data;
  841. struct trace_pid_list *no_pid_list;
  842. struct trace_pid_list *pid_list;
  843. /* Nothing to do if we are already tracing */
  844. if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
  845. return;
  846. pid_list = rcu_dereference_sched(tr->filtered_pids);
  847. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  848. this_cpu_write(tr->array_buffer.data->ignore_pid,
  849. trace_ignore_this_task(pid_list, no_pid_list, task));
  850. }
  851. static void
  852. event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
  853. {
  854. struct trace_array *tr = data;
  855. struct trace_pid_list *no_pid_list;
  856. struct trace_pid_list *pid_list;
  857. /* Nothing to do if we are not tracing */
  858. if (this_cpu_read(tr->array_buffer.data->ignore_pid))
  859. return;
  860. pid_list = rcu_dereference_sched(tr->filtered_pids);
  861. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  862. /* Set tracing if current is enabled */
  863. this_cpu_write(tr->array_buffer.data->ignore_pid,
  864. trace_ignore_this_task(pid_list, no_pid_list, current));
  865. }
  866. static void unregister_pid_events(struct trace_array *tr)
  867. {
  868. unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
  869. unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
  870. unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
  871. unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
  872. unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
  873. unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
  874. unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
  875. unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
  876. }
  877. static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
  878. {
  879. struct trace_pid_list *pid_list;
  880. struct trace_pid_list *no_pid_list;
  881. struct trace_event_file *file;
  882. int cpu;
  883. pid_list = rcu_dereference_protected(tr->filtered_pids,
  884. lockdep_is_held(&event_mutex));
  885. no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
  886. lockdep_is_held(&event_mutex));
  887. /* Make sure there's something to do */
  888. if (!pid_type_enabled(type, pid_list, no_pid_list))
  889. return;
  890. if (!still_need_pid_events(type, pid_list, no_pid_list)) {
  891. unregister_pid_events(tr);
  892. list_for_each_entry(file, &tr->events, list) {
  893. clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
  894. }
  895. for_each_possible_cpu(cpu)
  896. per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
  897. }
  898. if (type & TRACE_PIDS)
  899. rcu_assign_pointer(tr->filtered_pids, NULL);
  900. if (type & TRACE_NO_PIDS)
  901. rcu_assign_pointer(tr->filtered_no_pids, NULL);
  902. /* Wait till all users are no longer using pid filtering */
  903. tracepoint_synchronize_unregister();
  904. if ((type & TRACE_PIDS) && pid_list)
  905. trace_pid_list_free(pid_list);
  906. if ((type & TRACE_NO_PIDS) && no_pid_list)
  907. trace_pid_list_free(no_pid_list);
  908. }
  909. static void ftrace_clear_event_pids(struct trace_array *tr, int type)
  910. {
  911. mutex_lock(&event_mutex);
  912. __ftrace_clear_event_pids(tr, type);
  913. mutex_unlock(&event_mutex);
  914. }
  915. static void __put_system(struct event_subsystem *system)
  916. {
  917. struct event_filter *filter = system->filter;
  918. WARN_ON_ONCE(system_refcount(system) == 0);
  919. if (system_refcount_dec(system))
  920. return;
  921. list_del(&system->list);
  922. if (filter) {
  923. kfree(filter->filter_string);
  924. kfree(filter);
  925. }
  926. kfree_const(system->name);
  927. kfree(system);
  928. }
  929. static void __get_system(struct event_subsystem *system)
  930. {
  931. WARN_ON_ONCE(system_refcount(system) == 0);
  932. system_refcount_inc(system);
  933. }
  934. static void __get_system_dir(struct trace_subsystem_dir *dir)
  935. {
  936. WARN_ON_ONCE(dir->ref_count == 0);
  937. dir->ref_count++;
  938. __get_system(dir->subsystem);
  939. }
  940. static void __put_system_dir(struct trace_subsystem_dir *dir)
  941. {
  942. WARN_ON_ONCE(dir->ref_count == 0);
  943. /* If the subsystem is about to be freed, the dir must be too */
  944. WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
  945. __put_system(dir->subsystem);
  946. if (!--dir->ref_count)
  947. kfree(dir);
  948. }
  949. static void put_system(struct trace_subsystem_dir *dir)
  950. {
  951. mutex_lock(&event_mutex);
  952. __put_system_dir(dir);
  953. mutex_unlock(&event_mutex);
  954. }
  955. static void remove_subsystem(struct trace_subsystem_dir *dir)
  956. {
  957. if (!dir)
  958. return;
  959. if (!--dir->nr_events) {
  960. eventfs_remove_dir(dir->ei);
  961. list_del(&dir->list);
  962. __put_system_dir(dir);
  963. }
  964. }
  965. void event_file_get(struct trace_event_file *file)
  966. {
  967. refcount_inc(&file->ref);
  968. }
  969. void event_file_put(struct trace_event_file *file)
  970. {
  971. if (WARN_ON_ONCE(!refcount_read(&file->ref))) {
  972. if (file->flags & EVENT_FILE_FL_FREED)
  973. kmem_cache_free(file_cachep, file);
  974. return;
  975. }
  976. if (refcount_dec_and_test(&file->ref)) {
  977. /* Count should only go to zero when it is freed */
  978. if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED)))
  979. return;
  980. kmem_cache_free(file_cachep, file);
  981. }
  982. }
  983. static void remove_event_file_dir(struct trace_event_file *file)
  984. {
  985. eventfs_remove_dir(file->ei);
  986. list_del(&file->list);
  987. remove_subsystem(file->system);
  988. free_event_filter(file->filter);
  989. file->flags |= EVENT_FILE_FL_FREED;
  990. event_file_put(file);
  991. }
  992. /*
  993. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  994. */
  995. static int
  996. __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
  997. const char *sub, const char *event, int set)
  998. {
  999. struct trace_event_file *file;
  1000. struct trace_event_call *call;
  1001. const char *name;
  1002. int ret = -EINVAL;
  1003. int eret = 0;
  1004. list_for_each_entry(file, &tr->events, list) {
  1005. call = file->event_call;
  1006. name = trace_event_name(call);
  1007. if (!name || !call->class || !call->class->reg)
  1008. continue;
  1009. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  1010. continue;
  1011. if (match &&
  1012. strcmp(match, name) != 0 &&
  1013. strcmp(match, call->class->system) != 0)
  1014. continue;
  1015. if (sub && strcmp(sub, call->class->system) != 0)
  1016. continue;
  1017. if (event && strcmp(event, name) != 0)
  1018. continue;
  1019. ret = ftrace_event_enable_disable(file, set);
  1020. /*
  1021. * Save the first error and return that. Some events
  1022. * may still have been enabled, but let the user
  1023. * know that something went wrong.
  1024. */
  1025. if (ret && !eret)
  1026. eret = ret;
  1027. ret = eret;
  1028. }
  1029. return ret;
  1030. }
  1031. static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
  1032. const char *sub, const char *event, int set)
  1033. {
  1034. int ret;
  1035. mutex_lock(&event_mutex);
  1036. ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
  1037. mutex_unlock(&event_mutex);
  1038. return ret;
  1039. }
  1040. int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
  1041. {
  1042. char *event = NULL, *sub = NULL, *match;
  1043. int ret;
  1044. if (!tr)
  1045. return -ENOENT;
  1046. /*
  1047. * The buf format can be <subsystem>:<event-name>
  1048. * *:<event-name> means any event by that name.
  1049. * :<event-name> is the same.
  1050. *
  1051. * <subsystem>:* means all events in that subsystem
  1052. * <subsystem>: means the same.
  1053. *
  1054. * <name> (no ':') means all events in a subsystem with
  1055. * the name <name> or any event that matches <name>
  1056. */
  1057. match = strsep(&buf, ":");
  1058. if (buf) {
  1059. sub = match;
  1060. event = buf;
  1061. match = NULL;
  1062. if (!strlen(sub) || strcmp(sub, "*") == 0)
  1063. sub = NULL;
  1064. if (!strlen(event) || strcmp(event, "*") == 0)
  1065. event = NULL;
  1066. }
  1067. ret = __ftrace_set_clr_event(tr, match, sub, event, set);
  1068. /* Put back the colon to allow this to be called again */
  1069. if (buf)
  1070. *(buf - 1) = ':';
  1071. return ret;
  1072. }
  1073. /**
  1074. * trace_set_clr_event - enable or disable an event
  1075. * @system: system name to match (NULL for any system)
  1076. * @event: event name to match (NULL for all events, within system)
  1077. * @set: 1 to enable, 0 to disable
  1078. *
  1079. * This is a way for other parts of the kernel to enable or disable
  1080. * event recording.
  1081. *
  1082. * Returns 0 on success, -EINVAL if the parameters do not match any
  1083. * registered events.
  1084. */
  1085. int trace_set_clr_event(const char *system, const char *event, int set)
  1086. {
  1087. struct trace_array *tr = top_trace_array();
  1088. if (!tr)
  1089. return -ENODEV;
  1090. return __ftrace_set_clr_event(tr, NULL, system, event, set);
  1091. }
  1092. EXPORT_SYMBOL_GPL(trace_set_clr_event);
  1093. /**
  1094. * trace_array_set_clr_event - enable or disable an event for a trace array.
  1095. * @tr: concerned trace array.
  1096. * @system: system name to match (NULL for any system)
  1097. * @event: event name to match (NULL for all events, within system)
  1098. * @enable: true to enable, false to disable
  1099. *
  1100. * This is a way for other parts of the kernel to enable or disable
  1101. * event recording.
  1102. *
  1103. * Returns 0 on success, -EINVAL if the parameters do not match any
  1104. * registered events.
  1105. */
  1106. int trace_array_set_clr_event(struct trace_array *tr, const char *system,
  1107. const char *event, bool enable)
  1108. {
  1109. int set;
  1110. if (!tr)
  1111. return -ENOENT;
  1112. set = (enable == true) ? 1 : 0;
  1113. return __ftrace_set_clr_event(tr, NULL, system, event, set);
  1114. }
  1115. EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
  1116. /* 128 should be much more than enough */
  1117. #define EVENT_BUF_SIZE 127
  1118. static ssize_t
  1119. ftrace_event_write(struct file *file, const char __user *ubuf,
  1120. size_t cnt, loff_t *ppos)
  1121. {
  1122. struct trace_parser parser;
  1123. struct seq_file *m = file->private_data;
  1124. struct trace_array *tr = m->private;
  1125. ssize_t read, ret;
  1126. if (!cnt)
  1127. return 0;
  1128. ret = tracing_update_buffers(tr);
  1129. if (ret < 0)
  1130. return ret;
  1131. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  1132. return -ENOMEM;
  1133. read = trace_get_user(&parser, ubuf, cnt, ppos);
  1134. if (read >= 0 && trace_parser_loaded((&parser))) {
  1135. int set = 1;
  1136. if (*parser.buffer == '!')
  1137. set = 0;
  1138. ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
  1139. if (ret)
  1140. goto out_put;
  1141. }
  1142. ret = read;
  1143. out_put:
  1144. trace_parser_put(&parser);
  1145. return ret;
  1146. }
  1147. static void *
  1148. t_next(struct seq_file *m, void *v, loff_t *pos)
  1149. {
  1150. struct trace_event_file *file = v;
  1151. struct trace_event_call *call;
  1152. struct trace_array *tr = m->private;
  1153. (*pos)++;
  1154. list_for_each_entry_continue(file, &tr->events, list) {
  1155. call = file->event_call;
  1156. /*
  1157. * The ftrace subsystem is for showing formats only.
  1158. * They can not be enabled or disabled via the event files.
  1159. */
  1160. if (call->class && call->class->reg &&
  1161. !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
  1162. return file;
  1163. }
  1164. return NULL;
  1165. }
  1166. static void *t_start(struct seq_file *m, loff_t *pos)
  1167. {
  1168. struct trace_event_file *file;
  1169. struct trace_array *tr = m->private;
  1170. loff_t l;
  1171. mutex_lock(&event_mutex);
  1172. file = list_entry(&tr->events, struct trace_event_file, list);
  1173. for (l = 0; l <= *pos; ) {
  1174. file = t_next(m, file, &l);
  1175. if (!file)
  1176. break;
  1177. }
  1178. return file;
  1179. }
  1180. static void *
  1181. s_next(struct seq_file *m, void *v, loff_t *pos)
  1182. {
  1183. struct trace_event_file *file = v;
  1184. struct trace_array *tr = m->private;
  1185. (*pos)++;
  1186. list_for_each_entry_continue(file, &tr->events, list) {
  1187. if (file->flags & EVENT_FILE_FL_ENABLED)
  1188. return file;
  1189. }
  1190. return NULL;
  1191. }
  1192. static void *s_start(struct seq_file *m, loff_t *pos)
  1193. {
  1194. struct trace_event_file *file;
  1195. struct trace_array *tr = m->private;
  1196. loff_t l;
  1197. mutex_lock(&event_mutex);
  1198. file = list_entry(&tr->events, struct trace_event_file, list);
  1199. for (l = 0; l <= *pos; ) {
  1200. file = s_next(m, file, &l);
  1201. if (!file)
  1202. break;
  1203. }
  1204. return file;
  1205. }
  1206. static int t_show(struct seq_file *m, void *v)
  1207. {
  1208. struct trace_event_file *file = v;
  1209. struct trace_event_call *call = file->event_call;
  1210. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  1211. seq_printf(m, "%s:", call->class->system);
  1212. seq_printf(m, "%s\n", trace_event_name(call));
  1213. return 0;
  1214. }
  1215. static void t_stop(struct seq_file *m, void *p)
  1216. {
  1217. mutex_unlock(&event_mutex);
  1218. }
  1219. static void *
  1220. __next(struct seq_file *m, void *v, loff_t *pos, int type)
  1221. {
  1222. struct trace_array *tr = m->private;
  1223. struct trace_pid_list *pid_list;
  1224. if (type == TRACE_PIDS)
  1225. pid_list = rcu_dereference_sched(tr->filtered_pids);
  1226. else
  1227. pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  1228. return trace_pid_next(pid_list, v, pos);
  1229. }
  1230. static void *
  1231. p_next(struct seq_file *m, void *v, loff_t *pos)
  1232. {
  1233. return __next(m, v, pos, TRACE_PIDS);
  1234. }
  1235. static void *
  1236. np_next(struct seq_file *m, void *v, loff_t *pos)
  1237. {
  1238. return __next(m, v, pos, TRACE_NO_PIDS);
  1239. }
  1240. static void *__start(struct seq_file *m, loff_t *pos, int type)
  1241. __acquires(RCU)
  1242. {
  1243. struct trace_pid_list *pid_list;
  1244. struct trace_array *tr = m->private;
  1245. /*
  1246. * Grab the mutex, to keep calls to p_next() having the same
  1247. * tr->filtered_pids as p_start() has.
  1248. * If we just passed the tr->filtered_pids around, then RCU would
  1249. * have been enough, but doing that makes things more complex.
  1250. */
  1251. mutex_lock(&event_mutex);
  1252. rcu_read_lock_sched();
  1253. if (type == TRACE_PIDS)
  1254. pid_list = rcu_dereference_sched(tr->filtered_pids);
  1255. else
  1256. pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  1257. if (!pid_list)
  1258. return NULL;
  1259. return trace_pid_start(pid_list, pos);
  1260. }
  1261. static void *p_start(struct seq_file *m, loff_t *pos)
  1262. __acquires(RCU)
  1263. {
  1264. return __start(m, pos, TRACE_PIDS);
  1265. }
  1266. static void *np_start(struct seq_file *m, loff_t *pos)
  1267. __acquires(RCU)
  1268. {
  1269. return __start(m, pos, TRACE_NO_PIDS);
  1270. }
  1271. static void p_stop(struct seq_file *m, void *p)
  1272. __releases(RCU)
  1273. {
  1274. rcu_read_unlock_sched();
  1275. mutex_unlock(&event_mutex);
  1276. }
  1277. static ssize_t
  1278. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  1279. loff_t *ppos)
  1280. {
  1281. struct trace_event_file *file;
  1282. unsigned long flags;
  1283. char buf[4] = "0";
  1284. mutex_lock(&event_mutex);
  1285. file = event_file_file(filp);
  1286. if (likely(file))
  1287. flags = file->flags;
  1288. mutex_unlock(&event_mutex);
  1289. if (!file)
  1290. return -ENODEV;
  1291. if (flags & EVENT_FILE_FL_ENABLED &&
  1292. !(flags & EVENT_FILE_FL_SOFT_DISABLED))
  1293. strcpy(buf, "1");
  1294. if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
  1295. flags & EVENT_FILE_FL_SOFT_MODE)
  1296. strcat(buf, "*");
  1297. strcat(buf, "\n");
  1298. return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
  1299. }
  1300. static ssize_t
  1301. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1302. loff_t *ppos)
  1303. {
  1304. struct trace_event_file *file;
  1305. unsigned long val;
  1306. int ret;
  1307. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1308. if (ret)
  1309. return ret;
  1310. switch (val) {
  1311. case 0:
  1312. case 1:
  1313. ret = -ENODEV;
  1314. mutex_lock(&event_mutex);
  1315. file = event_file_file(filp);
  1316. if (likely(file)) {
  1317. ret = tracing_update_buffers(file->tr);
  1318. if (ret < 0) {
  1319. mutex_unlock(&event_mutex);
  1320. return ret;
  1321. }
  1322. ret = ftrace_event_enable_disable(file, val);
  1323. }
  1324. mutex_unlock(&event_mutex);
  1325. break;
  1326. default:
  1327. return -EINVAL;
  1328. }
  1329. *ppos += cnt;
  1330. return ret ? ret : cnt;
  1331. }
  1332. static ssize_t
  1333. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  1334. loff_t *ppos)
  1335. {
  1336. const char set_to_char[4] = { '?', '0', '1', 'X' };
  1337. struct trace_subsystem_dir *dir = filp->private_data;
  1338. struct event_subsystem *system = dir->subsystem;
  1339. struct trace_event_call *call;
  1340. struct trace_event_file *file;
  1341. struct trace_array *tr = dir->tr;
  1342. char buf[2];
  1343. int set = 0;
  1344. int ret;
  1345. mutex_lock(&event_mutex);
  1346. list_for_each_entry(file, &tr->events, list) {
  1347. call = file->event_call;
  1348. if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
  1349. !trace_event_name(call) || !call->class || !call->class->reg)
  1350. continue;
  1351. if (system && strcmp(call->class->system, system->name) != 0)
  1352. continue;
  1353. /*
  1354. * We need to find out if all the events are set
  1355. * or if all events or cleared, or if we have
  1356. * a mixture.
  1357. */
  1358. set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
  1359. /*
  1360. * If we have a mixture, no need to look further.
  1361. */
  1362. if (set == 3)
  1363. break;
  1364. }
  1365. mutex_unlock(&event_mutex);
  1366. buf[0] = set_to_char[set];
  1367. buf[1] = '\n';
  1368. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  1369. return ret;
  1370. }
  1371. static ssize_t
  1372. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1373. loff_t *ppos)
  1374. {
  1375. struct trace_subsystem_dir *dir = filp->private_data;
  1376. struct event_subsystem *system = dir->subsystem;
  1377. const char *name = NULL;
  1378. unsigned long val;
  1379. ssize_t ret;
  1380. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1381. if (ret)
  1382. return ret;
  1383. ret = tracing_update_buffers(dir->tr);
  1384. if (ret < 0)
  1385. return ret;
  1386. if (val != 0 && val != 1)
  1387. return -EINVAL;
  1388. /*
  1389. * Opening of "enable" adds a ref count to system,
  1390. * so the name is safe to use.
  1391. */
  1392. if (system)
  1393. name = system->name;
  1394. ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
  1395. if (ret)
  1396. goto out;
  1397. ret = cnt;
  1398. out:
  1399. *ppos += cnt;
  1400. return ret;
  1401. }
  1402. enum {
  1403. FORMAT_HEADER = 1,
  1404. FORMAT_FIELD_SEPERATOR = 2,
  1405. FORMAT_PRINTFMT = 3,
  1406. };
  1407. static void *f_next(struct seq_file *m, void *v, loff_t *pos)
  1408. {
  1409. struct trace_event_file *file = event_file_data(m->private);
  1410. struct trace_event_call *call = file->event_call;
  1411. struct list_head *common_head = &ftrace_common_fields;
  1412. struct list_head *head = trace_get_fields(call);
  1413. struct list_head *node = v;
  1414. (*pos)++;
  1415. switch ((unsigned long)v) {
  1416. case FORMAT_HEADER:
  1417. node = common_head;
  1418. break;
  1419. case FORMAT_FIELD_SEPERATOR:
  1420. node = head;
  1421. break;
  1422. case FORMAT_PRINTFMT:
  1423. /* all done */
  1424. return NULL;
  1425. }
  1426. node = node->prev;
  1427. if (node == common_head)
  1428. return (void *)FORMAT_FIELD_SEPERATOR;
  1429. else if (node == head)
  1430. return (void *)FORMAT_PRINTFMT;
  1431. else
  1432. return node;
  1433. }
  1434. static int f_show(struct seq_file *m, void *v)
  1435. {
  1436. struct trace_event_file *file = event_file_data(m->private);
  1437. struct trace_event_call *call = file->event_call;
  1438. struct ftrace_event_field *field;
  1439. const char *array_descriptor;
  1440. switch ((unsigned long)v) {
  1441. case FORMAT_HEADER:
  1442. seq_printf(m, "name: %s\n", trace_event_name(call));
  1443. seq_printf(m, "ID: %d\n", call->event.type);
  1444. seq_puts(m, "format:\n");
  1445. return 0;
  1446. case FORMAT_FIELD_SEPERATOR:
  1447. seq_putc(m, '\n');
  1448. return 0;
  1449. case FORMAT_PRINTFMT:
  1450. seq_printf(m, "\nprint fmt: %s\n",
  1451. call->print_fmt);
  1452. return 0;
  1453. }
  1454. field = list_entry(v, struct ftrace_event_field, link);
  1455. /*
  1456. * Smartly shows the array type(except dynamic array).
  1457. * Normal:
  1458. * field:TYPE VAR
  1459. * If TYPE := TYPE[LEN], it is shown:
  1460. * field:TYPE VAR[LEN]
  1461. */
  1462. array_descriptor = strchr(field->type, '[');
  1463. if (str_has_prefix(field->type, "__data_loc"))
  1464. array_descriptor = NULL;
  1465. if (!array_descriptor)
  1466. seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  1467. field->type, field->name, field->offset,
  1468. field->size, !!field->is_signed);
  1469. else if (field->len)
  1470. seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  1471. (int)(array_descriptor - field->type),
  1472. field->type, field->name,
  1473. field->len, field->offset,
  1474. field->size, !!field->is_signed);
  1475. else
  1476. seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  1477. (int)(array_descriptor - field->type),
  1478. field->type, field->name,
  1479. field->offset, field->size, !!field->is_signed);
  1480. return 0;
  1481. }
  1482. static void *f_start(struct seq_file *m, loff_t *pos)
  1483. {
  1484. struct trace_event_file *file;
  1485. void *p = (void *)FORMAT_HEADER;
  1486. loff_t l = 0;
  1487. /* ->stop() is called even if ->start() fails */
  1488. mutex_lock(&event_mutex);
  1489. file = event_file_file(m->private);
  1490. if (!file)
  1491. return ERR_PTR(-ENODEV);
  1492. while (l < *pos && p)
  1493. p = f_next(m, p, &l);
  1494. return p;
  1495. }
  1496. static void f_stop(struct seq_file *m, void *p)
  1497. {
  1498. mutex_unlock(&event_mutex);
  1499. }
  1500. static const struct seq_operations trace_format_seq_ops = {
  1501. .start = f_start,
  1502. .next = f_next,
  1503. .stop = f_stop,
  1504. .show = f_show,
  1505. };
  1506. static int trace_format_open(struct inode *inode, struct file *file)
  1507. {
  1508. struct seq_file *m;
  1509. int ret;
  1510. /* Do we want to hide event format files on tracefs lockdown? */
  1511. ret = seq_open(file, &trace_format_seq_ops);
  1512. if (ret < 0)
  1513. return ret;
  1514. m = file->private_data;
  1515. m->private = file;
  1516. return 0;
  1517. }
  1518. #ifdef CONFIG_PERF_EVENTS
  1519. static ssize_t
  1520. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1521. {
  1522. int id = (long)event_file_data(filp);
  1523. char buf[32];
  1524. int len;
  1525. if (unlikely(!id))
  1526. return -ENODEV;
  1527. len = sprintf(buf, "%d\n", id);
  1528. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  1529. }
  1530. #endif
  1531. static ssize_t
  1532. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  1533. loff_t *ppos)
  1534. {
  1535. struct trace_event_file *file;
  1536. struct trace_seq *s;
  1537. int r = -ENODEV;
  1538. if (*ppos)
  1539. return 0;
  1540. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1541. if (!s)
  1542. return -ENOMEM;
  1543. trace_seq_init(s);
  1544. mutex_lock(&event_mutex);
  1545. file = event_file_file(filp);
  1546. if (file)
  1547. print_event_filter(file, s);
  1548. mutex_unlock(&event_mutex);
  1549. if (file)
  1550. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1551. s->buffer, trace_seq_used(s));
  1552. kfree(s);
  1553. return r;
  1554. }
  1555. static ssize_t
  1556. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1557. loff_t *ppos)
  1558. {
  1559. struct trace_event_file *file;
  1560. char *buf;
  1561. int err = -ENODEV;
  1562. if (cnt >= PAGE_SIZE)
  1563. return -EINVAL;
  1564. buf = memdup_user_nul(ubuf, cnt);
  1565. if (IS_ERR(buf))
  1566. return PTR_ERR(buf);
  1567. mutex_lock(&event_mutex);
  1568. file = event_file_file(filp);
  1569. if (file) {
  1570. if (file->flags & EVENT_FILE_FL_FREED)
  1571. err = -ENODEV;
  1572. else
  1573. err = apply_event_filter(file, buf);
  1574. }
  1575. mutex_unlock(&event_mutex);
  1576. kfree(buf);
  1577. if (err < 0)
  1578. return err;
  1579. *ppos += cnt;
  1580. return cnt;
  1581. }
  1582. static LIST_HEAD(event_subsystems);
  1583. static int subsystem_open(struct inode *inode, struct file *filp)
  1584. {
  1585. struct trace_subsystem_dir *dir = NULL, *iter_dir;
  1586. struct trace_array *tr = NULL, *iter_tr;
  1587. struct event_subsystem *system = NULL;
  1588. int ret;
  1589. if (tracing_is_disabled())
  1590. return -ENODEV;
  1591. /* Make sure the system still exists */
  1592. mutex_lock(&event_mutex);
  1593. mutex_lock(&trace_types_lock);
  1594. list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
  1595. list_for_each_entry(iter_dir, &iter_tr->systems, list) {
  1596. if (iter_dir == inode->i_private) {
  1597. /* Don't open systems with no events */
  1598. tr = iter_tr;
  1599. dir = iter_dir;
  1600. if (dir->nr_events) {
  1601. __get_system_dir(dir);
  1602. system = dir->subsystem;
  1603. }
  1604. goto exit_loop;
  1605. }
  1606. }
  1607. }
  1608. exit_loop:
  1609. mutex_unlock(&trace_types_lock);
  1610. mutex_unlock(&event_mutex);
  1611. if (!system)
  1612. return -ENODEV;
  1613. /* Still need to increment the ref count of the system */
  1614. if (trace_array_get(tr) < 0) {
  1615. put_system(dir);
  1616. return -ENODEV;
  1617. }
  1618. ret = tracing_open_generic(inode, filp);
  1619. if (ret < 0) {
  1620. trace_array_put(tr);
  1621. put_system(dir);
  1622. }
  1623. return ret;
  1624. }
  1625. static int system_tr_open(struct inode *inode, struct file *filp)
  1626. {
  1627. struct trace_subsystem_dir *dir;
  1628. struct trace_array *tr = inode->i_private;
  1629. int ret;
  1630. /* Make a temporary dir that has no system but points to tr */
  1631. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  1632. if (!dir)
  1633. return -ENOMEM;
  1634. ret = tracing_open_generic_tr(inode, filp);
  1635. if (ret < 0) {
  1636. kfree(dir);
  1637. return ret;
  1638. }
  1639. dir->tr = tr;
  1640. filp->private_data = dir;
  1641. return 0;
  1642. }
  1643. static int subsystem_release(struct inode *inode, struct file *file)
  1644. {
  1645. struct trace_subsystem_dir *dir = file->private_data;
  1646. trace_array_put(dir->tr);
  1647. /*
  1648. * If dir->subsystem is NULL, then this is a temporary
  1649. * descriptor that was made for a trace_array to enable
  1650. * all subsystems.
  1651. */
  1652. if (dir->subsystem)
  1653. put_system(dir);
  1654. else
  1655. kfree(dir);
  1656. return 0;
  1657. }
  1658. static ssize_t
  1659. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  1660. loff_t *ppos)
  1661. {
  1662. struct trace_subsystem_dir *dir = filp->private_data;
  1663. struct event_subsystem *system = dir->subsystem;
  1664. struct trace_seq *s;
  1665. int r;
  1666. if (*ppos)
  1667. return 0;
  1668. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1669. if (!s)
  1670. return -ENOMEM;
  1671. trace_seq_init(s);
  1672. print_subsystem_event_filter(system, s);
  1673. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1674. s->buffer, trace_seq_used(s));
  1675. kfree(s);
  1676. return r;
  1677. }
  1678. static ssize_t
  1679. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1680. loff_t *ppos)
  1681. {
  1682. struct trace_subsystem_dir *dir = filp->private_data;
  1683. char *buf;
  1684. int err;
  1685. if (cnt >= PAGE_SIZE)
  1686. return -EINVAL;
  1687. buf = memdup_user_nul(ubuf, cnt);
  1688. if (IS_ERR(buf))
  1689. return PTR_ERR(buf);
  1690. err = apply_subsystem_event_filter(dir, buf);
  1691. kfree(buf);
  1692. if (err < 0)
  1693. return err;
  1694. *ppos += cnt;
  1695. return cnt;
  1696. }
  1697. static ssize_t
  1698. show_header_page_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1699. {
  1700. struct trace_array *tr = filp->private_data;
  1701. struct trace_seq *s;
  1702. int r;
  1703. if (*ppos)
  1704. return 0;
  1705. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1706. if (!s)
  1707. return -ENOMEM;
  1708. trace_seq_init(s);
  1709. ring_buffer_print_page_header(tr->array_buffer.buffer, s);
  1710. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1711. s->buffer, trace_seq_used(s));
  1712. kfree(s);
  1713. return r;
  1714. }
  1715. static ssize_t
  1716. show_header_event_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1717. {
  1718. struct trace_seq *s;
  1719. int r;
  1720. if (*ppos)
  1721. return 0;
  1722. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1723. if (!s)
  1724. return -ENOMEM;
  1725. trace_seq_init(s);
  1726. ring_buffer_print_entry_header(s);
  1727. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1728. s->buffer, trace_seq_used(s));
  1729. kfree(s);
  1730. return r;
  1731. }
  1732. static void ignore_task_cpu(void *data)
  1733. {
  1734. struct trace_array *tr = data;
  1735. struct trace_pid_list *pid_list;
  1736. struct trace_pid_list *no_pid_list;
  1737. /*
  1738. * This function is called by on_each_cpu() while the
  1739. * event_mutex is held.
  1740. */
  1741. pid_list = rcu_dereference_protected(tr->filtered_pids,
  1742. mutex_is_locked(&event_mutex));
  1743. no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
  1744. mutex_is_locked(&event_mutex));
  1745. this_cpu_write(tr->array_buffer.data->ignore_pid,
  1746. trace_ignore_this_task(pid_list, no_pid_list, current));
  1747. }
  1748. static void register_pid_events(struct trace_array *tr)
  1749. {
  1750. /*
  1751. * Register a probe that is called before all other probes
  1752. * to set ignore_pid if next or prev do not match.
  1753. * Register a probe this is called after all other probes
  1754. * to only keep ignore_pid set if next pid matches.
  1755. */
  1756. register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
  1757. tr, INT_MAX);
  1758. register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
  1759. tr, 0);
  1760. register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
  1761. tr, INT_MAX);
  1762. register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
  1763. tr, 0);
  1764. register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
  1765. tr, INT_MAX);
  1766. register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
  1767. tr, 0);
  1768. register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
  1769. tr, INT_MAX);
  1770. register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
  1771. tr, 0);
  1772. }
  1773. static ssize_t
  1774. event_pid_write(struct file *filp, const char __user *ubuf,
  1775. size_t cnt, loff_t *ppos, int type)
  1776. {
  1777. struct seq_file *m = filp->private_data;
  1778. struct trace_array *tr = m->private;
  1779. struct trace_pid_list *filtered_pids = NULL;
  1780. struct trace_pid_list *other_pids = NULL;
  1781. struct trace_pid_list *pid_list;
  1782. struct trace_event_file *file;
  1783. ssize_t ret;
  1784. if (!cnt)
  1785. return 0;
  1786. ret = tracing_update_buffers(tr);
  1787. if (ret < 0)
  1788. return ret;
  1789. mutex_lock(&event_mutex);
  1790. if (type == TRACE_PIDS) {
  1791. filtered_pids = rcu_dereference_protected(tr->filtered_pids,
  1792. lockdep_is_held(&event_mutex));
  1793. other_pids = rcu_dereference_protected(tr->filtered_no_pids,
  1794. lockdep_is_held(&event_mutex));
  1795. } else {
  1796. filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
  1797. lockdep_is_held(&event_mutex));
  1798. other_pids = rcu_dereference_protected(tr->filtered_pids,
  1799. lockdep_is_held(&event_mutex));
  1800. }
  1801. ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
  1802. if (ret < 0)
  1803. goto out;
  1804. if (type == TRACE_PIDS)
  1805. rcu_assign_pointer(tr->filtered_pids, pid_list);
  1806. else
  1807. rcu_assign_pointer(tr->filtered_no_pids, pid_list);
  1808. list_for_each_entry(file, &tr->events, list) {
  1809. set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
  1810. }
  1811. if (filtered_pids) {
  1812. tracepoint_synchronize_unregister();
  1813. trace_pid_list_free(filtered_pids);
  1814. } else if (pid_list && !other_pids) {
  1815. register_pid_events(tr);
  1816. }
  1817. /*
  1818. * Ignoring of pids is done at task switch. But we have to
  1819. * check for those tasks that are currently running.
  1820. * Always do this in case a pid was appended or removed.
  1821. */
  1822. on_each_cpu(ignore_task_cpu, tr, 1);
  1823. out:
  1824. mutex_unlock(&event_mutex);
  1825. if (ret > 0)
  1826. *ppos += ret;
  1827. return ret;
  1828. }
  1829. static ssize_t
  1830. ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
  1831. size_t cnt, loff_t *ppos)
  1832. {
  1833. return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
  1834. }
  1835. static ssize_t
  1836. ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
  1837. size_t cnt, loff_t *ppos)
  1838. {
  1839. return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
  1840. }
  1841. static int ftrace_event_avail_open(struct inode *inode, struct file *file);
  1842. static int ftrace_event_set_open(struct inode *inode, struct file *file);
  1843. static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
  1844. static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
  1845. static int ftrace_event_release(struct inode *inode, struct file *file);
  1846. static const struct seq_operations show_event_seq_ops = {
  1847. .start = t_start,
  1848. .next = t_next,
  1849. .show = t_show,
  1850. .stop = t_stop,
  1851. };
  1852. static const struct seq_operations show_set_event_seq_ops = {
  1853. .start = s_start,
  1854. .next = s_next,
  1855. .show = t_show,
  1856. .stop = t_stop,
  1857. };
  1858. static const struct seq_operations show_set_pid_seq_ops = {
  1859. .start = p_start,
  1860. .next = p_next,
  1861. .show = trace_pid_show,
  1862. .stop = p_stop,
  1863. };
  1864. static const struct seq_operations show_set_no_pid_seq_ops = {
  1865. .start = np_start,
  1866. .next = np_next,
  1867. .show = trace_pid_show,
  1868. .stop = p_stop,
  1869. };
  1870. static const struct file_operations ftrace_avail_fops = {
  1871. .open = ftrace_event_avail_open,
  1872. .read = seq_read,
  1873. .llseek = seq_lseek,
  1874. .release = seq_release,
  1875. };
  1876. static const struct file_operations ftrace_set_event_fops = {
  1877. .open = ftrace_event_set_open,
  1878. .read = seq_read,
  1879. .write = ftrace_event_write,
  1880. .llseek = seq_lseek,
  1881. .release = ftrace_event_release,
  1882. };
  1883. static const struct file_operations ftrace_set_event_pid_fops = {
  1884. .open = ftrace_event_set_pid_open,
  1885. .read = seq_read,
  1886. .write = ftrace_event_pid_write,
  1887. .llseek = seq_lseek,
  1888. .release = ftrace_event_release,
  1889. };
  1890. static const struct file_operations ftrace_set_event_notrace_pid_fops = {
  1891. .open = ftrace_event_set_npid_open,
  1892. .read = seq_read,
  1893. .write = ftrace_event_npid_write,
  1894. .llseek = seq_lseek,
  1895. .release = ftrace_event_release,
  1896. };
  1897. static const struct file_operations ftrace_enable_fops = {
  1898. .open = tracing_open_file_tr,
  1899. .read = event_enable_read,
  1900. .write = event_enable_write,
  1901. .release = tracing_release_file_tr,
  1902. .llseek = default_llseek,
  1903. };
  1904. static const struct file_operations ftrace_event_format_fops = {
  1905. .open = trace_format_open,
  1906. .read = seq_read,
  1907. .llseek = seq_lseek,
  1908. .release = seq_release,
  1909. };
  1910. #ifdef CONFIG_PERF_EVENTS
  1911. static const struct file_operations ftrace_event_id_fops = {
  1912. .read = event_id_read,
  1913. .llseek = default_llseek,
  1914. };
  1915. #endif
  1916. static const struct file_operations ftrace_event_filter_fops = {
  1917. .open = tracing_open_file_tr,
  1918. .read = event_filter_read,
  1919. .write = event_filter_write,
  1920. .release = tracing_release_file_tr,
  1921. .llseek = default_llseek,
  1922. };
  1923. static const struct file_operations ftrace_subsystem_filter_fops = {
  1924. .open = subsystem_open,
  1925. .read = subsystem_filter_read,
  1926. .write = subsystem_filter_write,
  1927. .llseek = default_llseek,
  1928. .release = subsystem_release,
  1929. };
  1930. static const struct file_operations ftrace_system_enable_fops = {
  1931. .open = subsystem_open,
  1932. .read = system_enable_read,
  1933. .write = system_enable_write,
  1934. .llseek = default_llseek,
  1935. .release = subsystem_release,
  1936. };
  1937. static const struct file_operations ftrace_tr_enable_fops = {
  1938. .open = system_tr_open,
  1939. .read = system_enable_read,
  1940. .write = system_enable_write,
  1941. .llseek = default_llseek,
  1942. .release = subsystem_release,
  1943. };
  1944. static const struct file_operations ftrace_show_header_page_fops = {
  1945. .open = tracing_open_generic_tr,
  1946. .read = show_header_page_file,
  1947. .llseek = default_llseek,
  1948. .release = tracing_release_generic_tr,
  1949. };
  1950. static const struct file_operations ftrace_show_header_event_fops = {
  1951. .open = tracing_open_generic_tr,
  1952. .read = show_header_event_file,
  1953. .llseek = default_llseek,
  1954. .release = tracing_release_generic_tr,
  1955. };
  1956. static int
  1957. ftrace_event_open(struct inode *inode, struct file *file,
  1958. const struct seq_operations *seq_ops)
  1959. {
  1960. struct seq_file *m;
  1961. int ret;
  1962. ret = security_locked_down(LOCKDOWN_TRACEFS);
  1963. if (ret)
  1964. return ret;
  1965. ret = seq_open(file, seq_ops);
  1966. if (ret < 0)
  1967. return ret;
  1968. m = file->private_data;
  1969. /* copy tr over to seq ops */
  1970. m->private = inode->i_private;
  1971. return ret;
  1972. }
  1973. static int ftrace_event_release(struct inode *inode, struct file *file)
  1974. {
  1975. struct trace_array *tr = inode->i_private;
  1976. trace_array_put(tr);
  1977. return seq_release(inode, file);
  1978. }
  1979. static int
  1980. ftrace_event_avail_open(struct inode *inode, struct file *file)
  1981. {
  1982. const struct seq_operations *seq_ops = &show_event_seq_ops;
  1983. /* Checks for tracefs lockdown */
  1984. return ftrace_event_open(inode, file, seq_ops);
  1985. }
  1986. static int
  1987. ftrace_event_set_open(struct inode *inode, struct file *file)
  1988. {
  1989. const struct seq_operations *seq_ops = &show_set_event_seq_ops;
  1990. struct trace_array *tr = inode->i_private;
  1991. int ret;
  1992. ret = tracing_check_open_get_tr(tr);
  1993. if (ret)
  1994. return ret;
  1995. if ((file->f_mode & FMODE_WRITE) &&
  1996. (file->f_flags & O_TRUNC))
  1997. ftrace_clear_events(tr);
  1998. ret = ftrace_event_open(inode, file, seq_ops);
  1999. if (ret < 0)
  2000. trace_array_put(tr);
  2001. return ret;
  2002. }
  2003. static int
  2004. ftrace_event_set_pid_open(struct inode *inode, struct file *file)
  2005. {
  2006. const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
  2007. struct trace_array *tr = inode->i_private;
  2008. int ret;
  2009. ret = tracing_check_open_get_tr(tr);
  2010. if (ret)
  2011. return ret;
  2012. if ((file->f_mode & FMODE_WRITE) &&
  2013. (file->f_flags & O_TRUNC))
  2014. ftrace_clear_event_pids(tr, TRACE_PIDS);
  2015. ret = ftrace_event_open(inode, file, seq_ops);
  2016. if (ret < 0)
  2017. trace_array_put(tr);
  2018. return ret;
  2019. }
  2020. static int
  2021. ftrace_event_set_npid_open(struct inode *inode, struct file *file)
  2022. {
  2023. const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
  2024. struct trace_array *tr = inode->i_private;
  2025. int ret;
  2026. ret = tracing_check_open_get_tr(tr);
  2027. if (ret)
  2028. return ret;
  2029. if ((file->f_mode & FMODE_WRITE) &&
  2030. (file->f_flags & O_TRUNC))
  2031. ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
  2032. ret = ftrace_event_open(inode, file, seq_ops);
  2033. if (ret < 0)
  2034. trace_array_put(tr);
  2035. return ret;
  2036. }
  2037. static struct event_subsystem *
  2038. create_new_subsystem(const char *name)
  2039. {
  2040. struct event_subsystem *system;
  2041. /* need to create new entry */
  2042. system = kmalloc(sizeof(*system), GFP_KERNEL);
  2043. if (!system)
  2044. return NULL;
  2045. system->ref_count = 1;
  2046. /* Only allocate if dynamic (kprobes and modules) */
  2047. system->name = kstrdup_const(name, GFP_KERNEL);
  2048. if (!system->name)
  2049. goto out_free;
  2050. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  2051. if (!system->filter)
  2052. goto out_free;
  2053. list_add(&system->list, &event_subsystems);
  2054. return system;
  2055. out_free:
  2056. kfree_const(system->name);
  2057. kfree(system);
  2058. return NULL;
  2059. }
  2060. static int system_callback(const char *name, umode_t *mode, void **data,
  2061. const struct file_operations **fops)
  2062. {
  2063. if (strcmp(name, "filter") == 0)
  2064. *fops = &ftrace_subsystem_filter_fops;
  2065. else if (strcmp(name, "enable") == 0)
  2066. *fops = &ftrace_system_enable_fops;
  2067. else
  2068. return 0;
  2069. *mode = TRACE_MODE_WRITE;
  2070. return 1;
  2071. }
  2072. static struct eventfs_inode *
  2073. event_subsystem_dir(struct trace_array *tr, const char *name,
  2074. struct trace_event_file *file, struct eventfs_inode *parent)
  2075. {
  2076. struct event_subsystem *system, *iter;
  2077. struct trace_subsystem_dir *dir;
  2078. struct eventfs_inode *ei;
  2079. int nr_entries;
  2080. static struct eventfs_entry system_entries[] = {
  2081. {
  2082. .name = "filter",
  2083. .callback = system_callback,
  2084. },
  2085. {
  2086. .name = "enable",
  2087. .callback = system_callback,
  2088. }
  2089. };
  2090. /* First see if we did not already create this dir */
  2091. list_for_each_entry(dir, &tr->systems, list) {
  2092. system = dir->subsystem;
  2093. if (strcmp(system->name, name) == 0) {
  2094. dir->nr_events++;
  2095. file->system = dir;
  2096. return dir->ei;
  2097. }
  2098. }
  2099. /* Now see if the system itself exists. */
  2100. system = NULL;
  2101. list_for_each_entry(iter, &event_subsystems, list) {
  2102. if (strcmp(iter->name, name) == 0) {
  2103. system = iter;
  2104. break;
  2105. }
  2106. }
  2107. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  2108. if (!dir)
  2109. goto out_fail;
  2110. if (!system) {
  2111. system = create_new_subsystem(name);
  2112. if (!system)
  2113. goto out_free;
  2114. } else
  2115. __get_system(system);
  2116. /* ftrace only has directories no files */
  2117. if (strcmp(name, "ftrace") == 0)
  2118. nr_entries = 0;
  2119. else
  2120. nr_entries = ARRAY_SIZE(system_entries);
  2121. ei = eventfs_create_dir(name, parent, system_entries, nr_entries, dir);
  2122. if (IS_ERR(ei)) {
  2123. pr_warn("Failed to create system directory %s\n", name);
  2124. __put_system(system);
  2125. goto out_free;
  2126. }
  2127. dir->ei = ei;
  2128. dir->tr = tr;
  2129. dir->ref_count = 1;
  2130. dir->nr_events = 1;
  2131. dir->subsystem = system;
  2132. file->system = dir;
  2133. list_add(&dir->list, &tr->systems);
  2134. return dir->ei;
  2135. out_free:
  2136. kfree(dir);
  2137. out_fail:
  2138. /* Only print this message if failed on memory allocation */
  2139. if (!dir || !system)
  2140. pr_warn("No memory to create event subsystem %s\n", name);
  2141. return NULL;
  2142. }
  2143. static int
  2144. event_define_fields(struct trace_event_call *call)
  2145. {
  2146. struct list_head *head;
  2147. int ret = 0;
  2148. /*
  2149. * Other events may have the same class. Only update
  2150. * the fields if they are not already defined.
  2151. */
  2152. head = trace_get_fields(call);
  2153. if (list_empty(head)) {
  2154. struct trace_event_fields *field = call->class->fields_array;
  2155. unsigned int offset = sizeof(struct trace_entry);
  2156. for (; field->type; field++) {
  2157. if (field->type == TRACE_FUNCTION_TYPE) {
  2158. field->define_fields(call);
  2159. break;
  2160. }
  2161. offset = ALIGN(offset, field->align);
  2162. ret = trace_define_field_ext(call, field->type, field->name,
  2163. offset, field->size,
  2164. field->is_signed, field->filter_type,
  2165. field->len, field->needs_test);
  2166. if (WARN_ON_ONCE(ret)) {
  2167. pr_err("error code is %d\n", ret);
  2168. break;
  2169. }
  2170. offset += field->size;
  2171. }
  2172. }
  2173. return ret;
  2174. }
  2175. static int event_callback(const char *name, umode_t *mode, void **data,
  2176. const struct file_operations **fops)
  2177. {
  2178. struct trace_event_file *file = *data;
  2179. struct trace_event_call *call = file->event_call;
  2180. if (strcmp(name, "format") == 0) {
  2181. *mode = TRACE_MODE_READ;
  2182. *fops = &ftrace_event_format_fops;
  2183. return 1;
  2184. }
  2185. /*
  2186. * Only event directories that can be enabled should have
  2187. * triggers or filters, with the exception of the "print"
  2188. * event that can have a "trigger" file.
  2189. */
  2190. if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
  2191. if (call->class->reg && strcmp(name, "enable") == 0) {
  2192. *mode = TRACE_MODE_WRITE;
  2193. *fops = &ftrace_enable_fops;
  2194. return 1;
  2195. }
  2196. if (strcmp(name, "filter") == 0) {
  2197. *mode = TRACE_MODE_WRITE;
  2198. *fops = &ftrace_event_filter_fops;
  2199. return 1;
  2200. }
  2201. }
  2202. if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
  2203. strcmp(trace_event_name(call), "print") == 0) {
  2204. if (strcmp(name, "trigger") == 0) {
  2205. *mode = TRACE_MODE_WRITE;
  2206. *fops = &event_trigger_fops;
  2207. return 1;
  2208. }
  2209. }
  2210. #ifdef CONFIG_PERF_EVENTS
  2211. if (call->event.type && call->class->reg &&
  2212. strcmp(name, "id") == 0) {
  2213. *mode = TRACE_MODE_READ;
  2214. *data = (void *)(long)call->event.type;
  2215. *fops = &ftrace_event_id_fops;
  2216. return 1;
  2217. }
  2218. #endif
  2219. #ifdef CONFIG_HIST_TRIGGERS
  2220. if (strcmp(name, "hist") == 0) {
  2221. *mode = TRACE_MODE_READ;
  2222. *fops = &event_hist_fops;
  2223. return 1;
  2224. }
  2225. #endif
  2226. #ifdef CONFIG_HIST_TRIGGERS_DEBUG
  2227. if (strcmp(name, "hist_debug") == 0) {
  2228. *mode = TRACE_MODE_READ;
  2229. *fops = &event_hist_debug_fops;
  2230. return 1;
  2231. }
  2232. #endif
  2233. #ifdef CONFIG_TRACE_EVENT_INJECT
  2234. if (call->event.type && call->class->reg &&
  2235. strcmp(name, "inject") == 0) {
  2236. *mode = 0200;
  2237. *fops = &event_inject_fops;
  2238. return 1;
  2239. }
  2240. #endif
  2241. return 0;
  2242. }
  2243. /* The file is incremented on creation and freeing the enable file decrements it */
  2244. static void event_release(const char *name, void *data)
  2245. {
  2246. struct trace_event_file *file = data;
  2247. event_file_put(file);
  2248. }
  2249. static int
  2250. event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file)
  2251. {
  2252. struct trace_event_call *call = file->event_call;
  2253. struct trace_array *tr = file->tr;
  2254. struct eventfs_inode *e_events;
  2255. struct eventfs_inode *ei;
  2256. const char *name;
  2257. int nr_entries;
  2258. int ret;
  2259. static struct eventfs_entry event_entries[] = {
  2260. {
  2261. .name = "enable",
  2262. .callback = event_callback,
  2263. .release = event_release,
  2264. },
  2265. {
  2266. .name = "filter",
  2267. .callback = event_callback,
  2268. },
  2269. {
  2270. .name = "trigger",
  2271. .callback = event_callback,
  2272. },
  2273. {
  2274. .name = "format",
  2275. .callback = event_callback,
  2276. },
  2277. #ifdef CONFIG_PERF_EVENTS
  2278. {
  2279. .name = "id",
  2280. .callback = event_callback,
  2281. },
  2282. #endif
  2283. #ifdef CONFIG_HIST_TRIGGERS
  2284. {
  2285. .name = "hist",
  2286. .callback = event_callback,
  2287. },
  2288. #endif
  2289. #ifdef CONFIG_HIST_TRIGGERS_DEBUG
  2290. {
  2291. .name = "hist_debug",
  2292. .callback = event_callback,
  2293. },
  2294. #endif
  2295. #ifdef CONFIG_TRACE_EVENT_INJECT
  2296. {
  2297. .name = "inject",
  2298. .callback = event_callback,
  2299. },
  2300. #endif
  2301. };
  2302. /*
  2303. * If the trace point header did not define TRACE_SYSTEM
  2304. * then the system would be called "TRACE_SYSTEM". This should
  2305. * never happen.
  2306. */
  2307. if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0))
  2308. return -ENODEV;
  2309. e_events = event_subsystem_dir(tr, call->class->system, file, parent);
  2310. if (!e_events)
  2311. return -ENOMEM;
  2312. nr_entries = ARRAY_SIZE(event_entries);
  2313. name = trace_event_name(call);
  2314. ei = eventfs_create_dir(name, e_events, event_entries, nr_entries, file);
  2315. if (IS_ERR(ei)) {
  2316. pr_warn("Could not create tracefs '%s' directory\n", name);
  2317. return -1;
  2318. }
  2319. file->ei = ei;
  2320. ret = event_define_fields(call);
  2321. if (ret < 0) {
  2322. pr_warn("Could not initialize trace point events/%s\n", name);
  2323. return ret;
  2324. }
  2325. /* Gets decremented on freeing of the "enable" file */
  2326. event_file_get(file);
  2327. return 0;
  2328. }
  2329. static void remove_event_from_tracers(struct trace_event_call *call)
  2330. {
  2331. struct trace_event_file *file;
  2332. struct trace_array *tr;
  2333. do_for_each_event_file_safe(tr, file) {
  2334. if (file->event_call != call)
  2335. continue;
  2336. remove_event_file_dir(file);
  2337. /*
  2338. * The do_for_each_event_file_safe() is
  2339. * a double loop. After finding the call for this
  2340. * trace_array, we use break to jump to the next
  2341. * trace_array.
  2342. */
  2343. break;
  2344. } while_for_each_event_file();
  2345. }
  2346. static void event_remove(struct trace_event_call *call)
  2347. {
  2348. struct trace_array *tr;
  2349. struct trace_event_file *file;
  2350. do_for_each_event_file(tr, file) {
  2351. if (file->event_call != call)
  2352. continue;
  2353. if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
  2354. tr->clear_trace = true;
  2355. ftrace_event_enable_disable(file, 0);
  2356. /*
  2357. * The do_for_each_event_file() is
  2358. * a double loop. After finding the call for this
  2359. * trace_array, we use break to jump to the next
  2360. * trace_array.
  2361. */
  2362. break;
  2363. } while_for_each_event_file();
  2364. if (call->event.funcs)
  2365. __unregister_trace_event(&call->event);
  2366. remove_event_from_tracers(call);
  2367. list_del(&call->list);
  2368. }
  2369. static int event_init(struct trace_event_call *call)
  2370. {
  2371. int ret = 0;
  2372. const char *name;
  2373. name = trace_event_name(call);
  2374. if (WARN_ON(!name))
  2375. return -EINVAL;
  2376. if (call->class->raw_init) {
  2377. ret = call->class->raw_init(call);
  2378. if (ret < 0 && ret != -ENOSYS)
  2379. pr_warn("Could not initialize trace events/%s\n", name);
  2380. }
  2381. return ret;
  2382. }
  2383. static int
  2384. __register_event(struct trace_event_call *call, struct module *mod)
  2385. {
  2386. int ret;
  2387. ret = event_init(call);
  2388. if (ret < 0)
  2389. return ret;
  2390. down_write(&trace_event_sem);
  2391. list_add(&call->list, &ftrace_events);
  2392. up_write(&trace_event_sem);
  2393. if (call->flags & TRACE_EVENT_FL_DYNAMIC)
  2394. atomic_set(&call->refcnt, 0);
  2395. else
  2396. call->module = mod;
  2397. return 0;
  2398. }
  2399. static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
  2400. {
  2401. int rlen;
  2402. int elen;
  2403. /* Find the length of the eval value as a string */
  2404. elen = snprintf(ptr, 0, "%ld", map->eval_value);
  2405. /* Make sure there's enough room to replace the string with the value */
  2406. if (len < elen)
  2407. return NULL;
  2408. snprintf(ptr, elen + 1, "%ld", map->eval_value);
  2409. /* Get the rest of the string of ptr */
  2410. rlen = strlen(ptr + len);
  2411. memmove(ptr + elen, ptr + len, rlen);
  2412. /* Make sure we end the new string */
  2413. ptr[elen + rlen] = 0;
  2414. return ptr + elen;
  2415. }
  2416. static void update_event_printk(struct trace_event_call *call,
  2417. struct trace_eval_map *map)
  2418. {
  2419. char *ptr;
  2420. int quote = 0;
  2421. int len = strlen(map->eval_string);
  2422. for (ptr = call->print_fmt; *ptr; ptr++) {
  2423. if (*ptr == '\\') {
  2424. ptr++;
  2425. /* paranoid */
  2426. if (!*ptr)
  2427. break;
  2428. continue;
  2429. }
  2430. if (*ptr == '"') {
  2431. quote ^= 1;
  2432. continue;
  2433. }
  2434. if (quote)
  2435. continue;
  2436. if (isdigit(*ptr)) {
  2437. /* skip numbers */
  2438. do {
  2439. ptr++;
  2440. /* Check for alpha chars like ULL */
  2441. } while (isalnum(*ptr));
  2442. if (!*ptr)
  2443. break;
  2444. /*
  2445. * A number must have some kind of delimiter after
  2446. * it, and we can ignore that too.
  2447. */
  2448. continue;
  2449. }
  2450. if (isalpha(*ptr) || *ptr == '_') {
  2451. if (strncmp(map->eval_string, ptr, len) == 0 &&
  2452. !isalnum(ptr[len]) && ptr[len] != '_') {
  2453. ptr = eval_replace(ptr, map, len);
  2454. /* enum/sizeof string smaller than value */
  2455. if (WARN_ON_ONCE(!ptr))
  2456. return;
  2457. /*
  2458. * No need to decrement here, as eval_replace()
  2459. * returns the pointer to the character passed
  2460. * the eval, and two evals can not be placed
  2461. * back to back without something in between.
  2462. * We can skip that something in between.
  2463. */
  2464. continue;
  2465. }
  2466. skip_more:
  2467. do {
  2468. ptr++;
  2469. } while (isalnum(*ptr) || *ptr == '_');
  2470. if (!*ptr)
  2471. break;
  2472. /*
  2473. * If what comes after this variable is a '.' or
  2474. * '->' then we can continue to ignore that string.
  2475. */
  2476. if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
  2477. ptr += *ptr == '.' ? 1 : 2;
  2478. if (!*ptr)
  2479. break;
  2480. goto skip_more;
  2481. }
  2482. /*
  2483. * Once again, we can skip the delimiter that came
  2484. * after the string.
  2485. */
  2486. continue;
  2487. }
  2488. }
  2489. }
  2490. static void add_str_to_module(struct module *module, char *str)
  2491. {
  2492. struct module_string *modstr;
  2493. modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
  2494. /*
  2495. * If we failed to allocate memory here, then we'll just
  2496. * let the str memory leak when the module is removed.
  2497. * If this fails to allocate, there's worse problems than
  2498. * a leaked string on module removal.
  2499. */
  2500. if (WARN_ON_ONCE(!modstr))
  2501. return;
  2502. modstr->module = module;
  2503. modstr->str = str;
  2504. list_add(&modstr->next, &module_strings);
  2505. }
  2506. static void update_event_fields(struct trace_event_call *call,
  2507. struct trace_eval_map *map)
  2508. {
  2509. struct ftrace_event_field *field;
  2510. struct list_head *head;
  2511. char *ptr;
  2512. char *str;
  2513. int len = strlen(map->eval_string);
  2514. /* Dynamic events should never have field maps */
  2515. if (WARN_ON_ONCE(call->flags & TRACE_EVENT_FL_DYNAMIC))
  2516. return;
  2517. head = trace_get_fields(call);
  2518. list_for_each_entry(field, head, link) {
  2519. ptr = strchr(field->type, '[');
  2520. if (!ptr)
  2521. continue;
  2522. ptr++;
  2523. if (!isalpha(*ptr) && *ptr != '_')
  2524. continue;
  2525. if (strncmp(map->eval_string, ptr, len) != 0)
  2526. continue;
  2527. str = kstrdup(field->type, GFP_KERNEL);
  2528. if (WARN_ON_ONCE(!str))
  2529. return;
  2530. ptr = str + (ptr - field->type);
  2531. ptr = eval_replace(ptr, map, len);
  2532. /* enum/sizeof string smaller than value */
  2533. if (WARN_ON_ONCE(!ptr)) {
  2534. kfree(str);
  2535. continue;
  2536. }
  2537. /*
  2538. * If the event is part of a module, then we need to free the string
  2539. * when the module is removed. Otherwise, it will stay allocated
  2540. * until a reboot.
  2541. */
  2542. if (call->module)
  2543. add_str_to_module(call->module, str);
  2544. field->type = str;
  2545. }
  2546. }
  2547. void trace_event_eval_update(struct trace_eval_map **map, int len)
  2548. {
  2549. struct trace_event_call *call, *p;
  2550. const char *last_system = NULL;
  2551. bool first = false;
  2552. int last_i;
  2553. int i;
  2554. down_write(&trace_event_sem);
  2555. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  2556. /* events are usually grouped together with systems */
  2557. if (!last_system || call->class->system != last_system) {
  2558. first = true;
  2559. last_i = 0;
  2560. last_system = call->class->system;
  2561. }
  2562. /*
  2563. * Since calls are grouped by systems, the likelihood that the
  2564. * next call in the iteration belongs to the same system as the
  2565. * previous call is high. As an optimization, we skip searching
  2566. * for a map[] that matches the call's system if the last call
  2567. * was from the same system. That's what last_i is for. If the
  2568. * call has the same system as the previous call, then last_i
  2569. * will be the index of the first map[] that has a matching
  2570. * system.
  2571. */
  2572. for (i = last_i; i < len; i++) {
  2573. if (call->class->system == map[i]->system) {
  2574. /* Save the first system if need be */
  2575. if (first) {
  2576. last_i = i;
  2577. first = false;
  2578. }
  2579. update_event_printk(call, map[i]);
  2580. update_event_fields(call, map[i]);
  2581. }
  2582. }
  2583. cond_resched();
  2584. }
  2585. up_write(&trace_event_sem);
  2586. }
  2587. static bool event_in_systems(struct trace_event_call *call,
  2588. const char *systems)
  2589. {
  2590. const char *system;
  2591. const char *p;
  2592. if (!systems)
  2593. return true;
  2594. system = call->class->system;
  2595. p = strstr(systems, system);
  2596. if (!p)
  2597. return false;
  2598. if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',')
  2599. return false;
  2600. p += strlen(system);
  2601. return !*p || isspace(*p) || *p == ',';
  2602. }
  2603. #ifdef CONFIG_HIST_TRIGGERS
  2604. /*
  2605. * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger
  2606. * may happen in any context.
  2607. */
  2608. static void hist_poll_event_irq_work(struct irq_work *work)
  2609. {
  2610. wake_up_all(&hist_poll_wq);
  2611. }
  2612. DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work);
  2613. DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq);
  2614. #endif
  2615. static struct trace_event_file *
  2616. trace_create_new_event(struct trace_event_call *call,
  2617. struct trace_array *tr)
  2618. {
  2619. struct trace_pid_list *no_pid_list;
  2620. struct trace_pid_list *pid_list;
  2621. struct trace_event_file *file;
  2622. unsigned int first;
  2623. if (!event_in_systems(call, tr->system_names))
  2624. return NULL;
  2625. file = kmem_cache_alloc(file_cachep, GFP_TRACE);
  2626. if (!file)
  2627. return ERR_PTR(-ENOMEM);
  2628. pid_list = rcu_dereference_protected(tr->filtered_pids,
  2629. lockdep_is_held(&event_mutex));
  2630. no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
  2631. lockdep_is_held(&event_mutex));
  2632. if (!trace_pid_list_first(pid_list, &first) ||
  2633. !trace_pid_list_first(no_pid_list, &first))
  2634. file->flags |= EVENT_FILE_FL_PID_FILTER;
  2635. file->event_call = call;
  2636. file->tr = tr;
  2637. atomic_set(&file->sm_ref, 0);
  2638. atomic_set(&file->tm_ref, 0);
  2639. INIT_LIST_HEAD(&file->triggers);
  2640. list_add(&file->list, &tr->events);
  2641. refcount_set(&file->ref, 1);
  2642. return file;
  2643. }
  2644. #define MAX_BOOT_TRIGGERS 32
  2645. static struct boot_triggers {
  2646. const char *event;
  2647. char *trigger;
  2648. } bootup_triggers[MAX_BOOT_TRIGGERS];
  2649. static char bootup_trigger_buf[COMMAND_LINE_SIZE];
  2650. static int nr_boot_triggers;
  2651. static __init int setup_trace_triggers(char *str)
  2652. {
  2653. char *trigger;
  2654. char *buf;
  2655. int i;
  2656. strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
  2657. trace_set_ring_buffer_expanded(NULL);
  2658. disable_tracing_selftest("running event triggers");
  2659. buf = bootup_trigger_buf;
  2660. for (i = 0; i < MAX_BOOT_TRIGGERS; i++) {
  2661. trigger = strsep(&buf, ",");
  2662. if (!trigger)
  2663. break;
  2664. bootup_triggers[i].event = strsep(&trigger, ".");
  2665. bootup_triggers[i].trigger = trigger;
  2666. if (!bootup_triggers[i].trigger)
  2667. break;
  2668. }
  2669. nr_boot_triggers = i;
  2670. return 1;
  2671. }
  2672. __setup("trace_trigger=", setup_trace_triggers);
  2673. /* Add an event to a trace directory */
  2674. static int
  2675. __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
  2676. {
  2677. struct trace_event_file *file;
  2678. file = trace_create_new_event(call, tr);
  2679. /*
  2680. * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
  2681. * allocation, or NULL if the event is not part of the tr->system_names.
  2682. * When the event is not part of the tr->system_names, return zero, not
  2683. * an error.
  2684. */
  2685. if (!file)
  2686. return 0;
  2687. if (IS_ERR(file))
  2688. return PTR_ERR(file);
  2689. if (eventdir_initialized)
  2690. return event_create_dir(tr->event_dir, file);
  2691. else
  2692. return event_define_fields(call);
  2693. }
  2694. static void trace_early_triggers(struct trace_event_file *file, const char *name)
  2695. {
  2696. int ret;
  2697. int i;
  2698. for (i = 0; i < nr_boot_triggers; i++) {
  2699. if (strcmp(name, bootup_triggers[i].event))
  2700. continue;
  2701. mutex_lock(&event_mutex);
  2702. ret = trigger_process_regex(file, bootup_triggers[i].trigger);
  2703. mutex_unlock(&event_mutex);
  2704. if (ret)
  2705. pr_err("Failed to register trigger '%s' on event %s\n",
  2706. bootup_triggers[i].trigger,
  2707. bootup_triggers[i].event);
  2708. }
  2709. }
  2710. /*
  2711. * Just create a descriptor for early init. A descriptor is required
  2712. * for enabling events at boot. We want to enable events before
  2713. * the filesystem is initialized.
  2714. */
  2715. static int
  2716. __trace_early_add_new_event(struct trace_event_call *call,
  2717. struct trace_array *tr)
  2718. {
  2719. struct trace_event_file *file;
  2720. int ret;
  2721. file = trace_create_new_event(call, tr);
  2722. /*
  2723. * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
  2724. * allocation, or NULL if the event is not part of the tr->system_names.
  2725. * When the event is not part of the tr->system_names, return zero, not
  2726. * an error.
  2727. */
  2728. if (!file)
  2729. return 0;
  2730. if (IS_ERR(file))
  2731. return PTR_ERR(file);
  2732. ret = event_define_fields(call);
  2733. if (ret)
  2734. return ret;
  2735. trace_early_triggers(file, trace_event_name(call));
  2736. return 0;
  2737. }
  2738. struct ftrace_module_file_ops;
  2739. static void __add_event_to_tracers(struct trace_event_call *call);
  2740. /* Add an additional event_call dynamically */
  2741. int trace_add_event_call(struct trace_event_call *call)
  2742. {
  2743. int ret;
  2744. lockdep_assert_held(&event_mutex);
  2745. mutex_lock(&trace_types_lock);
  2746. ret = __register_event(call, NULL);
  2747. if (ret >= 0)
  2748. __add_event_to_tracers(call);
  2749. mutex_unlock(&trace_types_lock);
  2750. return ret;
  2751. }
  2752. EXPORT_SYMBOL_GPL(trace_add_event_call);
  2753. /*
  2754. * Must be called under locking of trace_types_lock, event_mutex and
  2755. * trace_event_sem.
  2756. */
  2757. static void __trace_remove_event_call(struct trace_event_call *call)
  2758. {
  2759. event_remove(call);
  2760. trace_destroy_fields(call);
  2761. free_event_filter(call->filter);
  2762. call->filter = NULL;
  2763. }
  2764. static int probe_remove_event_call(struct trace_event_call *call)
  2765. {
  2766. struct trace_array *tr;
  2767. struct trace_event_file *file;
  2768. #ifdef CONFIG_PERF_EVENTS
  2769. if (call->perf_refcount)
  2770. return -EBUSY;
  2771. #endif
  2772. do_for_each_event_file(tr, file) {
  2773. if (file->event_call != call)
  2774. continue;
  2775. /*
  2776. * We can't rely on ftrace_event_enable_disable(enable => 0)
  2777. * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
  2778. * TRACE_REG_UNREGISTER.
  2779. */
  2780. if (file->flags & EVENT_FILE_FL_ENABLED)
  2781. goto busy;
  2782. if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
  2783. tr->clear_trace = true;
  2784. /*
  2785. * The do_for_each_event_file_safe() is
  2786. * a double loop. After finding the call for this
  2787. * trace_array, we use break to jump to the next
  2788. * trace_array.
  2789. */
  2790. break;
  2791. } while_for_each_event_file();
  2792. __trace_remove_event_call(call);
  2793. return 0;
  2794. busy:
  2795. /* No need to clear the trace now */
  2796. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  2797. tr->clear_trace = false;
  2798. }
  2799. return -EBUSY;
  2800. }
  2801. /* Remove an event_call */
  2802. int trace_remove_event_call(struct trace_event_call *call)
  2803. {
  2804. int ret;
  2805. lockdep_assert_held(&event_mutex);
  2806. mutex_lock(&trace_types_lock);
  2807. down_write(&trace_event_sem);
  2808. ret = probe_remove_event_call(call);
  2809. up_write(&trace_event_sem);
  2810. mutex_unlock(&trace_types_lock);
  2811. return ret;
  2812. }
  2813. EXPORT_SYMBOL_GPL(trace_remove_event_call);
  2814. #define for_each_event(event, start, end) \
  2815. for (event = start; \
  2816. (unsigned long)event < (unsigned long)end; \
  2817. event++)
  2818. #ifdef CONFIG_MODULES
  2819. static void trace_module_add_events(struct module *mod)
  2820. {
  2821. struct trace_event_call **call, **start, **end;
  2822. if (!mod->num_trace_events)
  2823. return;
  2824. /* Don't add infrastructure for mods without tracepoints */
  2825. if (trace_module_has_bad_taint(mod)) {
  2826. pr_err("%s: module has bad taint, not creating trace events\n",
  2827. mod->name);
  2828. return;
  2829. }
  2830. start = mod->trace_events;
  2831. end = mod->trace_events + mod->num_trace_events;
  2832. for_each_event(call, start, end) {
  2833. __register_event(*call, mod);
  2834. __add_event_to_tracers(*call);
  2835. }
  2836. }
  2837. static void trace_module_remove_events(struct module *mod)
  2838. {
  2839. struct trace_event_call *call, *p;
  2840. struct module_string *modstr, *m;
  2841. down_write(&trace_event_sem);
  2842. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  2843. if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
  2844. continue;
  2845. if (call->module == mod)
  2846. __trace_remove_event_call(call);
  2847. }
  2848. /* Check for any strings allocade for this module */
  2849. list_for_each_entry_safe(modstr, m, &module_strings, next) {
  2850. if (modstr->module != mod)
  2851. continue;
  2852. list_del(&modstr->next);
  2853. kfree(modstr->str);
  2854. kfree(modstr);
  2855. }
  2856. up_write(&trace_event_sem);
  2857. /*
  2858. * It is safest to reset the ring buffer if the module being unloaded
  2859. * registered any events that were used. The only worry is if
  2860. * a new module gets loaded, and takes on the same id as the events
  2861. * of this module. When printing out the buffer, traced events left
  2862. * over from this module may be passed to the new module events and
  2863. * unexpected results may occur.
  2864. */
  2865. tracing_reset_all_online_cpus_unlocked();
  2866. }
  2867. static int trace_module_notify(struct notifier_block *self,
  2868. unsigned long val, void *data)
  2869. {
  2870. struct module *mod = data;
  2871. mutex_lock(&event_mutex);
  2872. mutex_lock(&trace_types_lock);
  2873. switch (val) {
  2874. case MODULE_STATE_COMING:
  2875. trace_module_add_events(mod);
  2876. break;
  2877. case MODULE_STATE_GOING:
  2878. trace_module_remove_events(mod);
  2879. break;
  2880. }
  2881. mutex_unlock(&trace_types_lock);
  2882. mutex_unlock(&event_mutex);
  2883. return NOTIFY_OK;
  2884. }
  2885. static struct notifier_block trace_module_nb = {
  2886. .notifier_call = trace_module_notify,
  2887. .priority = 1, /* higher than trace.c module notify */
  2888. };
  2889. #endif /* CONFIG_MODULES */
  2890. /* Create a new event directory structure for a trace directory. */
  2891. static void
  2892. __trace_add_event_dirs(struct trace_array *tr)
  2893. {
  2894. struct trace_event_call *call;
  2895. int ret;
  2896. lockdep_assert_held(&trace_event_sem);
  2897. list_for_each_entry(call, &ftrace_events, list) {
  2898. ret = __trace_add_new_event(call, tr);
  2899. if (ret < 0)
  2900. pr_warn("Could not create directory for event %s\n",
  2901. trace_event_name(call));
  2902. }
  2903. }
  2904. /* Returns any file that matches the system and event */
  2905. struct trace_event_file *
  2906. __find_event_file(struct trace_array *tr, const char *system, const char *event)
  2907. {
  2908. struct trace_event_file *file;
  2909. struct trace_event_call *call;
  2910. const char *name;
  2911. list_for_each_entry(file, &tr->events, list) {
  2912. call = file->event_call;
  2913. name = trace_event_name(call);
  2914. if (!name || !call->class)
  2915. continue;
  2916. if (strcmp(event, name) == 0 &&
  2917. strcmp(system, call->class->system) == 0)
  2918. return file;
  2919. }
  2920. return NULL;
  2921. }
  2922. /* Returns valid trace event files that match system and event */
  2923. struct trace_event_file *
  2924. find_event_file(struct trace_array *tr, const char *system, const char *event)
  2925. {
  2926. struct trace_event_file *file;
  2927. file = __find_event_file(tr, system, event);
  2928. if (!file || !file->event_call->class->reg ||
  2929. file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  2930. return NULL;
  2931. return file;
  2932. }
  2933. /**
  2934. * trace_get_event_file - Find and return a trace event file
  2935. * @instance: The name of the trace instance containing the event
  2936. * @system: The name of the system containing the event
  2937. * @event: The name of the event
  2938. *
  2939. * Return a trace event file given the trace instance name, trace
  2940. * system, and trace event name. If the instance name is NULL, it
  2941. * refers to the top-level trace array.
  2942. *
  2943. * This function will look it up and return it if found, after calling
  2944. * trace_array_get() to prevent the instance from going away, and
  2945. * increment the event's module refcount to prevent it from being
  2946. * removed.
  2947. *
  2948. * To release the file, call trace_put_event_file(), which will call
  2949. * trace_array_put() and decrement the event's module refcount.
  2950. *
  2951. * Return: The trace event on success, ERR_PTR otherwise.
  2952. */
  2953. struct trace_event_file *trace_get_event_file(const char *instance,
  2954. const char *system,
  2955. const char *event)
  2956. {
  2957. struct trace_array *tr = top_trace_array();
  2958. struct trace_event_file *file = NULL;
  2959. int ret = -EINVAL;
  2960. if (instance) {
  2961. tr = trace_array_find_get(instance);
  2962. if (!tr)
  2963. return ERR_PTR(-ENOENT);
  2964. } else {
  2965. ret = trace_array_get(tr);
  2966. if (ret)
  2967. return ERR_PTR(ret);
  2968. }
  2969. mutex_lock(&event_mutex);
  2970. file = find_event_file(tr, system, event);
  2971. if (!file) {
  2972. trace_array_put(tr);
  2973. ret = -EINVAL;
  2974. goto out;
  2975. }
  2976. /* Don't let event modules unload while in use */
  2977. ret = trace_event_try_get_ref(file->event_call);
  2978. if (!ret) {
  2979. trace_array_put(tr);
  2980. ret = -EBUSY;
  2981. goto out;
  2982. }
  2983. ret = 0;
  2984. out:
  2985. mutex_unlock(&event_mutex);
  2986. if (ret)
  2987. file = ERR_PTR(ret);
  2988. return file;
  2989. }
  2990. EXPORT_SYMBOL_GPL(trace_get_event_file);
  2991. /**
  2992. * trace_put_event_file - Release a file from trace_get_event_file()
  2993. * @file: The trace event file
  2994. *
  2995. * If a file was retrieved using trace_get_event_file(), this should
  2996. * be called when it's no longer needed. It will cancel the previous
  2997. * trace_array_get() called by that function, and decrement the
  2998. * event's module refcount.
  2999. */
  3000. void trace_put_event_file(struct trace_event_file *file)
  3001. {
  3002. mutex_lock(&event_mutex);
  3003. trace_event_put_ref(file->event_call);
  3004. mutex_unlock(&event_mutex);
  3005. trace_array_put(file->tr);
  3006. }
  3007. EXPORT_SYMBOL_GPL(trace_put_event_file);
  3008. #ifdef CONFIG_DYNAMIC_FTRACE
  3009. /* Avoid typos */
  3010. #define ENABLE_EVENT_STR "enable_event"
  3011. #define DISABLE_EVENT_STR "disable_event"
  3012. struct event_probe_data {
  3013. struct trace_event_file *file;
  3014. unsigned long count;
  3015. int ref;
  3016. bool enable;
  3017. };
  3018. static void update_event_probe(struct event_probe_data *data)
  3019. {
  3020. if (data->enable)
  3021. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  3022. else
  3023. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  3024. }
  3025. static void
  3026. event_enable_probe(unsigned long ip, unsigned long parent_ip,
  3027. struct trace_array *tr, struct ftrace_probe_ops *ops,
  3028. void *data)
  3029. {
  3030. struct ftrace_func_mapper *mapper = data;
  3031. struct event_probe_data *edata;
  3032. void **pdata;
  3033. pdata = ftrace_func_mapper_find_ip(mapper, ip);
  3034. if (!pdata || !*pdata)
  3035. return;
  3036. edata = *pdata;
  3037. update_event_probe(edata);
  3038. }
  3039. static void
  3040. event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
  3041. struct trace_array *tr, struct ftrace_probe_ops *ops,
  3042. void *data)
  3043. {
  3044. struct ftrace_func_mapper *mapper = data;
  3045. struct event_probe_data *edata;
  3046. void **pdata;
  3047. pdata = ftrace_func_mapper_find_ip(mapper, ip);
  3048. if (!pdata || !*pdata)
  3049. return;
  3050. edata = *pdata;
  3051. if (!edata->count)
  3052. return;
  3053. /* Skip if the event is in a state we want to switch to */
  3054. if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  3055. return;
  3056. if (edata->count != -1)
  3057. (edata->count)--;
  3058. update_event_probe(edata);
  3059. }
  3060. static int
  3061. event_enable_print(struct seq_file *m, unsigned long ip,
  3062. struct ftrace_probe_ops *ops, void *data)
  3063. {
  3064. struct ftrace_func_mapper *mapper = data;
  3065. struct event_probe_data *edata;
  3066. void **pdata;
  3067. pdata = ftrace_func_mapper_find_ip(mapper, ip);
  3068. if (WARN_ON_ONCE(!pdata || !*pdata))
  3069. return 0;
  3070. edata = *pdata;
  3071. seq_printf(m, "%ps:", (void *)ip);
  3072. seq_printf(m, "%s:%s:%s",
  3073. edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
  3074. edata->file->event_call->class->system,
  3075. trace_event_name(edata->file->event_call));
  3076. if (edata->count == -1)
  3077. seq_puts(m, ":unlimited\n");
  3078. else
  3079. seq_printf(m, ":count=%ld\n", edata->count);
  3080. return 0;
  3081. }
  3082. static int
  3083. event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
  3084. unsigned long ip, void *init_data, void **data)
  3085. {
  3086. struct ftrace_func_mapper *mapper = *data;
  3087. struct event_probe_data *edata = init_data;
  3088. int ret;
  3089. if (!mapper) {
  3090. mapper = allocate_ftrace_func_mapper();
  3091. if (!mapper)
  3092. return -ENODEV;
  3093. *data = mapper;
  3094. }
  3095. ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
  3096. if (ret < 0)
  3097. return ret;
  3098. edata->ref++;
  3099. return 0;
  3100. }
  3101. static int free_probe_data(void *data)
  3102. {
  3103. struct event_probe_data *edata = data;
  3104. edata->ref--;
  3105. if (!edata->ref) {
  3106. /* Remove the SOFT_MODE flag */
  3107. __ftrace_event_enable_disable(edata->file, 0, 1);
  3108. trace_event_put_ref(edata->file->event_call);
  3109. kfree(edata);
  3110. }
  3111. return 0;
  3112. }
  3113. static void
  3114. event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
  3115. unsigned long ip, void *data)
  3116. {
  3117. struct ftrace_func_mapper *mapper = data;
  3118. struct event_probe_data *edata;
  3119. if (!ip) {
  3120. if (!mapper)
  3121. return;
  3122. free_ftrace_func_mapper(mapper, free_probe_data);
  3123. return;
  3124. }
  3125. edata = ftrace_func_mapper_remove_ip(mapper, ip);
  3126. if (WARN_ON_ONCE(!edata))
  3127. return;
  3128. if (WARN_ON_ONCE(edata->ref <= 0))
  3129. return;
  3130. free_probe_data(edata);
  3131. }
  3132. static struct ftrace_probe_ops event_enable_probe_ops = {
  3133. .func = event_enable_probe,
  3134. .print = event_enable_print,
  3135. .init = event_enable_init,
  3136. .free = event_enable_free,
  3137. };
  3138. static struct ftrace_probe_ops event_enable_count_probe_ops = {
  3139. .func = event_enable_count_probe,
  3140. .print = event_enable_print,
  3141. .init = event_enable_init,
  3142. .free = event_enable_free,
  3143. };
  3144. static struct ftrace_probe_ops event_disable_probe_ops = {
  3145. .func = event_enable_probe,
  3146. .print = event_enable_print,
  3147. .init = event_enable_init,
  3148. .free = event_enable_free,
  3149. };
  3150. static struct ftrace_probe_ops event_disable_count_probe_ops = {
  3151. .func = event_enable_count_probe,
  3152. .print = event_enable_print,
  3153. .init = event_enable_init,
  3154. .free = event_enable_free,
  3155. };
  3156. static int
  3157. event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
  3158. char *glob, char *cmd, char *param, int enabled)
  3159. {
  3160. struct trace_event_file *file;
  3161. struct ftrace_probe_ops *ops;
  3162. struct event_probe_data *data;
  3163. const char *system;
  3164. const char *event;
  3165. char *number;
  3166. bool enable;
  3167. int ret;
  3168. if (!tr)
  3169. return -ENODEV;
  3170. /* hash funcs only work with set_ftrace_filter */
  3171. if (!enabled || !param)
  3172. return -EINVAL;
  3173. system = strsep(&param, ":");
  3174. if (!param)
  3175. return -EINVAL;
  3176. event = strsep(&param, ":");
  3177. mutex_lock(&event_mutex);
  3178. ret = -EINVAL;
  3179. file = find_event_file(tr, system, event);
  3180. if (!file)
  3181. goto out;
  3182. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  3183. if (enable)
  3184. ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
  3185. else
  3186. ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
  3187. if (glob[0] == '!') {
  3188. ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
  3189. goto out;
  3190. }
  3191. ret = -ENOMEM;
  3192. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3193. if (!data)
  3194. goto out;
  3195. data->enable = enable;
  3196. data->count = -1;
  3197. data->file = file;
  3198. if (!param)
  3199. goto out_reg;
  3200. number = strsep(&param, ":");
  3201. ret = -EINVAL;
  3202. if (!strlen(number))
  3203. goto out_free;
  3204. /*
  3205. * We use the callback data field (which is a pointer)
  3206. * as our counter.
  3207. */
  3208. ret = kstrtoul(number, 0, &data->count);
  3209. if (ret)
  3210. goto out_free;
  3211. out_reg:
  3212. /* Don't let event modules unload while probe registered */
  3213. ret = trace_event_try_get_ref(file->event_call);
  3214. if (!ret) {
  3215. ret = -EBUSY;
  3216. goto out_free;
  3217. }
  3218. ret = __ftrace_event_enable_disable(file, 1, 1);
  3219. if (ret < 0)
  3220. goto out_put;
  3221. ret = register_ftrace_function_probe(glob, tr, ops, data);
  3222. /*
  3223. * The above returns on success the # of functions enabled,
  3224. * but if it didn't find any functions it returns zero.
  3225. * Consider no functions a failure too.
  3226. */
  3227. if (!ret) {
  3228. ret = -ENOENT;
  3229. goto out_disable;
  3230. } else if (ret < 0)
  3231. goto out_disable;
  3232. /* Just return zero, not the number of enabled functions */
  3233. ret = 0;
  3234. out:
  3235. mutex_unlock(&event_mutex);
  3236. return ret;
  3237. out_disable:
  3238. __ftrace_event_enable_disable(file, 0, 1);
  3239. out_put:
  3240. trace_event_put_ref(file->event_call);
  3241. out_free:
  3242. kfree(data);
  3243. goto out;
  3244. }
  3245. static struct ftrace_func_command event_enable_cmd = {
  3246. .name = ENABLE_EVENT_STR,
  3247. .func = event_enable_func,
  3248. };
  3249. static struct ftrace_func_command event_disable_cmd = {
  3250. .name = DISABLE_EVENT_STR,
  3251. .func = event_enable_func,
  3252. };
  3253. static __init int register_event_cmds(void)
  3254. {
  3255. int ret;
  3256. ret = register_ftrace_command(&event_enable_cmd);
  3257. if (WARN_ON(ret < 0))
  3258. return ret;
  3259. ret = register_ftrace_command(&event_disable_cmd);
  3260. if (WARN_ON(ret < 0))
  3261. unregister_ftrace_command(&event_enable_cmd);
  3262. return ret;
  3263. }
  3264. #else
  3265. static inline int register_event_cmds(void) { return 0; }
  3266. #endif /* CONFIG_DYNAMIC_FTRACE */
  3267. /*
  3268. * The top level array and trace arrays created by boot-time tracing
  3269. * have already had its trace_event_file descriptors created in order
  3270. * to allow for early events to be recorded.
  3271. * This function is called after the tracefs has been initialized,
  3272. * and we now have to create the files associated to the events.
  3273. */
  3274. static void __trace_early_add_event_dirs(struct trace_array *tr)
  3275. {
  3276. struct trace_event_file *file;
  3277. int ret;
  3278. list_for_each_entry(file, &tr->events, list) {
  3279. ret = event_create_dir(tr->event_dir, file);
  3280. if (ret < 0)
  3281. pr_warn("Could not create directory for event %s\n",
  3282. trace_event_name(file->event_call));
  3283. }
  3284. }
  3285. /*
  3286. * For early boot up, the top trace array and the trace arrays created
  3287. * by boot-time tracing require to have a list of events that can be
  3288. * enabled. This must be done before the filesystem is set up in order
  3289. * to allow events to be traced early.
  3290. */
  3291. void __trace_early_add_events(struct trace_array *tr)
  3292. {
  3293. struct trace_event_call *call;
  3294. int ret;
  3295. list_for_each_entry(call, &ftrace_events, list) {
  3296. /* Early boot up should not have any modules loaded */
  3297. if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
  3298. WARN_ON_ONCE(call->module))
  3299. continue;
  3300. ret = __trace_early_add_new_event(call, tr);
  3301. if (ret < 0)
  3302. pr_warn("Could not create early event %s\n",
  3303. trace_event_name(call));
  3304. }
  3305. }
  3306. /* Remove the event directory structure for a trace directory. */
  3307. static void
  3308. __trace_remove_event_dirs(struct trace_array *tr)
  3309. {
  3310. struct trace_event_file *file, *next;
  3311. list_for_each_entry_safe(file, next, &tr->events, list)
  3312. remove_event_file_dir(file);
  3313. }
  3314. static void __add_event_to_tracers(struct trace_event_call *call)
  3315. {
  3316. struct trace_array *tr;
  3317. list_for_each_entry(tr, &ftrace_trace_arrays, list)
  3318. __trace_add_new_event(call, tr);
  3319. }
  3320. extern struct trace_event_call *__start_ftrace_events[];
  3321. extern struct trace_event_call *__stop_ftrace_events[];
  3322. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  3323. static __init int setup_trace_event(char *str)
  3324. {
  3325. strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  3326. trace_set_ring_buffer_expanded(NULL);
  3327. disable_tracing_selftest("running event tracing");
  3328. return 1;
  3329. }
  3330. __setup("trace_event=", setup_trace_event);
  3331. static int events_callback(const char *name, umode_t *mode, void **data,
  3332. const struct file_operations **fops)
  3333. {
  3334. if (strcmp(name, "enable") == 0) {
  3335. *mode = TRACE_MODE_WRITE;
  3336. *fops = &ftrace_tr_enable_fops;
  3337. return 1;
  3338. }
  3339. if (strcmp(name, "header_page") == 0) {
  3340. *mode = TRACE_MODE_READ;
  3341. *fops = &ftrace_show_header_page_fops;
  3342. } else if (strcmp(name, "header_event") == 0) {
  3343. *mode = TRACE_MODE_READ;
  3344. *fops = &ftrace_show_header_event_fops;
  3345. } else
  3346. return 0;
  3347. return 1;
  3348. }
  3349. /* Expects to have event_mutex held when called */
  3350. static int
  3351. create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
  3352. {
  3353. struct eventfs_inode *e_events;
  3354. struct dentry *entry;
  3355. int nr_entries;
  3356. static struct eventfs_entry events_entries[] = {
  3357. {
  3358. .name = "enable",
  3359. .callback = events_callback,
  3360. },
  3361. {
  3362. .name = "header_page",
  3363. .callback = events_callback,
  3364. },
  3365. {
  3366. .name = "header_event",
  3367. .callback = events_callback,
  3368. },
  3369. };
  3370. entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
  3371. tr, &ftrace_set_event_fops);
  3372. if (!entry)
  3373. return -ENOMEM;
  3374. nr_entries = ARRAY_SIZE(events_entries);
  3375. e_events = eventfs_create_events_dir("events", parent, events_entries,
  3376. nr_entries, tr);
  3377. if (IS_ERR(e_events)) {
  3378. pr_warn("Could not create tracefs 'events' directory\n");
  3379. return -ENOMEM;
  3380. }
  3381. /* There are not as crucial, just warn if they are not created */
  3382. trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
  3383. tr, &ftrace_set_event_pid_fops);
  3384. trace_create_file("set_event_notrace_pid",
  3385. TRACE_MODE_WRITE, parent, tr,
  3386. &ftrace_set_event_notrace_pid_fops);
  3387. tr->event_dir = e_events;
  3388. return 0;
  3389. }
  3390. /**
  3391. * event_trace_add_tracer - add a instance of a trace_array to events
  3392. * @parent: The parent dentry to place the files/directories for events in
  3393. * @tr: The trace array associated with these events
  3394. *
  3395. * When a new instance is created, it needs to set up its events
  3396. * directory, as well as other files associated with events. It also
  3397. * creates the event hierarchy in the @parent/events directory.
  3398. *
  3399. * Returns 0 on success.
  3400. *
  3401. * Must be called with event_mutex held.
  3402. */
  3403. int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
  3404. {
  3405. int ret;
  3406. lockdep_assert_held(&event_mutex);
  3407. ret = create_event_toplevel_files(parent, tr);
  3408. if (ret)
  3409. goto out;
  3410. down_write(&trace_event_sem);
  3411. /* If tr already has the event list, it is initialized in early boot. */
  3412. if (unlikely(!list_empty(&tr->events)))
  3413. __trace_early_add_event_dirs(tr);
  3414. else
  3415. __trace_add_event_dirs(tr);
  3416. up_write(&trace_event_sem);
  3417. out:
  3418. return ret;
  3419. }
  3420. /*
  3421. * The top trace array already had its file descriptors created.
  3422. * Now the files themselves need to be created.
  3423. */
  3424. static __init int
  3425. early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
  3426. {
  3427. int ret;
  3428. mutex_lock(&event_mutex);
  3429. ret = create_event_toplevel_files(parent, tr);
  3430. if (ret)
  3431. goto out_unlock;
  3432. down_write(&trace_event_sem);
  3433. __trace_early_add_event_dirs(tr);
  3434. up_write(&trace_event_sem);
  3435. out_unlock:
  3436. mutex_unlock(&event_mutex);
  3437. return ret;
  3438. }
  3439. /* Must be called with event_mutex held */
  3440. int event_trace_del_tracer(struct trace_array *tr)
  3441. {
  3442. lockdep_assert_held(&event_mutex);
  3443. /* Disable any event triggers and associated soft-disabled events */
  3444. clear_event_triggers(tr);
  3445. /* Clear the pid list */
  3446. __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
  3447. /* Disable any running events */
  3448. __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
  3449. /* Make sure no more events are being executed */
  3450. tracepoint_synchronize_unregister();
  3451. down_write(&trace_event_sem);
  3452. __trace_remove_event_dirs(tr);
  3453. eventfs_remove_events_dir(tr->event_dir);
  3454. up_write(&trace_event_sem);
  3455. tr->event_dir = NULL;
  3456. return 0;
  3457. }
  3458. static __init int event_trace_memsetup(void)
  3459. {
  3460. field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
  3461. file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
  3462. return 0;
  3463. }
  3464. __init void
  3465. early_enable_events(struct trace_array *tr, char *buf, bool disable_first)
  3466. {
  3467. char *token;
  3468. int ret;
  3469. while (true) {
  3470. token = strsep(&buf, ",");
  3471. if (!token)
  3472. break;
  3473. if (*token) {
  3474. /* Restarting syscalls requires that we stop them first */
  3475. if (disable_first)
  3476. ftrace_set_clr_event(tr, token, 0);
  3477. ret = ftrace_set_clr_event(tr, token, 1);
  3478. if (ret)
  3479. pr_warn("Failed to enable trace event: %s\n", token);
  3480. }
  3481. /* Put back the comma to allow this to be called again */
  3482. if (buf)
  3483. *(buf - 1) = ',';
  3484. }
  3485. }
  3486. static __init int event_trace_enable(void)
  3487. {
  3488. struct trace_array *tr = top_trace_array();
  3489. struct trace_event_call **iter, *call;
  3490. int ret;
  3491. if (!tr)
  3492. return -ENODEV;
  3493. for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
  3494. call = *iter;
  3495. ret = event_init(call);
  3496. if (!ret)
  3497. list_add(&call->list, &ftrace_events);
  3498. }
  3499. register_trigger_cmds();
  3500. /*
  3501. * We need the top trace array to have a working set of trace
  3502. * points at early init, before the debug files and directories
  3503. * are created. Create the file entries now, and attach them
  3504. * to the actual file dentries later.
  3505. */
  3506. __trace_early_add_events(tr);
  3507. early_enable_events(tr, bootup_event_buf, false);
  3508. trace_printk_start_comm();
  3509. register_event_cmds();
  3510. return 0;
  3511. }
  3512. /*
  3513. * event_trace_enable() is called from trace_event_init() first to
  3514. * initialize events and perhaps start any events that are on the
  3515. * command line. Unfortunately, there are some events that will not
  3516. * start this early, like the system call tracepoints that need
  3517. * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
  3518. * event_trace_enable() is called before pid 1 starts, and this flag
  3519. * is never set, making the syscall tracepoint never get reached, but
  3520. * the event is enabled regardless (and not doing anything).
  3521. */
  3522. static __init int event_trace_enable_again(void)
  3523. {
  3524. struct trace_array *tr;
  3525. tr = top_trace_array();
  3526. if (!tr)
  3527. return -ENODEV;
  3528. early_enable_events(tr, bootup_event_buf, true);
  3529. return 0;
  3530. }
  3531. early_initcall(event_trace_enable_again);
  3532. /* Init fields which doesn't related to the tracefs */
  3533. static __init int event_trace_init_fields(void)
  3534. {
  3535. if (trace_define_generic_fields())
  3536. pr_warn("tracing: Failed to allocated generic fields");
  3537. if (trace_define_common_fields())
  3538. pr_warn("tracing: Failed to allocate common fields");
  3539. return 0;
  3540. }
  3541. __init int event_trace_init(void)
  3542. {
  3543. struct trace_array *tr;
  3544. int ret;
  3545. tr = top_trace_array();
  3546. if (!tr)
  3547. return -ENODEV;
  3548. trace_create_file("available_events", TRACE_MODE_READ,
  3549. NULL, tr, &ftrace_avail_fops);
  3550. ret = early_event_add_tracer(NULL, tr);
  3551. if (ret)
  3552. return ret;
  3553. #ifdef CONFIG_MODULES
  3554. ret = register_module_notifier(&trace_module_nb);
  3555. if (ret)
  3556. pr_warn("Failed to register trace events module notifier\n");
  3557. #endif
  3558. eventdir_initialized = true;
  3559. return 0;
  3560. }
  3561. void __init trace_event_init(void)
  3562. {
  3563. event_trace_memsetup();
  3564. init_ftrace_syscalls();
  3565. event_trace_enable();
  3566. event_trace_init_fields();
  3567. }
  3568. #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
  3569. static DEFINE_SPINLOCK(test_spinlock);
  3570. static DEFINE_SPINLOCK(test_spinlock_irq);
  3571. static DEFINE_MUTEX(test_mutex);
  3572. static __init void test_work(struct work_struct *dummy)
  3573. {
  3574. spin_lock(&test_spinlock);
  3575. spin_lock_irq(&test_spinlock_irq);
  3576. udelay(1);
  3577. spin_unlock_irq(&test_spinlock_irq);
  3578. spin_unlock(&test_spinlock);
  3579. mutex_lock(&test_mutex);
  3580. msleep(1);
  3581. mutex_unlock(&test_mutex);
  3582. }
  3583. static __init int event_test_thread(void *unused)
  3584. {
  3585. void *test_malloc;
  3586. test_malloc = kmalloc(1234, GFP_KERNEL);
  3587. if (!test_malloc)
  3588. pr_info("failed to kmalloc\n");
  3589. schedule_on_each_cpu(test_work);
  3590. kfree(test_malloc);
  3591. set_current_state(TASK_INTERRUPTIBLE);
  3592. while (!kthread_should_stop()) {
  3593. schedule();
  3594. set_current_state(TASK_INTERRUPTIBLE);
  3595. }
  3596. __set_current_state(TASK_RUNNING);
  3597. return 0;
  3598. }
  3599. /*
  3600. * Do various things that may trigger events.
  3601. */
  3602. static __init void event_test_stuff(void)
  3603. {
  3604. struct task_struct *test_thread;
  3605. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  3606. msleep(1);
  3607. kthread_stop(test_thread);
  3608. }
  3609. /*
  3610. * For every trace event defined, we will test each trace point separately,
  3611. * and then by groups, and finally all trace points.
  3612. */
  3613. static __init void event_trace_self_tests(void)
  3614. {
  3615. struct trace_subsystem_dir *dir;
  3616. struct trace_event_file *file;
  3617. struct trace_event_call *call;
  3618. struct event_subsystem *system;
  3619. struct trace_array *tr;
  3620. int ret;
  3621. tr = top_trace_array();
  3622. if (!tr)
  3623. return;
  3624. pr_info("Running tests on trace events:\n");
  3625. list_for_each_entry(file, &tr->events, list) {
  3626. call = file->event_call;
  3627. /* Only test those that have a probe */
  3628. if (!call->class || !call->class->probe)
  3629. continue;
  3630. /*
  3631. * Testing syscall events here is pretty useless, but
  3632. * we still do it if configured. But this is time consuming.
  3633. * What we really need is a user thread to perform the
  3634. * syscalls as we test.
  3635. */
  3636. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  3637. if (call->class->system &&
  3638. strcmp(call->class->system, "syscalls") == 0)
  3639. continue;
  3640. #endif
  3641. pr_info("Testing event %s: ", trace_event_name(call));
  3642. /*
  3643. * If an event is already enabled, someone is using
  3644. * it and the self test should not be on.
  3645. */
  3646. if (file->flags & EVENT_FILE_FL_ENABLED) {
  3647. pr_warn("Enabled event during self test!\n");
  3648. WARN_ON_ONCE(1);
  3649. continue;
  3650. }
  3651. ftrace_event_enable_disable(file, 1);
  3652. event_test_stuff();
  3653. ftrace_event_enable_disable(file, 0);
  3654. pr_cont("OK\n");
  3655. }
  3656. /* Now test at the sub system level */
  3657. pr_info("Running tests on trace event systems:\n");
  3658. list_for_each_entry(dir, &tr->systems, list) {
  3659. system = dir->subsystem;
  3660. /* the ftrace system is special, skip it */
  3661. if (strcmp(system->name, "ftrace") == 0)
  3662. continue;
  3663. pr_info("Testing event system %s: ", system->name);
  3664. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
  3665. if (WARN_ON_ONCE(ret)) {
  3666. pr_warn("error enabling system %s\n",
  3667. system->name);
  3668. continue;
  3669. }
  3670. event_test_stuff();
  3671. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
  3672. if (WARN_ON_ONCE(ret)) {
  3673. pr_warn("error disabling system %s\n",
  3674. system->name);
  3675. continue;
  3676. }
  3677. pr_cont("OK\n");
  3678. }
  3679. /* Test with all events enabled */
  3680. pr_info("Running tests on all trace events:\n");
  3681. pr_info("Testing all events: ");
  3682. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
  3683. if (WARN_ON_ONCE(ret)) {
  3684. pr_warn("error enabling all events\n");
  3685. return;
  3686. }
  3687. event_test_stuff();
  3688. /* reset sysname */
  3689. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
  3690. if (WARN_ON_ONCE(ret)) {
  3691. pr_warn("error disabling all events\n");
  3692. return;
  3693. }
  3694. pr_cont("OK\n");
  3695. }
  3696. #ifdef CONFIG_FUNCTION_TRACER
  3697. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  3698. static struct trace_event_file event_trace_file __initdata;
  3699. static void __init
  3700. function_test_events_call(unsigned long ip, unsigned long parent_ip,
  3701. struct ftrace_ops *op, struct ftrace_regs *regs)
  3702. {
  3703. struct trace_buffer *buffer;
  3704. struct ring_buffer_event *event;
  3705. struct ftrace_entry *entry;
  3706. unsigned int trace_ctx;
  3707. long disabled;
  3708. int cpu;
  3709. trace_ctx = tracing_gen_ctx();
  3710. preempt_disable_notrace();
  3711. cpu = raw_smp_processor_id();
  3712. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  3713. if (disabled != 1)
  3714. goto out;
  3715. event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
  3716. TRACE_FN, sizeof(*entry),
  3717. trace_ctx);
  3718. if (!event)
  3719. goto out;
  3720. entry = ring_buffer_event_data(event);
  3721. entry->ip = ip;
  3722. entry->parent_ip = parent_ip;
  3723. event_trigger_unlock_commit(&event_trace_file, buffer, event,
  3724. entry, trace_ctx);
  3725. out:
  3726. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  3727. preempt_enable_notrace();
  3728. }
  3729. static struct ftrace_ops trace_ops __initdata =
  3730. {
  3731. .func = function_test_events_call,
  3732. };
  3733. static __init void event_trace_self_test_with_function(void)
  3734. {
  3735. int ret;
  3736. event_trace_file.tr = top_trace_array();
  3737. if (WARN_ON(!event_trace_file.tr))
  3738. return;
  3739. ret = register_ftrace_function(&trace_ops);
  3740. if (WARN_ON(ret < 0)) {
  3741. pr_info("Failed to enable function tracer for event tests\n");
  3742. return;
  3743. }
  3744. pr_info("Running tests again, along with the function tracer\n");
  3745. event_trace_self_tests();
  3746. unregister_ftrace_function(&trace_ops);
  3747. }
  3748. #else
  3749. static __init void event_trace_self_test_with_function(void)
  3750. {
  3751. }
  3752. #endif
  3753. static __init int event_trace_self_tests_init(void)
  3754. {
  3755. if (!tracing_selftest_disabled) {
  3756. event_trace_self_tests();
  3757. event_trace_self_test_with_function();
  3758. }
  3759. return 0;
  3760. }
  3761. late_initcall(event_trace_self_tests_init);
  3762. #endif