From: Pedro Alves <pedro@palves.net>
To: Tom Tromey <tom@tromey.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 3/5] Add "save user" command
Date: Mon, 30 Jan 2023 14:53:53 +0000 [thread overview]
Message-ID: <1daf98a0-8568-8eac-310f-8688a611677d@palves.net> (raw)
In-Reply-To: <20230129162105.526266-4-tom@tromey.com>
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
>
next prev parent reply other threads:[~2023-01-30 14:53 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-29 16:21 [PATCH 0/5] Additions to "save" command Tom Tromey
2023-01-29 16:21 ` [PATCH 1/5] Save breakpoints so they are automatically pending Tom Tromey
2023-01-29 16:21 ` [PATCH 2/5] Move show_user_1 to cli-cmds.c Tom Tromey
2023-01-29 16:21 ` [PATCH 3/5] Add "save user" command Tom Tromey
2023-01-29 16:57 ` Eli Zaretskii
2023-01-30 14:53 ` Pedro Alves [this message]
2023-01-30 23:35 ` Tom Tromey
2023-01-29 16:21 ` [PATCH 4/5] Add "save skip" command Tom Tromey
2023-01-29 16:54 ` Eli Zaretskii
2023-01-29 16:21 ` [PATCH 5/5] Add "save history" command Tom Tromey
2023-01-29 16:56 ` Eli Zaretskii
2023-01-30 14:50 ` Pedro Alves
2023-01-30 15:12 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1daf98a0-8568-8eac-310f-8688a611677d@palves.net \
--to=pedro@palves.net \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).