public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Implement repl evaluation for DAP
Date: Mon,  6 Mar 2023 09:00:05 -0700	[thread overview]
Message-ID: <20230306160005.1301114-1-tromey@adacore.com> (raw)

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  | 24 +++++++++++++++++++++---
 gdb/testsuite/gdb.dap/basic-dap.exp |  5 +++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/gdb/python/lib/gdb/dap/evaluate.py b/gdb/python/lib/gdb/dap/evaluate.py
index d04ac169a8e..55d41b0806d 100644
--- a/gdb/python/lib/gdb/dap/evaluate.py
+++ b/gdb/python/lib/gdb/dap/evaluate.py
@@ -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
diff --git a/gdb/testsuite/gdb.dap/basic-dap.exp b/gdb/testsuite/gdb.dap/basic-dap.exp
index 6f14df5df01..5e6e1b5d6d4 100644
--- a/gdb/testsuite/gdb.dap/basic-dap.exp
+++ b/gdb/testsuite/gdb.dap/basic-dap.exp
@@ -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
-- 
2.39.1


             reply	other threads:[~2023-03-06 16:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-06 16:00 Tom Tromey [this message]
2023-03-24 16:01 ` 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=20230306160005.1301114-1-tromey@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).