Tracing system calls with strace

Advanced · 16 min

Watch a program's conversation with the kernel using strace: filter with -e, follow children with -f, summarise with -c, and respect its overhead.

Before thisKernel space, user space and system callsProcesses through /procDisk and I/O bottlenecks

LaurieWired · 16 min

Tracing system calls with strace

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by LaurieWiredReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Every file opened, socket connected and process started goes through a system call. `strace` prints each one with its arguments, return value and any error, which shows what a program is actually doing.
  • Filter instead of drowning: `-e trace=openat,connect`, or classes such as `%file`, `%network` and `%process`. `-f` follows child processes and threads. `-p PID` attaches to a running one.
  • `-c` prints a table of calls, errors and time per syscall, which is the fastest way to see where a slow program spends its kernel time. `-T` shows time per call.
  • Errors are often the answer: an `ENOENT` on a config path the program was not supposed to use, an `EACCES`, or a `connect` that hangs.
  • strace stops the target twice for every syscall it intercepts. Brendan Gregg measured a worst case of 442 times slower. Use it briefly in production, with `--seccomp-bpf` to limit the stops, or reach for eBPF tools.
  • `ltrace` does the same for calls into shared libraries rather than into the kernel.

When logs say nothing

A program hangs, or exits with “error”, or is slow for no visible reason, and its logs say nothing useful. You do not have its source code, or you do but it is a million lines. There is still one place where every program has to show its hand: the system call boundary from kernel space, user space and system calls. To open a file, read a socket, start a child or sleep, it must ask the kernel.

strace sits on that boundary and prints every request. Think of listening to one side of a phone call between the program and the kernel: you hear every question and every answer, without needing to understand how the caller thinks.

The output

Each line is a call, its arguments, and the result after =. A failed call ends with -1, an error name and its description. For illustration, a failed open has this shape. It is not captured output:

openat(AT_FDCWD, "/etc/myapp/config.yaml", O_RDONLY) = -1 ENOENT (No such file or directory)

That line alone settles many “config not found” mysteries, because it shows the path the program really tried. Strings are cut at 32 characters by default, and -s 256 shows more.

The options that matter

From the strace man page (strace 7.1 development snapshot, checked 2026-11-28):

strace -f -e trace=%file ./service          # files touched, including children
strace -f -e trace=%network -p 1234         # attach to a running process
strace -c -f ./build.sh                     # summary table at exit
strace -f -tt -T -o /tmp/trace.txt ./job    # timestamps, time per call, to a file
strace -Z -f ./service                      # only calls that failed
  • -f “traces child processes as they are created”. Without it, you see only the first process, which is useless for shell scripts and multi-threaded servers.
  • -e trace= limits the trace to named calls or classes: %file for anything taking a filename, %network, %process for fork, exec and exit.
  • -p PID attaches to something already running. Ptrace permissions apply, so you usually need sudo for another user’s process.
  • -c “counts time, calls, and errors for each system call and report a summary on program exit.” -C does the same and also prints the normal lines.
  • -T shows the time spent in each call, and -tt adds wall-clock timestamps. Together they show which call the program was stuck in, for example a connect that took 3 seconds or a futex wait.
  • -y prints the path behind each file descriptor, so read(3, ...) becomes read(3</var/lib/app/data.db>, ...).
  • -z and -Z show only successful or only failed calls.

A troubleshooting pattern

For a slow or hanging program:

  1. Get the overview: strace -c -f -p PID for ten seconds, then Ctrl-C. Which calls dominate the count and the time?
  2. Zoom in: strace -f -T -e trace=<those calls> -p PID. Which individual calls are slow, and on which file or socket (-y)?
  3. Connect it to the system: a slow fsync points at storage from the disk lesson, a slow connect at the network, and long futex waits at lock contention inside the program, which strace cannot see into.

The cost

strace uses the ptrace interface, which stops the traced process at the entry and the exit of each intercepted syscall and switches to strace each time. Brendan Gregg measured a deliberately syscall-heavy dd running 442 times slower under strace. His point was that even tracing just one unrelated syscall did not help, because the process still stopped at every call. The current man page adds --seccomp-bpf, which makes the kernel stop the process only for the syscalls you asked to trace. Use it together with -f, since the man page says it has no effect without -f. Even so, the man page is plain that “a traced process runs more slowly”. On a production server, keep traces short and filtered. For anything longer, the eBPF tools in the eBPF lesson give similar answers at far lower cost.

ltrace

ltrace works one layer up. It intercepts calls from a program into shared libraries, from the shared libraries lesson, such as strlen, malloc or SSL_read. It is useful when the behaviour you care about never reaches the kernel. It only sees dynamically linked calls and adds overhead of its own.

macOS has no strace, and there is no Linux machine here, so nothing was run. The options are from the strace man page on man7.org and the overhead figures from Gregg’s article, both read on 2026-11-28.

About the video

The embed is all of LaurieWired’s “Using Strace to Trace Linux Syscalls”, July 2023, about 16 minutes. Its chapters cover reading the man page, tracing a small C program, reading the output, filtering, excluding calls, counting with -c, and following child processes and forks. It approaches strace as a developer and reverse engineer would, which complements this lesson’s admin angle. It does not, going by its chapters, discuss overhead in production. Jacob Sorber’s four-minute comparison of strace and ltrace is in deeper. I judged it from its chapters and description, and English from its English caption track.

Where this fits

strace answers “what is it asking the kernel?”. To find where the CPU time goes inside the program, you sample it: profiling with perf and flame graphs.

Check yourself

  1. 01A service fails at start-up with 'config not found', but the file exists. How can strace show what is wrong?

    Run it under `strace -f -e trace=%file` and look for `openat` calls returning `ENOENT` or `EACCES`. The path it actually tries, or the permission error, is usually the answer.

  2. 02What does `strace -c -f ./build.sh` give you that plain strace does not?

    A summary table per system call, with call counts, errors and time spent, across the script and all its children. That shows the expensive calls without reading thousands of lines.

  3. 03Why can attaching strace to a busy production database cause an incident?

    strace uses ptrace, which stops the process on entry to and exit from each traced syscall. A syscall-heavy process can slow down by orders of magnitude, which can trip health checks or failovers.

  4. 04When would you use ltrace instead of strace?

    When the interesting work happens in library calls, such as string handling or crypto functions in libc or libssl, rather than in system calls.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark