Supported on windows
Supported on linux
Not supported on embedded
Supported on android

attach command

Attaches GDB to a running process

Syntax

attach [PID]

Parameters

PID
Specifies the process identifier of the process to attach to.

Remarks

To continue the process after you have attached to it use the continue command. If you use the run command instead, the process will be restarted.

Restrictions

When you attach to processes with GDB, the following restrictions are applied:

  • In order to attach to processes from different Linux terminals you need to be running as root.
  • In order to attach to processes on Windows machine you need to run GDB as Administrator.
  • You cannot attach two instances of GDB to the same process.
  • You cannot attach GDB to a Windows process that is already being debugged by native Visual Studio debugger.

Examples

In the following example we attach GDB to a running Linux process, view the call stack and continue executing:

(gdb) attach 13769
Attaching to process 13769
Reading symbols from /home/testuser/0.elf...done.
Reading symbols from /lib/i386-linux-gnu/libc.so.6...
Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/libc-2.15.so...done.
done.
Loaded symbols for /lib/i386-linux-gnu/libc.so.6
Reading symbols from /lib/ld-linux.so.2...
Reading symbols from /usr/lib/debug/lib/i386-linux-gnu/ld-2.15.so...done.
done.
Loaded symbols for /lib/ld-linux.so.2
0xb771b424 in __kernel_vsyscall ()
(gdb) bt
#0 0xb771b424 in __kernel_vsyscall ()
#1 0xb76393c3 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:82
#2 0xb75cd22b in _IO_new_file_underflow (fp=0xb7701ac0) at fileops.c:619
#3 0xb75ce449 in _IO_default_uflow (fp=0xb7701ac0) at genops.c:440
#4 0xb75ce260 in __GI___uflow (fp=0xb7701ac0) at genops.c:394
#5 0xb75ae565 in _IO_vfscanf_internal (s=0xb7701ac0, format=0x804856f "%d", argptr=0xbf8778a4 "errp=0x0) at vfscanf.c:620
#6 0xb75b0627 in __scanf (format=0x804856f "%d") at scanf.c:35
#7 0x08048475 in main (argc=1, argv=0xbf877964) at 0.cpp:7
(gdb) continue
Continuing.
[Inferior 1 (process 13769) exited normally]
(gdb)

Common errors

In this example we try attaching to a Linux process running on a different terminal. This fails as we are not running as root:

(gdb) attach 13618
Attaching to program: /home/testuser/0.elf, process 13618
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.
(gdb)

Compatibility with VisualGDB

Do not execute the attach command manually under Visual Studio. Use the Debug->Attach to Process command and select VisualGDB as the transport:

See also