blob: 9b8f7c6040a77e7a77a4cc35b210b8e3ebd59dc5 [file] [log] [blame]
Masami Hiramatsud8ec9182009-08-19 21:13:57 +02001 Kprobe-based Event Tracer
2 =========================
3
4 Documentation is written by Masami Hiramatsu
5
6
7Overview
8--------
9This tracer is similar to the events tracer which is based on Tracepoint
10infrastructure. Instead of Tracepoint, this tracer is based on kprobes(kprobe
11and kretprobe). It probes anywhere where kprobes can probe(this means, all
12functions body except for __kprobes functions).
13
14Unlike the function tracer, this tracer can probe instructions inside of
15kernel functions. It allows you to check which instruction has been executed.
16
17Unlike the Tracepoint based events tracer, this tracer can add and remove
18probe points on the fly.
19
20Similar to the events tracer, this tracer doesn't need to be activated via
21current_tracer, instead of that, just set probe points via
22/sys/kernel/debug/tracing/kprobe_events. And you can set filters on each
23probe events via /sys/kernel/debug/tracing/events/kprobes/<EVENT>/filter.
24
25
26Synopsis of kprobe_events
27-------------------------
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040028 p[:[GRP/]EVENT] SYMBOL[+offs]|MEMADDR [FETCHARGS] : Set a probe
29 r[:[GRP/]EVENT] SYMBOL[+0] [FETCHARGS] : Set a return probe
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020030
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040031 GRP : Group name. If omitted, use "kprobes" for it.
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -040032 EVENT : Event name. If omitted, the event name is generated
33 based on SYMBOL+offs or MEMADDR.
34 SYMBOL[+offs] : Symbol+offset where the probe is inserted.
35 MEMADDR : Address where the probe is inserted.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020036
Masami Hiramatsu2fba0c82009-09-10 19:53:14 -040037 FETCHARGS : Arguments. Each probe can have up to 128 args.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020038 %REG : Fetch register REG
39 sN : Fetch Nth entry of stack (N >= 0)
40 sa : Fetch stack address.
41 @ADDR : Fetch memory at ADDR (ADDR should be in kernel)
42 @SYM[+|-offs] : Fetch memory at SYM +|- offs (SYM should be a data symbol)
43 aN : Fetch function argument. (N >= 0)(*)
44 rv : Fetch return value.(**)
45 ra : Fetch return address.(**)
Masami Hiramatsueca0d912009-09-10 19:53:38 -040046 +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(***)
47 NAME=FETCHARG: Set NAME as the argument name of FETCHARG.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020048
49 (*) aN may not correct on asmlinkaged functions and at the middle of
50 function body.
51 (**) only for return probe.
52 (***) this is useful for fetching a field of data structures.
53
54
55Per-Probe Event Filtering
56-------------------------
57 Per-probe event filtering feature allows you to set different filter on each
58probe and gives you what arguments will be shown in trace buffer. If an event
59name is specified right after 'p:' or 'r:' in kprobe_events, the tracer adds
60an event under tracing/events/kprobes/<EVENT>, at the directory you can see
61'id', 'enabled', 'format' and 'filter'.
62
63enabled:
64 You can enable/disable the probe by writing 1 or 0 on it.
65
66format:
Masami Hiramatsueca0d912009-09-10 19:53:38 -040067 This shows the format of this probe event.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020068
69filter:
Masami Hiramatsueca0d912009-09-10 19:53:38 -040070 You can write filtering rules of this event.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020071
Masami Hiramatsue08d1c62009-09-10 19:53:30 -040072id:
73 This shows the id of this probe event.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020074
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -040075Event Profiling
76---------------
77 You can check the total number of probe hits and probe miss-hits via
78/sys/kernel/debug/tracing/kprobe_profile.
79 The first column is event name, the second is the number of probe hits,
80the third is the number of probe miss-hits.
81
82
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020083Usage examples
84--------------
85To add a probe as a new event, write a new definition to kprobe_events
86as below.
87
Masami Hiramatsueca0d912009-09-10 19:53:38 -040088 echo p:myprobe do_sys_open dfd=a0 filename=a1 flags=a2 mode=a3 > /sys/kernel/debug/tracing/kprobe_events
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020089
90 This sets a kprobe on the top of do_sys_open() function with recording
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400911st to 4th arguments as "myprobe" event. As this example shows, users can
92choose more familiar names for each arguments.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +020093
94 echo r:myretprobe do_sys_open rv ra >> /sys/kernel/debug/tracing/kprobe_events
95
96 This sets a kretprobe on the return point of do_sys_open() function with
97recording return value and return address as "myretprobe" event.
98 You can see the format of these events via
99/sys/kernel/debug/tracing/events/kprobes/<EVENT>/format.
100
101 cat /sys/kernel/debug/tracing/events/kprobes/myprobe/format
102name: myprobe
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400103ID: 75
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200104format:
105 field:unsigned short common_type; offset:0; size:2;
106 field:unsigned char common_flags; offset:2; size:1;
107 field:unsigned char common_preempt_count; offset:3; size:1;
108 field:int common_pid; offset:4; size:4;
109 field:int common_tgid; offset:8; size:4;
110
111 field: unsigned long ip; offset:16;tsize:8;
112 field: int nargs; offset:24;tsize:4;
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400113 field: unsigned long dfd; offset:32;tsize:8;
114 field: unsigned long filename; offset:40;tsize:8;
115 field: unsigned long flags; offset:48;tsize:8;
116 field: unsigned long mode; offset:56;tsize:8;
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200117
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400118print fmt: "(%lx) dfd=%lx filename=%lx flags=%lx mode=%lx", REC->ip, REC->dfd, REC->filename, REC->flags, REC->mode
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200119
120
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400121 You can see that the event has 4 arguments as in the expressions you specified.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200122
123 echo > /sys/kernel/debug/tracing/kprobe_events
124
Masami Hiramatsu5a0d9052009-09-14 16:49:37 -0400125 This clears all probe points.
126
127 Right after definition, each event is disabled by default. For tracing these
128events, you need to enable it.
129
130 echo 1 > /sys/kernel/debug/tracing/events/kprobes/myprobe/enable
131 echo 1 > /sys/kernel/debug/tracing/events/kprobes/myretprobe/enable
132
133 And you can see the traced information via /sys/kernel/debug/tracing/trace.
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200134
135 cat /sys/kernel/debug/tracing/trace
136# tracer: nop
137#
138# TASK-PID CPU# TIMESTAMP FUNCTION
139# | | | | |
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400140 <...>-1447 [001] 1038282.286875: myprobe: (do_sys_open+0x0/0xd6) dfd=3 filename=7fffd1ec4440 flags=8000 mode=0
141 <...>-1447 [001] 1038282.286878: myretprobe: (sys_openat+0xc/0xe <- do_sys_open) rv=fffffffffffffffe ra=ffffffff81367a3a
142 <...>-1447 [001] 1038282.286885: myprobe: (do_sys_open+0x0/0xd6) dfd=ffffff9c filename=40413c flags=8000 mode=1b6
143 <...>-1447 [001] 1038282.286915: myretprobe: (sys_open+0x1b/0x1d <- do_sys_open) rv=3 ra=ffffffff81367a3a
144 <...>-1447 [001] 1038282.286969: myprobe: (do_sys_open+0x0/0xd6) dfd=ffffff9c filename=4041c6 flags=98800 mode=10
145 <...>-1447 [001] 1038282.286976: myretprobe: (sys_open+0x1b/0x1d <- do_sys_open) rv=3 ra=ffffffff81367a3a
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200146
147
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -0400148 Each line shows when the kernel hits an event, and <- SYMBOL means kernel
Masami Hiramatsud8ec9182009-08-19 21:13:57 +0200149returns from SYMBOL(e.g. "sys_open+0x1b/0x1d <- do_sys_open" means kernel
150returns from do_sys_open to sys_open+0x1b).
151
152