125

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.

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the answer. that line behaves the same as just "gdb ./a.out" for me tho.
I just confirmed that you can also pass multiple -ex options on the command line and they'll run in order before transferring gdb input to you.
This solution doesn't work if the program has arguments like: ./a.out a b
since OSX/xcode no longer supports gdb out of the box (lldb is the new gdb), this now seems like the better approach.
I made a command file and use gdb -x on Linux systems. OS X uses LLVM and clang so check out the debugging capabilities of lldb.
30

I would use a gdb-script:

gdb -x your-script

where your-script contains something like:

file a.out
b main
r

afterwards you have the normal interactive gdb prompt

EDIT:

here is an optimization for the truly lazy:

  1. save the script as .gdbinit in the working directory.
  2. Afterwards you simply run gdb as

    gdb

... and gdb automatically loads and executes the content of .gdbinit.

5 Comments

hey thanks for the answer. that looks like just what i want except i don't get the gdb prompt, i get gdb being suspended and i'm back at the command line as if i'd typed "ctrl-z". if i "fg", then gdb resumes and the app runs. this is OS X.
orion: mmmh - that's surprising to me. Works well with every UNIX flavor I am actually using (Linux, Solaris, AIX). What gdb version you are using?
i just re-verfied the behaviour of this - still exits me to the command line w/ gdb suspended.GNU gdb 6.3.50-20050815 (Apple version gdb-1518) (Sat Feb 12 02:52:12 UTC 2011).
@orionelenzil No idea why, but if you put fg after the r line, it acts sanely
The start command provides a shortcut for b + r: stackoverflow.com/a/2119606/895245
20
(echo r ; cat) | gdb a.out

The cat allows you keep typing after gdb breaks.

3 Comments

whoa, awesome. you have to ctrl-c after exiting gdb to stop the cat, but that's great.
It looks like you lose some key-control on the gdb instance using this.
Moreover, questions like Make breakpoint pending on future shared library load? (y or [n]) will automatically assume answer "No".
12

start command

This command is another good option:

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

It is like run, but also sets a temporary breakpoint at main and stops there.

This temporary breakpoint is deactivated once it is hit.

starti

There is also a related starti which starts the program and stops at the very first instruction instead, see also: Stopping at the first machine code instruction in GDB

Great when you are doing some low level stuff.

Comments

4

gdb -x <(echo run) --args $program $args

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.