trace_events.c 95 KB

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