Sort includes for files gdb/[a-f]*.[chyl].
[external/binutils.git] / gdb / disasm-selftests.c
1 /* Self tests for disassembler for GDB, the GNU debugger.
2
3    Copyright (C) 2017-2019 Free Software Foundation, Inc.
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
22 /* Local non-gdb includes.  */
23 #include "disasm.h"
24
25 #if GDB_SELF_TEST
26 #include "common/selftest.h"
27 #include "selftest-arch.h"
28
29 namespace selftests {
30
31 /* Test disassembly of one instruction.  */
32
33 static void
34 print_one_insn_test (struct gdbarch *gdbarch)
35 {
36   size_t len = 0;
37   const gdb_byte *insn = NULL;
38
39   switch (gdbarch_bfd_arch_info (gdbarch)->arch)
40     {
41     case bfd_arch_bfin:
42       /* M3.L = 0xe117 */
43       static const gdb_byte bfin_insn[] = {0x17, 0xe1, 0xff, 0xff};
44
45       insn = bfin_insn;
46       len = sizeof (bfin_insn);
47       break;
48     case bfd_arch_arm:
49       /* mov     r0, #0 */
50       static const gdb_byte arm_insn[] = {0x0, 0x0, 0xa0, 0xe3};
51
52       insn = arm_insn;
53       len = sizeof (arm_insn);
54       break;
55     case bfd_arch_ia64:
56     case bfd_arch_mep:
57     case bfd_arch_mips:
58     case bfd_arch_tic6x:
59     case bfd_arch_xtensa:
60       return;
61     case bfd_arch_s390:
62       /* nopr %r7 */
63       static const gdb_byte s390_insn[] = {0x07, 0x07};
64
65       insn = s390_insn;
66       len = sizeof (s390_insn);
67       break;
68     case bfd_arch_xstormy16:
69       /* nop */
70       static const gdb_byte xstormy16_insn[] = {0x0, 0x0};
71
72       insn = xstormy16_insn;
73       len = sizeof (xstormy16_insn);
74       break;
75     case bfd_arch_arc:
76       /* PR 21003 */
77       if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_arc_arc601)
78         return;
79       /* fall through */
80     case bfd_arch_nios2:
81     case bfd_arch_score:
82     case bfd_arch_riscv:
83       /* nios2, riscv, and score need to know the current instruction
84          to select breakpoint instruction.  Give the breakpoint
85          instruction kind explicitly.  */
86       {
87         int bplen;
88         insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen);
89         len = bplen;
90       }
91       break;
92     default:
93       {
94         /* Test disassemble breakpoint instruction.  */
95         CORE_ADDR pc = 0;
96         int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, &pc);
97         int bplen;
98
99         insn = gdbarch_sw_breakpoint_from_kind (gdbarch, kind, &bplen);
100         len = bplen;
101
102         break;
103       }
104     }
105   SELF_CHECK (len > 0);
106
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;
111
112   class gdb_disassembler_test : public gdb_disassembler
113   {
114   public:
115
116     explicit gdb_disassembler_test (struct gdbarch *gdbarch,
117                                     const gdb_byte *insn,
118                                     size_t len)
119       : gdb_disassembler (gdbarch,
120                           (verbose ? gdb_stdout : &null_stream),
121                           gdb_disassembler_test::read_memory),
122         m_insn (insn), m_len (len)
123     {
124     }
125
126     int
127     print_insn (CORE_ADDR memaddr)
128     {
129       if (verbose)
130         {
131           fprintf_unfiltered (stream (), "%s ",
132                               gdbarch_bfd_arch_info (arch ())->arch_name);
133         }
134
135       int len = gdb_disassembler::print_insn (memaddr);
136
137       if (verbose)
138         fprintf_unfiltered (stream (), "\n");
139
140       return len;
141     }
142
143   private:
144     /* A buffer contain one instruction.  */
145     const gdb_byte *m_insn;
146
147     /* Length of the buffer.  */
148     size_t m_len;
149
150     static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
151                             unsigned int len, struct disassemble_info *info)
152     {
153       gdb_disassembler_test *self
154         = static_cast<gdb_disassembler_test *>(info->application_data);
155
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];
161
162       return 0;
163     }
164   };
165
166   gdb_disassembler_test di (gdbarch, insn, len);
167
168   SELF_CHECK (di.print_insn (0) == len);
169 }
170
171 /* Test disassembly on memory error.  */
172
173 static void
174 memory_error_test (struct gdbarch *gdbarch)
175 {
176   class gdb_disassembler_test : public gdb_disassembler
177   {
178   public:
179     gdb_disassembler_test (struct gdbarch *gdbarch)
180       : gdb_disassembler (gdbarch, &null_stream,
181                           gdb_disassembler_test::read_memory)
182     {
183     }
184
185     static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
186                             unsigned int len,
187                             struct disassemble_info *info)
188     {
189       /* Always return an error.  */
190       return -1;
191     }
192   };
193
194   gdb_disassembler_test di (gdbarch);
195   bool saw_memory_error = false;
196
197   TRY
198     {
199       di.print_insn (0);
200     }
201   CATCH (ex, RETURN_MASK_ERROR)
202     {
203       if (ex.error == MEMORY_ERROR)
204         saw_memory_error = true;
205     }
206   END_CATCH
207
208   /* Expect MEMORY_ERROR.  */
209   SELF_CHECK (saw_memory_error);
210 }
211
212 } // namespace selftests
213 #endif /* GDB_SELF_TEST */
214
215 void
216 _initialize_disasm_selftests (void)
217 {
218 #if GDB_SELF_TEST
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);
223 #endif
224 }