aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.dwarf2/implref-struct.exp
blob: a67df1a5ff775393791e98619e8317697cad98ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright 2016-2021 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
# The referenced value is a global struct whose location is a DW_OP_addr.

if [skip_cplus_tests] {
    continue
}

load_lib dwarf.exp

# This test can only be run on targets which support DWARF-2 and use gas.
if ![dwarf2_support] {
    return 0
}

# We'll place the output of Dwarf::assemble in implref-struct.S.
standard_testfile .c .S

# ${testfile} is now "implref-struct".  srcfile2 is "implref-struct.S".
set executable ${testfile}
set asm_file [standard_output_file ${srcfile2}]

# We need to know the size of integer and address types in order
# to write some of the debugging info we'd like to generate.
#
# For that, we ask GDB by debugging our implref-struct program.
# Any program would do, but since we already have implref-struct
# specifically for this testcase, might as well use that.
if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {debug c++}] {
    return -1
}

# Create the DWARF.  We need a regular variable for the struct and a reference
# to it that'll be marked with DW_OP_GNU_implicit_pointer. The variable must be
# global so that its name is an exported symbol that can we can reference from
# the DWARF using gdb_target_symbol.
Dwarf::assemble ${asm_file} {
    cu {} {
	DW_TAG_compile_unit {
	    {DW_AT_language @DW_LANG_C_plus_plus}
	} {
	    declare_labels int_label struct_label variable_label ref_label
	    set int_size [get_sizeof "int" -1]

	    # gdb always assumes references are implemented as pointers.
	    set addr_size [get_sizeof "void *" -1]
	    set S_size [get_sizeof "S" -1]

	    # The compiler shouldn't introduce structure padding here.
	    set b_offset 4
	    set c_offset 8

	    int_label: DW_TAG_base_type {
		{DW_AT_byte_size ${int_size} DW_FORM_udata}
		{DW_AT_encoding @DW_ATE_signed}
		{DW_AT_name "int"}
	    }

	    struct_label: DW_TAG_structure_type {
		{DW_AT_name "S"}
		{DW_AT_byte_size ${S_size} DW_FORM_udata}
	    } {
		DW_TAG_member {
		    {DW_AT_name "a"}
		    {DW_AT_type :${int_label}}
		    {DW_AT_data_member_location 0 DW_FORM_udata}
		}

		DW_TAG_member {
		    {DW_AT_name "b"}
		    {DW_AT_type :${int_label}}
		    {DW_AT_data_member_location ${b_offset} DW_FORM_udata}
		}

		DW_TAG_member {
		    {DW_AT_name "c"}
		    {DW_AT_type :${int_label}}
		    {DW_AT_data_member_location ${c_offset} DW_FORM_udata}
		}
	    }

	    ref_label: DW_TAG_reference_type {
		{DW_AT_byte_size ${addr_size} DW_FORM_udata}
		{DW_AT_type :${struct_label}}
	    }

	    variable_label: DW_TAG_variable {
		{DW_AT_name "s1"}
		{DW_AT_type :${struct_label}}
		{DW_AT_external 1 DW_FORM_flag}
		{DW_AT_location {DW_OP_addr [gdb_target_symbol "s1"]} SPECIAL_expr}
	    }

	    DW_TAG_variable {
		{DW_AT_name "s2"}
		{DW_AT_type :${struct_label}}
		{DW_AT_external 1 DW_FORM_flag}
		{DW_AT_location {DW_OP_addr [gdb_target_symbol "s2"]} SPECIAL_expr}
	    }

	    DW_TAG_subprogram {
		{MACRO_AT_func { "main" }}
		{DW_AT_type :${int_label}}
		{DW_AT_external 1 DW_FORM_flag}
	    } {
	        DW_TAG_variable {
		    {DW_AT_name "ref"}
		    {DW_AT_type :${ref_label}}
		    {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
		}
	    }
	}
    }
}

if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] {
    return -1
}

# DW_OP_GNU_implicit_pointer implementation requires a valid frame.
if ![runto_main] {
    return -1
}

# Returns the struct members, e.g. '{a = 0, b = 1, c = 2}'.
proc get_members {var} {
    set members [get_valueof "" ${var} ""]

    # Trim leading/trailing whitespace, '{' and '}' since they confuse TCL to no end.
    set members [string trim ${members}]
    set members [string trim ${members} "{}"]

    return ${members}
}

# Values of the struct members.
set s1_members [get_members "s1"]
set s2_members [get_members "s2"]

# Address of the referenced value.
set address [get_hexadecimal_valueof "&s1" ""]

# Test printing with both 'set print object off' and 'on', just to make sure
# the output doesn't change.
foreach_with_prefix print-object {"off" "on"} {
    gdb_test_no_output "set print object ${print-object}"

    # Doing 'print ref' should show us e.g.
    # '(S &) @0xdeadbeef: {a = 0, b = 1, c = 2}'.
    gdb_test "print ref" " = \\(S &\\) @${address}: \\{${s1_members}\\}"

    # Doing 'print &ref' should show us e.g. '(S *) 0xdeadbeef <s1>'.
    gdb_test "print &ref" " = \\(S \\*\\) ${address} <s1>"

    # gdb assumes C++ references are implemented as pointers, and
    # print &(&ref) shows us the underlying pointer's address.
    # Since in this case there's no physical pointer, gdb should tell us so.
    gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
}

# Test assignment through the synthetic reference.
gdb_test_no_output "set (ref = s2)"

with_test_prefix "after assignment" {
    foreach_with_prefix print-object {"off" "on"} {
	gdb_test_no_output "set print object ${print-object}"

	gdb_test "print ref" " = \\(S &\\) @${address}: \\{${s2_members}\\}" "print ref"
	gdb_test "print s1" " = \\{${s2_members}\\}" "print s1"
    }
}