]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.dwarf2/implref-struct.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / implref-struct.exp
CommitLineData
1d506c26 1# Copyright 2016-2024 Free Software Foundation, Inc.
3326303b
MG
2
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.
7#
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.
12#
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/>.
15
16# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
17# The referenced value is a global struct whose location is a DW_OP_addr.
18
0b94d2b9 19require allow_cplus_tests
3326303b
MG
20
21load_lib dwarf.exp
22
23# This test can only be run on targets which support DWARF-2 and use gas.
ce8d533e 24require dwarf2_support
3326303b
MG
25
26# We'll place the output of Dwarf::assemble in implref-struct.S.
27standard_testfile .c .S
28
29# ${testfile} is now "implref-struct". srcfile2 is "implref-struct.S".
30set executable ${testfile}
31set asm_file [standard_output_file ${srcfile2}]
32
33# We need to know the size of integer and address types in order
34# to write some of the debugging info we'd like to generate.
35#
36# For that, we ask GDB by debugging our implref-struct program.
37# Any program would do, but since we already have implref-struct
38# specifically for this testcase, might as well use that.
5b362f04 39if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {debug c++}] {
3326303b
MG
40 return -1
41}
42
43# Create the DWARF. We need a regular variable for the struct and a reference
44# to it that'll be marked with DW_OP_GNU_implicit_pointer. The variable must be
45# global so that its name is an exported symbol that can we can reference from
46# the DWARF using gdb_target_symbol.
47Dwarf::assemble ${asm_file} {
3326303b
MG
48 cu {} {
49 DW_TAG_compile_unit {
50 {DW_AT_language @DW_LANG_C_plus_plus}
51 } {
52 declare_labels int_label struct_label variable_label ref_label
53 set int_size [get_sizeof "int" -1]
54
55 # gdb always assumes references are implemented as pointers.
56 set addr_size [get_sizeof "void *" -1]
57 set S_size [get_sizeof "S" -1]
58
59 # The compiler shouldn't introduce structure padding here.
60 set b_offset 4
61 set c_offset 8
62
63 int_label: DW_TAG_base_type {
64 {DW_AT_byte_size ${int_size} DW_FORM_udata}
65 {DW_AT_encoding @DW_ATE_signed}
66 {DW_AT_name "int"}
67 }
68
69 struct_label: DW_TAG_structure_type {
70 {DW_AT_name "S"}
71 {DW_AT_byte_size ${S_size} DW_FORM_udata}
72 } {
73 DW_TAG_member {
74 {DW_AT_name "a"}
75 {DW_AT_type :${int_label}}
76 {DW_AT_data_member_location 0 DW_FORM_udata}
77 }
78
79 DW_TAG_member {
80 {DW_AT_name "b"}
81 {DW_AT_type :${int_label}}
82 {DW_AT_data_member_location ${b_offset} DW_FORM_udata}
83 }
84
85 DW_TAG_member {
86 {DW_AT_name "c"}
87 {DW_AT_type :${int_label}}
88 {DW_AT_data_member_location ${c_offset} DW_FORM_udata}
89 }
90 }
91
92 ref_label: DW_TAG_reference_type {
93 {DW_AT_byte_size ${addr_size} DW_FORM_udata}
94 {DW_AT_type :${struct_label}}
95 }
96
97 variable_label: DW_TAG_variable {
98 {DW_AT_name "s1"}
99 {DW_AT_type :${struct_label}}
100 {DW_AT_external 1 DW_FORM_flag}
101 {DW_AT_location {DW_OP_addr [gdb_target_symbol "s1"]} SPECIAL_expr}
102 }
103
104 DW_TAG_variable {
105 {DW_AT_name "s2"}
106 {DW_AT_type :${struct_label}}
107 {DW_AT_external 1 DW_FORM_flag}
108 {DW_AT_location {DW_OP_addr [gdb_target_symbol "s2"]} SPECIAL_expr}
109 }
110
111 DW_TAG_subprogram {
10da644d 112 {MACRO_AT_func { "main" }}
3326303b
MG
113 {DW_AT_type :${int_label}}
114 {DW_AT_external 1 DW_FORM_flag}
115 } {
116 DW_TAG_variable {
117 {DW_AT_name "ref"}
118 {DW_AT_type :${ref_label}}
119 {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
120 }
121 }
122 }
123 }
124}
125
5b362f04 126if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] {
3326303b
MG
127 return -1
128}
129
130# DW_OP_GNU_implicit_pointer implementation requires a valid frame.
131if ![runto_main] {
132 return -1
133}
134
135# Returns the struct members, e.g. '{a = 0, b = 1, c = 2}'.
136proc get_members {var} {
137 set members [get_valueof "" ${var} ""]
138
139 # Trim leading/trailing whitespace, '{' and '}' since they confuse TCL to no end.
140 set members [string trim ${members}]
141 set members [string trim ${members} "{}"]
142
143 return ${members}
144}
145
146# Values of the struct members.
147set s1_members [get_members "s1"]
148set s2_members [get_members "s2"]
149
150# Address of the referenced value.
151set address [get_hexadecimal_valueof "&s1" ""]
152
153# Test printing with both 'set print object off' and 'on', just to make sure
154# the output doesn't change.
155foreach_with_prefix print-object {"off" "on"} {
156 gdb_test_no_output "set print object ${print-object}"
157
158 # Doing 'print ref' should show us e.g.
159 # '(S &) @0xdeadbeef: {a = 0, b = 1, c = 2}'.
160 gdb_test "print ref" " = \\(S &\\) @${address}: \\{${s1_members}\\}"
161
162 # Doing 'print &ref' should show us e.g. '(S *) 0xdeadbeef <s1>'.
163 gdb_test "print &ref" " = \\(S \\*\\) ${address} <s1>"
164
165 # gdb assumes C++ references are implemented as pointers, and
166 # print &(&ref) shows us the underlying pointer's address.
167 # Since in this case there's no physical pointer, gdb should tell us so.
168 gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
169}
170
171# Test assignment through the synthetic reference.
172gdb_test_no_output "set (ref = s2)"
173
111b33f0
TV
174with_test_prefix "after assignment" {
175 foreach_with_prefix print-object {"off" "on"} {
176 gdb_test_no_output "set print object ${print-object}"
3326303b 177
111b33f0
TV
178 gdb_test "print ref" " = \\(S &\\) @${address}: \\{${s2_members}\\}" "print ref"
179 gdb_test "print s1" " = \\{${s2_members}\\}" "print s1"
180 }
3326303b 181}