]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Implement repl evaluation for DAP
authorTom Tromey <tromey@adacore.com>
Thu, 16 Feb 2023 14:49:33 +0000 (07:49 -0700)
committerTom Tromey <tromey@adacore.com>
Fri, 24 Mar 2023 16:05:51 +0000 (10:05 -0600)
The evaluate command supports a "context" parameter which tells the
adapter the context in which an evaluation occurs.  One of the
supported values is "repl", which we took to mean evaluation of a gdb
command.  That is what this patch implements.

Note that some gdb commands probably will not work correctly with the
rest of the protocol.  For example if the user types "continue",
confusion may result.

This patch requires the earlier patch to fix up scopes in DAP.

gdb/python/lib/gdb/dap/evaluate.py
gdb/testsuite/gdb.dap/basic-dap.exp

index d04ac169a8ebc3fad0aaaab96832dd5ddfae7e4d..55d41b0806d11ec9d67a53987de3f0501c489b43 100644 (file)
@@ -38,12 +38,30 @@ def _evaluate(expr, frame_id):
     return ref.to_object()
 
 
+# Helper function to evaluate a gdb command in a certain frame.
+@in_gdb_thread
+def _repl(command, frame_id):
+    if frame_id is not None:
+        frame = frame_for_id(frame_id)
+        frame.select()
+    val = gdb.execute(command, from_tty=True, to_string=True)
+    return {
+        "result": val,
+        "variablesReference": 0,
+    }
+
+
 # FIXME 'format' & hex
 # FIXME supportsVariableType handling
-# FIXME "repl"
 @request("evaluate")
-def eval_request(*, expression, frameId=None, **args):
-    return send_gdb_with_response(lambda: _evaluate(expression, frameId))
+def eval_request(*, expression, frameId=None, context="variables", **args):
+    if context in ("watch", "variables"):
+        # These seem to be expression-like.
+        return send_gdb_with_response(lambda: _evaluate(expression, frameId))
+    elif context == "repl":
+        return send_gdb_with_response(lambda: _repl(expression, frameId))
+    else:
+        raise Exception(f'unknown evaluate context "{context}"')
 
 
 @in_gdb_thread
index 6f14df5df01774ad9bc901cc54d6695e8f0e61a7..5e6e1b5d6d4d1b61d47da59ef70cc294a6dd3c32 100644 (file)
@@ -163,4 +163,9 @@ set obj [dap_check_request_and_response "disassemble one instruction" \
 set response [lindex $obj 0]
 gdb_assert { [dict exists $response body instructions] } "instructions in disassemble output"
 
+set obj [dap_check_request_and_response "command repl" \
+            evaluate {o expression [s {output 23}] context [s repl]}]
+set response [lindex $obj 0]
+gdb_assert {[dict get $response body result] == 23}
+
 dap_shutdown