Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically run the executable in GDB?

Tags:

gdb

I'd like to have gdb immediately run the executable, as if I'd typed "run" (motivation: I dislike typing "run").

One way is to pipe the command to gdb like this:

$ echo run | gdb myApp 

But the problem with this approach is that you lose interactivity with gdb, eg. if a breakpoint triggers or myApp crashes, gdb quits. This method is discussed here.

Looking at the options in --help, I don't see a way to do this, but perhaps I'm missing something.

like image 925
orion elenzil Avatar asked Jan 22 '10 18:01

orion elenzil


People also ask

How do I run a program under 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 GDB in batch mode?

Note that, if you intend to use it in batch mode, you have to "start up" the script at the end, with run or start or something similar. With this script in place, I can call gdb in batch mode - which will generate the following output in the terminal: $ gdb --batch --command=test.


1 Answers

gdb -ex run ./a.out 

If you need to pass arguments to a.out:

gdb -ex run --args ./a.out arg1 arg2 ... 

EDIT: Orion says this doesn't work on Mac OSX.

The -ex flag has been available since GDB-6.4 (released in 2005), but OSX uses Apple's fork of GDB, and the latest XCode for Leopard contains GDB 6.3.50-20050815 (Apple version gdb-967), so you are out of luck.

Building current GDB-7.0.1 release is one possible solution. Just be sure to read this.

like image 121
Employed Russian Avatar answered Sep 23 '22 07:09

Employed Russian