trace_events.c 105 KB

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