spying-on-your-program-with-strace.md

← Writing Desktop 8 min read

Spying on your program with strace

How to see which files, networks and commands a Linux program really touches, without reading its source code.

Created
Nov 29, 2026
Reading
8 min
Labels
linuxdebugging

Some programs fail with a message that tells you nothing. “Config not found.” Found where? “Connection refused.” To what? You can read the source, if you have it and if you know the language. Or you can ask the operating system what the program actually did.

That is what strace does. It works on Linux, it works on any program, and you do not need to know what the program was written in.

What it shows you

A program cannot touch the disk, the network or memory by itself. It has to ask the kernel, and each of those requests is called a system call. Open a file, read from it, connect to a server, start another program: all of these are system calls.

strace runs your program and prints every request it makes, along with the kernel’s answer. Python, Ruby, Go or a binary someone handed you, it makes no difference. They all end up asking the kernel the same way.

Reading one line

Start small:

$ strace -e trace=write echo hello
write(1, "hello\n", 6hello
)                  = 6

The program’s own output got mixed into the middle, which looks messy but is normal. The trace line itself has three parts:

  • write is the system call.
  • (1, "hello\n", 6) are its arguments: file descriptor 1 (standard output), the bytes to write, and how many.
  • = 6 is what the kernel returned. Here it means 6 bytes were written.

For anything that can fail, the end of the line matters most:

openat(AT_FDCWD, "./app.toml", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

A return of -1 followed by an error name means the call failed. ENOENT means the file does not exist. When you meet a system call you do not know, man 2 openat explains it. You can get a long way knowing only openat, read, write, connect and execve.

Where does it look for its config?

Say a script prints “no config found” and nothing else. This one checks three places:

$ strace -f -e trace=openat python3 app.py 2>&1 | grep -E "app.toml|no config"
openat(AT_FDCWD, "./app.toml", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/root/.config/app/app.toml", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/app/app.toml", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
no config found

Now you know the search order and the exact paths it gave up on. I used grep because Python opens hundreds of files while starting up. Filter the output or you will never find the line you want.

One trap. Older guides tell you to use -e trace=open. Modern Linux programs mostly call openat instead, so that filter can show you nothing at all. -e trace=file catches every file-related call and is safer.

Where is it connecting?

Same idea for the network:

$ strace -f -e trace=connect curl -s -o /dev/null http://example.com
connect(5, {sa_family=AF_INET6, sin6_port=htons(80), ..., "2606:4700:10::6814:179a", ...}, 28) = -1 ENETUNREACH (Network is unreachable)

I trimmed that line. You get the address family, the port (80), the address and the result. In this run curl tried an IPv6 address first and the container had no route to it. The same filter shows which DNS server a program asks, because that is just a connect to port 53.

If you only want the network, -e trace=network is the group for it.

Why is it stuck?

A program that hangs is the best use of strace -p, which attaches to a process that is already running:

$ strace -p 98
strace: Process 98 attached
restart_syscall(<... resuming interrupted clock_nanosleep ...>

The last line is what the process is waiting on right now. Here it is sleeping. A read on a socket that never returns means it is waiting for data from somewhere. A futex usually means it is waiting on a lock. Often that is enough to tell you which part of the code to look at.

To find slow calls instead of stuck ones, add -T. It prints how long each call took at the end of the line:

$ strace -T -e trace=clock_nanosleep sleep 1
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=1, tv_nsec=1}, ...) = 0 <1.005267>

Flags worth remembering

Flag What it does
-f Also trace child processes. Without it you only see the first process, and a lot of programs start others.
-e trace=file Show only file calls. Also network, process, or a list like openat,read.
-o out.txt Write the trace to a file. The output is huge, so do this by default.
-p PID Attach to a running process.
-s 200 Show up to 200 characters of each string. The default cuts them at 32.
-y Show the file name next to each file descriptor.
-c Print a summary of calls, counts and errors when the program ends.
-T Show time spent in each call. -tt adds timestamps.

Three of them are easier to see than to read about.

-s decides how much of a string you get to see:

$ strace -e trace=write -s 5 echo "a long sentence here"
write(1, "a lon"..., 21a long sentence here
$ strace -e trace=write -s 100 echo "a long sentence here"
write(1, "a long sentence here\n", 21a long sentence here

-y turns bare numbers into names:

read(3</etc/hostname>, "c352877c6b2d\n", 262144) = 13

And -c is a quick way to see where a program spends its effort. This is ls /etc:

% time     seconds  usecs/call     calls    errors syscall
 86.89    0.000053          26         2           getdents64
  8.20    0.000005           0        23           close
  0.00    0.000000           0        35        14 openat

Look at the errors column. Fourteen of the 35 openat calls failed. Most programs do that on purpose while they search for files, so a failure is not always a problem.

See the exact command a script ran

With -f and -e trace=execve you can watch a program start other programs:

$ strace -f -e trace=execve sh -c 'ls /tmp > /dev/null'
execve("/usr/bin/sh", ["sh", "-c", "ls /tmp > /dev/null"], ...) = 0
[pid   126] execve("/usr/bin/ls", ["ls", "/tmp"], ...) = 0

If a script builds a command out of variables and it does the wrong thing, this shows you the command after all the substitution, with every argument separated. Reading that is usually faster than reading the script.

Things to watch out for

It slows the program down. strace stops the process at every system call. A busy service can get many times slower. Do not point it at a production database or a server under load. On newer kernels perf trace or bpftrace are cheaper options, though harder to learn.

Traces contain data. Passwords, tokens and request bodies pass through read and write. Look at a trace before you paste it into a ticket or a chat.

It needs permission. Tracing your own program is fine. Attaching to someone else’s process usually needs sudo. In Docker you may need --cap-add SYS_PTRACE, which I needed to produce the examples here.

It is Linux only. macOS has dtruss, but it is fiddly to get working. A Linux container is the easy way to practise.

Try it

You need very little to start. Run strace -c ls and read the table. Then trace a program you already use and look at which files it opens. If you hit an error you cannot explain, trace the program and search the output for -1.

The examples above come from strace 6.13 running in a Debian container.