Debug a Running Process by Attaching With gdb -p
Attach GDB to a process that is already running with gdb -p or attach, inspect where it is stuck, then detach so it keeps running. Covers permissions.

Most debugging starts with gdb ./program, which means GDB launches the program and owns it from the first instruction. That is no help when the program is already running: a server that has stopped answering, a batch job that has used a core for an hour, a process you did not start. For these, GDB can attach to a process by its id, stop it where it is, let you look around, and then let go. The process carries on as if nothing happened, apart from the pause. This post shows the attach and detach cycle on a looping program, the form of the command, what you can and cannot see, and the permission check that makes the first attempt fail on many systems. Output comes from GDB 16.3 on a Linux 6.12 kernel with glibc 2.40.
A process that is stuck
Here is a program that spins in a loop waiting for a flag that never changes. It is a stand-in for "a process I did not start under GDB".
#include <stdio.h>
#include <unistd.h>
volatile int ready = 0;
static void wait_for_flag(int id)
{
unsigned long spins = 0;
while (!ready)
spins++;
printf("worker %d done after %lu spins\n", id, spins);
}
int main(void)
{
printf("pid %d\n", getpid());
fflush(stdout);
wait_for_flag(7);
return 0;
}Build it with debug information and start it in one terminal. It prints its own process id and then burns CPU.
gcc -g -O0 -Wall -Wextra -o spin spin.c
./spinpid 48210The id will differ on your machine. In real life you would find it with pgrep spin or pidof spin, or from ps.
pgrep -x spin48210Attach from the command line
In a second terminal, run GDB with -p and the process id. If you also give it the program file, GDB reads symbols from the file; otherwise it finds the executable through /proc/48210/exe, which usually works.
gdb -q -p 48210Attaching to process 48210
Reading symbols from /home/dev/spin...
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...
(No debugging symbols found in /lib/x86_64-linux-gnu/libc.so.6)
0x0000555555555151 in wait_for_flag (id=7) at spin.c:9
9 while (!ready)
(gdb)GDB sent the process a stop, waited, and reported where it was: inside wait_for_flag, at line 9, the loop test. The library line comes from the dynamic loader; a distribution that carries debuginfod or debug packages will say more. The process is now stopped, as if it had hit a breakpoint. Everything from the earlier posts works: bt, print, info locals, up.
(gdb) bt
#0 0x0000555555555151 in wait_for_flag (id=7) at spin.c:9
#1 0x00005555555551ab in main () at spin.c:18
(gdb) print spins
$1 = 2918374651
(gdb) print ready
$2 = 0The count is large, as expected for a tight loop, and will differ on every attach. ready is 0, so the process is waiting on exactly what the source says. You can also change it. Setting ready to 1 lets the process finish.
(gdb) set var ready = 1
(gdb) continue
Continuing.
[Inferior 1 (process 48210) exited normally]In the first terminal, the program prints worker 7 done after 2918374651 spins and exits; the count is the value after the continue, so it is a little larger than what print spins showed. Changing live state like this is a legitimate way to unstick a process in a test, and a risky one in production, because the program may not be written for its variables to change under it.
Attach from inside GDB
You can attach from a running session too, which suits the case where GDB is already open on the program file.
(gdb) file ./spin
Reading symbols from ./spin...
(gdb) attach 48210
Attaching to program: /home/dev/spin, process 48210
0x0000555555555151 in wait_for_flag (id=7) at spin.c:9
9 while (!ready)Loading the file first means GDB has the symbols before it stops the process, which avoids a lookup through /proc. Both forms do the same thing underneath: they call ptrace with PTRACE_ATTACH or PTRACE_SEIZE on the target, and the kernel makes GDB its tracer.
Detach, and what quit does
When you are done and the process should live on, use detach.
(gdb) detach
Detaching from program: /home/dev/spin, process 48210
[Inferior 1 (process 48210) detached]The process resumes. Any breakpoints GDB inserted are removed first, so it does not hit an int3 with nobody listening. This is the critical difference from a program GDB started: if you simply quit while attached, GDB asks A debugging session is active... Quit anyway?, and on y it detaches an attached process rather than killing it. A process GDB started itself is killed on quit. The safe habit in production is to type detach explicitly and then quit.
Do not run kill inside GDB unless you mean it. kill ends the process, and on a service that is an outage.
The permission check
The most common first failure is this.
Attaching to process 48210
Could not attach to process. If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.On Ubuntu and several other distributions, the Yama security module restricts ptrace to descendants of the tracer. Read the current mode.
cat /proc/sys/kernel/yama/ptrace_scope1The meanings are documented in the kernel's Documentation/admin-guide/LSM/Yama.rst: 0 allows any process of the same user, 1 allows only a parent to trace its descendants, 2 requires CAP_SYS_PTRACE, and 3 disables attaching entirely until reboot. With 1, gdb -p on an unrelated process fails for a normal user. The options are to run GDB with sudo, to start the target process from GDB or from a parent that is your own shell, or, on a development machine, to lower the setting.
sudo sysctl -w kernel.yama.ptrace_scope=0Lowering it weakens a defence against a compromised process reading other processes' memory, so do it on a disposable machine, and set it back after. A later post in this series covers ptrace_scope in full. Containers add another layer: a debugger in the host can attach to a containerised process by its host pid, but inside the container, the seccomp profile may block ptrace altogether, and Docker's default profile allows it only on recent kernels with the right capability. Check the container runtime's documentation before you rely on it.
What you can and cannot see
An attached process has no history. You are looking at its present, so a crash that already happened is gone, and a breakpoint only catches the future. Symbols come from the executable on disk. If the file was replaced after the process started, as happens during a deployment, GDB reads the new file and the symbols will not match the running code. info proc mappings shows what is mapped, and /proc/PID/exe shows (deleted) when the binary was replaced.
If the program was built without -g you still get a backtrace of function names from the symbol table, and info registers, but not local variables. For a stripped production binary, a matching debug file or debuginfod server fixes that, as covered in the debuginfod post.
Attaching stops the whole process, including every thread. Other processes waiting on a socket that this one serves will see a stall until you detach, so keep attach sessions short on live systems. Where even a pause is too much, gcore PID writes a core file of the live process and lets you analyse the copy offline.


