blob: eb72e5e5790094cbaf657a45f16e561c2583921e [file] [log] [blame]
Sasha Goldshtein38847f02016-02-22 02:19:24 -08001Demonstrations of trace.
2
3
4trace probes functions you specify and displays trace messages if a particular
ShelbyFrances69abacc2017-02-08 05:56:53 +03005condition is met. You can control the message format to display function
6arguments and return values.
Sasha Goldshtein38847f02016-02-22 02:19:24 -08007
8For example, suppose you want to trace all commands being exec'd across the
9system:
10
Sasha Goldshtein8acd0152016-02-22 02:25:03 -080011# trace 'sys_execve "%s", arg1'
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000012PID COMM FUNC -
134402 bash sys_execve /usr/bin/man
144411 man sys_execve /usr/local/bin/less
154411 man sys_execve /usr/bin/less
164410 man sys_execve /usr/local/bin/nroff
174410 man sys_execve /usr/bin/nroff
184409 man sys_execve /usr/local/bin/tbl
194409 man sys_execve /usr/bin/tbl
204408 man sys_execve /usr/local/bin/preconv
214408 man sys_execve /usr/bin/preconv
224415 nroff sys_execve /usr/bin/locale
234416 nroff sys_execve /usr/bin/groff
244418 groff sys_execve /usr/bin/grotty
254417 groff sys_execve /usr/bin/troff
Sasha Goldshtein38847f02016-02-22 02:19:24 -080026^C
27
28The ::sys_execve syntax specifies that you want an entry probe (which is the
29default), in a kernel function (which is the default) called sys_execve. Next,
30the format string to print is simply "%s", which prints a string. Finally, the
31value to print is the first argument to the sys_execve function, which happens
32to be the command that is exec'd. The above trace was generated by executing
33"man ls" in a separate shell. As you see, man executes a number of additional
34programs to finally display the man page.
35
36Next, suppose you are looking for large reads across the system. Let's trace
37the read system call and inspect the third argument, which is the number of
38bytes to be read:
39
Sasha Goldshtein8acd0152016-02-22 02:25:03 -080040# trace 'sys_read (arg3 > 20000) "read %d bytes", arg3'
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000041PID COMM FUNC -
424490 dd sys_read read 1048576 bytes
434490 dd sys_read read 1048576 bytes
444490 dd sys_read read 1048576 bytes
454490 dd sys_read read 1048576 bytes
Sasha Goldshtein38847f02016-02-22 02:19:24 -080046^C
47
48During the trace, I executed "dd if=/dev/zero of=/dev/null bs=1M count=4".
49The individual reads are visible, with the custom format message printed for
50each read. The parenthesized expression "(arg3 > 20000)" is a filter that is
51evaluated for each invocation of the probe before printing anything.
52
53You can also trace user functions. For example, let's simulate the bashreadline
54script, which attaches to the readline function in bash and prints its return
55value, effectively snooping all bash shell input across the system:
56
57# trace 'r:bash:readline "%s", retval'
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000058PID COMM FUNC -
592740 bash readline echo hi!
602740 bash readline man ls
Sasha Goldshtein38847f02016-02-22 02:19:24 -080061^C
62
63The special retval keywords stands for the function's return value, and can
64be used only in a retprobe, specified by the 'r' prefix. The next component
65of the probe is the library that contains the desired function. It's OK to
66specify executables too, as long as they can be found in the PATH. Or, you
67can specify the full path to the executable (e.g. "/usr/bin/bash").
68
69Multiple probes can be combined on the same command line. For example, let's
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000070trace failed read and write calls on the libc level, and include a time column:
Sasha Goldshtein38847f02016-02-22 02:19:24 -080071
72# trace 'r:c:read ((int)retval < 0) "read failed: %d", retval' \
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000073 'r:c:write ((int)retval < 0) "write failed: %d", retval' -T
Sasha Goldshtein38847f02016-02-22 02:19:24 -080074TIME PID COMM FUNC -
7505:31:57 3388 bash write write failed: -1
7605:32:00 3388 bash write write failed: -1
77^C
78
79Note that the retval variable must be cast to int before comparing to zero.
80The reason is that the default type for argN and retval is an unsigned 64-bit
81integer, which can never be smaller than 0.
82
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080083trace has also some basic support for kernel tracepoints. For example, let's
84trace the block:block_rq_complete tracepoint and print out the number of sectors
85transferred:
86
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000087# trace 't:block:block_rq_complete "sectors=%d", args->nr_sector' -T
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080088TIME PID COMM FUNC -
8901:23:51 0 swapper/0 block_rq_complete sectors=8
9001:23:55 10017 kworker/u64: block_rq_complete sectors=1
9101:23:55 0 swapper/0 block_rq_complete sectors=8
92^C
93
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +030094To discover the tracepoint structure format (which you can refer to as the "args"
95pointer variable), use the tplist tool. For example:
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080096
97# tplist -v block:block_rq_complete
98block:block_rq_complete
99 dev_t dev;
100 sector_t sector;
101 unsigned int nr_sector;
102 int errors;
103 char rwbs[8];
104
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300105This output tells you that you can use "args->dev", "args->sector", etc. in your
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800106predicate and trace arguments.
107
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700108
109More and more high-level libraries are instrumented with USDT probe support.
110These probes can be traced by trace just like kernel tracepoints. For example,
111trace new threads being created and their function name:
112
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000113# trace 'u:pthread:pthread_create "%U", arg3' -T
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700114TIME PID COMM FUNC -
11502:07:29 4051 contentions pthread_create primes_thread+0x0
11602:07:29 4051 contentions pthread_create primes_thread+0x0
11702:07:29 4051 contentions pthread_create primes_thread+0x0
11802:07:29 4051 contentions pthread_create primes_thread+0x0
119^C
120
121The "%U" format specifier tells trace to resolve arg3 as a user-space symbol,
122if possible. Similarly, use "%K" for kernel symbols.
123
124Ruby, Node, and OpenJDK are also instrumented with USDT. For example, let's
125trace Ruby methods being called (this requires a version of Ruby built with
126the --enable-dtrace configure flag):
127
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000128# trace 'u:ruby:method__entry "%s.%s", arg1, arg2' -p $(pidof irb) -T
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700129TIME PID COMM FUNC -
13012:08:43 18420 irb method__entry IRB::Context.verbose?
13112:08:43 18420 irb method__entry RubyLex.ungetc
13212:08:43 18420 irb method__entry RuxyLex.debug?
133^C
134
135In the previous invocation, arg1 and arg2 are the class name and method name
136for the Ruby method being invoked.
137
ShelbyFrances86cd5352017-02-08 05:56:37 +0300138You can also trace exported functions from shared libraries, or an imported
139function on the actual executable:
140
141# sudo ./trace.py 'r:/usr/lib64/libtinfo.so:curses_version "Version=%s", retval'
142# tput -V
143
144PID TID COMM FUNC -
14521720 21720 tput curses_version Version=ncurses 6.0.20160709
146^C
147
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700148
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700149Occasionally, it can be useful to filter specific strings. For example, you
150might be interested in open() calls that open a specific file:
151
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000152# trace 'p:c:open (STRCMP("test.txt", arg1)) "opening %s", arg1' -T
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700153TIME PID COMM FUNC -
15401:43:15 10938 cat open opening test.txt
15501:43:20 10939 cat open opening test.txt
156^C
157
158
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000159In the preceding example, as well as in many others, readability may be
160improved by providing the function's signature, which names the arguments and
161lets you access structure sub-fields, which is hard with the "arg1", "arg2"
162convention. For example:
163
164# trace 'p:c:open(char *filename) "opening %s", filename'
165PID TID COMM FUNC -
16617507 17507 cat open opening FAQ.txt
167^C
168
169# trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec'
170PID TID COMM FUNC -
171777 785 automount SyS_nanosleep sleep for 500000000 ns
172777 785 automount SyS_nanosleep sleep for 500000000 ns
173777 785 automount SyS_nanosleep sleep for 500000000 ns
174777 785 automount SyS_nanosleep sleep for 500000000 ns
175^C
176
ShelbyFrances69abacc2017-02-08 05:56:53 +0300177Remember to use the -I argument include the appropriate header file. We didn't
178need to do that here because `struct timespec` is used internally by the tool,
179so it always includes this header file.
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000180
ShelbyFrances69abacc2017-02-08 05:56:53 +0300181
182As a final example, let's trace open syscalls for a specific process. By
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800183default, tracing is system-wide, but the -p switch overrides this:
184
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000185# trace -p 2740 'do_sys_open "%s", arg2' -T
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800186TIME PID COMM FUNC -
18705:36:16 15872 ls do_sys_open /etc/ld.so.cache
18805:36:16 15872 ls do_sys_open /lib64/libselinux.so.1
18905:36:16 15872 ls do_sys_open /lib64/libcap.so.2
19005:36:16 15872 ls do_sys_open /lib64/libacl.so.1
19105:36:16 15872 ls do_sys_open /lib64/libc.so.6
19205:36:16 15872 ls do_sys_open /lib64/libpcre.so.1
19305:36:16 15872 ls do_sys_open /lib64/libdl.so.2
19405:36:16 15872 ls do_sys_open /lib64/libattr.so.1
19505:36:16 15872 ls do_sys_open /lib64/libpthread.so.0
19605:36:16 15872 ls do_sys_open /usr/lib/locale/locale-archive
19705:36:16 15872 ls do_sys_open /home/vagrant
198^C
199
200In this example, we traced the "ls ~" command as it was opening its shared
201libraries and then accessing the /home/vagrant directory listing.
202
203
Mark Drayton5f5687e2017-02-20 18:13:03 +0000204Lastly, if a high-frequency event is traced you may overflow the perf ring
205buffer. This shows as "Lost N samples":
206
207# trace sys_open
2085087 5087 pgrep sys_open
2095087 5087 pgrep sys_open
2105087 5087 pgrep sys_open
2115087 5087 pgrep sys_open
2125087 5087 pgrep sys_open
213Lost 764896 samples
214Lost 764896 samples
215Lost 764896 samples
216
217The perf ring buffer size can be changed with -b. The unit is size per-CPU buffer
218size and is measured in pages. The value must be a power of two and defaults to
21964 pages.
220
221
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800222USAGE message:
223
Mark Drayton5f5687e2017-02-20 18:13:03 +0000224usage: trace [-h] [-b BUFFER_PAGES] [-p PID] [-L TID] [-v] [-Z STRING_SIZE]
225 [-S] [-M MAX_EVENTS] [-t] [-T] [-K] [-U] [-I header]
Mark Draytonaa6c9162016-11-03 15:36:29 +0000226 probe [probe ...]
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800227
228Attach to functions and print trace messages.
229
230positional arguments:
231 probe probe specifier (see examples)
232
233optional arguments:
234 -h, --help show this help message and exit
Mark Drayton5f5687e2017-02-20 18:13:03 +0000235 -b BUFFER_PAGES, --buffer-pages BUFFER_PAGES
236 number of pages to use for perf_events ring buffer
237 (default: 64)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800238 -p PID, --pid PID id of the process to trace (optional)
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000239 -L TID, --tid TID id of the thread to trace (optional)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800240 -v, --verbose print resulting BPF program code before executing
241 -Z STRING_SIZE, --string-size STRING_SIZE
242 maximum size to read from strings
243 -S, --include-self do not filter trace's own pid from the trace
244 -M MAX_EVENTS, --max-events MAX_EVENTS
245 number of events to print before quitting
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000246 -t, --timestamp print timestamp column (offset from trace start)
Mark Drayton5f5687e2017-02-20 18:13:03 +0000247 -T, --time print time column
Sasha Goldshtein4725a722016-10-18 20:54:47 +0300248 -K, --kernel-stack output kernel stack trace
249 -U, --user-stack output user stack trace
250 -I header, --include header
251 additional header files to include in the BPF program
ShelbyFrances69abacc2017-02-08 05:56:53 +0300252 as either full path, or relative to '/usr/include'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800253
254EXAMPLES:
255
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800256trace do_sys_open
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800257 Trace the open syscall and print a default trace message when entered
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800258trace 'do_sys_open "%s", arg2'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800259 Trace the open syscall and print the filename being opened
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800260trace 'sys_read (arg3 > 20000) "read %d bytes", arg3'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800261 Trace the read syscall and print a message for reads >20000 bytes
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000262trace 'r::do_sys_open "%llx", retval'
Mark Draytonaa6c9162016-11-03 15:36:29 +0000263 Trace the return from the open syscall and print the return value
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800264trace 'c:open (arg2 == 42) "%s %d", arg1, arg2'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800265 Trace the open() call from libc only if the flags (arg2) argument is 42
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800266trace 'c:malloc "size = %d", arg1'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800267 Trace malloc calls and print the size being allocated
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800268trace 'p:c:write (arg1 == 1) "writing %d bytes to STDOUT", arg3'
269 Trace the write() call from libc to monitor writes to STDOUT
Mark Drayton5f5687e2017-02-20 18:13:03 +0000270trace 'r::__kmalloc (retval == 0) "kmalloc failed!"'
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800271 Trace returns from __kmalloc which returned a null pointer
Mark Drayton5f5687e2017-02-20 18:13:03 +0000272trace 'r:c:malloc (retval) "allocated = %x", retval'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800273 Trace returns from malloc and print non-NULL allocated buffers
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300274trace 't:block:block_rq_complete "sectors=%d", args->nr_sector'
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800275 Trace the block_rq_complete kernel tracepoint and print # of tx sectors
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700276trace 'u:pthread:pthread_create (arg4 != 0)'
277 Trace the USDT probe pthread_create when its 4th argument is non-zero
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000278trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec'
279 Trace the nanosleep syscall and print the sleep duration in ns