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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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
It seems can't print register.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.