Skip to content

Commit 41ab08f

Browse files
committed
Have DAP handle non-Value results from 'children'
A pretty-printer's 'children' method may return values other than a gdb.Value -- it may return any value that can be converted to a gdb.Value. I noticed that this case did not work for DAP. This patch fixes the problem.
1 parent ee81567 commit 41ab08f

File tree

3 files changed

+166
-2
lines changed

3 files changed

+166
-2
lines changed

gdb/python/lib/gdb/dap/varref.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,14 @@ def fetch_one_child(self, idx):
199199
if isinstance(self.printer, gdb.ValuePrinter) and hasattr(
200200
self.printer, "child"
201201
):
202-
return self.printer.child(idx)
202+
(name, val) = self.printer.child(idx)
203203
else:
204-
return self.cache_children()[idx]
204+
(name, val) = self.cache_children()[idx]
205+
# A pretty-printer can return something other than a
206+
# gdb.Value, but it must be convertible.
207+
if not isinstance(val, gdb.Value):
208+
val = gdb.Value(val)
209+
return (name, val)
205210

206211

207212
@in_gdb_thread

gdb/testsuite/gdb.dap/children.exp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Copyright 2023 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# Test the handling of non-Value responses from 'children' method.
17+
18+
require allow_dap_tests
19+
20+
load_lib dap-support.exp
21+
22+
standard_testfile lazy-string.c
23+
24+
if {[build_executable ${testfile}.exp $testfile $srcfile] == -1} {
25+
return
26+
}
27+
28+
set remote_python_file [gdb_remote_download host \
29+
${srcdir}/${subdir}/${testfile}.py]
30+
31+
save_vars GDBFLAGS {
32+
append GDBFLAGS " -iex \"source $remote_python_file\""
33+
34+
if {[dap_launch $testfile] == ""} {
35+
return
36+
}
37+
}
38+
39+
set line [gdb_get_line_number "STOP"]
40+
set obj [dap_check_request_and_response "set breakpoint by line number" \
41+
setBreakpoints \
42+
[format {o source [o path [%s]] breakpoints [a [o line [i %d]]]} \
43+
[list s $srcfile] $line]]
44+
set line_bpno [dap_get_breakpoint_number $obj]
45+
46+
dap_check_request_and_response "start inferior" configurationDone
47+
48+
dap_wait_for_event_and_check "stopped at line breakpoint" stopped \
49+
"body reason" breakpoint \
50+
"body hitBreakpointIds" $line_bpno
51+
52+
set bt [lindex [dap_check_request_and_response "backtrace" stackTrace \
53+
{o threadId [i 1]}] \
54+
0]
55+
set frame_id [dict get [lindex [dict get $bt body stackFrames] 0] id]
56+
57+
set scopes [dap_check_request_and_response "get scopes" scopes \
58+
[format {o frameId [i %d]} $frame_id]]
59+
set scopes [dict get [lindex $scopes 0] body scopes]
60+
61+
lassign $scopes scope reg_scope
62+
gdb_assert {[dict get $scope name] == "Locals"} "scope is locals"
63+
gdb_assert {[dict get $scope namedVariables] == 1} "one var in scope"
64+
65+
set num [dict get $scope variablesReference]
66+
set refs [lindex [dap_check_request_and_response "fetch variable" \
67+
"variables" \
68+
[format {o variablesReference [i %d] count [i 1]} \
69+
$num]] \
70+
0]
71+
72+
set vars [dict get $refs body variables]
73+
gdb_assert {[llength $vars] == 1} "just one variable"
74+
75+
set var [lindex $vars 0]
76+
gdb_assert {[dict get $var name] == "the_string"} "variable name"
77+
gdb_assert {[dict get $var value] == "contents"} "variable value"
78+
gdb_assert {[dict get $var namedVariables] == 2} "variable has two children"
79+
80+
set num [dict get $var variablesReference]
81+
set refs [lindex [dap_check_request_and_response "fetch children of variable" \
82+
"variables" \
83+
[format {o variablesReference [i %d] count [i 2]} \
84+
$num]] \
85+
0]
86+
87+
set vars [dict get $refs body variables]
88+
gdb_assert {[llength $vars] == 2} "got two children of variable"
89+
90+
set saw_first 0
91+
set saw_second 0
92+
foreach var [dict get $refs body variables] {
93+
set name [dict get $var name]
94+
switch $name {
95+
"first" {
96+
gdb_assert {[dict get $var value] == 23} "value of first"
97+
incr saw_first
98+
}
99+
100+
"second" {
101+
# The result looks strange here, but only because TON does
102+
# not handle the backslash-quote sequence properly when
103+
# decoding the JSON. The actual JSON is: "value":
104+
# "\"DEI\"".
105+
gdb_assert {[dict get $var value] == "\\\"DEI\\\""} \
106+
"value of second"
107+
incr saw_second
108+
}
109+
110+
default {
111+
fail "unknown variable $name"
112+
}
113+
}
114+
}
115+
116+
gdb_assert {$saw_first == 1 && $saw_second == 1} "saw both children"
117+
118+
dap_shutdown

gdb/testsuite/gdb.dap/children.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (C) 2023 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
17+
import gdb
18+
19+
20+
class Printer(gdb.ValuePrinter):
21+
"""Pretty print test class."""
22+
23+
def __init__(self, val):
24+
self._val = val
25+
26+
def to_string(self):
27+
return "contents"
28+
29+
def children(self):
30+
yield "first", 23
31+
yield "second", "DEI"
32+
33+
34+
def lookup_function(val):
35+
typ = val.type
36+
if typ.code == gdb.TYPE_CODE_PTR:
37+
return Printer(val)
38+
return None
39+
40+
41+
gdb.pretty_printers.append(lookup_function)

0 commit comments

Comments
 (0)