trace_events.c 79 KB

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