From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by sourceware.org (Postfix) with ESMTPS id 1FD173858004 for ; Thu, 4 May 2023 14:21:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1FD173858004 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-io1-xd32.google.com with SMTP id ca18e2360f4ac-760ecf3424bso10487839f.2 for ; Thu, 04 May 2023 07:21:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1683210090; x=1685802090; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=QtrXtPAJ6PBcimlI9slFAtcLYFZsacjs3QzqXtI1gy8=; b=QuY5Vh/QwYfaRJHTAkKo6qp5lhiYGBl8LnB1dFIkPlESbgA+OHSbc/xXkMlwl1sIH6 XsplpMk5rjjeTnFbMHmo6QOmU/LMSnhn2/4fmT6Cdhye8JAMPNpxAc0JOtEg+OSL4yhp LQGEYFT8Yj5oEx9mTnGl5QiETxKN7vQqk0JMk8WBhXcoAMtm7/H8LUnFiRx/Flrw/+hP LwvXE/1J+rSsxhXR6tahTY8539v+DFPHM5C3erZYh82YrSnazj7L1FcEoiFVWrjY5zJA wDBvEuLsHMVlYNMcfr8FMSzQFR4JUXlvU6rTnm1lTHhVOeCSn4MPDLuuBamfUXVh8Fev 2dLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683210090; x=1685802090; 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=QtrXtPAJ6PBcimlI9slFAtcLYFZsacjs3QzqXtI1gy8=; b=a2oA5F3mFmr8+s8pdfxguJ4zlo2nN3cbC8HOGT0IuJPdWPo0vPFmxnUobs8u2+Phd3 Dfe1tCCCVDKHtlFsJF5QaWK29NMU7REIJsSpa2yyecnHZ+HXcZgFmE3bDvFiEEh376Y5 hppVjTU1l9RJtDgPvZo0eBIsxvTq1ytG4oZjJfvBuOdk6D+oeMR65Tts9w3kLuBtuJ49 5X1C3KnXvvJGES5CTY6inw9MW4i6f+ovhAc9LqP9x4qSbPxc2yGaIEssvIQUbcbKttgT nofyKJgW+vwpLaeTNUrAuISow5H6HCL+Ikx9rfkQTYQpeLX2n2SVOKdK+EKAvVTdfYzy cjFA== X-Gm-Message-State: AC+VfDx+cZVNvdBXDMxW9IJ6loBJX/FsQV6ebqJXny+Q2zoUzGqk+vfu 9HEYJCUrxMrz6bP020wsHABhmq5W9MZfHyssikrEaA== X-Google-Smtp-Source: ACHHUZ5/r9Ni7ltSL1kLVdsnY1fC4HfHpk3hCeu345mBrYjwqeTKMpgqm/e1WwTgtdlb2T3CY8IBOA== X-Received: by 2002:a5d:9a06:0:b0:763:92eb:f81e with SMTP id s6-20020a5d9a06000000b0076392ebf81emr15868021iol.8.1683210090309; Thu, 04 May 2023 07:21:30 -0700 (PDT) Received: from localhost.localdomain (71-211-191-82.hlrn.qwest.net. [71.211.191.82]) by smtp.gmail.com with ESMTPSA id r16-20020a6b4410000000b00760a07e4e09sm9983384ioa.5.2023.05.04.07.21.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 07:21:29 -0700 (PDT) From: Tom Tromey Date: Thu, 04 May 2023 08:21:31 -0600 Subject: [PATCH 10/11] Add global_context parameter to gdb.parse_and_eval MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230504-frameless-v1-10-4191201740b0@adacore.com> References: <20230504-frameless-v1-0-4191201740b0@adacore.com> In-Reply-To: <20230504-frameless-v1-0-4191201740b0@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.4 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 List-Id: This adds a 'global_context' parse_and_eval to gdb.parse_and_eval. This lets users request a parse that is done at "global scope". I considered letting callers pass in a block instead, with None meaning "global" -- but then there didn't seem to be a clean way to express the default for this parameter. --- gdb/NEWS | 4 ++++ gdb/doc/python.texi | 7 ++++++- gdb/python/python.c | 28 ++++++++++++++++++++++------ gdb/testsuite/gdb.python/py-value.c | 3 +++ gdb/testsuite/gdb.python/py-value.exp | 5 +++++ 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index e3c095de09e..4bf5603b550 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -162,6 +162,10 @@ info main (program-counter) values, and can be used as the frame-id when calling gdb.PendingFrame.create_unwind_info. + ** gdb.parse_and_eval now has a new "global_context" parameter. + This can be used to request that the parse only examine global + symbols. + *** 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 7c3a3ccd379..dca16bb84e0 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -419,11 +419,16 @@ using the @code{gdb.Value} constructor. @end defun @findex gdb.parse_and_eval -@defun gdb.parse_and_eval (expression) +@defun gdb.parse_and_eval (expression, @r{[}, global_context@r{]}) Parse @var{expression}, which must be a string, as an expression in the current language, evaluate it, and return the result as a @code{gdb.Value}. +@var{global_context}, if provided, is a boolean indicating whether the +parsing should be done in the global context. The default is +@samp{False}, meaning that the current frame or current static context +should be used. + This function can be useful when implementing a new command (@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}), as it provides a way to parse the diff --git a/gdb/python/python.c b/gdb/python/python.c index 168a0009f1b..42bb2ed34b1 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -969,19 +969,34 @@ gdbpy_decode_line (PyObject *self, PyObject *args) /* Parse a string and evaluate it as an expression. */ static PyObject * -gdbpy_parse_and_eval (PyObject *self, PyObject *args) +gdbpy_parse_and_eval (PyObject *self, PyObject *args, PyObject *kw) { + static const char *keywords[] = { "expression", "global_context", nullptr }; + const char *expr_str; + PyObject *global_context_obj = nullptr; - if (!PyArg_ParseTuple (args, "s", &expr_str)) - return NULL; + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords, + &expr_str, + &PyBool_Type, &global_context_obj)) + return nullptr; + + parser_flags flags = 0; + if (global_context_obj != NULL) + { + int cmp = PyObject_IsTrue (global_context_obj); + if (cmp < 0) + return nullptr; + if (cmp) + flags |= PARSER_LEAVE_BLOCK_ALONE; + } PyObject *result = nullptr; try { gdbpy_allow_threads allow_threads; scoped_value_mark free_values; - struct value *val = parse_and_eval (expr_str); + struct value *val = parse_and_eval (expr_str, flags); result = value_to_value_object (val); } catch (const gdb_exception &except) @@ -2591,8 +2606,9 @@ The first element contains any unparsed portion of the String parameter\n\ (or None if the string was fully parsed). The second element contains\n\ a tuple that contains all the locations that match, represented as\n\ gdb.Symtab_and_line objects (or None)."}, - { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, - "parse_and_eval (String) -> Value.\n\ + { "parse_and_eval", (PyCFunction) gdbpy_parse_and_eval, + METH_VARARGS | METH_KEYWORDS, + "parse_and_eval (String, [Boolean]) -> Value.\n\ Parse String as an expression, evaluate it, and return the result as a Value." }, diff --git a/gdb/testsuite/gdb.python/py-value.c b/gdb/testsuite/gdb.python/py-value.c index 5a578de5d50..f0b1f5f160c 100644 --- a/gdb/testsuite/gdb.python/py-value.c +++ b/gdb/testsuite/gdb.python/py-value.c @@ -79,6 +79,8 @@ int func2 (int arg1, int arg2) char **save_argv; +int shadowed = 23; + int main (int argc, char *argv[]) { @@ -96,6 +98,7 @@ main (int argc, char *argv[]) int i = 2; int *ptr_i = &i; struct str *xstr; + int shadowed = 97; /* Prevent gcc from optimizing argv[] out. */ diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index 898208b90d1..9fc25814721 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -338,6 +338,11 @@ proc test_value_in_inferior {} { gdb_py_test_silent_cmd "python str = '\"str\"'" "set up str variable" 1 gdb_test "python print (gdb.parse_and_eval (str).string (length = 10))" \ "gdb.error: Attempt to take address of value not located in memory.\r\nError while executing Python code." + + gdb_test "python print (gdb.parse_and_eval ('shadowed'))" \ + 97 "shadowed local value" + gdb_test "python print (gdb.parse_and_eval ('shadowed', global_context=True))" \ + 23 "shadowed global value" } proc test_inferior_function_call {} { -- 2.39.1