I am debugging a program using gdb. Whenever I miss a breakpoint or decide to add another watchpoint, I have to kill the process and rerun it. In order to attach the existing gdb to it, I use attach <pid>
. However, I have to find out the pid of the new process.
The way I do that today is to suspend gdb, get the pid with ps -C <program_name>
and then return to gdb to attach to it.
Is there any way to run a unix command from the gdb command prompt without exiting to the shell, so that I could do something like this from inside gdb:
attach `ps -C <program_name>`
I am working on linux.
Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).
The bash script runs gdb with the -q option, which stands for quite. It tells gdb not to print the version number on startup. The --args option is used to pass command-line arguments to the binary that gdb loads for debugging.
Use the run command to start your program under gdb. You must first specify the program name (except on VxWorks) with an argument to gdb (see Getting In and Out of gdb), or by using the file or exec-file command (see Commands to Specify Files).
(gdb) help shell
Execute the rest of the line as a shell command. With no arguments, run an inferior shell.
After finish, you can use 'exit' to return to gdb.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With