1 /* Self tests for disassembler for GDB, the GNU debugger.
3 Copyright (C) 2017-2019 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/>. */
22 /* Local non-gdb includes. */
26 #include "common/selftest.h"
27 #include "selftest-arch.h"
31 /* Test disassembly of one instruction. */
34 print_one_insn_test (struct gdbarch
*gdbarch
)
37 const gdb_byte
*insn
= NULL
;
39 switch (gdbarch_bfd_arch_info (gdbarch
)->arch
)
43 static const gdb_byte bfin_insn
[] = {0x17, 0xe1, 0xff, 0xff};
46 len
= sizeof (bfin_insn
);
50 static const gdb_byte arm_insn
[] = {0x0, 0x0, 0xa0, 0xe3};
53 len
= sizeof (arm_insn
);
63 static const gdb_byte s390_insn
[] = {0x07, 0x07};
66 len
= sizeof (s390_insn
);
68 case bfd_arch_xstormy16
:
70 static const gdb_byte xstormy16_insn
[] = {0x0, 0x0};
72 insn
= xstormy16_insn
;
73 len
= sizeof (xstormy16_insn
);
77 if (gdbarch_bfd_arch_info (gdbarch
)->mach
== bfd_mach_arc_arc601
)
83 /* nios2, riscv, and score need to know the current instruction
84 to select breakpoint instruction. Give the breakpoint
85 instruction kind explicitly. */
88 insn
= gdbarch_sw_breakpoint_from_kind (gdbarch
, 4, &bplen
);
94 /* Test disassemble breakpoint instruction. */
96 int kind
= gdbarch_breakpoint_kind_from_pc (gdbarch
, &pc
);
99 insn
= gdbarch_sw_breakpoint_from_kind (gdbarch
, kind
, &bplen
);
105 SELF_CHECK (len
> 0);
107 /* Test gdb_disassembler for a given gdbarch by reading data from a
108 pre-allocated buffer. If you want to see the disassembled
109 instruction printed to gdb_stdout, set verbose to true. */
110 static const bool verbose
= false;
112 class gdb_disassembler_test
: public gdb_disassembler
116 explicit gdb_disassembler_test (struct gdbarch
*gdbarch
,
117 const gdb_byte
*insn
,
119 : gdb_disassembler (gdbarch
,
120 (verbose
? gdb_stdout
: &null_stream
),
121 gdb_disassembler_test::read_memory
),
122 m_insn (insn
), m_len (len
)
127 print_insn (CORE_ADDR memaddr
)
131 fprintf_unfiltered (stream (), "%s ",
132 gdbarch_bfd_arch_info (arch ())->arch_name
);
135 int len
= gdb_disassembler::print_insn (memaddr
);
138 fprintf_unfiltered (stream (), "\n");
144 /* A buffer contain one instruction. */
145 const gdb_byte
*m_insn
;
147 /* Length of the buffer. */
150 static int read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
151 unsigned int len
, struct disassemble_info
*info
)
153 gdb_disassembler_test
*self
154 = static_cast<gdb_disassembler_test
*>(info
->application_data
);
156 /* The disassembler in opcodes may read more data than one
157 instruction. Supply infinite consecutive copies
158 of the same instruction. */
159 for (size_t i
= 0; i
< len
; i
++)
160 myaddr
[i
] = self
->m_insn
[(memaddr
+ i
) % self
->m_len
];
166 gdb_disassembler_test
di (gdbarch
, insn
, len
);
168 SELF_CHECK (di
.print_insn (0) == len
);
171 /* Test disassembly on memory error. */
174 memory_error_test (struct gdbarch
*gdbarch
)
176 class gdb_disassembler_test
: public gdb_disassembler
179 gdb_disassembler_test (struct gdbarch
*gdbarch
)
180 : gdb_disassembler (gdbarch
, &null_stream
,
181 gdb_disassembler_test::read_memory
)
185 static int read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
187 struct disassemble_info
*info
)
189 /* Always return an error. */
194 gdb_disassembler_test
di (gdbarch
);
195 bool saw_memory_error
= false;
201 CATCH (ex
, RETURN_MASK_ERROR
)
203 if (ex
.error
== MEMORY_ERROR
)
204 saw_memory_error
= true;
208 /* Expect MEMORY_ERROR. */
209 SELF_CHECK (saw_memory_error
);
212 } // namespace selftests
213 #endif /* GDB_SELF_TEST */
216 _initialize_disasm_selftests (void)
219 selftests::register_test_foreach_arch ("print_one_insn",
220 selftests::print_one_insn_test
);
221 selftests::register_test_foreach_arch ("memory_error",
222 selftests::memory_error_test
);
This page took 0.037196 seconds and 5 git commands to generate.