]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.mi/mi-disassemble.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.mi / mi-disassemble.exp
CommitLineData
213516ef 1# Copyright 1999-2023 Free Software Foundation, Inc.
fb40c209
AC
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
fb40c209 6# (at your option) any later version.
e22f8b7c 7#
fb40c209
AC
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.
e22f8b7c 12#
fb40c209 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
fb40c209 15
fb40c209
AC
16#
17# Test Machine interface (MI) operations for disassembly.
18#
19# The goal is not to test gdb functionality, which is done by other tests,
20# but to verify the correct output response to MI operations.
21#
22
23load_lib mi-support.exp
b30bf9ee 24set MIFLAGS "-i=mi"
fb40c209 25
298a9cf0
TT
26standard_testfile basics.c
27
9357e021 28if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
5b362f04 29 untested "failed to compile"
b60f0898 30 return -1
fb40c209
AC
31}
32
fb40c209
AC
33proc test_disassembly_only {} {
34 global mi_gdb_prompt
35 global hex
19e08fb3 36 global decimal
fb40c209 37
45f07fef
MC
38 set line_main_head [gdb_get_line_number "main ("]
39 set line_main_body [expr $line_main_head + 2]
40
fb40c209
AC
41 # Test disassembly more only for the current function.
42 # Tests:
e2d00831 43 # -data-disassemble -s $pc -e "$pc+8" -- 0
26fb3983
JV
44 # -data-disassemble -a $pc -- 0
45 # -data-disassemble -a callee4 -- 0
45f07fef 46 # -data-disassembly -f basics.c -l $line_main_body -- 0
fb40c209 47
26fb3983 48
fb40c209 49 mi_gdb_test "print/x \$pc" "" ""
fb40c209 50
08f406e9
AB
51 foreach { test_name option_string } [list "mode 0" "-- 0" \
52 "default mode" "" ] {
53 with_test_prefix $test_name {
54 mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" ${option_string}" \
55 "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
56 "data-disassemble from pc to pc+12 assembly only"
57
58 mi_gdb_test "112-data-disassemble -a \$pc ${option_string}" \
59 "112\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
60 "data-disassemble function around pc assembly only"
61
62 mi_gdb_test "113-data-disassemble -a callee4 ${option_string}" \
63 "113\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
64 "data-disassemble function callee4 assembly only"
65
66 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body ${option_string}" \
67 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
68 "data-disassemble file & line, assembly only"
69 }
70 }
fb40c209
AC
71}
72
b716877b
AB
73proc test_disassembly_with_opcodes {} {
74 global mi_gdb_prompt
75 global hex
76 global decimal
77
78 set line_main_head [gdb_get_line_number "main ("]
79 set line_main_body [expr $line_main_head + 2]
80
81 # Test disassembly with opcodes for the current function.
82 # Tests:
83 # -data-disassemble -s $pc -e "$pc+8" -- 2
84 # -data-disassembly -f basics.c -l $line_main_body -- 2
85
86 mi_gdb_test "print/x \$pc" "" ""
b716877b 87
08f406e9
AB
88 foreach { test_name option_string} [list "mode 2" "-- 2" \
89 "mode 0 and --opcodes bytes" "--opcodes bytes -- 0" \
90 "default mode and --opcodes bytes" "--opcodes bytes"] {
91 with_test_prefix $test_name {
92 mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" ${option_string}" \
93 "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\]" \
94 "data-disassemble from pc to pc+12 assembly"
95
96 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body ${option_string}" \
97 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",opcodes=\".*\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]" \
98 "data-disassemble file & line, assembly"
99 }
100 }
b716877b
AB
101}
102
fb40c209
AC
103proc test_disassembly_lines_limit {} {
104 global mi_gdb_prompt
105 global hex
19e08fb3 106 global decimal
fb40c209 107
45f07fef
MC
108 set line_main_head [gdb_get_line_number "main ("]
109 set line_main_body [expr $line_main_head + 2]
110
fb40c209
AC
111 # Test disassembly more only for the current function.
112 # Tests:
45f07fef
MC
113 # -data-disassembly -f basics.c -l $line_main_body -n 20 -- 0
114 # -data-disassembly -f basics.c -l $line_main_body -n 0 -- 0
115 # -data-disassembly -f basics.c -l $line_main_body -n 50 -- 0
fb40c209
AC
116
117 mi_gdb_test "print/x \$pc" "" ""
45f07fef 118 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 20 -- 0" \
70242e8d 119 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
e2d00831 120 "data-disassemble file, line, number assembly only"
fb40c209 121
45f07fef 122 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 0 -- 0" \
70242e8d 123 "222\\^done,asm_insns=\\\[\\\]" \
e2d00831 124 "data-disassemble file, line, number (zero lines) assembly only"
fb40c209 125
45f07fef 126 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 50 -- 0" \
70242e8d 127 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
e2d00831 128 "data-disassemble file, line, number (more than main lines) assembly only"
fb40c209
AC
129}
130
e2d00831 131
fb40c209
AC
132proc test_disassembly_mixed {} {
133 global mi_gdb_prompt
134 global hex
135 global decimal
ed8a1c2d 136 global fullname_syntax
fb40c209 137
45f07fef
MC
138 set line_callee2_head [gdb_get_line_number "callee2 ("]
139 set line_callee2_open_brace [expr $line_callee2_head + 1]
140
fb40c209
AC
141 # Test disassembly more only for the current function.
142 # Tests:
45f07fef 143 # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 1
e2d00831 144 # -data-disassembly -s $pc -e "$pc+8" -- 1
fb40c209 145
45f07fef 146 mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 1" \
ed8a1c2d 147 "002\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_callee2_open_brace\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"callee2\",offset=\"0\",inst=\".*\"\}.*\\\]\}.*,src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[.*\{address=\"$hex\",func-name=\"callee2\",offset=\"$decimal\",inst=\".*\"\}\\\]\}\\\]" \
e2d00831 148 "data-disassemble file, line assembly mixed"
fb40c209
AC
149
150 #
151 # In mixed mode, the lowest level of granularity is the source line.
152 # So we are going to get the disassembly for the source line at
e2d00831 153 # which we are now, even if we have specified that the range is only 2 insns.
fb40c209 154 #
e2d00831 155 mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 1" \
ed8a1c2d 156 "003\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]\}\\\]" \
fb40c209 157 "data-disassemble range assembly mixed"
fb40c209
AC
158}
159
b716877b
AB
160proc test_disassembly_mixed_with_opcodes {} {
161 global mi_gdb_prompt
162 global hex
163 global decimal
ed8a1c2d 164 global fullname_syntax
b716877b
AB
165
166 set line_callee2_head [gdb_get_line_number "callee2 ("]
167 set line_callee2_open_brace [expr $line_callee2_head + 1]
168
169 # Test disassembly mixed with opcodes for the current function.
170 # Tests:
171 # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 3
172 # -data-disassembly -s $pc -e "$pc+8" -- 3
173
174 mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 3" \
ed8a1c2d 175 "002\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_callee2_open_brace\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"callee2\",offset=\"0\",opcodes=\".*\",inst=\".*\"\}.*\\\]\}.*,src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[.*\{address=\"$hex\",func-name=\"callee2\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]\}\\\]" \
b716877b
AB
176 "data-disassemble file, line assembly mixed with opcodes"
177
178 #
179 # In mixed mode, the lowest level of granularity is the source line.
180 # So we are going to get the disassembly for the source line at
181 # which we are now, even if we have specified that the range is only 2 insns.
182 #
183 mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 3" \
ed8a1c2d 184 "003\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]\}\\\]" \
b716877b
AB
185 "data-disassemble range assembly mixed with opcodes"
186}
187
fb40c209
AC
188proc test_disassembly_mixed_lines_limit {} {
189 global mi_gdb_prompt
190 global hex
191 global decimal
ed8a1c2d 192 global fullname_syntax
fb40c209 193
45f07fef
MC
194 set line_main_head [gdb_get_line_number "main ("]
195 set line_main_open_brace [expr $line_main_head + 1]
196 set line_main_body [expr $line_main_head + 2]
197
fb40c209
AC
198 # Test disassembly more only for the current function.
199 # Tests:
45f07fef
MC
200 # -data-disassembly -f basics.c -l $line_main_body -n 20 -- 1
201 # -data-disassembly -f basics.c -l $line_main_body -n 0 -- 1
202 # -data-disassembly -f basics.c -l $line_main_body -n 50 -- 1
fb40c209
AC
203
204 mi_gdb_test "print/x \$pc" "" ""
45f07fef 205 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 20 -- 1" \
ed8a1c2d 206 "222\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]\}\]" \
e2d00831 207 "data-disassemble file, line, number assembly mixed"
fb40c209 208
45f07fef 209 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 0 -- 1" \
ed8a1c2d 210 "222\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_main_open_brace\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\\\]\}\\\]" \
e2d00831 211 "data-disassemble file, line, number (zero lines) assembly mixed"
fb40c209 212
45f07fef 213 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 50 -- 1" \
ed8a1c2d 214 "222\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$decimal\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\}.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]\}\]" \
e2d00831 215 "data-disassemble file, line, number (more than main lines) assembly mixed"
fb40c209
AC
216}
217
218proc test_disassembly_bogus_args {} {
219 global mi_gdb_prompt
220 global hex
221
45f07fef
MC
222 set line_main_head [gdb_get_line_number "main ("]
223 set line_main_body [expr $line_main_head + 2]
224
fb40c209
AC
225 # Test that bogus input to disassembly command is rejected.
226 # Tests:
e2d00831
EZ
227 # -data-disassembly -f foo -l abc -n 0 -- 0
228 # -data-disassembly -s foo -e bar -- 0
26fb3983 229 # -data-disassembly -a foo -- 0
e2d00831
EZ
230 # -data-disassembly -s $pc -f basics.c -- 0
231 # -data-disassembly -f basics.c -l 32 -- 9
fb40c209 232
e2d00831 233 mi_gdb_test "123-data-disassemble -f foo -l abc -n 0 -- 0" \
1b05df00 234 "123\\^error,msg=\"-data-disassemble: Invalid filename.\"" \
e2d00831 235 "data-disassemble bogus filename"
fb40c209 236
e2d00831 237 mi_gdb_test "321-data-disassemble -s foo -e bar -- 0" \
a13e061a 238 "321\\^error,msg=\"No symbol \\\\\"foo\\\\\" in current context.\"" \
26fb3983
JV
239 "data-disassemble bogus address, -s -e"
240
241 mi_gdb_test "322-data-disassemble -a foo -- 0" \
242 "322\\^error,msg=\"No symbol \\\\\"foo\\\\\" in current context.\"" \
243 "data-disassemble bogus address, -a"
fb40c209 244
e2d00831 245 mi_gdb_test "456-data-disassemble -s \$pc -f basics.c -- 0" \
08f406e9
AB
246 "456\\^error,msg=\"-data-disassemble: Usage: \\( -f filename -l linenum .-n howmany. \\| -s startaddr -e endaddr \\| -a addr \\) . --opcodes mode . . --source . . .--. mode .\\.\"" \
247 "data-disassemble mix different args"
fb40c209 248
45f07fef 249 mi_gdb_test "789-data-disassemble -f basics.c -l $line_main_body -- 9" \
6ff0ba5f 250 "789\\^error,msg=\"-data-disassemble: Mode argument must be in the range 0-5.\"" \
08f406e9
AB
251 "data-disassemble wrong mode arg"
252
253 foreach mode { 1 2 3 4 5 } {
254 foreach opcode_arg { none bytes display } {
255 mi_gdb_test "801-data-disassemble -s \$pc -e \"\$pc + 12\" --opcodes ${opcode_arg} -- ${mode}" \
256 "801\\^error,msg=\"-data-disassemble: --opcodes and --source can only be used with mode 0\"" \
257 "data-disassemble use --opcode ${opcode_arg} with mode ${mode}"
258 }
259
260 mi_gdb_test "802-data-disassemble -s \$pc -e \"\$pc + 12\" --source -- ${mode}" \
261 "802\\^error,msg=\"-data-disassemble: --opcodes and --source can only be used with mode 0\"" \
262 "data-disassemble use --source with mode ${mode}"
263 }
fb40c209
AC
264}
265
21a52f7d
AB
266# Check the format of the opcode bytes.
267proc test_disassembly_opcode_format {} {
268 # First, we need to find a multi-byte instruction that we can
269 # then disassemble using the MI command.
270 set longest_insn_bytes ""
271 set longest_insn_addr ""
9c1c98cc 272 gdb_test_multiple "disassemble /b main" "" -prompt "$::mi_gdb_prompt$" {
d4ce49b7 273 -re "^disassemble /b main\r\n" {
21a52f7d
AB
274 exp_continue
275 }
276
d4ce49b7 277 -re "^&\"disassemble /b main.n\"\r\n" {
21a52f7d
AB
278 exp_continue
279 }
280
281 -re "^~\"Dump of assembler code for function \[^\r\n\]+\r\n" {
282 exp_continue
283 }
284
285 -re "^~\".. ($::hex) <\[^>\]+>:\\\\t(\[^\\\\\]+)\\\\t\[^\r\n\]+\r\n" {
286 set addr $expect_out(1,string)
0ffa9277 287 set bytes [string trim $expect_out(2,string)]
21a52f7d
AB
288 if { [string length $bytes] > [string length $longest_insn_bytes] } {
289 set longest_insn_addr $addr
290 set longest_insn_bytes $bytes
291 }
292 exp_continue
293 }
294
295 -re "^~\"End of assembler dump\[^\r\n\]+\r\n" {
296 exp_continue
297 }
298
299 -re "^\\^done\r\n$::mi_gdb_prompt$" {
300 gdb_assert { ![string equal $longest_insn_bytes ""] } \
301 "found the bytes string for a longest instruction"
302 gdb_assert { ![string equal $longest_insn_addr ""] } \
303 "found the address for a longest instruction"
304 }
305 }
306
307 verbose -log "Longest instruction at ${longest_insn_addr} with bytes '${longest_insn_bytes}'"
308
309 # Check that the instruction bytes that we found above consists of
310 # a series of individual bytes separated by a whitespace. Also,
311 # we check that the bytes reported match what can be found in the
312 # inferior memory.
313 set split_bytes [split $longest_insn_bytes " "]
314 set is_bad false
315 set addr $longest_insn_addr
316 set idx 0
317 foreach b $split_bytes {
318 if { [string length $b] != 2 } {
319 set is_bad true
320 }
321
322 # Load the actual byte value from memory, and check it matches
323 # the opcode byte reported in the disassembler output.
324 set addr 0x[format %x [expr $longest_insn_addr + $idx]]
325 set actual [format %02x [mi_get_valueof "/x" "*((unsigned char *) $addr)" "XX"]]
326 gdb_assert [string equal $actual "$b"] \
327 "byte at $addr matches"
328
329 incr idx
330 }
331 gdb_assert { !$is_bad } "check length of each byte"
332 set check_bytes [join $split_bytes " "]
333 gdb_assert { [string equal $check_bytes $longest_insn_bytes] } \
334 "bytes are separated by a single space"
335
336 # Figure out an end address at which to stop the disassembly.
337 set byte_count [llength $split_bytes]
338 set end_addr 0x[format %x [expr $longest_insn_addr + $byte_count]]
339 set start_addr $longest_insn_addr
340
341 verbose -log "Instruction is ${byte_count} bytes, end address ${end_addr}"
342
343 mi_gdb_test "321-data-disassemble -s $start_addr -e $end_addr -- 2" \
344 "321\\^done,asm_insns=\\\[\{address=\"$start_addr\",func-name=\"main\",offset=\"$::decimal\",opcodes=\"$longest_insn_bytes\",inst=\".*\"\}\]" \
345 "data-disassemble checking the opcodes bytes format"
346}
347
b75d55d4
PA
348mi_clean_restart $binfile
349mi_runto_main
fb40c209 350test_disassembly_only
b716877b 351test_disassembly_with_opcodes
fb40c209 352test_disassembly_mixed
b716877b 353test_disassembly_mixed_with_opcodes
fb40c209
AC
354test_disassembly_bogus_args
355test_disassembly_lines_limit
356test_disassembly_mixed_lines_limit
21a52f7d 357test_disassembly_opcode_format
fb40c209
AC
358
359mi_gdb_exit
360return 0