public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] Handle gdb.LazyString in DAP
Date: Thu, 05 Oct 2023 07:34:47 -0600	[thread overview]
Message-ID: <20231005-two-dap-fixes-v1-2-729c66007217@adacore.com> (raw)
In-Reply-To: <20231005-two-dap-fixes-v1-0-729c66007217@adacore.com>

Andry pointed out that the DAP code did not properly handle
gdb.LazyString results from a pretty-printer, yielding:

    TypeError: Object of type LazyString is not JSON serializable

This patch fixes the problem, partly with a small patch in varref.py,
but mainly by implementing tp_str for LazyString.
---
 gdb/NEWS                              |  2 +
 gdb/python/lib/gdb/dap/varref.py      |  2 +-
 gdb/python/py-lazy-string.c           | 28 +++++++++++-
 gdb/testsuite/gdb.dap/lazy-string.c   | 23 ++++++++++
 gdb/testsuite/gdb.dap/lazy-string.exp | 80 +++++++++++++++++++++++++++++++++++
 gdb/testsuite/gdb.dap/lazy-string.py  | 37 ++++++++++++++++
 6 files changed, 170 insertions(+), 2 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index fc4c7c01661..3936a221a20 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -340,6 +340,8 @@ show tui mouse-events
      gdb.Progspace that is either being added to GDB, or removed from
      GDB.
 
+  ** gdb.LazyString now implements the __str__ method.
+
 *** Changes in GDB 13
 
 * MI version 1 is deprecated, and will be removed in GDB 14.
diff --git a/gdb/python/lib/gdb/dap/varref.py b/gdb/python/lib/gdb/dap/varref.py
index 764a8291027..95f00f5fe06 100644
--- a/gdb/python/lib/gdb/dap/varref.py
+++ b/gdb/python/lib/gdb/dap/varref.py
@@ -173,7 +173,7 @@ class VariableReference(BaseReference):
 
     def to_object(self):
         result = super().to_object()
-        result[self.result_name] = self.printer.to_string()
+        result[self.result_name] = str(self.printer.to_string())
         num_children = self.child_count()
         if num_children is not None:
             if (
diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index 25a22b78af4..03393b0479e 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -296,6 +296,32 @@ gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
   encoding->reset (lazy->encoding ? xstrdup (lazy->encoding) : NULL);
 }
 
+/* __str__ for LazyString.  */
+
+static PyObject *
+stpy_str (PyObject *self)
+{
+  lazy_string_object *str = (lazy_string_object *) self;
+
+  struct value_print_options opts;
+  get_user_print_options (&opts);
+  opts.addressprint = false;
+
+  string_file stream;
+  try
+    {
+      struct type *type = stpy_lazy_string_elt_type (str);
+      val_print_string (type, str->encoding, str->address, str->length,
+			&stream, &opts);
+    }
+  catch (const gdb_exception &exc)
+    {
+      GDB_PY_HANDLE_EXCEPTION (exc);
+    }
+
+  return host_string_to_python_string (stream.c_str ()).release ();
+}
+
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_lazy_string);
 
 \f
@@ -331,7 +357,7 @@ PyTypeObject lazy_string_object_type = {
   0,				  /*tp_as_mapping*/
   0,				  /*tp_hash */
   0,				  /*tp_call*/
-  0,				  /*tp_str*/
+  stpy_str,			  /*tp_str*/
   0,				  /*tp_getattro*/
   0,				  /*tp_setattro*/
   0,				  /*tp_as_buffer*/
diff --git a/gdb/testsuite/gdb.dap/lazy-string.c b/gdb/testsuite/gdb.dap/lazy-string.c
new file mode 100644
index 00000000000..9df48bf2957
--- /dev/null
+++ b/gdb/testsuite/gdb.dap/lazy-string.c
@@ -0,0 +1,23 @@
+/* Copyright 2023 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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
+main ()
+{
+  const char *the_string = "DEI";
+  return 0;			/* STOP */
+}
diff --git a/gdb/testsuite/gdb.dap/lazy-string.exp b/gdb/testsuite/gdb.dap/lazy-string.exp
new file mode 100644
index 00000000000..10271fc216f
--- /dev/null
+++ b/gdb/testsuite/gdb.dap/lazy-string.exp
@@ -0,0 +1,80 @@
+# 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 printing of gdb.LazyString objects.
+
+require allow_dap_tests
+
+load_lib dap-support.exp
+
+standard_testfile
+
+if {[build_executable ${testfile}.exp $testfile] == -1} {
+    return
+}
+
+set remote_python_file [gdb_remote_download host \
+			    ${srcdir}/${subdir}/${testfile}.py]
+
+save_vars GDBFLAGS {
+    append GDBFLAGS " -iex \"source $remote_python_file\""
+
+    if {[dap_launch $testfile] == ""} {
+	return
+    }
+}
+
+set line [gdb_get_line_number "STOP"]
+set obj [dap_check_request_and_response "set breakpoint by line number" \
+	     setBreakpoints \
+	     [format {o source [o path [%s]] breakpoints [a [o line [i %d]]]} \
+		  [list s $srcfile] $line]]
+set line_bpno [dap_get_breakpoint_number $obj]
+
+dap_check_request_and_response "start inferior" configurationDone
+
+dap_wait_for_event_and_check "stopped at line breakpoint" stopped \
+    "body reason" breakpoint \
+    "body hitBreakpointIds" $line_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] 0] id]
+
+set scopes [dap_check_request_and_response "get scopes" scopes \
+		[format {o frameId [i %d]} $frame_id]]
+set scopes [dict get [lindex $scopes 0] body scopes]
+
+lassign $scopes scope reg_scope
+gdb_assert {[dict get $scope name] == "Locals"} "scope is locals"
+gdb_assert {[dict get $scope namedVariables] == 1} "one var in scope"
+
+set num [dict get $scope variablesReference]
+set refs [lindex [dap_check_request_and_response "fetch variable" \
+		      "variables" \
+		      [format {o variablesReference [i %d] count [i 1]} \
+			   $num]] \
+	      0]
+
+foreach var [dict get $refs body variables] {
+    gdb_assert {[dict get $var name] == "the_string"} "variable name"
+    # The result looks strange here, but only because TON does not
+    # handle the backslash-quote sequence properly when decoding the
+    # JSON.  The actual JSON is: "value": "\"DEI\"".
+    gdb_assert {[dict get $var value] == "\\\"DEI\\\""} "variable value"
+}
+
+dap_shutdown
diff --git a/gdb/testsuite/gdb.dap/lazy-string.py b/gdb/testsuite/gdb.dap/lazy-string.py
new file mode 100644
index 00000000000..a15539e6b5c
--- /dev/null
+++ b/gdb/testsuite/gdb.dap/lazy-string.py
@@ -0,0 +1,37 @@
+# Copyright (C) 2022-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
+
+
+class Printer(gdb.ValuePrinter):
+    """Pretty print a string"""
+
+    def __init__(self, val):
+        self._val = val
+
+    def to_string(self):
+        return self._val.lazy_string()
+
+
+def lookup_function(val):
+    typ = val.type
+    if typ.code == gdb.TYPE_CODE_PTR:
+        return Printer(val)
+    return None
+
+
+gdb.pretty_printers.append(lookup_function)

-- 
2.40.1


  parent reply	other threads:[~2023-10-05 13:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-05 13:34 [PATCH 0/2] Two more small DAP / Python fixes Tom Tromey
2023-10-05 13:34 ` [PATCH 1/2] Fix register-setting response from DAP Tom Tromey
2023-10-05 13:34 ` Tom Tromey [this message]
2023-10-05 16:14   ` [PATCH 2/2] Handle gdb.LazyString in DAP Eli Zaretskii
2023-10-16 15:19 ` [PATCH 0/2] Two more small DAP / Python fixes 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=20231005-two-dap-fixes-v1-2-729c66007217@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).