trace_events.c 59 KB

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