81

I have a simple C program that forks a process and then runs an executable.

I want to attach the child process to gdb.

I run the main program in a console and open another console to find the pid of the child process, then I start gdb with the following command:

gdb attach 12271

where 12271 is the child process id, but the attach fails with:

No such file or directory.

Any idea why?

Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
as3rdaccount
  • 3,711
  • 12
  • 42
  • 62
  • i don't think attach is a command line argument. you can use it once gdb is already running. this guy has a good tutorial: http://dirac.org/linux/gdb/06-Debugging_A_Running_Process.php – thang Jan 17 '13 at 01:51
  • 1
    Possible duplicate of [Can I use GDB to debug a running process?](http://stackoverflow.com/questions/2308653/can-i-use-gdb-to-debug-a-running-process) – Ciro Santilli OurBigBook.com Feb 17 '16 at 17:35

3 Answers3

137

Try one of these:

gdb -p 12271
gdb /path/to/exe 12271

gdb /path/to/exe
(gdb) attach 12271
Yostage
  • 292
  • 2
  • 4
  • 12
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
22

The first argument should be the path to the executable program. So

gdb progname 12271
DrC
  • 7,528
  • 1
  • 22
  • 37
7

With a running instance of myExecutableName having a PID 15073:

hitting Tab twice after $ gdb myExecutableName in the command line will automagically autocompletes to:

$ gdb myExecutableName 15073

and will attach gdb to this process. That's nice!

davidhcefx
  • 130
  • 1
  • 8
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169