Skip to content

strace: find out what a hung process is actually doing

A practitioner guide to using strace on a hung process: attach with -p, read the parked syscall, follow forks with -f, filter with -e trace=, and diagnose missing configs, network stalls, and lock contention.

A process that is “hung” is almost never doing nothing. It is sitting inside one system call, waiting for something that has not arrived — a byte on a socket, a lock another thread holds, a reply from a database that will never answer. strace shows you that call by name, with its arguments, and the moment you read it the mystery usually evaporates. Attach to the process, look at the last line printed, and the last line is the diagnosis: futex(... means it is waiting on a lock, connect(... to an address that never returns means the network is eating your packets, and a loop of openat(...) = -1 ENOENT means it is hunting for a config file that is not where it thinks it is. That is the whole technique. Everything else in this guide is about narrowing the firehose down to the line you need.

What you’ll accomplish

  • Attach to a running, unresponsive process and identify the exact syscall it is parked in.
  • Read that syscall’s arguments to name the file, socket, or address involved.
  • Follow forks and threads with -f so you trace the process that actually does the work.
  • Cut the output down with -e trace= and get a per-syscall cost table with -c.
  • Recognize the four or five failure shapes that cover most real hangs, and know when strace is the wrong tool entirely.

Prerequisites

  • strace installed: apt install strace, dnf install strace, or zypper install strace. Check with strace -V; anything from 4.x on behaves as described here, and a few notes below call out 5.x-only flags.
  • A Linux host. strace is Linux-specific — it is built on the ptrace syscall. On macOS the analogue is dtruss, and on FreeBSD it is truss.
  • Permission to trace the target. You can trace a process you own; to trace another user’s process, or one that has changed its privileges, you need root or the CAP_SYS_PTRACE capability.
  • Yama’s ptrace scope must allow it. Many distributions ship kernel.yama.ptrace_scope = 1, which permits tracing only your own direct descendants — so attaching to an already-running daemon fails even as the same user. Check it:
cat /proc/sys/kernel/yama/ptrace_scope
1

A value of 0 allows any same-user attach, 1 restricts to descendants, 2 requires admin, and 3 disables ptrace until reboot. Using sudo sidesteps levels 0 through 2.

  • In containers, ptrace is blocked unless you grant the capability. For Docker, start the container with docker run --cap-add=SYS_PTRACE .... In Kubernetes, add it to the container’s securityContext:
securityContext:
  capabilities:
    add: ["SYS_PTRACE"]

How it works

Your program does almost nothing interesting on its own. Every time it needs the outside world — a file, a socket, memory, the clock — it has to ask the kernel by making a system call. That boundary between user space and the kernel is a chokepoint, and it is the only place a process can block. If a process is stuck, it is stuck at that line.

strace does not sit inside your program and it does not patch anything. It uses ptrace, a kernel facility for debuggers, to register itself as your process’s tracer. From then on the kernel stops the traced process twice for every syscall — once on the way in, so strace can read the call number and arguments out of the registers, and once on the way out, so it can read the return value — waking strace at each stop.

strace attached via ptrace to a process, with the kernel stopping the process on syscall entry and exit at the user/kernel boundary

Two consequences fall out of that picture, and both matter. First, strace sees calls into the kernel and nothing else: a pure computation loop, a function call, or a malloc that never reaches the kernel is invisible to it. Second, every traced syscall now costs two extra context switches, which is why tracing is expensive — more on that below.

Step-by-step

1. Find the process. Get the PID of the thing that is stuck. pidof is fine for a named binary; ps gives you more context when several processes share a name:

pidof myapp
4127

2. Attach and read the last line. This is the move that solves most hangs. -p attaches to a running process; strace prints each syscall as it happens, then sits quietly on the one that is blocked. Press Ctrl-C to detach — the process keeps running:

sudo strace -p 4127
strace: Process 4127 attached
futex(0x7f9c4c0a9128, FUTEX_WAIT_PRIVATE, 0, NULL

That output is the answer. The line has no = 0 and no return value, because the call has not returned — the process is inside futex, waiting for a lock held by another thread. If instead the output scrolls forever, the process is busy, not hung, and you want step 3.

Decision tree mapping a parked syscall to its meaning: futex is lock contention, read is waiting input, connect is a network stall, openat ENOENT is a missing file

3. When it is slow rather than hung, start with the summary. -c suppresses the per-call output and prints a table when you detach: total time per syscall, call counts, and — the column everyone forgets — errors. Let it run for ten seconds, then Ctrl-C. This is the best first move on a process that is misbehaving but still moving, because one glance tells you whether you are looking at a network problem, a disk problem, or a process failing the same call thousands of times:

sudo strace -c -f -p 4127
strace: Process 4127 attached
^Cstrace: Process 4127 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 71.24    2.418033       19190       126           epoll_wait
 18.02    0.611648          72      8492      8492 stat
  6.11    0.207419          24      8492           openat
  ...
------ ----------- ----------- --------- --------- ----------------
100.00    3.394219                 17284      8492 total

Read the errors column first. 8,492 failing stat calls in ten seconds is not a performance mystery — it is a bug, and step 6 finds it.

4. Follow forks and threads with -f. This is the single most common reason people conclude “strace shows nothing”. You attached to a supervisor — nginx’s master, a shell wrapper, a Python parent — and it is idle by design while its children do the work. -f traces children as they are created and prefixes every line with the PID that made the call:

sudo strace -f -p 4127
[pid  4127] wait4(-1,  <unfinished ...>
[pid  4193] accept4(6, {sa_family=AF_INET, sin_port=htons(52344), ...

The parent is parked in wait4 — that is just what a parent does. PID 4193 is the one doing real work. Go trace that.

5. Cut the noise with -e trace=. Full traces are unreadable. -e trace= takes syscall names (-e trace=openat,read) or whole classes: file for anything that takes a filename, network for sockets, process for fork and exec, desc for file-descriptor operations. Filtering is also the cheapest way to reduce the tracing overhead, since strace can stop printing what you did not ask for.

6. Find the file it cannot open. The most common real diagnosis. Trace only file-related calls and look for ENOENT:

sudo strace -f -e trace=file -p 4127 2>&1 | grep ENOENT
openat(AT_FDCWD, "/etc/myapp/config.yml", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/myapp/etc/config.yml", O_RDONLY) = -1 ENOENT (No such file or directory)

The application searched two paths and gave up. Note the exact string it wanted — it is very often a typo, a relative path resolved against an unexpected working directory, or a file that exists but is being looked for under a different name. EACCES instead of ENOENT means the opposite problem: the file is there and the process is not allowed to read it, which points at ownership or a security policy rather than a missing deploy step.

7. Catch the network stall. -e trace=network narrows to sockets. Add -tt for wall-clock timestamps and -T to print how long each call took, in seconds, in angle brackets at the end of the line:

sudo strace -f -tt -T -e trace=network -p 4127
14:22:07.118432 socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 3 <0.000042>
14:22:07.118644 connect(3, {sa_family=AF_INET, sin_port=htons(5432), sin_addr=inet_addr("10.0.3.9")}, 16

connect to 10.0.3.9 on port 5432 never returned. The process is not broken — it is waiting on a Postgres host that is not answering, and your problem is a firewall, a security group, or a dead peer. The address is right there in the argument, which is what makes this so much faster than guessing.

8. Turn file descriptors into names with -y. A bare read(3, ...) tells you nothing about what fd 3 is. -y annotates every descriptor with the path it points to, and -yy additionally resolves socket descriptors to their actual addresses:

sudo strace -y -e trace=desc -p 4127
read(3</var/log/myapp/ingest.log>, "2026-07-15T14:22:07 ready\n", 4096) = 26
write(1</dev/null>, "2026-07-15T14:22:07 ready\n", 26) = 26

If you are chasing descriptor exhaustion rather than a hang — EMFILE in the trace — the companion piece is too many open files: raising the ulimit.

9. See the whole string with -s 256. strace truncates string arguments to 32 characters by default and marks the cut with .... When you need the full path, query, or payload, raise it:

sudo strace -s 256 -e trace=write -p 4127
write(1, "2026-07-15T14:22:07 starting worker pool with 32 threads, config /etc/myapp/config.yml\n", 87) = 87

10. Write it to a file with -o. Anything longer than a few seconds is easier to grep than to watch, and -o keeps strace’s output from tangling with the program’s own stderr. The same flags work on a command you launch yourself, which is the right way to debug a process that dies at startup:

strace -f -o /tmp/startup.log ./myapp --config /etc/myapp/config.yml
$ grep -c ENOENT /tmp/startup.log
14

Validate it works

Prove the tool works on something whose answer you already know, so you learn to trust the output. This traces cat reading a file that does not exist:

strace -e trace=openat cat /etc/definitely-not-here
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/etc/definitely-not-here", O_RDONLY) = -1 ENOENT (No such file or directory)
cat: /etc/definitely-not-here: No such file or directory
+++ exited with status 1 +++

Three things to notice. The first two openat calls are the dynamic loader pulling in libc — every dynamically linked program does this, and skipping past that preamble is most of what reading strace output means in practice. The third line is the real work, and its ENOENT is the shape you hunt for in step 6. cat’s own error appears interleaved because strace writes to stderr — which is why step 10 uses -o.

Now test the summary table on something with a known profile:

strace -c -e trace=openat cat /etc/hostname
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.000094          31         3           openat
------ ----------- ----------- --------- --------- ----------------
100.00    0.000094                     3         0 total

Three opens, zero errors. That is a healthy process.

Troubleshooting

  • strace: attach: ptrace(PTRACE_SEIZE, 4127): Operation not permitted. Either you do not own the process, or Yama’s ptrace_scope is 1 and the target is not your descendant. Run it under sudo. If sudo also fails, check sysctl kernel.yama.ptrace_scope — at 3, ptrace is off until reboot and no privilege will help. Inside a container, this is the missing SYS_PTRACE capability instead.
  • The trace is empty, or it just says wait4 / epoll_wait forever. You are tracing the wrong process. Add -f: the parent supervises, the child works. This is also the classic surprise with statically linked Go binaries, where the runtime spreads goroutines across threads and the interesting call is on a thread you did not attach to.
  • The output scrolls too fast to read. Do not fight it — filter it. Start with -c to find out which syscall is worth looking at, then re-run with -e trace= restricted to that one. Filtering first and reading second is the workflow; reading an unfiltered trace of a busy process is not a skill anyone has.
  • The process got dramatically slower the moment you attached. That is inherent, not a bug: every syscall now costs two extra context switches, and a syscall-heavy process can slow by an order of magnitude or more. On strace 5.3 and later, --seccomp-bpf pushes your -e trace= filter into the kernel so it only stops the process for the calls you asked about, which cuts the cost a lot. Detaching with Ctrl-C restores full speed immediately.
  • Stripped or statically linked binaries. strace itself does not care — it reads syscalls at the kernel boundary, so a stripped binary traces exactly like any other, and a static binary simply shows no library-loading preamble. What breaks is anything needing symbols: -k (print a stack trace per call) will give you bare addresses like [0x7f9c4c0a9128] without debug info installed. And if you were hoping to see library calls rather than syscalls, strace never shows those at any point — that is ltrace or a debugger.

When this is the wrong approach

strace is a scalpel for one question: what is this process asking the kernel, right now. It is superb on a hung process, a startup failure, or a mystery ENOENT, because a stopped process costs nothing to stop again. It is a bad choice almost everywhere else.

Never make it your first move on a hot production process under load. The slowdown is real and can trip health checks, cause a load balancer to eject the node, or turn a slow request into a timeout cascade — you can convert a degraded service into an outage while diagnosing it. If you must trace in production, attach to one process out of a pool, filter with -e trace= from the first command, keep it to a few seconds, and remember that Ctrl-C is your undo.

For “why is this slow?” on a running system, perf samples instead of stopping and costs a fraction as much; for CPU-bound code strace will show you nothing at all, since a compute loop makes no syscalls. For “what happened?”, read the logs first — reading systemd logs with journalctl answers a surprising share of the questions people reach for strace to solve, and it costs nothing. For continuous, low-overhead syscall visibility across a whole host, the modern answer is eBPF tooling such as bpftrace. And for application-level questions — which function, which query, which allocation — a profiler or a debugger is the right instrument; strace only ever sees the kernel boundary. The project lives at strace.io, and the full flag reference is in the strace man page.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.