Handle Source in DAP breakpointLocations
authorTom Tromey <tromey@adacore.com>
Thu, 27 Jul 2023 20:21:13 +0000 (14:21 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 1 Aug 2023 19:12:24 +0000 (13:12 -0600)
This changes the DAP breakpointLocations request to accept a Source
and to decode it properly.

gdb/python/lib/gdb/dap/locations.py
gdb/python/lib/gdb/dap/sources.py

index 594f1bad45bbd196d32d56de69091f4c0aeb6189..a299e8da9594f612b316dc2729f2dafb4e015529 100644 (file)
@@ -19,11 +19,13 @@ import gdb
 from typing import Optional
 
 from .server import capability, request
+from .sources import decode_source
 from .startup import in_gdb_thread, send_gdb_with_response
 
 
 @in_gdb_thread
-def _find_lines(filename, start_line, end_line):
+def _find_lines(source, start_line, end_line):
+    filename = decode_source(source)
     lines = set()
     for entry in gdb.execute_mi("-symbol-list-lines", filename)["lines"]:
         line = entry["line"]
@@ -44,10 +46,4 @@ def _find_lines(filename, start_line, end_line):
 def breakpoint_locations(*, source, line: int, endLine: Optional[int] = None, **extra):
     if endLine is None:
         endLine = line
-    if "path" in source:
-        filename = source["path"]
-    elif "name" in source:
-        filename = source["name"]
-    else:
-        raise Exception("")
-    return send_gdb_with_response(lambda: _find_lines(filename, line, endLine))
+    return send_gdb_with_response(lambda: _find_lines(source, line, endLine))
index 50b5909ed1d2b362b0c29d4e9ab02f671e12b73a..806352836dbf7dd0b9e65b3abe356ab7415c4161 100644 (file)
@@ -54,6 +54,22 @@ def make_source(fullname, filename):
     return result
 
 
+@in_gdb_thread
+def decode_source(source):
+    """Decode a Source object.
+
+    Finds and returns the filename of a given Source object."""
+    if "path" in source:
+        return source["path"]
+    if "sourceReference" not in source:
+        raise Exception("either 'path' or 'sourceReference' must appear in Source")
+    ref = source["sourceReference"]
+    global _id_map
+    if ref not in _id_map:
+        raise Exception("no sourceReference " + str(ref))
+    return _id_map[ref]["path"]
+
+
 @in_gdb_thread
 def _sources():
     result = []