How can I make gdb save the command history?

DebuggingGdb

Debugging Problem Overview


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.

Debugging Solutions


Solution 1 - Debugging

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

Solution 2 - Debugging

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrankView Question on Stackoverflow
Solution 1 - DebuggingFrankView Answer on Stackoverflow
Solution 2 - DebuggingXiaoView Answer on Stackoverflow