Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

commands to gdb from C program

Tags:

c

gdb

I am newbie to UNIX programs. I have encountered a situation wherein I have to issue commands to gdb from my C program. I have a C program which invokes another C program by forking a new child process. I need to debug this child C program and hence, I used system command to call gdb process on this C program. But I get a gdb prompt which I do not want. I want to issue commands to the gdb from my parent C program. Is there a way to issue commands to gdb from a C program ?

Please reply.

Thanks a lot.

Esash

like image 339
Esash Avatar asked Sep 11 '11 20:09

Esash


1 Answers

If you need to debug the child process, you don't necessarily need to invoke the child with GDB when you fork+exec. As long as you have the PID of the child process, you can use the "attach" command in GDB to attach to the running child process. Basically, you would start GDB like:

 $ gdb
 (gdb) attach pid-of-child

In the above, replace pid-of-child with the PID of the child process, and there you go, you can debug the child process from interactive GDB, without the parent process needing to deal with GDB at all.

like image 57
Michael Aaron Safyan Avatar answered Sep 30 '22 08:09

Michael Aaron Safyan