[3/3] Implement DAP modules request

Message ID 20230711-dap-modules-v1-3-b4f6067c1b2e@adacore.com
State New
Headers
Series Implement the DAP "modules" request |

Commit Message

Tom Tromey July 11, 2023, 2:48 p.m. UTC
  This implements the DAP "modules" request, and also arranges to add
the module ID to stack frames.
---
 gdb/data-directory/Makefile.in        |  1 +
 gdb/python/lib/gdb/dap/__init__.py    |  1 +
 gdb/python/lib/gdb/dap/bt.py          |  7 ++-
 gdb/python/lib/gdb/dap/events.py      | 14 ++++++
 gdb/python/lib/gdb/dap/modules.py     | 64 +++++++++++++++++++++++++++
 gdb/testsuite/gdb.dap/modules-solib.c | 21 +++++++++
 gdb/testsuite/gdb.dap/modules.c       | 40 +++++++++++++++++
 gdb/testsuite/gdb.dap/modules.exp     | 81 +++++++++++++++++++++++++++++++++++
 8 files changed, 228 insertions(+), 1 deletion(-)
  

Patch

diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
index a3775a4a666..04a8c8eca69 100644
--- a/gdb/data-directory/Makefile.in
+++ b/gdb/data-directory/Makefile.in
@@ -98,6 +98,7 @@  PYTHON_FILE_LIST = \
 	gdb/dap/launch.py \
 	gdb/dap/locations.py \
 	gdb/dap/memory.py \
+	gdb/dap/modules.py \
 	gdb/dap/next.py \
 	gdb/dap/pause.py \
 	gdb/dap/scopes.py \
diff --git a/gdb/python/lib/gdb/dap/__init__.py b/gdb/python/lib/gdb/dap/__init__.py
index f3dd3ff7ea8..689c2049eec 100644
--- a/gdb/python/lib/gdb/dap/__init__.py
+++ b/gdb/python/lib/gdb/dap/__init__.py
@@ -27,6 +27,7 @@  from . import evaluate
 from . import launch
 from . import locations
 from . import memory
+from . import modules
 from . import next
 from . import pause
 from . import scopes
diff --git a/gdb/python/lib/gdb/dap/bt.py b/gdb/python/lib/gdb/dap/bt.py
index d1f82817c5e..975c88f8208 100644
--- a/gdb/python/lib/gdb/dap/bt.py
+++ b/gdb/python/lib/gdb/dap/bt.py
@@ -18,6 +18,7 @@  import os
 
 from gdb.frames import frame_iterator
 from .frames import frame_id
+from .modules import module_id
 from .server import request, capability
 from .startup import send_gdb_with_response, in_gdb_thread
 from .state import set_thread
@@ -39,6 +40,7 @@  def _backtrace(thread_id, levels, startFrame):
     except gdb.error:
         frame_iter = ()
     for current_frame in frame_iter:
+        pc = current_frame.address()
         newframe = {
             "id": frame_id(current_frame),
             "name": current_frame.function(),
@@ -47,8 +49,11 @@  def _backtrace(thread_id, levels, startFrame):
             "line": 0,
             # GDB doesn't support columns.
             "column": 0,
-            "instructionPointerReference": hex(current_frame.address()),
+            "instructionPointerReference": hex(pc),
         }
+        objfile = gdb.current_progspace().objfile_for_address(pc)
+        if objfile is not None:
+            newframe["moduleId"] = module_id(objfile)
         line = current_frame.line()
         if line is not None:
             newframe["line"] = line
diff --git a/gdb/python/lib/gdb/dap/events.py b/gdb/python/lib/gdb/dap/events.py
index d9ae603dfa4..c1631442746 100644
--- a/gdb/python/lib/gdb/dap/events.py
+++ b/gdb/python/lib/gdb/dap/events.py
@@ -19,6 +19,7 @@  import gdb
 from .server import send_event
 from .startup import in_gdb_thread, Invoker, log
 from .breakpoint import breakpoint_descriptor
+from .modules import is_module, make_module
 
 
 @in_gdb_thread
@@ -78,6 +79,18 @@  def _new_thread(event):
     )
 
 
+@in_gdb_thread
+def _new_objfile(event):
+    if is_module(event.new_objfile):
+        send_event(
+            "module",
+            {
+                "reason": "new",
+                "module": make_module(event.new_objfile),
+            },
+        )
+
+
 _suppress_cont = False
 
 
@@ -161,3 +174,4 @@  gdb.events.breakpoint_modified.connect(_bp_modified)
 gdb.events.breakpoint_deleted.connect(_bp_deleted)
 gdb.events.new_thread.connect(_new_thread)
 gdb.events.cont.connect(_cont)
+gdb.events.new_objfile.connect(_new_objfile)
diff --git a/gdb/python/lib/gdb/dap/modules.py b/gdb/python/lib/gdb/dap/modules.py
new file mode 100644
index 00000000000..6ba613bcd2b
--- /dev/null
+++ b/gdb/python/lib/gdb/dap/modules.py
@@ -0,0 +1,64 @@ 
+# 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 <http://www.gnu.org/licenses/>.
+
+import gdb
+
+from .server import capability, request
+from .startup import in_gdb_thread, send_gdb_with_response
+
+
+@in_gdb_thread
+def module_id(objfile):
+    """Return the module ID for the objfile."""
+    return objfile.username
+
+
+@in_gdb_thread
+def is_module(objfile):
+    """Return True if OBJFILE represents a valid Module."""
+    return objfile.is_valid() and objfile.owner is None
+
+
+@in_gdb_thread
+def make_module(objf):
+    """Return a Module representing the objfile OBJF.
+
+    The objfile must pass the 'is_module' test."""
+    return {
+        "id": module_id(objf),
+        "name": objf.username,
+        "path": objf.filename,
+    }
+
+
+@in_gdb_thread
+def _modules(start, count):
+    # Don't count invalid objfiles or separate debug objfiles.
+    objfiles = [x for x in gdb.objfiles() if is_module(x)]
+    if count == 0:
+        # Use all items.
+        last = len(objfiles)
+    else:
+        last = start + count
+    return {
+        "modules": [make_module(x) for x in objfiles[start:last]],
+        "totalModules": len(objfiles),
+    }
+
+
+@capability("supportsModulesRequest")
+@request("modules")
+def modules(*, startModule: int = 0, moduleCount: int = 0, **args):
+    return send_gdb_with_response(lambda: _modules(startModule, moduleCount))
diff --git a/gdb/testsuite/gdb.dap/modules-solib.c b/gdb/testsuite/gdb.dap/modules-solib.c
new file mode 100644
index 00000000000..a0bfcaa83d0
--- /dev/null
+++ b/gdb/testsuite/gdb.dap/modules-solib.c
@@ -0,0 +1,21 @@ 
+/* 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 <http://www.gnu.org/licenses/>.  */
+
+int
+call_me (void (*callee) (void))
+{
+  callee ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.dap/modules.c b/gdb/testsuite/gdb.dap/modules.c
new file mode 100644
index 00000000000..6ef8a600c5d
--- /dev/null
+++ b/gdb/testsuite/gdb.dap/modules.c
@@ -0,0 +1,40 @@ 
+/* 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 <http://www.gnu.org/licenses/>.  */
+
+#include <dlfcn.h>
+#include <assert.h>
+#include <stddef.h>
+
+void
+stop (void)
+{
+}
+
+int
+main (void)
+{
+  void *handle;
+  int (*func)(void (*) (void));
+
+  stop ();
+
+  handle = dlopen (SHLIB_NAME, RTLD_LAZY);
+  assert (handle != NULL);
+
+  func = (int (*)(void (*) (void))) dlsym (handle, "call_me");
+  func (stop);
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.dap/modules.exp b/gdb/testsuite/gdb.dap/modules.exp
new file mode 100644
index 00000000000..e75ae578c78
--- /dev/null
+++ b/gdb/testsuite/gdb.dap/modules.exp
@@ -0,0 +1,81 @@ 
+# 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 <http://www.gnu.org/licenses/>.
+
+# Test DAP modules functionality.
+
+require allow_shlib_tests allow_dap_tests
+
+load_lib dap-support.exp
+
+standard_testfile
+
+set libname $testfile-solib
+set srcfile_lib $srcdir/$subdir/$libname.c
+set binfile_lib [standard_output_file $libname.so]
+
+if { [gdb_compile_shlib $srcfile_lib $binfile_lib {}] != "" } {
+    untested "failed to compile shared library"
+    return
+}
+
+set binfile_lib_target [gdb_download_shlib $binfile_lib]
+set define -DSHLIB_NAME=\"$binfile_lib_target\"
+
+if {[build_executable ${testfile}.exp $testfile $srcfile \
+	 [list additional_flags=$define debug shlib_load]] == -1} {
+    return
+}
+
+if {[dap_launch $testfile] == ""} {
+    return
+}
+
+set obj [dap_check_request_and_response "set breakpoint on two functions" \
+	     setFunctionBreakpoints \
+	     {o breakpoints [a [o name [s stop]]]}]
+set fn_bpno [dap_get_breakpoint_number $obj]
+
+dap_check_request_and_response "start inferior" configurationDone
+
+dap_wait_for_event_and_check "stopped at function breakpoint" stopped \
+    "body reason" breakpoint \
+    "body hitBreakpointIds" $fn_bpno
+
+dap_check_request_and_response "continue to next stop" continue \
+    {o threadId [i 1]}
+
+
+lassign [dap_wait_for_event_and_check "module event" module \
+	     "body reason" new] module_event ignore
+
+gdb_assert {[string match *$libname* [dict get $module_event body module id]]} \
+    "module.id"
+gdb_assert {[string match *$libname* [dict get $module_event body module name]]} \
+    "module.name"
+gdb_assert {[string match *$libname* [dict get $module_event body module path]]} \
+    "module.path"
+
+dap_wait_for_event_and_check "second stop at function 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] 1] moduleId]
+
+gdb_assert {[string match *$libname* $frame_id]} "module.id in stack trace"
+
+dap_shutdown