From patchwork Mon Jan 29 15:59:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Domani X-Patchwork-Id: 84869 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 938373858281 for ; Mon, 29 Jan 2024 16:00:04 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from sonic308-18.consmr.mail.ir2.yahoo.com (sonic308-18.consmr.mail.ir2.yahoo.com [77.238.178.146]) by sourceware.org (Postfix) with ESMTPS id 7E43B3858C2D for ; Mon, 29 Jan 2024 15:59:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7E43B3858C2D Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=yahoo.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=yahoo.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7E43B3858C2D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=77.238.178.146 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706543974; cv=none; b=s/tdf0PEYkRhXVukvl6cJrBMKuIu1RjPNmIr7nZWjJ1Q6nA8/bntfNmNM+uNaGnzeKuyr1yvFeYL0akcw6KfCyvJ0xENy5CmMXGpJ2X3WlxfmFrJUC3SPPwsd9FiWK0Cjm16N8RrXcFNFZohbdxOIS+/Bu1voKok0wv9Smr4mno= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706543974; c=relaxed/simple; bh=BDI2hN/K4aPcJmLUWJoM9vh89YNsj8TK/4iQW6iT5kQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Rypk4B2cirrUdqkBEgWsbAHXaEc3WKGBlz+DkIhZ/lSzD/bJCfkZes7CUmYo4J/CSahHIWraENXqr0TASr/CjccCSzC1/hdDRgcuO5TV5ZHaKFaHPK7AAnDemKDARj9gvpyHVcok6AHubA007aOYWKd7+wXvR/Py1xYHvIt6iAo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.de; s=s2048; t=1706543970; bh=8PCSB2XBeROhC/IBmdZOiN8vXu0GwQC6L8RAig3fLmE=; h=From:To:Subject:Date:References:From:Subject:Reply-To; b=WqYEchdLcZyeEwjIwfcjQykf7GcXWeLVUuWRr9L3kqDa3F+aNrRmo4tqAXBQ0b6WcYCYnruOCCrg9YeuQuxleMRHUMylBYPNaayuWwsuZjhYXLKRS7vuPw6siTvmDDRJ0SmdK6K4v6pzWO/ryUXzXxm1rCVf3k4QKqmOQvvrZjwtachN9fXG1BP0dTiEcI03RyNM0Z25TJDMmtgkggnzyg90tvzZKUrGk6cL5OI48w9QE0hUZkmHew4GeErn80N1wFdW/BuB6g5SW4ngAy5+a3FCleeJqJTvNhziAQvyS03Islsiz7kJMvuTTwIoWNg4gyw2MNPgSHnf6NVIxYb20w== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1706543970; bh=/+ZFSGJaA9jMhfCD0w9dIBTkqLkrP1kXuEH2ccf5p/j=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=rFhf7bB4QCcYrwxLbFdk1rVAGN7hNh1MXg4lEzIiQ7JehVNPgoBFrTItLcAeX/90LUNvl7UiM/XxWxJk+dhdUFh9EYj6YTyFPJf0juqy82o0Eq/Z6c5YufAFP1p9/gEf9KtS9cqVSFkFtuA2dkUGDgQhjK4KCS2SS7Pu7kJ+oUHEHgPegN8N7sLUm1g4liy0LSzhJ3QLksx0S4vUsTjADBJkP6c8gefRbKbrNG7YOflPJh+FwisWtlLNGIPrIkUVQnlx5sC5zVTmQcAtsYqvgP5erSd2xSwDuGafEGjepHl5VjEpxxtwtc8BrKw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1b_aSEsjtQLkEwRardR_P yc3e69DNIJcF0267_TUN1VUz..TJMicmnvtywIjRm8pPuyHxm.5FZfCqCVQV3AvGS_qNEdZGxnf7 SeukobwUPoZqL2zXIJ1aGX08QsI33WcHWx9bCG9aybrg99K8QDnyls._6U4JQTxMjqI8vshG.eB9 sz1WDxZvSonLdwNY7grOB8XfN3p8YScM- X-Sonic-MF: X-Sonic-ID: b3bbd8f8-e0f6-48d2-9741-09b67456c441 Received: from sonic.gate.mail.ne1.yahoo.com by sonic308.consmr.mail.ir2.yahoo.com with HTTP; Mon, 29 Jan 2024 15:59:30 +0000 Received: by hermes--production-ir2-7bc88bfc75-fzfp2 (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 85254c9fb6d4ae5ddb874506b71b2197; Mon, 29 Jan 2024 15:59:29 +0000 (UTC) From: Hannes Domani To: gdb-patches@sourceware.org Subject: [PATCH v3] Raise exception if ambiguous name is used in gdb.parameter Date: Mon, 29 Jan 2024 16:59:17 +0100 Message-Id: <20240129155917.3135-1-ssbssa@yahoo.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Antivirus: Avast (VPS 240129-2, 01/29/2024), Outbound message X-Antivirus-Status: Clean References: <20240129155917.3135-1-ssbssa.ref@yahoo.de> X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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 "", line 1, in 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 "", line 1, in 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 --- v2: - Small changes in commit message. - Use CMD_LIST_AMBIGUOUS in lookup_cmd_composition_1 to mark ambiguous commands, so gdbpy_parameter can show a better error message. v3: - Adapt do_document_command (to fix crash), which now also shows an "Ambiguous command" error, plus test. --- gdb/cli/cli-decode.c | 13 ++++++--- gdb/cli/cli-script.c | 2 ++ gdb/python/python.c | 5 +++- gdb/testsuite/gdb.base/document.exp | 10 +++++++ gdb/testsuite/gdb.python/py-parameter.exp | 32 +++++++++++++++++++++++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c index a69939e9035..bbabd40db07 100644 --- a/gdb/cli/cli-decode.c +++ b/gdb/cli/cli-decode.c @@ -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. diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c index bdf5ab8360c..cfb32bb9c9b 100644 --- a/gdb/cli/cli-script.c +++ b/gdb/cli/cli-script.c @@ -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)) diff --git a/gdb/python/python.c b/gdb/python/python.c index 9fb5972bc21..b3adb264915 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -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); diff --git a/gdb/testsuite/gdb.base/document.exp b/gdb/testsuite/gdb.base/document.exp index b7cc92fb912..8a5e7d0d634 100644 --- a/gdb/testsuite/gdb.base/document.exp +++ b/gdb/testsuite/gdb.base/document.exp @@ -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\"." diff --git a/gdb/testsuite/gdb.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp index 18c6837b1f1..94702f42816 100644 --- a/gdb/testsuite/gdb.python/py-parameter.exp +++ b/gdb/testsuite/gdb.python/py-parameter.exp @@ -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 ""