trace_events.c 60 KB

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