aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.dwarf2/varval.exp
blob: 594591025f552d5c37e8db4b089bbd16b0768df0 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Copyright 2018-2019 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 support for DW_OP_GNU_variable_value. 

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 varval.S.
standard_testfile .c .S

# ${testfile} is now "varval".  srcfile2 is "varval.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 varval program.
# Any program would do, but since we already have varval
# specifically for this testcase, might as well use that.
if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] {
    return -1
}

proc setup_exec { arg_bad } {
    global asm_file executable srcfile bad
    set bad ${arg_bad}

    # Create the DWARF.  
    Dwarf::assemble ${asm_file} {
	global srcdir subdir srcfile bad

	cu {} {
	    DW_TAG_compile_unit {
		{DW_AT_language @DW_LANG_C_plus_plus}
	    } {
		declare_labels int_label ptr_label struct_label var_a_label \
		    var_b_label var_c_label var_p_label var_bad_label \
		    varval_label var_s_label var_untyped_label \
		    var_a_abstract_label var_a_concrete_label \
		    varval2_label

		set int_size [get_sizeof "int" -1]

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

		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"}
		}

		ptr_label: DW_TAG_pointer_type {
		    {DW_AT_type :$int_label}
		}

		var_a_label: DW_TAG_variable {
		    {DW_AT_name "var_a"}
		    {DW_AT_type :${int_label}}
		    {DW_AT_external 1 DW_FORM_flag}
		    {DW_AT_location {DW_OP_addr [gdb_target_symbol "var_a"]} SPECIAL_expr}
		}

		var_a_abstract_label: DW_TAG_variable {
		    {DW_AT_type :${int_label}}
		    {DW_AT_external 1 DW_FORM_flag}
		}

		var_b_label: DW_TAG_variable {
		    {DW_AT_name "var_b"}
		    {DW_AT_type :${int_label}}
		    {DW_AT_external 1 DW_FORM_flag}
		    {DW_AT_location {DW_OP_addr [gdb_target_symbol "var_b"]} SPECIAL_expr}
		}

		var_c_label: DW_TAG_variable {
		    {DW_AT_name "var_c"}
		    {DW_AT_type :${int_label}}
		    {DW_AT_external 1 DW_FORM_flag}
		    {DW_AT_const_value 53 DW_FORM_sdata}
		}

		var_p_label: DW_TAG_variable {
		    {DW_AT_name "var_p"}
		    {DW_AT_type :${ptr_label}}
		    {DW_AT_external 1 DW_FORM_flag}
		    {DW_AT_location {DW_OP_addr [gdb_target_symbol "var_p"]} SPECIAL_expr}
		}

		if { $bad } {
		    var_bad_label: DW_TAG_variable {
			{DW_AT_name "var_bad"}
			{DW_AT_type :${int_label}}
			{DW_AT_external 1 DW_FORM_flag}
		    }
		}

		struct_label: DW_TAG_structure_type {
		    {DW_AT_byte_size 8*$int_size DW_FORM_sdata}
		} {
		    DW_TAG_member {
			{DW_AT_name "a"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 0*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "b"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 1*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "c"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 2*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "d"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 3*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "e"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 4*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "f"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 5*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "g"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 6*$int_size DW_FORM_udata}
		    }
		    DW_TAG_member {
			{DW_AT_name "h"}
			{DW_AT_type :$int_label}
			{DW_AT_data_member_location 7*$int_size DW_FORM_udata}
		    }
		}

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

		var_untyped_label: DW_TAG_variable {
		    {DW_AT_name "var_untyped"}
		    {DW_AT_external 1 DW_FORM_flag}
		    {DW_AT_location {DW_OP_addr [gdb_target_symbol "var_b"]} SPECIAL_expr}
		}

		DW_TAG_subprogram {
		    {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }}
		    {DW_AT_type :${int_label}}
		    {DW_AT_external 1 DW_FORM_flag}
		} {
		    varval_label: DW_TAG_variable {
			{DW_AT_name "varval"}
			{DW_AT_type :${int_label}}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_a_label}
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    varval2_label: DW_TAG_variable {
			{DW_AT_name "varval2"}
			{DW_AT_type :${int_label}}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_a_abstract_label}
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    var_a_concrete_label: DW_TAG_variable {
			{DW_AT_abstract_origin :${var_a_abstract_label}}
			{DW_AT_location {DW_OP_addr [gdb_target_symbol "var_a"]} SPECIAL_expr}
		    }
		    DW_TAG_variable {
			{DW_AT_name "constval"}
			{DW_AT_type :${int_label}}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_c_label}
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    DW_TAG_variable {
			{DW_AT_name "mixedval"}
			{DW_AT_type :${int_label}}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_c_label}
			    DW_OP_GNU_variable_value ${var_b_label}
			    DW_OP_div
			    DW_OP_GNU_variable_value ${varval_label}
			    DW_OP_plus
			    DW_OP_dup
			    DW_OP_plus
			    DW_OP_GNU_variable_value ${varval_label}
			    DW_OP_minus
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    DW_TAG_variable {
			{DW_AT_name "pointerval"}
			{DW_AT_type :${ptr_label}}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_p_label}
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    if { $bad } {
			DW_TAG_variable {
			    {DW_AT_name "badval"}
			    {DW_AT_type :${int_label}}
			    {DW_AT_location {
				DW_OP_GNU_variable_value ${var_bad_label}
				DW_OP_stack_value
			    } SPECIAL_expr}
			}
		    }
		    DW_TAG_variable {
			{DW_AT_name "structval"}
			{DW_AT_type :${struct_label}}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_s_label}
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    DW_TAG_variable {
			{DW_AT_name "untypedval"}
			{DW_AT_location {
			    DW_OP_GNU_variable_value ${var_untyped_label}
			    DW_OP_stack_value
			} SPECIAL_expr}
		    }
		    if { $bad } {
			DW_TAG_variable {
			    {DW_AT_name "bad_die_val1"}
			    {DW_AT_location {
				DW_OP_GNU_variable_value 0xabcdef11
				DW_OP_stack_value
			    } SPECIAL_expr}
			}
			DW_TAG_variable {
			    {DW_AT_name "bad_die_val2"}
			    {DW_AT_location {
				DW_OP_GNU_variable_value ${ptr_label}+1
				DW_OP_stack_value
			    } SPECIAL_expr}
			}
		    }
		}
	    }
	}
    }

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

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

if { [setup_exec 0] == -1 } {
    return -1
}

gdb_test "print varval" "= 8"
gdb_test "print varval2" "= 8"
gdb_test "print constval" "= 53"
gdb_test "print mixedval" "= 42"
gdb_test "print pointerval" "= \\(int \\*\\) $hex <var_b>"
gdb_test "print *pointerval" "= 3"

# Jakub says:  "The intended behavior is that the debug info consumer
# computes the value of that referenced variable at the current PC,
# and if it can compute it and pushes the value as a generic type
# integer into the DWARF stack (it is really only meaningful when
# referring to integral/pointer typed variables)."

gdb_test "print structval" \
         "Type of DW_OP_GNU_variable_value DIE must be an integer or pointer\\."

gdb_test "print untypedval" \
         "Type of DW_OP_GNU_variable_value DIE must be an integer or pointer\\."

if { [setup_exec 1] == -1 } {
    return -1
}

gdb_test "print badval" "value has been optimized out"
gdb_test "print bad_die_val1" \
         "invalid dwarf2 offset 0xabcdef11"
gdb_test "print bad_die_val2" \
         "Bad DW_OP_GNU_variable_value DIE\\."