51

Does anyone know how to save gdb settings (like "set print pretty on" or "set print elements 0", both from here)? I don't want to set my configuration every time that I will use gdb :/

I searched in google and SO, but I found nothing.

3 Answers 3

91

Add any commands you want to auto run in the .gdbinit file in your home directory.

4
  • For the record, this also works for the gdb inside Xcode. I added handle SIGPIPE nostop to .gdbinit to fix this problem.
    – mpontillo
    Commented Feb 22, 2012 at 17:27
  • Dear I like to add command to let my cross arm gdb starts the target gdb(on raspberry) automatically, how may I set the gdbinit file? please help bro, commands?
    – user2889419
    Commented Jun 4, 2014 at 18:36
  • 4
    TIP: adding add-auto-load-safe-path <path> in ~/.gdbinit allows to add additional gdbinits, so you can have one per project for example!
    – Paschalis
    Commented May 19, 2016 at 15:35
  • Or even add set-auto-load safe-path / to .gdbinit, if you don't want to add every project separately and security is not a concern.
    – fala
    Commented Nov 24, 2016 at 11:28
6

The existing answer works for commands that can run before the binary is loaded, but for example if you want to add catch throw you cannot do it in .gdbinit since that command needs to run after the binary is loaded.

But gdb can take a file with commands to run after binary loading using:

-x file
       Execute GDB commands from file file.

I automated that by creating an alias:

alias gdb='gdb -x ~/.gdbinit_x'

and added my after binary load commands in that file.

1
  • 1
    Nice addition to the answer. Thanks!
    – coelhudo
    Commented Apr 30, 2021 at 17:08
1

The authorative Debugging with GDB manual explains initialization files. In particular, the manual says the following for user-specific home directory initialization files:

There are a number of locations that GDB will search in the home directory, these locations are searched in order and GDB will load the first file that it finds, and subsequent locations will not be checked.

On non-Apple hosts the locations searched are:

$XDG_CONFIG_HOME/gdb/gdbinit 
$HOME/.config/gdb/gdbinit
$HOME/.gdbinit

While on Apple hosts the locations searched are:

$HOME/Library/Preferences/gdb/gdbinit
$HOME/.gdbinit

Furthermore, there exist home directory early initialization files, system wide initialization files, and local directory initialization file. The documentation explains their evaluation order.

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.