xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.mi/mi-disassemble.exp (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1# Copyright 1999-2020 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#
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
24set MIFLAGS "-i=mi"
25
26gdb_exit
27if [mi_gdb_start] {
28    continue
29}
30
31standard_testfile basics.c
32
33if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34     untested "failed to compile"
35     return -1
36}
37
38proc test_disassembly_only {} {
39    global mi_gdb_prompt
40    global hex
41    global decimal
42
43    set line_main_head [gdb_get_line_number "main ("]
44    set line_main_body [expr $line_main_head + 2]
45
46    # Test disassembly more only for the current function.
47    # Tests:
48    # -data-disassemble -s $pc -e "$pc+8" -- 0
49    # -data-disassemble -a $pc -- 0
50    # -data-disassemble -a callee4 -- 0
51    # -data-disassembly -f basics.c -l $line_main_body -- 0
52
53
54    mi_gdb_test "print/x \$pc" "" ""
55    mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 0" \
56	    "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
57             "data-disassemble from pc to pc+12 assembly only"
58
59    mi_gdb_test "112-data-disassemble -a \$pc -- 0" \
60	    "112\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
61             "data-disassemble function around pc assembly only"
62
63    mi_gdb_test "113-data-disassemble -a callee4 -- 0" \
64	    "113\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\},\{address=\"$hex\",func-name=\"callee4\",offset=\"$decimal\",inst=\".*\"\}.*\]" \
65             "data-disassemble function callee4 assembly only"
66
67    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -- 0" \
68	    "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
69              "data-disassemble file & line, assembly only"
70}
71
72proc test_disassembly_with_opcodes {} {
73    global mi_gdb_prompt
74    global hex
75    global decimal
76
77    set line_main_head [gdb_get_line_number "main ("]
78    set line_main_body [expr $line_main_head + 2]
79
80    # Test disassembly with opcodes for the current function.
81    # Tests:
82    # -data-disassemble -s $pc -e "$pc+8" -- 2
83    # -data-disassembly -f basics.c -l $line_main_body -- 2
84
85    mi_gdb_test "print/x \$pc" "" ""
86    mi_gdb_test "111-data-disassemble -s \$pc -e \"\$pc + 12\" -- 2" \
87           "111\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\},\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}.*\]" \
88             "data-disassemble from pc to pc+12 assembly with opcodes"
89
90    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -- 2" \
91           "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",opcodes=\".*\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",opcodes=\".*\",inst=\".*\"\}\\\]" \
92              "data-disassemble file & line, assembly with opcodes"
93}
94
95proc test_disassembly_lines_limit {} {
96    global mi_gdb_prompt
97    global hex
98    global decimal
99
100    set line_main_head [gdb_get_line_number "main ("]
101    set line_main_body [expr $line_main_head + 2]
102
103    # Test disassembly more only for the current function.
104    # Tests:
105    # -data-disassembly -f basics.c -l $line_main_body -n 20 -- 0
106    # -data-disassembly -f basics.c -l $line_main_body -n 0 -- 0
107    # -data-disassembly -f basics.c -l $line_main_body -n 50 -- 0
108
109    mi_gdb_test "print/x \$pc" "" ""
110    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -n 20 -- 0" \
111	    "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
112              "data-disassemble file, line, number assembly only"
113
114    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -n 0 -- 0" \
115	    "222\\^done,asm_insns=\\\[\\\]" \
116              "data-disassemble file, line, number (zero lines) assembly only"
117
118    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -n 50 -- 0" \
119	    "222\\^done,asm_insns=\\\[\{address=\"$hex\",func-name=\"main\",offset=\"0\",inst=\".*\"\},.*,\{address=\"$hex\",func-name=\"main\",offset=\"$decimal\",inst=\".*\"\}\\\]" \
120              "data-disassemble file, line, number (more than main lines) assembly only"
121}
122
123
124proc test_disassembly_mixed {} {
125    global mi_gdb_prompt
126    global hex
127    global decimal
128    global fullname_syntax
129
130    set line_callee2_head       [gdb_get_line_number "callee2 ("]
131    set line_callee2_open_brace [expr $line_callee2_head + 1]
132
133    # Test disassembly more only for the current function.
134    # Tests:
135    # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 1
136    # -data-disassembly -s $pc -e "$pc+8" -- 1
137
138    mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 1" \
139	    "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=\".*\"\}\\\]\}\\\]" \
140             "data-disassemble file, line assembly mixed"
141
142    #
143    # In mixed mode, the lowest level of granularity is the source line.
144    # So we are going to get the disassembly for the source line at
145    # which we are now, even if we have specified that the range is only 2 insns.
146    #
147    mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 1" \
148	    "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=\".*\"\}\\\]\}\\\]" \
149             "data-disassemble range assembly mixed"
150}
151
152proc test_disassembly_mixed_with_opcodes {} {
153    global mi_gdb_prompt
154    global hex
155    global decimal
156    global fullname_syntax
157
158    set line_callee2_head       [gdb_get_line_number "callee2 ("]
159    set line_callee2_open_brace [expr $line_callee2_head + 1]
160
161    # Test disassembly mixed with opcodes for the current function.
162    # Tests:
163    # -data-disassembly -f basics.c -l $line_callee2_open_brace -- 3
164    # -data-disassembly -s $pc -e "$pc+8" -- 3
165
166    mi_gdb_test "002-data-disassemble -f basics.c -l $line_callee2_open_brace -- 3" \
167           "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=\".*\"\}\\\]\}\\\]" \
168             "data-disassemble file, line assembly mixed with opcodes"
169
170    #
171    # In mixed mode, the lowest level of granularity is the source line.
172    # So we are going to get the disassembly for the source line at
173    # which we are now, even if we have specified that the range is only 2 insns.
174    #
175    mi_gdb_test "003-data-disassemble -s \$pc -e \"\$pc+4\" -- 3" \
176           "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=\".*\"\}\\\]\}\\\]" \
177             "data-disassemble range assembly mixed with opcodes"
178}
179
180proc test_disassembly_mixed_lines_limit {} {
181    global mi_gdb_prompt
182    global hex
183    global decimal
184    global fullname_syntax
185
186    set line_main_head       [gdb_get_line_number "main ("]
187    set line_main_open_brace [expr $line_main_head + 1]
188    set line_main_body       [expr $line_main_head + 2]
189
190    # Test disassembly more only for the current function.
191    # Tests:
192    # -data-disassembly -f basics.c -l $line_main_body -n 20 -- 1
193    # -data-disassembly -f basics.c -l $line_main_body -n 0 -- 1
194    # -data-disassembly -f basics.c -l $line_main_body -n 50 -- 1
195
196    mi_gdb_test "print/x \$pc" "" ""
197    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -n 20 -- 1" \
198	    "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=\".*\"\}\\\]\}\]" \
199              "data-disassemble file, line, number assembly mixed"
200
201    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -n 0 -- 1" \
202	    "222\\^done,asm_insns=\\\[src_and_asm_line=\{line=\"$line_main_open_brace\",file=\".*basics.c\",fullname=\"${fullname_syntax}basics.c\",line_asm_insn=\\\[\\\]\}\\\]" \
203              "data-disassemble file, line, number (zero lines) assembly mixed"
204
205    mi_gdb_test "222-data-disassemble  -f basics.c -l $line_main_body -n 50 -- 1" \
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=\".*\"\}\\\]\}\]" \
207              "data-disassemble file, line, number (more than main lines) assembly mixed"
208}
209
210proc test_disassembly_bogus_args {} {
211    global mi_gdb_prompt
212    global hex
213
214    set line_main_head [gdb_get_line_number "main ("]
215    set line_main_body [expr $line_main_head + 2]
216
217    # Test that bogus input to disassembly command is rejected.
218    # Tests:
219    # -data-disassembly -f foo -l abc -n 0 -- 0
220    # -data-disassembly -s foo -e bar -- 0
221    # -data-disassembly -a foo -- 0
222    # -data-disassembly -s $pc -f basics.c -- 0
223    # -data-disassembly -f basics.c -l 32 -- 9
224
225    mi_gdb_test "123-data-disassemble -f foo -l abc -n 0 -- 0" \
226             "123\\^error,msg=\"-data-disassemble: Invalid filename.\"" \
227             "data-disassemble bogus filename"
228
229    mi_gdb_test "321-data-disassemble -s foo -e bar -- 0" \
230             "321\\^error,msg=\"No symbol \\\\\"foo\\\\\" in current context.\"" \
231             "data-disassemble bogus address, -s -e"
232
233    mi_gdb_test "322-data-disassemble -a foo -- 0" \
234             "322\\^error,msg=\"No symbol \\\\\"foo\\\\\" in current context.\"" \
235             "data-disassemble bogus address, -a"
236
237    mi_gdb_test "456-data-disassemble -s \$pc -f basics.c -- 0" \
238             "456\\^error,msg=\"-data-disassemble: Usage: \\( .-f filename -l linenum .-n howmany.. \\| .-s startaddr -e endaddr. \\| .-a addr. \\) .--. mode.\"" \
239             "data-disassemble mix different args"
240
241    mi_gdb_test "789-data-disassemble -f basics.c -l $line_main_body -- 9" \
242             "789\\^error,msg=\"-data-disassemble: Mode argument must be in the range 0-5.\"" \
243             "data-disassemble wrong mode arg"
244
245}
246
247mi_run_to_main
248test_disassembly_only
249test_disassembly_with_opcodes
250test_disassembly_mixed
251test_disassembly_mixed_with_opcodes
252test_disassembly_bogus_args
253test_disassembly_lines_limit
254test_disassembly_mixed_lines_limit
255
256mi_gdb_exit
257return 0
258