247

How can I set up gdb so that it saves the command history? When starting a new gdb session I'd like to use the arrow up keys to access the commands of the previous sessions.

Frank
  • 64,140
  • 93
  • 237
  • 324
  • 1
    Note: The `.gdbinit` file has to be write-protected by others than you on linux. – urzeit Jul 03 '13 at 08:47
  • @urzeit - I think that's a lost cause on distros like Ubuntu. They allow anyone into anyone else's home directory by default. – jww Jul 30 '16 at 17:47
  • 1
    @jww The `.gdbinit` file is not processed if the permissions are not set up correctly, no matter what is the default or what other limitations on home-directories are used by the system. – urzeit Aug 02 '16 at 05:18
  • @urzeit If you create a `.gdbinit` file and that is not the case, it means your `umask` isn't set to a sane value like `022`. Then in a multi-user system, you have bigger problems than just your `.gdbinit` file. – Kaz Apr 09 '20 at 17:12

3 Answers3

364

Short answer:

mkdir -p ~/.config/gdb
echo 'set history save on' >> ~/.config/gdb/gdbinit

Long answer:

Command history is covered in the GDB manual, 22.3 Command History. Create a file $HOME/.config/gdb/gdbinit, and add the following line:

set history save on

You can set the number of past commands saved with the following. The command is described as "Set the number of commands which gdb keeps in its history list. This defaults to the value of the environment variable GDBHISTSIZE, or to 256 if this variable is not set. Non-numeric values of GDBHISTSIZE are ignored. If size is unlimited or if GDBHISTSIZE is either a negative number or the empty string, then the number of commands gdb keeps in the history list is unlimited".

set history size <size>

A related command is set history remove-duplicates <count>. The command is described as "Control the removal of duplicate history entries in the command history list. If count is non-zero, gdb will look back at the last count history entries and remove the first entry that is a duplicate of the current entry being added to the command history list. If count is unlimited then this lookbehind is unbounded. If count is 0, then removal of duplicate history entries is disabled".

set history remove-duplicates <count>

By default, gdb saves the history into the file ./.gdb_history in the current directory. If you want your command history not to depend on the directory you are in, also include:

set history filename ~/.gdb_history
Slim
  • 648
  • 4
  • 15
Frank
  • 64,140
  • 93
  • 237
  • 324
  • 5
    Just found this out for myself and thought I'd share it on SO. – Frank Jul 05 '10 at 01:21
  • 4
    For next time, it's perfectly legitimate to create such self-answered questions as non-community-wiki. :) – Greg Hewgill Jul 05 '10 at 01:46
  • @Frank How about if I type `n` then I arrow up key, I get all previous commands that start with `n`. – SIFE Jan 27 '13 at 00:09
  • On GDB 8.0.1 this appears to work without the special file permissions. – Ryan1729 Nov 15 '17 at 22:19
  • @Ryan1729 Permissions of 0600 (octal) are not mandatory. 0644 (read/write to the owner, read to everybody else) will also work. Many linux variants set umask to 0022, which means, that files are created with 0644, so they are fine for the purpose of `.gdbinit` then. But Debian/Ubuntu create a private group for each user and sets umask to 0002, so that files by default are also writeable to anybody in that private group. This eases account sharing (just add your friend to your private group to give them full access), but violates gdbs security requirement for `.gdbinit`. – Kai Petzke May 16 '21 at 14:19
  • 2
    I'm running Ubuntu 20.04, and this setup only worked for me when I created the init file in `$HOME/.gdbinit`. This is my version: "GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2" – mper Jul 24 '22 at 13:04
  • Needed to use `$HOME/.gdbinit` on Fedora 35 and RHEL 8 – Aaron Swan Jan 05 '23 at 20:52
9

If you're still having trouble, make sure your HISTSIZE environment variable is a suitably high number. Mine was empty, causing gdb's "history size" setting to default to 0.

Added

export HISTSIZE=100000000

to my ~/.bashrc and everything is swell

You can check your gdb history settings by doing (inside gdb) "show history":

gdb$ show history
expansion:  History expansion on command input is off.
filename:  The filename in which to record the command history is "/home/xiao/.gdb_history".
save:  Saving of the history record on exit is on.
size:  The size of the command history is 100000000.

From the docs:

set history size size
set history size unlimited
Set the number of commands which GDB keeps in its history list. This defaults to the value of the environment variable HISTSIZE, or to 256 if this variable is not set. If size is unlimited, the number of commands GDB keeps in the history list is unlimited.

Xiao
  • 1,552
  • 2
  • 22
  • 20
  • 1
    I had the same problem. Despite putting `set history size 100` in the .gdbinit file, the HISTSIZE environment variable was still causing the history size to be set to zero. The problem is that gdb doesn't understand that [HISTSIZE can be set to unlimited](http://stackoverflow.com/questions/9457233/unlimited-bash-history) – DavidW Nov 17 '15 at 13:06
  • 1
    The HISTSIZE bug in gdb has been [fixed as of June 2015](https://sourceware.org/bugzilla/show_bug.cgi?id=16999), not that this change is likely to be present on your system yet. In the meanwhile, if `HISTSIZE` is set it must be > 0, or gdb will disable history. – Nate R. Feb 26 '16 at 18:33
0

In Ubuntu 22.04 set ~/.bashrc with

export GDBHISTSIZE=10000

then run

source ~/.bashrc
Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50