trace_events.c 76 KB

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