Add translations for various sub-directories
[binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / var-access.exp
blob3a7bd70db37ab46a73f370a3dc9654458d4b30a5
1 # Copyright 2017-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 # Test reading/writing variables with non-trivial DWARF locations.  In
17 # particular the test uses register- and memory locations as well as
18 # composite locations with register- and memory pieces.
20 load_lib dwarf.exp
22 # This test can only be run on targets which support DWARF-2 and use gas.
23 require dwarf2_support
25 # Choose suitable integer registers for the test.
27 set dwarf_regnum {0 1}
29 if { [is_aarch64_target] } {
30     set regname {x0 x1}
31 } elseif { [is_aarch32_target]
32            || [istarget "s390*-*-*" ]
33            || [istarget "powerpc*-*-*"]
34            || [istarget "rs6000*-*-aix*"] } {
35     set regname {r0 r1}
36 } elseif { [is_x86_like_target] } {
37     set regname {eax ecx}
38 } elseif { [is_amd64_regs_target] } {
39     set regname {rax rdx}
40 } else {
41     verbose "Skipping $gdb_test_file_name."
42     return
45 standard_testfile .c -dw.S
47 # Make some DWARF for the test.
49 set asm_file [standard_output_file $srcfile2]
50 Dwarf::assemble $asm_file {
51     global dwarf_regnum regname srcfile
53     set buf_var [gdb_target_symbol buf]
55     cu {} {
56         DW_TAG_compile_unit {
57                 {DW_AT_name $srcfile}
58                 {DW_AT_comp_dir /tmp}
59         } {
60             declare_labels char_type_label
61             declare_labels int_type_label short_type_label
62             declare_labels array_a8_label struct_s_label struct_t_label
63             declare_labels struct_st_label
65             # char
66             char_type_label: base_type {
67                 {name "char"}
68                 {encoding @DW_ATE_unsigned_char}
69                 {byte_size 1 DW_FORM_sdata}
70             }
72             # int
73             int_type_label: base_type {
74                 {name "int"}
75                 {encoding @DW_ATE_signed}
76                 {byte_size 4 DW_FORM_sdata}
77             }
79             # char [8]
80             array_a8_label: array_type {
81                 {type :$char_type_label}
82             } {
83                 subrange_type {
84                     {type :$int_type_label}
85                     {upper_bound 7 DW_FORM_udata}
86                 }
87             }
89             # struct s { char a, b, c, d; };
90             struct_s_label: structure_type {
91                 {name "s"}
92                 {byte_size 4 DW_FORM_sdata}
93             } {
94                 member {
95                     {name "a"}
96                     {type :$char_type_label}
97                     {data_member_location 0 DW_FORM_udata}
98                 }
99                 member {
100                     {name "b"}
101                     {type :$char_type_label}
102                     {data_member_location 1 DW_FORM_udata}
103                 }
104                 member {
105                     {name "c"}
106                     {type :$char_type_label}
107                     {data_member_location 2 DW_FORM_udata}
108                 }
109                 member {
110                     {name "d"}
111                     {type :$char_type_label}
112                     {data_member_location 3 DW_FORM_udata}
113                 }
114             }
116             # struct t { int u, x:9, y:13, z:10; };
117             struct_t_label: structure_type {
118                 {name "t"}
119                 {byte_size 8 DW_FORM_sdata}
120             } {
121                 member {
122                     {name "u"}
123                     {type :$int_type_label}
124                 }
125                 member {
126                     {name "x"}
127                     {type :$int_type_label}
128                     {data_member_location 4 DW_FORM_udata}
129                     {bit_size 9 DW_FORM_udata}
130                 }
131                 member {
132                     {name "y"}
133                     {type :$int_type_label}
134                     {data_bit_offset 41 DW_FORM_udata}
135                     {bit_size 13 DW_FORM_udata}
136                 }
137                 member {
138                     {name "z"}
139                     {type :$int_type_label}
140                     {data_bit_offset 54 DW_FORM_udata}
141                     {bit_size 10 DW_FORM_udata}
142                 }
143             }
145             # struct st { struct s s; struct t t; };
146             struct_st_label: structure_type {
147                 {name "st"}
148                 {byte_size 12 DW_FORM_udata}
149             } {
150                 member {
151                     {name "s"}
152                     {type :$struct_s_label}
153                 }
154                 member {
155                     {name "t"}
156                     {type :$struct_t_label}
157                     {data_member_location 4 DW_FORM_udata}
158                 }
159             }
161             DW_TAG_subprogram {
162                 {MACRO_AT_func { main }}
163                 {DW_AT_external 1 flag}
164             } {
165                 # Simple memory location.
166                 DW_TAG_variable {
167                     {name "a"}
168                     {type :$array_a8_label}
169                     {location {
170                         addr $buf_var
171                     } SPECIAL_expr}
172                 }
173                 # Memory pieces: two bytes from &buf[2], and two bytes
174                 # from &buf[0].
175                 DW_TAG_variable {
176                     {name "s1"}
177                     {type :$struct_s_label}
178                     {location {
179                         addr $buf_var
180                         plus_uconst 2
181                         piece 2
182                         addr $buf_var
183                         piece 2
184                     } SPECIAL_expr}
185                 }
186                 # Register- and memory pieces: one byte each from r0,
187                 # &buf[4], r1, and &buf[5].
188                 DW_TAG_variable {
189                     {name "s2"}
190                     {type :$struct_s_label}
191                     {location {
192                         regx [lindex $dwarf_regnum 0]
193                         piece 1
194                         addr "$buf_var + 4"
195                         piece 1
196                         regx [lindex $dwarf_regnum 1]
197                         piece 1
198                         addr "$buf_var + 5"
199                         piece 1
200                     } SPECIAL_expr}
201                 }
202                 # Memory pieces for bitfield access: 8 bytes optimized
203                 # out, 3 bytes from &buf[3], and 1 byte from &buf[1].
204                 DW_TAG_variable {
205                     {name "st1"}
206                     {type :$struct_st_label}
207                     {location {
208                         piece 8
209                         addr "$buf_var + 3"
210                         piece 3
211                         addr "$buf_var + 1"
212                         piece 1
213                     } SPECIAL_expr}
214                 }
215                 # Register pieces for bitfield access: 4 bytes optimized
216                 # out, 3 bytes from r0, and 1 byte from r1.
217                 DW_TAG_variable {
218                     {name "t2"}
219                     {type :$struct_t_label}
220                     {location {
221                         piece 4
222                         regx [lindex $dwarf_regnum 0]
223                         piece 3
224                         regx [lindex $dwarf_regnum 1]
225                         piece 1
226                     } SPECIAL_expr}
227                 }
228                 # One piece per bitfield, using piece offsets: 32 bits of
229                 # an implicit value, 9 bits of a stack value, 13 bits of
230                 # r0, and 10 bits of buf.
231                 DW_TAG_variable {
232                     {name "t3"}
233                     {type :$struct_t_label}
234                     {location {
235                         implicit_value 0x12 0x34 0x56 0x78 0x9a
236                         bit_piece 32 4
237                         const2s -280
238                         stack_value
239                         bit_piece 9 2
240                         regx [lindex $dwarf_regnum 0]
241                         bit_piece 13 14
242                         addr $buf_var
243                         bit_piece 10 42
244                     } SPECIAL_expr}
245                 }
246             }
247         }
248     }
251 if { [prepare_for_testing ${testfile}.exp ${testfile} \
252           [list $srcfile $asm_file] {nodebug}] } {
253     return -1
256 if ![runto_main] {
257     return -1
260 # Determine byte order.
261 set endian [get_endianness]
263 # Byte-aligned memory pieces.
264 gdb_test "print/d s1" " = \\{a = 2, b = 3, c = 0, d = 1\\}" \
265     "s1 == re-ordered buf"
266 gdb_test_no_output "set var s1.a = 63"
267 gdb_test "print/d s1" " = \\{a = 63, b = 3, c = 0, d = 1\\}" \
268     "verify s1.a"
269 gdb_test "print/d a" " = \\{0, 1, 63, 3, 4, 5, 6, 7\\}" \
270     "verify s1.a through a"
271 gdb_test_no_output "set var s1.b = 42"
272 gdb_test "print/d s1" " = \\{a = 63, b = 42, c = 0, d = 1\\}" \
273     "verify s1.b"
274 gdb_test "print/d a" " = \\{0, 1, 63, 42, 4, 5, 6, 7\\}" \
275     "verify s1.b through a"
277 # Byte-aligned register- and memory pieces.
278 gdb_test_no_output "set var \$[lindex $regname 0] = 81" \
279     "init reg for s2.a"
280 gdb_test_no_output "set var \$[lindex $regname 1] = 28" \
281     "init reg for s2.c"
282 gdb_test "print/u s2" " = \\{a = 81, b = 4, c = 28, d = 5\\}" \
283     "initialized s2 from mem and regs"
284 gdb_test_no_output "set var s2.c += s2.a + s2.b - s2.d"
285 gdb_test "print/u s2" " = \\{a = 81, b = 4, c = 108, d = 5\\}" \
286     "verify s2.c"
287 gdb_test "print/u \$[lindex $regname 1]" " = 108" \
288     "verify s2.c through reg"
289 gdb_test_no_output "set var s2 = {191, 73, 231, 123}" \
290     "re-initialize s2"
291 gdb_test "print/u s2"  " = \\{a = 191, b = 73, c = 231, d = 123\\}" \
292     "verify re-initialized s2"
294 # Unaligned bitfield access through byte-aligned pieces.
295 gdb_test_no_output "set var a = { 0 }"
296 gdb_test_no_output "set var st1.t.x = -7"
297 gdb_test_no_output "set var st1.t.z = 340"
298 gdb_test_no_output "set var st1.t.y = 1234"
299 gdb_test "print st1.t" " = \\{u = <optimized out>, x = -7, y = 1234, z = 340\\}" \
300     "verify st1.t"
301 switch $endian {
302     little {set val "0x55, 0x0, 0xf9, 0xa5, 0x9"}
303     big {set val "0x54, 0x0, 0xfc, 0x93, 0x49"}
305 # | -- | z:2-9 | -- | x:0-7 | x:8 y:0-6 | y:7-12 z:0-1 | -- | -- |
306 #      \_______________________________________________/
307 #                             val
308 gdb_test "print/x a" " = \\{0x0, ${val}, 0x0, 0x0\\}" \
309     "verify st1 through a"
311 switch $endian { big {set val 0x7ffc} little {set val 0x3ffe00} }
312 gdb_test_no_output "set var \$[lindex $regname 0] = $val" \
313     "init t2, first piece"
314 gdb_test_no_output "set var \$[lindex $regname 1] = 0" \
315     "init t2, second piece"
316 gdb_test "print/d t2" " = \\{u = <optimized out>, x = 0, y = -1, z = 0\\}" \
317     "initialized t2 from regs"
318 gdb_test_no_output "set var t2.y = 2641"
319 gdb_test_no_output "set var t2.z = -400"
320 gdb_test_no_output "set var t2.x = 200"
321 gdb_test "print t2.x + t2.y + t2.z" " = 2441"
323 # Bitfield access through pieces with nonzero piece offsets.
324 gdb_test_no_output "set var \$[lindex $regname 0] = 0xa8000" \
325     "init reg for t3.y"
326 gdb_test_no_output "set var *(char \[2\] *) (a + 5) = { 70, 82 }" \
327     "init mem for t3.z"
328 switch $endian {
329     little {set val "u = -1484430527, x = -70, y = 42, z = 145"}
330     big {set val "u = 591751049, x = -70, y = 42, z = 101"}
332 gdb_test "print t3" " = \\{$val\\}" \
333     "initialized t3 from reg and mem"
334 gdb_test_no_output "set var t3.y = -1" \
335     "overwrite t3.y"
336 gdb_test "print/x \$[lindex $regname 0]" " = 0x7ffc000" \
337     "verify t3.y through reg"
338 gdb_test_no_output "set var t3.z = -614" \
339     "overwrite t3.z"
340 switch $endian {big {set val "0x59, 0xa2"} little {set val "0x6a, 0x56"}}
341 gdb_test "print/x *(char \[2\] *) (a + 5)" " = \\{$val\\}" \
342     "verify t3.z through mem"