strace — What the Program Asked the Kernel, and What It Was Told

ByLinuxtastic

November 29, 2026

A program says Permission denied and does not say to what. A service starts, prints nothing, and exits 1. A backup job has been sitting at the same line for forty minutes and nobody knows whether it is working or stuck. In each case the program knows exactly what happened, because the kernel told it, and it chose not to pass the message on. strace sits between the two and writes down every request the program makes to the kernel and every answer it gets back.

strace -f -yy -s 200 -o trace.txt ./the-program --its-args
less trace.txt

That is the invocation to start from. -f follows child processes, which is nearly always what you want; -yy prints the path or socket behind every file descriptor number; -s 200 stops strings being cut at the default 32 characters; -o keeps the trace out of the program’s own output. Each line of the result is one system call: its name, its arguments, an equals sign, and what the kernel returned.

openat(AT_FDCWD, "/etc/hostname", O_RDONLY) = 3</etc/hostname>
read(3</etc/hostname>, "vm\n", 131072)      = 3
openat(AT_FDCWD, "/etc/nosuchfile", O_RDONLY) = -1 ENOENT (No such file or directory)

A return of -1 followed by an errno name is the kernel saying no, and the name is the whole diagnosis: ENOENT is a path that does not exist, EACCES a permission problem, ECONNREFUSED nothing listening, EAGAIN a non-blocking call that would have waited. The program’s error message, if it printed one at all, was a translation of that name; strace gives you the original and the argument it applied to. The Life of a System Call is the page on what happens between the two sides of that equals sign.

The cost, measured

strace stops the traced program twice for every system call, and on a busy program that is not a small tax. On the machine used for this page (kernel 6.18, strace 6.8), piping 400 MB of zeros through cat in 4 KB reads took 0.25 seconds; the same pipeline under a plain strace took 12.0 seconds, forty-eight times slower. Attach that to a production database or a web server under load and you have caused the outage you were investigating. The fix is to never trace unfiltered: strace -f --seccomp-bpf -e trace=openat on the same pipeline took 0.23 seconds, because with a filter and --seccomp-bpf the kernel only stops the program for the calls you asked about and lets the rest run at full speed. Those numbers are one machine’s; the ratio is what to carry away, and the command below will give you your own.

time (dd if=/dev/zero bs=4k count=100000 2>/dev/null | cat >/dev/null)
time (strace -o /dev/null dd if=/dev/zero bs=4k count=100000 2>/dev/null | cat >/dev/null)
time (strace -f --seccomp-bpf -e trace=openat -o /dev/null dd if=/dev/zero bs=4k count=100000 2>/dev/null | cat >/dev/null)

The three questions it answers best

Which file did it actually try to open?

Configuration that is “not being picked up” is almost always being read from somewhere other than where you edited it. Trace only the file-related calls and only the ones that failed, and the search order falls out in the open:

$ strace -e trace=%file -Z git config --get user.name
access("/root/.config/git/config", R_OK) = -1 ENOENT (No such file or directory)
access("/root/.gitconfig", R_OK)        = -1 ENOENT (No such file or directory)

%file is a class covering every call that takes a pathname; -Z prints only calls that failed. Between them they turn a 400-line trace into the four lines that matter. The same pair finds the missing shared library behind a cryptic start-up failure, the .so looked for in six directories and found in none.

Where is it stuck?

Attach to a running process with -p. If it is blocked, the first thing printed is the call it is blocked in, and that call names what it is waiting for:

$ strace -p 864
strace: Process 864 attached
read(0,  ^C
strace: Process 864 detached
 <detached ...>

A read on descriptor 0 is a program waiting for input that is not coming; read on a socket is a remote end that has gone quiet; wait4 is a parent waiting for a child; futex is a thread waiting for another thread, which usually means the interesting thread is a different one — add -f, or attach to the specific thread ID from ls /proc/PID/task. -yy turns the descriptor number into the file or the TCP four-tuple, so you do not have to go and look it up in lsof. Press Ctrl-C to detach; the program carries on as if nothing happened.

What did the children do?

Most of what a shell script, a build or a service manager does happens in processes it starts. -f follows them and prefixes each line with the PID; -e trace=execve on its own gives you the tree of commands that actually ran, with their real arguments after every expansion:

$ strace -f -e trace=execve sh -c 'echo hi | wc -c'
execve("/usr/bin/sh", ["sh", "-c", "echo hi | wc -c"], 0x7ffe7af58218 /* 225 vars */) = 0
strace: Process 726 attached
strace: Process 727 attached
[pid   727] execve("/usr/bin/wc", ["wc", "-c"], 0x55bab115df28 /* 225 vars */) = 0

Notice that echo never appears: it is a shell builtin, and the trace proves it. When the children are many, -ff -o trace writes one file per process, trace.PID, which is far easier to read than an interleaved log.

The flags that matter

FlagDoes
-fFollow forks, clones and threads. Almost always wanted
-p PIDAttach to a running process instead of starting one. Several -p are allowed
-o FILEWrite the trace to a file; with -ff, one file per process
-e trace=SETOnly these calls: names, or a class such as %file, %network, %desc, %process, %memory, %signal
-Z / -zOnly failed calls / only successful calls
-y / -yyShow the path behind each descriptor / also sockets as protocol and addresses
-s NPrint up to N characters of each string (default 32)
-x / -xxNon-ASCII bytes in hex / every byte in hex
-TTime spent inside each call, in angle brackets at the end of the line
-t / -tt / -rWall-clock timestamp / with microseconds / time since the previous line
-cCount calls, errors and time per call and print a table at the end; -C prints the trace too
-kA stack trace after each call: which function in the program made it
--seccomp-bpfWith -f and a -e trace= filter, have the kernel skip the calls you did not ask for
-e inject=CALL:error=ERRNOMake a call fail on purpose, to test the program’s error path
-u USERRun the program as another user (as root), useful for setuid programs

Timing: slow, or waiting?

A program that is slow in user space shows a trace of quick calls with large gaps between them; a program waiting on the kernel shows a few calls that each take a long time. -r prints the gap before each line and -T prints the time inside each call, which is enough to tell the two apart:

$ strace -r -T -e trace=clock_nanosleep sleep 0.3
     0.000000 clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=300000000}, 0x7ffe6d965930) = 0 <0.300435>

-c is the summary form: a table of which calls ran, how often, and how many failed. It is a good first look at an unfamiliar program, and the errors column is often the answer by itself. Read the times in that table with the box above in mind: they are times under tracing, and the tracing is most of them.

Attaching, and who is allowed to

Tracing is done with ptrace, and the kernel’s Yama module restricts it. On most distributions /proc/sys/kernel/yama/ptrace_scope is 1, which means an unprivileged user can trace only processes that are their own descendants: starting a program under strace works, attaching to an already-running one with -p does not, even when it is your own process. Use sudo strace -p, and expect -p on a service to need root regardless, because the service is not yours.

Setuid programs have their own rule. An unprivileged user can run strace sudo or strace passwd, but the kernel quietly runs the program without its elevated privileges while it is being traced, so the behaviour you see is not the behaviour the program normally has. Trace setuid programs as root, with -u to run them as the user in question.

For a systemd service, attach to the main process rather than restarting it under strace: sudo strace -f -p "$(systemctl show -p MainPID --value nginx)". If the problem is at start-up and you must trace from the first instruction, run the service’s command line by hand under strace or, for one run only, systemctl edit --runtime an ExecStart= that wraps it, with a -o file and a filter, and remove the override afterwards.

Making a call fail on purpose

Error paths are the least-tested code in any program, and strace can exercise them without touching the filesystem:

$ strace -e trace=openat -e inject=openat:error=EACCES:when=3 cat /etc/hostname
openat(AT_FDCWD, "/etc/hostname", O_RDONLY) = -1 EACCES (Permission denied) (INJECTED)
cat: /etc/hostname: Permission denied

when=3 fails the third matching call and leaves the rest alone, which is how you get past the loader’s own opens to the one the program makes. delay_enter= and delay_exit= inject latency the same way, for testing what a program does when a disk or a network is slow rather than broken.

When strace is the wrong tool

It sees system calls and nothing else. A program spinning in its own code, a lock contended inside a library, a slow SQL query that the client is patiently waiting for over one socket: all of those show as a trace with nothing happening in it. For library-level calls, ltrace; for “what is this process doing right now” with negligible overhead on a production machine, perf trace or bpftrace; for a program’s own logic, a debugger. strace -k is the bridge between the layers: a stack trace after each call shows which function in the program asked for it, which is often enough to go straight to the code.

Common problems

SymptomCauseFix
strace: attach: ptrace(PTRACE_SEIZE, PID): Operation not permittedYama ptrace_scope is 1 and the process is not your descendant, or it belongs to another usersudo strace -p PID
The trace is enormous and the program crawlsNo filter: every call stops the program twice-e trace= the class you care about, add --seccomp-bpf, write to -o
Strings end in "... just before the interesting partDefault -s 32-s 500, or -s 0 to suppress strings entirely when you only want the calls
The failing call is in a child you cannot seeNo -f-f; with many children, -ff -o trace
Everything is futex(...) and nothing elseYou attached to a thread that is waiting on another thread-f -p PID, or attach to the thread IDs in /proc/PID/task/
A setuid program behaves differently under straceTracing by an unprivileged user strips the setuid privilegeTrace it as root: sudo strace -u USER program
Output of the program and of strace are mixed togetherBoth go to the terminal; strace writes to stderr-o trace.txt
-c shows nothing while the program runs-c prints only the summary, at exit-C for the trace and the summary, or just wait

Quick reference

strace -f -yy -s 200 -o trace.txt CMD           # the default: everything, to a file
strace -e trace=%file -Z CMD                   # which path did it fail to find?
strace -e trace=%network -yy CMD               # what did it connect to?
sudo strace -f -p PID                          # where is it stuck right now?
strace -f -e trace=execve CMD                  # what commands did it really run?
strace -c -f CMD                               # which calls, how many, how many failed
strace -r -T -e trace=read,write CMD           # gaps between calls vs time inside them
strace -f --seccomp-bpf -e trace=openat CMD    # filtered, at near-native speed
strace -e inject=openat:error=ENOSPC:when=5 CMD   # make the fifth openat fail

Related reading

  • The Life of a System Call — what a line of strace output is a record of, from the instruction to the return value
  • lsof — the descriptor table from the outside, when you want the files without the calls
  • The Life of a File Descriptor — why the numbers in read(3, …) mean what they mean
  • Signals — the --- SIGCHLD {…} --- lines, and what a program can and cannot do about them
  • How a Program Runs — the execve, mmap and openat of shared libraries that open every trace
  • Processes — finding the PID to attach to, and reading its state before you do