trace_events.c 61 KB

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