Respect supportsMemoryReferences in DAP
authorTom Tromey <tromey@adacore.com>
Thu, 27 Jul 2023 19:06:38 +0000 (13:06 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 1 Aug 2023 19:00:57 +0000 (13:00 -0600)
I noticed that the support for memoryReference in the "variables"
output is gated on the client "supportsMemoryReferences" capability.

This patch implements this and makes some other changes to the DAP
memory reference code:

* Remove the memoryReference special case from _SetResult.
  Upstream DAP fixed this oversight in response to
  https://github.com/microsoft/debug-adapter-protocol/issues/414

* Don't use the address of a variable as its memoryReference -- only
  emit this for pointer types.  There's no spec support for the
  previous approach.

* Use strip_typedefs to handle typedefs of pointers.

gdb/python/lib/gdb/dap/evaluate.py
gdb/python/lib/gdb/dap/varref.py
gdb/testsuite/gdb.dap/memory.c
gdb/testsuite/gdb.dap/memory.exp
gdb/testsuite/lib/dap-support.exp

index 63e80331b246604f6303a180881a5aad87f7c0c0..9fa94e08121fb4c99186ee883a1674b9975d7238 100644 (file)
@@ -55,13 +55,6 @@ class _SetResult(VariableReference):
     def __init__(self, value):
         super().__init__(None, value, "value")
 
-    def to_object(self):
-        result = super().to_object()
-        # This is not specified in the setExpression result.
-        if "memoryReference" in result:
-            del result["memoryReference"]
-        return result
-
 
 # Helper function to perform an assignment.
 @in_gdb_thread
index 213151fd3d34164b574379b8ce819248b86b9edb..0e0d92a883205b1686c16ef548eb43131f5115aa 100644 (file)
@@ -162,10 +162,13 @@ class VariableReference(BaseReference):
                 result["indexedVariables"] = num_children
             else:
                 result["namedVariables"] = num_children
-        if self.value.type.code == gdb.TYPE_CODE_PTR:
-            result["memoryReference"] = hex(int(self.value))
-        elif self.value.address is not None:
-            result["memoryReference"] = hex(int(self.value.address))
+        if client_bool_capability("supportsMemoryReferences"):
+            # https://github.com/microsoft/debug-adapter-protocol/issues/414
+            # changed DAP to allow memory references for any of the
+            # variable response requests, and to lift the restriction
+            # to pointer-to-function from Variable.
+            if self.value.type.strip_typedefs().code == gdb.TYPE_CODE_PTR:
+                result["memoryReference"] = hex(int(self.value))
         if client_bool_capability("supportsVariableType"):
             result["type"] = str(self.value.type)
         return result
index 3b9f6138abee6fd34f436fb75b43decf0739baa9..630e23dcf0190d6bef43b2e20517d427a1cf8a36 100644 (file)
@@ -19,6 +19,8 @@
 
 uint32_t thirty_two = 7;
 
+uint32_t *thirty_two_p = &thirty_two;
+
 int main ()
 {
   return 0;                    /* BREAK */
index ab0516d6b3d5373248f188b211e7a3fdfae5a80c..d702d5b5deeffbc6fe731f6819b5572ff07d9aac 100644 (file)
@@ -47,6 +47,8 @@ set obj [dap_check_request_and_response "evaluate global" \
             evaluate {o expression [s thirty_two]}]
 dap_match_values "global value" [lindex $obj 0] "body result" 7
 
+set obj [dap_check_request_and_response "evaluate global pointer" \
+            evaluate {o expression [s thirty_two_p]}]
 set addr [dict get [lindex $obj 0] body memoryReference]
 
 set obj [dap_check_request_and_response "read memory" \
index 657ad7b29bcd48d66db3d355bfc176d70cb62813..2a0cb886528f027ae7ba21a1ed03e549d7d84f56 100644 (file)
@@ -233,7 +233,8 @@ proc _dap_initialize {name} {
     return [dap_check_request_and_response $name initialize \
                {o clientID [s "gdb testsuite"] \
                     supportsVariableType [l true] \
-                    supportsVariablePaging [l true]}]
+                    supportsVariablePaging [l true] \
+                    supportsMemoryReferences [l true]}]
 }
 
 # Start gdb, send a DAP initialize request, and then a launch request