首页 文章

如何让gdb保存命令历史记录?

提问于
浏览
174

如何设置 gdb 以便保存命令历史记录?当我开始一个新的 gdb 会话时,我想使用向上箭头键来访问先前会话的命令 .

2 回答

  • 7

    简短回答: echo 'set history save on' >> ~/.gdbinit && chmod 600 ~/.gdbinit


    答案很长:

    命令历史记录包含在GDB manual, 22.3 Command History中 . 创建文件 $HOME/.gdbinit ,将其权限更改为 0600 ,并添加以下内容:

    set history save on
    

    您可以使用以下命令设置保存的过去命令的数量 . 该命令描述为"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>
    

    相关命令是 set history remove-duplicates <count> . 该命令描述为"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>
    

    默认情况下,gdb将历史记录保存到文件./.gdb_history in the current directory中 . 如果您希望命令历史记录不依赖于您所在的目录,还包括:

    set history filename ~/.gdb_history
    
  • 246

    如果您仍遇到问题,请确保您的HISTSIZE环境变量数字合适 . 我的是空的,导致gdb的“历史大小”设置默认为0 .

    添加

    export HISTSIZE=100000000
    

    到我的〜/ .bashrc,一切都很膨胀

    您可以通过执行(在gdb内部)“show history”来检查您的gdb历史记录设置:

    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.
    

    来自docs

    set history size size set history size unlimited设置GDB在其历史列表中保留的命令数 . 默认为环境变量HISTSIZE的值,如果未设置此变量,则默认为256 . 如果大小不受限制,则GDB在历史列表中保留的命令数量不受限制 .

相关问题