Disassembly unit test: memory error
authorYao Qi <yao.qi@linaro.org>
Thu, 26 Jan 2017 14:29:19 +0000 (14:29 +0000)
committerYao Qi <yao.qi@linaro.org>
Thu, 26 Jan 2017 14:29:19 +0000 (14:29 +0000)
This patch adds a unit test about memory error occurs on reading
memory, and check MEMORY_ERROR exception is always thrown.

gdb:

2017-01-26  Yao Qi  <yao.qi@linaro.org>

* disasm-selftests.c (memory_error_test): New function.
(_initialize_disasm_selftests): Register memory_error_test.

gdb/ChangeLog
gdb/disasm-selftests.c

index 422697dfddd06c3c941b13502c9df81e81026038..2e8a96fb20bcbb07555d37d2e38ee98a4605844a 100644 (file)
@@ -1,3 +1,8 @@
+2017-01-26  Yao Qi  <yao.qi@linaro.org>
+
+       * disasm-selftests.c (memory_error_test): New function.
+       (_initialize_disasm_selftests): Register memory_error_test.
+
 2017-01-26  Yao Qi  <yao.qi@linaro.org>
 
        * Makefile.in (SFILES): Add disasm-selftests.c and
index 3d68abd037f1d6e1e743e1e0ed8cca39937d2871..c89c5ed83b61423dbc4380fe2a1f1036641e8d45 100644 (file)
@@ -164,6 +164,47 @@ print_one_insn_test (struct gdbarch *gdbarch)
   SELF_CHECK (di.print_insn (0) == len);
 }
 
+/* Test disassembly on memory error.  */
+
+static void
+memory_error_test (struct gdbarch *gdbarch)
+{
+  class gdb_disassembler_test : public gdb_disassembler
+  {
+  public:
+    gdb_disassembler_test (struct gdbarch *gdbarch)
+      : gdb_disassembler (gdbarch, null_stream (),
+                         gdb_disassembler_test::read_memory)
+    {
+    }
+
+    static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
+                           unsigned int len,
+                           struct disassemble_info *info)
+    {
+      /* Always return an error.  */
+      return -1;
+    }
+  };
+
+  gdb_disassembler_test di (gdbarch);
+  bool saw_memory_error = false;
+
+  TRY
+    {
+      di.print_insn (0);
+    }
+  CATCH (ex, RETURN_MASK_ERROR)
+    {
+      if (ex.error == MEMORY_ERROR)
+       saw_memory_error = true;
+    }
+  END_CATCH
+
+  /* Expect MEMORY_ERROR.  */
+  SELF_CHECK (saw_memory_error);
+}
+
 } // namespace selftests
 #endif /* GDB_SELF_TEST */
 
@@ -175,5 +216,6 @@ _initialize_disasm_selftests (void)
 {
 #if GDB_SELF_TEST
   register_self_test_foreach_arch (selftests::print_one_insn_test);
+  register_self_test_foreach_arch (selftests::memory_error_test);
 #endif
 }