[PR gdb/19893] Fix handling of synthetic C++ references
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / implref-array.exp
1 # Copyright 2016 Free Software Foundation, Inc.
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 array whose location is a DW_OP_addr.
18
19 if [skip_cplus_tests] {
20 continue
21 }
22
23 load_lib dwarf.exp
24
25 # This test can only be run on targets which support DWARF-2 and use gas.
26 if ![dwarf2_support] {
27 return 0
28 }
29
30 # We'll place the output of Dwarf::assemble in implref-array.S.
31 standard_testfile .c .S
32
33 # ${testfile} is now "implref-array". srcfile2 is "implref-array.S".
34 set executable ${testfile}
35 set asm_file [standard_output_file ${srcfile2}]
36
37 # We need to know the size of integer and address types in order
38 # to write some of the debugging info we'd like to generate.
39 #
40 # For that, we ask GDB by debugging our implref-array program.
41 # Any program would do, but since we already have implref-array
42 # specifically for this testcase, might as well use that.
43 if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
44 untested ${testfile}.exp
45 return -1
46 }
47
48 set array_length [get_valueof "/u" "sizeof(array) / sizeof(array\[0\])" -1]
49
50 # Create the DWARF. We need a regular variable which represents the array, and
51 # a reference to it that'll be marked with DW_OP_GNU_implicit_pointer.
52 # The variable must be global so that its name is an exported symbol that we
53 # can reference from the DWARF using gdb_target_symbol.
54 Dwarf::assemble ${asm_file} {
55 global srcdir subdir srcfile array_length
56
57 cu {} {
58 DW_TAG_compile_unit {
59 {DW_AT_language @DW_LANG_C_plus_plus}
60 } {
61 declare_labels int_label sizetype_label array_label variable_label ref_label
62 set int_size [get_sizeof "int" -1]
63 set upper_bound [expr ${array_length} - 1]
64
65 # gdb always assumes references are implemented as pointers.
66 set addr_size [get_sizeof "void *" -1]
67
68 int_label: DW_TAG_base_type {
69 {DW_AT_byte_size ${int_size} DW_FORM_udata}
70 {DW_AT_encoding @DW_ATE_signed}
71 {DW_AT_name "int"}
72 }
73
74 sizetype_label: DW_TAG_base_type {
75 {DW_AT_byte_size ${int_size} DW_FORM_udata}
76 {DW_AT_encoding @DW_ATE_unsigned}
77 {DW_AT_name "sizetype"}
78 }
79
80 array_label: DW_TAG_array_type {
81 {DW_AT_type :${int_label}}
82 } {
83 DW_TAG_subrange_type {
84 {DW_AT_type :${sizetype_label}}
85 {DW_AT_lower_bound 0 DW_FORM_udata}
86 {DW_AT_upper_bound ${upper_bound} DW_FORM_udata}
87 }
88 }
89
90 ref_label: DW_TAG_reference_type {
91 {DW_AT_byte_size ${addr_size} DW_FORM_udata}
92 {DW_AT_type :${array_label}}
93 }
94
95 variable_label: DW_TAG_variable {
96 {DW_AT_name "array"}
97 {DW_AT_type :${array_label}}
98 {DW_AT_external 1 DW_FORM_flag}
99 {DW_AT_location {DW_OP_addr [gdb_target_symbol "array"]} SPECIAL_expr}
100 }
101
102 DW_TAG_subprogram {
103 {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }}
104 {DW_AT_type :${int_label}}
105 {DW_AT_external 1 DW_FORM_flag}
106 } {
107 DW_TAG_variable {
108 {DW_AT_name "ref"}
109 {DW_AT_type :${ref_label}}
110 {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
111 }
112 }
113 }
114 }
115 }
116
117 if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] {
118 return -1
119 }
120
121 # DW_OP_GNU_implicit_pointer implementation requires a valid frame.
122 if ![runto_main] {
123 return -1
124 }
125
126 # This matches e.g. '(int (&)[5])'
127 set ref_type [format {\(int \(&\)\[%d\]\)} ${array_length}]
128
129 # This matches e.g. '(int (*)[5])'
130 set ptr_type [format {\(int \(\*\)\[%d\]\)} ${array_length}]
131
132 # Contents of the array. Trim leading/trailing whitespace, '{' and '}'
133 # since they confuse TCL to no end.
134 set contents [get_valueof "" "array" ""]
135 set contents [string trim ${contents}]
136 set contents [string trim ${contents} "{}"]
137
138 # Address of the referenced value.
139 set address [get_hexadecimal_valueof "&array" ""]
140
141 # Doing 'print ref' should show us e.g. '(int (&)[5]) 0xdeadbeef: {0, 1, 2, 3, 4}'.
142 gdb_test "print ref" " = ${ref_type} @${address}: \\{${contents}\\}"
143
144 # Doing 'print &ref' should show us e.g. '(int (*)[5]) 0xdeadbeef <array>'.
145 gdb_test "print &ref" " = ${ptr_type} ${address} <array>"
146
147 # gdb assumes C++ references are implemented as pointers, and print &(&ref)
148 # shows us the underlying pointer's address. Since in this case there's no
149 # physical pointer, gdb should tell us so.
150 gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
151
152 # Test assignment through the synthetic reference.
153 set first_value 10
154 gdb_test_no_output "set (ref\[0\] = ${first_value})"
155
156 # This matches '{10, 1, 2, 3, 4}'.
157 set new_contents [format {\{%d, 1, 2, 3, 4\}} ${first_value}]
158
159 # Doing 'print ref' should now show us e.g.
160 # '(int (&)[5]) <synthetic pointer>: {10, 1, 2, 3, 4}'.
161 gdb_test "print ref" " = ${ref_type} @${address}: ${new_contents}" "print ref after assignment"
162 gdb_test "print array" " = ${new_contents}" "print array after assignment"
163
164 # Test treating the array as a pointer.
165 set second_value 20
166 set new_contents [format {\{%d, %d, 2, 3, 4\}} ${first_value} ${second_value}]
167
168 gdb_test "print *ref" " = ${first_value}"
169 gdb_test_no_output "set (*(ref + 1) = ${second_value})"
170 gdb_test "print ref\[1\]" " = ${second_value}"
171 gdb_test "print array" " = ${new_contents}" "print array after second assignment"
This page took 0.033296 seconds and 4 git commands to generate.