]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add "save history" command
authorTom Tromey <tom@tromey.com>
Tue, 24 Jan 2023 00:40:16 +0000 (17:40 -0700)
committerTom Tromey <tom@tromey.com>
Thu, 19 Feb 2026 00:33:51 +0000 (17:33 -0700)
PR cli/23664 points out that it would sometimes be convenient to
immediately save the current history to a file.  This patch implements
this feature.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23664
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Keith Seitz <keiths@redhat.com>
gdb/NEWS
gdb/doc/gdb.texinfo
gdb/testsuite/gdb.base/save-history.exp [new file with mode: 0644]
gdb/top.c

index 888d1a468b3e1bb9680287540af82d8306962426..47d8189d3447d61d644770dc6ad5430a0d8481cf 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -81,6 +81,9 @@ unset local-environment
   environment.  The local environment is used by "shell", "pipe", and
   other commands that launch a subprocess other than an inferior.
 
+save history FILENAME
+  Save the command history to the given file.
+
 save skip FILENAME
   Save all current "skip"s to the given file.
 
index 92e7366428165e4a47144d12472b6903bd74d132..5b99c2b2e232e34194afb89ccbfc1c315c0c763d 100644 (file)
@@ -28127,6 +28127,17 @@ removal of duplicate history entries is disabled.
 Only history entries added during the current session are considered for
 removal.  This option is set to 0 by default.
 
+@kindex save history
+@item save history @var{filename}
+Occasionally you may want to save your history to a file.  This
+command will do just that, saving the commands in your current session
+to the named file.  Note that you should normally edit the saved
+history before using @code{source} to reload it, as the @code{save
+history} command itself will be at the end of the file.
+
+Alternatively, you can use @code{server save history} to avoid this
+issue, as the @code{server} prefix prevents the command from being
+recorded (@pxref{Server Prefix}).
 @end table
 
 History expansion assigns special meaning to the character @kbd{!}.
diff --git a/gdb/testsuite/gdb.base/save-history.exp b/gdb/testsuite/gdb.base/save-history.exp
new file mode 100644 (file)
index 0000000..bb7a56b
--- /dev/null
@@ -0,0 +1,36 @@
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the gdb testsuite.
+
+# Test the "save history" command.
+
+require {!is_remote host}
+
+gdb_start
+
+# Get something in history.
+gdb_test "print 23" " = 23"
+
+set filename [standard_output_file histfile]
+gdb_test_no_output "save history $filename" \
+    "save history"
+
+set expected "set height 0\n"
+append expected "set width 0\n"
+append expected "print 23\n"
+append expected "save history $filename\n"
+
+cmp_file_string $filename $expected "check history contents"
index d9966c6ec693983b8d0d947d3fa72a8c7ff81ccb..76dc2d27fc5b04dc126f1a1c92ad396b1f5c8aee 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1258,6 +1258,18 @@ gdb_safe_append_history (void)
     }
 }
 
+/* Implementation of 'save history' command.  */
+
+static void
+save_history_command (const char *filename, int from_tty)
+{
+  if (filename == nullptr || *filename == '\0')
+    error (_("Argument required (file name in which to save)"));
+
+  gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
+  write_history (expanded_filename.get ());
+}
+
 /* Read one line from the command input stream `instream'.
 
    CMD_LINE_BUFFER is a buffer that the function may use to store the result, if
@@ -2413,6 +2425,13 @@ Without an argument, saving is enabled."),
                           show_write_history_p,
                           &sethistlist, &showhistlist);
 
+  cmd_list_element *c = add_cmd ("history", no_class, save_history_command,
+                                _("\
+Save current history as a script.\n\
+Usage: save history FILE"),
+              &save_cmdlist);
+  set_cmd_completer (c, deprecated_filename_completer);
+
   add_setshow_zuinteger_unlimited_cmd ("size", no_class,
                                       &history_size_setshow_var, _("\
 Set the size of the command history."), _("\