xref: /linux/Documentation/trace/kprobetrace.rst (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1==========================
2Kprobe-based Event Tracing
3==========================
4
5:Author: Masami Hiramatsu
6
7Overview
8--------
9These events are similar to tracepoint-based events. Instead of tracepoints,
10this is based on kprobes (kprobe and kretprobe). So it can probe wherever
11kprobes can probe (this means, all functions except those with
12__kprobes/nokprobe_inline annotation and those marked NOKPROBE_SYMBOL).
13Unlike the tracepoint-based event, this can be added and removed
14dynamically, on the fly.
15
16To enable this feature, build your kernel with CONFIG_KPROBE_EVENTS=y.
17
18Similar to the event tracer, this doesn't need to be activated via
19current_tracer. Instead of that, add probe points via
20/sys/kernel/tracing/kprobe_events, and enable it via
21/sys/kernel/tracing/events/kprobes/<EVENT>/enable.
22
23You can also use /sys/kernel/tracing/dynamic_events instead of
24kprobe_events. That interface will provide unified access to other
25dynamic events too.
26
27Synopsis of kprobe_events
28-------------------------
29::
30
31  p[:[GRP/][EVENT]] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS]	: Set a probe
32  r[MAXACTIVE][:[GRP/][EVENT]] [MOD:]SYM[+0] [FETCHARGS]	: Set a return probe
33  p[:[GRP/][EVENT]] [MOD:]SYM[+0]%return [FETCHARGS]	: Set a return probe
34  -:[GRP/][EVENT]						: Clear a probe
35
36 GRP		: Group name. If omitted, use "kprobes" for it.
37 EVENT		: Event name. If omitted, the event name is generated
38		  based on SYM+offs or MEMADDR.
39 MOD		: Module name which has given SYM.
40 SYM[+offs]	: Symbol+offset where the probe is inserted.
41 SYM%return	: Return address of the symbol
42 MEMADDR	: Address where the probe is inserted.
43 MAXACTIVE	: Maximum number of instances of the specified function that
44		  can be probed simultaneously, or 0 for the default value
45		  as defined in Documentation/trace/kprobes.rst section 1.3.1.
46
47 FETCHARGS	: Arguments. Each probe can have up to 128 args.
48  %REG		: Fetch register REG
49  @ADDR		: Fetch memory at ADDR (ADDR should be in kernel)
50  @SYM[+|-offs]	: Fetch memory at SYM +|- offs (SYM should be a data symbol)
51  $stackN	: Fetch Nth entry of stack (N >= 0)
52  $stack	: Fetch stack address.
53  $argN		: Fetch the Nth function argument. (N >= 1) (\*1)
54  $retval	: Fetch return value.(\*2)
55  $comm		: Fetch current task comm.
56  +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*3)(\*4)
57  \IMM		: Store an immediate value to the argument.
58  NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
59  FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
60		  (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
61		  (x8/x16/x32/x64), VFS layer common type(%pd/%pD), "char",
62                  "string", "ustring", "symbol", "symstr" and bitfield are
63                  supported.
64
65  (\*1) only for the probe on function entry (offs == 0). Note, this argument access
66        is best effort, because depending on the argument type, it may be passed on
67        the stack. But this only support the arguments via registers.
68  (\*2) only for return probe. Note that this is also best effort. Depending on the
69        return value type, it might be passed via a pair of registers. But this only
70        accesses one register.
71  (\*3) this is useful for fetching a field of data structures.
72  (\*4) "u" means user-space dereference. See :ref:`user_mem_access`.
73
74Function arguments at kretprobe
75-------------------------------
76Function arguments can be accessed at kretprobe using $arg<N> fetcharg. This
77is useful to record the function parameter and return value at once, and
78trace the difference of structure fields (for debugging a function whether it
79correctly updates the given data structure or not).
80See the :ref:`sample<fprobetrace_exit_args_sample>` in fprobe event for how
81it works.
82
83.. _kprobetrace_types:
84
85Types
86-----
87Several types are supported for fetchargs. Kprobe tracer will access memory
88by given type. Prefix 's' and 'u' means those types are signed and unsigned
89respectively. 'x' prefix implies it is unsigned. Traced arguments are shown
90in decimal ('s' and 'u') or hexadecimal ('x'). Without type casting, 'x32'
91or 'x64' is used depends on the architecture (e.g. x86-32 uses x32, and
92x86-64 uses x64).
93
94These value types can be an array. To record array data, you can add '[N]'
95(where N is a fixed number, less than 64) to the base type.
96E.g. 'x16[4]' means an array of x16 (2-byte hex) with 4 elements.
97Note that the array can be applied to memory type fetchargs, you can not
98apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
99wrong, but '+8($stack):x8[8]' is OK.)
100
101Char type can be used to show the character value of traced arguments.
102
103String type is a special type, which fetches a "null-terminated" string from
104kernel space. This means it will fail and store NULL if the string container
105has been paged out. "ustring" type is an alternative of string for user-space.
106See :ref:`user_mem_access` for more info.
107
108The string array type is a bit different from other types. For other base
109types, <base-type>[1] is equal to <base-type> (e.g. +0(%di):x32[1] is same
110as +0(%di):x32.) But string[1] is not equal to string. The string type itself
111represents "char array", but string array type represents "char * array".
112So, for example, +0(%di):string[1] is equal to +0(+0(%di)):string.
113Bitfield is another special type, which takes 3 parameters, bit-width, bit-
114offset, and container-size (usually 32). The syntax is::
115
116 b<bit-width>@<bit-offset>/<container-size>
117
118Symbol type('symbol') is an alias of u32 or u64 type (depends on BITS_PER_LONG)
119which shows given pointer in "symbol+offset" style.
120On the other hand, symbol-string type ('symstr') converts the given address to
121"symbol+offset/symbolsize" style and stores it as a null-terminated string.
122With 'symstr' type, you can filter the event with wildcard pattern of the
123symbols, and you don't need to solve symbol name by yourself.
124For $comm, the default type is "string"; any other type is invalid.
125
126VFS layer common type(%pd/%pD) is a special type, which fetches dentry's or
127file's name from struct dentry's address or struct file's address.
128
129.. _user_mem_access:
130
131User Memory Access
132------------------
133Kprobe events supports user-space memory access. For that purpose, you can use
134either user-space dereference syntax or 'ustring' type.
135
136The user-space dereference syntax allows you to access a field of a data
137structure in user-space. This is done by adding the "u" prefix to the
138dereference syntax. For example, +u4(%si) means it will read memory from the
139address in the register %si offset by 4, and the memory is expected to be in
140user-space. You can use this for strings too, e.g. +u0(%si):string will read
141a string from the address in the register %si that is expected to be in user-
142space. 'ustring' is a shortcut way of performing the same task. That is,
143+0(%si):ustring is equivalent to +u0(%si):string.
144
145Note that kprobe-event provides the user-memory access syntax but it doesn't
146use it transparently. This means if you use normal dereference or string type
147for user memory, it might fail, and may always fail on some architectures. The
148user has to carefully check if the target data is in kernel or user space.
149
150Per-Probe Event Filtering
151-------------------------
152Per-probe event filtering feature allows you to set different filter on each
153probe and gives you what arguments will be shown in trace buffer. If an event
154name is specified right after 'p:' or 'r:' in kprobe_events, it adds an event
155under tracing/events/kprobes/<EVENT>, at the directory you can see 'id',
156'enable', 'format', 'filter' and 'trigger'.
157
158enable:
159  You can enable/disable the probe by writing 1 or 0 on it.
160
161format:
162  This shows the format of this probe event.
163
164filter:
165  You can write filtering rules of this event.
166
167id:
168  This shows the id of this probe event.
169
170trigger:
171  This allows to install trigger commands which are executed when the event is
172  hit (for details, see Documentation/trace/events.rst, section 6).
173
174Event Profiling
175---------------
176You can check the total number of probe hits and probe miss-hits via
177/sys/kernel/tracing/kprobe_profile.
178The first column is event name, the second is the number of probe hits,
179the third is the number of probe miss-hits.
180
181Kernel Boot Parameter
182---------------------
183You can add and enable new kprobe events when booting up the kernel by
184"kprobe_event=" parameter. The parameter accepts a semicolon-delimited
185kprobe events, which format is similar to the kprobe_events.
186The difference is that the probe definition parameters are comma-delimited
187instead of space. For example, adding myprobe event on do_sys_open like below::
188
189  p:myprobe do_sys_open dfd=%ax filename=%dx flags=%cx mode=+4($stack)
190
191should be below for kernel boot parameter (just replace spaces with comma)::
192
193  p:myprobe,do_sys_open,dfd=%ax,filename=%dx,flags=%cx,mode=+4($stack)
194
195
196Usage examples
197--------------
198To add a probe as a new event, write a new definition to kprobe_events
199as below::
200
201  echo 'p:myprobe do_sys_open dfd=%ax filename=%dx flags=%cx mode=+4($stack)' > /sys/kernel/tracing/kprobe_events
202
203This sets a kprobe on the top of do_sys_open() function with recording
2041st to 4th arguments as "myprobe" event. Note, which register/stack entry is
205assigned to each function argument depends on arch-specific ABI. If you unsure
206the ABI, please try to use probe subcommand of perf-tools (you can find it
207under tools/perf/).
208As this example shows, users can choose more familiar names for each arguments.
209::
210
211  echo 'r:myretprobe do_sys_open $retval' >> /sys/kernel/tracing/kprobe_events
212
213This sets a kretprobe on the return point of do_sys_open() function with
214recording return value as "myretprobe" event.
215You can see the format of these events via
216/sys/kernel/tracing/events/kprobes/<EVENT>/format.
217::
218
219  cat /sys/kernel/tracing/events/kprobes/myprobe/format
220  name: myprobe
221  ID: 780
222  format:
223          field:unsigned short common_type;       offset:0;       size:2; signed:0;
224          field:unsigned char common_flags;       offset:2;       size:1; signed:0;
225          field:unsigned char common_preempt_count;       offset:3; size:1;signed:0;
226          field:int common_pid;   offset:4;       size:4; signed:1;
227
228          field:unsigned long __probe_ip; offset:12;      size:4; signed:0;
229          field:int __probe_nargs;        offset:16;      size:4; signed:1;
230          field:unsigned long dfd;        offset:20;      size:4; signed:0;
231          field:unsigned long filename;   offset:24;      size:4; signed:0;
232          field:unsigned long flags;      offset:28;      size:4; signed:0;
233          field:unsigned long mode;       offset:32;      size:4; signed:0;
234
235
236  print fmt: "(%lx) dfd=%lx filename=%lx flags=%lx mode=%lx", REC->__probe_ip,
237  REC->dfd, REC->filename, REC->flags, REC->mode
238
239You can see that the event has 4 arguments as in the expressions you specified.
240::
241
242  echo > /sys/kernel/tracing/kprobe_events
243
244This clears all probe points.
245
246Or,
247::
248
249  echo -:myprobe >> kprobe_events
250
251This clears probe points selectively.
252
253Right after definition, each event is disabled by default. For tracing these
254events, you need to enable it.
255::
256
257  echo 1 > /sys/kernel/tracing/events/kprobes/myprobe/enable
258  echo 1 > /sys/kernel/tracing/events/kprobes/myretprobe/enable
259
260Use the following command to start tracing in an interval.
261::
262
263    # echo 1 > tracing_on
264    Open something...
265    # echo 0 > tracing_on
266
267And you can see the traced information via /sys/kernel/tracing/trace.
268::
269
270  cat /sys/kernel/tracing/trace
271  # tracer: nop
272  #
273  #           TASK-PID    CPU#    TIMESTAMP  FUNCTION
274  #              | |       |          |         |
275             <...>-1447  [001] 1038282.286875: myprobe: (do_sys_open+0x0/0xd6) dfd=3 filename=7fffd1ec4440 flags=8000 mode=0
276             <...>-1447  [001] 1038282.286878: myretprobe: (sys_openat+0xc/0xe <- do_sys_open) $retval=fffffffffffffffe
277             <...>-1447  [001] 1038282.286885: myprobe: (do_sys_open+0x0/0xd6) dfd=ffffff9c filename=40413c flags=8000 mode=1b6
278             <...>-1447  [001] 1038282.286915: myretprobe: (sys_open+0x1b/0x1d <- do_sys_open) $retval=3
279             <...>-1447  [001] 1038282.286969: myprobe: (do_sys_open+0x0/0xd6) dfd=ffffff9c filename=4041c6 flags=98800 mode=10
280             <...>-1447  [001] 1038282.286976: myretprobe: (sys_open+0x1b/0x1d <- do_sys_open) $retval=3
281
282
283Each line shows when the kernel hits an event, and <- SYMBOL means kernel
284returns from SYMBOL(e.g. "sys_open+0x1b/0x1d <- do_sys_open" means kernel
285returns from do_sys_open to sys_open+0x1b).
286