182

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?

8 Answers 8

234

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.

3
  • 2
    Bad method when gdb is invoked just to print stacktrace of the invoker: execlp("gdb", "gdb", "-batch", "-n", "-ex", "bt full", ... and I can't turn off pagination.
    – Vi.
    Commented Jun 30, 2010 at 18:01
  • 5
    And if you forget how you defined a function, you can use show user <function name> to see its source, e.g. show user fn.
    – ntc2
    Commented Dec 27, 2016 at 22:17
  • Good method when you're debugging a loop that reads a config line by line and you want to inspect what happens at a particular line. Then define cpline <Enter> c <Enter> p line <Enter> end <Enter> cpline <Enter> <Enter> <Enter>... until you see the needed line.
    – x-yuri
    Commented Jun 26 at 22:26
69

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.

15

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
python
from __future__ import print_function
import gdb


class Cmds(gdb.Command):
  """run multiple commands separated by ';'"""
  def __init__(self):
    gdb.Command.__init__(
      self,
      "cmds",
      gdb.COMMAND_DATA,
      gdb.COMPLETE_SYMBOL,
      True,
    )

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


Cmds()
end

example invocation:

$ gdb
(gdb) cmds echo hi ; echo bye
hi
bye
0
12

GDB has no such command separator character. I looked briefly, in case it would be easy to add one, but unfortunately no....

2

Certainly it is possible. Given, for example, C code

int a = 3;
double b = 4.4;
short c = 555;

, say we want to ask GDB what is the type of each of those variables.  The following sequence of GDB commands will allow us to enter 3 whatis requests all on a single line:

  1. set prompt #gdb#
    • Any prompt whose first non-whitespace character is # will work: it just so happens that # starts a comment in GDB command scripts.
  2. set logging overwrite on
    • By default, GDB appends to a log file; choosing to instead overwrite will let us easily deploy this trick again later, with different commands.
  3. set logging redirect on
    • Meaning, save output of typed commands to log file only: do not also show it at the terminal. (Not absolutely required for our purposes, but keeps the clutter down.)
  4. set logging on
    • This causes GDB to start actually logging; by default, the log file is called gdb.txt.
  5. printf "\nwhatis a\nwhatis b\nwhatis c\n"
    • Our 3 whatis requests, entered on a single line as promised!  Separating the commands, before the first, and after the last is \n.
  6. set logging off
    • Done writing to gdb.txt; that file now contains a valid GDB command script:
   #gdb#
   whatis a
   whatis b
   whatis c
   #gdb#
  1. source gdb.txt
    • GDB now executes commands in the script which it just generated, producing the expected results:
type = int
type = double
type = short

Notes

  • Should you wish to deploy this trick again in the same GDB session, just perform steps 4-7.
  • Generating a command script with shell would be less cumbersome, and may well be possible; the above method, however, is platform-agnostic.
1

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

example:

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

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

0

This link describes gdb "User-defined commands" and contains the solutions mentioned above.

0

I used to just copy and paste multiple commands in multiple lines from txt file to gdb command line and it worked fine with gdb 8.3. But gdb 12.1 just interprets the whole block including line changes as one command and fails.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.