debugging scripts - Multiple commands in gdb separated by some sort of delimiter ';'?

source comment (5)

You can do this using the python integration in gdb.

It would be nice if s ; bt stepped and then printed a backtrace, but it doesn't.

You can accomplish the same thing by calling into the Python interpreter.

python import gdb ; print gdb.execute("s") ; print gdb.execute("bt")

It's possible to wrap this up into a dedicated command, here called "cmds", backed by a python definition.

Here's an example .gdbinit extended with a function to run multiple commands.

# multiple commands
import gdb

class Cmds(gdb.Command):
  """run multiple commands separated by ';'"""
  def __init__(self):

  def invoke(self, arg, from_tty):
    for fragment in arg.split(';'):
      # from_tty is true. These commands should be considered interactive.
      # to_string is false. We just want to write the output of the commands, not capture it.
      gdb.execute(fragment, from_tty=True, to_string=False)


example invocation:

$ gdb
(gdb) cmds echo hi ; echo bye

I am trying to execute two commands at once in gdb:

finish; next

I tried using the ';' to separate the commands but gdb did not let me do both at once.

Is it possible to do multiple commands in gdb similar to bash commands separated by ';' delimiter?

If you are running gdb from command line you can pass multiple commands with the -ex parameter like:

$ gdb ./prog -ex 'b srcfile.c:90' -ex 'b somefunc' -ex 'r -p arg1 -q arg2'

This coupled with display and other commands makes running gdb less cumbersome.

I don't believe so (but I may be wrong). You can do something like this:

(gdb) define fn
> finish
> next
> end

And then just type:

(gdb) fn

You can put this in your ~/.gdbinit file as well so it is always available.

i ran across another way to do multiple commands in GDB using a Bash HERE document.


cat << EOF | gdb
print "command_1"
print "..."
print "command_n"

this has limited value/usability IMO because GDB quits after executing the list of commands.

debugging gdb