public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/3] Introduce sourceReference handling in DAP
Date: Fri, 28 Jul 2023 11:09:15 -0600	[thread overview]
Message-ID: <20230728-dap-source-refs-v1-1-1bcc5cdffd44@adacore.com> (raw)
In-Reply-To: <20230728-dap-source-refs-v1-0-1bcc5cdffd44@adacore.com>

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


  reply	other threads:[~2023-07-28 17:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-28 17:09 [PATCH 0/3] Implement DAP "source" request Tom Tromey
2023-07-28 17:09 ` Tom Tromey [this message]
2023-07-28 17:09 ` [PATCH 2/3] Handle Source in DAP breakpointLocations Tom Tromey
2023-07-28 17:09 ` [PATCH 3/3] Implement DAP "source" request Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230728-dap-source-refs-v1-1-1bcc5cdffd44@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).