trace_events.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425
  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. #define pr_fmt(fmt) fmt
  11. #include <linux/workqueue.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/kthread.h>
  14. #include <linux/tracefs.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/bsearch.h>
  17. #include <linux/module.h>
  18. #include <linux/ctype.h>
  19. #include <linux/sort.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <trace/events/sched.h>
  23. #include <asm/setup.h>
  24. #include "trace_output.h"
  25. #undef TRACE_SYSTEM
  26. #define TRACE_SYSTEM "TRACE_SYSTEM"
  27. DEFINE_MUTEX(event_mutex);
  28. LIST_HEAD(ftrace_events);
  29. static LIST_HEAD(ftrace_generic_fields);
  30. static LIST_HEAD(ftrace_common_fields);
  31. #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
  32. static struct kmem_cache *field_cachep;
  33. static struct kmem_cache *file_cachep;
  34. static inline int system_refcount(struct event_subsystem *system)
  35. {
  36. return system->ref_count;
  37. }
  38. static int system_refcount_inc(struct event_subsystem *system)
  39. {
  40. return system->ref_count++;
  41. }
  42. static int system_refcount_dec(struct event_subsystem *system)
  43. {
  44. return --system->ref_count;
  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 trace_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 trace_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 trace_event_call *call, char *name)
  75. {
  76. struct ftrace_event_field *field;
  77. struct list_head *head;
  78. field = __find_event_field(&ftrace_generic_fields, name);
  79. if (field)
  80. return field;
  81. field = __find_event_field(&ftrace_common_fields, name);
  82. if (field)
  83. return field;
  84. head = trace_get_fields(call);
  85. return __find_event_field(head, name);
  86. }
  87. static int __trace_define_field(struct list_head *head, const char *type,
  88. const char *name, int offset, int size,
  89. int is_signed, int filter_type)
  90. {
  91. struct ftrace_event_field *field;
  92. field = kmem_cache_alloc(field_cachep, GFP_TRACE);
  93. if (!field)
  94. return -ENOMEM;
  95. field->name = name;
  96. field->type = type;
  97. if (filter_type == FILTER_OTHER)
  98. field->filter_type = filter_assign_type(type);
  99. else
  100. field->filter_type = filter_type;
  101. field->offset = offset;
  102. field->size = size;
  103. field->is_signed = is_signed;
  104. list_add(&field->link, head);
  105. return 0;
  106. }
  107. int trace_define_field(struct trace_event_call *call, const char *type,
  108. const char *name, int offset, int size, int is_signed,
  109. int filter_type)
  110. {
  111. struct list_head *head;
  112. if (WARN_ON(!call->class))
  113. return 0;
  114. head = trace_get_fields(call);
  115. return __trace_define_field(head, type, name, offset, size,
  116. is_signed, filter_type);
  117. }
  118. EXPORT_SYMBOL_GPL(trace_define_field);
  119. #define __generic_field(type, item, filter_type) \
  120. ret = __trace_define_field(&ftrace_generic_fields, #type, \
  121. #item, 0, 0, is_signed_type(type), \
  122. filter_type); \
  123. if (ret) \
  124. return ret;
  125. #define __common_field(type, item) \
  126. ret = __trace_define_field(&ftrace_common_fields, #type, \
  127. "common_" #item, \
  128. offsetof(typeof(ent), item), \
  129. sizeof(ent.item), \
  130. is_signed_type(type), FILTER_OTHER); \
  131. if (ret) \
  132. return ret;
  133. static int trace_define_generic_fields(void)
  134. {
  135. int ret;
  136. __generic_field(int, cpu, FILTER_OTHER);
  137. __generic_field(char *, comm, FILTER_PTR_STRING);
  138. return ret;
  139. }
  140. static int trace_define_common_fields(void)
  141. {
  142. int ret;
  143. struct trace_entry ent;
  144. __common_field(unsigned short, type);
  145. __common_field(unsigned char, flags);
  146. __common_field(unsigned char, preempt_count);
  147. __common_field(int, pid);
  148. return ret;
  149. }
  150. static void trace_destroy_fields(struct trace_event_call *call)
  151. {
  152. struct ftrace_event_field *field, *next;
  153. struct list_head *head;
  154. head = trace_get_fields(call);
  155. list_for_each_entry_safe(field, next, head, link) {
  156. list_del(&field->link);
  157. kmem_cache_free(field_cachep, field);
  158. }
  159. }
  160. int trace_event_raw_init(struct trace_event_call *call)
  161. {
  162. int id;
  163. id = register_trace_event(&call->event);
  164. if (!id)
  165. return -ENODEV;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(trace_event_raw_init);
  169. bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
  170. {
  171. struct trace_array *tr = trace_file->tr;
  172. struct trace_array_cpu *data;
  173. struct trace_pid_list *pid_list;
  174. pid_list = rcu_dereference_sched(tr->filtered_pids);
  175. if (!pid_list)
  176. return false;
  177. data = this_cpu_ptr(tr->trace_buffer.data);
  178. return data->ignore_pid;
  179. }
  180. EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
  181. void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  182. struct trace_event_file *trace_file,
  183. unsigned long len)
  184. {
  185. struct trace_event_call *event_call = trace_file->event_call;
  186. if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
  187. trace_event_ignore_this_pid(trace_file))
  188. return NULL;
  189. local_save_flags(fbuffer->flags);
  190. fbuffer->pc = preempt_count();
  191. fbuffer->trace_file = trace_file;
  192. fbuffer->event =
  193. trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
  194. event_call->event.type, len,
  195. fbuffer->flags, fbuffer->pc);
  196. if (!fbuffer->event)
  197. return NULL;
  198. fbuffer->entry = ring_buffer_event_data(fbuffer->event);
  199. return fbuffer->entry;
  200. }
  201. EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
  202. static DEFINE_SPINLOCK(tracepoint_iter_lock);
  203. static void output_printk(struct trace_event_buffer *fbuffer)
  204. {
  205. struct trace_event_call *event_call;
  206. struct trace_event *event;
  207. unsigned long flags;
  208. struct trace_iterator *iter = tracepoint_print_iter;
  209. if (!iter)
  210. return;
  211. event_call = fbuffer->trace_file->event_call;
  212. if (!event_call || !event_call->event.funcs ||
  213. !event_call->event.funcs->trace)
  214. return;
  215. event = &fbuffer->trace_file->event_call->event;
  216. spin_lock_irqsave(&tracepoint_iter_lock, flags);
  217. trace_seq_init(&iter->seq);
  218. iter->ent = fbuffer->entry;
  219. event_call->event.funcs->trace(iter, 0, event);
  220. trace_seq_putc(&iter->seq, 0);
  221. printk("%s", iter->seq.buffer);
  222. spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
  223. }
  224. void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
  225. {
  226. if (tracepoint_printk)
  227. output_printk(fbuffer);
  228. event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
  229. fbuffer->event, fbuffer->entry,
  230. fbuffer->flags, fbuffer->pc);
  231. }
  232. EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
  233. int trace_event_reg(struct trace_event_call *call,
  234. enum trace_reg type, void *data)
  235. {
  236. struct trace_event_file *file = data;
  237. WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
  238. switch (type) {
  239. case TRACE_REG_REGISTER:
  240. return tracepoint_probe_register(call->tp,
  241. call->class->probe,
  242. file);
  243. case TRACE_REG_UNREGISTER:
  244. tracepoint_probe_unregister(call->tp,
  245. call->class->probe,
  246. file);
  247. return 0;
  248. #ifdef CONFIG_PERF_EVENTS
  249. case TRACE_REG_PERF_REGISTER:
  250. return tracepoint_probe_register(call->tp,
  251. call->class->perf_probe,
  252. call);
  253. case TRACE_REG_PERF_UNREGISTER:
  254. tracepoint_probe_unregister(call->tp,
  255. call->class->perf_probe,
  256. call);
  257. return 0;
  258. case TRACE_REG_PERF_OPEN:
  259. case TRACE_REG_PERF_CLOSE:
  260. case TRACE_REG_PERF_ADD:
  261. case TRACE_REG_PERF_DEL:
  262. return 0;
  263. #endif
  264. }
  265. return 0;
  266. }
  267. EXPORT_SYMBOL_GPL(trace_event_reg);
  268. void trace_event_enable_cmd_record(bool enable)
  269. {
  270. struct trace_event_file *file;
  271. struct trace_array *tr;
  272. mutex_lock(&event_mutex);
  273. do_for_each_event_file(tr, file) {
  274. if (!(file->flags & EVENT_FILE_FL_ENABLED))
  275. continue;
  276. if (enable) {
  277. tracing_start_cmdline_record();
  278. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  279. } else {
  280. tracing_stop_cmdline_record();
  281. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  282. }
  283. } while_for_each_event_file();
  284. mutex_unlock(&event_mutex);
  285. }
  286. static int __ftrace_event_enable_disable(struct trace_event_file *file,
  287. int enable, int soft_disable)
  288. {
  289. struct trace_event_call *call = file->event_call;
  290. struct trace_array *tr = file->tr;
  291. int ret = 0;
  292. int disable;
  293. switch (enable) {
  294. case 0:
  295. /*
  296. * When soft_disable is set and enable is cleared, the sm_ref
  297. * reference counter is decremented. If it reaches 0, we want
  298. * to clear the SOFT_DISABLED flag but leave the event in the
  299. * state that it was. That is, if the event was enabled and
  300. * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
  301. * is set we do not want the event to be enabled before we
  302. * clear the bit.
  303. *
  304. * When soft_disable is not set but the SOFT_MODE flag is,
  305. * we do nothing. Do not disable the tracepoint, otherwise
  306. * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
  307. */
  308. if (soft_disable) {
  309. if (atomic_dec_return(&file->sm_ref) > 0)
  310. break;
  311. disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
  312. clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
  313. } else
  314. disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
  315. if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
  316. clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  317. if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
  318. tracing_stop_cmdline_record();
  319. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  320. }
  321. call->class->reg(call, TRACE_REG_UNREGISTER, file);
  322. }
  323. /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
  324. if (file->flags & EVENT_FILE_FL_SOFT_MODE)
  325. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  326. else
  327. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  328. break;
  329. case 1:
  330. /*
  331. * When soft_disable is set and enable is set, we want to
  332. * register the tracepoint for the event, but leave the event
  333. * as is. That means, if the event was already enabled, we do
  334. * nothing (but set SOFT_MODE). If the event is disabled, we
  335. * set SOFT_DISABLED before enabling the event tracepoint, so
  336. * it still seems to be disabled.
  337. */
  338. if (!soft_disable)
  339. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  340. else {
  341. if (atomic_inc_return(&file->sm_ref) > 1)
  342. break;
  343. set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
  344. }
  345. if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
  346. /* Keep the event disabled, when going to SOFT_MODE. */
  347. if (soft_disable)
  348. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  349. if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
  350. tracing_start_cmdline_record();
  351. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  352. }
  353. ret = call->class->reg(call, TRACE_REG_REGISTER, file);
  354. if (ret) {
  355. tracing_stop_cmdline_record();
  356. pr_info("event trace: Could not enable event "
  357. "%s\n", trace_event_name(call));
  358. break;
  359. }
  360. set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  361. /* WAS_ENABLED gets set but never cleared. */
  362. call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
  363. }
  364. break;
  365. }
  366. return ret;
  367. }
  368. int trace_event_enable_disable(struct trace_event_file *file,
  369. int enable, int soft_disable)
  370. {
  371. return __ftrace_event_enable_disable(file, enable, soft_disable);
  372. }
  373. static int ftrace_event_enable_disable(struct trace_event_file *file,
  374. int enable)
  375. {
  376. return __ftrace_event_enable_disable(file, enable, 0);
  377. }
  378. static void ftrace_clear_events(struct trace_array *tr)
  379. {
  380. struct trace_event_file *file;
  381. mutex_lock(&event_mutex);
  382. list_for_each_entry(file, &tr->events, list) {
  383. ftrace_event_enable_disable(file, 0);
  384. }
  385. mutex_unlock(&event_mutex);
  386. }
  387. static int cmp_pid(const void *key, const void *elt)
  388. {
  389. const pid_t *search_pid = key;
  390. const pid_t *pid = elt;
  391. if (*search_pid == *pid)
  392. return 0;
  393. if (*search_pid < *pid)
  394. return -1;
  395. return 1;
  396. }
  397. static bool
  398. check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task)
  399. {
  400. pid_t search_pid;
  401. pid_t *pid;
  402. /*
  403. * Return false, because if filtered_pids does not exist,
  404. * all pids are good to trace.
  405. */
  406. if (!filtered_pids)
  407. return false;
  408. search_pid = task->pid;
  409. pid = bsearch(&search_pid, filtered_pids->pids,
  410. filtered_pids->nr_pids, sizeof(pid_t),
  411. cmp_pid);
  412. if (!pid)
  413. return true;
  414. return false;
  415. }
  416. static void
  417. event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
  418. struct task_struct *prev, struct task_struct *next)
  419. {
  420. struct trace_array *tr = data;
  421. struct trace_pid_list *pid_list;
  422. pid_list = rcu_dereference_sched(tr->filtered_pids);
  423. this_cpu_write(tr->trace_buffer.data->ignore_pid,
  424. check_ignore_pid(pid_list, prev) &&
  425. check_ignore_pid(pid_list, next));
  426. }
  427. static void
  428. event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
  429. struct task_struct *prev, struct task_struct *next)
  430. {
  431. struct trace_array *tr = data;
  432. struct trace_pid_list *pid_list;
  433. pid_list = rcu_dereference_sched(tr->filtered_pids);
  434. this_cpu_write(tr->trace_buffer.data->ignore_pid,
  435. check_ignore_pid(pid_list, next));
  436. }
  437. static void
  438. event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
  439. {
  440. struct trace_array *tr = data;
  441. struct trace_pid_list *pid_list;
  442. /* Nothing to do if we are already tracing */
  443. if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
  444. return;
  445. pid_list = rcu_dereference_sched(tr->filtered_pids);
  446. this_cpu_write(tr->trace_buffer.data->ignore_pid,
  447. check_ignore_pid(pid_list, task));
  448. }
  449. static void
  450. event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
  451. {
  452. struct trace_array *tr = data;
  453. struct trace_pid_list *pid_list;
  454. /* Nothing to do if we are not tracing */
  455. if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
  456. return;
  457. pid_list = rcu_dereference_sched(tr->filtered_pids);
  458. /* Set tracing if current is enabled */
  459. this_cpu_write(tr->trace_buffer.data->ignore_pid,
  460. check_ignore_pid(pid_list, current));
  461. }
  462. static void __ftrace_clear_event_pids(struct trace_array *tr)
  463. {
  464. struct trace_pid_list *pid_list;
  465. struct trace_event_file *file;
  466. int cpu;
  467. pid_list = rcu_dereference_protected(tr->filtered_pids,
  468. lockdep_is_held(&event_mutex));
  469. if (!pid_list)
  470. return;
  471. unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
  472. unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
  473. unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
  474. unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
  475. unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
  476. unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
  477. unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
  478. unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
  479. list_for_each_entry(file, &tr->events, list) {
  480. clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
  481. }
  482. for_each_possible_cpu(cpu)
  483. per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
  484. rcu_assign_pointer(tr->filtered_pids, NULL);
  485. /* Wait till all users are no longer using pid filtering */
  486. synchronize_sched();
  487. free_pages((unsigned long)pid_list->pids, pid_list->order);
  488. kfree(pid_list);
  489. }
  490. static void ftrace_clear_event_pids(struct trace_array *tr)
  491. {
  492. mutex_lock(&event_mutex);
  493. __ftrace_clear_event_pids(tr);
  494. mutex_unlock(&event_mutex);
  495. }
  496. static void __put_system(struct event_subsystem *system)
  497. {
  498. struct event_filter *filter = system->filter;
  499. WARN_ON_ONCE(system_refcount(system) == 0);
  500. if (system_refcount_dec(system))
  501. return;
  502. list_del(&system->list);
  503. if (filter) {
  504. kfree(filter->filter_string);
  505. kfree(filter);
  506. }
  507. kfree_const(system->name);
  508. kfree(system);
  509. }
  510. static void __get_system(struct event_subsystem *system)
  511. {
  512. WARN_ON_ONCE(system_refcount(system) == 0);
  513. system_refcount_inc(system);
  514. }
  515. static void __get_system_dir(struct trace_subsystem_dir *dir)
  516. {
  517. WARN_ON_ONCE(dir->ref_count == 0);
  518. dir->ref_count++;
  519. __get_system(dir->subsystem);
  520. }
  521. static void __put_system_dir(struct trace_subsystem_dir *dir)
  522. {
  523. WARN_ON_ONCE(dir->ref_count == 0);
  524. /* If the subsystem is about to be freed, the dir must be too */
  525. WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
  526. __put_system(dir->subsystem);
  527. if (!--dir->ref_count)
  528. kfree(dir);
  529. }
  530. static void put_system(struct trace_subsystem_dir *dir)
  531. {
  532. mutex_lock(&event_mutex);
  533. __put_system_dir(dir);
  534. mutex_unlock(&event_mutex);
  535. }
  536. static void remove_subsystem(struct trace_subsystem_dir *dir)
  537. {
  538. if (!dir)
  539. return;
  540. if (!--dir->nr_events) {
  541. tracefs_remove_recursive(dir->entry);
  542. list_del(&dir->list);
  543. __put_system_dir(dir);
  544. }
  545. }
  546. static void remove_event_file_dir(struct trace_event_file *file)
  547. {
  548. struct dentry *dir = file->dir;
  549. struct dentry *child;
  550. if (dir) {
  551. spin_lock(&dir->d_lock); /* probably unneeded */
  552. list_for_each_entry(child, &dir->d_subdirs, d_child) {
  553. if (d_really_is_positive(child)) /* probably unneeded */
  554. d_inode(child)->i_private = NULL;
  555. }
  556. spin_unlock(&dir->d_lock);
  557. tracefs_remove_recursive(dir);
  558. }
  559. list_del(&file->list);
  560. remove_subsystem(file->system);
  561. free_event_filter(file->filter);
  562. kmem_cache_free(file_cachep, file);
  563. }
  564. /*
  565. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  566. */
  567. static int
  568. __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
  569. const char *sub, const char *event, int set)
  570. {
  571. struct trace_event_file *file;
  572. struct trace_event_call *call;
  573. const char *name;
  574. int ret = -EINVAL;
  575. list_for_each_entry(file, &tr->events, list) {
  576. call = file->event_call;
  577. name = trace_event_name(call);
  578. if (!name || !call->class || !call->class->reg)
  579. continue;
  580. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  581. continue;
  582. if (match &&
  583. strcmp(match, name) != 0 &&
  584. strcmp(match, call->class->system) != 0)
  585. continue;
  586. if (sub && strcmp(sub, call->class->system) != 0)
  587. continue;
  588. if (event && strcmp(event, name) != 0)
  589. continue;
  590. ftrace_event_enable_disable(file, set);
  591. ret = 0;
  592. }
  593. return ret;
  594. }
  595. static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
  596. const char *sub, const char *event, int set)
  597. {
  598. int ret;
  599. mutex_lock(&event_mutex);
  600. ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
  601. mutex_unlock(&event_mutex);
  602. return ret;
  603. }
  604. static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
  605. {
  606. char *event = NULL, *sub = NULL, *match;
  607. int ret;
  608. /*
  609. * The buf format can be <subsystem>:<event-name>
  610. * *:<event-name> means any event by that name.
  611. * :<event-name> is the same.
  612. *
  613. * <subsystem>:* means all events in that subsystem
  614. * <subsystem>: means the same.
  615. *
  616. * <name> (no ':') means all events in a subsystem with
  617. * the name <name> or any event that matches <name>
  618. */
  619. match = strsep(&buf, ":");
  620. if (buf) {
  621. sub = match;
  622. event = buf;
  623. match = NULL;
  624. if (!strlen(sub) || strcmp(sub, "*") == 0)
  625. sub = NULL;
  626. if (!strlen(event) || strcmp(event, "*") == 0)
  627. event = NULL;
  628. }
  629. ret = __ftrace_set_clr_event(tr, match, sub, event, set);
  630. /* Put back the colon to allow this to be called again */
  631. if (buf)
  632. *(buf - 1) = ':';
  633. return ret;
  634. }
  635. /**
  636. * trace_set_clr_event - enable or disable an event
  637. * @system: system name to match (NULL for any system)
  638. * @event: event name to match (NULL for all events, within system)
  639. * @set: 1 to enable, 0 to disable
  640. *
  641. * This is a way for other parts of the kernel to enable or disable
  642. * event recording.
  643. *
  644. * Returns 0 on success, -EINVAL if the parameters do not match any
  645. * registered events.
  646. */
  647. int trace_set_clr_event(const char *system, const char *event, int set)
  648. {
  649. struct trace_array *tr = top_trace_array();
  650. if (!tr)
  651. return -ENODEV;
  652. return __ftrace_set_clr_event(tr, NULL, system, event, set);
  653. }
  654. EXPORT_SYMBOL_GPL(trace_set_clr_event);
  655. /* 128 should be much more than enough */
  656. #define EVENT_BUF_SIZE 127
  657. static ssize_t
  658. ftrace_event_write(struct file *file, const char __user *ubuf,
  659. size_t cnt, loff_t *ppos)
  660. {
  661. struct trace_parser parser;
  662. struct seq_file *m = file->private_data;
  663. struct trace_array *tr = m->private;
  664. ssize_t read, ret;
  665. if (!cnt)
  666. return 0;
  667. ret = tracing_update_buffers();
  668. if (ret < 0)
  669. return ret;
  670. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  671. return -ENOMEM;
  672. read = trace_get_user(&parser, ubuf, cnt, ppos);
  673. if (read >= 0 && trace_parser_loaded((&parser))) {
  674. int set = 1;
  675. if (*parser.buffer == '!')
  676. set = 0;
  677. parser.buffer[parser.idx] = 0;
  678. ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
  679. if (ret)
  680. goto out_put;
  681. }
  682. ret = read;
  683. out_put:
  684. trace_parser_put(&parser);
  685. return ret;
  686. }
  687. static void *
  688. t_next(struct seq_file *m, void *v, loff_t *pos)
  689. {
  690. struct trace_event_file *file = v;
  691. struct trace_event_call *call;
  692. struct trace_array *tr = m->private;
  693. (*pos)++;
  694. list_for_each_entry_continue(file, &tr->events, list) {
  695. call = file->event_call;
  696. /*
  697. * The ftrace subsystem is for showing formats only.
  698. * They can not be enabled or disabled via the event files.
  699. */
  700. if (call->class && call->class->reg &&
  701. !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
  702. return file;
  703. }
  704. return NULL;
  705. }
  706. static void *t_start(struct seq_file *m, loff_t *pos)
  707. {
  708. struct trace_event_file *file;
  709. struct trace_array *tr = m->private;
  710. loff_t l;
  711. mutex_lock(&event_mutex);
  712. file = list_entry(&tr->events, struct trace_event_file, list);
  713. for (l = 0; l <= *pos; ) {
  714. file = t_next(m, file, &l);
  715. if (!file)
  716. break;
  717. }
  718. return file;
  719. }
  720. static void *
  721. s_next(struct seq_file *m, void *v, loff_t *pos)
  722. {
  723. struct trace_event_file *file = v;
  724. struct trace_array *tr = m->private;
  725. (*pos)++;
  726. list_for_each_entry_continue(file, &tr->events, list) {
  727. if (file->flags & EVENT_FILE_FL_ENABLED)
  728. return file;
  729. }
  730. return NULL;
  731. }
  732. static void *s_start(struct seq_file *m, loff_t *pos)
  733. {
  734. struct trace_event_file *file;
  735. struct trace_array *tr = m->private;
  736. loff_t l;
  737. mutex_lock(&event_mutex);
  738. file = list_entry(&tr->events, struct trace_event_file, list);
  739. for (l = 0; l <= *pos; ) {
  740. file = s_next(m, file, &l);
  741. if (!file)
  742. break;
  743. }
  744. return file;
  745. }
  746. static int t_show(struct seq_file *m, void *v)
  747. {
  748. struct trace_event_file *file = v;
  749. struct trace_event_call *call = file->event_call;
  750. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  751. seq_printf(m, "%s:", call->class->system);
  752. seq_printf(m, "%s\n", trace_event_name(call));
  753. return 0;
  754. }
  755. static void t_stop(struct seq_file *m, void *p)
  756. {
  757. mutex_unlock(&event_mutex);
  758. }
  759. static void *p_start(struct seq_file *m, loff_t *pos)
  760. __acquires(RCU)
  761. {
  762. struct trace_pid_list *pid_list;
  763. struct trace_array *tr = m->private;
  764. /*
  765. * Grab the mutex, to keep calls to p_next() having the same
  766. * tr->filtered_pids as p_start() has.
  767. * If we just passed the tr->filtered_pids around, then RCU would
  768. * have been enough, but doing that makes things more complex.
  769. */
  770. mutex_lock(&event_mutex);
  771. rcu_read_lock_sched();
  772. pid_list = rcu_dereference_sched(tr->filtered_pids);
  773. if (!pid_list || *pos >= pid_list->nr_pids)
  774. return NULL;
  775. return (void *)&pid_list->pids[*pos];
  776. }
  777. static void p_stop(struct seq_file *m, void *p)
  778. __releases(RCU)
  779. {
  780. rcu_read_unlock_sched();
  781. mutex_unlock(&event_mutex);
  782. }
  783. static void *
  784. p_next(struct seq_file *m, void *v, loff_t *pos)
  785. {
  786. struct trace_array *tr = m->private;
  787. struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
  788. (*pos)++;
  789. if (*pos >= pid_list->nr_pids)
  790. return NULL;
  791. return (void *)&pid_list->pids[*pos];
  792. }
  793. static int p_show(struct seq_file *m, void *v)
  794. {
  795. pid_t *pid = v;
  796. seq_printf(m, "%d\n", *pid);
  797. return 0;
  798. }
  799. static ssize_t
  800. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  801. loff_t *ppos)
  802. {
  803. struct trace_event_file *file;
  804. unsigned long flags;
  805. char buf[4] = "0";
  806. mutex_lock(&event_mutex);
  807. file = event_file_data(filp);
  808. if (likely(file))
  809. flags = file->flags;
  810. mutex_unlock(&event_mutex);
  811. if (!file)
  812. return -ENODEV;
  813. if (flags & EVENT_FILE_FL_ENABLED &&
  814. !(flags & EVENT_FILE_FL_SOFT_DISABLED))
  815. strcpy(buf, "1");
  816. if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
  817. flags & EVENT_FILE_FL_SOFT_MODE)
  818. strcat(buf, "*");
  819. strcat(buf, "\n");
  820. return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
  821. }
  822. static ssize_t
  823. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  824. loff_t *ppos)
  825. {
  826. struct trace_event_file *file;
  827. unsigned long val;
  828. int ret;
  829. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  830. if (ret)
  831. return ret;
  832. ret = tracing_update_buffers();
  833. if (ret < 0)
  834. return ret;
  835. switch (val) {
  836. case 0:
  837. case 1:
  838. ret = -ENODEV;
  839. mutex_lock(&event_mutex);
  840. file = event_file_data(filp);
  841. if (likely(file))
  842. ret = ftrace_event_enable_disable(file, val);
  843. mutex_unlock(&event_mutex);
  844. break;
  845. default:
  846. return -EINVAL;
  847. }
  848. *ppos += cnt;
  849. return ret ? ret : cnt;
  850. }
  851. static ssize_t
  852. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  853. loff_t *ppos)
  854. {
  855. const char set_to_char[4] = { '?', '0', '1', 'X' };
  856. struct trace_subsystem_dir *dir = filp->private_data;
  857. struct event_subsystem *system = dir->subsystem;
  858. struct trace_event_call *call;
  859. struct trace_event_file *file;
  860. struct trace_array *tr = dir->tr;
  861. char buf[2];
  862. int set = 0;
  863. int ret;
  864. mutex_lock(&event_mutex);
  865. list_for_each_entry(file, &tr->events, list) {
  866. call = file->event_call;
  867. if (!trace_event_name(call) || !call->class || !call->class->reg)
  868. continue;
  869. if (system && strcmp(call->class->system, system->name) != 0)
  870. continue;
  871. /*
  872. * We need to find out if all the events are set
  873. * or if all events or cleared, or if we have
  874. * a mixture.
  875. */
  876. set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
  877. /*
  878. * If we have a mixture, no need to look further.
  879. */
  880. if (set == 3)
  881. break;
  882. }
  883. mutex_unlock(&event_mutex);
  884. buf[0] = set_to_char[set];
  885. buf[1] = '\n';
  886. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  887. return ret;
  888. }
  889. static ssize_t
  890. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  891. loff_t *ppos)
  892. {
  893. struct trace_subsystem_dir *dir = filp->private_data;
  894. struct event_subsystem *system = dir->subsystem;
  895. const char *name = NULL;
  896. unsigned long val;
  897. ssize_t ret;
  898. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  899. if (ret)
  900. return ret;
  901. ret = tracing_update_buffers();
  902. if (ret < 0)
  903. return ret;
  904. if (val != 0 && val != 1)
  905. return -EINVAL;
  906. /*
  907. * Opening of "enable" adds a ref count to system,
  908. * so the name is safe to use.
  909. */
  910. if (system)
  911. name = system->name;
  912. ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
  913. if (ret)
  914. goto out;
  915. ret = cnt;
  916. out:
  917. *ppos += cnt;
  918. return ret;
  919. }
  920. enum {
  921. FORMAT_HEADER = 1,
  922. FORMAT_FIELD_SEPERATOR = 2,
  923. FORMAT_PRINTFMT = 3,
  924. };
  925. static void *f_next(struct seq_file *m, void *v, loff_t *pos)
  926. {
  927. struct trace_event_call *call = event_file_data(m->private);
  928. struct list_head *common_head = &ftrace_common_fields;
  929. struct list_head *head = trace_get_fields(call);
  930. struct list_head *node = v;
  931. (*pos)++;
  932. switch ((unsigned long)v) {
  933. case FORMAT_HEADER:
  934. node = common_head;
  935. break;
  936. case FORMAT_FIELD_SEPERATOR:
  937. node = head;
  938. break;
  939. case FORMAT_PRINTFMT:
  940. /* all done */
  941. return NULL;
  942. }
  943. node = node->prev;
  944. if (node == common_head)
  945. return (void *)FORMAT_FIELD_SEPERATOR;
  946. else if (node == head)
  947. return (void *)FORMAT_PRINTFMT;
  948. else
  949. return node;
  950. }
  951. static int f_show(struct seq_file *m, void *v)
  952. {
  953. struct trace_event_call *call = event_file_data(m->private);
  954. struct ftrace_event_field *field;
  955. const char *array_descriptor;
  956. switch ((unsigned long)v) {
  957. case FORMAT_HEADER:
  958. seq_printf(m, "name: %s\n", trace_event_name(call));
  959. seq_printf(m, "ID: %d\n", call->event.type);
  960. seq_puts(m, "format:\n");
  961. return 0;
  962. case FORMAT_FIELD_SEPERATOR:
  963. seq_putc(m, '\n');
  964. return 0;
  965. case FORMAT_PRINTFMT:
  966. seq_printf(m, "\nprint fmt: %s\n",
  967. call->print_fmt);
  968. return 0;
  969. }
  970. field = list_entry(v, struct ftrace_event_field, link);
  971. /*
  972. * Smartly shows the array type(except dynamic array).
  973. * Normal:
  974. * field:TYPE VAR
  975. * If TYPE := TYPE[LEN], it is shown:
  976. * field:TYPE VAR[LEN]
  977. */
  978. array_descriptor = strchr(field->type, '[');
  979. if (!strncmp(field->type, "__data_loc", 10))
  980. array_descriptor = NULL;
  981. if (!array_descriptor)
  982. seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  983. field->type, field->name, field->offset,
  984. field->size, !!field->is_signed);
  985. else
  986. seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  987. (int)(array_descriptor - field->type),
  988. field->type, field->name,
  989. array_descriptor, field->offset,
  990. field->size, !!field->is_signed);
  991. return 0;
  992. }
  993. static void *f_start(struct seq_file *m, loff_t *pos)
  994. {
  995. void *p = (void *)FORMAT_HEADER;
  996. loff_t l = 0;
  997. /* ->stop() is called even if ->start() fails */
  998. mutex_lock(&event_mutex);
  999. if (!event_file_data(m->private))
  1000. return ERR_PTR(-ENODEV);
  1001. while (l < *pos && p)
  1002. p = f_next(m, p, &l);
  1003. return p;
  1004. }
  1005. static void f_stop(struct seq_file *m, void *p)
  1006. {
  1007. mutex_unlock(&event_mutex);
  1008. }
  1009. static const struct seq_operations trace_format_seq_ops = {
  1010. .start = f_start,
  1011. .next = f_next,
  1012. .stop = f_stop,
  1013. .show = f_show,
  1014. };
  1015. static int trace_format_open(struct inode *inode, struct file *file)
  1016. {
  1017. struct seq_file *m;
  1018. int ret;
  1019. ret = seq_open(file, &trace_format_seq_ops);
  1020. if (ret < 0)
  1021. return ret;
  1022. m = file->private_data;
  1023. m->private = file;
  1024. return 0;
  1025. }
  1026. static ssize_t
  1027. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1028. {
  1029. int id = (long)event_file_data(filp);
  1030. char buf[32];
  1031. int len;
  1032. if (*ppos)
  1033. return 0;
  1034. if (unlikely(!id))
  1035. return -ENODEV;
  1036. len = sprintf(buf, "%d\n", id);
  1037. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  1038. }
  1039. static ssize_t
  1040. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  1041. loff_t *ppos)
  1042. {
  1043. struct trace_event_file *file;
  1044. struct trace_seq *s;
  1045. int r = -ENODEV;
  1046. if (*ppos)
  1047. return 0;
  1048. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1049. if (!s)
  1050. return -ENOMEM;
  1051. trace_seq_init(s);
  1052. mutex_lock(&event_mutex);
  1053. file = event_file_data(filp);
  1054. if (file)
  1055. print_event_filter(file, s);
  1056. mutex_unlock(&event_mutex);
  1057. if (file)
  1058. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1059. s->buffer, trace_seq_used(s));
  1060. kfree(s);
  1061. return r;
  1062. }
  1063. static ssize_t
  1064. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1065. loff_t *ppos)
  1066. {
  1067. struct trace_event_file *file;
  1068. char *buf;
  1069. int err = -ENODEV;
  1070. if (cnt >= PAGE_SIZE)
  1071. return -EINVAL;
  1072. buf = memdup_user_nul(ubuf, cnt);
  1073. if (IS_ERR(buf))
  1074. return PTR_ERR(buf);
  1075. mutex_lock(&event_mutex);
  1076. file = event_file_data(filp);
  1077. if (file)
  1078. err = apply_event_filter(file, buf);
  1079. mutex_unlock(&event_mutex);
  1080. kfree(buf);
  1081. if (err < 0)
  1082. return err;
  1083. *ppos += cnt;
  1084. return cnt;
  1085. }
  1086. static LIST_HEAD(event_subsystems);
  1087. static int subsystem_open(struct inode *inode, struct file *filp)
  1088. {
  1089. struct event_subsystem *system = NULL;
  1090. struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
  1091. struct trace_array *tr;
  1092. int ret;
  1093. if (tracing_is_disabled())
  1094. return -ENODEV;
  1095. /* Make sure the system still exists */
  1096. mutex_lock(&trace_types_lock);
  1097. mutex_lock(&event_mutex);
  1098. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  1099. list_for_each_entry(dir, &tr->systems, list) {
  1100. if (dir == inode->i_private) {
  1101. /* Don't open systems with no events */
  1102. if (dir->nr_events) {
  1103. __get_system_dir(dir);
  1104. system = dir->subsystem;
  1105. }
  1106. goto exit_loop;
  1107. }
  1108. }
  1109. }
  1110. exit_loop:
  1111. mutex_unlock(&event_mutex);
  1112. mutex_unlock(&trace_types_lock);
  1113. if (!system)
  1114. return -ENODEV;
  1115. /* Some versions of gcc think dir can be uninitialized here */
  1116. WARN_ON(!dir);
  1117. /* Still need to increment the ref count of the system */
  1118. if (trace_array_get(tr) < 0) {
  1119. put_system(dir);
  1120. return -ENODEV;
  1121. }
  1122. ret = tracing_open_generic(inode, filp);
  1123. if (ret < 0) {
  1124. trace_array_put(tr);
  1125. put_system(dir);
  1126. }
  1127. return ret;
  1128. }
  1129. static int system_tr_open(struct inode *inode, struct file *filp)
  1130. {
  1131. struct trace_subsystem_dir *dir;
  1132. struct trace_array *tr = inode->i_private;
  1133. int ret;
  1134. if (tracing_is_disabled())
  1135. return -ENODEV;
  1136. if (trace_array_get(tr) < 0)
  1137. return -ENODEV;
  1138. /* Make a temporary dir that has no system but points to tr */
  1139. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  1140. if (!dir) {
  1141. trace_array_put(tr);
  1142. return -ENOMEM;
  1143. }
  1144. dir->tr = tr;
  1145. ret = tracing_open_generic(inode, filp);
  1146. if (ret < 0) {
  1147. trace_array_put(tr);
  1148. kfree(dir);
  1149. return ret;
  1150. }
  1151. filp->private_data = dir;
  1152. return 0;
  1153. }
  1154. static int subsystem_release(struct inode *inode, struct file *file)
  1155. {
  1156. struct trace_subsystem_dir *dir = file->private_data;
  1157. trace_array_put(dir->tr);
  1158. /*
  1159. * If dir->subsystem is NULL, then this is a temporary
  1160. * descriptor that was made for a trace_array to enable
  1161. * all subsystems.
  1162. */
  1163. if (dir->subsystem)
  1164. put_system(dir);
  1165. else
  1166. kfree(dir);
  1167. return 0;
  1168. }
  1169. static ssize_t
  1170. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  1171. loff_t *ppos)
  1172. {
  1173. struct trace_subsystem_dir *dir = filp->private_data;
  1174. struct event_subsystem *system = dir->subsystem;
  1175. struct trace_seq *s;
  1176. int r;
  1177. if (*ppos)
  1178. return 0;
  1179. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1180. if (!s)
  1181. return -ENOMEM;
  1182. trace_seq_init(s);
  1183. print_subsystem_event_filter(system, s);
  1184. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1185. s->buffer, trace_seq_used(s));
  1186. kfree(s);
  1187. return r;
  1188. }
  1189. static ssize_t
  1190. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1191. loff_t *ppos)
  1192. {
  1193. struct trace_subsystem_dir *dir = filp->private_data;
  1194. char *buf;
  1195. int err;
  1196. if (cnt >= PAGE_SIZE)
  1197. return -EINVAL;
  1198. buf = memdup_user_nul(ubuf, cnt);
  1199. if (IS_ERR(buf))
  1200. return PTR_ERR(buf);
  1201. err = apply_subsystem_event_filter(dir, buf);
  1202. kfree(buf);
  1203. if (err < 0)
  1204. return err;
  1205. *ppos += cnt;
  1206. return cnt;
  1207. }
  1208. static ssize_t
  1209. show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1210. {
  1211. int (*func)(struct trace_seq *s) = filp->private_data;
  1212. struct trace_seq *s;
  1213. int r;
  1214. if (*ppos)
  1215. return 0;
  1216. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1217. if (!s)
  1218. return -ENOMEM;
  1219. trace_seq_init(s);
  1220. func(s);
  1221. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1222. s->buffer, trace_seq_used(s));
  1223. kfree(s);
  1224. return r;
  1225. }
  1226. static int max_pids(struct trace_pid_list *pid_list)
  1227. {
  1228. return (PAGE_SIZE << pid_list->order) / sizeof(pid_t);
  1229. }
  1230. static void ignore_task_cpu(void *data)
  1231. {
  1232. struct trace_array *tr = data;
  1233. struct trace_pid_list *pid_list;
  1234. /*
  1235. * This function is called by on_each_cpu() while the
  1236. * event_mutex is held.
  1237. */
  1238. pid_list = rcu_dereference_protected(tr->filtered_pids,
  1239. mutex_is_locked(&event_mutex));
  1240. this_cpu_write(tr->trace_buffer.data->ignore_pid,
  1241. check_ignore_pid(pid_list, current));
  1242. }
  1243. static ssize_t
  1244. ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
  1245. size_t cnt, loff_t *ppos)
  1246. {
  1247. struct seq_file *m = filp->private_data;
  1248. struct trace_array *tr = m->private;
  1249. struct trace_pid_list *filtered_pids = NULL;
  1250. struct trace_pid_list *pid_list = NULL;
  1251. struct trace_event_file *file;
  1252. struct trace_parser parser;
  1253. unsigned long val;
  1254. loff_t this_pos;
  1255. ssize_t read = 0;
  1256. ssize_t ret = 0;
  1257. pid_t pid;
  1258. int i;
  1259. if (!cnt)
  1260. return 0;
  1261. ret = tracing_update_buffers();
  1262. if (ret < 0)
  1263. return ret;
  1264. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  1265. return -ENOMEM;
  1266. mutex_lock(&event_mutex);
  1267. /*
  1268. * Load as many pids into the array before doing a
  1269. * swap from the tr->filtered_pids to the new list.
  1270. */
  1271. while (cnt > 0) {
  1272. this_pos = 0;
  1273. ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
  1274. if (ret < 0 || !trace_parser_loaded(&parser))
  1275. break;
  1276. read += ret;
  1277. ubuf += ret;
  1278. cnt -= ret;
  1279. parser.buffer[parser.idx] = 0;
  1280. ret = -EINVAL;
  1281. if (kstrtoul(parser.buffer, 0, &val))
  1282. break;
  1283. if (val > INT_MAX)
  1284. break;
  1285. pid = (pid_t)val;
  1286. ret = -ENOMEM;
  1287. if (!pid_list) {
  1288. pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
  1289. if (!pid_list)
  1290. break;
  1291. filtered_pids = rcu_dereference_protected(tr->filtered_pids,
  1292. lockdep_is_held(&event_mutex));
  1293. if (filtered_pids)
  1294. pid_list->order = filtered_pids->order;
  1295. else
  1296. pid_list->order = 0;
  1297. pid_list->pids = (void *)__get_free_pages(GFP_KERNEL,
  1298. pid_list->order);
  1299. if (!pid_list->pids)
  1300. break;
  1301. if (filtered_pids) {
  1302. pid_list->nr_pids = filtered_pids->nr_pids;
  1303. memcpy(pid_list->pids, filtered_pids->pids,
  1304. pid_list->nr_pids * sizeof(pid_t));
  1305. } else
  1306. pid_list->nr_pids = 0;
  1307. }
  1308. if (pid_list->nr_pids >= max_pids(pid_list)) {
  1309. pid_t *pid_page;
  1310. pid_page = (void *)__get_free_pages(GFP_KERNEL,
  1311. pid_list->order + 1);
  1312. if (!pid_page)
  1313. break;
  1314. memcpy(pid_page, pid_list->pids,
  1315. pid_list->nr_pids * sizeof(pid_t));
  1316. free_pages((unsigned long)pid_list->pids, pid_list->order);
  1317. pid_list->order++;
  1318. pid_list->pids = pid_page;
  1319. }
  1320. pid_list->pids[pid_list->nr_pids++] = pid;
  1321. trace_parser_clear(&parser);
  1322. ret = 0;
  1323. }
  1324. trace_parser_put(&parser);
  1325. if (ret < 0) {
  1326. if (pid_list)
  1327. free_pages((unsigned long)pid_list->pids, pid_list->order);
  1328. kfree(pid_list);
  1329. mutex_unlock(&event_mutex);
  1330. return ret;
  1331. }
  1332. if (!pid_list) {
  1333. mutex_unlock(&event_mutex);
  1334. return ret;
  1335. }
  1336. sort(pid_list->pids, pid_list->nr_pids, sizeof(pid_t), cmp_pid, NULL);
  1337. /* Remove duplicates */
  1338. for (i = 1; i < pid_list->nr_pids; i++) {
  1339. int start = i;
  1340. while (i < pid_list->nr_pids &&
  1341. pid_list->pids[i - 1] == pid_list->pids[i])
  1342. i++;
  1343. if (start != i) {
  1344. if (i < pid_list->nr_pids) {
  1345. memmove(&pid_list->pids[start], &pid_list->pids[i],
  1346. (pid_list->nr_pids - i) * sizeof(pid_t));
  1347. pid_list->nr_pids -= i - start;
  1348. i = start;
  1349. } else
  1350. pid_list->nr_pids = start;
  1351. }
  1352. }
  1353. rcu_assign_pointer(tr->filtered_pids, pid_list);
  1354. list_for_each_entry(file, &tr->events, list) {
  1355. set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
  1356. }
  1357. if (filtered_pids) {
  1358. synchronize_sched();
  1359. free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
  1360. kfree(filtered_pids);
  1361. } else {
  1362. /*
  1363. * Register a probe that is called before all other probes
  1364. * to set ignore_pid if next or prev do not match.
  1365. * Register a probe this is called after all other probes
  1366. * to only keep ignore_pid set if next pid matches.
  1367. */
  1368. register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
  1369. tr, INT_MAX);
  1370. register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
  1371. tr, 0);
  1372. register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
  1373. tr, INT_MAX);
  1374. register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
  1375. tr, 0);
  1376. register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
  1377. tr, INT_MAX);
  1378. register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
  1379. tr, 0);
  1380. register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
  1381. tr, INT_MAX);
  1382. register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
  1383. tr, 0);
  1384. }
  1385. /*
  1386. * Ignoring of pids is done at task switch. But we have to
  1387. * check for those tasks that are currently running.
  1388. * Always do this in case a pid was appended or removed.
  1389. */
  1390. on_each_cpu(ignore_task_cpu, tr, 1);
  1391. mutex_unlock(&event_mutex);
  1392. ret = read;
  1393. *ppos += read;
  1394. return ret;
  1395. }
  1396. static int ftrace_event_avail_open(struct inode *inode, struct file *file);
  1397. static int ftrace_event_set_open(struct inode *inode, struct file *file);
  1398. static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
  1399. static int ftrace_event_release(struct inode *inode, struct file *file);
  1400. static const struct seq_operations show_event_seq_ops = {
  1401. .start = t_start,
  1402. .next = t_next,
  1403. .show = t_show,
  1404. .stop = t_stop,
  1405. };
  1406. static const struct seq_operations show_set_event_seq_ops = {
  1407. .start = s_start,
  1408. .next = s_next,
  1409. .show = t_show,
  1410. .stop = t_stop,
  1411. };
  1412. static const struct seq_operations show_set_pid_seq_ops = {
  1413. .start = p_start,
  1414. .next = p_next,
  1415. .show = p_show,
  1416. .stop = p_stop,
  1417. };
  1418. static const struct file_operations ftrace_avail_fops = {
  1419. .open = ftrace_event_avail_open,
  1420. .read = seq_read,
  1421. .llseek = seq_lseek,
  1422. .release = seq_release,
  1423. };
  1424. static const struct file_operations ftrace_set_event_fops = {
  1425. .open = ftrace_event_set_open,
  1426. .read = seq_read,
  1427. .write = ftrace_event_write,
  1428. .llseek = seq_lseek,
  1429. .release = ftrace_event_release,
  1430. };
  1431. static const struct file_operations ftrace_set_event_pid_fops = {
  1432. .open = ftrace_event_set_pid_open,
  1433. .read = seq_read,
  1434. .write = ftrace_event_pid_write,
  1435. .llseek = seq_lseek,
  1436. .release = ftrace_event_release,
  1437. };
  1438. static const struct file_operations ftrace_enable_fops = {
  1439. .open = tracing_open_generic,
  1440. .read = event_enable_read,
  1441. .write = event_enable_write,
  1442. .llseek = default_llseek,
  1443. };
  1444. static const struct file_operations ftrace_event_format_fops = {
  1445. .open = trace_format_open,
  1446. .read = seq_read,
  1447. .llseek = seq_lseek,
  1448. .release = seq_release,
  1449. };
  1450. static const struct file_operations ftrace_event_id_fops = {
  1451. .read = event_id_read,
  1452. .llseek = default_llseek,
  1453. };
  1454. static const struct file_operations ftrace_event_filter_fops = {
  1455. .open = tracing_open_generic,
  1456. .read = event_filter_read,
  1457. .write = event_filter_write,
  1458. .llseek = default_llseek,
  1459. };
  1460. static const struct file_operations ftrace_subsystem_filter_fops = {
  1461. .open = subsystem_open,
  1462. .read = subsystem_filter_read,
  1463. .write = subsystem_filter_write,
  1464. .llseek = default_llseek,
  1465. .release = subsystem_release,
  1466. };
  1467. static const struct file_operations ftrace_system_enable_fops = {
  1468. .open = subsystem_open,
  1469. .read = system_enable_read,
  1470. .write = system_enable_write,
  1471. .llseek = default_llseek,
  1472. .release = subsystem_release,
  1473. };
  1474. static const struct file_operations ftrace_tr_enable_fops = {
  1475. .open = system_tr_open,
  1476. .read = system_enable_read,
  1477. .write = system_enable_write,
  1478. .llseek = default_llseek,
  1479. .release = subsystem_release,
  1480. };
  1481. static const struct file_operations ftrace_show_header_fops = {
  1482. .open = tracing_open_generic,
  1483. .read = show_header,
  1484. .llseek = default_llseek,
  1485. };
  1486. static int
  1487. ftrace_event_open(struct inode *inode, struct file *file,
  1488. const struct seq_operations *seq_ops)
  1489. {
  1490. struct seq_file *m;
  1491. int ret;
  1492. ret = seq_open(file, seq_ops);
  1493. if (ret < 0)
  1494. return ret;
  1495. m = file->private_data;
  1496. /* copy tr over to seq ops */
  1497. m->private = inode->i_private;
  1498. return ret;
  1499. }
  1500. static int ftrace_event_release(struct inode *inode, struct file *file)
  1501. {
  1502. struct trace_array *tr = inode->i_private;
  1503. trace_array_put(tr);
  1504. return seq_release(inode, file);
  1505. }
  1506. static int
  1507. ftrace_event_avail_open(struct inode *inode, struct file *file)
  1508. {
  1509. const struct seq_operations *seq_ops = &show_event_seq_ops;
  1510. return ftrace_event_open(inode, file, seq_ops);
  1511. }
  1512. static int
  1513. ftrace_event_set_open(struct inode *inode, struct file *file)
  1514. {
  1515. const struct seq_operations *seq_ops = &show_set_event_seq_ops;
  1516. struct trace_array *tr = inode->i_private;
  1517. int ret;
  1518. if (trace_array_get(tr) < 0)
  1519. return -ENODEV;
  1520. if ((file->f_mode & FMODE_WRITE) &&
  1521. (file->f_flags & O_TRUNC))
  1522. ftrace_clear_events(tr);
  1523. ret = ftrace_event_open(inode, file, seq_ops);
  1524. if (ret < 0)
  1525. trace_array_put(tr);
  1526. return ret;
  1527. }
  1528. static int
  1529. ftrace_event_set_pid_open(struct inode *inode, struct file *file)
  1530. {
  1531. const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
  1532. struct trace_array *tr = inode->i_private;
  1533. int ret;
  1534. if (trace_array_get(tr) < 0)
  1535. return -ENODEV;
  1536. if ((file->f_mode & FMODE_WRITE) &&
  1537. (file->f_flags & O_TRUNC))
  1538. ftrace_clear_event_pids(tr);
  1539. ret = ftrace_event_open(inode, file, seq_ops);
  1540. if (ret < 0)
  1541. trace_array_put(tr);
  1542. return ret;
  1543. }
  1544. static struct event_subsystem *
  1545. create_new_subsystem(const char *name)
  1546. {
  1547. struct event_subsystem *system;
  1548. /* need to create new entry */
  1549. system = kmalloc(sizeof(*system), GFP_KERNEL);
  1550. if (!system)
  1551. return NULL;
  1552. system->ref_count = 1;
  1553. /* Only allocate if dynamic (kprobes and modules) */
  1554. system->name = kstrdup_const(name, GFP_KERNEL);
  1555. if (!system->name)
  1556. goto out_free;
  1557. system->filter = NULL;
  1558. system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
  1559. if (!system->filter)
  1560. goto out_free;
  1561. list_add(&system->list, &event_subsystems);
  1562. return system;
  1563. out_free:
  1564. kfree_const(system->name);
  1565. kfree(system);
  1566. return NULL;
  1567. }
  1568. static struct dentry *
  1569. event_subsystem_dir(struct trace_array *tr, const char *name,
  1570. struct trace_event_file *file, struct dentry *parent)
  1571. {
  1572. struct trace_subsystem_dir *dir;
  1573. struct event_subsystem *system;
  1574. struct dentry *entry;
  1575. /* First see if we did not already create this dir */
  1576. list_for_each_entry(dir, &tr->systems, list) {
  1577. system = dir->subsystem;
  1578. if (strcmp(system->name, name) == 0) {
  1579. dir->nr_events++;
  1580. file->system = dir;
  1581. return dir->entry;
  1582. }
  1583. }
  1584. /* Now see if the system itself exists. */
  1585. list_for_each_entry(system, &event_subsystems, list) {
  1586. if (strcmp(system->name, name) == 0)
  1587. break;
  1588. }
  1589. /* Reset system variable when not found */
  1590. if (&system->list == &event_subsystems)
  1591. system = NULL;
  1592. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  1593. if (!dir)
  1594. goto out_fail;
  1595. if (!system) {
  1596. system = create_new_subsystem(name);
  1597. if (!system)
  1598. goto out_free;
  1599. } else
  1600. __get_system(system);
  1601. dir->entry = tracefs_create_dir(name, parent);
  1602. if (!dir->entry) {
  1603. pr_warn("Failed to create system directory %s\n", name);
  1604. __put_system(system);
  1605. goto out_free;
  1606. }
  1607. dir->tr = tr;
  1608. dir->ref_count = 1;
  1609. dir->nr_events = 1;
  1610. dir->subsystem = system;
  1611. file->system = dir;
  1612. entry = tracefs_create_file("filter", 0644, dir->entry, dir,
  1613. &ftrace_subsystem_filter_fops);
  1614. if (!entry) {
  1615. kfree(system->filter);
  1616. system->filter = NULL;
  1617. pr_warn("Could not create tracefs '%s/filter' entry\n", name);
  1618. }
  1619. trace_create_file("enable", 0644, dir->entry, dir,
  1620. &ftrace_system_enable_fops);
  1621. list_add(&dir->list, &tr->systems);
  1622. return dir->entry;
  1623. out_free:
  1624. kfree(dir);
  1625. out_fail:
  1626. /* Only print this message if failed on memory allocation */
  1627. if (!dir || !system)
  1628. pr_warn("No memory to create event subsystem %s\n", name);
  1629. return NULL;
  1630. }
  1631. static int
  1632. event_create_dir(struct dentry *parent, struct trace_event_file *file)
  1633. {
  1634. struct trace_event_call *call = file->event_call;
  1635. struct trace_array *tr = file->tr;
  1636. struct list_head *head;
  1637. struct dentry *d_events;
  1638. const char *name;
  1639. int ret;
  1640. /*
  1641. * If the trace point header did not define TRACE_SYSTEM
  1642. * then the system would be called "TRACE_SYSTEM".
  1643. */
  1644. if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
  1645. d_events = event_subsystem_dir(tr, call->class->system, file, parent);
  1646. if (!d_events)
  1647. return -ENOMEM;
  1648. } else
  1649. d_events = parent;
  1650. name = trace_event_name(call);
  1651. file->dir = tracefs_create_dir(name, d_events);
  1652. if (!file->dir) {
  1653. pr_warn("Could not create tracefs '%s' directory\n", name);
  1654. return -1;
  1655. }
  1656. if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
  1657. trace_create_file("enable", 0644, file->dir, file,
  1658. &ftrace_enable_fops);
  1659. #ifdef CONFIG_PERF_EVENTS
  1660. if (call->event.type && call->class->reg)
  1661. trace_create_file("id", 0444, file->dir,
  1662. (void *)(long)call->event.type,
  1663. &ftrace_event_id_fops);
  1664. #endif
  1665. /*
  1666. * Other events may have the same class. Only update
  1667. * the fields if they are not already defined.
  1668. */
  1669. head = trace_get_fields(call);
  1670. if (list_empty(head)) {
  1671. ret = call->class->define_fields(call);
  1672. if (ret < 0) {
  1673. pr_warn("Could not initialize trace point events/%s\n",
  1674. name);
  1675. return -1;
  1676. }
  1677. }
  1678. trace_create_file("filter", 0644, file->dir, file,
  1679. &ftrace_event_filter_fops);
  1680. trace_create_file("trigger", 0644, file->dir, file,
  1681. &event_trigger_fops);
  1682. trace_create_file("format", 0444, file->dir, call,
  1683. &ftrace_event_format_fops);
  1684. return 0;
  1685. }
  1686. static void remove_event_from_tracers(struct trace_event_call *call)
  1687. {
  1688. struct trace_event_file *file;
  1689. struct trace_array *tr;
  1690. do_for_each_event_file_safe(tr, file) {
  1691. if (file->event_call != call)
  1692. continue;
  1693. remove_event_file_dir(file);
  1694. /*
  1695. * The do_for_each_event_file_safe() is
  1696. * a double loop. After finding the call for this
  1697. * trace_array, we use break to jump to the next
  1698. * trace_array.
  1699. */
  1700. break;
  1701. } while_for_each_event_file();
  1702. }
  1703. static void event_remove(struct trace_event_call *call)
  1704. {
  1705. struct trace_array *tr;
  1706. struct trace_event_file *file;
  1707. do_for_each_event_file(tr, file) {
  1708. if (file->event_call != call)
  1709. continue;
  1710. ftrace_event_enable_disable(file, 0);
  1711. /*
  1712. * The do_for_each_event_file() is
  1713. * a double loop. After finding the call for this
  1714. * trace_array, we use break to jump to the next
  1715. * trace_array.
  1716. */
  1717. break;
  1718. } while_for_each_event_file();
  1719. if (call->event.funcs)
  1720. __unregister_trace_event(&call->event);
  1721. remove_event_from_tracers(call);
  1722. list_del(&call->list);
  1723. }
  1724. static int event_init(struct trace_event_call *call)
  1725. {
  1726. int ret = 0;
  1727. const char *name;
  1728. name = trace_event_name(call);
  1729. if (WARN_ON(!name))
  1730. return -EINVAL;
  1731. if (call->class->raw_init) {
  1732. ret = call->class->raw_init(call);
  1733. if (ret < 0 && ret != -ENOSYS)
  1734. pr_warn("Could not initialize trace events/%s\n", name);
  1735. }
  1736. return ret;
  1737. }
  1738. static int
  1739. __register_event(struct trace_event_call *call, struct module *mod)
  1740. {
  1741. int ret;
  1742. ret = event_init(call);
  1743. if (ret < 0)
  1744. return ret;
  1745. list_add(&call->list, &ftrace_events);
  1746. call->mod = mod;
  1747. return 0;
  1748. }
  1749. static char *enum_replace(char *ptr, struct trace_enum_map *map, int len)
  1750. {
  1751. int rlen;
  1752. int elen;
  1753. /* Find the length of the enum value as a string */
  1754. elen = snprintf(ptr, 0, "%ld", map->enum_value);
  1755. /* Make sure there's enough room to replace the string with the value */
  1756. if (len < elen)
  1757. return NULL;
  1758. snprintf(ptr, elen + 1, "%ld", map->enum_value);
  1759. /* Get the rest of the string of ptr */
  1760. rlen = strlen(ptr + len);
  1761. memmove(ptr + elen, ptr + len, rlen);
  1762. /* Make sure we end the new string */
  1763. ptr[elen + rlen] = 0;
  1764. return ptr + elen;
  1765. }
  1766. static void update_event_printk(struct trace_event_call *call,
  1767. struct trace_enum_map *map)
  1768. {
  1769. char *ptr;
  1770. int quote = 0;
  1771. int len = strlen(map->enum_string);
  1772. for (ptr = call->print_fmt; *ptr; ptr++) {
  1773. if (*ptr == '\\') {
  1774. ptr++;
  1775. /* paranoid */
  1776. if (!*ptr)
  1777. break;
  1778. continue;
  1779. }
  1780. if (*ptr == '"') {
  1781. quote ^= 1;
  1782. continue;
  1783. }
  1784. if (quote)
  1785. continue;
  1786. if (isdigit(*ptr)) {
  1787. /* skip numbers */
  1788. do {
  1789. ptr++;
  1790. /* Check for alpha chars like ULL */
  1791. } while (isalnum(*ptr));
  1792. if (!*ptr)
  1793. break;
  1794. /*
  1795. * A number must have some kind of delimiter after
  1796. * it, and we can ignore that too.
  1797. */
  1798. continue;
  1799. }
  1800. if (isalpha(*ptr) || *ptr == '_') {
  1801. if (strncmp(map->enum_string, ptr, len) == 0 &&
  1802. !isalnum(ptr[len]) && ptr[len] != '_') {
  1803. ptr = enum_replace(ptr, map, len);
  1804. /* Hmm, enum string smaller than value */
  1805. if (WARN_ON_ONCE(!ptr))
  1806. return;
  1807. /*
  1808. * No need to decrement here, as enum_replace()
  1809. * returns the pointer to the character passed
  1810. * the enum, and two enums can not be placed
  1811. * back to back without something in between.
  1812. * We can skip that something in between.
  1813. */
  1814. continue;
  1815. }
  1816. skip_more:
  1817. do {
  1818. ptr++;
  1819. } while (isalnum(*ptr) || *ptr == '_');
  1820. if (!*ptr)
  1821. break;
  1822. /*
  1823. * If what comes after this variable is a '.' or
  1824. * '->' then we can continue to ignore that string.
  1825. */
  1826. if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
  1827. ptr += *ptr == '.' ? 1 : 2;
  1828. if (!*ptr)
  1829. break;
  1830. goto skip_more;
  1831. }
  1832. /*
  1833. * Once again, we can skip the delimiter that came
  1834. * after the string.
  1835. */
  1836. continue;
  1837. }
  1838. }
  1839. }
  1840. void trace_event_enum_update(struct trace_enum_map **map, int len)
  1841. {
  1842. struct trace_event_call *call, *p;
  1843. const char *last_system = NULL;
  1844. int last_i;
  1845. int i;
  1846. down_write(&trace_event_sem);
  1847. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  1848. /* events are usually grouped together with systems */
  1849. if (!last_system || call->class->system != last_system) {
  1850. last_i = 0;
  1851. last_system = call->class->system;
  1852. }
  1853. for (i = last_i; i < len; i++) {
  1854. if (call->class->system == map[i]->system) {
  1855. /* Save the first system if need be */
  1856. if (!last_i)
  1857. last_i = i;
  1858. update_event_printk(call, map[i]);
  1859. }
  1860. }
  1861. }
  1862. up_write(&trace_event_sem);
  1863. }
  1864. static struct trace_event_file *
  1865. trace_create_new_event(struct trace_event_call *call,
  1866. struct trace_array *tr)
  1867. {
  1868. struct trace_event_file *file;
  1869. file = kmem_cache_alloc(file_cachep, GFP_TRACE);
  1870. if (!file)
  1871. return NULL;
  1872. file->event_call = call;
  1873. file->tr = tr;
  1874. atomic_set(&file->sm_ref, 0);
  1875. atomic_set(&file->tm_ref, 0);
  1876. INIT_LIST_HEAD(&file->triggers);
  1877. list_add(&file->list, &tr->events);
  1878. return file;
  1879. }
  1880. /* Add an event to a trace directory */
  1881. static int
  1882. __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
  1883. {
  1884. struct trace_event_file *file;
  1885. file = trace_create_new_event(call, tr);
  1886. if (!file)
  1887. return -ENOMEM;
  1888. return event_create_dir(tr->event_dir, file);
  1889. }
  1890. /*
  1891. * Just create a decriptor for early init. A descriptor is required
  1892. * for enabling events at boot. We want to enable events before
  1893. * the filesystem is initialized.
  1894. */
  1895. static __init int
  1896. __trace_early_add_new_event(struct trace_event_call *call,
  1897. struct trace_array *tr)
  1898. {
  1899. struct trace_event_file *file;
  1900. file = trace_create_new_event(call, tr);
  1901. if (!file)
  1902. return -ENOMEM;
  1903. return 0;
  1904. }
  1905. struct ftrace_module_file_ops;
  1906. static void __add_event_to_tracers(struct trace_event_call *call);
  1907. /* Add an additional event_call dynamically */
  1908. int trace_add_event_call(struct trace_event_call *call)
  1909. {
  1910. int ret;
  1911. mutex_lock(&trace_types_lock);
  1912. mutex_lock(&event_mutex);
  1913. ret = __register_event(call, NULL);
  1914. if (ret >= 0)
  1915. __add_event_to_tracers(call);
  1916. mutex_unlock(&event_mutex);
  1917. mutex_unlock(&trace_types_lock);
  1918. return ret;
  1919. }
  1920. /*
  1921. * Must be called under locking of trace_types_lock, event_mutex and
  1922. * trace_event_sem.
  1923. */
  1924. static void __trace_remove_event_call(struct trace_event_call *call)
  1925. {
  1926. event_remove(call);
  1927. trace_destroy_fields(call);
  1928. free_event_filter(call->filter);
  1929. call->filter = NULL;
  1930. }
  1931. static int probe_remove_event_call(struct trace_event_call *call)
  1932. {
  1933. struct trace_array *tr;
  1934. struct trace_event_file *file;
  1935. #ifdef CONFIG_PERF_EVENTS
  1936. if (call->perf_refcount)
  1937. return -EBUSY;
  1938. #endif
  1939. do_for_each_event_file(tr, file) {
  1940. if (file->event_call != call)
  1941. continue;
  1942. /*
  1943. * We can't rely on ftrace_event_enable_disable(enable => 0)
  1944. * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
  1945. * TRACE_REG_UNREGISTER.
  1946. */
  1947. if (file->flags & EVENT_FILE_FL_ENABLED)
  1948. return -EBUSY;
  1949. /*
  1950. * The do_for_each_event_file_safe() is
  1951. * a double loop. After finding the call for this
  1952. * trace_array, we use break to jump to the next
  1953. * trace_array.
  1954. */
  1955. break;
  1956. } while_for_each_event_file();
  1957. __trace_remove_event_call(call);
  1958. return 0;
  1959. }
  1960. /* Remove an event_call */
  1961. int trace_remove_event_call(struct trace_event_call *call)
  1962. {
  1963. int ret;
  1964. mutex_lock(&trace_types_lock);
  1965. mutex_lock(&event_mutex);
  1966. down_write(&trace_event_sem);
  1967. ret = probe_remove_event_call(call);
  1968. up_write(&trace_event_sem);
  1969. mutex_unlock(&event_mutex);
  1970. mutex_unlock(&trace_types_lock);
  1971. return ret;
  1972. }
  1973. #define for_each_event(event, start, end) \
  1974. for (event = start; \
  1975. (unsigned long)event < (unsigned long)end; \
  1976. event++)
  1977. #ifdef CONFIG_MODULES
  1978. static void trace_module_add_events(struct module *mod)
  1979. {
  1980. struct trace_event_call **call, **start, **end;
  1981. if (!mod->num_trace_events)
  1982. return;
  1983. /* Don't add infrastructure for mods without tracepoints */
  1984. if (trace_module_has_bad_taint(mod)) {
  1985. pr_err("%s: module has bad taint, not creating trace events\n",
  1986. mod->name);
  1987. return;
  1988. }
  1989. start = mod->trace_events;
  1990. end = mod->trace_events + mod->num_trace_events;
  1991. for_each_event(call, start, end) {
  1992. __register_event(*call, mod);
  1993. __add_event_to_tracers(*call);
  1994. }
  1995. }
  1996. static void trace_module_remove_events(struct module *mod)
  1997. {
  1998. struct trace_event_call *call, *p;
  1999. bool clear_trace = false;
  2000. down_write(&trace_event_sem);
  2001. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  2002. if (call->mod == mod) {
  2003. if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
  2004. clear_trace = true;
  2005. __trace_remove_event_call(call);
  2006. }
  2007. }
  2008. up_write(&trace_event_sem);
  2009. /*
  2010. * It is safest to reset the ring buffer if the module being unloaded
  2011. * registered any events that were used. The only worry is if
  2012. * a new module gets loaded, and takes on the same id as the events
  2013. * of this module. When printing out the buffer, traced events left
  2014. * over from this module may be passed to the new module events and
  2015. * unexpected results may occur.
  2016. */
  2017. if (clear_trace)
  2018. tracing_reset_all_online_cpus();
  2019. }
  2020. static int trace_module_notify(struct notifier_block *self,
  2021. unsigned long val, void *data)
  2022. {
  2023. struct module *mod = data;
  2024. mutex_lock(&trace_types_lock);
  2025. mutex_lock(&event_mutex);
  2026. switch (val) {
  2027. case MODULE_STATE_COMING:
  2028. trace_module_add_events(mod);
  2029. break;
  2030. case MODULE_STATE_GOING:
  2031. trace_module_remove_events(mod);
  2032. break;
  2033. }
  2034. mutex_unlock(&event_mutex);
  2035. mutex_unlock(&trace_types_lock);
  2036. return 0;
  2037. }
  2038. static struct notifier_block trace_module_nb = {
  2039. .notifier_call = trace_module_notify,
  2040. .priority = 1, /* higher than trace.c module notify */
  2041. };
  2042. #endif /* CONFIG_MODULES */
  2043. /* Create a new event directory structure for a trace directory. */
  2044. static void
  2045. __trace_add_event_dirs(struct trace_array *tr)
  2046. {
  2047. struct trace_event_call *call;
  2048. int ret;
  2049. list_for_each_entry(call, &ftrace_events, list) {
  2050. ret = __trace_add_new_event(call, tr);
  2051. if (ret < 0)
  2052. pr_warn("Could not create directory for event %s\n",
  2053. trace_event_name(call));
  2054. }
  2055. }
  2056. struct trace_event_file *
  2057. find_event_file(struct trace_array *tr, const char *system, const char *event)
  2058. {
  2059. struct trace_event_file *file;
  2060. struct trace_event_call *call;
  2061. const char *name;
  2062. list_for_each_entry(file, &tr->events, list) {
  2063. call = file->event_call;
  2064. name = trace_event_name(call);
  2065. if (!name || !call->class || !call->class->reg)
  2066. continue;
  2067. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  2068. continue;
  2069. if (strcmp(event, name) == 0 &&
  2070. strcmp(system, call->class->system) == 0)
  2071. return file;
  2072. }
  2073. return NULL;
  2074. }
  2075. #ifdef CONFIG_DYNAMIC_FTRACE
  2076. /* Avoid typos */
  2077. #define ENABLE_EVENT_STR "enable_event"
  2078. #define DISABLE_EVENT_STR "disable_event"
  2079. struct event_probe_data {
  2080. struct trace_event_file *file;
  2081. unsigned long count;
  2082. int ref;
  2083. bool enable;
  2084. };
  2085. static void
  2086. event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
  2087. {
  2088. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  2089. struct event_probe_data *data = *pdata;
  2090. if (!data)
  2091. return;
  2092. if (data->enable)
  2093. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  2094. else
  2095. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  2096. }
  2097. static void
  2098. event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
  2099. {
  2100. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  2101. struct event_probe_data *data = *pdata;
  2102. if (!data)
  2103. return;
  2104. if (!data->count)
  2105. return;
  2106. /* Skip if the event is in a state we want to switch to */
  2107. if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  2108. return;
  2109. if (data->count != -1)
  2110. (data->count)--;
  2111. event_enable_probe(ip, parent_ip, _data);
  2112. }
  2113. static int
  2114. event_enable_print(struct seq_file *m, unsigned long ip,
  2115. struct ftrace_probe_ops *ops, void *_data)
  2116. {
  2117. struct event_probe_data *data = _data;
  2118. seq_printf(m, "%ps:", (void *)ip);
  2119. seq_printf(m, "%s:%s:%s",
  2120. data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
  2121. data->file->event_call->class->system,
  2122. trace_event_name(data->file->event_call));
  2123. if (data->count == -1)
  2124. seq_puts(m, ":unlimited\n");
  2125. else
  2126. seq_printf(m, ":count=%ld\n", data->count);
  2127. return 0;
  2128. }
  2129. static int
  2130. event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
  2131. void **_data)
  2132. {
  2133. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  2134. struct event_probe_data *data = *pdata;
  2135. data->ref++;
  2136. return 0;
  2137. }
  2138. static void
  2139. event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
  2140. void **_data)
  2141. {
  2142. struct event_probe_data **pdata = (struct event_probe_data **)_data;
  2143. struct event_probe_data *data = *pdata;
  2144. if (WARN_ON_ONCE(data->ref <= 0))
  2145. return;
  2146. data->ref--;
  2147. if (!data->ref) {
  2148. /* Remove the SOFT_MODE flag */
  2149. __ftrace_event_enable_disable(data->file, 0, 1);
  2150. module_put(data->file->event_call->mod);
  2151. kfree(data);
  2152. }
  2153. *pdata = NULL;
  2154. }
  2155. static struct ftrace_probe_ops event_enable_probe_ops = {
  2156. .func = event_enable_probe,
  2157. .print = event_enable_print,
  2158. .init = event_enable_init,
  2159. .free = event_enable_free,
  2160. };
  2161. static struct ftrace_probe_ops event_enable_count_probe_ops = {
  2162. .func = event_enable_count_probe,
  2163. .print = event_enable_print,
  2164. .init = event_enable_init,
  2165. .free = event_enable_free,
  2166. };
  2167. static struct ftrace_probe_ops event_disable_probe_ops = {
  2168. .func = event_enable_probe,
  2169. .print = event_enable_print,
  2170. .init = event_enable_init,
  2171. .free = event_enable_free,
  2172. };
  2173. static struct ftrace_probe_ops event_disable_count_probe_ops = {
  2174. .func = event_enable_count_probe,
  2175. .print = event_enable_print,
  2176. .init = event_enable_init,
  2177. .free = event_enable_free,
  2178. };
  2179. static int
  2180. event_enable_func(struct ftrace_hash *hash,
  2181. char *glob, char *cmd, char *param, int enabled)
  2182. {
  2183. struct trace_array *tr = top_trace_array();
  2184. struct trace_event_file *file;
  2185. struct ftrace_probe_ops *ops;
  2186. struct event_probe_data *data;
  2187. const char *system;
  2188. const char *event;
  2189. char *number;
  2190. bool enable;
  2191. int ret;
  2192. if (!tr)
  2193. return -ENODEV;
  2194. /* hash funcs only work with set_ftrace_filter */
  2195. if (!enabled || !param)
  2196. return -EINVAL;
  2197. system = strsep(&param, ":");
  2198. if (!param)
  2199. return -EINVAL;
  2200. event = strsep(&param, ":");
  2201. mutex_lock(&event_mutex);
  2202. ret = -EINVAL;
  2203. file = find_event_file(tr, system, event);
  2204. if (!file)
  2205. goto out;
  2206. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  2207. if (enable)
  2208. ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
  2209. else
  2210. ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
  2211. if (glob[0] == '!') {
  2212. unregister_ftrace_function_probe_func(glob+1, ops);
  2213. ret = 0;
  2214. goto out;
  2215. }
  2216. ret = -ENOMEM;
  2217. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2218. if (!data)
  2219. goto out;
  2220. data->enable = enable;
  2221. data->count = -1;
  2222. data->file = file;
  2223. if (!param)
  2224. goto out_reg;
  2225. number = strsep(&param, ":");
  2226. ret = -EINVAL;
  2227. if (!strlen(number))
  2228. goto out_free;
  2229. /*
  2230. * We use the callback data field (which is a pointer)
  2231. * as our counter.
  2232. */
  2233. ret = kstrtoul(number, 0, &data->count);
  2234. if (ret)
  2235. goto out_free;
  2236. out_reg:
  2237. /* Don't let event modules unload while probe registered */
  2238. ret = try_module_get(file->event_call->mod);
  2239. if (!ret) {
  2240. ret = -EBUSY;
  2241. goto out_free;
  2242. }
  2243. ret = __ftrace_event_enable_disable(file, 1, 1);
  2244. if (ret < 0)
  2245. goto out_put;
  2246. ret = register_ftrace_function_probe(glob, ops, data);
  2247. /*
  2248. * The above returns on success the # of functions enabled,
  2249. * but if it didn't find any functions it returns zero.
  2250. * Consider no functions a failure too.
  2251. */
  2252. if (!ret) {
  2253. ret = -ENOENT;
  2254. goto out_disable;
  2255. } else if (ret < 0)
  2256. goto out_disable;
  2257. /* Just return zero, not the number of enabled functions */
  2258. ret = 0;
  2259. out:
  2260. mutex_unlock(&event_mutex);
  2261. return ret;
  2262. out_disable:
  2263. __ftrace_event_enable_disable(file, 0, 1);
  2264. out_put:
  2265. module_put(file->event_call->mod);
  2266. out_free:
  2267. kfree(data);
  2268. goto out;
  2269. }
  2270. static struct ftrace_func_command event_enable_cmd = {
  2271. .name = ENABLE_EVENT_STR,
  2272. .func = event_enable_func,
  2273. };
  2274. static struct ftrace_func_command event_disable_cmd = {
  2275. .name = DISABLE_EVENT_STR,
  2276. .func = event_enable_func,
  2277. };
  2278. static __init int register_event_cmds(void)
  2279. {
  2280. int ret;
  2281. ret = register_ftrace_command(&event_enable_cmd);
  2282. if (WARN_ON(ret < 0))
  2283. return ret;
  2284. ret = register_ftrace_command(&event_disable_cmd);
  2285. if (WARN_ON(ret < 0))
  2286. unregister_ftrace_command(&event_enable_cmd);
  2287. return ret;
  2288. }
  2289. #else
  2290. static inline int register_event_cmds(void) { return 0; }
  2291. #endif /* CONFIG_DYNAMIC_FTRACE */
  2292. /*
  2293. * The top level array has already had its trace_event_file
  2294. * descriptors created in order to allow for early events to
  2295. * be recorded. This function is called after the tracefs has been
  2296. * initialized, and we now have to create the files associated
  2297. * to the events.
  2298. */
  2299. static __init void
  2300. __trace_early_add_event_dirs(struct trace_array *tr)
  2301. {
  2302. struct trace_event_file *file;
  2303. int ret;
  2304. list_for_each_entry(file, &tr->events, list) {
  2305. ret = event_create_dir(tr->event_dir, file);
  2306. if (ret < 0)
  2307. pr_warn("Could not create directory for event %s\n",
  2308. trace_event_name(file->event_call));
  2309. }
  2310. }
  2311. /*
  2312. * For early boot up, the top trace array requires to have
  2313. * a list of events that can be enabled. This must be done before
  2314. * the filesystem is set up in order to allow events to be traced
  2315. * early.
  2316. */
  2317. static __init void
  2318. __trace_early_add_events(struct trace_array *tr)
  2319. {
  2320. struct trace_event_call *call;
  2321. int ret;
  2322. list_for_each_entry(call, &ftrace_events, list) {
  2323. /* Early boot up should not have any modules loaded */
  2324. if (WARN_ON_ONCE(call->mod))
  2325. continue;
  2326. ret = __trace_early_add_new_event(call, tr);
  2327. if (ret < 0)
  2328. pr_warn("Could not create early event %s\n",
  2329. trace_event_name(call));
  2330. }
  2331. }
  2332. /* Remove the event directory structure for a trace directory. */
  2333. static void
  2334. __trace_remove_event_dirs(struct trace_array *tr)
  2335. {
  2336. struct trace_event_file *file, *next;
  2337. list_for_each_entry_safe(file, next, &tr->events, list)
  2338. remove_event_file_dir(file);
  2339. }
  2340. static void __add_event_to_tracers(struct trace_event_call *call)
  2341. {
  2342. struct trace_array *tr;
  2343. list_for_each_entry(tr, &ftrace_trace_arrays, list)
  2344. __trace_add_new_event(call, tr);
  2345. }
  2346. extern struct trace_event_call *__start_ftrace_events[];
  2347. extern struct trace_event_call *__stop_ftrace_events[];
  2348. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  2349. static __init int setup_trace_event(char *str)
  2350. {
  2351. strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
  2352. ring_buffer_expanded = true;
  2353. tracing_selftest_disabled = true;
  2354. return 1;
  2355. }
  2356. __setup("trace_event=", setup_trace_event);
  2357. /* Expects to have event_mutex held when called */
  2358. static int
  2359. create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
  2360. {
  2361. struct dentry *d_events;
  2362. struct dentry *entry;
  2363. entry = tracefs_create_file("set_event", 0644, parent,
  2364. tr, &ftrace_set_event_fops);
  2365. if (!entry) {
  2366. pr_warn("Could not create tracefs 'set_event' entry\n");
  2367. return -ENOMEM;
  2368. }
  2369. d_events = tracefs_create_dir("events", parent);
  2370. if (!d_events) {
  2371. pr_warn("Could not create tracefs 'events' directory\n");
  2372. return -ENOMEM;
  2373. }
  2374. entry = tracefs_create_file("set_event_pid", 0644, parent,
  2375. tr, &ftrace_set_event_pid_fops);
  2376. /* ring buffer internal formats */
  2377. trace_create_file("header_page", 0444, d_events,
  2378. ring_buffer_print_page_header,
  2379. &ftrace_show_header_fops);
  2380. trace_create_file("header_event", 0444, d_events,
  2381. ring_buffer_print_entry_header,
  2382. &ftrace_show_header_fops);
  2383. trace_create_file("enable", 0644, d_events,
  2384. tr, &ftrace_tr_enable_fops);
  2385. tr->event_dir = d_events;
  2386. return 0;
  2387. }
  2388. /**
  2389. * event_trace_add_tracer - add a instance of a trace_array to events
  2390. * @parent: The parent dentry to place the files/directories for events in
  2391. * @tr: The trace array associated with these events
  2392. *
  2393. * When a new instance is created, it needs to set up its events
  2394. * directory, as well as other files associated with events. It also
  2395. * creates the event hierachry in the @parent/events directory.
  2396. *
  2397. * Returns 0 on success.
  2398. */
  2399. int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
  2400. {
  2401. int ret;
  2402. mutex_lock(&event_mutex);
  2403. ret = create_event_toplevel_files(parent, tr);
  2404. if (ret)
  2405. goto out_unlock;
  2406. down_write(&trace_event_sem);
  2407. __trace_add_event_dirs(tr);
  2408. up_write(&trace_event_sem);
  2409. out_unlock:
  2410. mutex_unlock(&event_mutex);
  2411. return ret;
  2412. }
  2413. /*
  2414. * The top trace array already had its file descriptors created.
  2415. * Now the files themselves need to be created.
  2416. */
  2417. static __init int
  2418. early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
  2419. {
  2420. int ret;
  2421. mutex_lock(&event_mutex);
  2422. ret = create_event_toplevel_files(parent, tr);
  2423. if (ret)
  2424. goto out_unlock;
  2425. down_write(&trace_event_sem);
  2426. __trace_early_add_event_dirs(tr);
  2427. up_write(&trace_event_sem);
  2428. out_unlock:
  2429. mutex_unlock(&event_mutex);
  2430. return ret;
  2431. }
  2432. int event_trace_del_tracer(struct trace_array *tr)
  2433. {
  2434. mutex_lock(&event_mutex);
  2435. /* Disable any event triggers and associated soft-disabled events */
  2436. clear_event_triggers(tr);
  2437. /* Clear the pid list */
  2438. __ftrace_clear_event_pids(tr);
  2439. /* Disable any running events */
  2440. __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
  2441. /* Access to events are within rcu_read_lock_sched() */
  2442. synchronize_sched();
  2443. down_write(&trace_event_sem);
  2444. __trace_remove_event_dirs(tr);
  2445. tracefs_remove_recursive(tr->event_dir);
  2446. up_write(&trace_event_sem);
  2447. tr->event_dir = NULL;
  2448. mutex_unlock(&event_mutex);
  2449. return 0;
  2450. }
  2451. static __init int event_trace_memsetup(void)
  2452. {
  2453. field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
  2454. file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
  2455. return 0;
  2456. }
  2457. static __init void
  2458. early_enable_events(struct trace_array *tr, bool disable_first)
  2459. {
  2460. char *buf = bootup_event_buf;
  2461. char *token;
  2462. int ret;
  2463. while (true) {
  2464. token = strsep(&buf, ",");
  2465. if (!token)
  2466. break;
  2467. if (*token) {
  2468. /* Restarting syscalls requires that we stop them first */
  2469. if (disable_first)
  2470. ftrace_set_clr_event(tr, token, 0);
  2471. ret = ftrace_set_clr_event(tr, token, 1);
  2472. if (ret)
  2473. pr_warn("Failed to enable trace event: %s\n", token);
  2474. }
  2475. /* Put back the comma to allow this to be called again */
  2476. if (buf)
  2477. *(buf - 1) = ',';
  2478. }
  2479. }
  2480. static __init int event_trace_enable(void)
  2481. {
  2482. struct trace_array *tr = top_trace_array();
  2483. struct trace_event_call **iter, *call;
  2484. int ret;
  2485. if (!tr)
  2486. return -ENODEV;
  2487. for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
  2488. call = *iter;
  2489. ret = event_init(call);
  2490. if (!ret)
  2491. list_add(&call->list, &ftrace_events);
  2492. }
  2493. /*
  2494. * We need the top trace array to have a working set of trace
  2495. * points at early init, before the debug files and directories
  2496. * are created. Create the file entries now, and attach them
  2497. * to the actual file dentries later.
  2498. */
  2499. __trace_early_add_events(tr);
  2500. early_enable_events(tr, false);
  2501. trace_printk_start_comm();
  2502. register_event_cmds();
  2503. register_trigger_cmds();
  2504. return 0;
  2505. }
  2506. /*
  2507. * event_trace_enable() is called from trace_event_init() first to
  2508. * initialize events and perhaps start any events that are on the
  2509. * command line. Unfortunately, there are some events that will not
  2510. * start this early, like the system call tracepoints that need
  2511. * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
  2512. * is called before pid 1 starts, and this flag is never set, making
  2513. * the syscall tracepoint never get reached, but the event is enabled
  2514. * regardless (and not doing anything).
  2515. */
  2516. static __init int event_trace_enable_again(void)
  2517. {
  2518. struct trace_array *tr;
  2519. tr = top_trace_array();
  2520. if (!tr)
  2521. return -ENODEV;
  2522. early_enable_events(tr, true);
  2523. return 0;
  2524. }
  2525. early_initcall(event_trace_enable_again);
  2526. static __init int event_trace_init(void)
  2527. {
  2528. struct trace_array *tr;
  2529. struct dentry *d_tracer;
  2530. struct dentry *entry;
  2531. int ret;
  2532. tr = top_trace_array();
  2533. if (!tr)
  2534. return -ENODEV;
  2535. d_tracer = tracing_init_dentry();
  2536. if (IS_ERR(d_tracer))
  2537. return 0;
  2538. entry = tracefs_create_file("available_events", 0444, d_tracer,
  2539. tr, &ftrace_avail_fops);
  2540. if (!entry)
  2541. pr_warn("Could not create tracefs 'available_events' entry\n");
  2542. if (trace_define_generic_fields())
  2543. pr_warn("tracing: Failed to allocated generic fields");
  2544. if (trace_define_common_fields())
  2545. pr_warn("tracing: Failed to allocate common fields");
  2546. ret = early_event_add_tracer(d_tracer, tr);
  2547. if (ret)
  2548. return ret;
  2549. #ifdef CONFIG_MODULES
  2550. ret = register_module_notifier(&trace_module_nb);
  2551. if (ret)
  2552. pr_warn("Failed to register trace events module notifier\n");
  2553. #endif
  2554. return 0;
  2555. }
  2556. void __init trace_event_init(void)
  2557. {
  2558. event_trace_memsetup();
  2559. init_ftrace_syscalls();
  2560. event_trace_enable();
  2561. }
  2562. fs_initcall(event_trace_init);
  2563. #ifdef CONFIG_FTRACE_STARTUP_TEST
  2564. static DEFINE_SPINLOCK(test_spinlock);
  2565. static DEFINE_SPINLOCK(test_spinlock_irq);
  2566. static DEFINE_MUTEX(test_mutex);
  2567. static __init void test_work(struct work_struct *dummy)
  2568. {
  2569. spin_lock(&test_spinlock);
  2570. spin_lock_irq(&test_spinlock_irq);
  2571. udelay(1);
  2572. spin_unlock_irq(&test_spinlock_irq);
  2573. spin_unlock(&test_spinlock);
  2574. mutex_lock(&test_mutex);
  2575. msleep(1);
  2576. mutex_unlock(&test_mutex);
  2577. }
  2578. static __init int event_test_thread(void *unused)
  2579. {
  2580. void *test_malloc;
  2581. test_malloc = kmalloc(1234, GFP_KERNEL);
  2582. if (!test_malloc)
  2583. pr_info("failed to kmalloc\n");
  2584. schedule_on_each_cpu(test_work);
  2585. kfree(test_malloc);
  2586. set_current_state(TASK_INTERRUPTIBLE);
  2587. while (!kthread_should_stop()) {
  2588. schedule();
  2589. set_current_state(TASK_INTERRUPTIBLE);
  2590. }
  2591. __set_current_state(TASK_RUNNING);
  2592. return 0;
  2593. }
  2594. /*
  2595. * Do various things that may trigger events.
  2596. */
  2597. static __init void event_test_stuff(void)
  2598. {
  2599. struct task_struct *test_thread;
  2600. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  2601. msleep(1);
  2602. kthread_stop(test_thread);
  2603. }
  2604. /*
  2605. * For every trace event defined, we will test each trace point separately,
  2606. * and then by groups, and finally all trace points.
  2607. */
  2608. static __init void event_trace_self_tests(void)
  2609. {
  2610. struct trace_subsystem_dir *dir;
  2611. struct trace_event_file *file;
  2612. struct trace_event_call *call;
  2613. struct event_subsystem *system;
  2614. struct trace_array *tr;
  2615. int ret;
  2616. tr = top_trace_array();
  2617. if (!tr)
  2618. return;
  2619. pr_info("Running tests on trace events:\n");
  2620. list_for_each_entry(file, &tr->events, list) {
  2621. call = file->event_call;
  2622. /* Only test those that have a probe */
  2623. if (!call->class || !call->class->probe)
  2624. continue;
  2625. /*
  2626. * Testing syscall events here is pretty useless, but
  2627. * we still do it if configured. But this is time consuming.
  2628. * What we really need is a user thread to perform the
  2629. * syscalls as we test.
  2630. */
  2631. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  2632. if (call->class->system &&
  2633. strcmp(call->class->system, "syscalls") == 0)
  2634. continue;
  2635. #endif
  2636. pr_info("Testing event %s: ", trace_event_name(call));
  2637. /*
  2638. * If an event is already enabled, someone is using
  2639. * it and the self test should not be on.
  2640. */
  2641. if (file->flags & EVENT_FILE_FL_ENABLED) {
  2642. pr_warn("Enabled event during self test!\n");
  2643. WARN_ON_ONCE(1);
  2644. continue;
  2645. }
  2646. ftrace_event_enable_disable(file, 1);
  2647. event_test_stuff();
  2648. ftrace_event_enable_disable(file, 0);
  2649. pr_cont("OK\n");
  2650. }
  2651. /* Now test at the sub system level */
  2652. pr_info("Running tests on trace event systems:\n");
  2653. list_for_each_entry(dir, &tr->systems, list) {
  2654. system = dir->subsystem;
  2655. /* the ftrace system is special, skip it */
  2656. if (strcmp(system->name, "ftrace") == 0)
  2657. continue;
  2658. pr_info("Testing event system %s: ", system->name);
  2659. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
  2660. if (WARN_ON_ONCE(ret)) {
  2661. pr_warn("error enabling system %s\n",
  2662. system->name);
  2663. continue;
  2664. }
  2665. event_test_stuff();
  2666. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
  2667. if (WARN_ON_ONCE(ret)) {
  2668. pr_warn("error disabling system %s\n",
  2669. system->name);
  2670. continue;
  2671. }
  2672. pr_cont("OK\n");
  2673. }
  2674. /* Test with all events enabled */
  2675. pr_info("Running tests on all trace events:\n");
  2676. pr_info("Testing all events: ");
  2677. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
  2678. if (WARN_ON_ONCE(ret)) {
  2679. pr_warn("error enabling all events\n");
  2680. return;
  2681. }
  2682. event_test_stuff();
  2683. /* reset sysname */
  2684. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
  2685. if (WARN_ON_ONCE(ret)) {
  2686. pr_warn("error disabling all events\n");
  2687. return;
  2688. }
  2689. pr_cont("OK\n");
  2690. }
  2691. #ifdef CONFIG_FUNCTION_TRACER
  2692. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  2693. static struct trace_array *event_tr;
  2694. static void __init
  2695. function_test_events_call(unsigned long ip, unsigned long parent_ip,
  2696. struct ftrace_ops *op, struct pt_regs *pt_regs)
  2697. {
  2698. struct ring_buffer_event *event;
  2699. struct ring_buffer *buffer;
  2700. struct ftrace_entry *entry;
  2701. unsigned long flags;
  2702. long disabled;
  2703. int cpu;
  2704. int pc;
  2705. pc = preempt_count();
  2706. preempt_disable_notrace();
  2707. cpu = raw_smp_processor_id();
  2708. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  2709. if (disabled != 1)
  2710. goto out;
  2711. local_save_flags(flags);
  2712. event = trace_current_buffer_lock_reserve(&buffer,
  2713. TRACE_FN, sizeof(*entry),
  2714. flags, pc);
  2715. if (!event)
  2716. goto out;
  2717. entry = ring_buffer_event_data(event);
  2718. entry->ip = ip;
  2719. entry->parent_ip = parent_ip;
  2720. trace_buffer_unlock_commit(event_tr, buffer, event, flags, pc);
  2721. out:
  2722. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  2723. preempt_enable_notrace();
  2724. }
  2725. static struct ftrace_ops trace_ops __initdata =
  2726. {
  2727. .func = function_test_events_call,
  2728. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  2729. };
  2730. static __init void event_trace_self_test_with_function(void)
  2731. {
  2732. int ret;
  2733. event_tr = top_trace_array();
  2734. if (WARN_ON(!event_tr))
  2735. return;
  2736. ret = register_ftrace_function(&trace_ops);
  2737. if (WARN_ON(ret < 0)) {
  2738. pr_info("Failed to enable function tracer for event tests\n");
  2739. return;
  2740. }
  2741. pr_info("Running tests again, along with the function tracer\n");
  2742. event_trace_self_tests();
  2743. unregister_ftrace_function(&trace_ops);
  2744. }
  2745. #else
  2746. static __init void event_trace_self_test_with_function(void)
  2747. {
  2748. }
  2749. #endif
  2750. static __init int event_trace_self_tests_init(void)
  2751. {
  2752. if (!tracing_selftest_disabled) {
  2753. event_trace_self_tests();
  2754. event_trace_self_test_with_function();
  2755. }
  2756. return 0;
  2757. }
  2758. late_initcall(event_trace_self_tests_init);
  2759. #endif