events.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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/debug/tracing/available_events.
  21. To enable a particular event, such as 'sched_wakeup', simply echo it
  22. to /sys/kernel/debug/tracing/set_event. For example::
  23. # echo sched_wakeup >> /sys/kernel/debug/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/debug/tracing/set_event
  28. To disable all events, echo an empty line to the set_event file::
  29. # echo > /sys/kernel/debug/tracing/set_event
  30. To enable all events, echo ``*:*`` or ``*:`` to the set_event file::
  31. # echo *:* > /sys/kernel/debug/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/debug/tracing/set_event
  39. 2.2 Via the 'enable' toggle
  40. ---------------------------
  41. The events available are also listed in /sys/kernel/debug/tracing/events/ hierarchy
  42. of directories.
  43. To enable event 'sched_wakeup'::
  44. # echo 1 > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
  45. To disable it::
  46. # echo 0 > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
  47. To enable all events in sched subsystem::
  48. # echo 1 > /sys/kernel/debug/tracing/events/sched/enable
  49. To enable all events::
  50. # echo 1 > /sys/kernel/debug/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/debug/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. 5.2 Setting filters
  138. -------------------
  139. A filter for an individual event is set by writing a filter expression
  140. to the 'filter' file for the given event.
  141. For example::
  142. # cd /sys/kernel/debug/tracing/events/sched/sched_wakeup
  143. # echo "common_preempt_count > 4" > filter
  144. A slightly more involved example::
  145. # cd /sys/kernel/debug/tracing/events/signal/signal_generate
  146. # echo "((sig >= 10 && sig < 15) || sig == 17) && comm != bash" > filter
  147. If there is an error in the expression, you'll get an 'Invalid
  148. argument' error when setting it, and the erroneous string along with
  149. an error message can be seen by looking at the filter e.g.::
  150. # cd /sys/kernel/debug/tracing/events/signal/signal_generate
  151. # echo "((sig >= 10 && sig < 15) || dsig == 17) && comm != bash" > filter
  152. -bash: echo: write error: Invalid argument
  153. # cat filter
  154. ((sig >= 10 && sig < 15) || dsig == 17) && comm != bash
  155. ^
  156. parse_error: Field not found
  157. Currently the caret ('^') for an error always appears at the beginning of
  158. the filter string; the error message should still be useful though
  159. even without more accurate position info.
  160. 5.3 Clearing filters
  161. --------------------
  162. To clear the filter for an event, write a '0' to the event's filter
  163. file.
  164. To clear the filters for all events in a subsystem, write a '0' to the
  165. subsystem's filter file.
  166. 5.3 Subsystem filters
  167. ---------------------
  168. For convenience, filters for every event in a subsystem can be set or
  169. cleared as a group by writing a filter expression into the filter file
  170. at the root of the subsystem. Note however, that if a filter for any
  171. event within the subsystem lacks a field specified in the subsystem
  172. filter, or if the filter can't be applied for any other reason, the
  173. filter for that event will retain its previous setting. This can
  174. result in an unintended mixture of filters which could lead to
  175. confusing (to the user who might think different filters are in
  176. effect) trace output. Only filters that reference just the common
  177. fields can be guaranteed to propagate successfully to all events.
  178. Here are a few subsystem filter examples that also illustrate the
  179. above points:
  180. Clear the filters on all events in the sched subsystem::
  181. # cd /sys/kernel/debug/tracing/events/sched
  182. # echo 0 > filter
  183. # cat sched_switch/filter
  184. none
  185. # cat sched_wakeup/filter
  186. none
  187. Set a filter using only common fields for all events in the sched
  188. subsystem (all events end up with the same filter)::
  189. # cd /sys/kernel/debug/tracing/events/sched
  190. # echo common_pid == 0 > filter
  191. # cat sched_switch/filter
  192. common_pid == 0
  193. # cat sched_wakeup/filter
  194. common_pid == 0
  195. Attempt to set a filter using a non-common field for all events in the
  196. sched subsystem (all events but those that have a prev_pid field retain
  197. their old filters)::
  198. # cd /sys/kernel/debug/tracing/events/sched
  199. # echo prev_pid == 0 > filter
  200. # cat sched_switch/filter
  201. prev_pid == 0
  202. # cat sched_wakeup/filter
  203. common_pid == 0
  204. 5.4 PID filtering
  205. -----------------
  206. The set_event_pid file in the same directory as the top events directory
  207. exists, will filter all events from tracing any task that does not have the
  208. PID listed in the set_event_pid file.
  209. ::
  210. # cd /sys/kernel/debug/tracing
  211. # echo $$ > set_event_pid
  212. # echo 1 > events/enable
  213. Will only trace events for the current task.
  214. To add more PIDs without losing the PIDs already included, use '>>'.
  215. ::
  216. # echo 123 244 1 >> set_event_pid
  217. 6. Event triggers
  218. =================
  219. Trace events can be made to conditionally invoke trigger 'commands'
  220. which can take various forms and are described in detail below;
  221. examples would be enabling or disabling other trace events or invoking
  222. a stack trace whenever the trace event is hit. Whenever a trace event
  223. with attached triggers is invoked, the set of trigger commands
  224. associated with that event is invoked. Any given trigger can
  225. additionally have an event filter of the same form as described in
  226. section 5 (Event filtering) associated with it - the command will only
  227. be invoked if the event being invoked passes the associated filter.
  228. If no filter is associated with the trigger, it always passes.
  229. Triggers are added to and removed from a particular event by writing
  230. trigger expressions to the 'trigger' file for the given event.
  231. A given event can have any number of triggers associated with it,
  232. subject to any restrictions that individual commands may have in that
  233. regard.
  234. Event triggers are implemented on top of "soft" mode, which means that
  235. whenever a trace event has one or more triggers associated with it,
  236. the event is activated even if it isn't actually enabled, but is
  237. disabled in a "soft" mode. That is, the tracepoint will be called,
  238. but just will not be traced, unless of course it's actually enabled.
  239. This scheme allows triggers to be invoked even for events that aren't
  240. enabled, and also allows the current event filter implementation to be
  241. used for conditionally invoking triggers.
  242. The syntax for event triggers is roughly based on the syntax for
  243. set_ftrace_filter 'ftrace filter commands' (see the 'Filter commands'
  244. section of Documentation/trace/ftrace.rst), but there are major
  245. differences and the implementation isn't currently tied to it in any
  246. way, so beware about making generalizations between the two.
  247. Note: Writing into trace_marker (See Documentation/trace/ftrace.rst)
  248. can also enable triggers that are written into
  249. /sys/kernel/tracing/events/ftrace/print/trigger
  250. 6.1 Expression syntax
  251. ---------------------
  252. Triggers are added by echoing the command to the 'trigger' file::
  253. # echo 'command[:count] [if filter]' > trigger
  254. Triggers are removed by echoing the same command but starting with '!'
  255. to the 'trigger' file::
  256. # echo '!command[:count] [if filter]' > trigger
  257. The [if filter] part isn't used in matching commands when removing, so
  258. leaving that off in a '!' command will accomplish the same thing as
  259. having it in.
  260. The filter syntax is the same as that described in the 'Event
  261. filtering' section above.
  262. For ease of use, writing to the trigger file using '>' currently just
  263. adds or removes a single trigger and there's no explicit '>>' support
  264. ('>' actually behaves like '>>') or truncation support to remove all
  265. triggers (you have to use '!' for each one added.)
  266. 6.2 Supported trigger commands
  267. ------------------------------
  268. The following commands are supported:
  269. - enable_event/disable_event
  270. These commands can enable or disable another trace event whenever
  271. the triggering event is hit. When these commands are registered,
  272. the other trace event is activated, but disabled in a "soft" mode.
  273. That is, the tracepoint will be called, but just will not be traced.
  274. The event tracepoint stays in this mode as long as there's a trigger
  275. in effect that can trigger it.
  276. For example, the following trigger causes kmalloc events to be
  277. traced when a read system call is entered, and the :1 at the end
  278. specifies that this enablement happens only once::
  279. # echo 'enable_event:kmem:kmalloc:1' > \
  280. /sys/kernel/debug/tracing/events/syscalls/sys_enter_read/trigger
  281. The following trigger causes kmalloc events to stop being traced
  282. when a read system call exits. This disablement happens on every
  283. read system call exit::
  284. # echo 'disable_event:kmem:kmalloc' > \
  285. /sys/kernel/debug/tracing/events/syscalls/sys_exit_read/trigger
  286. The format is::
  287. enable_event:<system>:<event>[:count]
  288. disable_event:<system>:<event>[:count]
  289. To remove the above commands::
  290. # echo '!enable_event:kmem:kmalloc:1' > \
  291. /sys/kernel/debug/tracing/events/syscalls/sys_enter_read/trigger
  292. # echo '!disable_event:kmem:kmalloc' > \
  293. /sys/kernel/debug/tracing/events/syscalls/sys_exit_read/trigger
  294. Note that there can be any number of enable/disable_event triggers
  295. per triggering event, but there can only be one trigger per
  296. triggered event. e.g. sys_enter_read can have triggers enabling both
  297. kmem:kmalloc and sched:sched_switch, but can't have two kmem:kmalloc
  298. versions such as kmem:kmalloc and kmem:kmalloc:1 or 'kmem:kmalloc if
  299. bytes_req == 256' and 'kmem:kmalloc if bytes_alloc == 256' (they
  300. could be combined into a single filter on kmem:kmalloc though).
  301. - stacktrace
  302. This command dumps a stacktrace in the trace buffer whenever the
  303. triggering event occurs.
  304. For example, the following trigger dumps a stacktrace every time the
  305. kmalloc tracepoint is hit::
  306. # echo 'stacktrace' > \
  307. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  308. The following trigger dumps a stacktrace the first 5 times a kmalloc
  309. request happens with a size >= 64K::
  310. # echo 'stacktrace:5 if bytes_req >= 65536' > \
  311. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  312. The format is::
  313. stacktrace[:count]
  314. To remove the above commands::
  315. # echo '!stacktrace' > \
  316. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  317. # echo '!stacktrace:5 if bytes_req >= 65536' > \
  318. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  319. The latter can also be removed more simply by the following (without
  320. the filter)::
  321. # echo '!stacktrace:5' > \
  322. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  323. Note that there can be only one stacktrace trigger per triggering
  324. event.
  325. - snapshot
  326. This command causes a snapshot to be triggered whenever the
  327. triggering event occurs.
  328. The following command creates a snapshot every time a block request
  329. queue is unplugged with a depth > 1. If you were tracing a set of
  330. events or functions at the time, the snapshot trace buffer would
  331. capture those events when the trigger event occurred::
  332. # echo 'snapshot if nr_rq > 1' > \
  333. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  334. To only snapshot once::
  335. # echo 'snapshot:1 if nr_rq > 1' > \
  336. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  337. To remove the above commands::
  338. # echo '!snapshot if nr_rq > 1' > \
  339. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  340. # echo '!snapshot:1 if nr_rq > 1' > \
  341. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  342. Note that there can be only one snapshot trigger per triggering
  343. event.
  344. - traceon/traceoff
  345. These commands turn tracing on and off when the specified events are
  346. hit. The parameter determines how many times the tracing system is
  347. turned on and off. If unspecified, there is no limit.
  348. The following command turns tracing off the first time a block
  349. request queue is unplugged with a depth > 1. If you were tracing a
  350. set of events or functions at the time, you could then examine the
  351. trace buffer to see the sequence of events that led up to the
  352. trigger event::
  353. # echo 'traceoff:1 if nr_rq > 1' > \
  354. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  355. To always disable tracing when nr_rq > 1::
  356. # echo 'traceoff if nr_rq > 1' > \
  357. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  358. To remove the above commands::
  359. # echo '!traceoff:1 if nr_rq > 1' > \
  360. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  361. # echo '!traceoff if nr_rq > 1' > \
  362. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  363. Note that there can be only one traceon or traceoff trigger per
  364. triggering event.
  365. - hist
  366. This command aggregates event hits into a hash table keyed on one or
  367. more trace event format fields (or stacktrace) and a set of running
  368. totals derived from one or more trace event format fields and/or
  369. event counts (hitcount).
  370. See Documentation/trace/histogram.rst for details and examples.