trace_events.c 78 KB

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