From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 5BA1A38582BC for ; Wed, 14 Jun 2023 16:30:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5BA1A38582BC 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-il1-x12e.google.com with SMTP id e9e14a558f8ab-33b1e83e1daso29539975ab.0 for ; Wed, 14 Jun 2023 09:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1686760251; x=1689352251; 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=VW+x4piylKX5Gjt+Z9cq6o6FYK4pmPI6G9wxHuM6hPI=; b=N7B9jBZ9nMaNYrFZQPmnqM5U6lOn/mUnhu7QfrMsVII5NNt+VzQApkzmJsMiBlmd6w 1Lwybm2t1Z5AYyLTdyj5yI+8ZqVxFj9zxcPRVw5z47BoL4beuLI850u8W+C/bz9BaGre eo4lk64JsFrEQLYwLAk+ZwYQb+RuE9DUHX5gRjI61WSdzFdadbQ7/wsylN1vxOgaCXtC VIOAFKH+x2C/O82iB5OtIJ21yqmX6WBJMmFW9twEpdq2Yqu6J27F1+mXCWphDyz2m9gV 2P+jcsyR6CcL5ZZGIE3rEwuNdEHymaVGjRz7cx86a8OIJuCpY+N6Xbamc/uJ+Y85hbwo TSNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686760251; x=1689352251; 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=VW+x4piylKX5Gjt+Z9cq6o6FYK4pmPI6G9wxHuM6hPI=; b=Z48wcFDOoGnUggwr/oRk1YAS1yt51fnYi4bI1Lu7tWb6K9TfvhXwkG2JP/b+k1uSyZ 6kA0CkirtdINWHI3zcaIKPELCdfpgM8WiAs6ugK7SKI4R+Bz5QQ24FwZYVDbRR10BLrg bfNEwXNPDF7DNA43KivygcfcUnC0LnGeZ1cL7Czg7IBb1EcTXD5Sazr7/OQ6zy0Nmusa 43r6rlH1iS74cBgHaAHdcvg3xxCRVhME2WD7OHj2yxMKouFMcRd3j+jVzF5Zl13Lw1fO f7DKB0vYOCpx+azb6YfCNVUvdqPRnykcYrndDn4FwQxOcyC6gKdv23WmhQWoXt2J8s19 6sMw== X-Gm-Message-State: AC+VfDzf5zn1mmj2GAmPvGgtOFN+FhtPAdwPRwbX9hGzDrDJ734qajsA 1N93VJ/0LEcRXHweB9242arXWxN9ZRim60kGhL+9bA== X-Google-Smtp-Source: ACHHUZ4UGTTsXA6YJoLDJNJVyu3noyNW9hxBaHfrMXl5yg5tWhRP458n2clEAYU+4M4Ke3qfWJeAew== X-Received: by 2002:a92:d949:0:b0:33e:7b40:18b7 with SMTP id l9-20020a92d949000000b0033e7b4018b7mr14280441ilq.0.1686760251302; Wed, 14 Jun 2023 09:30:51 -0700 (PDT) Received: from localhost.localdomain (75-166-136-83.hlrn.qwest.net. [75.166.136.83]) by smtp.gmail.com with ESMTPSA id t5-20020a028785000000b0040fa5258658sm5020983jai.77.2023.06.14.09.30.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 09:30:51 -0700 (PDT) From: Tom Tromey Date: Wed, 14 Jun 2023 10:30:51 -0600 Subject: [PATCH 5/5] Reimplement DAP stack traces using frame filters MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230614-dap-frame-decor-v1-5-af87db6467b2@adacore.com> References: <20230614-dap-frame-decor-v1-0-af87db6467b2@adacore.com> In-Reply-To: <20230614-dap-frame-decor-v1-0-af87db6467b2@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 reimplements DAP stack traces using frame filters. This slightly simplifies the code, because frame filters and DAP were already doing some similar work. This also renames RegisterReference and ScopeReference to make it clear that these are private (and so changes don't have to worry about other files). Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30468 --- gdb/python/lib/gdb/dap/bt.py | 81 ++++++++++++++++--------------------- gdb/python/lib/gdb/dap/evaluate.py | 11 ++--- gdb/python/lib/gdb/dap/frames.py | 7 ++++ gdb/python/lib/gdb/dap/scopes.py | 83 +++++++++++++++----------------------- 4 files changed, 79 insertions(+), 103 deletions(-) diff --git a/gdb/python/lib/gdb/dap/bt.py b/gdb/python/lib/gdb/dap/bt.py index 4439b428926..0350a3bb6d5 100644 --- a/gdb/python/lib/gdb/dap/bt.py +++ b/gdb/python/lib/gdb/dap/bt.py @@ -15,67 +15,56 @@ import gdb import os +import itertools +from gdb.frames import frame_iterator +from gdb.FrameIterator import FrameIterator +from gdb.FrameDecorator import FrameDecorator from .frames import frame_id from .server import request, capability from .startup import send_gdb_with_response, in_gdb_thread from .state import set_thread -# Helper function to safely get the name of a frame as a string. -@in_gdb_thread -def _frame_name(frame): - name = frame.name() - if name is None: - name = "???" - return name - - -# Helper function to get a frame's SAL without an error. -@in_gdb_thread -def _safe_sal(frame): - try: - return frame.find_sal() - except gdb.error: - return None - - # Helper function to compute a stack trace. @in_gdb_thread def _backtrace(thread_id, levels, startFrame): set_thread(thread_id) frames = [] - current_number = 0 + if levels == 0: + # Zero means all remaining frames. + high = -1 + else: + # frame_iterator uses an inclusive range, so subtract one. + high = startFrame + levels - 1 try: - current_frame = gdb.newest_frame() + frame_iter = frame_iterator(gdb.newest_frame(), startFrame, high) except gdb.error: - current_frame = None - while current_frame is not None and (levels == 0 or len(frames) < levels): - # This condition handles the startFrame==0 case as well. - if current_number >= startFrame: - newframe = { - "id": frame_id(current_frame), - "name": _frame_name(current_frame), - # This must always be supplied, but we will set it - # correctly later if that is possible. - "line": 0, - # GDB doesn't support columns. - "column": 0, - "instructionPointerReference": hex(current_frame.pc()), + frame_iter = () + for current_frame in frame_iter: + newframe = { + "id": frame_id(current_frame), + "name": current_frame.function(), + # This must always be supplied, but we will set it + # correctly later if that is possible. + "line": 0, + # GDB doesn't support columns. + "column": 0, + "instructionPointerReference": hex(current_frame.address()), + } + line = current_frame.line() + if line is not None: + newframe["line"] = line + filename = current_frame.filename() + if filename is not None: + newframe["source"] = { + "name": os.path.basename(filename), + "path": filename, + # We probably don't need this but it doesn't hurt + # to be explicit. + "sourceReference": 0, } - sal = _safe_sal(current_frame) - if sal is not None and sal.symtab is not None: - newframe["source"] = { - "name": os.path.basename(sal.symtab.filename), - "path": sal.symtab.filename, - # We probably don't need this but it doesn't hurt - # to be explicit. - "sourceReference": 0, - } - newframe["line"] = sal.line - frames.append(newframe) - current_number = current_number + 1 - current_frame = current_frame.older() + frames.append(newframe) # Note that we do not calculate totalFrames here. Its absence # tells the client that it may simply ask for frames until a # response yields fewer frames than requested. diff --git a/gdb/python/lib/gdb/dap/evaluate.py b/gdb/python/lib/gdb/dap/evaluate.py index af7bf43afd0..01b9eb77202 100644 --- a/gdb/python/lib/gdb/dap/evaluate.py +++ b/gdb/python/lib/gdb/dap/evaluate.py @@ -19,7 +19,7 @@ import gdb.printing # This is deprecated in 3.9, but required in older versions. from typing import Optional -from .frames import frame_for_id +from .frames import select_frame from .server import capability, request from .startup import send_gdb_with_response, in_gdb_thread from .varref import find_variable, VariableReference @@ -35,8 +35,7 @@ class EvaluateResult(VariableReference): def _evaluate(expr, frame_id): global_context = True if frame_id is not None: - frame = frame_for_id(frame_id) - frame.select() + select_frame(frame_id) global_context = False val = gdb.parse_and_eval(expr, global_context=global_context) ref = EvaluateResult(val) @@ -48,8 +47,7 @@ def _evaluate(expr, frame_id): def _set_expression(expression, value, frame_id): global_context = True if frame_id is not None: - frame = frame_for_id(frame_id) - frame.select() + select_frame(frame_id) global_context = False lhs = gdb.parse_and_eval(expression, global_context=global_context) rhs = gdb.parse_and_eval(value, global_context=global_context) @@ -61,8 +59,7 @@ def _set_expression(expression, value, frame_id): @in_gdb_thread def _repl(command, frame_id): if frame_id is not None: - frame = frame_for_id(frame_id) - frame.select() + select_frame(frame_id) val = gdb.execute(command, from_tty=True, to_string=True) return { "result": val, diff --git a/gdb/python/lib/gdb/dap/frames.py b/gdb/python/lib/gdb/dap/frames.py index 08209d0b361..1d2d1371354 100644 --- a/gdb/python/lib/gdb/dap/frames.py +++ b/gdb/python/lib/gdb/dap/frames.py @@ -52,3 +52,10 @@ def frame_for_id(id): """Given a frame identifier ID, return the corresponding frame.""" global _all_frames return _all_frames[id] + + +@in_gdb_thread +def select_frame(id): + """Given a frame identifier ID, select the corresponding frame.""" + frame = frame_for_id(id) + frame.inferior_frame().select() diff --git a/gdb/python/lib/gdb/dap/scopes.py b/gdb/python/lib/gdb/dap/scopes.py index 9b80dd9ce80..1687094c4ce 100644 --- a/gdb/python/lib/gdb/dap/scopes.py +++ b/gdb/python/lib/gdb/dap/scopes.py @@ -21,41 +21,17 @@ from .server import request from .varref import BaseReference -# Helper function to return a frame's block without error. -@in_gdb_thread -def _safe_block(frame): - try: - return frame.block() - except gdb.error: - return None - - -# Helper function to return two lists of variables of block, up to the -# enclosing function. The result is of the form (ARGS, LOCALS), where -# each element is itself a list. -@in_gdb_thread -def _block_vars(block): - args = [] - locs = [] - while True: - for var in block: - if var.is_argument: - args.append(var) - elif var.is_variable or var.is_constant: - locs.append(var) - if block.function is not None: - break - block = block.superblock - return (args, locs) - - -class ScopeReference(BaseReference): +class _ScopeReference(BaseReference): def __init__(self, name, hint, frame, var_list): super().__init__(name) self.hint = hint self.frame = frame + self.inf_frame = frame.inferior_frame() self.func = frame.function() - self.var_list = var_list + self.line = frame.line() + # VAR_LIST might be any kind of iterator, but it's convenient + # here if it is just a collection. + self.var_list = tuple(var_list) def to_object(self): result = super().to_object() @@ -63,8 +39,8 @@ class ScopeReference(BaseReference): # How would we know? result["expensive"] = False result["namedVariables"] = len(self.var_list) - if self.func is not None: - result["line"] = self.func.line + if self.line is not None: + result["line"] = self.line # FIXME construct a Source object return result @@ -73,38 +49,45 @@ class ScopeReference(BaseReference): @in_gdb_thread def fetch_one_child(self, idx): + # Here SYM will conform to the SymValueWrapper interface. sym = self.var_list[idx] - if sym.needs_frame: - val = sym.value(self.frame) - else: - val = sym.value() - return (sym.print_name, val) - - -class RegisterReference(ScopeReference): + name = str(sym.symbol()) + val = sym.value() + if val is None: + # No synthetic value, so must read the symbol value + # ourselves. + val = sym.symbol().value(self.inf_frame) + elif not isinstance(val, gdb.Value): + val = gdb.Value(val) + return (name, val) + + +class _RegisterReference(_ScopeReference): def __init__(self, name, frame): super().__init__( - name, "registers", frame, list(frame.architecture().registers()) + name, "registers", frame, frame.inferior_frame().architecture().registers() ) @in_gdb_thread def fetch_one_child(self, idx): - return (self.var_list[idx].name, self.frame.read_register(self.var_list[idx])) + return ( + self.var_list[idx].name, + self.inf_frame.read_register(self.var_list[idx]), + ) # Helper function to create a DAP scopes for a given frame ID. @in_gdb_thread def _get_scope(id): frame = frame_for_id(id) - block = _safe_block(frame) scopes = [] - if block is not None: - (args, locs) = _block_vars(block) - if args: - scopes.append(ScopeReference("Arguments", "arguments", frame, args)) - if locs: - scopes.append(ScopeReference("Locals", "locals", frame, locs)) - scopes.append(RegisterReference("Registers", frame)) + args = frame.frame_args() + if args: + scopes.append(_ScopeReference("Arguments", "arguments", frame, args)) + locs = frame.frame_locals() + if locs: + scopes.append(_ScopeReference("Locals", "locals", frame, locs)) + scopes.append(_RegisterReference("Registers", frame)) return [x.to_object() for x in scopes] -- 2.40.1