Re: [PATCH v5 1/2] gdb: improve help text for set commands with limited options

Andrew Burgess
2026-03-17T13:59:41Z

Guinevere Larsen <guinevere@redhat.com> writes:

> Some "set" commands only allow a select few options, such as the "set
> architecture" command, however, there is no way for a user to know which
> options are allowed without trying to set something and getting an
> error. This commit improves the situation by making the help command list
> all the available options.

I was experimenting with this patch looking at how other enum set
commands, checking that their output also looks good with this new
feature, and I ran into 'help set demangle-style'. It's output (before
this patch) is:

(gdb) help set demangle-style
Set the current C++ demangling style.
Use `set demangle-style' without arguments for a list of demangling styles.

And indeed:

(gdb) set demangle-style
Requires an argument. Valid arguments are none, auto, gnu-v3, java, gnat, dlang, rust.

And:

(gdb) set architecture
Requires an argument. Valid arguments are ARC600, A6, ARC601, ARC700, A7, ... etc ...

So there is an existing way to get an option list. Maybe what's missing
here is that most enum command help texts don't reference this mechanism.

What if, instead of listing all the options, which can get pretty long,
you auto-added a sentence similar to the one seen in the demangle-style
output? E.g. "Use `set <command>` without arguments for a list of
available options." This would keep the help text less cluttered, but
would give clear guidance on how to get the option list.

Just my thoughts.

Thanks,
Andrew

>
> Reviewed-By: Ciaran Woodward <ciaranwoodward@xmos.com>
> ---
> gdb/cli/cli-decode.c | 12 ++++++++++++
> gdb/testsuite/gdb.base/help.exp | 9 +++++++++
> gdb/testsuite/gdb.python/py-doc-reformat.exp | 2 +-
> gdb/testsuite/gdb.python/py-parameter.exp | 17 +++++++++++++----
> 4 files changed, 35 insertions(+), 5 deletions(-)
>
> diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
> index 285f5f1f0c4..5a580387fee 100644
> --- a/gdb/cli/cli-decode.c
> +++ b/gdb/cli/cli-decode.c
> @@ -1876,6 +1876,18 @@ help_cmd (const char *command, struct ui_file *stream)
> /* Be sure to expand TABs in the documentation. */
> tab_expansion_file expander (stream);
> gdb_puts (c->doc, &expander);
> + if (c->enums != nullptr)
> + {
> + gdb_puts ("\nAvailable options are:\n", &expander);
> + const char * const *opt = c->enums;
> + gdb_printf (&expander, " ");
> + while (opt != nullptr && *opt != nullptr)

The 'opt != nullptr' check here seems redundant. We already check
'c->enums != nullptr' before entering this block, and I think if 'opt++'
causes us to reach nullptr then things have probably gone seriously
wrong.

You could fold the setup of 'opt', the condition check, and the
increment into a for loop like:

for (const char * const *opt = c->enums;
*opt != nullptr;
opt++)
{ ... }

> + {
> + expander.wrap_here (2);
> + gdb_printf (&expander, "%s, ", *opt);
> + opt++;

Given the simplified loop condition check, you could solve the trailing
', ' like:

{
expander.wrap_here (2);
const char *suffix = (*(opt + 1) == nullptr) ? "." : ", ";
gdb_printf (&expander, "%s%s", *opt, suffix);
}

> + }
> + }
> }
> else
> {
> diff --git a/gdb/testsuite/gdb.base/help.exp b/gdb/testsuite/gdb.base/help.exp
> index a8c55721ef2..fca8522faa0 100644
> --- a/gdb/testsuite/gdb.base/help.exp
> +++ b/gdb/testsuite/gdb.base/help.exp
> @@ -174,3 +174,12 @@ gdb_test "help mybt" " alias mybt = backtrace \[\r\n\]+An alias of command backt
> # Check pre-defined aliases cannot be documented.
> gdb_test "document where" "Alias \"where\" is built-in.*" \
> "documenting builtin where alias disallowed"
> +
> +# Check help of a command with limited options lists all the options
> +# as expected. The command "set breakpoint condition-evaluation" was
> +# chosen because it has few options and isn't dependent on configure
> +# options.
> +gdb_test "help set breakpoint condition-evaluation" \
> + [multi_line ".*" \
> + "Available options are:" \
> + " auto, host, target, "]
> diff --git a/gdb/testsuite/gdb.python/py-doc-reformat.exp b/gdb/testsuite/gdb.python/py-doc-reformat.exp
> index 8b0a27f4937..a3c0b85c9e8 100644
> --- a/gdb/testsuite/gdb.python/py-doc-reformat.exp
> +++ b/gdb/testsuite/gdb.python/py-doc-reformat.exp
> @@ -126,7 +126,7 @@ proc test { input_docs expected_output } {
> -re "^This is the set doc line\r\n" {
> exp_continue
> }
> - -re "^$expected_output\r\n$::gdb_prompt $" {
> + -re "^$expected_output\r\nAvailable options are:\r\n on, off, \r\n$::gdb_prompt $" {
> pass $gdb_test_name
> }
> }
> diff --git a/gdb/testsuite/gdb.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp
> index e8b0ef24efb..aec00d527b2 100644
> --- a/gdb/testsuite/gdb.python/py-parameter.exp
> +++ b/gdb/testsuite/gdb.python/py-parameter.exp
> @@ -367,7 +367,10 @@ proc_with_prefix test_empty_doc_parameter {} {
> # Setting the __doc__ string to empty means GDB will completely
> # elide it from the output.
> gdb_test "help set print test-empty-doc-param" \
> - "^Set the current value of 'print test-empty-doc-param'\\."
> + [multi_line \
> + "^Set the current value of 'print test-empty-doc-param'\\." \
> + "Available options are:" \
> + " on, off, "]
>
> gdb_test_multiline "None __doc__ parameter" \
> "python" "" \
> @@ -384,7 +387,9 @@ proc_with_prefix test_empty_doc_parameter {} {
> gdb_test "help set print test-none-doc-param" \
> [multi_line \
> "^Set the current value of 'print test-none-doc-param'\\." \
> - "This command is not documented\\."]
> + "This command is not documented\\." \
> + "Available options are:" \
> + " on, off, "]
> }
>
> # Test a parameter in which the set_doc/show_doc strings are either
> @@ -406,7 +411,9 @@ proc_with_prefix test_empty_set_show_doc_parameter {} {
> gdb_test "help set print test-empty-set-show-param" \
> [multi_line \
> "^Set the current value of 'print test-empty-set-show-param'\\." \
> - "This command is not documented\\."]
> + "This command is not documented\\." \
> + "Available options are:" \
> + " on, off, "]
>
> gdb_test "help show print test-empty-set-show-param" \
> [multi_line \
> @@ -429,7 +436,9 @@ proc_with_prefix test_empty_set_show_doc_parameter {} {
> gdb_test "help set print test-none-set-show-param" \
> [multi_line \
> "^Set the current value of 'print test-none-set-show-param'\\." \
> - "This command is not documented\\."]
> + "This command is not documented\\." \
> + "Available options are:" \
> + " on, off, "]
>
> gdb_test "help show print test-none-set-show-param" \
> [multi_line \
> --
> 2.53.0