Part 2 of 7 · 2 min read
What strace shows
Think of listening to the calls between a shop and its warehouse. You do not see inside either building, but every request and every answer passes through the phone, so you learn what the shop really needs. The analogy stops in one place: a phone call is heard by a person, while strace slows the program that it watches.
In plain words: strace runs a program (or attaches to a running one) and prints each system call: the name, the arguments, and the value returned. It uses the ptrace system call, which lets one process stop another at each system call entry and exit and read its registers. A line looks like:
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
The call is openat with the file name and flags, and the result is 3: the new file descriptor (Level 9). A failure shows = -1 followed by the name of the error and its text:
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
Here the program looked for a file that does not exist, and got ENOENT. Programs often try optional files, so a failed call is not always a bug.
A worked example. The error name tells the family of cause (Level 10, page 7): ENOENT (no such name), EACCES (permission refused), EPERM (not permitted), EMFILE (too many open files), EAGAIN (try again, for example a full process table), and EINTR (interrupted by a signal). A message in a log such as "cannot open file" is far less precise than openat(...) = -1 EACCES.
The key lines. strace shows what the program asks, not what the kernel does inside. It is the best first step for a program that fails without a clear message.
strace
strace is a tool that traces the system calls of a process and the signals that it receives, printing each call with its arguments and return value. It uses the ptrace system call to stop the traced process at every system call.
Example: strace -e trace=openat cat /nonexistent prints the one openat call and its ENOENT error.
Check yourself:
1. What does = -1 ENOENT at the end of a line mean?
2. Which system call does strace use to watch another process?
3. Why is a failed call not always a bug?