trace_events.c 77 KB

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