1 # Copyright
2016-2024 Free Software Foundation
, Inc.
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.
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.
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
/>.
16 # This test ensures that with
"set print object on", -var-create will
17 #
return "<optimized out>" for an optimized out pointer to structure,
18 # rather than attempting to dereference the pointer to determine its
19 # actual type
(instead of its declared type
). We want to test that GDB
20 # can display such a pointer without throwing an error
, while also
21 # ensuring that
any attempt to dereference the pointer
*will
* throw an
26 # This test can only be run
on targets which support DWARF
-2 and use gas.
27 require dwarf2_support
29 standard_testfile .c
-dw.S
31 # Generate a test
program with dwarf information showing the
variable
32 #
'ptr', a pointer
-to
-struct
, as optimized out. The dwarf will also
33 #
describe the structure as have a scalar
, array
, and pointer
-to
-struct
36 proc build_test_program
{} {
37 global testfile srcfile srcfile2
39 # Make some DWARF
for the test.
40 set asm_file
[standard_output_file $srcfile2
]
41 Dwarf
::assemble $asm_file
{
44 # Creating a CU with
4-byte addresses lets this test link
on
45 # both
32- and
64-bit machines.
49 {DW_AT_language @DW_LANG_C99
}
53 declare_labels int_label struct_label pointer_label \
56 int_label
: DW_TAG_base_type
{
57 {DW_AT_byte_size
4 DW_FORM_sdata
}
58 {DW_AT_encoding @DW_ATE_signed
}
62 array_label
: DW_TAG_array_type
{
63 {DW_AT_name foo__array_type
}
64 {DW_AT_type
:$int_label
}
66 DW_TAG_subrange_type
{
67 {DW_AT_type
:$int_label
}
68 {DW_AT_lower_bound
0 DW_FORM_data1
}
69 {DW_AT_upper_bound
127 DW_FORM_data1
}
73 struct_label
: DW_TAG_structure_type
{
75 {DW_AT_byte_size
12 DW_FORM_sdata
}
80 {data_member_location
0 data1
}
85 {data_member_location
4 data1
}
89 {type
:$pointer_label
}
90 {data_member_location
8 data1
}
94 pointer_label
: DW_TAG_pointer_type
{
95 {DW_AT_byte_size
4 DW_FORM_sdata
}
96 {DW_AT_type
:$struct_label
}
101 {DW_AT_type
:$int_label
}
103 {MACRO_AT_func
{func01
}}
107 {DW_AT_type
:$pointer_label
}
108 {DW_AT_location
{} DW_FORM_block1
}
114 {DW_AT_type
:$int_label
}
116 {MACRO_AT_func
{main
}}
123 set sources
"$srcfile $asm_file"
124 if {[build_executable
"failed to compile" $testfile $sources {nodebug}]} {
129 set re_address_class
"@\[^\r\n\]+"
131 # Test access to an optimized
-out pointer
-to
-struct using the
132 # console interpreter.
134 proc do_console_test
{} {
137 clean_restart $binfile
139 with_test_prefix
"console" {
140 gdb_test_no_output
"set print object on"
146 if {![runto func01
]} {
150 gdb_test
"info addr ptr" "Symbol \"ptr\" is optimized out."
152 gdb_test
"print ptr" "<optimized out>"
154 gdb_test
"print *ptr" "value has been optimized out"
156 gdb_test
"print ptr->a" "value has been optimized out"
158 gdb_test
"print ptr->x" "value has been optimized out"
160 gdb_test
"print ptr->y" "value has been optimized out"
164 # Test access to an optimized
-out pointer
-to
-struct using the
169 load_lib mi
-support.exp
175 with_test_prefix
"mi" {
176 if {[mi_clean_restart $binfile
]} {
180 # This causes GDB to dereference a pointer
-to
-structure when doing
182 mi_gdb_test
"-gdb-set print object on" ".*" "set print object on"
184 mi_gdb_test
"-break-insert main" ".*" "insert breakpoint main"
185 mi_gdb_test
"-break-insert func01" ".*" "insert breakpoint func01"
187 # Run to main. Use an explicit expect here since the limited
188 # debug
info will result in output that isn
't handled by the
190 set test "run to main"
193 -re "\\*stopped,reason=\"breakpoint-hit\".*func=\"main\".*$mi_gdb_prompt$" {
197 fail "$test (timeout)"
201 # Run to func01. Use an explicit expect here as above.
202 set test "continue to func01"
203 mi_send_resuming_command "exec-continue" "$test"
205 -re "\\*stopped,reason=\"breakpoint-hit\".*func=\"func01\".*$mi_gdb_prompt$" {
209 fail "$test (timeout)"
213 # Test that -var-create for 'ptr
' is successful.
214 mi_create_varobj "var1" "ptr" "create varobj for ptr"
218 [string_to_regexp "struct foo *"] "( $::re_address_class)?"]
220 # Test that -var-list-children of 'ptr
' is successful.
221 mi_list_varobj_children "var1" \
223 {var1.a a 0 integer} \
224 {var1.x x 128 foo__array_type} \
225 [list "var1.y" "y" "3" $struct_foo_ptr]] \
226 "get children of var1 (ptr)"
228 # Test that dereferencing 'ptr
' will throw an error.
229 mi_gdb_test "-var-create var2 * &((ptr)->a)" \
230 "\\^error,msg=\"value has been optimized out\"" \
231 "throw error, dereference ptr to access integer member "
233 # Test that dereferencing 'ptr
' will throw an error.
234 mi_gdb_test "-var-create var3 * &((ptr)->x)" \
235 "\\^error,msg=\"value has been optimized out\"" \
236 "throw error, dereference ptr to access array member "
238 # Test that dereferencing 'ptr
' will throw an error.
239 mi_gdb_test "-var-create var4 * &((ptr)->y)" \
240 "\\^error,msg=\"value has been optimized out\"" \
241 "throw error, dereference ptr to access pointer member "
245 if { [build_test_program] == -1 } {