aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.dwarf2/dw2-disasm-over-non-stmt.exp
blob: 5311dd61a4c72962b6aec4252616faa660ba0b41 (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
# Copyright 2020-2022 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/>.

# Create an example function that contains both addresses marked as
# statements and addresses marked as non-statements, and then
# disassemble the function.
#
# Of particular interest is how 'disassemble /m' handles the
# non-statement addresses, we want to ensure that these addresses are
# included in the disassembly output.  For completeness we test both
# 'disassemble /m' and 'disassemble /s'.

load_lib dwarf.exp

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

# The .c files use __attribute__.
if [get_compiler_info] {
    return -1
}
if !$gcc_compiled {
    return 0
}

# Reuse many of the test source files from dw2-inline-header-1.exp.
standard_testfile dw2-inline-header-lbls.c dw2-inline-header.S \
    dw2-inline-header.c

set asm_file [standard_output_file $srcfile2]
Dwarf::assemble $asm_file {
    global srcdir subdir srcfile srcfile3
    declare_labels lines_label

    get_func_info main

    cu {} {
	compile_unit {
	    {producer "gcc" }
	    {language @DW_LANG_C}
	    {name ${srcfile3}}
	    {low_pc 0 addr}
	    {stmt_list ${lines_label} DW_FORM_sec_offset}
	} {
	    subprogram {
		{external 1 flag}
		{MACRO_AT_func {main}}
	    }
	}
    }

    lines {version 2 default_is_stmt 1} lines_label {
	include_dir "${srcdir}/${subdir}"
	file_name "$srcfile3" 1

	program {
	    DW_LNE_set_address $main_start
	    DW_LNS_advance_line 15
	    DW_LNS_copy

	    DW_LNE_set_address line_label_2
	    DW_LNS_negate_stmt
	    DW_LNS_copy

	    DW_LNE_set_address line_label_3
	    DW_LNS_advance_line 1
	    DW_LNS_copy

	    DW_LNE_set_address line_label_4
	    DW_LNS_negate_stmt
	    DW_LNS_copy

	    DW_LNE_set_address line_label_5
	    DW_LNS_negate_stmt
	    DW_LNS_copy

	    DW_LNE_set_address line_label_6
	    DW_LNS_advance_line 1
	    DW_LNS_negate_stmt
	    DW_LNS_copy

	    DW_LNE_set_address $main_end
	    DW_LNS_copy
	    DW_LNE_end_sequence
	}
    }
}

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

if ![runto_main] {
    return -1
}

# Global lines array, maps lines numbers to the list of addresses
# associated with that line in the debug output.
array set lines {}

# Look in the global LINES array and check that the disassembly for
# line LINENUM includes the address of LABEL.
proc check_disassembly_results { linenum label } {
    global lines

    set address [get_hexadecimal_valueof "&${label}" "__unknown__"]
    set testname "check_disassembly_results $linenum $label"
    if {![info exists lines($linenum)]} {
	fail "$testname (no disassembly for $linenum)"
	return
    }

    # Use a loop to compare the addresses as the addresses extracted
    # from the disassembly output can be padded with zeros, while the
    # address of the label will not be padded.
    set addrs $lines($linenum)
    foreach a $addrs {
	if { $a == $address } {
	    pass $testname
	    return
	}
    }
    fail $testname
}

foreach_with_prefix opt { m s } {
    # Disassemble 'main' and split up the disassembly output.  We
    # build an associative array, for each line number store the list
    # of addresses that were part of its disassembly output.
    #
    # LINENUM is the line we are currently collecting the disassembly
    # addresses for, and ADDRS is the list of addresses collected for
    # this line.
    set linenum -1
    set addrs {}

    # Clear the global associative array used to hold the results.
    unset lines
    array set lines {}

    gdb_test_multiple "disassemble /${opt} main" "" {
	-re "Dump of assembler code for function main:\r\n" {
	    exp_continue
	}

	-re "^\[^\r\n\]+${srcfile3}:" {
	    exp_continue
	}

	-re "^(\\d+)\\s+\[^\r\n\]+\r\n" {
	    if { $linenum != -1 } {
		set lines($linenum) $addrs
		set addrs {}
	    }
	    set linenum $expect_out(1,string)
	    exp_continue
	}

	-re "^(?:=>)?\\s*($hex)\\s*\[^\r\n\]+\r\n" {
	    set address $expect_out(1,string)
	    lappend addrs $address
	    exp_continue
	}

	-re "^\\s*\r\n" {
	    exp_continue
	}

	-re "^End of assembler dump\\.\r\n" {
	    if { $linenum != -1 } {
		set lines($linenum) $addrs
		set linenum -1
		set addrs {}
	    }
	    exp_continue
	}

	-re "^$gdb_prompt $" {
	    # All done.
	}
    }

    # Now check that each label we expect to be associated with each line
    # shows up in the disassembly output.
    check_disassembly_results 16 "line_label_1"
    check_disassembly_results 16 "line_label_2"
    check_disassembly_results 17 "line_label_3"
    check_disassembly_results 17 "line_label_4"
    check_disassembly_results 17 "line_label_5"
    check_disassembly_results 18 "line_label_6"
}