From patchwork Tue Dec 19 16:38:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Domani X-Patchwork-Id: 82524 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 304953858282 for ; Tue, 19 Dec 2023 16:39:34 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from sonic312-26.consmr.mail.ir2.yahoo.com (sonic312-26.consmr.mail.ir2.yahoo.com [77.238.178.97]) by sourceware.org (Postfix) with ESMTPS id C58803858C42 for ; Tue, 19 Dec 2023 16:39:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C58803858C42 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 C58803858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=77.238.178.97 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703003960; cv=none; b=XQhv7UCQ3M1qB1yjZ/kXW2c2ZR9wF6yAaiAqPL90p/WDFJsAXlEFHIAfUqlL313TRCA07wxcplmXy/rzC9foDtcqCaz08DyW429w1pqeo1T4EjQYy3Xxg+MkyPJ3GT641KndRb270AtZmzxgWzxit6LV43I4zVNNsXrFkHIaJNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703003960; c=relaxed/simple; bh=61LTADJUgg1CMrDLGusXUU2P90iW76/yJ5GPJWvNYb8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=SYJYIYpSJm4MLFi2ZyhEiIC0A7UsWzwTRtdO090vn7Vt1u1SpZS5E41oAziTrQQG6F2lhc4lzyYHYYhrGLaRw8VpHozHnxN6z2EYyWORmXQ70exTouNxAVNnlfOLnm4aS+HvQLt4A8ODfF+du94RjXbrVbYlUMzvdyYSqE5ofco= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.de; s=s2048; t=1703003956; bh=BZKPrMFe7zi1VcCYKrKt4xFZv2+IFWlaBhUI61da69M=; h=From:To:Subject:Date:References:From:Subject:Reply-To; b=bZ+ULfAHWOaVG9jddjwEEQc/Gqeb4mPcB7Haa4NB5Tqw1DtolDe1i09m/z/d1mnjPfEjMNloLQwO/F86/rL4AdjJXMkRY3JgUgnc9NYnB36IVUwPdOaqV6VSy8+SzASgel9MfvbySUYhwDDolat5+T7b9okW3xXB6rKgmUwc7+/R/t6dhSvm+4Q2PI1VQUrSVwdyJXqTcIA1RvXBKiw0IHDyTHXhRwDpfyz222gNzIPIArBKD5yyY8hI+Avc0UXEsU1nXTVsCY89bivaeETQlSKbvLV4qCOtHw0IBuxZ6iHecy+ynWkZriBhII0qNOC1tcoPVFMBvaLcgGwBNpI23g== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1703003956; bh=L2L6g1eoGnBqja7L7czTdNWctWXRY1g9SPHTV67Ctj6=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=uTCAqWqlYjLRSBSanLPbur63obQMImwZRFkVp+qlbDfZ1WBhUHy1i8D9RZUyGqvKMjSWV66HZGej7LkDM3s5u3l7zTLlFsIrGVPfwczDXCe0Fc4SCnKH8tkOR+7cHJ4+7UFBF6UMQnIfNvecAzBq+BF1iBPalA6/A04cd4ExWwvD6T/uvgCLEosY4bxzMsL7sk8dG80xkWUDuEZswHTIW5Dj0bYp4QAe/4RZ1oiYRXvzTu7AsgfKWCn00GKez3PbxCMJUucNYacm5Wvord4Gi5QvzNLOVtQqIUmCR+osFgWu631qGX2CrLcevLeR7eFVnjHwBMip2gvoCgCgW4I0LA== X-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plnzneEDoNX8Q_qcKDDLj.ZhUSQyXHDe8YnyRY6_mYWAVh4kvXbbe VrON4u_d0w1cYYzqv4Hk8Ho__HRbQgXLj2ytJ8cBc1DJ1FboGQlNnoFX8iNh.5aQGbLQ2Vn1Fw_L .oKHfXUW3HU.FdrIV0St40sZd61C9eg-- X-Sonic-MF: X-Sonic-ID: fac8565c-40a4-41d0-8994-bef66914b6c7 Received: from sonic.gate.mail.ne1.yahoo.com by sonic312.consmr.mail.ir2.yahoo.com with HTTP; Tue, 19 Dec 2023 16:39:16 +0000 Received: by hermes--production-ir2-6488884f6f-kblsg (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 640bef4781bf9582b4065561e6feafe5; Tue, 19 Dec 2023 16:39:11 +0000 (UTC) From: Hannes Domani To: gdb-patches@sourceware.org Subject: [PATCH v2] Raise exception if ambiguous name is used in gdb.parameter Date: Tue, 19 Dec 2023 17:38:51 +0100 Message-Id: <20231219163851.2748-1-ssbssa@yahoo.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Antivirus: Avast (VPS 231219-0, 12/19/2023), Outbound message X-Antivirus-Status: Clean References: <20231219163851.2748-1-ssbssa.ref@yahoo.de> X-Spam-Status: No, score=-10.5 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 ``` Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=14639 Reviewed-By: Guinevere Larsen --- 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. --- gdb/cli/cli-decode.c | 13 ++++++--- gdb/python/python.c | 5 +++- gdb/testsuite/gdb.python/py-parameter.exp | 32 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c index 940cd6a2c8e..db57764578d 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/python/python.c b/gdb/python/python.c index 951de82ef06..fa0b1ca6604 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.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp index c1f8a80c6b7..828fe84694d 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 ""