From patchwork Wed Nov 1 17:09:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 78885 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 8D5B53857B9B for ; Wed, 1 Nov 2023 17:10:33 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by sourceware.org (Postfix) with ESMTPS id 9EB763858CD1 for ; Wed, 1 Nov 2023 17:09:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9EB763858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9EB763858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698858599; cv=none; b=idb6bC/ck+nyWKCCqhLFzbbIaFtbuykozEws2sxKtSfG0lmernlseJOs31ttRS3IhBNwPbswmoNpJgC9d55l3ce6lrDBxNhN/GvppfZ2YiN9mvDQeSMOQITpeVUScJXewV1z+Yfmy/0Z5/7tGgAZg+LaKTR02VW/vuw77KgL/os= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698858599; c=relaxed/simple; bh=MsFabVKnTzCyNz1CNTqTM7G6hBAvLERlzmnzjKhVRm0=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=tu/ewhA6OcWWQgu5MjIectW7R61UVOo8g9FBM6e83+EPQ9yv5fZCfMXlpwPWQdQlTcWQqM3t3oKqZbFfgZoHE4qk3izw3w7v/2flObuhNZU2JGF2JXyipMLj3WE2llfj3kqHlEjZ7FiDgUqXMZs4/vLeQs+PlxhbChNJZ+CY6/g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd33.google.com with SMTP id ca18e2360f4ac-7a683bd610dso236893039f.1 for ; Wed, 01 Nov 2023 10:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1698858582; x=1699463382; darn=sourceware.org; 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=LzVlJ17BxTlM3jAfKuPriFYd/vaw81jBaafXziB3cBw=; b=IX5O9v9/mkzfXGoBEp9wyNdJviZekZfKa2fJF4NbLlcfcjxtwb/Rv5AsW98/KV9BGG +gxk9EkjeTV2dKKYsMRZ5BjoZbeIvZ0prQuZ/+FFI+HaV4fq/j2gCAOyM2lHvWPk4SNo +iYCRdOL+M/CTZIGDelZiDH5TF+WdUFCqUCJFOwUIDVgHacdLBqOxn12Sl3SUtqoJnXq A52qhMhnyUyPBaADamKZLfseZ8zjUMkRpIiTA30ilS0JLQ9bxdShwVu7Myj/15qInWOv FdmTuGYWRiO4PVAg2/1LcT4R8KVkLKqR19xjf1nodtSr4CiIAnZroDTQaD9mGu4Xao+U 9t5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698858582; x=1699463382; 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=LzVlJ17BxTlM3jAfKuPriFYd/vaw81jBaafXziB3cBw=; b=QB+Gw/iA6/FkR+4B/NxmE1j9aOTYyTO92jkKc+Ys0Fok4uKBUuWl8aaoG1prMpEauQ 7IYhCi8SG1mo8KnQb3wd+D69a0a8JqgCP2hgHsARjX+2Tql7poj6n/d4MGkSe8mU5wmU iueFG2pZ/naeQORGMO50+eQ6N/J8+eLiCFJ2cMvNptX/x5EppUVa0mpWJPDikgYl6lJl J8gu6cJ0W60CwZxdPHQx4kT0m7UbU8/FiHBDPHbyb/k8bvgBE7ROBLOY90atAsOmVH6A 65rLhxQLVHVvImYDiD7dVrBEyFlff6y6yeT7tZL+e0jBnMZRSmgXLpRdCRUJVXA0nLi5 y4Dg== X-Gm-Message-State: AOJu0Yy0OAefW0cPLGTY5/e1i/+1E3cNVS77lbGq30zTPn3CZNJRXmF3 JfE9s1mfEPZe6lU/mFUgqcbama5/cA6zIuKah81j8Q== X-Google-Smtp-Source: AGHT+IE0xdgaFGKPQsizsXKqMBb4rRmqRvcPCs+wSjgcHiG/hnqXzhTujMeAegunbApnsyhkGmWdqw== X-Received: by 2002:a05:6602:1492:b0:786:ef34:1a6 with SMTP id a18-20020a056602149200b00786ef3401a6mr20568460iow.7.1698858581729; Wed, 01 Nov 2023 10:09:41 -0700 (PDT) Received: from localhost.localdomain (97-122-77-73.hlrn.qwest.net. [97.122.77.73]) by smtp.gmail.com with ESMTPSA id gm2-20020a0566382b8200b0042b3a328ee0sm1017019jab.166.2023.11.01.10.09.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 10:09:41 -0700 (PDT) From: Tom Tromey Date: Wed, 01 Nov 2023 11:09:38 -0600 Subject: [PATCH 6/7] Handle the static link in FrameDecorator MIME-Version: 1.0 Message-Id: <20231101-dap-nested-function-v1-6-0b0c3b228ac7@adacore.com> References: <20231101-dap-nested-function-v1-0-0b0c3b228ac7@adacore.com> In-Reply-To: <20231101-dap-nested-function-v1-0-0b0c3b228ac7@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.8 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org A co-worker requested that the DAP scope for a nested function's frame also show the variables from outer frames. DAP doesn't directly support this notion, so this patch arranges to put these variables into the inner frames "Locals" scope. I chose to do this only for DAP. For CLI and MI, gdb currently does not do this, so this preserves the behavior. Note that an earlier patch (see commit 4a1311ba) removed some code that seemed to do something similar. However, that code did not actually work. --- gdb/python/lib/gdb/FrameDecorator.py | 64 +++++++++++++++++----- gdb/testsuite/gdb.dap/ada-nested.exp | 91 +++++++++++++++++++++++++++++++ gdb/testsuite/gdb.dap/ada-nested/prog.adb | 32 +++++++++++ 3 files changed, 174 insertions(+), 13 deletions(-) diff --git a/gdb/python/lib/gdb/FrameDecorator.py b/gdb/python/lib/gdb/FrameDecorator.py index 39ee2e2547c..b6eb1ab1de0 100644 --- a/gdb/python/lib/gdb/FrameDecorator.py +++ b/gdb/python/lib/gdb/FrameDecorator.py @@ -213,18 +213,36 @@ class DAPFrameDecorator(_FrameDecoratorBase): return sal.symtab.fullname() return None + def frame_locals(self): + """Return an iterable of local variables for this frame, if + any. The iterable object contains objects conforming with the + Symbol/Value interface. If there are no frame locals, or if + this frame is deemed to be a special case, return None.""" + + if hasattr(self._base, "frame_locals"): + return self._base.frame_locals() + + frame = self.inferior_frame() + args = FrameVars(frame) + return args.fetch_frame_locals(True) + class SymValueWrapper(object): """A container class conforming to the Symbol/Value interface which holds frame locals or frame arguments.""" - def __init__(self, symbol, value): + # The FRAME argument is needed here because gdb.Symbol doesn't + # carry the block with it, and so read_var can't find symbols from + # outer (static link) frames. + def __init__(self, frame, symbol): + self.frame = frame self.sym = symbol - self.val = value def value(self): """Return the value associated with this symbol, or None""" - return self.val + if self.frame is None: + return None + return self.frame.read_var(self.sym) def symbol(self): """Return the symbol, or Python text, associated with this @@ -240,32 +258,50 @@ class FrameVars(object): def __init__(self, frame): self.frame = frame - def fetch_frame_locals(self): + def fetch_frame_locals(self, follow_link=False): """Public utility method to fetch frame local variables for the stored frame. Frame arguments are not fetched. If there are no frame local variables, return an empty list.""" lvars = [] + frame = self.frame try: - block = self.frame.block() + block = frame.block() except RuntimeError: block = None + traversed_link = False while block is not None: if block.is_global or block.is_static: break for sym in block: + # Exclude arguments from the innermost function, but + # if we found and traversed a static link, just treat + # all such variables as "local". if sym.is_argument: + if not traversed_link: + continue + elif not sym.is_variable: + # We use an 'elif' here because is_variable + # returns False for arguments as well. Anyway, + # don't include non-variables here. continue - if sym.is_variable: - lvars.append(SymValueWrapper(sym, None)) + lvars.append(SymValueWrapper(frame, sym)) - # Stop when the function itself is seen, to avoid showing - # variables from outer functions in a nested function. if block.function is not None: - break - - block = block.superblock + if not follow_link: + break + # If the frame has a static link, follow it here. + traversed_link = True + frame = frame.static_link() + if frame is None: + break + try: + block = frame.block() + except RuntimeError: + block = None + else: + block = block.superblock return lvars @@ -287,10 +323,12 @@ class FrameVars(object): for sym in block: if not sym.is_argument: continue - args.append(SymValueWrapper(sym, None)) + args.append(SymValueWrapper(None, sym)) # Stop when the function itself is seen, to avoid showing # variables from outer functions in a nested function. + # Note that we don't traverse the static link for + # arguments, only for locals. if block.function is not None: break diff --git a/gdb/testsuite/gdb.dap/ada-nested.exp b/gdb/testsuite/gdb.dap/ada-nested.exp new file mode 100644 index 00000000000..1a02f4f352e --- /dev/null +++ b/gdb/testsuite/gdb.dap/ada-nested.exp @@ -0,0 +1,91 @@ +# 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 . + +# Check the scope of a nested function. + +load_lib ada.exp +load_lib dap-support.exp + +require allow_ada_tests allow_dap_tests + +standard_ada_testfile prog + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable \ + {debug additional_flags=-gnata}] != ""} { + return -1 +} + +if {[dap_launch $binfile] == ""} { + return +} + +set line [gdb_get_line_number "STOP"] +set obj [dap_check_request_and_response "set breakpoint" \ + setBreakpoints \ + [format {o source [o path [%s]] \ + breakpoints [a [o line [i %d]]]} \ + [list s $srcfile] $line]] +set fn_bpno [dap_get_breakpoint_number $obj] + +dap_check_request_and_response "start inferior" configurationDone + +dap_wait_for_event_and_check "stopped at breakpoint" stopped \ + "body reason" breakpoint \ + "body hitBreakpointIds" $fn_bpno + +set bt [lindex [dap_check_request_and_response "backtrace" stackTrace \ + {o threadId [i 1]}] \ + 0] +set frame_id [dict get [lindex [dict get $bt body stackFrames] 0] id] + +set scopes [dap_check_request_and_response "get scopes" scopes \ + [format {o frameId [i %d]} $frame_id]] +set scopes [dict get [lindex $scopes 0] body scopes] + +# This is what the implementation does, so we can assume it, but check +# just in case something changes. +lassign $scopes args locals _ignore +gdb_assert {[dict get $args name] == "Arguments"} "argument scope" +gdb_assert {[dict get $locals name] == "Locals"} "local scope" + +gdb_assert {[dict get $locals namedVariables] == 3} "two locals" + +set num [dict get $locals variablesReference] +set refs [lindex [dap_check_request_and_response "fetch variables" \ + "variables" \ + [format {o variablesReference [i %d] count [i 3]} \ + $num]] \ + 0] + +foreach var [dict get $refs body variables] { + set name [dict get $var name] + + switch $name { + "i" { + gdb_assert {[dict get $var value] == "1"} "check value of i" + } + "x" { + gdb_assert {[dict get $var value] == "12"} "check value of x" + } + "outer_arg" { + gdb_assert {[dict get $var value] == "1"} "check value of outer_arg" + } + default { + fail "unknown variable $name" + } + } +} + +dap_shutdown diff --git a/gdb/testsuite/gdb.dap/ada-nested/prog.adb b/gdb/testsuite/gdb.dap/ada-nested/prog.adb new file mode 100644 index 00000000000..6b38835a94c --- /dev/null +++ b/gdb/testsuite/gdb.dap/ada-nested/prog.adb @@ -0,0 +1,32 @@ +-- 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 . + +procedure Foo is + X : Integer := 12; + + procedure Outer (Outer_Arg : Integer) is + procedure Bump (Stride : Integer) is + begin + X := X + Stride; -- STOP + end; + begin + Bump (Outer_Arg); + end; + +begin + for I in 1 .. 20 loop + Outer (1); + end loop; +end Foo;