Commit | Line | Data |
---|---|---|
79843d45 YQ |
1 | /* Self tests for disassembler for GDB, the GNU debugger. |
2 | ||
42a4f53d | 3 | Copyright (C) 2017-2019 Free Software Foundation, Inc. |
79843d45 YQ |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "disasm.h" | |
22 | ||
23 | #if GDB_SELF_TEST | |
0747795c | 24 | #include "common/selftest.h" |
79843d45 YQ |
25 | #include "selftest-arch.h" |
26 | ||
27 | namespace selftests { | |
28 | ||
29 | /* Test disassembly of one instruction. */ | |
30 | ||
31 | static void | |
32 | print_one_insn_test (struct gdbarch *gdbarch) | |
33 | { | |
34 | size_t len = 0; | |
35 | const gdb_byte *insn = NULL; | |
36 | ||
37 | switch (gdbarch_bfd_arch_info (gdbarch)->arch) | |
38 | { | |
39 | case bfd_arch_bfin: | |
40 | /* M3.L = 0xe117 */ | |
41 | static const gdb_byte bfin_insn[] = {0x17, 0xe1, 0xff, 0xff}; | |
42 | ||
43 | insn = bfin_insn; | |
44 | len = sizeof (bfin_insn); | |
45 | break; | |
46 | case bfd_arch_arm: | |
47 | /* mov r0, #0 */ | |
48 | static const gdb_byte arm_insn[] = {0x0, 0x0, 0xa0, 0xe3}; | |
49 | ||
50 | insn = arm_insn; | |
51 | len = sizeof (arm_insn); | |
52 | break; | |
53 | case bfd_arch_ia64: | |
54 | case bfd_arch_mep: | |
55 | case bfd_arch_mips: | |
56 | case bfd_arch_tic6x: | |
57 | case bfd_arch_xtensa: | |
58 | return; | |
59 | case bfd_arch_s390: | |
60 | /* nopr %r7 */ | |
61 | static const gdb_byte s390_insn[] = {0x07, 0x07}; | |
62 | ||
63 | insn = s390_insn; | |
64 | len = sizeof (s390_insn); | |
65 | break; | |
66 | case bfd_arch_xstormy16: | |
67 | /* nop */ | |
68 | static const gdb_byte xstormy16_insn[] = {0x0, 0x0}; | |
69 | ||
70 | insn = xstormy16_insn; | |
71 | len = sizeof (xstormy16_insn); | |
72 | break; | |
73 | case bfd_arch_arc: | |
74 | /* PR 21003 */ | |
75 | if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_arc_arc601) | |
76 | return; | |
77 | /* fall through */ | |
78 | case bfd_arch_nios2: | |
79 | case bfd_arch_score: | |
f37bc8b1 JB |
80 | case bfd_arch_riscv: |
81 | /* nios2, riscv, and score need to know the current instruction | |
82 | to select breakpoint instruction. Give the breakpoint | |
83 | instruction kind explicitly. */ | |
b926417a TT |
84 | { |
85 | int bplen; | |
86 | insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen); | |
87 | len = bplen; | |
88 | } | |
79843d45 YQ |
89 | break; |
90 | default: | |
91 | { | |
92 | /* Test disassemble breakpoint instruction. */ | |
93 | CORE_ADDR pc = 0; | |
94 | int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, &pc); | |
95 | int bplen; | |
96 | ||
97 | insn = gdbarch_sw_breakpoint_from_kind (gdbarch, kind, &bplen); | |
98 | len = bplen; | |
99 | ||
100 | break; | |
101 | } | |
102 | } | |
103 | SELF_CHECK (len > 0); | |
104 | ||
105 | /* Test gdb_disassembler for a given gdbarch by reading data from a | |
106 | pre-allocated buffer. If you want to see the disassembled | |
107 | instruction printed to gdb_stdout, set verbose to true. */ | |
b1ace6bd | 108 | static const bool verbose = false; |
79843d45 YQ |
109 | |
110 | class gdb_disassembler_test : public gdb_disassembler | |
111 | { | |
112 | public: | |
113 | ||
79843d45 YQ |
114 | explicit gdb_disassembler_test (struct gdbarch *gdbarch, |
115 | const gdb_byte *insn, | |
116 | size_t len) | |
117 | : gdb_disassembler (gdbarch, | |
d7e74731 | 118 | (verbose ? gdb_stdout : &null_stream), |
79843d45 YQ |
119 | gdb_disassembler_test::read_memory), |
120 | m_insn (insn), m_len (len) | |
121 | { | |
122 | } | |
123 | ||
124 | int | |
125 | print_insn (CORE_ADDR memaddr) | |
126 | { | |
127 | if (verbose) | |
128 | { | |
129 | fprintf_unfiltered (stream (), "%s ", | |
130 | gdbarch_bfd_arch_info (arch ())->arch_name); | |
131 | } | |
132 | ||
133 | int len = gdb_disassembler::print_insn (memaddr); | |
134 | ||
135 | if (verbose) | |
136 | fprintf_unfiltered (stream (), "\n"); | |
137 | ||
138 | return len; | |
139 | } | |
140 | ||
141 | private: | |
142 | /* A buffer contain one instruction. */ | |
143 | const gdb_byte *m_insn; | |
144 | ||
145 | /* Length of the buffer. */ | |
146 | size_t m_len; | |
147 | ||
148 | static int read_memory (bfd_vma memaddr, gdb_byte *myaddr, | |
149 | unsigned int len, struct disassemble_info *info) | |
150 | { | |
151 | gdb_disassembler_test *self | |
152 | = static_cast<gdb_disassembler_test *>(info->application_data); | |
153 | ||
154 | /* The disassembler in opcodes may read more data than one | |
155 | instruction. Supply infinite consecutive copies | |
156 | of the same instruction. */ | |
157 | for (size_t i = 0; i < len; i++) | |
158 | myaddr[i] = self->m_insn[(memaddr + i) % self->m_len]; | |
159 | ||
160 | return 0; | |
161 | } | |
162 | }; | |
163 | ||
164 | gdb_disassembler_test di (gdbarch, insn, len); | |
165 | ||
166 | SELF_CHECK (di.print_insn (0) == len); | |
167 | } | |
168 | ||
658ca58c YQ |
169 | /* Test disassembly on memory error. */ |
170 | ||
171 | static void | |
172 | memory_error_test (struct gdbarch *gdbarch) | |
173 | { | |
174 | class gdb_disassembler_test : public gdb_disassembler | |
175 | { | |
176 | public: | |
177 | gdb_disassembler_test (struct gdbarch *gdbarch) | |
d7e74731 | 178 | : gdb_disassembler (gdbarch, &null_stream, |
658ca58c YQ |
179 | gdb_disassembler_test::read_memory) |
180 | { | |
181 | } | |
182 | ||
183 | static int read_memory (bfd_vma memaddr, gdb_byte *myaddr, | |
184 | unsigned int len, | |
185 | struct disassemble_info *info) | |
186 | { | |
187 | /* Always return an error. */ | |
188 | return -1; | |
189 | } | |
190 | }; | |
191 | ||
192 | gdb_disassembler_test di (gdbarch); | |
193 | bool saw_memory_error = false; | |
194 | ||
195 | TRY | |
196 | { | |
197 | di.print_insn (0); | |
198 | } | |
199 | CATCH (ex, RETURN_MASK_ERROR) | |
200 | { | |
201 | if (ex.error == MEMORY_ERROR) | |
202 | saw_memory_error = true; | |
203 | } | |
204 | END_CATCH | |
205 | ||
206 | /* Expect MEMORY_ERROR. */ | |
207 | SELF_CHECK (saw_memory_error); | |
208 | } | |
209 | ||
79843d45 YQ |
210 | } // namespace selftests |
211 | #endif /* GDB_SELF_TEST */ | |
212 | ||
79843d45 YQ |
213 | void |
214 | _initialize_disasm_selftests (void) | |
215 | { | |
216 | #if GDB_SELF_TEST | |
1526853e SM |
217 | selftests::register_test_foreach_arch ("print_one_insn", |
218 | selftests::print_one_insn_test); | |
219 | selftests::register_test_foreach_arch ("memory_error", | |
220 | selftests::memory_error_test); | |
79843d45 YQ |
221 | #endif |
222 | } |