aboutsummaryrefslogtreecommitdiff
path: root/gdb/disasm.c
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2021-10-27 10:07:56 +0100
committerAndrew Burgess <aburgess@redhat.com>2022-06-15 09:44:54 +0100
commitf0c2e3e020d350b410e1bbe4ed636f2ea228d555 (patch)
treee27ca17e48a702c63330b2c6e4852d7a594284d8 /gdb/disasm.c
parent8a0b60471a75ce81b8ea067f6e87457b3ed0c7a3 (diff)
downloadgdb-f0c2e3e020d350b410e1bbe4ed636f2ea228d555.zip
gdb-f0c2e3e020d350b410e1bbe4ed636f2ea228d555.tar.gz
gdb-f0c2e3e020d350b410e1bbe4ed636f2ea228d555.tar.bz2
gdb: add new base class to gdb_disassembler
The motivation for this change is an upcoming Python disassembler API that I would like to add. As part of that change I need to create a new disassembler like class that contains a disassemble_info and a gdbarch. The management of these two objects is identical to how we manage these objects within gdb_disassembler, so it might be tempting for my new class to inherit from gdb_disassembler. The problem however, is that gdb_disassembler has a tight connection between its constructor, and its print_insn method. In the constructor the ui_file* that is passed in is replaced with a member variable string_file*, and then in print_insn, the contents of the member variable string_file are printed to the original ui_file*. What this means is that the gdb_disassembler class has a tight coupling between its constructor and print_insn; the class just isn't intended to be used in a situation where print_insn is not going to be called, which is how my (upcoming) sub-class would need to operate. My solution then, is to separate out the management of the disassemble_info and gdbarch into a new gdb_disassemble_info class, and make this class a parent of gdb_disassembler. In arm-tdep.c and mips-tdep.c, where we used to cast the disassemble_info->application_data to a gdb_disassembler, we can now cast to a gdb_disassemble_info as we only need to access the gdbarch information. Now, my new Python disassembler sub-class will still want to print things to an output stream, and so we will want access to the dis_asm_fprintf functionality for printing. However, rather than move this printing code into the gdb_disassemble_info base class, I have added yet another level of hierarchy, a gdb_printing_disassembler, thus the class structure is now: struct gdb_disassemble_info {}; struct gdb_printing_disassembler : public gdb_disassemble_info {}; struct gdb_disassembler : public gdb_printing_disassembler {}; In a later commit my new Python disassembler will inherit from gdb_printing_disassembler. The reason for adding the additional layer to the class hierarchy is that in yet another commit I intend to rewrite the function gdb_buffered_insn_length, and to do this I will be creating yet more disassembler like classes, however, these will not print anything, thus I will add a gdb_non_printing_disassembler class that also inherits from gdb_disassemble_info. Knowing that that change is coming, I've gone with the above class hierarchy now. There should be no user visible changes after this commit.
Diffstat (limited to 'gdb/disasm.c')
-rw-r--r--gdb/disasm.c58
1 files changed, 38 insertions, 20 deletions
diff --git a/gdb/disasm.c b/gdb/disasm.c
index f2df5ef..6ac8438 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -166,7 +166,8 @@ gdb_disassembler::dis_asm_print_address (bfd_vma addr,
/* Format disassembler output to STREAM. */
int
-gdb_disassembler::dis_asm_fprintf (void *stream, const char *format, ...)
+gdb_printing_disassembler::fprintf_func (void *stream,
+ const char *format, ...)
{
va_list args;
@@ -180,9 +181,9 @@ gdb_disassembler::dis_asm_fprintf (void *stream, const char *format, ...)
/* See disasm.h. */
int
-gdb_disassembler::dis_asm_styled_fprintf (void *stream,
- enum disassembler_style style,
- const char *format, ...)
+gdb_printing_disassembler::fprintf_styled_func (void *stream,
+ enum disassembler_style style,
+ const char *format, ...)
{
va_list args;
@@ -797,26 +798,41 @@ get_all_disassembler_options (struct gdbarch *gdbarch)
gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
struct ui_file *file,
- di_read_memory_ftype read_memory_func)
- : m_gdbarch (gdbarch),
+ read_memory_ftype func)
+ : gdb_printing_disassembler (gdbarch, &m_buffer, func,
+ dis_asm_memory_error, dis_asm_print_address),
m_buffer (!use_ext_lang_colorization_p && disassembler_styling
&& file->can_emit_style_escape ()),
m_dest (file)
+{ /* Nothing. */ }
+
+/* See disasm.h. */
+
+gdb_disassemble_info::gdb_disassemble_info
+ (struct gdbarch *gdbarch, struct ui_file *stream,
+ read_memory_ftype read_memory_func, memory_error_ftype memory_error_func,
+ print_address_ftype print_address_func, fprintf_ftype fprintf_func,
+ fprintf_styled_ftype fprintf_styled_func)
+ : m_gdbarch (gdbarch)
{
- init_disassemble_info (&m_di, &m_buffer, dis_asm_fprintf,
- dis_asm_styled_fprintf);
+ gdb_assert (fprintf_func != nullptr);
+ gdb_assert (fprintf_styled_func != nullptr);
+ init_disassemble_info (&m_di, stream, fprintf_func,
+ fprintf_styled_func);
m_di.flavour = bfd_target_unknown_flavour;
- m_di.memory_error_func = dis_asm_memory_error;
- m_di.print_address_func = dis_asm_print_address;
- /* NOTE: cagney/2003-04-28: The original code, from the old Insight
- disassembler had a local optimization here. By default it would
- access the executable file, instead of the target memory (there
- was a growing list of exceptions though). Unfortunately, the
- heuristic was flawed. Commands like "disassemble &variable"
- didn't work as they relied on the access going to the target.
- Further, it has been superseeded by trust-read-only-sections
- (although that should be superseeded by target_trust..._p()). */
- m_di.read_memory_func = read_memory_func;
+
+ /* The memory_error_func, print_address_func, and read_memory_func are
+ all initialized to a default (non-nullptr) value by the call to
+ init_disassemble_info above. If the user is overriding these fields
+ (by passing non-nullptr values) then do that now, otherwise, leave
+ these fields as the defaults. */
+ if (memory_error_func != nullptr)
+ m_di.memory_error_func = memory_error_func;
+ if (print_address_func != nullptr)
+ m_di.print_address_func = print_address_func;
+ if (read_memory_func != nullptr)
+ m_di.read_memory_func = read_memory_func;
+
m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
m_di.endian = gdbarch_byte_order (gdbarch);
@@ -828,7 +844,9 @@ gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
disassemble_init_for_target (&m_di);
}
-gdb_disassembler::~gdb_disassembler ()
+/* See disasm.h. */
+
+gdb_disassemble_info::~gdb_disassemble_info ()
{
disassemble_free_target (&m_di);
}