aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/make-visualizer.exp
blob: 5794bbda8a2d7e80062f1f4cf926e1a42bdfbf66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (C) 2025 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/>.

# Tests for gdb.printing.make_visualizer; specifically that the
# optimized-out and synthetic pointer cases work properly.

load_lib dwarf.exp
load_lib gdb-python.exp

require dwarf2_support
require allow_python_tests

# Use a simple plain-"main" source file.
standard_testfile py-progspace.c -dw.S

if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug}]} {
    return
}

if {![runto_main]} {
    return
}

# Get size to create fake DWARF for the test.
set ptr_size [get_sizeof "void *" 96]

set asm_file [standard_output_file ${srcfile2}]
Dwarf::assemble $asm_file {
    cu {} {
	compile_unit {} {
	    declare_labels i32_type i32_array \
		struct_label variable_label pointer_label

	    i32_type: DW_TAG_base_type {
		DW_AT_name "int32_t"
		DW_AT_encoding @DW_ATE_signed
		DW_AT_byte_size 4 DW_FORM_sdata
	    }

	    i32_array: DW_TAG_array_type {
		DW_AT_name array_type
		DW_AT_type :$i32_type
	    } {
		DW_TAG_subrange_type {
		    DW_AT_type :$i32_type
		    DW_AT_lower_bound 0 DW_FORM_data1
		    DW_AT_upper_bound 3 DW_FORM_data1
		}
	    }

	    # Used for testing optimized-out elements of an array.
	    DW_TAG_variable {
		DW_AT_name i32_noptr
		DW_AT_type :$i32_array
		DW_AT_location {
		    DW_OP_constu 1779823878
		    DW_OP_stack_value
		    DW_OP_piece 4
		} SPECIAL_expr
	    }

	    struct_label: DW_TAG_structure_type {
		DW_AT_name i32_noptr
		DW_AT_byte_size 4 DW_FORM_sdata
	    } {
		DW_TAG_member {
		    DW_AT_name f
		    DW_AT_type :$i32_type
		    DW_AT_data_member_location 0 DW_FORM_data1
		}
	    }

	    pointer_label: DW_TAG_pointer_type {
		DW_AT_byte_size $::ptr_size DW_FORM_sdata
		DW_AT_type :$struct_label
	    }

	    variable_label: DW_TAG_variable {
		DW_AT_name v
		DW_AT_location {
		    DW_OP_implicit_value 0x1 0x1 0x1 0x1
		} SPECIAL_expr
		DW_AT_type :$struct_label
	    }

	    # Used for testing synthetic pointers.
	    DW_TAG_variable {
		DW_AT_name synthptr
		DW_AT_location [subst {
		    GNU_implicit_pointer $variable_label 0
		}] SPECIAL_expr
		DW_AT_type :$pointer_label
	    }

	    # Optimized-out pointer.
	    DW_TAG_variable {
		DW_AT_name optoutptr
		DW_AT_location { } SPECIAL_expr
		DW_AT_type :$pointer_label
	    }
	}
    }
}

if {[prepare_for_testing "failed to prepare" ${testfile} \
	 [list $srcfile $asm_file] {nodebug}]} {
    return
}

# Need a frame to evaluate a synthetic pointer.
if {![runto_main]} {
    return
}

gdb_test_no_output "python import gdb"
gdb_test_no_output "python import gdb.printing"

gdb_test_no_output "python val = gdb.parse_and_eval('i32_noptr')" \
    "fetch i32_noptr"
gdb_test_no_output "python vz = gdb.printing.make_visualizer(val)" \
    "create i32_noptr visualizer"
gdb_test "python print(isinstance(vz, gdb.printing.NoOpArrayPrinter))" \
    True \
    "i32_noptr uses array printer"

gdb_test_no_output "python vz1 = gdb.printing.make_visualizer(val\[0\])" \
    "create visualizer for valid element"
gdb_test "python print(isinstance(vz1, gdb.printing.NoOpScalarPrinter))" \
    True \
    "valid element uses scalar printer"
gdb_test "python print(vz1.to_string())" \
    1779823878 \
    "string form of valid element"

gdb_test_no_output "python vz2 = gdb.printing.make_visualizer(val\[1\])" \
    "create visualizer for optimized-out element"
gdb_test "python print(isinstance(vz2, gdb.printing.NoOpScalarPrinter))" \
    True \
    "optimized-out element uses scalar printer"
gdb_test "python print(vz2.to_string())" \
    "<optimized out>" \
    "string form of optimized-out element"

gdb_test_no_output "python val2 = gdb.parse_and_eval('synthptr')" \
    "fetch synthetic pointer"
gdb_test_no_output "python vzv2 = gdb.printing.make_visualizer(val2)" \
    "create synthetic pointer visualizer"
gdb_test "python print(isinstance(vzv2, gdb.printing.NoOpPointerReferencePrinter))" \
    True \
    "synthetic pointer uses pointer printer"
gdb_test "python print(vzv2.child(0)\[1\])" \
    "{f = 16843009}" \
    "child of synthetic pointer"

gdb_test_no_output "python val3 = gdb.parse_and_eval('optoutptr')" \
    "fetch optimized-out pointer"
gdb_test_no_output "python vzv3 = gdb.printing.make_visualizer(val3)" \
    "create optimized-out pointer visualizer"
gdb_test "python print(isinstance(vzv3, gdb.printing.NoOpScalarPrinter))" \
    True \
    "optimized-out pointer uses scalar printer"
gdb_test "python print(vzv3.to_string())" \
    "<optimized out>" \
    "string representation of optimized-out pointer"