trace_events.c 58 KB

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