Trace Every System Call a Program Makes With strace
Run a program under strace, read each line as name, arguments and result, then filter, follow child processes and write the trace to a file to find real faults.

When a program fails with a vague message, or hangs without any message at all, the kernel often knows the reason. Every file the program tried to open, every connection it tried to make and every permission it was refused passed through a system call. strace records those calls, and reading its output is one of the fastest ways to see what a program is actually doing, as opposed to what its source suggests. This post works through a small failing program on Linux with kernel 6.12 and strace 6.11, then covers the options you will use most.
A program that fails quietly
Here is a program that tries to load a configuration file and falls back to a default if it cannot:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *f = fopen("/etc/acme/app.conf", "r");
if (!f) {
perror("config");
return EXIT_FAILURE;
}
puts("config loaded");
fclose(f);
return EXIT_SUCCESS;
}config: No such file or directoryThe message says the file does not exist, but not which file, because the program printed only the generic reason. In a real program that buries the path in a long list of defaults, you may not know which path it tried. strace shows it.
Running a command under strace
Prefix the command with strace:
strace ./config_demoThe first lines come from the dynamic loader and the C library starting the process, which is normal. The part that matters is near the end:
execve("./config_demo", ["./config_demo"], 0x7ffd1c0a4e20 /* 34 vars */) = 0
brk(NULL) = 0x55c8c2a7e000
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
...
openat(AT_FDCWD, "/etc/acme/app.conf", O_RDONLY) = -1 ENOENT (No such file or directory)
newfstatat(2, "", {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0), ...}, AT_EMPTY_PATH) = 0
write(2, "config: No such file or directory\n", 34config: No such file or directory
) = 34
exit_group(1) = ?
+++ exited with 1 +++The addresses and the number of lines in the preamble will differ on your system. The shape of every line is the same, and once you can read one you can read all of them.
How to read a line
Each line is name(arguments) = result. Take the failing one:
openat(AT_FDCWD, "/etc/acme/app.conf", O_RDONLY) = -1 ENOENT (No such file or directory)openatis the system call. glibc'sfopencallsopen, and glibc implementsopenwithopenat, so that is what the kernel sees.AT_FDCWDmeans the path is relative to the current directory when it is not absolute. Here it is absolute, so it is irrelevant."/etc/acme/app.conf"is the path, decoded from the pointer the program passed.O_RDONLYis the flag word, shown by name instead of as a number.= -1 ENOENT (No such file or directory)is the result. A failing call prints-1, the error name and its description.
A successful call prints the return value. openat(...) = 3 means the kernel returned file descriptor 3, and later lines such as read(3, ...) refer to it. strace does not guess what the program meant; it shows the exact request and the exact answer, which is why it settles arguments about what went wrong.
Narrowing the output
A real program makes thousands of calls. Four options keep the noise down.
| Option | What it does |
|---|---|
-e trace=openat,read | Show only the named calls |
-e trace=file | Show every call that takes a file name |
-e trace=network | Show every socket-related call |
-o trace.txt | Write the trace to a file instead of the terminal |
Using the file group is the quick way to find the path a program is hunting for:
strace -e trace=file -o trace.txt ./config_demo
grep ENOENT trace.txtopenat(AT_FDCWD, "/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/acme/app.conf", O_RDONLY) = -1 ENOENT (No such file or directory)The first line is normal: the dynamic loader checks for an optional preload file. The second is the program's real problem, a missing configuration file at an exact path. Writing to a file with -o also keeps the program's own output from tangling with the trace, which is the reason the combined output above looked untidy.
The group names, such as file, network, process and memory, are documented in the strace manual page. They save you from listing every open variant by hand.
Seeing arguments in full
By default strace truncates strings to 32 bytes, which hides the interesting part of a long write or read. The -s option raises the limit:
strace -s 200 -e trace=write ./config_demoTwo more options help when the arguments are file descriptors. -y appends the path to each descriptor, so read(3</etc/hostname>, ...) explains itself. -T appends the time spent inside each call, which is how you find the call that is blocking. It prints the time in seconds in angle brackets at the end of the line, for example <0.000134>. Those numbers depend on the machine and the moment, so treat them as a way to compare one call with another in the same trace.
Following child processes
Programs that start other programs are traced one process at a time by default. A shell script that runs five commands would show only the shell. The -f flag follows every child created by fork, vfork or clone:
strace -f -e trace=execve -o spawn.txt sh -c 'ls /etc/acme; date'2841 execve("/bin/sh", ["sh", "-c", "ls /etc/acme; date"], 0x7ffc1a2b3c40 /* 34 vars */) = 0
2842 execve("/usr/bin/ls", ["ls", "/etc/acme"], 0x55e0c3b5d1a0 /* 34 vars */) = 0
2843 execve("/usr/bin/date", ["date"], 0x55e0c3b5d1a0 /* 34 vars */) = 0With -f, each line starts with a process id. The numbers will differ, but the structure shows you the tree of commands a script starts. This is the most direct way to answer "what does this installer actually run?".
Attaching to a running process
If a service is already running and seems stuck, attach to it with -p:
sudo strace -p 2841 -f -TA hung process is usually waiting inside one call. The trace stops on a line such as futex(0x7f12a8001e40, FUTEX_WAIT_PRIVATE, 0, NULL or read(5, with no result printed yet. That unfinished call is the answer: it is waiting on a lock, a socket or a pipe, and the descriptor or address tells you which. Press Ctrl-C to detach; the program carries on.
Attaching needs permission to trace the target. By default, Linux's Yama module restricts ptrace to a process's own children, controlled by /proc/sys/kernel/yama/ptrace_scope, which is why attaching to an existing process usually needs sudo. Tracing also slows the target down, since strace stops it twice per call, so use it on production services briefly.
A summary instead of a trace
For a quick profile, -c counts calls and prints a table when the program ends:
strace -c ./config_demo% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
31.25 0.000050 50 1 write
25.00 0.000040 40 1 1 openat
...
------ ----------- ----------- --------- --------- ----------------
100.00 0.000160 3 42 3 totalThe timings will differ on every run. The column to read is errors: it shows which calls fail, and how often. A program that makes ten thousand failing stat calls looking for a file is easy to spot here, long before you read a trace line by line.
What strace cannot tell you
strace shows the boundary between the program and the kernel and nothing else. It does not show which source line made a call, what happens inside the program between calls, or calls that never reach the kernel, such as reads served from a library's own cache or time queries answered by the vDSO. For function-level questions use a debugger or ltrace. And a trace changes timing, so a race condition may vanish while you watch it.
When strace cannot attach or changes the result
Three situations surprise people. Inside a container, strace needs the CAP_SYS_PTRACE capability and a seccomp profile that allows the ptrace call, and many default profiles refuse it, so the command fails with Operation not permitted even for root. Setuid programs run with extra privileges that a tracer must not inherit, so tracing one as an ordinary user drops the setuid effect and the program behaves differently from the real run. And a process can be traced by only one tracer at a time, so strace fails on a program already under a debugger, with ptrace: Operation not permitted or Device or resource busy.
Tracing also changes timing. Every call is stopped twice, so a program that is time-sensitive may behave differently, and a race condition may disappear. When you trace a program that starts many children, add -ff together with -o prefix to write one file per process, named by process id, so the traces do not interleave. For a program that you cannot afford to slow down, use perf trace, which reads the same events through the kernel's tracepoints.


