trace_events.c 60 KB

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