trace_events.c 78 KB

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