8.2. Using strace
strace utility on a program you want to analyze, type the following at a shell prompt:
scl enable devtoolset-6 'strace program [argument...]'-p command line option followed by the process ID:
scl enable devtoolset-6 'strace -p process_id'scl utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset strace as default:
scl enable devtoolset-6 'bash'Note
strace you are using at any point, type the following at a shell prompt:
which stracestrace executable path will begin with /opt. Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset strace:
strace -V8.2.1. Redirecting Output to a File
strace prints the name of each system call, its arguments and the return value to standard error output. To redirect this output to a file, use the -o command line option followed by the file name:
scl enable devtoolset-6 'strace -o file_name program [argument...]'Example 8.1. Redirecting Output to a File
fibonacci file from Example 7.1, “Compiling a C Program With Debugging Information”. This executable file displays the Fibonacci sequence and optionally allows you to specify how many members of this sequence to list. To run the strace utility on this file and redirect the trace output to fibonacci.log, type:
~]$ scl enable devtoolset-6 'strace -o fibonacci.log ./fibonacci 20'
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765fibonacci.log in the current working directory.
8.2.2. Tracing Selected System Calls
strace utility with the -e command line option:
scl enable devtoolset-6 'strace -e expression program [argument...]'Table 8.1. Commonly Used Values of the -e Option
| Value | Description |
|---|---|
file | System calls that accept a file name as an argument. |
process | System calls that are related to process management. |
network | System calls that are related to networking. |
signal | System calls that are related to signal management. |
ipc | System calls that are related to inter-process communication (IPC). |
desc | System calls that are related to file descriptors. |
Example 8.2. Tracing Selected System Calls
employee file from Example 10.1, “Using memstomp”. To run the strace utility on this executable file and trace only the mmap and munmap system calls, type:
~]$ scl enable devtoolset-6 'strace -e mmap,munmap ./employee'
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f896c744000
mmap(NULL, 61239, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f896c735000
mmap(0x3146a00000, 3745960, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3146a00000
mmap(0x3146d89000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x189000) = 0x3146d89000
mmap(0x3146d8e000, 18600, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x3146d8e000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f896c734000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f896c733000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f896c732000
munmap(0x7f896c735000, 61239) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f896c743000
John,john@example.comDoe,
+++ exited with 0 +++8.2.3. Displaying Time Stamps
strace utility with the -t command line option:
scl enable devtoolset-6 'strace -t program [argument...]'-t option twice:
scl enable devtoolset-6 'strace -tt program [argument...]'-r command line option:
scl enable devtoolset-6 'strace -r program [argument...]'Example 8.3. Displaying Time Stamps
pwd. To run the strace utility on this file and include time stamps in the output, type:
~]$ scl enable devtoolset-6 'strace -tt pwd'
19:43:28.011815 execve("./pwd", ["./pwd"], [/* 36 vars */]) = 0
19:43:28.012128 brk(0) = 0xcd3000
19:43:28.012174 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc869cb0000
19:43:28.012427 open("/etc/ld.so.cache", O_RDONLY) = 3
19:43:28.012446 fstat(3, {st_mode=S_IFREG|0644, st_size=61239, ...}) = 0
19:43:28.012464 mmap(NULL, 61239, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fc869ca1000
19:43:28.012483 close(3) = 0
...
19:43:28.013410 +++ exited with 0 +++8.2.4. Displaying a Summary
strace utility with the -c command line option:
scl enable devtoolset-6 'strace -c program [argument...]'Example 8.4. Displaying a Summary
lsblk. To run the strace utility on this file and display a trace summary, type:
~]$ scl enable devtoolset-6 'strace -c lsblk > /dev/null'
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
80.88 0.000055 1 106 16 open
19.12 0.000013 0 140 munmap
0.00 0.000000 0 148 read
0.00 0.000000 0 1 write
0.00 0.000000 0 258 close
0.00 0.000000 0 37 2 stat
...
------ ----------- ----------- --------- --------- ----------------
100.00 0.000068 1790 35 total
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.