GDB: trad-frame: Store length of value_bytes in trad_frame_saved_reg
[binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / dw2-opt-structptr.exp
blobd73f4a892ba1b91d252ff24a50cd4dab1b45d16a
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
22 # error.
24 load_lib dwarf.exp
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
34 # members.
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 {
42 global srcfile
44 # Creating a CU with 4-byte addresses lets this test link on
45 # both 32- and 64-bit machines.
46 cu { addr_size 4 } {
48 DW_TAG_compile_unit {
49 {DW_AT_language @DW_LANG_C99}
50 {DW_AT_name $srcfile}
51 {DW_AT_comp_dir /tmp}
52 } {
53 declare_labels int_label struct_label pointer_label \
54 array_label
56 int_label: DW_TAG_base_type {
57 {DW_AT_byte_size 4 DW_FORM_sdata}
58 {DW_AT_encoding @DW_ATE_signed}
59 {DW_AT_name integer}
62 array_label: DW_TAG_array_type {
63 {DW_AT_name foo__array_type}
64 {DW_AT_type :$int_label}
65 } {
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 {
74 {DW_AT_name "foo"}
75 {DW_AT_byte_size 12 DW_FORM_sdata}
76 } {
77 member {
78 {name a}
79 {type :$int_label}
80 {data_member_location 0 data1}
82 member {
83 {name x}
84 {type :$array_label}
85 {data_member_location 4 data1}
87 member {
88 {name y}
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}
99 DW_TAG_subprogram {
100 {DW_AT_name func01}
101 {DW_AT_type :$int_label}
102 {external 1 flag}
103 {MACRO_AT_func {func01}}
105 DW_TAG_variable {
106 {DW_AT_name ptr}
107 {DW_AT_type :$pointer_label}
108 {DW_AT_location {} DW_FORM_block1}
112 DW_TAG_subprogram {
113 {DW_AT_name main}
114 {DW_AT_type :$int_label}
115 {external 1 flag}
116 {MACRO_AT_func {main}}
123 set sources "$srcfile $asm_file"
124 if {[build_executable "failed to compile" $testfile $sources {nodebug}]} {
125 return -1
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 {} {
135 global binfile
137 clean_restart $binfile
139 with_test_prefix "console" {
140 gdb_test_no_output "set print object on"
142 if {![runto_main]} {
143 return -1
146 if {![runto func01]} {
147 return -1
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
165 # MI interpreter.
167 proc do_mi_test {} {
169 load_lib mi-support.exp
170 set MIFLAGS "-i=mi"
172 global mi_gdb_prompt
173 global binfile
175 with_test_prefix "mi" {
176 if {[mi_clean_restart $binfile]} {
177 return
180 # This causes GDB to dereference a pointer-to-structure when doing
181 # -var-create.
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
189 # MI test utilities.
190 set test "run to main"
191 mi_run_cmd
192 gdb_expect {
193 -re "\\*stopped,reason=\"breakpoint-hit\".*func=\"main\".*$mi_gdb_prompt$" {
194 pass "$test"
196 timeout {
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"
204 gdb_expect {
205 -re "\\*stopped,reason=\"breakpoint-hit\".*func=\"func01\".*$mi_gdb_prompt$" {
206 pass "$test"
208 timeout {
209 fail "$test (timeout)"
213 # Test that -var-create for 'ptr' is successful.
214 mi_create_varobj "var1" "ptr" "create varobj for ptr"
216 set struct_foo_ptr \
217 [string cat \
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" \
222 [list \
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 } {
246 return -1
249 do_console_test
250 do_mi_test