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>
This commit is contained in:
Tom Tromey
2023-10-04 11:22:05 -06:00
parent 138c7d2661
commit ee81567c7c
6 changed files with 170 additions and 2 deletions

View File

@@ -358,6 +358,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.

View File

@@ -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 (

View File

@@ -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*/

View File

@@ -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 */
}

View File

@@ -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

View File

@@ -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)