Skip to content
systemcoding.dev
Debugging

Attach strace to an Already Running Process

Attach strace -p to a process that is already running on Linux, fix the Yama ptrace_scope permission error, watch a stuck call, then detach without killing it.

7 min read
Summarize in AI
Flat illustration of a debugger stopped at a breakpoint beside the register values, with the LLVM, GNU and Arm logos.

A program that has been running for three days and has stopped responding cannot be restarted under a debugger without losing the state you want to look at. strace -p solves this: it attaches to the live process, prints every system call it makes from that moment, and lets go when you press Ctrl-C. The process keeps running afterward. This post uses strace 6.8 on Ubuntu 24.04 with a 6.8 kernel, and explains the one permission setting that stops most first attempts. If you have not used strace before, the introduction to tracing system calls covers the output format first.

Find the process ID

Attaching needs the process ID, or PID. Two common ways to get it:

Shell
pgrep -a myserver
Text
2310 /usr/local/bin/myserver --port 8080

pgrep -a prints the PID followed by the full command line, which is how you tell two similarly named processes apart. ps -ef | grep works too, but it also lists the grep itself, so pgrep is less noisy. In the examples below the PID is 2310.

Attach and watch

Shell
sudo strace -p 2310
Text
strace: Process 2310 attached
read(0,

The line stops after read(0, because the process is blocked inside that system call, waiting for input on file descriptor 0 (standard input). strace prints the call name and the arguments it already knows as soon as the call starts, and it completes the line when the call returns. A stuck process often looks exactly like this: the last line is an unfinished call, and the call it names is the thing the program waits on.

If someone now types hi and presses Enter on that process's terminal, the line completes and the next calls appear:

Text
read(0, "hi\n", 131072)                 = 3
write(1, "hi\n", 3)                     = 3
read(0,

The program here is cat, and the buffer size of 131072 is its internal read size. It echoed the three bytes it read, then went back to waiting. The numbers after the equals signs are return values: 3 bytes read, 3 bytes written.

Detach without stopping the process

Press Ctrl-C in the terminal that runs strace. It does not send a signal to the traced program; it tells strace to stop tracing and detach:

Text
strace: Process 2310 detached
 <detached ...>

The traced process continues as if nothing happened. Killing strace with kill -9 is the one way to cause trouble, because strace then cannot detach cleanly and the tracee may be left stopped. Use Ctrl-C, or send SIGINT or SIGTERM to strace.

When attaching fails with Operation not permitted

The most common first failure on a modern distribution:

Shell
strace -p 2310
Text
strace: attach: ptrace(PTRACE_SEIZE, 2310): Operation not permitted

Two mechanisms can cause it. The first is plain ownership: without root or the CAP_SYS_PTRACE capability, you can only trace your own processes. The second is the Yama security module. The kernel documentation for Yama describes kernel.yama.ptrace_scope, which has four values:

ValueMeaning
0any process can trace another process with the same user ID
1a process can trace only its own descendants (the default on Ubuntu)
2only processes with CAP_SYS_PTRACE can use ptrace
3no process can use ptrace, and the setting cannot be changed until reboot

At the default of 1, even your own process is off limits unless strace started it as a child. Check the current value:

Shell
cat /proc/sys/kernel/yama/ptrace_scope
Text
1

The simple fix is sudo strace -p, which works at values 0, 1 and 2. Lowering the setting to 0 lets any process trace any other process of the same user, which is a real security trade, because a compromised process could then read the memory of its siblings. Prefer sudo for a one-off and leave the setting alone on shared machines. In containers, ptrace is often blocked by the runtime's seccomp profile as well; a separate post covers running strace inside containers.

Make the output readable

A busy process produces more output than you can read live. Four options help most:

Shell
sudo strace -p 2310 -tt -T -s 128 -o /tmp/myserver.strace
  • -tt prefixes each line with a wall-clock time with microseconds, so you can see when a call started.

  • -T appends the time spent inside each call in angle brackets, which shows slow calls directly.

  • -s 128 raises the string display limit from the default of 32 bytes, so a request line is not cut short.

  • -o writes to a file instead of the terminal, which keeps strace's own messages out of the stream.

A line produced by these options looks like this:

Text
14:02:11.482913 recvfrom(7, "GET /health HTTP/1.1\r\nHost: loc"..., 128, 0, NULL, NULL) = 62 <0.000042>

The time stamp, the truncated request and the call duration are all there. The duration is the time in the kernel, not CPU time.

Threads and children

By default strace -p PID attaches to the one thread with that ID. A multi-threaded server has many threads, and the work may happen in a different one. Add -f to follow every thread of the process, and any threads or children it creates later:

Shell
sudo strace -f -p 2310 -o /tmp/myserver.strace

The output then has a thread ID in front of each line. The follow child processes post covers the prefix format and per-process output files.

Read a stall from the last line

The skill in attaching is reading what the last unfinished line says about the cause. A few patterns cover most hangs you will meet:

  • read(0, or read(7, with no result means the process waits for data on that descriptor. Look up the descriptor with ls -l /proc/2310/fd/7, which prints a symbolic link such as socket:[48213] or a file path. A socket usually means the peer has not sent anything.

  • futex(0x7f3c2c0009d0, FUTEX_WAIT_PRIVATE, 2, NULL means a thread waits on a lock or condition variable held by another thread. The process is not doing anything wrong at the system call level; the cause is in the program's locking, and a debugger with a thread backtrace is the next tool.

  • connect(5, {sa_family=AF_INET, sin_port=htons(5432), sin_addr=inet_addr("10.0.0.7")}, 16 with no result means the connection attempt is waiting, often on a firewall that drops packets instead of rejecting them.

  • epoll_wait(4, with a long or infinite timeout is a healthy idle event loop. A process that looks stuck but sits here is waiting for work, and the real question is why none arrives.

  • A tight loop of the same call with a result like -1 EAGAIN (Resource temporarily unavailable) means the process is spinning rather than blocked, and a CPU graph will agree.

The first step is always the same: check whether the last line ends in a result. If it does, the process is moving and you are looking at a busy loop. If it does not, the named call is where the process lives.

Running strace -p twice a few seconds apart tells you whether the process is genuinely stuck. If both runs show the same unfinished call with the same arguments, it is. If the arguments change, it is making progress, only slowly.

Attach to a service managed by systemd

A service started by systemd runs under its own unit, but ptrace does not care: attach by PID as usual. Get the main PID from the unit:

Shell
systemctl show --property=MainPID --value myserver.service
Text
2310

Some unit files set NoNewPrivileges=yes or a SystemCallFilter= that blocks ptrace for the service itself. That affects the service tracing others, not you tracing it from outside, so it rarely stops you. What does stop you is a hardened kernel where kernel.yama.ptrace_scope is 2 or 3, covered above.

Keep the capture useful after you detach

The file written by -o is the evidence, so give it a name that says what it is, and keep the first and last hundred lines when you share it. A short capture of a hang, the attach line plus the unfinished call, is usually enough to tell a colleague where to look. For a process that fails intermittently, run strace with -o in the background and read the file after the failure, which avoids staring at a scrolling terminal.

What attaching costs

strace stops the traced process at every system call entry and exit, so a busy process slows down while it is attached. That is acceptable for a stuck or misbehaving process and a poor choice for a latency-sensitive production service under heavy load. For that case a kernel-side tool such as perf trace or bpftrace has far lower overhead, at the price of more setup. Attach for a short window, capture what you need with -o, and detach.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.