Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / disasm-selftests.c
blob928d26f7018e49906d5572eae8cf3dd1a916f466
1 /* Self tests for disassembler for GDB, the GNU debugger.
3 Copyright (C) 2017-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "disasm.h"
22 #include "gdbsupport/selftest.h"
23 #include "selftest-arch.h"
24 #include "gdbarch.h"
26 namespace selftests {
28 /* Test disassembly of one instruction. */
30 static void
31 print_one_insn_test (struct gdbarch *gdbarch)
33 size_t len = 0;
34 const gdb_byte *insn = NULL;
36 switch (gdbarch_bfd_arch_info (gdbarch)->arch)
38 case bfd_arch_bfin:
39 /* M3.L = 0xe117 */
40 static const gdb_byte bfin_insn[] = {0x17, 0xe1, 0xff, 0xff};
42 insn = bfin_insn;
43 len = sizeof (bfin_insn);
44 break;
45 case bfd_arch_arm:
46 /* mov r0, #0 */
47 static const gdb_byte arm_insn[] = {0x0, 0x0, 0xa0, 0xe3};
49 insn = arm_insn;
50 len = sizeof (arm_insn);
51 break;
52 case bfd_arch_ia64:
53 case bfd_arch_mep:
54 case bfd_arch_mips:
55 case bfd_arch_tic6x:
56 case bfd_arch_xtensa:
57 return;
58 case bfd_arch_s390:
59 /* nopr %r7 */
60 static const gdb_byte s390_insn[] = {0x07, 0x07};
62 insn = s390_insn;
63 len = sizeof (s390_insn);
64 break;
65 case bfd_arch_xstormy16:
66 /* nop */
67 static const gdb_byte xstormy16_insn[] = {0x0, 0x0};
69 insn = xstormy16_insn;
70 len = sizeof (xstormy16_insn);
71 break;
72 case bfd_arch_nios2:
73 case bfd_arch_score:
74 case bfd_arch_riscv:
75 /* nios2, riscv, and score need to know the current instruction
76 to select breakpoint instruction. Give the breakpoint
77 instruction kind explicitly. */
79 int bplen;
80 insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen);
81 len = bplen;
83 break;
84 case bfd_arch_arc:
85 /* PR 21003 */
86 if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_arc_arc601)
87 return;
88 goto generic_case;
89 case bfd_arch_i386:
91 const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
92 /* The disassembly tests will fail on x86-linux because
93 opcodes rejects an attempt to disassemble for an arch with
94 a 64-bit address size when bfd_vma is 32-bit. */
95 if (info->bits_per_address > sizeof (bfd_vma) * CHAR_BIT)
96 return;
98 /* fall through */
99 default:
100 generic_case:
102 /* Test disassemble breakpoint instruction. */
103 CORE_ADDR pc = 0;
104 int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, &pc);
105 int bplen;
107 insn = gdbarch_sw_breakpoint_from_kind (gdbarch, kind, &bplen);
108 len = bplen;
110 break;
113 SELF_CHECK (len > 0);
115 /* Test gdb_disassembler for a given gdbarch by reading data from a
116 pre-allocated buffer. If you want to see the disassembled
117 instruction printed to gdb_stdout, use maint selftest -verbose. */
119 class gdb_disassembler_test : public gdb_disassembler
121 public:
123 explicit gdb_disassembler_test (struct gdbarch *gdbarch,
124 const gdb_byte *insn,
125 size_t len)
126 : gdb_disassembler (gdbarch,
127 (run_verbose () ? gdb_stdout : &null_stream),
128 gdb_disassembler_test::read_memory),
129 m_insn (insn), m_len (len)
134 print_insn (CORE_ADDR memaddr)
136 if (run_verbose ())
138 gdb_printf (stream (), "%s ",
139 gdbarch_bfd_arch_info (arch ())->arch_name);
142 int len = gdb_disassembler::print_insn (memaddr);
144 if (run_verbose ())
145 gdb_printf (stream (), "\n");
147 return len;
150 private:
151 /* A buffer contain one instruction. */
152 const gdb_byte *m_insn;
154 /* Length of the buffer. */
155 size_t m_len;
157 static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
158 unsigned int len, struct disassemble_info *info)
160 gdb_disassembler_test *self
161 = static_cast<gdb_disassembler_test *>(info->application_data);
163 /* The disassembler in opcodes may read more data than one
164 instruction. Supply infinite consecutive copies
165 of the same instruction. */
166 for (size_t i = 0; i < len; i++)
167 myaddr[i] = self->m_insn[(memaddr + i) % self->m_len];
169 return 0;
173 gdb_disassembler_test di (gdbarch, insn, len);
175 SELF_CHECK (di.print_insn (0) == len);
178 /* Test disassembly on memory error. */
180 static void
181 memory_error_test (struct gdbarch *gdbarch)
183 class gdb_disassembler_test : public gdb_disassembler
185 public:
186 gdb_disassembler_test (struct gdbarch *gdbarch)
187 : gdb_disassembler (gdbarch, &null_stream,
188 gdb_disassembler_test::read_memory)
192 static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
193 unsigned int len,
194 struct disassemble_info *info)
196 /* Always return an error. */
197 return -1;
201 if (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_i386)
203 const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
204 /* This test will fail on x86-linux because opcodes rejects an
205 attempt to disassemble for an arch with a 64-bit address size
206 when bfd_vma is 32-bit. */
207 if (info->bits_per_address > sizeof (bfd_vma) * CHAR_BIT)
208 return;
211 gdb_disassembler_test di (gdbarch);
212 bool saw_memory_error = false;
216 di.print_insn (0);
218 catch (const gdb_exception_error &ex)
220 if (ex.error == MEMORY_ERROR)
221 saw_memory_error = true;
224 /* Expect MEMORY_ERROR. */
225 SELF_CHECK (saw_memory_error);
228 } // namespace selftests
230 void _initialize_disasm_selftests ();
231 void
232 _initialize_disasm_selftests ()
234 selftests::register_test_foreach_arch ("print_one_insn",
235 selftests::print_one_insn_test);
236 selftests::register_test_foreach_arch ("memory_error",
237 selftests::memory_error_test);