[3/5] Add "save user" command

Message ID 20230129162105.526266-4-tom@tromey.com
State New
Headers
Series Additions to "save" command |

Commit Message

Tom Tromey Jan. 29, 2023, 4:21 p.m. UTC
  PR cli/19395 points out that it would sometimes be convenient to save
one's user-defined commands to a file.  This patch implements this
feature.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19395
---
 gdb/NEWS            |  3 ++
 gdb/cli/cli-cmds.c  | 72 +++++++++++++++++++++++++++++++++++++--------
 gdb/doc/gdb.texinfo |  6 ++++
 3 files changed, 68 insertions(+), 13 deletions(-)
  

Comments

Eli Zaretskii Jan. 29, 2023, 4:57 p.m. UTC | #1
> From: Tom Tromey <tom@tromey.com>
> Cc: Tom Tromey <tom@tromey.com>
> Date: Sun, 29 Jan 2023 09:21:03 -0700
> 
> PR cli/19395 points out that it would sometimes be convenient to save
> one's user-defined commands to a file.  This patch implements this
> feature.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19395
> ---
>  gdb/NEWS            |  3 ++
>  gdb/cli/cli-cmds.c  | 72 +++++++++++++++++++++++++++++++++++++--------
>  gdb/doc/gdb.texinfo |  6 ++++
>  3 files changed, 68 insertions(+), 13 deletions(-)

OK for the documentation parts, thanks.
  
Pedro Alves Jan. 30, 2023, 2:53 p.m. UTC | #2
On 2023-01-29 4:21 p.m., Tom Tromey wrote:
> PR cli/19395 points out that it would sometimes be convenient to save
> one's user-defined commands to a file.  This patch implements this
> feature.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19395

Could we have a testcase for this?

Pedro Alves

> ---
>  gdb/NEWS            |  3 ++
>  gdb/cli/cli-cmds.c  | 72 +++++++++++++++++++++++++++++++++++++--------
>  gdb/doc/gdb.texinfo |  6 ++++
>  3 files changed, 68 insertions(+), 13 deletions(-)
> 
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 2bc1672632a..3b7d768732c 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -17,6 +17,9 @@ maintenance print record-instruction [ N ]
>    prints how GDB would undo the N-th previous instruction, and if N is
>    positive, it prints how GDB will redo the N-th following instruction.
>  
> +save user FILENAME
> +  Save all user-defined commands to the given file.
> +
>  * MI changes
>  
>  ** mi now reports 'no-history' as a stop reason when hitting the end of the
> diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
> index 61f890a7dae..3f488d1a544 100644
> --- a/gdb/cli/cli-cmds.c
> +++ b/gdb/cli/cli-cmds.c
> @@ -51,6 +51,7 @@
>  #include "cli/cli-style.h"
>  #include "cli/cli-utils.h"
>  #include "cli/cli-style.h"
> +#include "cli-out.h"
>  
>  #include "extension.h"
>  #include "gdbsupport/pathstuff.h"
> @@ -1646,24 +1647,41 @@ make_command (const char *arg, int from_tty)
>  /* Print the definition of user command C to STREAM.  Or, if C is a
>     prefix command, show the definitions of all user commands under C
>     (recursively).  PREFIX and NAME combined are the name of the
> -   current command.  */
> +   current command.  DEF is true if the output should be written as a
> +   source-able script.  */
>  static void
>  show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
> -	     struct ui_file *stream)
> +	     struct ui_file *stream, struct ui_out *uiout, bool def)
>  {
>    if (cli_user_command_p (c))
>      {
>        struct command_line *cmdlines = c->user_commands.get ();
>  
> -      gdb_printf (stream, "User %scommand \"",
> -		  c->is_prefix () ? "prefix" : "");
> -      fprintf_styled (stream, title_style.style (), "%s%s",
> -		      prefix, name);
> -      gdb_printf (stream, "\":\n");
> +      if (def)
> +	gdb_printf (stream, "define%s %s%s\n",
> +		    c->is_prefix () ? "-prefix" : "",
> +		    prefix, name);
> +      else
> +	{
> +	  gdb_printf (stream, "User %scommand \"",
> +		      c->is_prefix () ? "prefix" : "");
> +	  fprintf_styled (stream, title_style.style (), "%s%s",
> +			  prefix, name);
> +	  gdb_printf (stream, "\":\n");
> +	}
>        if (cmdlines)
>  	{
> -	  print_command_lines (current_uiout, cmdlines, 1);
> -	  gdb_puts ("\n", stream);
> +	  print_command_lines (uiout, cmdlines, 1);
> +	  if (!def)
> +	    gdb_puts ("\n", stream);
> +	}
> +      if (def)
> +	{
> +	  gdb_puts ("end\n", stream);
> +
> +	  if (!c->is_prefix () && !streq (c->doc, "User-defined."))
> +	    gdb_printf (stream, "document %s%s\n%s\nend\n",
> +			prefix, name, c->doc);
>  	}
>      }
>  
> @@ -1673,9 +1691,8 @@ show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
>  
>        for (c = *c->subcommands; c != NULL; c = c->next)
>  	if (c->theclass == class_user || c->is_prefix ())
> -	  show_user_1 (c, prefixname.c_str (), c->name, gdb_stdout);
> +	  show_user_1 (c, prefixname.c_str (), c->name, stream, uiout, def);
>      }
> -
>  }
>  
>  static void
> @@ -1690,18 +1707,40 @@ show_user (const char *args, int from_tty)
>        c = lookup_cmd (&comname, cmdlist, "", NULL, 0, 1);
>        if (!cli_user_command_p (c))
>  	error (_("Not a user command."));
> -      show_user_1 (c, "", args, gdb_stdout);
> +      show_user_1 (c, "", args, gdb_stdout, current_uiout, false);
>      }
>    else
>      {
>        for (c = cmdlist; c; c = c->next)
>  	{
>  	  if (cli_user_command_p (c) || c->is_prefix ())
> -	    show_user_1 (c, "", c->name, gdb_stdout);
> +	    show_user_1 (c, "", c->name, gdb_stdout, current_uiout, false);
>  	}
>      }
>  }
>  
> +/* The "save user" command.  */
> +
> +static void
> +save_user_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));
> +  stdio_file fp;
> +  if (!fp.open (expanded_filename.get (), "w"))
> +    error (_("Unable to open file '%s' for saving (%s)"),
> +	   expanded_filename.get (), safe_strerror (errno));
> +
> +  cli_ui_out uiout (&fp);
> +  for (struct cmd_list_element *c = cmdlist; c != nullptr; c = c->next)
> +    {
> +      if (cli_user_command_p (c) || c->is_prefix ())
> +	show_user_1 (c, "", c->name, &fp, &uiout, true);
> +    }
> +}
> +
>  /* Return true if COMMAND or any of its sub-commands is a user defined command.
>     This is a helper function for show_user_completer.  */
>  
> @@ -2762,6 +2801,13 @@ Usage: apropos [-v] REGEXP\n\
>  Flag -v indicates to produce a verbose output, showing full documentation\n\
>  of the matching commands."));
>  
> +  c = add_cmd ("user", no_class, save_user_command, _("\
> +Save current user-defined commands as a script.\n\
> +Usage: save user FILE\n\
> +Use the 'source' command in another debug session to restore them."),
> +	       &save_cmdlist);
> +  set_cmd_completer (c, filename_completer);
> +
>    add_setshow_uinteger_cmd ("max-user-call-depth", no_class,
>  			   &max_user_call_depth, _("\
>  Set the max call depth for non-python/scheme user-defined commands."), _("\
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index b5fad2cb16e..37db4785fd2 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -28230,6 +28230,12 @@ The value of @code{max-user-call-depth} controls how many recursion
>  levels are allowed in user-defined commands before @value{GDBN} suspects an
>  infinite recursion and aborts the command.
>  This does not apply to user-defined python commands.
> +
> +@kindex save user
> +@item save user @var{filename}
> +Save all user-defined commands to the file @var{filename}.  This
> +command writes out the user-defined commands as a script that can be
> +re-read into @value{GDBN} using the @code{source} command.
>  @end table
>  
>  In addition to the above commands, user-defined commands frequently
>
  
Tom Tromey Jan. 30, 2023, 11:35 p.m. UTC | #3
>>>>> "Pedro" == Pedro Alves <pedro@palves.net> writes:

Pedro> On 2023-01-29 4:21 p.m., Tom Tromey wrote:
>> PR cli/19395 points out that it would sometimes be convenient to save
>> one's user-defined commands to a file.  This patch implements this
>> feature.
>> 
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19395

Pedro> Could we have a testcase for this?

Oops, I thought I'd written one.
Sure, will do.

Tom
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 2bc1672632a..3b7d768732c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -17,6 +17,9 @@  maintenance print record-instruction [ N ]
   prints how GDB would undo the N-th previous instruction, and if N is
   positive, it prints how GDB will redo the N-th following instruction.
 
+save user FILENAME
+  Save all user-defined commands to the given file.
+
 * MI changes
 
 ** mi now reports 'no-history' as a stop reason when hitting the end of the
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 61f890a7dae..3f488d1a544 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -51,6 +51,7 @@ 
 #include "cli/cli-style.h"
 #include "cli/cli-utils.h"
 #include "cli/cli-style.h"
+#include "cli-out.h"
 
 #include "extension.h"
 #include "gdbsupport/pathstuff.h"
@@ -1646,24 +1647,41 @@  make_command (const char *arg, int from_tty)
 /* Print the definition of user command C to STREAM.  Or, if C is a
    prefix command, show the definitions of all user commands under C
    (recursively).  PREFIX and NAME combined are the name of the
-   current command.  */
+   current command.  DEF is true if the output should be written as a
+   source-able script.  */
 static void
 show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
-	     struct ui_file *stream)
+	     struct ui_file *stream, struct ui_out *uiout, bool def)
 {
   if (cli_user_command_p (c))
     {
       struct command_line *cmdlines = c->user_commands.get ();
 
-      gdb_printf (stream, "User %scommand \"",
-		  c->is_prefix () ? "prefix" : "");
-      fprintf_styled (stream, title_style.style (), "%s%s",
-		      prefix, name);
-      gdb_printf (stream, "\":\n");
+      if (def)
+	gdb_printf (stream, "define%s %s%s\n",
+		    c->is_prefix () ? "-prefix" : "",
+		    prefix, name);
+      else
+	{
+	  gdb_printf (stream, "User %scommand \"",
+		      c->is_prefix () ? "prefix" : "");
+	  fprintf_styled (stream, title_style.style (), "%s%s",
+			  prefix, name);
+	  gdb_printf (stream, "\":\n");
+	}
       if (cmdlines)
 	{
-	  print_command_lines (current_uiout, cmdlines, 1);
-	  gdb_puts ("\n", stream);
+	  print_command_lines (uiout, cmdlines, 1);
+	  if (!def)
+	    gdb_puts ("\n", stream);
+	}
+      if (def)
+	{
+	  gdb_puts ("end\n", stream);
+
+	  if (!c->is_prefix () && !streq (c->doc, "User-defined."))
+	    gdb_printf (stream, "document %s%s\n%s\nend\n",
+			prefix, name, c->doc);
 	}
     }
 
@@ -1673,9 +1691,8 @@  show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
 
       for (c = *c->subcommands; c != NULL; c = c->next)
 	if (c->theclass == class_user || c->is_prefix ())
-	  show_user_1 (c, prefixname.c_str (), c->name, gdb_stdout);
+	  show_user_1 (c, prefixname.c_str (), c->name, stream, uiout, def);
     }
-
 }
 
 static void
@@ -1690,18 +1707,40 @@  show_user (const char *args, int from_tty)
       c = lookup_cmd (&comname, cmdlist, "", NULL, 0, 1);
       if (!cli_user_command_p (c))
 	error (_("Not a user command."));
-      show_user_1 (c, "", args, gdb_stdout);
+      show_user_1 (c, "", args, gdb_stdout, current_uiout, false);
     }
   else
     {
       for (c = cmdlist; c; c = c->next)
 	{
 	  if (cli_user_command_p (c) || c->is_prefix ())
-	    show_user_1 (c, "", c->name, gdb_stdout);
+	    show_user_1 (c, "", c->name, gdb_stdout, current_uiout, false);
 	}
     }
 }
 
+/* The "save user" command.  */
+
+static void
+save_user_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));
+  stdio_file fp;
+  if (!fp.open (expanded_filename.get (), "w"))
+    error (_("Unable to open file '%s' for saving (%s)"),
+	   expanded_filename.get (), safe_strerror (errno));
+
+  cli_ui_out uiout (&fp);
+  for (struct cmd_list_element *c = cmdlist; c != nullptr; c = c->next)
+    {
+      if (cli_user_command_p (c) || c->is_prefix ())
+	show_user_1 (c, "", c->name, &fp, &uiout, true);
+    }
+}
+
 /* Return true if COMMAND or any of its sub-commands is a user defined command.
    This is a helper function for show_user_completer.  */
 
@@ -2762,6 +2801,13 @@  Usage: apropos [-v] REGEXP\n\
 Flag -v indicates to produce a verbose output, showing full documentation\n\
 of the matching commands."));
 
+  c = add_cmd ("user", no_class, save_user_command, _("\
+Save current user-defined commands as a script.\n\
+Usage: save user FILE\n\
+Use the 'source' command in another debug session to restore them."),
+	       &save_cmdlist);
+  set_cmd_completer (c, filename_completer);
+
   add_setshow_uinteger_cmd ("max-user-call-depth", no_class,
 			   &max_user_call_depth, _("\
 Set the max call depth for non-python/scheme user-defined commands."), _("\
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index b5fad2cb16e..37db4785fd2 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -28230,6 +28230,12 @@  The value of @code{max-user-call-depth} controls how many recursion
 levels are allowed in user-defined commands before @value{GDBN} suspects an
 infinite recursion and aborts the command.
 This does not apply to user-defined python commands.
+
+@kindex save user
+@item save user @var{filename}
+Save all user-defined commands to the file @var{filename}.  This
+command writes out the user-defined commands as a script that can be
+re-read into @value{GDBN} using the @code{source} command.
 @end table
 
 In addition to the above commands, user-defined commands frequently