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 ?

1

1 Answer 1

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.

2
  • @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 Jul 5, 2013 at 4:36
  • It seems can't print register.
    – Eric
    Jun 4, 2016 at 16:37

Your Answer

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

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