From patchwork Tue Apr 4 17:08:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67270 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 AFC133858C5E for ; Tue, 4 Apr 2023 17:09:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AFC133858C5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628159; bh=yW7MhkMzoyopTFxaiZZKdmkZc2n8BWw/VfkgX9d2rfE=; 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=QzxhzaB8YdZWesOg2tax040HAVAx3G4ObHknOp7azkfsdFPy3Qc45GjbgesBj8GRs V+Vg3NjWw/x2wQLGPCMTDLYVlaeVQCCQmm4BAa/NBxDUgoJ/uimw2ntXKdDUEcsyGX y4dUyv6b3IAIrgXxQSfBwkZ5fUrKv6LyQn5tDmEQ= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id DD2563858C5F for ; Tue, 4 Apr 2023 17:08:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD2563858C5F Received: by mail-il1-x135.google.com with SMTP id t5so6117205ilu.5 for ; Tue, 04 Apr 2023 10:08:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628132; 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=yW7MhkMzoyopTFxaiZZKdmkZc2n8BWw/VfkgX9d2rfE=; b=Hb4QQRpmbIPA8CpZJlIEpDktLnK+AJ1cSkyRe8wtPKNAVzpgzZFf9IteQWOSzHz7iU mDTPhDBRQezV2jXq35hhP58rHtyvx/f/SilIM9dmlfhzCMfe+AzkWDp8X1oIz/0v/kSH Shem65CCYwmM/5aH9q26G8EB2/j7mTldnz+5J8KDeBWT1yTjLCHp/dpAF4936jEpGFoG Nn19hlnkKWSnOnH7qyLd16cxbRJIC1PKiGgXmvUrL0ApiTItPeEkfhn4328M0ip+VtGu /RAWMLRUO1Vm46TYMmyx5yEUGF8+mnMgbLX67ce8YAqBwFpkUTc+CCgWq3hN1GAHqGMj vpKA== X-Gm-Message-State: AAQBX9dDVarA50ejR+6AGsm+IVdRGbPt0QQEKqtjNeFPxxhQtZh7v8J9 TMP5PmGZFwnnPb1Zmsj2iVx8G+1WuIcjoM5+pd9f+w== X-Google-Smtp-Source: AKy350agh8FY6rhX6TzGUd8vF00FpKji5dmsUJt6WtiMaVUjMDem+CAxon+4tGfFfqlJNiNgytfsNg== X-Received: by 2002:a92:d5c8:0:b0:322:fe6b:d779 with SMTP id d8-20020a92d5c8000000b00322fe6bd779mr2257896ilq.29.1680628131945; Tue, 04 Apr 2023 10:08:51 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:51 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:49 -0600 Subject: [PATCH 1/9] Use field_signed from Python MI commands MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-1-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 c7c5a609f56..09dd88ca268 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 258f5c42537..617bdb23669 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 Tue Apr 4 17:08:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67271 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 E25DC3857C4F for ; Tue, 4 Apr 2023 17:09:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E25DC3857C4F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628163; bh=34+CWNNpsk+jaiKId5SZk3RGQZdppNNQDOz5AxwXLww=; 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=GCrriqH/6umi8vzMZgpzRNPE4Il23duder8xmdXr2E703+yVTW556pLXHSDtWocjQ McNsQoZs7deT3avEi456LUbOYZRn+WUWBNZ6n+ZDv1EJ/BeA9AzvQYGmUcZBUCyBvH XhrKhQex0/NR5AgKJrDA4UX55qleJd4Ac2klsz7I= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 7D06E3858C62 for ; Tue, 4 Apr 2023 17:08:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D06E3858C62 Received: by mail-il1-x12d.google.com with SMTP id n1so16716397ili.10 for ; Tue, 04 Apr 2023 10:08:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628132; 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=34+CWNNpsk+jaiKId5SZk3RGQZdppNNQDOz5AxwXLww=; b=2yxanbcDlcLnKB9jY1BRUrDyLe8DCc1CwoaB93wUu9DDfujmJSPIsj9e60bFOkKPAp /eqcW+cS5IC1zkN3ciOrpAlwDemQo0V2Vs+KnwWOVOWaXChx7LW2hDpPPeOKHfttV4we U756sg71URcgSNN8i4XOjK0Qm8TD56ScGJLapkTq4vz9BtVRz3B8W4OpNICWAzhD+REB DQVfMriyQyC9NNS4FDhmk0wSveyjOEtUHF7IDEpXLzx+mehyu+/VvKdP8j/e0jy4Z11K BDOQskTu54cDnXy/jC37cryJbGpDrLDR3/8zc4FRBUj/v/vMHig2Up0crJ24wHzmFyKi 1LaQ== X-Gm-Message-State: AAQBX9fJRFo1rUmOSZN9A1ZXMdyVUW3gQiUrb0hMOKexGFr9Vx7nWKsi KvT9iOC7qIE49lBsP8QHJElLq67/pAs1x0k/ZV67yw== X-Google-Smtp-Source: AKy350Z7z26XRAqFrFCLRIG2ImKcCSHns6nQwe+Re+goggIsV7jEg5BgJXOmDmjyg3CMB/8JLdmAsQ== X-Received: by 2002:a92:6e0a:0:b0:326:4898:c631 with SMTP id j10-20020a926e0a000000b003264898c631mr2363865ilc.1.1680628132542; Tue, 04 Apr 2023 10:08:52 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:52 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:50 -0600 Subject: [PATCH 2/9] Use member initializers in mi_parse MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-2-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 e334968cc88..3bf944763f1 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 Tue Apr 4 17:08:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67272 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 2605B3853D19 for ; Tue, 4 Apr 2023 17:09:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2605B3853D19 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628168; bh=iN/VyoHvSAsiJ6dBoa9Q+KQrBN7DwdGiuk3RRcLMH/s=; 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=nPJAO/HGj/M0iqwd9K0ACCz/ufTBk1XtghFzO22tOv+knC75V3tKu1zVLvNRFQHDQ Y8atMSW9RfnObcFmR2G6Ve9KOGiEmcM2Vl1Hp1DWBC/uxR7Q3WngztW/ip1Tqzh7OJ SpjWtn7bb0y1v05qxptaGKX9hWcaeiu633eSpSLc= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 0BC2D3858C66 for ; Tue, 4 Apr 2023 17:08:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0BC2D3858C66 Received: by mail-il1-x12e.google.com with SMTP id n1so16716408ili.10 for ; Tue, 04 Apr 2023 10:08:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628133; 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=iN/VyoHvSAsiJ6dBoa9Q+KQrBN7DwdGiuk3RRcLMH/s=; b=v41IkDnSSTpIFS++Y20wxr6JrCT8EQc3uSLo3Wyf4JMEzIZ2dCPA7NuE9Af3TUED9w 0WR3zrGyuYgvZTdtXXrAX/j4OkaLFev59dtAft4YWoVleEn55tkEcAqIc3asRwu6sokt rU73i1h/Gc8EDd1ct4f6ujt533lse9hRSQVCB1HLnm1xfMGmgrcofqhhNkY3nONznayk gx/ICJbEZ1NVAQnUkiFcNVCqDVO556vR3eB8cBaKwefi5Gf+Tn0LFxD3wN5bzIukSOS8 YyGW12sAPeSft0xBiejDrCzSnif0HxpvjC9MtZvDacJJnFleeF/YVVAIRSh06Jv3NPly 5OoQ== X-Gm-Message-State: AAQBX9dSq8SqIq50mwDIrNNo5o/ErViIKUQBoApIOCi5PvLM/zLlQGyZ WKgNyDl5QkCI5nd7pJj56bjjnKpgebn/jRYlAA0k5w== X-Google-Smtp-Source: AKy350a2VTNSiJj+b9O6zMWYjHPKgfBeZkte53V1mASoE4JnfBKdnoNoB8V0cz0khz7/r5IdgsEppg== X-Received: by 2002:a92:cc46:0:b0:323:515:c9fc with SMTP id t6-20020a92cc46000000b003230515c9fcmr3369840ilq.6.1680628133108; Tue, 04 Apr 2023 10:08:53 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:52 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:51 -0600 Subject: [PATCH 3/9] Use accessor for mi_parse::args MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-3-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 0013e5dfafd..1149cb984d3 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1811,7 +1811,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 3bf944763f1..41399fc619e 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); } @@ -347,7 +346,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 09dd88ca268..af89be693da 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 Tue Apr 4 17:08:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67275 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 760F138432D6 for ; Tue, 4 Apr 2023 17:10:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 760F138432D6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628201; bh=kQGpntrXprDNTor/a8K0xjNQAZr0xLlYD9/pPdRcbxQ=; 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=qEoHB7zGUYrGVBjazLlpbk6g6IJSrEsWH+3jmyBHKXp7lwC8q48KnOpU63wUBV/Of hEnFzzFlM6bwB069SzmcVjAObt0RKM03ZOJJVkKndEwBmJR1G66E7zKlL+L5aB6+iu Tcg/xDwWhi7n1gXkInk07dwWdysYpBuORhTYvtCQ= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 7F6003858C78 for ; Tue, 4 Apr 2023 17:08:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F6003858C78 Received: by mail-il1-x135.google.com with SMTP id j2so10419964ila.8 for ; Tue, 04 Apr 2023 10:08:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628133; 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=kQGpntrXprDNTor/a8K0xjNQAZr0xLlYD9/pPdRcbxQ=; b=Qy40d3cRgYmF3VgPue9Xnh2CSXIitCSTGukCsm6CrMZyPsVUJNyadDwug1gxo+qY2W F4s30G/MvdQMqgwZLhuMn7qLQm7ibbED6melxJ0aP/4Tr6spjIp3J8tJdWVvTuplkGza FYjAlov76bBBGilURrG9M454TJEgbDsS6Pm82LLXP1PWgVwV1szHdJs8Coq3k7tff5Kw 01Dl72uKqGh3pUx6GWGQ0Kcrm0Q8LN9CrXSuWQFZ6yfxGkAAgAgshoDfrMU/b9mrfdys p3AHOp1TN9rxgUgYcK27DUHsBaTan4mKyKj400JV4LGlvnL2wBHyao5xfWRKsQrYK6Xr JVLw== X-Gm-Message-State: AAQBX9dkpoZ0Lt+JqYsaXUPR6Rtdiq0+rIsEDt1+V+dE3fPTUrKE68wG XaMpHDNExsRsrztoFjmed0M80ygLBlQu0T10Akk13A== X-Google-Smtp-Source: AKy350aIBhdy8UNxCPTz3YT208qpeS+9NoKCGEN8171dfpYcIU0JHEmsi6PIWXTGgqun6P4Dyg3WWw== X-Received: by 2002:a92:d749:0:b0:31f:9b6e:2f4d with SMTP id e9-20020a92d749000000b0031f9b6e2f4dmr2473665ilq.0.1680628133624; Tue, 04 Apr 2023 10:08:53 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:53 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:52 -0600 Subject: [PATCH 4/9] Change mi_parse_argv to a method MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-4-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 41399fc619e..db3c4ae2368 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 af89be693da..3c2555c7665 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 Tue Apr 4 17:08:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67273 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 97E9738515F4 for ; Tue, 4 Apr 2023 17:09:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97E9738515F4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628182; bh=2j8GmxYcemfdPyFLqyWblCIyu3No/ppk8nyFxE83C+0=; 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=MydL9cjXWMRwHwnTNhVisTulbylZTiGwONhc22O6n4k851/ytu/MeejLmbrpIZC6z DFd6mfYuAJw0QsdT3xJreYe3u8C+zPJq7BAprNtz7YtzeMGFPrz8NGUiUQ/4ztLU1+ R8otnEKZovorzm+trb4t2wivSAYXIiCjMAeCNyZo= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 90DA93858C20 for ; Tue, 4 Apr 2023 17:08:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 90DA93858C20 Received: by mail-il1-x135.google.com with SMTP id t5so6117249ilu.5 for ; Tue, 04 Apr 2023 10:08:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628134; 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=2j8GmxYcemfdPyFLqyWblCIyu3No/ppk8nyFxE83C+0=; b=8MMcacjIotPM/jpO41CVRh0nKvZ/jbMV2KbU5WCS6ZC7g3IfUyCVPdXtxaIfyanaDV GPoHuaybwmJ2Zv9ZqyLWtxx05XCfNQhyvCDOzkF4ulBvcPE6KUxeiQs7mtzTbuweYLqr sHdOO/2DyAS+UJ3dvBH4Z2LvXwybwUOkG0gJJ8A/BZGpKKTgK3ZZKiDu0aHfY3Edv/iN HIxAj2q5IYvyWzaWquYcXDDiqjxgyodE/qE9dzrhH7LGim24jytsgfwwDkrvvcOfkWUx 6IqJhiliiL3hsjkk9xUksp+/HftNkiQVZrrGBQityFEb8dk3wk1nes379xi04UZpiYxh DhfQ== X-Gm-Message-State: AAQBX9cvbwoMPCRROkiygrvgXCBBmlbWo5TYQhjc0ZWhAieFq18/68to x+Sl62RVy7Cguw4Oc8D4QZxIp4pYSTei0Iza/WSQ3g== X-Google-Smtp-Source: AKy350Zx4qbPYlKlnw7+uA9s8BzXohu1X26bgAxRqVTVsUDjrim+kx4yT5mYqT9YNZ/seMDFbdfoqA== X-Received: by 2002:a92:c84c:0:b0:325:b52f:2e3d with SMTP id b12-20020a92c84c000000b00325b52f2e3dmr2368151ilq.27.1680628134166; Tue, 04 Apr 2023 10:08:54 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:53 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:53 -0600 Subject: [PATCH 5/9] Introduce "static constructor" for mi_parse MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-5-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 1149cb984d3..3a114589e7c 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1917,7 +1917,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 db3c4ae2368..57b9c46fc24 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; @@ -346,7 +346,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 Tue Apr 4 17:08:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67277 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 5918B38555B6 for ; Tue, 4 Apr 2023 17:10:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5918B38555B6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628230; bh=sdYJTX55cMsz53dDi80kjayn8nMBeeoV32zaH9luIUc=; 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=Fp0dDb/4J+/Qnh+okNzIX1Nlqfxbzi98fqirkXhrLzJGDSp7LuVNXF2UIqtNAoDR/ sxqahuteKTrQtl3UARDHTkV3maNS3nySBQUhFSMbW8qS65mafG1IXe+qMIonODzZxp bD/V5VLhCUXGqFc9EpRyKiIdCrYgFj2tKrVJ60yM= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id E2109385840A for ; Tue, 4 Apr 2023 17:08:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2109385840A Received: by mail-il1-x12b.google.com with SMTP id t5so6117263ilu.5 for ; Tue, 04 Apr 2023 10:08:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628134; 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=sdYJTX55cMsz53dDi80kjayn8nMBeeoV32zaH9luIUc=; b=ZZ30Zgc1HmGKkbSe45ONPWsfOm8vdjFivLZyhO9dCQ5FqVosIRz7/NyVvCb4ZoQ4di 2CcbLQhtutp6dqhT7TLj/bkvKwVhqvOgBQXMQLNQ3vK2c0bOXHtuIeZr2keeI1TEP2jg dR5p/h/CyQzVtskhHKQY0tsXRvgM+r7BdKMc+ZXUfUNpno09osQUy8wLT/e0XIhYoK9z oLs/0klaalfZKTxqw1L5DPeTo8wROtFuBpiwzR4/gyOnnus89M/pV0HJo2r7jXu65Y63 X9zTS2RwnX6qosAQPZ1TgTXhPqidPYDYObLU2N+GrW01FIshec90AxeKsISm7o0a0wJy AxPA== X-Gm-Message-State: AAQBX9faR89ytPUB19CiBITpRrRlJ3qNQeQEI1ov4eHlzdo6NwqFtjkn HMbCII24xCnnWuliY+jVUAiDJ+c4fRgaOb6YMKeGog== X-Google-Smtp-Source: AKy350ZytIqFIxF241Cc5DWUGITAX4wxiH5H8UDQs3LOVLj/40SDT84ZaQj91txN/VbkbzPeIDhNhw== X-Received: by 2002:a92:cc0d:0:b0:325:d560:2758 with SMTP id s13-20020a92cc0d000000b00325d5602758mr2682620ilp.17.1680628134741; Tue, 04 Apr 2023 10:08:54 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:54 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:54 -0600 Subject: [PATCH 6/9] Introduce mi_parse helper methods MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-6-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 | 70 +++++++++++++++++++++++++++++++++++++++++-------------- gdb/mi/mi-parse.h | 9 +++++++ 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index 57b9c46fc24..c9c446e0821 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 || language == language_auto) + 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,22 +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 - || parse->language == language_auto) - 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 Tue Apr 4 17:08:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67274 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 EF14D384D1BB for ; Tue, 4 Apr 2023 17:09:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EF14D384D1BB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628198; bh=YL5lkVULLsDc3crbkCxhIg0V4lZqSoPrtsotwrVddrc=; 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=TUbqFu3viDEALxyJryPyHKhKRqYD2fXD62G8O7sPWgMM6C3n2D/h585/NCaar4L/9 qWFpIV/CGDU826wwFjVGTr8Va8qMQoG/RrfJBzNCM5thjLHOuqf76I/kwDHPd8otdL V9QVgm+d+6Q9fvnZg36QlbDwusxOOKkeQIf8XOkc= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id A225F3858C39 for ; Tue, 4 Apr 2023 17:08:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A225F3858C39 Received: by mail-il1-x12d.google.com with SMTP id n1so16716448ili.10 for ; Tue, 04 Apr 2023 10:08:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628135; 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=YL5lkVULLsDc3crbkCxhIg0V4lZqSoPrtsotwrVddrc=; b=pBqz6hRU/Gl/ez0gEY3CV1cvU3/N5TcYYj390HrLylK9C0wyH0G84X+KxjcDPRLEzj 1kGXmPsFf08uqd2ZmZH8yQASXUnyGXIIBX6GINvQRH0GMFWPFsQYtXgnMC4Cv3Cvut8K 4+lIj+Ig/7D5kKBLSwF82Nyz2G6k1b2lf3n5il5sDPpO2qHrN8mJbIM4ZenLRpfyc7XJ GAZ+j9LiEDsYHZCSmwC/anGZgQts0VEsr/83+CzZX7x6UKPZYPRLZswzr3MPF5u5Lr2Z oz8o1RPvCTSF1Dlkk++MLgJEF6sQBnp/GJB9kkYvCT0nIxFuB8aFF3vrExPR9ebwAvx2 vmrA== X-Gm-Message-State: AAQBX9db9X0mknioxAsr/WABGAxns7r1uiKymddGzKUQQ0pSITpxyUtp 7w2+v98ZGA3R5YynpgDf24oOD/W1cQah65f/UYq7HQ== X-Google-Smtp-Source: AKy350aT0BFOnVH7QC7Ibd/kdGJxIGD82VsPr/5oop2D0jPIaYbfOEnxF3t/yEZ/vOJlZ7AJ5Gl3og== X-Received: by 2002:a92:d941:0:b0:315:352e:d5d0 with SMTP id l1-20020a92d941000000b00315352ed5d0mr2411457ilq.32.1680628135272; Tue, 04 Apr 2023 10:08:55 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:55 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:55 -0600 Subject: [PATCH 7/9] Add second mi_parse constructor MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-7-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/mi/mi-parse.h | 12 ++++++-- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index c9c446e0821..e570bdceff5 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,72 @@ 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 (""); + + /* Find the command in the MI table. */ + parse->cmd = mi_cmd_lookup (parse->command); + 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 Tue Apr 4 17:08:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67279 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 563C23858005 for ; Tue, 4 Apr 2023 17:11:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 563C23858005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628305; bh=pDNbSdUoiBgvkuBhfU9uk8uBtLihklA94sfGBLuLBeU=; 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=nF8am1mKMn2+/lheXGGAkgkUFDpjbGrjryHEgHBhJ9P2OtL3/DgOYYayvUJ1lAfSZ Bf7y1XXrStV2Atlr3Pjl0DR+6e35xy/gQXQa+MwpUrTPM5CVf/cF/i7xOIUetP/8sS W2xCgkMhKt2apYVKX3N5GUdz+Zf50b35JOmWwZ0Q= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by sourceware.org (Postfix) with ESMTPS id D961E385842A for ; Tue, 4 Apr 2023 17:08:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D961E385842A Received: by mail-io1-xd2c.google.com with SMTP id g16so4126383iom.11 for ; Tue, 04 Apr 2023 10:08:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628136; 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=pDNbSdUoiBgvkuBhfU9uk8uBtLihklA94sfGBLuLBeU=; b=1qPDfkrr4WC5ueukuM9PXCHQqe6/9EHvVGVadCsnZgTJQ0gEAl/LXzLT57wUeubVgJ BpLyCQhRb0DQmnJohHapfrsxZRBZRKkaq8YWBYUkDezbGGNONsK5VsLDG6SoKhiSihsv aNq01kADhSqEHL19I3ELirPlUrIoSDa/+ALiOexHOzIPI3uRNwcUvF15uB+QA3H7rcPF AQfoDyBTGaQQGhlyK1MQQF6We7bEYmWy0E0SYZNyBDpuRTwbwF28RtmmGFHnm27Zg3Q4 Bb5O8epzlF/5F9TT7vScYxDeemsBkAVmjLpa+XA5UkOsQiCIAgtvGNV4ivA+30PFzp8c 0NHw== X-Gm-Message-State: AAQBX9eRcRmTXJL/zT12PxQZydiFLc2NXXSLTioWCdHOKZpLjyHz2JHX ThDs632P0zRte0JW5M3T2z2jtg5ILacoCLmNqAofZw== X-Google-Smtp-Source: AKy350aKp0bsXz0pKVsHPRLA31fmVWxpR396SWT1haaaHii0o3xrRvx4DUCC7erujn8E/pJcfmFHlQ== X-Received: by 2002:a5e:c31a:0:b0:74c:9cc4:ac0 with SMTP id a26-20020a5ec31a000000b0074c9cc40ac0mr2677666iok.14.1680628135852; Tue, 04 Apr 2023 10:08:55 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:55 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:56 -0600 Subject: [PATCH 8/9] Implement gdb.execute_mi MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-8-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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 | 29 ++++ 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 | 18 ++ 10 files changed, 409 insertions(+) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 40497541880..35f7cd46e6c 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 10a1a70fa52..8d4cf2028b8 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -152,6 +152,9 @@ info main (program-counter) values, and can be used as the frame-id when calling gdb.PendingFrame.create_unwind_info. + ** 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 c74d586ef39..2d0e192a7a7 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4584,6 +4584,35 @@ 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. (Note that the leading @samp{-} should be omitted +here.) 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. +@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 490f50484d9..a64639b0cd5 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 3a114589e7c..1e6657487cd 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1963,6 +1963,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 617bdb23669..74c4c50a257 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 b295ff88743..70dd8ea3463 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2514,6 +2514,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..1e0c93e7c76 --- /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..44a533aa638 100644 --- a/gdb/testsuite/gdb.python/py-mi-cmd.py +++ b/gdb/testsuite/gdb.python/py-mi-cmd.py @@ -118,3 +118,21 @@ 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 + print("PASS") From patchwork Tue Apr 4 17:08:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 67276 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 22008385703A for ; Tue, 4 Apr 2023 17:10:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22008385703A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680628215; bh=HazBnl2lVz8zPPQU9aQrQ7hcz7JLg4566IjruQ6JdOQ=; 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=tyFlQzF2B5/7WrRaR3I9ebo7CqrZ8CKKbJboCchQ5rV4/PhJpPSpI/BDzJnL9dLwZ UIlk6oZOVH8ezXrMbUh5Mwszl/wybgGqSM96SkH3m/OZaZs+apmksUY6mMTPDdyqD3 lTu2q6ebq8F5SrHzgQp84LHFOLZFdj1+31RSccu0= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id D58243858428 for ; Tue, 4 Apr 2023 17:08:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D58243858428 Received: by mail-il1-x132.google.com with SMTP id q8so2812018ilo.1 for ; Tue, 04 Apr 2023 10:08:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680628136; 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=HazBnl2lVz8zPPQU9aQrQ7hcz7JLg4566IjruQ6JdOQ=; b=Cw3J5i343WvzSF0Ox8alehdl3ZJoB+pdbnrbWDnFedZixjFgCjoAgjnEBh3X7X8SN5 sVkxcdgbOrqkOVWAQJeWXgGz5K5EqCdxxgOnPyLe96lNbuDEzNRfpyOubt5n0CgXWw1X 80OutLguBew/V6A4vhI1i0JD0NskTYkqmXtqDRteW6UY4RpfCn5xEdGny9aexZ3UC6J6 hWl32YjsiIP6hURXAzFRoqG1hPOvNATg1XDm+5YnuPFgEPDPASioxH2cLkHj2hEIsesW x7FbUmlZR7gNIMSFN4RgaIkKgB9HYn2fX8kHEMteqrir6Gl3cHUeb+/zVeeqPVy/PPAb ZqRA== X-Gm-Message-State: AAQBX9e18oFZmTHyu2DDVmV2IkC2eqmUAvqPmSpiZhB/Lo2BgbWa5jeQ ZLWH19WXC8gDmSYPFQsTj2M+Hnk5dLaVlRkvHsIIjQ== X-Google-Smtp-Source: AKy350au1oYzSVlsA3nNmpl0/d7c4MaPSU1NdIGVzBpQMaXpusZbHb7kJuiBM856Pideb6gcSvWAaw== X-Received: by 2002:a92:db11:0:b0:326:2df9:3f18 with SMTP id b17-20020a92db11000000b003262df93f18mr2340937iln.20.1680628136431; Tue, 04 Apr 2023 10:08:56 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id f4-20020a02a104000000b0040b4c29010fsm1188306jag.140.2023.04.04.10.08.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 10:08:56 -0700 (PDT) Date: Tue, 04 Apr 2023 11:08:57 -0600 Subject: [PATCH 9/9] Implement DAP loadedSources request MIME-Version: 1.0 Message-Id: <20230404-dap-loaded-sources-v1-9-75c796bd644b@adacore.com> References: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> In-Reply-To: <20230404-dap-loaded-sources-v1-0-75c796bd644b@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.6 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 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..2dd26710d31 --- /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