events.rst 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. =============
  2. Event Tracing
  3. =============
  4. :Author: Theodore Ts'o
  5. :Updated: Li Zefan and Tom Zanussi
  6. 1. Introduction
  7. ===============
  8. Tracepoints (see Documentation/trace/tracepoints.rst) can be used
  9. without creating custom kernel modules to register probe functions
  10. using the event tracing infrastructure.
  11. Not all tracepoints can be traced using the event tracing system;
  12. the kernel developer must provide code snippets which define how the
  13. tracing information is saved into the tracing buffer, and how the
  14. tracing information should be printed.
  15. 2. Using Event Tracing
  16. ======================
  17. 2.1 Via the 'set_event' interface
  18. ---------------------------------
  19. The events which are available for tracing can be found in the file
  20. /sys/kernel/tracing/available_events.
  21. To enable a particular event, such as 'sched_wakeup', simply echo it
  22. to /sys/kernel/tracing/set_event. For example::
  23. # echo sched_wakeup >> /sys/kernel/tracing/set_event
  24. .. Note:: '>>' is necessary, otherwise it will firstly disable all the events.
  25. To disable an event, echo the event name to the set_event file prefixed
  26. with an exclamation point::
  27. # echo '!sched_wakeup' >> /sys/kernel/tracing/set_event
  28. To disable all events, echo an empty line to the set_event file::
  29. # echo > /sys/kernel/tracing/set_event
  30. To enable all events, echo ``*:*`` or ``*:`` to the set_event file::
  31. # echo *:* > /sys/kernel/tracing/set_event
  32. The events are organized into subsystems, such as ext4, irq, sched,
  33. etc., and a full event name looks like this: <subsystem>:<event>. The
  34. subsystem name is optional, but it is displayed in the available_events
  35. file. All of the events in a subsystem can be specified via the syntax
  36. ``<subsystem>:*``; for example, to enable all irq events, you can use the
  37. command::
  38. # echo 'irq:*' > /sys/kernel/tracing/set_event
  39. 2.2 Via the 'enable' toggle
  40. ---------------------------
  41. The events available are also listed in /sys/kernel/tracing/events/ hierarchy
  42. of directories.
  43. To enable event 'sched_wakeup'::
  44. # echo 1 > /sys/kernel/tracing/events/sched/sched_wakeup/enable
  45. To disable it::
  46. # echo 0 > /sys/kernel/tracing/events/sched/sched_wakeup/enable
  47. To enable all events in sched subsystem::
  48. # echo 1 > /sys/kernel/tracing/events/sched/enable
  49. To enable all events::
  50. # echo 1 > /sys/kernel/tracing/events/enable
  51. When reading one of these enable files, there are four results:
  52. - 0 - all events this file affects are disabled
  53. - 1 - all events this file affects are enabled
  54. - X - there is a mixture of events enabled and disabled
  55. - ? - this file does not affect any event
  56. 2.3 Boot option
  57. ---------------
  58. In order to facilitate early boot debugging, use boot option::
  59. trace_event=[event-list]
  60. event-list is a comma separated list of events. See section 2.1 for event
  61. format.
  62. 3. Defining an event-enabled tracepoint
  63. =======================================
  64. See The example provided in samples/trace_events
  65. 4. Event formats
  66. ================
  67. Each trace event has a 'format' file associated with it that contains
  68. a description of each field in a logged event. This information can
  69. be used to parse the binary trace stream, and is also the place to
  70. find the field names that can be used in event filters (see section 5).
  71. It also displays the format string that will be used to print the
  72. event in text mode, along with the event name and ID used for
  73. profiling.
  74. Every event has a set of ``common`` fields associated with it; these are
  75. the fields prefixed with ``common_``. The other fields vary between
  76. events and correspond to the fields defined in the TRACE_EVENT
  77. definition for that event.
  78. Each field in the format has the form::
  79. field:field-type field-name; offset:N; size:N;
  80. where offset is the offset of the field in the trace record and size
  81. is the size of the data item, in bytes.
  82. For example, here's the information displayed for the 'sched_wakeup'
  83. event::
  84. # cat /sys/kernel/tracing/events/sched/sched_wakeup/format
  85. name: sched_wakeup
  86. ID: 60
  87. format:
  88. field:unsigned short common_type; offset:0; size:2;
  89. field:unsigned char common_flags; offset:2; size:1;
  90. field:unsigned char common_preempt_count; offset:3; size:1;
  91. field:int common_pid; offset:4; size:4;
  92. field:int common_tgid; offset:8; size:4;
  93. field:char comm[TASK_COMM_LEN]; offset:12; size:16;
  94. field:pid_t pid; offset:28; size:4;
  95. field:int prio; offset:32; size:4;
  96. field:int success; offset:36; size:4;
  97. field:int cpu; offset:40; size:4;
  98. print fmt: "task %s:%d [%d] success=%d [%03d]", REC->comm, REC->pid,
  99. REC->prio, REC->success, REC->cpu
  100. This event contains 10 fields, the first 5 common and the remaining 5
  101. event-specific. All the fields for this event are numeric, except for
  102. 'comm' which is a string, a distinction important for event filtering.
  103. 5. Event filtering
  104. ==================
  105. Trace events can be filtered in the kernel by associating boolean
  106. 'filter expressions' with them. As soon as an event is logged into
  107. the trace buffer, its fields are checked against the filter expression
  108. associated with that event type. An event with field values that
  109. 'match' the filter will appear in the trace output, and an event whose
  110. values don't match will be discarded. An event with no filter
  111. associated with it matches everything, and is the default when no
  112. filter has been set for an event.
  113. 5.1 Expression syntax
  114. ---------------------
  115. A filter expression consists of one or more 'predicates' that can be
  116. combined using the logical operators '&&' and '||'. A predicate is
  117. simply a clause that compares the value of a field contained within a
  118. logged event with a constant value and returns either 0 or 1 depending
  119. on whether the field value matched (1) or didn't match (0)::
  120. field-name relational-operator value
  121. Parentheses can be used to provide arbitrary logical groupings and
  122. double-quotes can be used to prevent the shell from interpreting
  123. operators as shell metacharacters.
  124. The field-names available for use in filters can be found in the
  125. 'format' files for trace events (see section 4).
  126. The relational-operators depend on the type of the field being tested:
  127. The operators available for numeric fields are:
  128. ==, !=, <, <=, >, >=, &
  129. And for string fields they are:
  130. ==, !=, ~
  131. The glob (~) accepts a wild card character (\*,?) and character classes
  132. ([). For example::
  133. prev_comm ~ "*sh"
  134. prev_comm ~ "sh*"
  135. prev_comm ~ "*sh*"
  136. prev_comm ~ "ba*sh"
  137. If the field is a pointer that points into user space (for example
  138. "filename" from sys_enter_openat), then you have to append ".ustring" to the
  139. field name::
  140. filename.ustring ~ "password"
  141. As the kernel will have to know how to retrieve the memory that the pointer
  142. is at from user space.
  143. You can convert any long type to a function address and search by function name::
  144. call_site.function == security_prepare_creds
  145. The above will filter when the field "call_site" falls on the address within
  146. "security_prepare_creds". That is, it will compare the value of "call_site" and
  147. the filter will return true if it is greater than or equal to the start of
  148. the function "security_prepare_creds" and less than the end of that function.
  149. The ".function" postfix can only be attached to values of size long, and can only
  150. be compared with "==" or "!=".
  151. Cpumask fields or scalar fields that encode a CPU number can be filtered using
  152. a user-provided cpumask in cpulist format. The format is as follows::
  153. CPUS{$cpulist}
  154. Operators available to cpumask filtering are:
  155. & (intersection), ==, !=
  156. For example, this will filter events that have their .target_cpu field present
  157. in the given cpumask::
  158. target_cpu & CPUS{17-42}
  159. 5.2 Setting filters
  160. -------------------
  161. A filter for an individual event is set by writing a filter expression
  162. to the 'filter' file for the given event.
  163. For example::
  164. # cd /sys/kernel/tracing/events/sched/sched_wakeup
  165. # echo "common_preempt_count > 4" > filter
  166. A slightly more involved example::
  167. # cd /sys/kernel/tracing/events/signal/signal_generate
  168. # echo "((sig >= 10 && sig < 15) || sig == 17) && comm != bash" > filter
  169. If there is an error in the expression, you'll get an 'Invalid
  170. argument' error when setting it, and the erroneous string along with
  171. an error message can be seen by looking at the filter e.g.::
  172. # cd /sys/kernel/tracing/events/signal/signal_generate
  173. # echo "((sig >= 10 && sig < 15) || dsig == 17) && comm != bash" > filter
  174. -bash: echo: write error: Invalid argument
  175. # cat filter
  176. ((sig >= 10 && sig < 15) || dsig == 17) && comm != bash
  177. ^
  178. parse_error: Field not found
  179. Currently the caret ('^') for an error always appears at the beginning of
  180. the filter string; the error message should still be useful though
  181. even without more accurate position info.
  182. 5.2.1 Filter limitations
  183. ------------------------
  184. If a filter is placed on a string pointer ``(char *)`` that does not point
  185. to a string on the ring buffer, but instead points to kernel or user space
  186. memory, then, for safety reasons, at most 1024 bytes of the content is
  187. copied onto a temporary buffer to do the compare. If the copy of the memory
  188. faults (the pointer points to memory that should not be accessed), then the
  189. string compare will be treated as not matching.
  190. 5.3 Clearing filters
  191. --------------------
  192. To clear the filter for an event, write a '0' to the event's filter
  193. file.
  194. To clear the filters for all events in a subsystem, write a '0' to the
  195. subsystem's filter file.
  196. 5.4 Subsystem filters
  197. ---------------------
  198. For convenience, filters for every event in a subsystem can be set or
  199. cleared as a group by writing a filter expression into the filter file
  200. at the root of the subsystem. Note however, that if a filter for any
  201. event within the subsystem lacks a field specified in the subsystem
  202. filter, or if the filter can't be applied for any other reason, the
  203. filter for that event will retain its previous setting. This can
  204. result in an unintended mixture of filters which could lead to
  205. confusing (to the user who might think different filters are in
  206. effect) trace output. Only filters that reference just the common
  207. fields can be guaranteed to propagate successfully to all events.
  208. Here are a few subsystem filter examples that also illustrate the
  209. above points:
  210. Clear the filters on all events in the sched subsystem::
  211. # cd /sys/kernel/tracing/events/sched
  212. # echo 0 > filter
  213. # cat sched_switch/filter
  214. none
  215. # cat sched_wakeup/filter
  216. none
  217. Set a filter using only common fields for all events in the sched
  218. subsystem (all events end up with the same filter)::
  219. # cd /sys/kernel/tracing/events/sched
  220. # echo common_pid == 0 > filter
  221. # cat sched_switch/filter
  222. common_pid == 0
  223. # cat sched_wakeup/filter
  224. common_pid == 0
  225. Attempt to set a filter using a non-common field for all events in the
  226. sched subsystem (all events but those that have a prev_pid field retain
  227. their old filters)::
  228. # cd /sys/kernel/tracing/events/sched
  229. # echo prev_pid == 0 > filter
  230. # cat sched_switch/filter
  231. prev_pid == 0
  232. # cat sched_wakeup/filter
  233. common_pid == 0
  234. 5.5 PID filtering
  235. -----------------
  236. The set_event_pid file in the same directory as the top events directory
  237. exists, will filter all events from tracing any task that does not have the
  238. PID listed in the set_event_pid file.
  239. ::
  240. # cd /sys/kernel/tracing
  241. # echo $$ > set_event_pid
  242. # echo 1 > events/enable
  243. Will only trace events for the current task.
  244. To add more PIDs without losing the PIDs already included, use '>>'.
  245. ::
  246. # echo 123 244 1 >> set_event_pid
  247. 6. Event triggers
  248. =================
  249. Trace events can be made to conditionally invoke trigger 'commands'
  250. which can take various forms and are described in detail below;
  251. examples would be enabling or disabling other trace events or invoking
  252. a stack trace whenever the trace event is hit. Whenever a trace event
  253. with attached triggers is invoked, the set of trigger commands
  254. associated with that event is invoked. Any given trigger can
  255. additionally have an event filter of the same form as described in
  256. section 5 (Event filtering) associated with it - the command will only
  257. be invoked if the event being invoked passes the associated filter.
  258. If no filter is associated with the trigger, it always passes.
  259. Triggers are added to and removed from a particular event by writing
  260. trigger expressions to the 'trigger' file for the given event.
  261. A given event can have any number of triggers associated with it,
  262. subject to any restrictions that individual commands may have in that
  263. regard.
  264. Event triggers are implemented on top of "soft" mode, which means that
  265. whenever a trace event has one or more triggers associated with it,
  266. the event is activated even if it isn't actually enabled, but is
  267. disabled in a "soft" mode. That is, the tracepoint will be called,
  268. but just will not be traced, unless of course it's actually enabled.
  269. This scheme allows triggers to be invoked even for events that aren't
  270. enabled, and also allows the current event filter implementation to be
  271. used for conditionally invoking triggers.
  272. The syntax for event triggers is roughly based on the syntax for
  273. set_ftrace_filter 'ftrace filter commands' (see the 'Filter commands'
  274. section of Documentation/trace/ftrace.rst), but there are major
  275. differences and the implementation isn't currently tied to it in any
  276. way, so beware about making generalizations between the two.
  277. .. Note::
  278. Writing into trace_marker (See Documentation/trace/ftrace.rst)
  279. can also enable triggers that are written into
  280. /sys/kernel/tracing/events/ftrace/print/trigger
  281. 6.1 Expression syntax
  282. ---------------------
  283. Triggers are added by echoing the command to the 'trigger' file::
  284. # echo 'command[:count] [if filter]' > trigger
  285. Triggers are removed by echoing the same command but starting with '!'
  286. to the 'trigger' file::
  287. # echo '!command[:count] [if filter]' > trigger
  288. The [if filter] part isn't used in matching commands when removing, so
  289. leaving that off in a '!' command will accomplish the same thing as
  290. having it in.
  291. The filter syntax is the same as that described in the 'Event
  292. filtering' section above.
  293. For ease of use, writing to the trigger file using '>' currently just
  294. adds or removes a single trigger and there's no explicit '>>' support
  295. ('>' actually behaves like '>>') or truncation support to remove all
  296. triggers (you have to use '!' for each one added.)
  297. 6.2 Supported trigger commands
  298. ------------------------------
  299. The following commands are supported:
  300. - enable_event/disable_event
  301. These commands can enable or disable another trace event whenever
  302. the triggering event is hit. When these commands are registered,
  303. the other trace event is activated, but disabled in a "soft" mode.
  304. That is, the tracepoint will be called, but just will not be traced.
  305. The event tracepoint stays in this mode as long as there's a trigger
  306. in effect that can trigger it.
  307. For example, the following trigger causes kmalloc events to be
  308. traced when a read system call is entered, and the :1 at the end
  309. specifies that this enablement happens only once::
  310. # echo 'enable_event:kmem:kmalloc:1' > \
  311. /sys/kernel/tracing/events/syscalls/sys_enter_read/trigger
  312. The following trigger causes kmalloc events to stop being traced
  313. when a read system call exits. This disablement happens on every
  314. read system call exit::
  315. # echo 'disable_event:kmem:kmalloc' > \
  316. /sys/kernel/tracing/events/syscalls/sys_exit_read/trigger
  317. The format is::
  318. enable_event:<system>:<event>[:count]
  319. disable_event:<system>:<event>[:count]
  320. To remove the above commands::
  321. # echo '!enable_event:kmem:kmalloc:1' > \
  322. /sys/kernel/tracing/events/syscalls/sys_enter_read/trigger
  323. # echo '!disable_event:kmem:kmalloc' > \
  324. /sys/kernel/tracing/events/syscalls/sys_exit_read/trigger
  325. Note that there can be any number of enable/disable_event triggers
  326. per triggering event, but there can only be one trigger per
  327. triggered event. e.g. sys_enter_read can have triggers enabling both
  328. kmem:kmalloc and sched:sched_switch, but can't have two kmem:kmalloc
  329. versions such as kmem:kmalloc and kmem:kmalloc:1 or 'kmem:kmalloc if
  330. bytes_req == 256' and 'kmem:kmalloc if bytes_alloc == 256' (they
  331. could be combined into a single filter on kmem:kmalloc though).
  332. - stacktrace
  333. This command dumps a stacktrace in the trace buffer whenever the
  334. triggering event occurs.
  335. For example, the following trigger dumps a stacktrace every time the
  336. kmalloc tracepoint is hit::
  337. # echo 'stacktrace' > \
  338. /sys/kernel/tracing/events/kmem/kmalloc/trigger
  339. The following trigger dumps a stacktrace the first 5 times a kmalloc
  340. request happens with a size >= 64K::
  341. # echo 'stacktrace:5 if bytes_req >= 65536' > \
  342. /sys/kernel/tracing/events/kmem/kmalloc/trigger
  343. The format is::
  344. stacktrace[:count]
  345. To remove the above commands::
  346. # echo '!stacktrace' > \
  347. /sys/kernel/tracing/events/kmem/kmalloc/trigger
  348. # echo '!stacktrace:5 if bytes_req >= 65536' > \
  349. /sys/kernel/tracing/events/kmem/kmalloc/trigger
  350. The latter can also be removed more simply by the following (without
  351. the filter)::
  352. # echo '!stacktrace:5' > \
  353. /sys/kernel/tracing/events/kmem/kmalloc/trigger
  354. Note that there can be only one stacktrace trigger per triggering
  355. event.
  356. - snapshot
  357. This command causes a snapshot to be triggered whenever the
  358. triggering event occurs.
  359. The following command creates a snapshot every time a block request
  360. queue is unplugged with a depth > 1. If you were tracing a set of
  361. events or functions at the time, the snapshot trace buffer would
  362. capture those events when the trigger event occurred::
  363. # echo 'snapshot if nr_rq > 1' > \
  364. /sys/kernel/tracing/events/block/block_unplug/trigger
  365. To only snapshot once::
  366. # echo 'snapshot:1 if nr_rq > 1' > \
  367. /sys/kernel/tracing/events/block/block_unplug/trigger
  368. To remove the above commands::
  369. # echo '!snapshot if nr_rq > 1' > \
  370. /sys/kernel/tracing/events/block/block_unplug/trigger
  371. # echo '!snapshot:1 if nr_rq > 1' > \
  372. /sys/kernel/tracing/events/block/block_unplug/trigger
  373. Note that there can be only one snapshot trigger per triggering
  374. event.
  375. - traceon/traceoff
  376. These commands turn tracing on and off when the specified events are
  377. hit. The parameter determines how many times the tracing system is
  378. turned on and off. If unspecified, there is no limit.
  379. The following command turns tracing off the first time a block
  380. request queue is unplugged with a depth > 1. If you were tracing a
  381. set of events or functions at the time, you could then examine the
  382. trace buffer to see the sequence of events that led up to the
  383. trigger event::
  384. # echo 'traceoff:1 if nr_rq > 1' > \
  385. /sys/kernel/tracing/events/block/block_unplug/trigger
  386. To always disable tracing when nr_rq > 1::
  387. # echo 'traceoff if nr_rq > 1' > \
  388. /sys/kernel/tracing/events/block/block_unplug/trigger
  389. To remove the above commands::
  390. # echo '!traceoff:1 if nr_rq > 1' > \
  391. /sys/kernel/tracing/events/block/block_unplug/trigger
  392. # echo '!traceoff if nr_rq > 1' > \
  393. /sys/kernel/tracing/events/block/block_unplug/trigger
  394. Note that there can be only one traceon or traceoff trigger per
  395. triggering event.
  396. - hist
  397. This command aggregates event hits into a hash table keyed on one or
  398. more trace event format fields (or stacktrace) and a set of running
  399. totals derived from one or more trace event format fields and/or
  400. event counts (hitcount).
  401. See Documentation/trace/histogram.rst for details and examples.
  402. 7. In-kernel trace event API
  403. ============================
  404. In most cases, the command-line interface to trace events is more than
  405. sufficient. Sometimes, however, applications might find the need for
  406. more complex relationships than can be expressed through a simple
  407. series of linked command-line expressions, or putting together sets of
  408. commands may be simply too cumbersome. An example might be an
  409. application that needs to 'listen' to the trace stream in order to
  410. maintain an in-kernel state machine detecting, for instance, when an
  411. illegal kernel state occurs in the scheduler.
  412. The trace event subsystem provides an in-kernel API allowing modules
  413. or other kernel code to generate user-defined 'synthetic' events at
  414. will, which can be used to either augment the existing trace stream
  415. and/or signal that a particular important state has occurred.
  416. A similar in-kernel API is also available for creating kprobe and
  417. kretprobe events.
  418. Both the synthetic event and k/ret/probe event APIs are built on top
  419. of a lower-level "dynevent_cmd" event command API, which is also
  420. available for more specialized applications, or as the basis of other
  421. higher-level trace event APIs.
  422. The API provided for these purposes is describe below and allows the
  423. following:
  424. - dynamically creating synthetic event definitions
  425. - dynamically creating kprobe and kretprobe event definitions
  426. - tracing synthetic events from in-kernel code
  427. - the low-level "dynevent_cmd" API
  428. 7.1 Dyamically creating synthetic event definitions
  429. ---------------------------------------------------
  430. There are a couple ways to create a new synthetic event from a kernel
  431. module or other kernel code.
  432. The first creates the event in one step, using synth_event_create().
  433. In this method, the name of the event to create and an array defining
  434. the fields is supplied to synth_event_create(). If successful, a
  435. synthetic event with that name and fields will exist following that
  436. call. For example, to create a new "schedtest" synthetic event::
  437. ret = synth_event_create("schedtest", sched_fields,
  438. ARRAY_SIZE(sched_fields), THIS_MODULE);
  439. The sched_fields param in this example points to an array of struct
  440. synth_field_desc, each of which describes an event field by type and
  441. name::
  442. static struct synth_field_desc sched_fields[] = {
  443. { .type = "pid_t", .name = "next_pid_field" },
  444. { .type = "char[16]", .name = "next_comm_field" },
  445. { .type = "u64", .name = "ts_ns" },
  446. { .type = "u64", .name = "ts_ms" },
  447. { .type = "unsigned int", .name = "cpu" },
  448. { .type = "char[64]", .name = "my_string_field" },
  449. { .type = "int", .name = "my_int_field" },
  450. };
  451. See synth_field_size() for available types.
  452. If field_name contains [n], the field is considered to be a static array.
  453. If field_names contains[] (no subscript), the field is considered to
  454. be a dynamic array, which will only take as much space in the event as
  455. is required to hold the array.
  456. Because space for an event is reserved before assigning field values
  457. to the event, using dynamic arrays implies that the piecewise
  458. in-kernel API described below can't be used with dynamic arrays. The
  459. other non-piecewise in-kernel APIs can, however, be used with dynamic
  460. arrays.
  461. If the event is created from within a module, a pointer to the module
  462. must be passed to synth_event_create(). This will ensure that the
  463. trace buffer won't contain unreadable events when the module is
  464. removed.
  465. At this point, the event object is ready to be used for generating new
  466. events.
  467. In the second method, the event is created in several steps. This
  468. allows events to be created dynamically and without the need to create
  469. and populate an array of fields beforehand.
  470. To use this method, an empty or partially empty synthetic event should
  471. first be created using synth_event_gen_cmd_start() or
  472. synth_event_gen_cmd_array_start(). For synth_event_gen_cmd_start(),
  473. the name of the event along with one or more pairs of args each pair
  474. representing a 'type field_name;' field specification should be
  475. supplied. For synth_event_gen_cmd_array_start(), the name of the
  476. event along with an array of struct synth_field_desc should be
  477. supplied. Before calling synth_event_gen_cmd_start() or
  478. synth_event_gen_cmd_array_start(), the user should create and
  479. initialize a dynevent_cmd object using synth_event_cmd_init().
  480. For example, to create a new "schedtest" synthetic event with two
  481. fields::
  482. struct dynevent_cmd cmd;
  483. char *buf;
  484. /* Create a buffer to hold the generated command */
  485. buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
  486. /* Before generating the command, initialize the cmd object */
  487. synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
  488. ret = synth_event_gen_cmd_start(&cmd, "schedtest", THIS_MODULE,
  489. "pid_t", "next_pid_field",
  490. "u64", "ts_ns");
  491. Alternatively, using an array of struct synth_field_desc fields
  492. containing the same information::
  493. ret = synth_event_gen_cmd_array_start(&cmd, "schedtest", THIS_MODULE,
  494. fields, n_fields);
  495. Once the synthetic event object has been created, it can then be
  496. populated with more fields. Fields are added one by one using
  497. synth_event_add_field(), supplying the dynevent_cmd object, a field
  498. type, and a field name. For example, to add a new int field named
  499. "intfield", the following call should be made::
  500. ret = synth_event_add_field(&cmd, "int", "intfield");
  501. See synth_field_size() for available types. If field_name contains [n]
  502. the field is considered to be an array.
  503. A group of fields can also be added all at once using an array of
  504. synth_field_desc with add_synth_fields(). For example, this would add
  505. just the first four sched_fields::
  506. ret = synth_event_add_fields(&cmd, sched_fields, 4);
  507. If you already have a string of the form 'type field_name',
  508. synth_event_add_field_str() can be used to add it as-is; it will
  509. also automatically append a ';' to the string.
  510. Once all the fields have been added, the event should be finalized and
  511. registered by calling the synth_event_gen_cmd_end() function::
  512. ret = synth_event_gen_cmd_end(&cmd);
  513. At this point, the event object is ready to be used for tracing new
  514. events.
  515. 7.2 Tracing synthetic events from in-kernel code
  516. ------------------------------------------------
  517. To trace a synthetic event, there are several options. The first
  518. option is to trace the event in one call, using synth_event_trace()
  519. with a variable number of values, or synth_event_trace_array() with an
  520. array of values to be set. A second option can be used to avoid the
  521. need for a pre-formed array of values or list of arguments, via
  522. synth_event_trace_start() and synth_event_trace_end() along with
  523. synth_event_add_next_val() or synth_event_add_val() to add the values
  524. piecewise.
  525. 7.2.1 Tracing a synthetic event all at once
  526. -------------------------------------------
  527. To trace a synthetic event all at once, the synth_event_trace() or
  528. synth_event_trace_array() functions can be used.
  529. The synth_event_trace() function is passed the trace_event_file
  530. representing the synthetic event (which can be retrieved using
  531. trace_get_event_file() using the synthetic event name, "synthetic" as
  532. the system name, and the trace instance name (NULL if using the global
  533. trace array)), along with an variable number of u64 args, one for each
  534. synthetic event field, and the number of values being passed.
  535. So, to trace an event corresponding to the synthetic event definition
  536. above, code like the following could be used::
  537. ret = synth_event_trace(create_synth_test, 7, /* number of values */
  538. 444, /* next_pid_field */
  539. (u64)"clackers", /* next_comm_field */
  540. 1000000, /* ts_ns */
  541. 1000, /* ts_ms */
  542. smp_processor_id(),/* cpu */
  543. (u64)"Thneed", /* my_string_field */
  544. 999); /* my_int_field */
  545. All vals should be cast to u64, and string vals are just pointers to
  546. strings, cast to u64. Strings will be copied into space reserved in
  547. the event for the string, using these pointers.
  548. Alternatively, the synth_event_trace_array() function can be used to
  549. accomplish the same thing. It is passed the trace_event_file
  550. representing the synthetic event (which can be retrieved using
  551. trace_get_event_file() using the synthetic event name, "synthetic" as
  552. the system name, and the trace instance name (NULL if using the global
  553. trace array)), along with an array of u64, one for each synthetic
  554. event field.
  555. To trace an event corresponding to the synthetic event definition
  556. above, code like the following could be used::
  557. u64 vals[7];
  558. vals[0] = 777; /* next_pid_field */
  559. vals[1] = (u64)"tiddlywinks"; /* next_comm_field */
  560. vals[2] = 1000000; /* ts_ns */
  561. vals[3] = 1000; /* ts_ms */
  562. vals[4] = smp_processor_id(); /* cpu */
  563. vals[5] = (u64)"thneed"; /* my_string_field */
  564. vals[6] = 398; /* my_int_field */
  565. The 'vals' array is just an array of u64, the number of which must
  566. match the number of field in the synthetic event, and which must be in
  567. the same order as the synthetic event fields.
  568. All vals should be cast to u64, and string vals are just pointers to
  569. strings, cast to u64. Strings will be copied into space reserved in
  570. the event for the string, using these pointers.
  571. In order to trace a synthetic event, a pointer to the trace event file
  572. is needed. The trace_get_event_file() function can be used to get
  573. it - it will find the file in the given trace instance (in this case
  574. NULL since the top trace array is being used) while at the same time
  575. preventing the instance containing it from going away::
  576. schedtest_event_file = trace_get_event_file(NULL, "synthetic",
  577. "schedtest");
  578. Before tracing the event, it should be enabled in some way, otherwise
  579. the synthetic event won't actually show up in the trace buffer.
  580. To enable a synthetic event from the kernel, trace_array_set_clr_event()
  581. can be used (which is not specific to synthetic events, so does need
  582. the "synthetic" system name to be specified explicitly).
  583. To enable the event, pass 'true' to it::
  584. trace_array_set_clr_event(schedtest_event_file->tr,
  585. "synthetic", "schedtest", true);
  586. To disable it pass false::
  587. trace_array_set_clr_event(schedtest_event_file->tr,
  588. "synthetic", "schedtest", false);
  589. Finally, synth_event_trace_array() can be used to actually trace the
  590. event, which should be visible in the trace buffer afterwards::
  591. ret = synth_event_trace_array(schedtest_event_file, vals,
  592. ARRAY_SIZE(vals));
  593. To remove the synthetic event, the event should be disabled, and the
  594. trace instance should be 'put' back using trace_put_event_file()::
  595. trace_array_set_clr_event(schedtest_event_file->tr,
  596. "synthetic", "schedtest", false);
  597. trace_put_event_file(schedtest_event_file);
  598. If those have been successful, synth_event_delete() can be called to
  599. remove the event::
  600. ret = synth_event_delete("schedtest");
  601. 7.2.2 Tracing a synthetic event piecewise
  602. -----------------------------------------
  603. To trace a synthetic using the piecewise method described above, the
  604. synth_event_trace_start() function is used to 'open' the synthetic
  605. event trace::
  606. struct synth_event_trace_state trace_state;
  607. ret = synth_event_trace_start(schedtest_event_file, &trace_state);
  608. It's passed the trace_event_file representing the synthetic event
  609. using the same methods as described above, along with a pointer to a
  610. struct synth_event_trace_state object, which will be zeroed before use and
  611. used to maintain state between this and following calls.
  612. Once the event has been opened, which means space for it has been
  613. reserved in the trace buffer, the individual fields can be set. There
  614. are two ways to do that, either one after another for each field in
  615. the event, which requires no lookups, or by name, which does. The
  616. tradeoff is flexibility in doing the assignments vs the cost of a
  617. lookup per field.
  618. To assign the values one after the other without lookups,
  619. synth_event_add_next_val() should be used. Each call is passed the
  620. same synth_event_trace_state object used in the synth_event_trace_start(),
  621. along with the value to set the next field in the event. After each
  622. field is set, the 'cursor' points to the next field, which will be set
  623. by the subsequent call, continuing until all the fields have been set
  624. in order. The same sequence of calls as in the above examples using
  625. this method would be (without error-handling code)::
  626. /* next_pid_field */
  627. ret = synth_event_add_next_val(777, &trace_state);
  628. /* next_comm_field */
  629. ret = synth_event_add_next_val((u64)"slinky", &trace_state);
  630. /* ts_ns */
  631. ret = synth_event_add_next_val(1000000, &trace_state);
  632. /* ts_ms */
  633. ret = synth_event_add_next_val(1000, &trace_state);
  634. /* cpu */
  635. ret = synth_event_add_next_val(smp_processor_id(), &trace_state);
  636. /* my_string_field */
  637. ret = synth_event_add_next_val((u64)"thneed_2.01", &trace_state);
  638. /* my_int_field */
  639. ret = synth_event_add_next_val(395, &trace_state);
  640. To assign the values in any order, synth_event_add_val() should be
  641. used. Each call is passed the same synth_event_trace_state object used in
  642. the synth_event_trace_start(), along with the field name of the field
  643. to set and the value to set it to. The same sequence of calls as in
  644. the above examples using this method would be (without error-handling
  645. code)::
  646. ret = synth_event_add_val("next_pid_field", 777, &trace_state);
  647. ret = synth_event_add_val("next_comm_field", (u64)"silly putty",
  648. &trace_state);
  649. ret = synth_event_add_val("ts_ns", 1000000, &trace_state);
  650. ret = synth_event_add_val("ts_ms", 1000, &trace_state);
  651. ret = synth_event_add_val("cpu", smp_processor_id(), &trace_state);
  652. ret = synth_event_add_val("my_string_field", (u64)"thneed_9",
  653. &trace_state);
  654. ret = synth_event_add_val("my_int_field", 3999, &trace_state);
  655. Note that synth_event_add_next_val() and synth_event_add_val() are
  656. incompatible if used within the same trace of an event - either one
  657. can be used but not both at the same time.
  658. Finally, the event won't be actually traced until it's 'closed',
  659. which is done using synth_event_trace_end(), which takes only the
  660. struct synth_event_trace_state object used in the previous calls::
  661. ret = synth_event_trace_end(&trace_state);
  662. Note that synth_event_trace_end() must be called at the end regardless
  663. of whether any of the add calls failed (say due to a bad field name
  664. being passed in).
  665. 7.3 Dyamically creating kprobe and kretprobe event definitions
  666. --------------------------------------------------------------
  667. To create a kprobe or kretprobe trace event from kernel code, the
  668. kprobe_event_gen_cmd_start() or kretprobe_event_gen_cmd_start()
  669. functions can be used.
  670. To create a kprobe event, an empty or partially empty kprobe event
  671. should first be created using kprobe_event_gen_cmd_start(). The name
  672. of the event and the probe location should be specified along with one
  673. or args each representing a probe field should be supplied to this
  674. function. Before calling kprobe_event_gen_cmd_start(), the user
  675. should create and initialize a dynevent_cmd object using
  676. kprobe_event_cmd_init().
  677. For example, to create a new "schedtest" kprobe event with two fields::
  678. struct dynevent_cmd cmd;
  679. char *buf;
  680. /* Create a buffer to hold the generated command */
  681. buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
  682. /* Before generating the command, initialize the cmd object */
  683. kprobe_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
  684. /*
  685. * Define the gen_kprobe_test event with the first 2 kprobe
  686. * fields.
  687. */
  688. ret = kprobe_event_gen_cmd_start(&cmd, "gen_kprobe_test", "do_sys_open",
  689. "dfd=%ax", "filename=%dx");
  690. Once the kprobe event object has been created, it can then be
  691. populated with more fields. Fields can be added using
  692. kprobe_event_add_fields(), supplying the dynevent_cmd object along
  693. with a variable arg list of probe fields. For example, to add a
  694. couple additional fields, the following call could be made::
  695. ret = kprobe_event_add_fields(&cmd, "flags=%cx", "mode=+4($stack)");
  696. Once all the fields have been added, the event should be finalized and
  697. registered by calling the kprobe_event_gen_cmd_end() or
  698. kretprobe_event_gen_cmd_end() functions, depending on whether a kprobe
  699. or kretprobe command was started::
  700. ret = kprobe_event_gen_cmd_end(&cmd);
  701. or::
  702. ret = kretprobe_event_gen_cmd_end(&cmd);
  703. At this point, the event object is ready to be used for tracing new
  704. events.
  705. Similarly, a kretprobe event can be created using
  706. kretprobe_event_gen_cmd_start() with a probe name and location and
  707. additional params such as $retval::
  708. ret = kretprobe_event_gen_cmd_start(&cmd, "gen_kretprobe_test",
  709. "do_sys_open", "$retval");
  710. Similar to the synthetic event case, code like the following can be
  711. used to enable the newly created kprobe event::
  712. gen_kprobe_test = trace_get_event_file(NULL, "kprobes", "gen_kprobe_test");
  713. ret = trace_array_set_clr_event(gen_kprobe_test->tr,
  714. "kprobes", "gen_kprobe_test", true);
  715. Finally, also similar to synthetic events, the following code can be
  716. used to give the kprobe event file back and delete the event::
  717. trace_put_event_file(gen_kprobe_test);
  718. ret = kprobe_event_delete("gen_kprobe_test");
  719. 7.4 The "dynevent_cmd" low-level API
  720. ------------------------------------
  721. Both the in-kernel synthetic event and kprobe interfaces are built on
  722. top of a lower-level "dynevent_cmd" interface. This interface is
  723. meant to provide the basis for higher-level interfaces such as the
  724. synthetic and kprobe interfaces, which can be used as examples.
  725. The basic idea is simple and amounts to providing a general-purpose
  726. layer that can be used to generate trace event commands. The
  727. generated command strings can then be passed to the command-parsing
  728. and event creation code that already exists in the trace event
  729. subsystem for creating the corresponding trace events.
  730. In a nutshell, the way it works is that the higher-level interface
  731. code creates a struct dynevent_cmd object, then uses a couple
  732. functions, dynevent_arg_add() and dynevent_arg_pair_add() to build up
  733. a command string, which finally causes the command to be executed
  734. using the dynevent_create() function. The details of the interface
  735. are described below.
  736. The first step in building a new command string is to create and
  737. initialize an instance of a dynevent_cmd. Here, for instance, we
  738. create a dynevent_cmd on the stack and initialize it::
  739. struct dynevent_cmd cmd;
  740. char *buf;
  741. int ret;
  742. buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
  743. dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_FOO,
  744. foo_event_run_command);
  745. The dynevent_cmd initialization needs to be given a user-specified
  746. buffer and the length of the buffer (MAX_DYNEVENT_CMD_LEN can be used
  747. for this purpose - at 2k it's generally too big to be comfortably put
  748. on the stack, so is dynamically allocated), a dynevent type id, which
  749. is meant to be used to check that further API calls are for the
  750. correct command type, and a pointer to an event-specific run_command()
  751. callback that will be called to actually execute the event-specific
  752. command function.
  753. Once that's done, the command string can by built up by successive
  754. calls to argument-adding functions.
  755. To add a single argument, define and initialize a struct dynevent_arg
  756. or struct dynevent_arg_pair object. Here's an example of the simplest
  757. possible arg addition, which is simply to append the given string as
  758. a whitespace-separated argument to the command::
  759. struct dynevent_arg arg;
  760. dynevent_arg_init(&arg, NULL, 0);
  761. arg.str = name;
  762. ret = dynevent_arg_add(cmd, &arg);
  763. The arg object is first initialized using dynevent_arg_init() and in
  764. this case the parameters are NULL or 0, which means there's no
  765. optional sanity-checking function or separator appended to the end of
  766. the arg.
  767. Here's another more complicated example using an 'arg pair', which is
  768. used to create an argument that consists of a couple components added
  769. together as a unit, for example, a 'type field_name;' arg or a simple
  770. expression arg e.g. 'flags=%cx'::
  771. struct dynevent_arg_pair arg_pair;
  772. dynevent_arg_pair_init(&arg_pair, dynevent_foo_check_arg_fn, 0, ';');
  773. arg_pair.lhs = type;
  774. arg_pair.rhs = name;
  775. ret = dynevent_arg_pair_add(cmd, &arg_pair);
  776. Again, the arg_pair is first initialized, in this case with a callback
  777. function used to check the sanity of the args (for example, that
  778. neither part of the pair is NULL), along with a character to be used
  779. to add an operator between the pair (here none) and a separator to be
  780. appended onto the end of the arg pair (here ';').
  781. There's also a dynevent_str_add() function that can be used to simply
  782. add a string as-is, with no spaces, delimiters, or arg check.
  783. Any number of dynevent_*_add() calls can be made to build up the string
  784. (until its length surpasses cmd->maxlen). When all the arguments have
  785. been added and the command string is complete, the only thing left to
  786. do is run the command, which happens by simply calling
  787. dynevent_create()::
  788. ret = dynevent_create(&cmd);
  789. At that point, if the return value is 0, the dynamic event has been
  790. created and is ready to use.
  791. See the dynevent_cmd function definitions themselves for the details
  792. of the API.