Raise exception if ambiguous name is used in gdb.parameter

Currently gdb.parameter doesn't raise an exception if an
ambiguous name is used, it instead returns the value of the
last partly matching parameter:
```
(gdb) show print sym
Ambiguous show print command "sym": symbol, symbol-filename, symbol-loading.
(gdb) show print symbol-loading
Printing of symbol loading messages is "full".
(gdb) py print(gdb.parameter("print sym"))
full
```

It's because lookup_cmd_composition_1 tries to detect
ambigous names by checking the return value of find_cmd
for CMD_LIST_AMBIGUOUS, which never happens, since only
lookup_cmd_1 returns CMD_LIST_AMBIGUOUS.
Instead the nfound argument contains the number of found
matches.

By using it instead, and by setting *CMD to the special value
CMD_LIST_AMBIGUOUS in this case, gdbpy_parameter can now show
the appropriate error message:
```
(gdb) py print(gdb.parameter("print sym"))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError: Parameter `print sym' is ambiguous.
Error while executing Python code.
(gdb) py print(gdb.parameter("print symbol"))
True
(gdb) py print(gdb.parameter("print symbol-"))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError: Parameter `print symbol-' is ambiguous.
Error while executing Python code.
(gdb) py print(gdb.parameter("print symbol-load"))
full
```

Since the document command also uses lookup_cmd_composition, it needed
to check for CMD_LIST_AMBIGUOUS as well, so it now also shows an
"Ambiguous command" error message in this case.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=14639
Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Hannes Domani
2024-02-07 19:59:29 +01:00
parent ad7b7cb1f4
commit 276d9db22d
5 changed files with 58 additions and 4 deletions

View File

@@ -2549,7 +2549,8 @@ deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
If TEXT is a subcommand (i.e. one that is preceded by a prefix
command) set *PREFIX_CMD.
Set *CMD to point to the command TEXT indicates.
Set *CMD to point to the command TEXT indicates, or to
CMD_LIST_AMBIGUOUS if there are multiple possible matches.
If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
exist, they are NULL when we return.
@@ -2589,7 +2590,12 @@ lookup_cmd_composition_1 (const char *text,
*cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
/* We only handle the case where a single command was found. */
if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
if (nfound > 1)
{
*cmd = CMD_LIST_AMBIGUOUS;
return 0;
}
else if (*cmd == nullptr)
return 0;
else
{
@@ -2623,7 +2629,8 @@ lookup_cmd_composition_1 (const char *text,
If TEXT is a subcommand (i.e. one that is preceded by a prefix
command) set *PREFIX_CMD.
Set *CMD to point to the command TEXT indicates.
Set *CMD to point to the command TEXT indicates, or to
CMD_LIST_AMBIGUOUS if there are multiple possible matches.
If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
exist, they are NULL when we return.