6

I am currently debugging a program with gdb. I have to start gdb over and over again and do the same steps:

set a breakpoint, run, print a variable, quit

Is there a way to let gdb do that automatically for me? Probably a script that could be attached as a parameter?

Thanks in advance!

hennr
  • 2,632
  • 2
  • 23
  • 26

2 Answers2

6

You can do it either by -x file option or by -ex command option. From Gdb manual:

-command file
-x file
Execute commands from file file. The contents of this file is evaluated exactly as the source command would. See Command files. 
-eval-command command
-ex command
Execute a single gdb command.
This option may be used multiple times to call multiple commands. It may also be interleaved with `-command' as required.

          gdb -ex 'target sim' -ex 'load' \
             -x setbreakpoints -ex 'run' a.out
ks1322
  • 33,961
  • 14
  • 109
  • 164
1

The interwebs differ on whether the name of the file is .gdbrc or .gdbinit, but GDB will read this file from your home directory on start-up, and it can give any GDB command (including setting breakpoints).

Also check out http://www.andrews.edu/~seidel/gdb.help

Jon Watte
  • 6,579
  • 4
  • 53
  • 63
  • Just checked the man page to see what the file is called, it's the .gdbinit You can tell gdb to ignore this with -n or -nx Anyway, there is a better way for my purpose I just found in the man page (sorry!): -x FILE, -command=FILE Execute GDB commands from file file. One can write the commands taken by the interpreter in that file, one each line. – hennr Sep 19 '11 at 13:45
  • 3
    This answer is wrong: GDB will not read `.gdbrc` nor `.gdbin`. It _will_ read `.gdbinit` – Employed Russian Oct 10 '20 at 14:56