How to attach a process in gdb

UnixGdb

Unix Problem Overview


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?

Unix Solutions


Solution 1 - Unix

Try one of these:

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

gdb /path/to/exe
(gdb) attach 12271

Solution 2 - Unix

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

gdb progname 12271

Solution 3 - Unix

With a running instance of myExecutableName having a PID 15073:

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

$ gdb myExecutableName 15073

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionas3rdaccountView Question on Stackoverflow
Solution 1 - UnixEmployed RussianView Answer on Stackoverflow
Solution 2 - UnixDrCView Answer on Stackoverflow
Solution 3 - UnixStephane RollandView Answer on Stackoverflow