trace_events.c 60 KB

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