trace_events.c 78 KB

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