Skip to content

Gdb Basic Setup

Steven Lalewicz edited this page Jul 7, 2022 · 9 revisions

Using GDB can be made more productive by the setup of some little config files.

~/.inputrc

set editing-mode vi

Now you can edit gdb command like you do on the shell, including searching the history. See command line editing for more information.

.gdbinit

Add this file to your working directory and populate with

set history save
set confirm off

This makes gdb save the command history. Now you can recall commands from previous sessions. The last line turns off the conformation message each time you quit gdb.

~/.gdbinit

By default gdb won’t load the .gdbinit from the current directory as a security measure. You need to create another .gdbinit in your home directory

add-auto-load-safe-path /home/[user]/[directory]/.gdbinit

This allows gdb to auto load your .gdbinit file. You probably want to check any .gdbinit file you download from a repository first. I'm sure everything on GitHub is good. But better safe than sorry. To be honest I'm not sure what issues to look out for. Some research needed.

myapp.gdb

It might be tempting to place lots of commands in your .gdbinit file but best practice is to place them in a text file and load them using gdb -x myapp.gdb.

 b main
 run
 layout src

gdb -q myapp -x myapp.gdb

Then you can put your gdb command in a shell script to save typing or recall from the command history each time. See the end of this wiki to be able to auto load these command files when GDB starts up.

With .gdbinit and command files, there are a lot of options in this area. See the Gdb Documentation for more information.

.gitignore

If you are using git then a .gitignore in your repository directory with these files will be handy. The main one to stop git tracking your gdb history file.

.gdb_history
__pycache__
*.swp

tty gdb command

While debugging C printf() pretty much always start messing up your gdb session and you have to do (gdb) refresh a lot. Adding this to your gdb command file or just doing it in a gdb command prompt

tty /dev/pts/1

will direct such things to a different terminal. Use $ tty to find out what that terminal is and use in the tty command.

$ tty
/dev/pts/1
(gdb) tty /dev/pts/1

Auto-load Files in Current Directory

For an executable in your directory called myapp, gdb will autoload a gdb command file myapp-gdb.gdb and a python file myapp-gdb.py.

You need to add the directory to your home .gdbinit file. Otherwise gdb will give you a message about the various options to enable auto-loading of these files.

add-auto-load-safe-path /home/[user]/[directory]/

This is a more advanced technique over using 'gdb -x myapp.gdb' when you are comfortable about the contents of the file being automatically executed every time.

Clone this wiki locally