12

Is there a way to set break point via cmd line on using GDB.

Currently on running GDB, I need to set

 (gdb) b fun1
 (gdb) b fun2
 (gdb) b fun3

and If I close and open the GDB again, I need to set all the break points :( . Is there a way to set break point for GDB in cmd line itself, like

  $> gdb -break fun1 -break fun2 -break fun3 ./myprog

4 Answers 4

23

GDB Provides -ex option to set GDB commands such as 'break' 'info' 'set print' 'display x' on invoking GDB from command line, as shown below

    $> gdb -ex 'break main' -ex 'info b' -ex 'set print pretty on' ./myprog

The option of -ex option is not specified in man page or in GDB help :(

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

1 Comment

good work. this is new to me, and doesn't work on my main machine, gdb version 6.3.5, so be aware that this appears to be a recent feature.
7

Put your break commands in a file, and pass the file to gdb on the command line using the -x flag.

From man gdb:

   -x file
           Execute GDB commands from file file.

It turns out that there is a command for similarly passing commands, but on the command line: both -ex and -eval-command allow you to pass an individual command. It appears to have been introduced in version 7: it's unavailable on gdb 6.3.5, but available in 7.3.1. -ex and -eval-command are documented in the online gdb docs with the other command-line arguments here.

So, for your example:

$> gdb -ex 'break fun1' -ex 'break fun2' -ex 'break fun3' ./myprog

The other answers schooled me on this.

2 Comments

It works but I need to keep file :(; No way setting it on cmd line?
Not very elegant, but a command line: echo -e b fun1 "\n"b fun2 "\n"b fun3 >/tmp/cmd.tmp ; gdb -x /tmp/cmd.tmp ; rm /tmp/cmd.tmp @Viswesn
1

You can use the command gdb -ex 'b fun1' -ex 'b fun2' (or in long form -eval-command).

Curiously, it is missing in man gdb, but it is there in info gdb. And in the sources, of course!

Comments

0

I Don't know if you can set breakpoints outside gdb, but I know you can use Linux commands inside gdb, so that you don't have to quit gdb frequently. The gdb command is shell yourcommand, if u wanna run ls, just type shell ls under gdb.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.