1 /* Self tests for disassembler for GDB, the GNU debugger.
3 Copyright (C) 2017 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/>. */
25 #include "selftest-arch.h"
29 /* Test disassembly of one instruction. */
32 print_one_insn_test (struct gdbarch *gdbarch)
35 const gdb_byte *insn = NULL;
37 switch (gdbarch_bfd_arch_info (gdbarch)->arch)
41 static const gdb_byte bfin_insn[] = {0x17, 0xe1, 0xff, 0xff};
44 len = sizeof (bfin_insn);
48 static const gdb_byte arm_insn[] = {0x0, 0x0, 0xa0, 0xe3};
51 len = sizeof (arm_insn);
61 static const gdb_byte s390_insn[] = {0x07, 0x07};
64 len = sizeof (s390_insn);
66 case bfd_arch_xstormy16:
68 static const gdb_byte xstormy16_insn[] = {0x0, 0x0};
70 insn = xstormy16_insn;
71 len = sizeof (xstormy16_insn);
75 if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_arc_arc601)
80 /* nios2 and score need to know the current instruction to select
81 breakpoint instruction. Give the breakpoint instruction kind
84 insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen);
89 /* Test disassemble breakpoint instruction. */
91 int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, &pc);
94 insn = gdbarch_sw_breakpoint_from_kind (gdbarch, kind, &bplen);
100 SELF_CHECK (len > 0);
102 /* Test gdb_disassembler for a given gdbarch by reading data from a
103 pre-allocated buffer. If you want to see the disassembled
104 instruction printed to gdb_stdout, set verbose to true. */
105 static const bool verbose = false;
107 class gdb_disassembler_test : public gdb_disassembler
111 explicit gdb_disassembler_test (struct gdbarch *gdbarch,
112 const gdb_byte *insn,
114 : gdb_disassembler (gdbarch,
115 (verbose ? gdb_stdout : &null_stream),
116 gdb_disassembler_test::read_memory),
117 m_insn (insn), m_len (len)
122 print_insn (CORE_ADDR memaddr)
126 fprintf_unfiltered (stream (), "%s ",
127 gdbarch_bfd_arch_info (arch ())->arch_name);
130 int len = gdb_disassembler::print_insn (memaddr);
133 fprintf_unfiltered (stream (), "\n");
139 /* A buffer contain one instruction. */
140 const gdb_byte *m_insn;
142 /* Length of the buffer. */
145 static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
146 unsigned int len, struct disassemble_info *info)
148 gdb_disassembler_test *self
149 = static_cast<gdb_disassembler_test *>(info->application_data);
151 /* The disassembler in opcodes may read more data than one
152 instruction. Supply infinite consecutive copies
153 of the same instruction. */
154 for (size_t i = 0; i < len; i++)
155 myaddr[i] = self->m_insn[(memaddr + i) % self->m_len];
161 gdb_disassembler_test di (gdbarch, insn, len);
163 SELF_CHECK (di.print_insn (0) == len);
166 /* Test disassembly on memory error. */
169 memory_error_test (struct gdbarch *gdbarch)
171 class gdb_disassembler_test : public gdb_disassembler
174 gdb_disassembler_test (struct gdbarch *gdbarch)
175 : gdb_disassembler (gdbarch, &null_stream,
176 gdb_disassembler_test::read_memory)
180 static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
182 struct disassemble_info *info)
184 /* Always return an error. */
189 gdb_disassembler_test di (gdbarch);
190 bool saw_memory_error = false;
196 CATCH (ex, RETURN_MASK_ERROR)
198 if (ex.error == MEMORY_ERROR)
199 saw_memory_error = true;
203 /* Expect MEMORY_ERROR. */
204 SELF_CHECK (saw_memory_error);
207 } // namespace selftests
208 #endif /* GDB_SELF_TEST */
211 _initialize_disasm_selftests (void)
214 selftests::register_test_foreach_arch ("print_one_insn",
215 selftests::print_one_insn_test);
216 selftests::register_test_foreach_arch ("memory_error",
217 selftests::memory_error_test);