Danny Taffari
Danny Taffari

Reputation: 43

How to ensure GDB terminates in command file error?

I want to automate a GDB debug session using command files but want to make sure GDB terminates every time.

This is a problem as described in the documentation :

An error in any command terminates execution of the command file and control is returned to the console.

In my case I want the script to either continue or terminate GDB if there is an error. I.e. dont let it return to console (and make me quit manually). E.g. see below

echo Starting GDB script\n
set confirm off
set pagination off
echo Connecting to target\n
target remote localhost:1234
thread 1
thread 2 # <-- This command will produce an error, and stop the script from terminating GDB
k
q

Upvotes: 0

Views: 234

Answers (1)

Employed Russian
Employed Russian

Reputation: 213486

In my case I want the script to either continue or terminate GDB if there is an error. I.e. dont let it return to console (and make me quit manually).

Invoke GDB with -batch argument, and it will ignore script errors.

Example:

$ gdb -q -batch -ex 'start' -ex 'thread 2' -ex 'print foo' -ex kill  ./a.out
Temporary breakpoint 1 at 0x112d

Temporary breakpoint 1, 0x000055555555512d in main ()
Unknown thread 2.
No symbol "foo" in current context.
[Inferior 1 (process 2359421) killed]

Upvotes: 1

Related Questions