]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Raise exception if ambiguous name is used in gdb.parameter
authorHannes Domani <ssbssa@yahoo.de>
Wed, 7 Feb 2024 18:59:29 +0000 (19:59 +0100)
committerHannes Domani <ssbssa@yahoo.de>
Wed, 7 Feb 2024 18:59:32 +0000 (19:59 +0100)
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>
gdb/cli/cli-decode.c
gdb/cli/cli-script.c
gdb/python/python.c
gdb/testsuite/gdb.base/document.exp
gdb/testsuite/gdb.python/py-parameter.exp

index a69939e9035781d7285dc1e1a4e59571fbd2d065..bbabd40db0714f1a7819e7d1145cec88648c4084 100644 (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.
index 439ba5004c7ba4d1aa556f1be0059cd52e786fe0..f06724af325af1645978b9db60299834c39bbc1e 100644 (file)
@@ -1521,6 +1521,8 @@ do_document_command (const char *comname, int from_tty,
   lookup_cmd_composition (comfull, &alias, &prefix_cmd, &c);
   if (c == nullptr)
     error (_("Undefined command: \"%s\"."), comfull);
+  else if (c == CMD_LIST_AMBIGUOUS)
+    error (_("Ambiguous command: \"%s\"."), comfull);
 
   if (c->theclass != class_user
       && (alias == nullptr || alias->theclass != class_alias))
index de3a94dfc9ad518372dc508f9eae16b52efc034d..fc1de8b9441c3f213020fc8f7cb82c08b6e29f08 100644 (file)
@@ -559,7 +559,10 @@ gdbpy_parameter (PyObject *self, PyObject *args)
       GDB_PY_HANDLE_EXCEPTION (ex);
     }
 
-  if (!found)
+  if (cmd == CMD_LIST_AMBIGUOUS)
+    return PyErr_Format (PyExc_RuntimeError,
+                        _("Parameter `%s' is ambiguous."), arg);
+  else if (!found)
     return PyErr_Format (PyExc_RuntimeError,
                         _("Could not find parameter `%s'."), arg);
 
index b7cc92fb9121170f47e74cc91a030cfb27744e83..8a5e7d0d6342bb351500176f1ee486ac1d999dcf 100644 (file)
@@ -32,3 +32,13 @@ gdb_test "help do-document" "usage: do-document" "invoke help do-document"
 # Test that document of a non-existing command prints an error.  There
 # was a regression at one point causing this to crash.
 gdb_test "document nosuchcommand" "Undefined command: \"nosuchcommand\"\\."
+
+# Test that document of an ambiguous command prints an error.
+foreach_with_prefix cmd {1 2} {
+    gdb_test_multiple "define ambiguous_command_$cmd" "" {
+       -re "Type commands for definition of \"ambiguous_command_$cmd\".\r\nEnd with a line saying just \"end\".\r\n>$" {
+           gdb_test "end" "" "define ambiguous_command_$cmd"
+       }
+    }
+}
+gdb_test "document ambiguous_command" "Ambiguous command: \"ambiguous_command\"."
index 18c6837b1f1ff0a63d514247ef343e9e8f32a4dd..94702f42816a3efd3d617613f8388a10552765c4 100644 (file)
@@ -588,6 +588,37 @@ proc_with_prefix test_language {} {
     gdb_test_no_output "set lang auto"
 }
 
+proc_with_prefix test_ambiguous_parameter {} {
+    gdb_test_multiline "create parameter" \
+       "python" "" \
+       "class TestAmbiguousParam (gdb.Parameter):" "" \
+       "   def __init__ (self, name, value):" "" \
+       "      super (TestAmbiguousParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_INTEGER)" "" \
+       "      self.value = value" "" \
+       "end"
+
+    # Create parameters.
+    gdb_test "python TestAmbiguousParam('test-ambiguous-value-1', 1)" ""
+    gdb_test "python TestAmbiguousParam('test-ambiguous-value-2-extra', 2)" ""
+    gdb_test "python TestAmbiguousParam('test-ambiguous', 3)" ""
+
+    # Test unambiguous matches.
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-1'))" "1"
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-2-extra'))" "2"
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-2'))" "2"
+    gdb_test "python print(gdb.parameter('test-ambiguous'))" "3"
+
+    # Test ambiguous names.
+    gdb_test "python print(gdb.parameter('test-ambiguou'))" \
+       "Parameter .* is ambiguous.*Error while executing Python code."
+    gdb_test "python print(gdb.parameter('test-ambiguous-'))" \
+       "Parameter .* is ambiguous.*Error while executing Python code."
+    gdb_test "python print(gdb.parameter('test-ambiguous-v'))" \
+       "Parameter .* is ambiguous.*Error while executing Python code."
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-1a'))" \
+       "Could not find parameter.*Error while executing Python code."
+}
+
 test_directories
 test_data_directory
 test_boolean_parameter
@@ -600,5 +631,6 @@ test_gdb_parameter
 test_integer_parameter
 test_throwing_parameter
 test_language
+test_ambiguous_parameter
 
 rename py_param_test_maybe_no_output ""