events.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. Event Tracing
  2. Documentation written by Theodore Ts'o
  3. Updated by Li Zefan and Tom Zanussi
  4. 1. Introduction
  5. ===============
  6. Tracepoints (see Documentation/trace/tracepoints.txt) can be used
  7. without creating custom kernel modules to register probe functions
  8. using the event tracing infrastructure.
  9. Not all tracepoints can be traced using the event tracing system;
  10. the kernel developer must provide code snippets which define how the
  11. tracing information is saved into the tracing buffer, and how the
  12. tracing information should be printed.
  13. 2. Using Event Tracing
  14. ======================
  15. 2.1 Via the 'set_event' interface
  16. ---------------------------------
  17. The events which are available for tracing can be found in the file
  18. /sys/kernel/debug/tracing/available_events.
  19. To enable a particular event, such as 'sched_wakeup', simply echo it
  20. to /sys/kernel/debug/tracing/set_event. For example:
  21. # echo sched_wakeup >> /sys/kernel/debug/tracing/set_event
  22. [ Note: '>>' is necessary, otherwise it will firstly disable
  23. all the events. ]
  24. To disable an event, echo the event name to the set_event file prefixed
  25. with an exclamation point:
  26. # echo '!sched_wakeup' >> /sys/kernel/debug/tracing/set_event
  27. To disable all events, echo an empty line to the set_event file:
  28. # echo > /sys/kernel/debug/tracing/set_event
  29. To enable all events, echo '*:*' or '*:' to the set_event file:
  30. # echo *:* > /sys/kernel/debug/tracing/set_event
  31. The events are organized into subsystems, such as ext4, irq, sched,
  32. etc., and a full event name looks like this: <subsystem>:<event>. The
  33. subsystem name is optional, but it is displayed in the available_events
  34. file. All of the events in a subsystem can be specified via the syntax
  35. "<subsystem>:*"; for example, to enable all irq events, you can use the
  36. command:
  37. # echo 'irq:*' > /sys/kernel/debug/tracing/set_event
  38. 2.2 Via the 'enable' toggle
  39. ---------------------------
  40. The events available are also listed in /sys/kernel/debug/tracing/events/ hierarchy
  41. of directories.
  42. To enable event 'sched_wakeup':
  43. # echo 1 > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
  44. To disable it:
  45. # echo 0 > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
  46. To enable all events in sched subsystem:
  47. # echo 1 > /sys/kernel/debug/tracing/events/sched/enable
  48. To enable all events:
  49. # echo 1 > /sys/kernel/debug/tracing/events/enable
  50. When reading one of these enable files, there are four results:
  51. 0 - all events this file affects are disabled
  52. 1 - all events this file affects are enabled
  53. X - there is a mixture of events enabled and disabled
  54. ? - this file does not affect any event
  55. 2.3 Boot option
  56. ---------------
  57. In order to facilitate early boot debugging, use boot option:
  58. trace_event=[event-list]
  59. event-list is a comma separated list of events. See section 2.1 for event
  60. format.
  61. 3. Defining an event-enabled tracepoint
  62. =======================================
  63. See The example provided in samples/trace_events
  64. 4. Event formats
  65. ================
  66. Each trace event has a 'format' file associated with it that contains
  67. a description of each field in a logged event. This information can
  68. be used to parse the binary trace stream, and is also the place to
  69. find the field names that can be used in event filters (see section 5).
  70. It also displays the format string that will be used to print the
  71. event in text mode, along with the event name and ID used for
  72. profiling.
  73. Every event has a set of 'common' fields associated with it; these are
  74. the fields prefixed with 'common_'. The other fields vary between
  75. events and correspond to the fields defined in the TRACE_EVENT
  76. definition for that event.
  77. Each field in the format has the form:
  78. field:field-type field-name; offset:N; size:N;
  79. where offset is the offset of the field in the trace record and size
  80. is the size of the data item, in bytes.
  81. For example, here's the information displayed for the 'sched_wakeup'
  82. event:
  83. # cat /sys/kernel/debug/tracing/events/sched/sched_wakeup/format
  84. name: sched_wakeup
  85. ID: 60
  86. format:
  87. field:unsigned short common_type; offset:0; size:2;
  88. field:unsigned char common_flags; offset:2; size:1;
  89. field:unsigned char common_preempt_count; offset:3; size:1;
  90. field:int common_pid; offset:4; size:4;
  91. field:int common_tgid; offset:8; size:4;
  92. field:char comm[TASK_COMM_LEN]; offset:12; size:16;
  93. field:pid_t pid; offset:28; size:4;
  94. field:int prio; offset:32; size:4;
  95. field:int success; offset:36; size:4;
  96. field:int cpu; offset:40; size:4;
  97. print fmt: "task %s:%d [%d] success=%d [%03d]", REC->comm, REC->pid,
  98. REC->prio, REC->success, REC->cpu
  99. This event contains 10 fields, the first 5 common and the remaining 5
  100. event-specific. All the fields for this event are numeric, except for
  101. 'comm' which is a string, a distinction important for event filtering.
  102. 5. Event filtering
  103. ==================
  104. Trace events can be filtered in the kernel by associating boolean
  105. 'filter expressions' with them. As soon as an event is logged into
  106. the trace buffer, its fields are checked against the filter expression
  107. associated with that event type. An event with field values that
  108. 'match' the filter will appear in the trace output, and an event whose
  109. values don't match will be discarded. An event with no filter
  110. associated with it matches everything, and is the default when no
  111. filter has been set for an event.
  112. 5.1 Expression syntax
  113. ---------------------
  114. A filter expression consists of one or more 'predicates' that can be
  115. combined using the logical operators '&&' and '||'. A predicate is
  116. simply a clause that compares the value of a field contained within a
  117. logged event with a constant value and returns either 0 or 1 depending
  118. on whether the field value matched (1) or didn't match (0):
  119. field-name relational-operator value
  120. Parentheses can be used to provide arbitrary logical groupings and
  121. double-quotes can be used to prevent the shell from interpreting
  122. operators as shell metacharacters.
  123. The field-names available for use in filters can be found in the
  124. 'format' files for trace events (see section 4).
  125. The relational-operators depend on the type of the field being tested:
  126. The operators available for numeric fields are:
  127. ==, !=, <, <=, >, >=, &
  128. And for string fields they are:
  129. ==, !=, ~
  130. The glob (~) only accepts a wild card character (*) at the start and or
  131. end of the string. For example:
  132. prev_comm ~ "*sh"
  133. prev_comm ~ "sh*"
  134. prev_comm ~ "*sh*"
  135. But does not allow for it to be within the string:
  136. prev_comm ~ "ba*sh" <-- is invalid
  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. # cd /sys/kernel/debug/tracing
  210. # echo $$ > set_event_pid
  211. # echo 1 > events/enabled
  212. Will only trace events for the current task.
  213. To add more PIDs without losing the PIDs already included, use '>>'.
  214. # echo 123 244 1 >> set_event_pid
  215. 6. Event triggers
  216. =================
  217. Trace events can be made to conditionally invoke trigger 'commands'
  218. which can take various forms and are described in detail below;
  219. examples would be enabling or disabling other trace events or invoking
  220. a stack trace whenever the trace event is hit. Whenever a trace event
  221. with attached triggers is invoked, the set of trigger commands
  222. associated with that event is invoked. Any given trigger can
  223. additionally have an event filter of the same form as described in
  224. section 5 (Event filtering) associated with it - the command will only
  225. be invoked if the event being invoked passes the associated filter.
  226. If no filter is associated with the trigger, it always passes.
  227. Triggers are added to and removed from a particular event by writing
  228. trigger expressions to the 'trigger' file for the given event.
  229. A given event can have any number of triggers associated with it,
  230. subject to any restrictions that individual commands may have in that
  231. regard.
  232. Event triggers are implemented on top of "soft" mode, which means that
  233. whenever a trace event has one or more triggers associated with it,
  234. the event is activated even if it isn't actually enabled, but is
  235. disabled in a "soft" mode. That is, the tracepoint will be called,
  236. but just will not be traced, unless of course it's actually enabled.
  237. This scheme allows triggers to be invoked even for events that aren't
  238. enabled, and also allows the current event filter implementation to be
  239. used for conditionally invoking triggers.
  240. The syntax for event triggers is roughly based on the syntax for
  241. set_ftrace_filter 'ftrace filter commands' (see the 'Filter commands'
  242. section of Documentation/trace/ftrace.txt), but there are major
  243. differences and the implementation isn't currently tied to it in any
  244. way, so beware about making generalizations between the two.
  245. 6.1 Expression syntax
  246. ---------------------
  247. Triggers are added by echoing the command to the 'trigger' file:
  248. # echo 'command[:count] [if filter]' > trigger
  249. Triggers are removed by echoing the same command but starting with '!'
  250. to the 'trigger' file:
  251. # echo '!command[:count] [if filter]' > trigger
  252. The [if filter] part isn't used in matching commands when removing, so
  253. leaving that off in a '!' command will accomplish the same thing as
  254. having it in.
  255. The filter syntax is the same as that described in the 'Event
  256. filtering' section above.
  257. For ease of use, writing to the trigger file using '>' currently just
  258. adds or removes a single trigger and there's no explicit '>>' support
  259. ('>' actually behaves like '>>') or truncation support to remove all
  260. triggers (you have to use '!' for each one added.)
  261. 6.2 Supported trigger commands
  262. ------------------------------
  263. The following commands are supported:
  264. - enable_event/disable_event
  265. These commands can enable or disable another trace event whenever
  266. the triggering event is hit. When these commands are registered,
  267. the other trace event is activated, but disabled in a "soft" mode.
  268. That is, the tracepoint will be called, but just will not be traced.
  269. The event tracepoint stays in this mode as long as there's a trigger
  270. in effect that can trigger it.
  271. For example, the following trigger causes kmalloc events to be
  272. traced when a read system call is entered, and the :1 at the end
  273. specifies that this enablement happens only once:
  274. # echo 'enable_event:kmem:kmalloc:1' > \
  275. /sys/kernel/debug/tracing/events/syscalls/sys_enter_read/trigger
  276. The following trigger causes kmalloc events to stop being traced
  277. when a read system call exits. This disablement happens on every
  278. read system call exit:
  279. # echo 'disable_event:kmem:kmalloc' > \
  280. /sys/kernel/debug/tracing/events/syscalls/sys_exit_read/trigger
  281. The format is:
  282. enable_event:<system>:<event>[:count]
  283. disable_event:<system>:<event>[:count]
  284. To remove the above commands:
  285. # echo '!enable_event:kmem:kmalloc:1' > \
  286. /sys/kernel/debug/tracing/events/syscalls/sys_enter_read/trigger
  287. # echo '!disable_event:kmem:kmalloc' > \
  288. /sys/kernel/debug/tracing/events/syscalls/sys_exit_read/trigger
  289. Note that there can be any number of enable/disable_event triggers
  290. per triggering event, but there can only be one trigger per
  291. triggered event. e.g. sys_enter_read can have triggers enabling both
  292. kmem:kmalloc and sched:sched_switch, but can't have two kmem:kmalloc
  293. versions such as kmem:kmalloc and kmem:kmalloc:1 or 'kmem:kmalloc if
  294. bytes_req == 256' and 'kmem:kmalloc if bytes_alloc == 256' (they
  295. could be combined into a single filter on kmem:kmalloc though).
  296. - stacktrace
  297. This command dumps a stacktrace in the trace buffer whenever the
  298. triggering event occurs.
  299. For example, the following trigger dumps a stacktrace every time the
  300. kmalloc tracepoint is hit:
  301. # echo 'stacktrace' > \
  302. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  303. The following trigger dumps a stacktrace the first 5 times a kmalloc
  304. request happens with a size >= 64K
  305. # echo 'stacktrace:5 if bytes_req >= 65536' > \
  306. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  307. The format is:
  308. stacktrace[:count]
  309. To remove the above commands:
  310. # echo '!stacktrace' > \
  311. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  312. # echo '!stacktrace:5 if bytes_req >= 65536' > \
  313. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  314. The latter can also be removed more simply by the following (without
  315. the filter):
  316. # echo '!stacktrace:5' > \
  317. /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
  318. Note that there can be only one stacktrace trigger per triggering
  319. event.
  320. - snapshot
  321. This command causes a snapshot to be triggered whenever the
  322. triggering event occurs.
  323. The following command creates a snapshot every time a block request
  324. queue is unplugged with a depth > 1. If you were tracing a set of
  325. events or functions at the time, the snapshot trace buffer would
  326. capture those events when the trigger event occurred:
  327. # echo 'snapshot if nr_rq > 1' > \
  328. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  329. To only snapshot once:
  330. # echo 'snapshot:1 if nr_rq > 1' > \
  331. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  332. To remove the above commands:
  333. # echo '!snapshot if nr_rq > 1' > \
  334. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  335. # echo '!snapshot:1 if nr_rq > 1' > \
  336. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  337. Note that there can be only one snapshot trigger per triggering
  338. event.
  339. - traceon/traceoff
  340. These commands turn tracing on and off when the specified events are
  341. hit. The parameter determines how many times the tracing system is
  342. turned on and off. If unspecified, there is no limit.
  343. The following command turns tracing off the first time a block
  344. request queue is unplugged with a depth > 1. If you were tracing a
  345. set of events or functions at the time, you could then examine the
  346. trace buffer to see the sequence of events that led up to the
  347. trigger event:
  348. # echo 'traceoff:1 if nr_rq > 1' > \
  349. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  350. To always disable tracing when nr_rq > 1 :
  351. # echo 'traceoff if nr_rq > 1' > \
  352. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  353. To remove the above commands:
  354. # echo '!traceoff:1 if nr_rq > 1' > \
  355. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  356. # echo '!traceoff if nr_rq > 1' > \
  357. /sys/kernel/debug/tracing/events/block/block_unplug/trigger
  358. Note that there can be only one traceon or traceoff trigger per
  359. triggering event.