The strace is a powerful command-line tool for process monitoring, diagnostic and troubleshooting programs in Linux. Generally, it is used to intercept and record the system calls and the signals received by a process. You can use strace to analyze how a program interacts with the system to debug any program.
This tool is very useful if the program continually crashes, or does not behave as expected. It provides deep insight into how the system operates. Any user may trace their own running processes.
In this tutorial, we will show you how to use the strace command-line tool on Linux.
Install Strace
By default, strace is available in the default repository of all Linux operating systems.
On Debian and Ubuntu operating systems, install the strace with the following command:
apt-get install strace -y
On RHEL and CentOS operating systems, install the strace with the following command:
dnf install strace -y
After installing strace, you can verify the strace version using the following command:
strace -V
You should get the following output:
strace -- version 4.8
You can print all options available with strace command with the following command:
strace -help
Output usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]... [-a column] [-o file] [-s strsize] [-P path]... -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS] or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby] -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS] -c -- count time, calls, and errors for each syscall and report summary -C -- like -c but also print regular output -d -- enable debug output to stderr -D -- run tracer process as a detached grandchild, not as parent -f -- follow forks, -ff -- with output into separate files -i -- print instruction pointer at time of syscall -q -- suppress messages about attaching, detaching, etc. -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs -T -- print time spent in each syscall -v -- verbose mode: print unabbreviated argv, stat, termios, etc. args -x -- print non-ascii strings in hex, -xx -- print all strings in hex -y -- print paths associated with file descriptor arguments -h -- print help message, -V -- print version -a column -- alignment COLUMN for printing syscall results (default 40) -b execve -- detach on this syscall -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]... options: trace, abbrev, verbose, raw, signal, read, write -I interruptible -- 1: no signals are blocked 2: fatal signals are blocked while decoding syscall (default) 3: fatal signals are always blocked (default if '-o FILE PROG') 4: fatal signals and SIGTSTP (^Z) are always blocked (useful to make 'strace -o FILE PROG' not stop on ^Z) -o file -- send trace output to FILE instead of stderr -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs -p pid -- trace process with process id PID, may be repeated -s strsize -- limit length of print strings to STRSIZE chars (default 32) -S sortby -- sort syscall counts by: time, calls, name, nothing (default time) -u username -- run command as username handling setuid and/or setgid -E var=val -- put var=val in the environment for command -E var -- remove var from the environment for command -P path -- trace accesses to path
1. Trace a System Calls
If you want to trace the system calls of the command ls, run the following command:
strace ls file1.txt
Output execve("/bin/ls", ["ls", "file1.txt"], [/* 61 vars */]) = 0 brk(0) = 0x1f2a000 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4dd0d30000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=114633, ...}) = 0 mmap(NULL, 114633, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f4dd0d14000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) open("/lib/x86_64-linux-gnu/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3 read(3, "177ELF211 3 >