trace_events.c 65 KB

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