6

I am encountering a problem to print multiple variables (say 25) in a function on GDB Prompt.

Is there any convenient way to do this instead of printing every variables manually ?

can I have a script or a simpler way which can do my job ?

San
  • 905
  • 3
  • 16
  • 33
  • Possible duplicate of [gdb - print multiple variables with one command](http://stackoverflow.com/questions/1882857/gdb-print-multiple-variables-with-one-command) – phuclv Nov 23 '15 at 12:14

1 Answers1

14

You can print multiple values using printf command in gdb.

printf "%d,%d\n", a, b

To use it in future, you can either define a gdb-function or use gdb-history feature.

  1. To define a gdb-function, create/modify file $HOME/.gdbinit with the following conten,

    define print_all
        printf "%d,%d\n", a, b
    end
    document print_all
        Prints all my variables.
    end
    

    Then you can use print_all as a command.

  2. For history trick, create/modify file $HOME/.gdbinit with the following content:

    set history filename ~/.gdb_history
    set history save
    

    and get it using ctrl+r same like in bash. Actual gdb-history answer is here.

Community
  • 1
  • 1
VoidPointer
  • 3,037
  • 21
  • 25
  • @Jeyaram, I have no experience on different linux distros, but you can use `-x` option to specify command file if it is not picked from `$HOME/.gdbinit`. Ex.: `gdb -x /path/.gdbinit /path/binary` – VoidPointer Jul 05 '13 at 04:36
  • It seems can't print register. – Eric Jun 04 '16 at 16:37