From 728a79135f51a1c20719ebaf3c98446d9ee248db Mon Sep 17 00:00:00 2001 From: Yao Qi Date: Thu, 16 Mar 2017 16:35:18 +0000 Subject: Add instruction_reader to arm process record This patch adds an abstract class abstract_memory_reader a and pass it to the code reading instructions in arm process record, rather than using target_read_memory to read from real target. This paves the way for adding more unit tests to arm process record. gdb: 2017-03-16 Yao Qi * arm-tdep.c (abstract_memory_reader): New class. (instruction_reader): New class. (extract_arm_insn): Add argument 'reader'. Callers updated. (decode_insn): Likewise. --- gdb/ChangeLog | 7 +++++++ gdb/arm-tdep.c | 46 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 9 deletions(-) (limited to 'gdb') diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0c3017b..402a84a 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2017-03-16 Yao Qi + + * arm-tdep.c (abstract_memory_reader): New class. + (instruction_reader): New class. + (extract_arm_insn): Add argument 'reader'. Callers updated. + (decode_insn): Likewise. + 2017-03-16 Doug Evans * guile/scm-lazy-string.c (lazy_string_smob): Clarify use of LENGTH diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c index 3aee722..b43368e 100644 --- a/gdb/arm-tdep.c +++ b/gdb/arm-tdep.c @@ -12905,17 +12905,43 @@ thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r) return -1; } +/* Abstract memory reader. */ + +class abstract_memory_reader +{ +public: + /* Read LEN bytes of target memory at address MEMADDR, placing the + results in GDB's memory at BUF. Return true on success. */ + + virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0; +}; + +/* Instruction reader from real target. */ + +class instruction_reader : public abstract_memory_reader +{ + public: + bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) + { + if (target_read_memory (memaddr, buf, len)) + return false; + else + return true; + } +}; + /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success and positive val on fauilure. */ static int -extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size) +extract_arm_insn (abstract_memory_reader& reader, + insn_decode_record *insn_record, uint32_t insn_size) { gdb_byte buf[insn_size]; memset (&buf[0], 0, insn_size); - if (target_read_memory (insn_record->this_addr, &buf[0], insn_size)) + if (!reader.read (insn_record->this_addr, buf, insn_size)) return 1; insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0], insn_size, @@ -12929,8 +12955,8 @@ typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*); dispatch it. */ static int -decode_insn (insn_decode_record *arm_record, record_type_t record_type, - uint32_t insn_size) +decode_insn (abstract_memory_reader &reader, insn_decode_record *arm_record, + record_type_t record_type, uint32_t insn_size) { /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm @@ -12964,7 +12990,7 @@ decode_insn (insn_decode_record *arm_record, record_type_t record_type, uint32_t ret = 0; /* return value: negative:failure 0:success. */ uint32_t insn_id = 0; - if (extract_arm_insn (arm_record, insn_size)) + if (extract_arm_insn (reader, arm_record, insn_size)) { if (record_debug) { @@ -13073,7 +13099,8 @@ arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache, paddress (gdbarch, arm_record.this_addr)); } - if (extract_arm_insn (&arm_record, 2)) + instruction_reader reader; + if (extract_arm_insn (reader, &arm_record, 2)) { if (record_debug) { @@ -13094,7 +13121,7 @@ arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache, if (!(u_regval & t_bit)) { /* We are decoding arm insn. */ - ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES); + ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES); } else { @@ -13102,13 +13129,14 @@ arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache, /* is it thumb2 insn? */ if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id)) { - ret = decode_insn (&arm_record, THUMB2_RECORD, + ret = decode_insn (reader, &arm_record, THUMB2_RECORD, THUMB2_INSN_SIZE_BYTES); } else { /* We are decoding thumb insn. */ - ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES); + ret = decode_insn (reader, &arm_record, THUMB_RECORD, + THUMB_INSN_SIZE_BYTES); } } -- cgit v1.1