Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / nonvar-access.exp
blob18be85ffeed4d4f17be63a0143f9c2c9e70af8a2
1 # Copyright 2016-2023 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 # Test accessing "non-variable" variables, i.e., variables which are
17 # optimized to a constant DWARF location expression and/or
18 # partially/fully optimized out.
20 load_lib dwarf.exp
22 if {![dwarf2_support]} { return 0 }
24 standard_testfile main.c -dw.S
26 # Make some DWARF for the test.
27 set asm_file [standard_output_file $srcfile2]
29 Dwarf::assemble $asm_file {
30     global srcfile
32     cu {} {
33         compile_unit {
34             {DW_AT_name $srcfile}
35         } {
36             declare_labels int_type_label char_type_label \
37                 struct_s_label struct_t_label array_a9_label \
38                 char_ptr_label implicit_a_label stack_b_label
40             int_type_label: base_type {
41                 {name "int"}
42                 {encoding @DW_ATE_signed}
43                 {byte_size 4 DW_FORM_sdata}
44             }
45             char_type_label: base_type {
46                 {name "char"}
47                 {encoding @DW_ATE_unsigned}
48                 {byte_size 1 DW_FORM_sdata}
49             }
50             char_ptr_label: pointer_type {
51                 {type :$char_type_label}
52             }
54             struct_s_label: structure_type {
55                 {name s}
56                 {byte_size 4 DW_FORM_sdata}
57             } {
58                 member {
59                     {name a}
60                     {type :$int_type_label}
61                     {data_member_location 0 DW_FORM_udata}
62                     {bit_size 8 DW_FORM_udata}
63                 }
64                 member {
65                     {name b}
66                     {type :$int_type_label}
67                     {data_bit_offset 8 DW_FORM_udata}
68                     {bit_size 24 DW_FORM_udata}
69                 }
70             }
72             struct_t_label: structure_type {
73                 {name t}
74                 {byte_size 4 DW_FORM_sdata}
75             } {
76                 member {
77                     {name a}
78                     {type :$int_type_label}
79                     {data_member_location 0 DW_FORM_udata}
80                     {bit_size 9 DW_FORM_udata}
81                 }
82                 member {
83                     {name b}
84                     {type :$int_type_label}
85                     {data_bit_offset 9 DW_FORM_udata}
86                     {bit_size 23 DW_FORM_udata}
87                 }
88             }
90             array_a9_label: array_type {
91                 {type :$char_type_label}
92             } {
93                 subrange_type {
94                     {type :$int_type_label}
95                     {upper_bound 8 DW_FORM_udata}
96                 }
97             }
99             DW_TAG_subprogram {
100                 {MACRO_AT_func {main}}
101                 {DW_AT_external 1 flag}
102             } {
103                 # Simple variable without location.
104                 DW_TAG_variable {
105                     {name undef_int}
106                     {type :$int_type_label}
107                 }
108                 # Struct variable without location.
109                 DW_TAG_variable {
110                     {name undef_s}
111                     {type :$struct_s_label}
112                 }
113                 # Composite location: byte-aligned pieces.
114                 DW_TAG_variable {
115                     {name def_s}
116                     {type :$struct_s_label}
117                     {location {
118                         const1u 0
119                         stack_value
120                         bit_piece 8 0
121                         const1s -1
122                         stack_value
123                         bit_piece 24 0
124                     } SPECIAL_expr}
125                 }
126                 # Composite location: non-byte-aligned pieces.
127                 DW_TAG_variable {
128                     {name def_t}
129                     {type :$struct_t_label}
130                     {location {
131                         const2s -184
132                         stack_value
133                         bit_piece 9 0
134                         const4u 1752286
135                         stack_value
136                         bit_piece 23 0
137                     } SPECIAL_expr}
138                 }
139                 # Composite location with some empty pieces.
140                 DW_TAG_variable {
141                     {name part_def_a}
142                     {type :$array_a9_label}
143                     {location {
144                         piece 3
145                         const4u 0xf1927314
146                         stack_value
147                         piece 4
148                         piece 2
149                     } SPECIAL_expr}
150                 }
151                 # Implicit location: immediate value.
152                 DW_TAG_variable {
153                     {name def_implicit_s}
154                     {type :$struct_s_label}
155                     {location {
156                         implicit_value 0x12 0x34 0x56 0x78
157                     } SPECIAL_expr}
158                 }
159                 # Implicit location: immediate value for whole array, with
160                 # excess bytes.
161                 implicit_a_label: DW_TAG_variable {
162                     {name def_implicit_a}
163                     {type :$array_a9_label}
164                     {location {
165                         implicit_value 0x1 0x12 0x23 0x34 0x45 \
166                             0x56 0x67 0x78 0x89 0x9a 0xab
167                     } SPECIAL_expr}
168                 }
169                 # Implicit pointer into immediate value.
170                 DW_TAG_variable {
171                     {name implicit_a_ptr}
172                     {type :$char_ptr_label}
173                     {location {
174                         implicit_pointer $implicit_a_label 5
175                     } SPECIAL_expr}
176                 }
177                 # Stack-value location.
178                 stack_b_label: DW_TAG_variable {
179                     {name def_stack_b}
180                     {type :$struct_t_label}
181                     {location {
182                         const4u 0x1a2b3c4d
183                         stack_value
184                     } SPECIAL_expr}
185                 }
186                 # Implicit pointer into stack value.
187                 DW_TAG_variable {
188                     {name implicit_b_ptr}
189                     {type :$char_ptr_label}
190                     {location {
191                         implicit_pointer $stack_b_label 1
192                     } SPECIAL_expr}
193                 }
194             }
195         }
196     }
199 if { [prepare_for_testing "failed to prepare" ${testfile} \
200           [list $srcfile $asm_file] {nodebug}] } {
201     return -1
204 if ![runto_main] {
205     return -1
208 # Determine byte order.
209 set endian [get_endianness]
211 # Byte-aligned objects with simple location descriptions.
212 switch $endian { big {set val 0x345678} little {set val 0x785634} }
213 gdb_test "print/x def_implicit_s" " = \\{a = 0x12, b = $val\\}"
214 gdb_test "print/x def_implicit_s.b" " = $val"
215 gdb_test "print/x def_implicit_a" \
216     " = \\{0x1, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89\\}"
217 gdb_test "print/x def_implicit_a\[5\]" " = 0x56"
218 gdb_test "print/x *(char (*)\[5\]) implicit_a_ptr" \
219     " = \\{0x56, 0x67, 0x78, 0x89, 0x9a\\}"
220 switch $endian {
221     big {set val "a = 52, b = 2833485"}
222     little {set val "a = 77, b = 857502"}
224 gdb_test "print def_stack_b" " = \\{$val\\}"
225 switch $endian {big {set val 0x2b} little {set val 0x3c}}
226 gdb_test "print/x *implicit_b_ptr" " = $val"
228 # Byte-aligned fields, pieced together from DWARF stack values.
229 gdb_test "print def_s" " = \\{a = 0, b = -1\\}"
230 switch $endian { big {set val 0x92} little {set val 0x73} }
231 gdb_test "print/x part_def_a\[4\]" " = $val"
232 gdb_test "print/x part_def_a\[8\]" " = <optimized out>"
234 # Non-byte-aligned fields, pieced together from DWARF stack values.
235 gdb_test "print def_t" " = \\{a = -184, b = 1752286\\}"
237 # Simple variable without location.
238 gdb_test "print undef_int" " = <optimized out>"
240 # Member of a structure without location.
241 gdb_test "print undef_s.a" " = <optimized out>"