Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute external commands from the gdb command prompt?

Tags:

gdb

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.

like image 473
Nathan Fellman Avatar asked Nov 28 '10 15:11

Nathan Fellman


People also ask

How do I run a command in GDB?

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).

How do I run a bash script in GDB?

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.

What command is used to start up GDB?

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).


1 Answers

(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.

like image 168
Huang F. Lei Avatar answered Nov 10 '22 06:11

Huang F. Lei