trace_events.c 62 KB

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