diff options
author | Tom Tromey <tromey@adacore.com> | 2023-10-04 11:22:05 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-10-16 09:28:22 -0600 |
commit | ba0725dfa025420a36c00f8847780ff5730bf0af (patch) | |
tree | 1249835c8afcfad3748ea77e0a08e3280c352988 | |
parent | 8836926927c8f712131ec2d3c03bdf885dbe33d1 (diff) | |
download | binutils-ba0725dfa025420a36c00f8847780ff5730bf0af.zip binutils-ba0725dfa025420a36c00f8847780ff5730bf0af.tar.gz binutils-ba0725dfa025420a36c00f8847780ff5730bf0af.tar.bz2 |
Handle gdb.LazyString in DAP
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.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
-rw-r--r-- | gdb/NEWS | 2 | ||||
-rw-r--r-- | gdb/python/lib/gdb/dap/varref.py | 2 | ||||
-rw-r--r-- | gdb/python/py-lazy-string.c | 28 | ||||
-rw-r--r-- | gdb/testsuite/gdb.dap/lazy-string.c | 23 | ||||
-rw-r--r-- | gdb/testsuite/gdb.dap/lazy-string.exp | 80 | ||||
-rw-r--r-- | gdb/testsuite/gdb.dap/lazy-string.py | 37 |
6 files changed, 170 insertions, 2 deletions
@@ -348,6 +348,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 764a829..95f00f5 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 25a22b7..03393b0 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); @@ -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 0000000..9df48bf --- /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 0000000..10271fc --- /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 0000000..a15539e --- /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) |