0a383d6b79590522514700e32430e9e7392107a3
1 /* Self tests for disassembler for GDB, the GNU debugger.
3 Copyright (C) 2017-2021 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 #include "gdbsupport/selftest.h"
23 #include "selftest-arch.h"
28 /* Test disassembly of one instruction. */
31 print_one_insn_test (struct gdbarch
*gdbarch
)
34 const gdb_byte
*insn
= NULL
;
36 switch (gdbarch_bfd_arch_info (gdbarch
)->arch
)
40 static const gdb_byte bfin_insn
[] = {0x17, 0xe1, 0xff, 0xff};
43 len
= sizeof (bfin_insn
);
47 static const gdb_byte arm_insn
[] = {0x0, 0x0, 0xa0, 0xe3};
50 len
= sizeof (arm_insn
);
60 static const gdb_byte s390_insn
[] = {0x07, 0x07};
63 len
= sizeof (s390_insn
);
65 case bfd_arch_xstormy16
:
67 static const gdb_byte xstormy16_insn
[] = {0x0, 0x0};
69 insn
= xstormy16_insn
;
70 len
= sizeof (xstormy16_insn
);
75 /* nios2, riscv, and score need to know the current instruction
76 to select breakpoint instruction. Give the breakpoint
77 instruction kind explicitly. */
80 insn
= gdbarch_sw_breakpoint_from_kind (gdbarch
, 4, &bplen
);
86 if (gdbarch_bfd_arch_info (gdbarch
)->mach
== bfd_mach_arc_arc601
)
91 /* Test disassemble breakpoint instruction. */
93 int kind
= gdbarch_breakpoint_kind_from_pc (gdbarch
, &pc
);
96 insn
= gdbarch_sw_breakpoint_from_kind (gdbarch
, kind
, &bplen
);
102 SELF_CHECK (len
> 0);
104 /* Test gdb_disassembler for a given gdbarch by reading data from a
105 pre-allocated buffer. If you want to see the disassembled
106 instruction printed to gdb_stdout, use maint selftest -verbose. */
108 class gdb_disassembler_test
: public gdb_disassembler
112 explicit gdb_disassembler_test (struct gdbarch
*gdbarch
,
113 const gdb_byte
*insn
,
115 : gdb_disassembler (gdbarch
,
116 (run_verbose () ? gdb_stdout
: &null_stream
),
117 gdb_disassembler_test::read_memory
),
118 m_insn (insn
), m_len (len
)
123 print_insn (CORE_ADDR memaddr
)
127 fprintf_unfiltered (stream (), "%s ",
128 gdbarch_bfd_arch_info (arch ())->arch_name
);
131 int len
= gdb_disassembler::print_insn (memaddr
);
134 fprintf_unfiltered (stream (), "\n");
140 /* A buffer contain one instruction. */
141 const gdb_byte
*m_insn
;
143 /* Length of the buffer. */
146 static int read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
147 unsigned int len
, struct disassemble_info
*info
)
149 gdb_disassembler_test
*self
150 = static_cast<gdb_disassembler_test
*>(info
->application_data
);
152 /* The disassembler in opcodes may read more data than one
153 instruction. Supply infinite consecutive copies
154 of the same instruction. */
155 for (size_t i
= 0; i
< len
; i
++)
156 myaddr
[i
] = self
->m_insn
[(memaddr
+ i
) % self
->m_len
];
162 gdb_disassembler_test
di (gdbarch
, insn
, len
);
164 SELF_CHECK (di
.print_insn (0) == len
);
167 /* Test disassembly on memory error. */
170 memory_error_test (struct gdbarch
*gdbarch
)
172 class gdb_disassembler_test
: public gdb_disassembler
175 gdb_disassembler_test (struct gdbarch
*gdbarch
)
176 : gdb_disassembler (gdbarch
, &null_stream
,
177 gdb_disassembler_test::read_memory
)
181 static int read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
183 struct disassemble_info
*info
)
185 /* Always return an error. */
190 gdb_disassembler_test
di (gdbarch
);
191 bool saw_memory_error
= false;
197 catch (const gdb_exception_error
&ex
)
199 if (ex
.error
== MEMORY_ERROR
)
200 saw_memory_error
= true;
203 /* Expect MEMORY_ERROR. */
204 SELF_CHECK (saw_memory_error
);
207 } // namespace selftests
209 void _initialize_disasm_selftests ();
211 _initialize_disasm_selftests ()
213 selftests::register_test_foreach_arch ("print_one_insn",
214 selftests::print_one_insn_test
);
215 selftests::register_test_foreach_arch ("memory_error",
216 selftests::memory_error_test
);