From patchwork Thu May 18 20:18:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69631 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 C502338432C2 for ; Thu, 18 May 2023 20:18:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C502338432C2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441117; bh=Gk+BALlw0ifxM8gxs1OA3S00f5IfYDlxLuF/J/2edVI=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ohoDRKJ6wZtj3XSIHDlMfzScdhffo91ieLR/SQmksG30buuTw1ztrYBXVw7eRC5oB 2znQEh4Ip0pboXDhwgF+o3dwqSHnH+5Jjm6mPaGWJh+zKjqONmoOD/AYx/hB8XewzA PdjVvtajqCoTI/xsAPZM+TjHN1NdM6na4mRdBpD0= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id 54F7E3858CDB for ; Thu, 18 May 2023 20:18:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54F7E3858CDB Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-33818ccd411so19372365ab.1 for ; Thu, 18 May 2023 13:18:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441089; x=1687033089; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gk+BALlw0ifxM8gxs1OA3S00f5IfYDlxLuF/J/2edVI=; b=AU1Nw8nPyR/bnlZEeX8M6EcszavH+U/q1100pHSgQNBM+6a0mTUsjKh8s5/V0V5cxv ThqfjS1Pz0JcYONGQ8rwNTbSEAxSS6H6ypbON3vRuXNzjlKvUUnDYCU8ogZjrK8p3Sjv lDV1+apf1o3wjZjWGii4V6N6dgegCIFJlKw+mk2Atk0hfKkEkXXWbjomrHg3J7L/755O D2SwanNSTGBT4IGdRSYlgW8PmK0EZeeKaKkgWgjk2YyycdoF6xcLLagzl1Y1U28bqDeY k/1jZ4yrl+CNpmTx5hyxIHhntzW4j5mGSAHQu5+rfOqUMb9GU7haHOfpXlZyCtcbpVNC 5DdQ== X-Gm-Message-State: AC+VfDywZgDGByhyP+wCG+yvpC/sD9BW+Wz9154m0bW/PkTKYxkmZywG S0uhdoDyZpU8/TCyYJRLNCP+wkpAuvRJsafjm1lDWg== X-Google-Smtp-Source: ACHHUZ4liVEf4XQG0dItGQ29RPYnHj0RKUruNlPOuFQjgrOekEzh1zz2TxMjdVo21HOI5UdwILGHbA== X-Received: by 2002:a92:db03:0:b0:331:a2f5:b50d with SMTP id b3-20020a92db03000000b00331a2f5b50dmr5681076iln.30.1684441089593; Thu, 18 May 2023 13:18:09 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:09 -0700 (PDT) Date: Thu, 18 May 2023 14:18:07 -0600 Subject: [PATCH v2 1/9] Use field_signed from Python MI commands MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-1-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" If an MI command written in Python includes a number in its output, currently that is simply emitted as a string. However, it's convenient for a later patch if these are emitted using field_signed. This does not make a difference to ordinary MI clients. --- gdb/python/py-micmd.c | 15 +++++++++++++++ gdb/python/python-internal.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c index e86807d049f..d7d95918cb4 100644 --- a/gdb/python/py-micmd.c +++ b/gdb/python/py-micmd.c @@ -293,6 +293,21 @@ serialize_mi_result_1 (PyObject *result, const char *field_name) } else { + if (PyLong_Check (result)) + { + int overflow = 0; + gdb_py_longest val = gdb_py_long_as_long_and_overflow (result, + &overflow); + if (PyErr_Occurred () != nullptr) + gdbpy_handle_exception (); + if (overflow == 0) + { + uiout->field_signed (field_name, val); + return; + } + /* Fall through to the string case on overflow. */ + } + gdb::unique_xmalloc_ptr string (gdbpy_obj_to_string (result)); if (string == nullptr) gdbpy_handle_exception (); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index dbd33570a78..1142e0e739d 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -110,6 +110,7 @@ typedef PY_LONG_LONG gdb_py_longest; typedef unsigned PY_LONG_LONG gdb_py_ulongest; #define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong +#define gdb_py_long_as_long_and_overflow PyLong_AsLongLongAndOverflow #else /* HAVE_LONG_LONG */ @@ -118,6 +119,7 @@ typedef unsigned PY_LONG_LONG gdb_py_ulongest; typedef long gdb_py_longest; typedef unsigned long gdb_py_ulongest; #define gdb_py_long_as_ulongest PyLong_AsUnsignedLong +#define gdb_py_long_as_long_and_overflow PyLong_AsLongAndOverflow #endif /* HAVE_LONG_LONG */ From patchwork Thu May 18 20:18:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69630 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 DC18838560B9 for ; Thu, 18 May 2023 20:18:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC18838560B9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441116; bh=ScFJWc6g+WqX8XELLWDsXZDankrNhZGpKmimJH/cvXg=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=BgfVREOlCItdzsGTCnEb+60X/50f40SuynYU23hhzTfyKQ9n6yZQ5eFLa9EvCFkOr oy0/1lK6DIfGa7bQ+NoaFONqDlZCEKhNtjrPmgnVdAehsGBcRJoJxSqb9Jo7Iu/pFV 9TK8MvWtJQUxByF3ozxGglBcs/bVNPJyUJ8MLiR8= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id D6DFA3858C41 for ; Thu, 18 May 2023 20:18:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6DFA3858C41 Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-332cc0efe88so18715545ab.0 for ; Thu, 18 May 2023 13:18:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441090; x=1687033090; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ScFJWc6g+WqX8XELLWDsXZDankrNhZGpKmimJH/cvXg=; b=H/LD6UGk19s+b+CbpChRSz5PwvF+URQ1g1yoDvnJCe30uKpb4We8Nke1oskl0qIrWJ gyu2NQuwl/wlCqDXBaK+Bl2B/R8qkf8XylxGXA2s4vGm3uI4Y8vJBMwFtG+4AnxCfiFU CI8JGE9wxABRGmXE1p85vfsVRjRb8rdMVt62gjixQZnzlp9acgmzOyByrtWx27xz+1ld +Y23OUNZstBzONqvvN/5Rsz++/z7UqAHxG0S9VdOmJdTjuKuy9ZmyxwF/4afY0ee/oMU giXWh3Eq9U63pJ8+Du+05QsuwbUmk+tzBOwEV6qXNGbb3fwxU6+nWDblrz23tt7C1y3Z w2TQ== X-Gm-Message-State: AC+VfDy6kGe0rqm8vIfCs0YCoehBDo9Jl6XHCGdLvn5EyyblfGVTq8/0 ihmfb+2TN6NWk8WbY//VVU2e/HkUyNrGZQ1ue0C7mA== X-Google-Smtp-Source: ACHHUZ6V4Y6I2uFpJHpA7Xd9YEhLzNBOpzzlk9M6O9SHcZ4MbTCtjiNIlkx1PN5A7uzqWE79/ogYVg== X-Received: by 2002:a92:dd07:0:b0:331:2a69:96fe with SMTP id n7-20020a92dd07000000b003312a6996femr5888361ilm.1.1684441090173; Thu, 18 May 2023 13:18:10 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:09 -0700 (PDT) Date: Thu, 18 May 2023 14:18:08 -0600 Subject: [PATCH v2 2/9] Use member initializers in mi_parse MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-2-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This changes mi_parse to use member initializers rather than a constructor. This is easier to follow. --- gdb/mi/mi-parse.c | 17 ----------------- gdb/mi/mi-parse.h | 28 ++++++++++++++-------------- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index 737ec57eddd..bda554a568e 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -208,23 +208,6 @@ mi_parse_argv (const char *args, struct mi_parse *parse) } } -mi_parse::mi_parse () - : op (MI_COMMAND), - command (NULL), - token (NULL), - cmd (NULL), - cmd_start (NULL), - args (NULL), - argv (NULL), - argc (0), - all (0), - thread_group (-1), - thread (-1), - frame (-1), - language (language_unknown) -{ -} - mi_parse::~mi_parse () { xfree (command); diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index e34796988a4..75bb36b1c77 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -41,27 +41,27 @@ enum mi_command_type struct mi_parse { - mi_parse (); + mi_parse () = default; ~mi_parse (); DISABLE_COPY_AND_ASSIGN (mi_parse); - enum mi_command_type op; - char *command; - char *token; - const struct mi_command *cmd; - struct mi_timestamp *cmd_start; - char *args; - char **argv; - int argc; - int all; - int thread_group; /* At present, the same as inferior number. */ - int thread; - int frame; + enum mi_command_type op = MI_COMMAND; + char *command = nullptr; + char *token = nullptr; + const struct mi_command *cmd = nullptr; + struct mi_timestamp *cmd_start = nullptr; + char *args = nullptr; + char **argv = nullptr; + int argc = 0; + int all = 0; + int thread_group = -1; /* At present, the same as inferior number. */ + int thread = -1; + int frame = -1; /* The language that should be used to evaluate the MI command. Ignored if set to language_unknown. */ - enum language language; + enum language language = language_unknown; }; /* Attempts to parse CMD returning a ``struct mi_parse''. If CMD is From patchwork Thu May 18 20:18:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69632 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 A17F33836E9D for ; Thu, 18 May 2023 20:18:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A17F33836E9D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441123; bh=i1nQ9t2omyjzsFaoKmKyo74/O9KoF4r3iMELs4EGkOI=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=NY7AV5FxM3PKdr7im04J57K0Q7KhQlbT+o1js5eHy2MpYSv2iur26Xd8K1ImkNcpK 8Uu3lfc45T4mkGKQspFmEMpG3EDXCjVKDZ9/qzNvoHab+W9k9J7moPHsivzL9ApmJ1 HXSQg6wOnxjyOK6bxyJvHwfe60SUFfc44UDB5hos= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id 8871D3858C5F for ; Thu, 18 May 2023 20:18:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8871D3858C5F Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-76fe17da95fso57535639f.2 for ; Thu, 18 May 2023 13:18:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441091; x=1687033091; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i1nQ9t2omyjzsFaoKmKyo74/O9KoF4r3iMELs4EGkOI=; b=Y8FpfkAlLcXvMol9kjH/R35HUsIBTZb4+RGPGSuyRZ6/H3JU4u8GLNhxlKFjuYvkBZ 2VfxBAXz54izN4iUoEXXom+eS+4DSPHdpc6+hXX9HSRX4rK/sEjcUMO6A8VJS92oPPMe K1Lkj4Iwc1V/gocDOE3arUqt5zWojBjOL1wQ51fyOpYwt2eJUF0OVicFZBcGMTY2zPwm 8uxz74EeJvJzp+uPpXLBJc7UYo62fEOGKHV6bWeB2xolo+DuHFVdS8quuPfrqTz5C+Oo v+FpSttUyTQDCBPX4NYgTlTxx6vLm98Uk0fI5Sxt/YGc+du3H1ll7xy8ClNl6MW6dskD dDuA== X-Gm-Message-State: AC+VfDwSCV+3AOABgo2dlJ/Wn1cPhRtOHgZEBTrwgB5XV/U/4388rPwB vN7y41I9s017buVCwzSNy2CXtuDqqKGRR9md9mJ4hw== X-Google-Smtp-Source: ACHHUZ5QBsJjS/wEQoeT9qW7zIGaP59YB9K5mWgIOO11pPVaPWyQs3LClPSxsMmPdJESw/hUzCCdtQ== X-Received: by 2002:a5d:9f06:0:b0:760:e308:107e with SMTP id q6-20020a5d9f06000000b00760e308107emr6940469iot.0.1684441090743; Thu, 18 May 2023 13:18:10 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:10 -0700 (PDT) Date: Thu, 18 May 2023 14:18:09 -0600 Subject: [PATCH v2 3/9] Use accessor for mi_parse::args MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-3-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This changes mi_parse::args to be a private member, retrieved via accessor. It also changes this member to be a std::string. This makes it simpler for a subsequent patch to implement different behavior for argument parsing. --- gdb/mi/mi-cmds.c | 6 +++--- gdb/mi/mi-main.c | 2 +- gdb/mi/mi-parse.c | 3 +-- gdb/mi/mi-parse.h | 13 ++++++++++++- gdb/python/py-micmd.c | 5 +++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c index 284453db85d..ca8c633e218 100644 --- a/gdb/mi/mi-cmds.c +++ b/gdb/mi/mi-cmds.c @@ -49,11 +49,11 @@ struct mi_command_mi : public mi_command with arguments contained within PARSE. */ void invoke (struct mi_parse *parse) const override { - mi_parse_argv (parse->args, parse); + mi_parse_argv (parse->args (), parse); if (parse->argv == nullptr) error (_("Problem parsing arguments: %s %s"), parse->command, - parse->args); + parse->args ()); this->m_argv_function (parse->command, parse->argv, parse->argc); } @@ -87,7 +87,7 @@ struct mi_command_cli : public mi_command is passed through to the CLI function as its argument string. */ void invoke (struct mi_parse *parse) const override { - const char *args = m_args_p ? parse->args : nullptr; + const char *args = m_args_p ? parse->args () : nullptr; mi_execute_cli_command (m_cli_name, m_args_p, args); } diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 840663e0fb4..aaebce40fac 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1813,7 +1813,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) if (mi_debug_p) gdb_printf (gdb_stdlog, " token=`%s' command=`%s' args=`%s'\n", - context->token, context->command, context->args); + context->token, context->command, context->args ()); mi_cmd_execute (context); diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index bda554a568e..bf3b534e590 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -212,7 +212,6 @@ mi_parse::~mi_parse () { xfree (command); xfree (token); - xfree (args); freeargv (argv); } @@ -346,7 +345,7 @@ mi_parse (const char *cmd, char **token) } /* Save the rest of the arguments for the command. */ - parse->args = xstrdup (chp); + parse->set_args (chp); /* Fully parsed, flag as an MI command. */ parse->op = MI_COMMAND; diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 75bb36b1c77..d4ac3f002e4 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -46,12 +46,19 @@ struct mi_parse DISABLE_COPY_AND_ASSIGN (mi_parse); + /* Return the full argument string, as used by commands which are + implemented as CLI commands. */ + const char *args () const + { return m_args.c_str (); } + + void set_args (const char *args) + { m_args = args; } + enum mi_command_type op = MI_COMMAND; char *command = nullptr; char *token = nullptr; const struct mi_command *cmd = nullptr; struct mi_timestamp *cmd_start = nullptr; - char *args = nullptr; char **argv = nullptr; int argc = 0; int all = 0; @@ -62,6 +69,10 @@ struct mi_parse /* The language that should be used to evaluate the MI command. Ignored if set to language_unknown. */ enum language language = language_unknown; + + private: + + std::string m_args; }; /* Attempts to parse CMD returning a ``struct mi_parse''. If CMD is diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c index d7d95918cb4..88d52db2202 100644 --- a/gdb/python/py-micmd.c +++ b/gdb/python/py-micmd.c @@ -355,10 +355,11 @@ mi_command_py::invoke (struct mi_parse *parse) const pymicmd_debug_printf ("this = %p, name = %s", this, name ()); - mi_parse_argv (parse->args, parse); + mi_parse_argv (parse->args (), parse); if (parse->argv == nullptr) - error (_("Problem parsing arguments: %s %s"), parse->command, parse->args); + error (_("Problem parsing arguments: %s %s"), parse->command, + parse->args ()); gdbpy_enter enter_py; From patchwork Thu May 18 20:18:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69634 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 AAC523836EA6 for ; Thu, 18 May 2023 20:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AAC523836EA6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441148; bh=FkyCYudp/tJQ3iNztH4uhYIHCDquZ/O3dTE+BPQ00kE=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=HRIWjZ+vpYp6Jxwa10xFyPYHt94sjOgL56LGlSUFoUWMDaiprq93Nd9Kmtje39+bd ptA5xq4z1N9W/nUg1qqv8yxwvBiNdgz64E5w5TdRwX22Uk4UI2lI7r+i7VreOwQH3B W9kMcl0Vb0XHEDOg9EezL7IixO33hHZ0Ajxbb/xM= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id 27FAC3858C62 for ; Thu, 18 May 2023 20:18:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27FAC3858C62 Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3360a95c0a1so6740395ab.0 for ; Thu, 18 May 2023 13:18:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441091; x=1687033091; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FkyCYudp/tJQ3iNztH4uhYIHCDquZ/O3dTE+BPQ00kE=; b=f63KntFWQphhhW/2BoPnVEIfrNq+sPxDv97mN7VJAOzqwuNm7ADBil8SU2epbr01AD qu0daNvjM4ChNAwQ0elhpPmo5CeDoHx/1AhlTeaZ9N/QfE0rq92fljPh+Li1is2yswle FwyF8vmm2kpHwJvA5w187vLx1/NoIPgIC48S7kXcrM52CeNa3GPyGYTG47C1LQKLvGLW SOvR8lzgHH0UxRuXGeainsLbgO2U+g/EXho0FVcwMDIYmcmhfInVsDb3B1eyVw21krlW W94JnfqE7vUX9rnOqCDhLdFgBZ+/hxQceQc0VIRMHn8J9mqYS72V1xpHUUrDDFZLdyXV +BDA== X-Gm-Message-State: AC+VfDx3xP/CORDKgQRo88eZBe0FdmkOktd8cQ/a+SojN6m+qatxbhGJ YZbskNRUQc3mP4ze7+v2dlK73IA+FjTUoqNEv9YLGg== X-Google-Smtp-Source: ACHHUZ5IgyouWvWeLGBfBg0yj3gROy/TbpmhW8VxUOZokhW990bn4YbO202LWgfSsf2rvZTI7F6MrQ== X-Received: by 2002:a05:6e02:4cd:b0:32b:2884:667d with SMTP id f13-20020a056e0204cd00b0032b2884667dmr5629367ils.7.1684441091381; Thu, 18 May 2023 13:18:11 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:11 -0700 (PDT) Date: Thu, 18 May 2023 14:18:10 -0600 Subject: [PATCH v2 4/9] Change mi_parse_argv to a method MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-4-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This changes mi_parse_argv to be a method of mi_parse. This is just a minor cleanup. --- gdb/mi/mi-cmds.c | 2 +- gdb/mi/mi-parse.c | 8 ++++---- gdb/mi/mi-parse.h | 7 +++---- gdb/python/py-micmd.c | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c index ca8c633e218..f8cae4131d8 100644 --- a/gdb/mi/mi-cmds.c +++ b/gdb/mi/mi-cmds.c @@ -49,7 +49,7 @@ struct mi_command_mi : public mi_command with arguments contained within PARSE. */ void invoke (struct mi_parse *parse) const override { - mi_parse_argv (parse->args (), parse); + parse->parse_argv (); if (parse->argv == nullptr) error (_("Problem parsing arguments: %s %s"), parse->command, diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index bf3b534e590..f077eb36a7c 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -107,9 +107,9 @@ mi_parse_escape (const char **string_ptr) } void -mi_parse_argv (const char *args, struct mi_parse *parse) +mi_parse::parse_argv () { - const char *chp = args; + const char *chp = m_args.get (); int argc = 0; char **argv = XNEWVEC (char *, argc + 1); @@ -124,8 +124,8 @@ mi_parse_argv (const char *args, struct mi_parse *parse) switch (*chp) { case '\0': - parse->argv = argv; - parse->argc = argc; + this->argv = argv; + this->argc = argc; return; case '"': { diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index d4ac3f002e4..edb61547354 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -46,6 +46,9 @@ struct mi_parse DISABLE_COPY_AND_ASSIGN (mi_parse); + /* Split the arguments into argc/argv and store the result. */ + void parse_argv (); + /* Return the full argument string, as used by commands which are implemented as CLI commands. */ const char *args () const @@ -90,8 +93,4 @@ extern std::unique_ptr mi_parse (const char *cmd, enum print_values mi_parse_print_values (const char *name); -/* Split ARGS into argc/argv and store the result in PARSE. */ - -extern void mi_parse_argv (const char *args, struct mi_parse *parse); - #endif /* MI_MI_PARSE_H */ diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c index 88d52db2202..7027210d0d8 100644 --- a/gdb/python/py-micmd.c +++ b/gdb/python/py-micmd.c @@ -355,7 +355,7 @@ mi_command_py::invoke (struct mi_parse *parse) const pymicmd_debug_printf ("this = %p, name = %s", this, name ()); - mi_parse_argv (parse->args (), parse); + parse->parse_argv (); if (parse->argv == nullptr) error (_("Problem parsing arguments: %s %s"), parse->command, From patchwork Thu May 18 20:18:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69637 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 9D4CB3839DEB for ; Thu, 18 May 2023 20:19:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D4CB3839DEB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441176; bh=a86gpxxU92zJSsk6KF8a1BPXHNbIF4wXRprKKpyq4qw=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=QNWQBcKM/fyTzGU5ESJ/nsyxlzwuK9f/LnHuK7NQGHT2cDyKhTjQtuIeOhez4rb5Q MJes6b/6Yf2008GzOmAMUGKBsdUtDgse6sbNKkK1ASGs/Y95e4ShfMmrxvhrWFckg4 czrQF5hknHCSa2sO2ZVWhq/ppSQjdfn0kRe0FiUU= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id C49213858C2F for ; Thu, 18 May 2023 20:18:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C49213858C2F Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-76c7dfc2e8dso63616639f.0 for ; Thu, 18 May 2023 13:18:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441092; x=1687033092; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a86gpxxU92zJSsk6KF8a1BPXHNbIF4wXRprKKpyq4qw=; b=MaWRtTvwO0p2O9HE6HhIGD29kR8Q0mbTFyPEr8Zw9Q7/Qok3HT8z7AM5KRP4hiNV8n 20mgI+C4kYlBCwCt7SNx5chUTuDzDCdeXKRvF3rGOcac1YiK/MULXmBxREy8hMPCm0DU mPbMvV/4fS80Q3dc37S6RMZXjVzzuYZj5sp5uO9J0a1ExIZoPmvdoYuryZVMZA3unHBo bUk1gkZynzdYb8lcSIwC+yKHMjdVnIuMKoVjv+Yqv9ZqWz1ZkYtW56pWNvmz8FiEhtBp RpJvwIJfN87YT7Xa4A6JUCmTBn7ntk5BXY6K1Ate8QLy09iLKxAzYBB3qAl69TxiQADj Eh+Q== X-Gm-Message-State: AC+VfDwkP8C4wGBHBIgBABTEggUtl5RqR147Pyf9sjJH+AIpKmWBOTQe DuNu6iqfangeri1UBSPmbwnuRaCe6s22ATTwERx91g== X-Google-Smtp-Source: ACHHUZ4CNgMAfVcHZDRICHw1YrUNPALiSy9aOFBcKS3Ur/ehUROd9lFcO7HSIob2ZF5/iCJIKhT3Fw== X-Received: by 2002:a92:c8c3:0:b0:331:1836:1bdc with SMTP id c3-20020a92c8c3000000b0033118361bdcmr4904397ilq.31.1684441092037; Thu, 18 May 2023 13:18:12 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:11 -0700 (PDT) Date: Thu, 18 May 2023 14:18:11 -0600 Subject: [PATCH v2 5/9] Introduce "static constructor" for mi_parse MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-5-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Change the mi_parse function to be a static method of mi_parse. This lets us remove the 'set_args' setter function. --- gdb/mi/mi-main.c | 2 +- gdb/mi/mi-parse.c | 6 +++--- gdb/mi/mi-parse.h | 28 +++++++++++++--------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index aaebce40fac..b430115daea 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1919,7 +1919,7 @@ mi_execute_command (const char *cmd, int from_tty) try { - command = mi_parse (cmd, &token); + command = mi_parse::make (cmd, &token); } catch (const gdb_exception &exception) { diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index f077eb36a7c..b7c5a8cecdf 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -109,7 +109,7 @@ mi_parse_escape (const char **string_ptr) void mi_parse::parse_argv () { - const char *chp = m_args.get (); + const char *chp = m_args.c_str (); int argc = 0; char **argv = XNEWVEC (char *, argc + 1); @@ -216,7 +216,7 @@ mi_parse::~mi_parse () } std::unique_ptr -mi_parse (const char *cmd, char **token) +mi_parse::make (const char *cmd, char **token) { const char *chp; @@ -345,7 +345,7 @@ mi_parse (const char *cmd, char **token) } /* Save the rest of the arguments for the command. */ - parse->set_args (chp); + parse->m_args = chp; /* Fully parsed, flag as an MI command. */ parse->op = MI_COMMAND; diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index edb61547354..6f1da6e6eb5 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -41,7 +41,17 @@ enum mi_command_type struct mi_parse { - mi_parse () = default; + /* Attempts to parse CMD returning a ``struct mi_parse''. If CMD is + invalid, an exception is thrown. For an MI_COMMAND COMMAND, ARGS + and OP are initialized. Un-initialized fields are zero. *TOKEN is + set to the token, even if an exception is thrown. It is allocated + with xmalloc; it must either be freed with xfree, or assigned to + the TOKEN field of the resultant mi_parse object, to be freed by + mi_parse_free. */ + + static std::unique_ptr make (const char *cmd, + char **token); + ~mi_parse (); DISABLE_COPY_AND_ASSIGN (mi_parse); @@ -54,9 +64,6 @@ struct mi_parse const char *args () const { return m_args.c_str (); } - void set_args (const char *args) - { m_args = args; } - enum mi_command_type op = MI_COMMAND; char *command = nullptr; char *token = nullptr; @@ -75,20 +82,11 @@ struct mi_parse private: + mi_parse () = default; + std::string m_args; }; -/* Attempts to parse CMD returning a ``struct mi_parse''. If CMD is - invalid, an exception is thrown. For an MI_COMMAND COMMAND, ARGS - and OP are initialized. Un-initialized fields are zero. *TOKEN is - set to the token, even if an exception is thrown. It is allocated - with xmalloc; it must either be freed with xfree, or assigned to - the TOKEN field of the resultant mi_parse object, to be freed by - mi_parse_free. */ - -extern std::unique_ptr mi_parse (const char *cmd, - char **token); - /* Parse a string argument into a print_values value. */ enum print_values mi_parse_print_values (const char *name); From patchwork Thu May 18 20:18:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69633 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 D619A3836EA6 for ; Thu, 18 May 2023 20:19:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D619A3836EA6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441142; bh=QgrM7Y8T6usk7DLj3PECZcUbNPmsr9Sb7DMQU14/q7s=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=oa1enLcWCDB+gqDvUfbczz0w9S1Q+fws3siObWey2FdOMHQr4NSJfzzNEvMRJ0Es0 84qbgUg6gYCk1kwUPYJoCV9L88tfvn0mGxM/b5RNWsw84K/rXGPbioy2vubsoqcNvY HqvT1E1St+PtUsMv/G0S00+2jNGFXrXpsLasswHY= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by sourceware.org (Postfix) with ESMTPS id F176A3858415 for ; Thu, 18 May 2023 20:18:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F176A3858415 Received: by mail-io1-xd2e.google.com with SMTP id ca18e2360f4ac-76c64ddee11so62620639f.2 for ; Thu, 18 May 2023 13:18:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441092; x=1687033092; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QgrM7Y8T6usk7DLj3PECZcUbNPmsr9Sb7DMQU14/q7s=; b=fSnA6F7pHQWjzd0g9d/pSAkH2/uCkw/pwGBDhK7ypiDhvQ9IPeEjoyBLzbWlUoPP5n pG3D6Ih1MIJkm0jW64mNhESy5EmwjiuzE0UO45xP/nSjY/5SB4TWZs5OpIskxsxU4m10 H5b/jnEpbBA5VtAyLOqvBwQdvHmycgv3mL1u8B/AWRZ3zgxQzeR4aan9RrIrXpdsUYCB La1f9XtT8n3kQrRV3zhy8hcjyp1HzGgswPCwRvJ2cnZQczZYxFUMfpN7bLuOM70DYz26 oFEKoLUzUX9ERtraWGA+2qrs5hT4BfMF7SDxiu8fSp1KGby1hvhP+cY1MoXT3ORTQga4 IX5g== X-Gm-Message-State: AC+VfDzY2A7UU29/iDWTzurIXDYPCNpZo+U/o49NQx/C2d5zNocg9Qqz E6icxTW2UacYdHYpmQqinvzoB2DaPvUIPxTZqf2J8A== X-Google-Smtp-Source: ACHHUZ5Wh1GB9CYp7nrBSwrFwdfhnmOtO+VLwv7r1leojeyrZn5pUPeoiPZrVg2ADoakvxeUYetOtg== X-Received: by 2002:a6b:620e:0:b0:76c:5641:136 with SMTP id f14-20020a6b620e000000b0076c56410136mr6595569iog.0.1684441092648; Thu, 18 May 2023 13:18:12 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:12 -0700 (PDT) Date: Thu, 18 May 2023 14:18:12 -0600 Subject: [PATCH v2 6/9] Introduce mi_parse helper methods MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-6-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This introduces some helper methods for mi_parse that handle some of the details of parsing. This approach lets us reuse them later. --- gdb/mi/mi-parse.c | 69 +++++++++++++++++++++++++++++++++++++++++-------------- gdb/mi/mi-parse.h | 9 ++++++++ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index b7c5a8cecdf..09207164291 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -215,6 +215,54 @@ mi_parse::~mi_parse () freeargv (argv); } +/* See mi-parse.h. */ + +void +mi_parse::set_thread_group (const char *arg, char **endp) +{ + if (thread_group != -1) + error (_("Duplicate '--thread-group' option")); + if (*arg != 'i') + error (_("Invalid thread group id")); + arg += 1; + thread_group = strtol (arg, endp, 10); +} + +/* See mi-parse.h. */ + +void +mi_parse::set_thread (const char *arg, char **endp) +{ + if (thread != -1) + error (_("Duplicate '--thread' option")); + thread = strtol (arg, endp, 10); +} + +/* See mi-parse.h. */ + +void +mi_parse::set_frame (const char *arg, char **endp) +{ + if (frame != -1) + error (_("Duplicate '--frame' option")); + frame = strtol (arg, endp, 10); +} + +/* See mi-parse.h. */ + +void +mi_parse::set_language (const char *arg, const char **endp) +{ + std::string lang_name = extract_arg (&arg); + + language = language_enum (lang_name.c_str ()); + if (language == language_unknown) + error (_("Invalid --language argument: %s"), lang_name.c_str ()); + + if (endp != nullptr) + *endp = arg; +} + std::unique_ptr mi_parse::make (const char *cmd, char **token) { @@ -295,13 +343,8 @@ mi_parse::make (const char *cmd, char **token) char *endp; option = "--thread-group"; - if (parse->thread_group != -1) - error (_("Duplicate '--thread-group' option")); chp += tgs; - if (*chp != 'i') - error (_("Invalid thread group id")); - chp += 1; - parse->thread_group = strtol (chp, &endp, 10); + parse->set_thread_group (chp, &endp); chp = endp; } else if (strncmp (chp, "--thread ", ts) == 0) @@ -309,10 +352,8 @@ mi_parse::make (const char *cmd, char **token) char *endp; option = "--thread"; - if (parse->thread != -1) - error (_("Duplicate '--thread' option")); chp += ts; - parse->thread = strtol (chp, &endp, 10); + parse->set_thread (chp, &endp); chp = endp; } else if (strncmp (chp, "--frame ", fs) == 0) @@ -320,21 +361,15 @@ mi_parse::make (const char *cmd, char **token) char *endp; option = "--frame"; - if (parse->frame != -1) - error (_("Duplicate '--frame' option")); chp += fs; - parse->frame = strtol (chp, &endp, 10); + parse->set_frame (chp, &endp); chp = endp; } else if (strncmp (chp, "--language ", ls) == 0) { option = "--language"; chp += ls; - std::string lang_name = extract_arg (&chp); - - parse->language = language_enum (lang_name.c_str ()); - if (parse->language == language_unknown) - error (_("Invalid --language argument: %s"), lang_name.c_str ()); + parse->set_language (chp, &chp); } else break; diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 6f1da6e6eb5..19c41f23ed6 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -84,6 +84,15 @@ struct mi_parse mi_parse () = default; + /* Helper methods for parsing arguments. Each takes the argument + to be parsed. It will either set a member of this object, or + throw an exception on error. In each case, *ENDP, if non-NULL, + will be updated to just after the argument text. */ + void set_thread_group (const char *arg, char **endp); + void set_thread (const char *arg, char **endp); + void set_frame (const char *arg, char **endp); + void set_language (const char *arg, const char **endp); + std::string m_args; }; From patchwork Thu May 18 20:18:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69635 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 8B07D3882039 for ; Thu, 18 May 2023 20:19:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B07D3882039 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441153; bh=6uB759dveLVkwbJGPQlq4dwdL+LgQp/CsR88AGTZGc4=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=FeszXfqSE9a23DG8RqDuJtCHT+TCaWT3n6MmmwfdyEtyqeCC3tW9Zcg1S+SKgQIxI gAUUbG0uiV1crVdDa2OA2yfImGByyn2LB0F4UW6Y2Zd0Hqc/GDHD2SjmVnkAZLU441 aHUl038I5STzeaj3LjS6rbS/+XkjWRomD4VcQeJE= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 0DF5E385700F for ; Thu, 18 May 2023 20:18:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0DF5E385700F Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-3381e9ec12bso19247715ab.3 for ; Thu, 18 May 2023 13:18:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441093; x=1687033093; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6uB759dveLVkwbJGPQlq4dwdL+LgQp/CsR88AGTZGc4=; b=kISdipxZjY9smpZ/RcGKqt/FORxT9XA2RCFD9fcoOpIxBnC/RljWxoj+lHYTDJX0/9 XpOSoFksyPRZO1ZzECEvjle5LyAloY9YKWHEi6vyYp4/lAShe92fsCe86IKmXNB0LTJw z7K2IhhXxt/STpgQ4d6I4K4YiM4znsXKkLNjHez6VL8OcPAO3ByD1+MTVeAUQ+RyzSD3 nM1U87tcKkIkicvu60dhJn5xNd8uxE5mkNc6P+9/9kGHRfd+I3OR0Sv081y7q2dXif+z FJ6UZykMoIXIc2hNlaJiVX3Lp3SRXN62MGLXGsnMAxFP/LzlJJt7LDtl7OlQcnIel7Dt jKIA== X-Gm-Message-State: AC+VfDyrEUCLOaPBFcnyU7pC7F8oqNQIMi1BpO2YH2omiEbPD5t1Q64i anyqSzCfSp0UZPjKGdFaTkVgIqGYIoZNqcXvhhx6MQ== X-Google-Smtp-Source: ACHHUZ6vkhJHhxf4QhACTG3U4obWVtI0WrLvC2fFIqr8C2k0NjnqB+SpJswbEPatO9C3K587tBKpFA== X-Received: by 2002:a92:c8c3:0:b0:331:773f:e67a with SMTP id c3-20020a92c8c3000000b00331773fe67amr5604588ilq.31.1684441093253; Thu, 18 May 2023 13:18:13 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:12 -0700 (PDT) Date: Thu, 18 May 2023 14:18:13 -0600 Subject: [PATCH v2 7/9] Add second mi_parse constructor MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-7-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This adds a second mi_parse constructor. This constructor takes a command name and vector of arguments, and does not do any escape processing. This also changes mi_parse::args to handle parse objects created this new way. --- gdb/mi/mi-parse.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/mi/mi-parse.h | 12 +++++-- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index 09207164291..a113d4d48da 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -109,6 +109,11 @@ mi_parse_escape (const char **string_ptr) void mi_parse::parse_argv () { + /* If arguments were already computed (or were supplied at + construction), then there's no need to re-compute them. */ + if (argv != nullptr) + return; + const char *chp = m_args.c_str (); int argc = 0; char **argv = XNEWVEC (char *, argc + 1); @@ -217,6 +222,27 @@ mi_parse::~mi_parse () /* See mi-parse.h. */ +const char * +mi_parse::args () +{ + /* If args were already computed, or if there is no pre-computed + argv, just return the args. */ + if (!m_args.empty () || argv == nullptr) + return m_args.c_str (); + + /* Compute args from argv. */ + for (int i = 0; i < argc; ++i) + { + if (!m_args.empty ()) + m_args += " "; + m_args += argv[i]; + } + + return m_args.c_str (); +} + +/* See mi-parse.h. */ + void mi_parse::set_thread_group (const char *arg, char **endp) { @@ -387,6 +413,77 @@ mi_parse::make (const char *cmd, char **token) return parse; } +/* See mi-parse.h. */ + +std::unique_ptr +mi_parse::make (gdb::unique_xmalloc_ptr command, + std::vector> args) +{ + std::unique_ptr parse (new struct mi_parse); + + parse->command = command.release (); + parse->token = xstrdup (""); + + if (parse->command[0] != '-') + throw_error (UNDEFINED_COMMAND_ERROR, + _("MI command '%s' does not start with '-'"), + parse->command); + + /* Find the command in the MI table. */ + parse->cmd = mi_cmd_lookup (parse->command + 1); + if (parse->cmd == NULL) + throw_error (UNDEFINED_COMMAND_ERROR, + _("Undefined MI command: %s"), parse->command); + + /* This over-allocates slightly, but it seems unimportant. */ + parse->argv = XCNEWVEC (char *, args.size () + 1); + + for (size_t i = 0; i < args.size (); ++i) + { + const char *chp = args[i].get (); + + /* See if --all is the last token in the input. */ + if (strcmp (chp, "--all") == 0) + { + parse->all = 1; + } + else if (strcmp (chp, "--thread-group") == 0) + { + ++i; + if (i == args.size ()) + error ("No argument to '--thread-group'"); + parse->set_thread_group (args[i].get (), nullptr); + } + else if (strcmp (chp, "--thread") == 0) + { + ++i; + if (i == args.size ()) + error ("No argument to '--thread'"); + parse->set_thread (args[i].get (), nullptr); + } + else if (strcmp (chp, "--frame") == 0) + { + ++i; + if (i == args.size ()) + error ("No argument to '--frame'"); + parse->set_frame (args[i].get (), nullptr); + } + else if (strcmp (chp, "--language") == 0) + { + ++i; + if (i == args.size ()) + error ("No argument to '--language'"); + parse->set_language (args[i].get (), nullptr); + } + else + parse->argv[parse->argc++] = args[i].release (); + } + + /* Fully parsed, flag as an MI command. */ + parse->op = MI_COMMAND; + return parse; +} + enum print_values mi_parse_print_values (const char *name) { diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 19c41f23ed6..6373543529b 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -52,6 +52,15 @@ struct mi_parse static std::unique_ptr make (const char *cmd, char **token); + /* Create an mi_parse object given the command name and a vector + of arguments. Unlike with the other constructor, here the + arguments are treated "as is" -- no escape processing is + done. */ + + static std::unique_ptr make + (gdb::unique_xmalloc_ptr command, + std::vector> args); + ~mi_parse (); DISABLE_COPY_AND_ASSIGN (mi_parse); @@ -61,8 +70,7 @@ struct mi_parse /* Return the full argument string, as used by commands which are implemented as CLI commands. */ - const char *args () const - { return m_args.c_str (); } + const char *args (); enum mi_command_type op = MI_COMMAND; char *command = nullptr; From patchwork Thu May 18 20:18:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69638 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 917E03857027 for ; Thu, 18 May 2023 20:19:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 917E03857027 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441199; bh=wDNRpAlJ65AYPpMxQ7MYpAZkoWonOFecc8kjta5lpmo=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=QZSbL9RbxtyjuwUGxCBOUFD9Gkh5O+ZiaX5bGxvvJBXnKjOtgWJLT36XbQLXFjRKk q2AfPax8hI1EeScsxLYCz0kMJSpme68sEXOwe06jI3iccr2JH+JSnpsetEuHiXnSa2 asLt0Iu7szICVHlmVrIUVcjT1G3MLi2wrHaAmves= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id AA3EB3857027 for ; Thu, 18 May 2023 20:18:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA3EB3857027 Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-76c7517612dso64932039f.0 for ; Thu, 18 May 2023 13:18:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441094; x=1687033094; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wDNRpAlJ65AYPpMxQ7MYpAZkoWonOFecc8kjta5lpmo=; b=kwTSfAMGiaz2MB1XXLL84kJEoD0F9cLwPzVCDjG45uEeOq5ErWDln3p2GheFLJ8PN8 HLE/+FW2pCcoNc+W4T9YhLPCgngmWDPU49ftQd4ziOStXsLPv7+Xvk6dwOFYhdPRnnj4 B2nNXMw5yfP1iOSiBUIfXyMTK5f3k5REmqvJlGH+X40Op+TTZPk1R7X8SnuaFlZTkZx3 3RObOX6HFvISgcqiS1jh0Uvax25jNN0J9+BTvY2Ex7ggYgKoWPVc5ufUqo2cxYOKFf2/ HCzWmTpHPaijMpNE1s0CAqoMtM+nyr6udcIMOXWNRuBx5Fk3YxttPYXtMg9p1c9gaqfs iWbA== X-Gm-Message-State: AC+VfDx95alC+1rbIvF3wRQawQ1jIXmXnwXhw30YSrsDGU6nvxSTGs2Z O6p2Gh1jH/JksOOYJIvb1Yd3DA/mnGN1cOcKfZdlIw== X-Google-Smtp-Source: ACHHUZ4xzm+jvqGeL3TrLDAR31oWkBZFjDx9OS6w/hUuwYYBBnQ/A33rwGQdRBPy5reEE03jeRT3+g== X-Received: by 2002:a92:d10e:0:b0:331:96e5:678b with SMTP id a14-20020a92d10e000000b0033196e5678bmr5240758ilb.23.1684441093849; Thu, 18 May 2023 13:18:13 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:13 -0700 (PDT) Date: Thu, 18 May 2023 14:18:14 -0600 Subject: [PATCH v2 8/9] Implement gdb.execute_mi MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-8-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This adds a new Python function, gdb.execute_mi, that can be used to invoke an MI command but get the output as a Python object, rather than a string. This is done by implementing a new ui_out subclass that builds a Python object. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=11688 Reviewed-By: Eli Zaretskii --- gdb/Makefile.in | 1 + gdb/NEWS | 3 + gdb/doc/python.texi | 30 ++++ gdb/mi/mi-cmds.h | 5 + gdb/mi/mi-main.c | 15 ++ gdb/python/py-mi.c | 298 ++++++++++++++++++++++++++++++++ gdb/python/python-internal.h | 3 + gdb/python/python.c | 5 + gdb/testsuite/gdb.python/py-exec-mi.exp | 32 ++++ gdb/testsuite/gdb.python/py-mi-cmd.py | 27 +++ 10 files changed, 419 insertions(+) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 14b5dd0bad6..d909786792c 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -414,6 +414,7 @@ SUBDIR_PYTHON_SRCS = \ python/py-lazy-string.c \ python/py-linetable.c \ python/py-membuf.c \ + python/py-mi.c \ python/py-micmd.c \ python/py-newobjfileevent.c \ python/py-objfile.c \ diff --git a/gdb/NEWS b/gdb/NEWS index b82114d80b0..3ea7237d022 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -192,6 +192,9 @@ info main - Changes are backwards compatible, the older API can still be used to disassemble instructions without styling. + ** New function gdb.execute_mi(COMMAND, [ARG]...), that invokes a + GDB/MI command and returns the output as a Python dictionary. + *** Changes in GDB 13 * MI version 1 is deprecated, and will be removed in GDB 14. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index d93ee55690e..b2f3cb4874b 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4584,6 +4584,36 @@ commands have been added: (@value{GDBP}) @end smallexample +Conversely, it is possible to execute @sc{gdb/mi} commands from +Python, with the results being a Python object and not a +specially-formatted string. This is done with the +@code{gdb.execute_mi} function. + +@findex gdb.execute_mi +@defun gdb.execute_mi (command @r{[}, arg @r{]}@dots{}) +Invoke a @sc{gdb/mi} command. @var{command} is the name of the +command, a string. The arguments, @var{arg}, are passed to the +command. Each argument must also be a string. + +This function returns a Python dictionary whose contents reflect the +corresponding @sc{GDB/MI} command's output. Refer to the +documentation for these commands for details. Lists are represented +as Python lists, and tuples are represented as Python dictionaries. + +If the command fails, it will raise a Python exception. +@end defun + +Here is how this works using the commands from the example above: + +@smallexample +(@value{GDBP}) python print(gdb.execute_mi("-echo-dict", "abc", "def", "ghi")) +@{'dict': @{'argv': ['abc', 'def', 'ghi']@}@} +(@value{GDBP}) python print(gdb.execute_mi("-echo-list", "abc", "def", "ghi")) +@{'list': ['abc', 'def', 'ghi']@} +(@value{GDBP}) python print(gdb.execute_mi("-echo-string", "abc", "def", "ghi")) +@{'string': 'abc, def, ghi'@} +@end smallexample + @node Parameters In Python @subsubsection Parameters In Python diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h index d867c298df9..c62b134fe3d 100644 --- a/gdb/mi/mi-cmds.h +++ b/gdb/mi/mi-cmds.h @@ -206,6 +206,11 @@ extern mi_command *mi_cmd_lookup (const char *command); extern void mi_execute_command (const char *cmd, int from_tty); +/* Execute an MI command given an already-constructed parse + object. */ + +extern void mi_execute_command (mi_parse *context); + /* Insert COMMAND into the global mi_cmd_table. Return false if COMMAND->name already exists in mi_cmd_table, in which case COMMAND will not have been added to mi_cmd_table. Otherwise, return true, and diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index b430115daea..f3b7490e089 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1965,6 +1965,21 @@ mi_execute_command (const char *cmd, int from_tty) } } +/* See mi-cmds.h. */ + +void +mi_execute_command (mi_parse *context) +{ + if (context->op != MI_COMMAND) + error (_("Command is not an MI command")); + + scoped_restore save_token = make_scoped_restore (¤t_token, + context->token); + scoped_restore save_debug = make_scoped_restore (&mi_debug_p, 0); + + mi_cmd_execute (context); +} + /* Captures the current user selected context state, that is the current thread and frame. Later we can then check if the user selected context has changed at all. */ diff --git a/gdb/python/py-mi.c b/gdb/python/py-mi.c new file mode 100644 index 00000000000..0fcd57844e7 --- /dev/null +++ b/gdb/python/py-mi.c @@ -0,0 +1,298 @@ +/* Python interface to MI commands + + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "defs.h" +#include "python-internal.h" +#include "ui-out.h" +#include "mi/mi-parse.h" + +/* A ui_out subclass that creates a Python object based on the data + that is passed in. */ + +class py_ui_out : public ui_out +{ +public: + + py_ui_out () + : ui_out (fix_multi_location_breakpoint_output + | fix_breakpoint_script_output) + { + do_begin (ui_out_type_tuple, nullptr); + } + + bool can_emit_style_escape () const override + { return false; } + + bool do_is_mi_like_p () const override + { return true; } + + /* Return the Python object that was created. If a Python error + occurred during the processing, set the Python error and return + nullptr. */ + PyObject *result () + { + if (m_error.has_value ()) + { + m_error->restore (); + return nullptr; + } + return current ().obj.release (); + } + +protected: + + void do_progress_end () override { } + void do_progress_start () override { } + void do_progress_notify (const std::string &, const char *, double, double) + override + { } + + void do_table_begin (int nbrofcols, int nr_rows, const char *tblid) override + { + do_begin (ui_out_type_list, tblid); + } + void do_table_body () override + { } + void do_table_end () override + { + do_end (ui_out_type_list); + } + void do_table_header (int width, ui_align align, + const std::string &col_name, + const std::string &col_hdr) override + { } + + void do_begin (ui_out_type type, const char *id) override; + void do_end (ui_out_type type) override; + + void do_field_signed (int fldno, int width, ui_align align, + const char *fldname, LONGEST value) override; + void do_field_unsigned (int fldno, int width, ui_align align, + const char *fldname, ULONGEST value) override; + + void do_field_skip (int fldno, int width, ui_align align, + const char *fldname) override + { } + + void do_field_string (int fldno, int width, ui_align align, + const char *fldname, const char *string, + const ui_file_style &style) override; + void do_field_fmt (int fldno, int width, ui_align align, + const char *fldname, const ui_file_style &style, + const char *format, va_list args) override + ATTRIBUTE_PRINTF (7, 0); + + void do_spaces (int numspaces) override + { } + + void do_text (const char *string) override + { } + + void do_message (const ui_file_style &style, + const char *format, va_list args) + override ATTRIBUTE_PRINTF (3,0) + { } + + void do_wrap_hint (int indent) override + { } + + void do_flush () override + { } + + void do_redirect (struct ui_file *outstream) override + { } + +private: + + /* When constructing Python objects, this class keeps a stack of + objects being constructed. Each such object has this type. */ + struct object_desc + { + /* Name of the field (or empty for lists) that this object will + eventually become. */ + std::string field_name; + /* The object under construction. */ + gdbpy_ref<> obj; + /* The type of structure being created. Note that tables are + treated as lists here. */ + ui_out_type type; + }; + + /* The stack of objects being created. */ + std::vector m_objects; + + /* If an error occurred, this holds the exception information for + use by the 'release' method. */ + gdb::optional m_error; + + /* Return a reference to the object under construction. */ + object_desc ¤t () + { return m_objects.back (); } + + /* Add a new field to the current object under construction. */ + void add_field (const char *name, const gdbpy_ref<> &obj); +}; + +void +py_ui_out::add_field (const char *name, const gdbpy_ref<> &obj) +{ + if (obj == nullptr) + { + m_error.emplace (); + return; + } + + object_desc &desc = current (); + if (desc.type == ui_out_type_list) + { + if (PyList_Append (desc.obj.get (), obj.get ()) < 0) + m_error.emplace (); + } + else + { + if (PyDict_SetItemString (desc.obj.get (), name, obj.get ()) < 0) + m_error.emplace (); + } +} + +void +py_ui_out::do_begin (ui_out_type type, const char *id) +{ + if (m_error.has_value ()) + return; + + gdbpy_ref<> new_obj (type == ui_out_type_list + ? PyList_New (0) + : PyDict_New ()); + if (new_obj == nullptr) + { + m_error.emplace (); + return; + } + + object_desc new_desc; + if (id != nullptr) + new_desc.field_name = id; + new_desc.obj = std::move (new_obj); + new_desc.type = type; + + m_objects.push_back (std::move (new_desc)); +} + +void +py_ui_out::do_end (ui_out_type type) +{ + if (m_error.has_value ()) + return; + + object_desc new_obj = std::move (current ()); + m_objects.pop_back (); + add_field (new_obj.field_name.c_str (), new_obj.obj); +} + +void +py_ui_out::do_field_signed (int fldno, int width, ui_align align, + const char *fldname, LONGEST value) +{ + if (m_error.has_value ()) + return; + + gdbpy_ref<> val = gdb_py_object_from_longest (value); + add_field (fldname, val); +} + +void +py_ui_out::do_field_unsigned (int fldno, int width, ui_align align, + const char *fldname, ULONGEST value) +{ + if (m_error.has_value ()) + return; + + gdbpy_ref<> val = gdb_py_object_from_ulongest (value); + add_field (fldname, val); +} + +void +py_ui_out::do_field_string (int fldno, int width, ui_align align, + const char *fldname, const char *string, + const ui_file_style &style) +{ + if (m_error.has_value ()) + return; + + gdbpy_ref<> val = host_string_to_python_string (string); + add_field (fldname, val); +} + +void +py_ui_out::do_field_fmt (int fldno, int width, ui_align align, + const char *fldname, const ui_file_style &style, + const char *format, va_list args) +{ + if (m_error.has_value ()) + return; + + std::string str = string_vprintf (format, args); + do_field_string (fldno, width, align, fldname, str.c_str (), style); +} + +/* Implementation of the gdb.execute_mi command. */ + +PyObject * +gdbpy_execute_mi_command (PyObject *self, PyObject *args, PyObject *kw) +{ + gdb::unique_xmalloc_ptr mi_command; + std::vector> arg_strings; + + Py_ssize_t n_args = PyTuple_Size (args); + if (n_args < 0) + return nullptr; + + for (Py_ssize_t i = 0; i < n_args; ++i) + { + /* Note this returns a borrowed reference. */ + PyObject *arg = PyTuple_GetItem (args, i); + if (arg == nullptr) + return nullptr; + gdb::unique_xmalloc_ptr str = python_string_to_host_string (arg); + if (str == nullptr) + return nullptr; + if (i == 0) + mi_command = std::move (str); + else + arg_strings.push_back (std::move (str)); + } + + py_ui_out uiout; + + try + { + scoped_restore save_uiout = make_scoped_restore (¤t_uiout, &uiout); + std::unique_ptr parser + = mi_parse::make (std::move (mi_command), std::move (arg_strings)); + mi_execute_command (parser.get ()); + } + catch (const gdb_exception &except) + { + gdbpy_convert_exception (except); + return nullptr; + } + + return uiout.result (); +} diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 1142e0e739d..93217375cc5 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -483,6 +483,9 @@ struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj); frame_info_ptr frame_object_to_frame_info (PyObject *frame_obj); struct gdbarch *arch_object_to_gdbarch (PyObject *obj); +extern PyObject *gdbpy_execute_mi_command (PyObject *self, PyObject *args, + PyObject *kw); + /* Convert Python object OBJ to a program_space pointer. OBJ must be a gdb.Progspace reference. Return nullptr if the gdb.Progspace is not valid (see gdb.Progspace.is_valid), otherwise return the program_space diff --git a/gdb/python/python.c b/gdb/python/python.c index fd5a920cbdb..9703d6ef900 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2484,6 +2484,11 @@ PyMethodDef python_GdbMethods[] = Evaluate command, a string, as a gdb CLI command. Optionally returns\n\ a Python String containing the output of the command if to_string is\n\ set to True." }, + { "execute_mi", (PyCFunction) gdbpy_execute_mi_command, + METH_VARARGS | METH_KEYWORDS, + "execute_mi (command, arg...) -> dictionary\n\ +Evaluate command, a string, as a gdb MI command.\n\ +Arguments (also strings) are passed to the command." }, { "parameter", gdbpy_parameter, METH_VARARGS, "Return a gdb parameter's value" }, diff --git a/gdb/testsuite/gdb.python/py-exec-mi.exp b/gdb/testsuite/gdb.python/py-exec-mi.exp new file mode 100644 index 00000000000..09c4877870c --- /dev/null +++ b/gdb/testsuite/gdb.python/py-exec-mi.exp @@ -0,0 +1,32 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test gdb.execute_mi. + +load_lib gdb-python.exp +require allow_python_tests + +clean_restart + +gdb_test_no_output "source ${srcdir}/${subdir}/py-mi-cmd.py" \ + "load python file" + +gdb_test "python run_execute_mi_tests()" "PASS" + +# Be sure to test a command implemented as CLI command, as those fetch +# the args. +gdb_test_no_output "python gdb.execute_mi('-exec-arguments', 'a', 'b', 'c')" \ + "set arguments" + +gdb_test "show args" ".*\"a b c\"." diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py index c7bf5b7226f..2c86c909858 100644 --- a/gdb/testsuite/gdb.python/py-mi-cmd.py +++ b/gdb/testsuite/gdb.python/py-mi-cmd.py @@ -118,3 +118,30 @@ def free_invoke(obj, args): # these as a Python function which is then called from the exp script. def run_exception_tests(): print("PASS") + + +# Run some execute_mi tests. This is easier to do from Python. +def run_execute_mi_tests(): + # Install the command. + cmd = pycmd1("-pycmd") + # Pass in a representative subset of the pycmd1 keys, and then + # check that the result via MI is the same as the result via a + # direct Python call. Note that some results won't compare as + # equal -- for example, a Python MI command can return a tuple, + # but that will be translated to a Python list. + for name in ("int", "str", "dct"): + expect = cmd.invoke([name]) + got = gdb.execute_mi("-pycmd", name) + if expect != got: + print("FAIL: saw " + repr(got) + ", but expected " + repr(expect)) + return + ok = False + try: + gdb.execute_mi("-pycmd", "exp") + # Due to the "denaturation" problem, we have to expect a gdb.error + # here and not a gdb.GdbError. + except gdb.error: + ok = True + if not ok: + print("FAIL: did not throw exception") + print("PASS") From patchwork Thu May 18 20:18:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69636 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 F39E4385382B for ; Thu, 18 May 2023 20:19:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F39E4385382B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684441171; bh=9YMnnD1CjeVqrQU0oSvADRbqzNRplRCaQPuwcNuA1JQ=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=uPjDBUT1VjxVpGINX4QENNj9P11vyn4K7x0+e9uAaK4hGy54HQYrw0VBBg0F0+5fj xSu+ewK1FSfT1eUKe3RRCOs4rngxx3YXaC6zTYPEbLpQFUUKkS6i91MKct2PJsezO2 d7D3YYj3orkZTm14GsMu8UgKjPM6gO3QrirMxeGM= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id 1D8B83858430 for ; Thu, 18 May 2023 20:18:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1D8B83858430 Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-3318961b385so235075ab.1 for ; Thu, 18 May 2023 13:18:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684441094; x=1687033094; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9YMnnD1CjeVqrQU0oSvADRbqzNRplRCaQPuwcNuA1JQ=; b=T5tM3Or0g2w8pTGqyWmgX7Vao7ymMzgj9ODe7kwZjeax/zAp5SZaW9ah9pQGFFNPZJ kELgOsCTU5jk6Ujf59yn57R+S23YTcoCyPmRQ41H3f5Lz2aiyadikhwk+WwDAbVml7vh 8nadaMTyH6mEhUscrxVt2IPZUMAs67gpN398vEPqzVynIwJwAPjEGGK/D2ET5bssnnV2 FHtjzjs8Noj2lipWf9OQA3xqcKC7H4+L7ZN5Sqz6Jwdqp4yntmCQy0Lws+L57RJWCBZ2 adrhCC91gqO6bxYGQqaBQhMb/iRo0bEcCDWZdvgNBoao56wXWrP5LbKzN2NIaXYCwyEi W9jg== X-Gm-Message-State: AC+VfDyhhu7NrRZJFL62Oy2axME3CoZQBV61yn85T4SToxOTU7xzni44 VKMCuLu24iXaAv0Q7y/wABUlEhAyrPHkwVpVgTgw5Q== X-Google-Smtp-Source: ACHHUZ5lbA74piPlDCaGV2y8OE/iQvKcRWkC/niK7akn2LD/cVheQQYxEu0PnpFCSRF2bhjeLzkI7A== X-Received: by 2002:a05:6e02:349c:b0:331:5854:eb47 with SMTP id bp28-20020a056e02349c00b003315854eb47mr4718243ilb.3.1684441094435; Thu, 18 May 2023 13:18:14 -0700 (PDT) Received: from localhost.localdomain (75-166-157-55.hlrn.qwest.net. [75.166.157.55]) by smtp.gmail.com with ESMTPSA id i9-20020a05663813c900b00418ba399842sm703087jaj.13.2023.05.18.13.18.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 13:18:14 -0700 (PDT) Date: Thu, 18 May 2023 14:18:15 -0600 Subject: [PATCH v2 9/9] Implement DAP loadedSources request MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v2-9-93f229095e03@adacore.com> References: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v2-0-93f229095e03@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This implements the DAP loadedSources request, using gdb.execute_mi to avoid having to write another custom Python API. --- gdb/data-directory/Makefile.in | 1 + gdb/python/lib/gdb/dap/__init__.py | 1 + gdb/python/lib/gdb/dap/sources.py | 40 +++++++++++++++++++++++++++++++++++++ gdb/testsuite/gdb.dap/basic-dap.exp | 3 +++ 4 files changed, 45 insertions(+) diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in index 39979037245..a95c2d7ab37 100644 --- a/gdb/data-directory/Makefile.in +++ b/gdb/data-directory/Makefile.in @@ -101,6 +101,7 @@ PYTHON_FILE_LIST = \ gdb/dap/pause.py \ gdb/dap/scopes.py \ gdb/dap/server.py \ + gdb/dap/sources.py \ gdb/dap/startup.py \ gdb/dap/state.py \ gdb/dap/threads.py \ diff --git a/gdb/python/lib/gdb/dap/__init__.py b/gdb/python/lib/gdb/dap/__init__.py index 014fd086f4b..f07228e46ce 100644 --- a/gdb/python/lib/gdb/dap/__init__.py +++ b/gdb/python/lib/gdb/dap/__init__.py @@ -29,6 +29,7 @@ from . import memory from . import next from . import pause from . import scopes +from . import sources from . import threads from .server import Server diff --git a/gdb/python/lib/gdb/dap/sources.py b/gdb/python/lib/gdb/dap/sources.py new file mode 100644 index 00000000000..af7313ca2f9 --- /dev/null +++ b/gdb/python/lib/gdb/dap/sources.py @@ -0,0 +1,40 @@ +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import gdb + +from .server import request, capability +from .startup import send_gdb_with_response, in_gdb_thread + + +@in_gdb_thread +def _sources(): + result = [] + for elt in gdb.execute_mi("-file-list-exec-source-files")["files"]: + result.append( + { + "name": elt["file"], + "path": elt["fullname"], + } + ) + return { + "sources": result, + } + + +@request("loadedSources") +@capability("supportsLoadedSourcesRequest") +def sources(**extra): + return send_gdb_with_response(_sources) diff --git a/gdb/testsuite/gdb.dap/basic-dap.exp b/gdb/testsuite/gdb.dap/basic-dap.exp index 0026690ba44..f28239d8268 100644 --- a/gdb/testsuite/gdb.dap/basic-dap.exp +++ b/gdb/testsuite/gdb.dap/basic-dap.exp @@ -168,4 +168,7 @@ set obj [dap_check_request_and_response "command repl" \ set response [lindex $obj 0] gdb_assert {[dict get $response body result] == 23} +set obj [dap_check_request_and_response sources loadedSources] +gdb_assert {[string first basic-dap.c $obj] != -1} + dap_shutdown