gdb: unify two dis_asm_read_memory functions in disasm.c

After the recent restructuring of the disassembler code, GDB has ended
up with two identical class static functions, both called
dis_asm_read_memory, with identical implementations.

My first thought was to move these out of their respective classes,
and just make them global functions, then I'd only need a single
copy.

And maybe that's the right way to go.  But I disliked that by doing
that I loose the encapsulation of the method with the corresponding
disassembler class.

So, instead, I placed the static method into its own class, and had
both the gdb_non_printing_memory_disassembler and gdb_disassembler
classes inherit from this new class as an additional base-class.

In terms of code generated, I don't think there's any significant
difference with this approach, but I think this better reflects how
the function is closely tied to the disassembler.

There should be no user visible changes after this commit.
This commit is contained in:
Andrew Burgess
2022-04-04 22:52:58 +01:00
parent 8b39b1e7ab
commit 75033d0841
2 changed files with 25 additions and 30 deletions

View File

@ -132,8 +132,8 @@ line_has_code_p (htab_t table, struct symtab *symtab, int line)
/* Wrapper of target_read_code. */ /* Wrapper of target_read_code. */
int int
gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, gdb_disassembler_memory_reader::dis_asm_read_memory
unsigned int len, (bfd_vma memaddr, gdb_byte *myaddr, unsigned int len,
struct disassemble_info *info) struct disassemble_info *info)
{ {
return target_read_code (memaddr, myaddr, len); return target_read_code (memaddr, myaddr, len);
@ -1021,16 +1021,6 @@ gdb_non_printing_disassembler::null_fprintf_styled_func
return 0; return 0;
} }
/* See disasm.h. */
int
gdb_non_printing_memory_disassembler::dis_asm_read_memory
(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
struct disassemble_info *dinfo)
{
return target_read_code (memaddr, myaddr, length);
}
/* A non-printing disassemble_info management class. The disassemble_info /* A non-printing disassemble_info management class. The disassemble_info
setup by this class will not print anything to the output stream (there setup by this class will not print anything to the output stream (there
is no output stream), and the instruction to be disassembled will be is no output stream), and the instruction to be disassembled will be

View File

@ -165,31 +165,39 @@ private:
ATTRIBUTE_PRINTF(3,4); ATTRIBUTE_PRINTF(3,4);
}; };
/* A non-printing disassemble_info management class. The disassemble_info /* This is a helper class, for use as an additional base-class, by some of
setup by this class will not print anything to the output stream (there the disassembler classes below. This class just defines a static method
is no output stream), and the instruction to be disassembled will be for reading from target memory, which can then be used by the various
read from target memory. */ disassembler sub-classes. */
struct gdb_non_printing_memory_disassembler struct gdb_disassembler_memory_reader
: public gdb_non_printing_disassembler
{ {
/* Constructor. GDBARCH is the architecture to disassemble for. */
gdb_non_printing_memory_disassembler (struct gdbarch *gdbarch)
:gdb_non_printing_disassembler (gdbarch, dis_asm_read_memory)
{ /* Nothing. */ }
private:
/* Implements the read_memory_func disassemble_info callback. */ /* Implements the read_memory_func disassemble_info callback. */
static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
unsigned int len, unsigned int len,
struct disassemble_info *info); struct disassemble_info *info);
}; };
/* A non-printing disassemble_info management class. The disassemble_info
setup by this class will not print anything to the output stream (there
is no output stream), and the instruction to be disassembled will be
read from target memory. */
struct gdb_non_printing_memory_disassembler
: public gdb_non_printing_disassembler,
private gdb_disassembler_memory_reader
{
/* Constructor. GDBARCH is the architecture to disassemble for. */
gdb_non_printing_memory_disassembler (struct gdbarch *gdbarch)
:gdb_non_printing_disassembler (gdbarch, dis_asm_read_memory)
{ /* Nothing. */ }
};
/* A dissassembler class that provides 'print_insn', a method for /* A dissassembler class that provides 'print_insn', a method for
disassembling a single instruction to the output stream. */ disassembling a single instruction to the output stream. */
struct gdb_disassembler : public gdb_printing_disassembler struct gdb_disassembler : public gdb_printing_disassembler,
private gdb_disassembler_memory_reader
{ {
gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file) gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file)
: gdb_disassembler (gdbarch, file, dis_asm_read_memory) : gdb_disassembler (gdbarch, file, dis_asm_read_memory)
@ -239,9 +247,6 @@ private:
(currently just to addresses and symbols) as it goes. */ (currently just to addresses and symbols) as it goes. */
static bool use_ext_lang_colorization_p; static bool use_ext_lang_colorization_p;
static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
unsigned int len,
struct disassemble_info *info);
static void dis_asm_memory_error (int err, bfd_vma memaddr, static void dis_asm_memory_error (int err, bfd_vma memaddr,
struct disassemble_info *info); struct disassemble_info *info);
static void dis_asm_print_address (bfd_vma addr, static void dis_asm_print_address (bfd_vma addr,