Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting break via cmd line on running GDB

Tags:

c

gdb

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
like image 201
Viswesn Avatar asked Aug 30 '12 13:08

Viswesn


People also ask

How do you set a breakpoint in a line in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

What is break command in GDB?

Creates a breakpoint at a specified line, address or function.

How do you use the breakpoint command?

You can use breakpoint commands to start your program up again. Simply use the continue command, or step , or any other command that resumes execution. Any other commands in the command list, after a command that resumes execution, are ignored.

What GDB command would you need to call in order to set a breakpoint on line 14?

break function-name will set a breakpoint at the start of the function.


2 Answers

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

like image 99
Viswesn Avatar answered Oct 13 '22 19:10

Viswesn


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.

like image 30
pb2q Avatar answered Oct 13 '22 20:10

pb2q