From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by sourceware.org (Postfix) with ESMTPS id A51DA3858C50 for ; Fri, 28 Jul 2023 17:09:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A51DA3858C50 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-xd35.google.com with SMTP id ca18e2360f4ac-77ac14ff51bso90500039f.3 for ; Fri, 28 Jul 2023 10:09:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1690564158; x=1691168958; 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=6Iy//AYRtic4BVCDzJgOS5C7LUDenphcVEXI5U1k2XI=; b=X0Vc9kJo7DtEFh7MXNKfMgW+rmw1ZUFBo0KDBXAMxgxEFL7KpR3AiYM1ac4p7xsb8z P/Zm8Z+xD9jh2sCr6Kq2TzX+rpHyzrWmQv3wqdm7rwgj/h89xp87m9fb4ZNOdJ9if2sV 4FET/jyS+Cx86NGgdxleAlRlTbUJh/ctZRPGnoxvSIMoRu5eqBDAFmmj4TuCq22T6ljm umsSy0P8q0Zfp4+kYUD+GCXUj+xvSyZPqLcfolhCry5daqUC5pJheELYrbrUs4kg1keV ujixxn7Rg6OqFWfnqlhMOS/ARbP0wayDkIF+ZEIWJNbmANH9Hil91nkpVfk0PWpGeR40 GSNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690564158; x=1691168958; 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=6Iy//AYRtic4BVCDzJgOS5C7LUDenphcVEXI5U1k2XI=; b=K/Kmni/Rh3r1LVAoAhwx9zBTwmyhun4TPvuBXMw05y9bG41AOr/gPa69mWn1sh1G8f IYwGUteEc/WFPbr/L0A3tgp/0A7gDjX2khVHWObPk7fDTbh4MSuhx79C6nErM4RiC1r/ q9wAtOBhsu7Z9jFj99nJo06QDA26GRDwpIdx/rWP7ZlG+4Xg4/WzWbEDhdgptt63rwnj v34MpWeRVfLnhoyg1PFAnejWnjnBfB/aBNa9HhdCfPx9fmjTzbsXFuiF6157UPx1D9QK LxP4NPEDMeyX7giw7uaGbXlxbJMqfCR9F5pz6ZmzlTsSZhegci56qAfBAmhkUTYnGyRl 0ZiA== X-Gm-Message-State: ABy/qLY7ONMxFfUV3UjCrU8kcd8avrTs5gdjAbibxYmpy/q8a3R/0jNe lYO3zH4riLlD+X6iUUArnesJrTD54Zmd6ZuekaeyhQ== X-Google-Smtp-Source: APBJJlHwG/llUoMzvKvWGJkrOpmVI0rzRxS2svQbSEz5EJmkG3vUJkC/9HXtGFKSKJ2VVadug005GA== X-Received: by 2002:a6b:d617:0:b0:787:8fc:75d8 with SMTP id w23-20020a6bd617000000b0078708fc75d8mr190007ioa.9.1690564158407; Fri, 28 Jul 2023 10:09:18 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id k17-20020a5e8911000000b0077e3566a801sm1279269ioj.29.2023.07.28.10.09.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 10:09:18 -0700 (PDT) From: Tom Tromey Date: Fri, 28 Jul 2023 11:09:15 -0600 Subject: [PATCH 1/3] Introduce sourceReference handling in DAP MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230728-dap-source-refs-v1-1-1bcc5cdffd44@adacore.com> References: <20230728-dap-source-refs-v1-0-1bcc5cdffd44@adacore.com> In-Reply-To: <20230728-dap-source-refs-v1-0-1bcc5cdffd44@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 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,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 changes the gdb DAP implementation to emit a real sourceReference, rather than emitting 0. Sources are tracked in some maps in sources.py, and a new helper function is introduced to compute the "Source" object that can be sent to the client. --- gdb/python/lib/gdb/dap/breakpoint.py | 10 +++------ gdb/python/lib/gdb/dap/bt.py | 9 ++------ gdb/python/lib/gdb/dap/sources.py | 42 ++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/gdb/python/lib/gdb/dap/breakpoint.py b/gdb/python/lib/gdb/dap/breakpoint.py index 27745ebfd2c..9c214b2bf14 100644 --- a/gdb/python/lib/gdb/dap/breakpoint.py +++ b/gdb/python/lib/gdb/dap/breakpoint.py @@ -21,6 +21,7 @@ import re from typing import Optional, Sequence from .server import request, capability, send_event +from .sources import make_source from .startup import send_gdb_with_response, in_gdb_thread, log_stack from .typecheck import type_check @@ -48,15 +49,10 @@ def breakpoint_descriptor(bp): # multiple locations. See # https://github.com/microsoft/debug-adapter-protocol/issues/13 loc = bp.locations[0] - (basename, line) = loc.source + (filename, line) = loc.source result.update( { - "source": { - "name": os.path.basename(basename), - # We probably don't need this but it doesn't hurt to - # be explicit. - "sourceReference": 0, - }, + "source": make_source(filename, os.path.basename(filename)), "line": line, "instructionReference": hex(loc.address), } diff --git a/gdb/python/lib/gdb/dap/bt.py b/gdb/python/lib/gdb/dap/bt.py index 975c88f8208..34d05cbaac0 100644 --- a/gdb/python/lib/gdb/dap/bt.py +++ b/gdb/python/lib/gdb/dap/bt.py @@ -20,6 +20,7 @@ from gdb.frames import frame_iterator from .frames import frame_id from .modules import module_id from .server import request, capability +from .sources import make_source from .startup import send_gdb_with_response, in_gdb_thread from .state import set_thread @@ -59,13 +60,7 @@ def _backtrace(thread_id, levels, startFrame): 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, - } + newframe["source"] = make_source(filename, os.path.basename(filename)) 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 diff --git a/gdb/python/lib/gdb/dap/sources.py b/gdb/python/lib/gdb/dap/sources.py index af7313ca2f9..50b5909ed1d 100644 --- a/gdb/python/lib/gdb/dap/sources.py +++ b/gdb/python/lib/gdb/dap/sources.py @@ -19,16 +19,46 @@ from .server import request, capability from .startup import send_gdb_with_response, in_gdb_thread +# The next available source reference ID. Must be greater than 0. +_next_source = 1 + +# Map from full paths to Source dictionaries. +_source_map = {} + +# Map from a source reference ID back to the same Source that is +# stored in _source_map. +_id_map = {} + + +@in_gdb_thread +def make_source(fullname, filename): + """Return the Source for a given file name. + + FULLNAME is the full name. This is used as the key. + FILENAME is the base name. + """ + global _source_map + if fullname in _source_map: + result = _source_map[fullname] + else: + global _next_source + result = { + "name": filename, + "path": fullname, + "sourceReference": _next_source, + } + _source_map[fullname] = result + global _id_map + _id_map[_next_source] = result + _next_source += 1 + return result + + @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"], - } - ) + result.append(make_source(elt["fullname"], elt["file"])) return { "sources": result, } -- 2.40.1