1 # Copyright 1999-2021 Free Software Foundation, Inc.
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.
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.
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/>.
17 # Test Machine interface (MI) operations for disassembly.
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.
23 load_lib mi-support.exp
26 standard_testfile basics.c
28 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
29 untested "failed to compile"
33 proc test_disassembly_only {} {
38 set line_main_head [gdb_get_line_number "main ("]
39 set line_main_body [expr $line_main_head + 2]
41 # Test disassembly more only for the current function.
43 # -data-disassemble -s $pc -e "$pc+8" -- 0
44 # -data-disassemble -a $pc -- 0
45 # -data-disassemble -a callee4 -- 0
46 # -data-disassembly -f basics.c -l $line_main_body -- 0
49 mi_gdb_test "print/x \$pc" "" ""
50 mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 0" \
51 "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
52 "data-disassemble from pc to pc+12 assembly only"
54 mi_gdb_test "112-data-disassemble -a \$pc -- 0" \
55 "112\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
56 "data-disassemble function around pc assembly only"
58 mi_gdb_test "113-data-disassemble -a callee4 -- 0" \
59 "113\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
60 "data-disassemble function callee4 assembly only"
62 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -- 0" \
63 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
64 "data-disassemble file & line, assembly only"
67 proc test_disassembly_with_opcodes {} {
72 set line_main_head [gdb_get_line_number "main ("]
73 set line_main_body [expr $line_main_head + 2]
75 # Test disassembly with opcodes for the current function.
77 # -data-disassemble -s $pc -e "$pc+8" -- 2
78 # -data-disassembly -f basics.c -l $line_main_body -- 2
80 mi_gdb_test "print/x \$pc" "" ""
81 mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 2" \
82 "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\]" \
83 "data-disassemble from pc to pc+12 assembly with opcodes"
85 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -- 2" \
86 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",opcodes=\".*\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]" \
87 "data-disassemble file & line, assembly with opcodes"
90 proc test_disassembly_lines_limit {} {
95 set line_main_head [gdb_get_line_number "main ("]
96 set line_main_body [expr $line_main_head + 2]
98 # Test disassembly more only for the current function.
100 # -data-disassembly -f basics.c -l $line_main_body -n 20 -- 0
101 # -data-disassembly -f basics.c -l $line_main_body -n 0 -- 0
102 # -data-disassembly -f basics.c -l $line_main_body -n 50 -- 0
104 mi_gdb_test "print/x \$pc" "" ""
105 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 20 -- 0" \
106 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
107 "data-disassemble file, line, number assembly only"
109 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 0 -- 0" \
110 "222\\^done,asm_insns=\\\[\\\]" \
111 "data-disassemble file, line, number (zero lines) assembly only"
113 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 50 -- 0" \
114 "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
115 "data-disassemble file, line, number (more than main lines) assembly only"
119 proc test_disassembly_mixed {} {
123 global fullname_syntax
125 set line_callee2_head [gdb_get_line_number "callee2 ("]
126 set line_callee2_open_brace [expr $line_callee2_head + 1]
128 # Test disassembly more only for the current function.
130 # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 1
131 # -data-disassembly -s $pc -e "$pc+8" -- 1
133 mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 1" \
134 "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=\".*\"\}\\\]\}\\\]" \
135 "data-disassemble file, line assembly mixed"
138 # In mixed mode, the lowest level of granularity is the source line.
139 # So we are going to get the disassembly for the source line at
140 # which we are now, even if we have specified that the range is only 2 insns.
142 mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 1" \
143 "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=\".*\"\}\\\]\}\\\]" \
144 "data-disassemble range assembly mixed"
147 proc test_disassembly_mixed_with_opcodes {} {
151 global fullname_syntax
153 set line_callee2_head [gdb_get_line_number "callee2 ("]
154 set line_callee2_open_brace [expr $line_callee2_head + 1]
156 # Test disassembly mixed with opcodes for the current function.
158 # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 3
159 # -data-disassembly -s $pc -e "$pc+8" -- 3
161 mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 3" \
162 "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=\".*\"\}\\\]\}\\\]" \
163 "data-disassemble file, line assembly mixed with opcodes"
166 # In mixed mode, the lowest level of granularity is the source line.
167 # So we are going to get the disassembly for the source line at
168 # which we are now, even if we have specified that the range is only 2 insns.
170 mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 3" \
171 "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=\".*\"\}\\\]\}\\\]" \
172 "data-disassemble range assembly mixed with opcodes"
175 proc test_disassembly_mixed_lines_limit {} {
179 global fullname_syntax
181 set line_main_head [gdb_get_line_number "main ("]
182 set line_main_open_brace [expr $line_main_head + 1]
183 set line_main_body [expr $line_main_head + 2]
185 # Test disassembly more only for the current function.
187 # -data-disassembly -f basics.c -l $line_main_body -n 20 -- 1
188 # -data-disassembly -f basics.c -l $line_main_body -n 0 -- 1
189 # -data-disassembly -f basics.c -l $line_main_body -n 50 -- 1
191 mi_gdb_test "print/x \$pc" "" ""
192 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 20 -- 1" \
193 "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=\".*\"\}\\\]\}\]" \
194 "data-disassemble file, line, number assembly mixed"
196 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 0 -- 1" \
197 "222\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_main_open_brace\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\\\]\}\\\]" \
198 "data-disassemble file, line, number (zero lines) assembly mixed"
200 mi_gdb_test "222-data-disassemble -f basics.c -l $line_main_body -n 50 -- 1" \
201 "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=\".*\"\}\\\]\}\]" \
202 "data-disassemble file, line, number (more than main lines) assembly mixed"
205 proc test_disassembly_bogus_args {} {
209 set line_main_head [gdb_get_line_number "main ("]
210 set line_main_body [expr $line_main_head + 2]
212 # Test that bogus input to disassembly command is rejected.
214 # -data-disassembly -f foo -l abc -n 0 -- 0
215 # -data-disassembly -s foo -e bar -- 0
216 # -data-disassembly -a foo -- 0
217 # -data-disassembly -s $pc -f basics.c -- 0
218 # -data-disassembly -f basics.c -l 32 -- 9
220 mi_gdb_test "123-data-disassemble -f foo -l abc -n 0 -- 0" \
221 "123\\^error,msg=\"-data-disassemble: Invalid filename.\"" \
222 "data-disassemble bogus filename"
224 mi_gdb_test "321-data-disassemble -s foo -e bar -- 0" \
225 "321\\^error,msg=\"No symbol \\\\\"foo\\\\\" in current context.\"" \
226 "data-disassemble bogus address, -s -e"
228 mi_gdb_test "322-data-disassemble -a foo -- 0" \
229 "322\\^error,msg=\"No symbol \\\\\"foo\\\\\" in current context.\"" \
230 "data-disassemble bogus address, -a"
232 mi_gdb_test "456-data-disassemble -s \$pc -f basics.c -- 0" \
233 "456\\^error,msg=\"-data-disassemble: Usage: \\( .-f filename -l linenum .-n howmany.. \\| .-s startaddr -e endaddr. \\| .-a addr. \\) .--. mode.\"" \
234 "data-disassemble mix different args"
236 mi_gdb_test "789-data-disassemble -f basics.c -l $line_main_body -- 9" \
237 "789\\^error,msg=\"-data-disassemble: Mode argument must be in the range 0-5.\"" \
238 "data-disassemble wrong mode arg"
242 mi_clean_restart $binfile
244 test_disassembly_only
245 test_disassembly_with_opcodes
246 test_disassembly_mixed
247 test_disassembly_mixed_with_opcodes
248 test_disassembly_bogus_args
249 test_disassembly_lines_limit
250 test_disassembly_mixed_lines_limit