aboutsummaryrefslogtreecommitdiff
path: root/opcodes
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2022-01-22 11:38:18 +0000
committerAndrew Burgess <aburgess@redhat.com>2022-04-04 13:10:52 +0100
commit60a3da00bd5407f07d64dff82a4dae98230dfaac (patch)
treeb4a05a973c60abfc9dc5af43aad2a87c547db437 /opcodes
parentea6303b4971b0959329a1ae01648b16a4f4ac154 (diff)
downloadfsf-binutils-gdb-60a3da00bd5407f07d64dff82a4dae98230dfaac.zip
fsf-binutils-gdb-60a3da00bd5407f07d64dff82a4dae98230dfaac.tar.gz
fsf-binutils-gdb-60a3da00bd5407f07d64dff82a4dae98230dfaac.tar.bz2
objdump/opcodes: add syntax highlighting to disassembler output
This commit adds the _option_ of having disassembler output syntax highlighted in objdump. This option is _off_ by default. The new command line options are: --disassembler-color=off # The default. --disassembler-color=color --disassembler-color=extended-color I have implemented two colour modes, using the same option names as we use of --visualize-jumps, a basic 8-color mode ("color"), and an extended 8bit color mode ("extended-color"). The syntax highlighting requires that each targets disassembler be updated; each time the disassembler produces some output we now pass through an additional parameter indicating what style should be applied to the text. As updating all target disassemblers is a large task, the old API is maintained. And so, a user of the disassembler (i.e. objdump, gdb) must provide two functions, the current non-styled print function, and a new, styled print function. I don't currently have a plan for converting every single target disassembler, my hope is that interested folk will update the disassemblers they are interested in. But it is possible some might never get updated. In this initial series I intend to convert the RISC-V disassembler completely, and also do a partial conversion of the x86 disassembler. Hopefully having the x86 disassembler at least partial converted will allow more people to try this out easily and provide feedback. In this commit I have focused on objdump. The changes to GDB at this point are the bare minimum required to get things compiling, GDB makes no use of the styling information to provide any colors, that will come later, if this commit is accepted. This first commit in the series doesn't convert any target disassemblers at all (the next two commits will update some targets), so after this commit, the only color you will see in the disassembler output, is that produced from objdump itself, e.g. from objdump_print_addr_with_sym, where we print an address and a symbol name, these are now printed with styling information, and so will have colors applied (if the option is on). Finally, my ability to pick "good" colors is ... well, terrible. I'm in no way committed to the colors I've picked here, so I encourage people to suggest new colors, or wait for this commit to land, and then patch the choice of colors. I do have an idea about using possibly an environment variable to allow the objdump colors to be customised, but I haven't done anything like that in this commit, the color choices are just fixed in the code for now. binutils/ChangeLog: * NEWS: Mention new feature. * doc/binutils.texi (objdump): Describe --disassembler-color option. * objdump.c (disassembler_color): New global. (disassembler_extended_color): Likewise. (disassembler_in_comment): Likewise. (usage): Mention --disassembler-color option. (long_options): Add --disassembler-color option. (objdump_print_value): Use fprintf_styled_func instead of fprintf_func. (objdump_print_symname): Likewise. (objdump_print_addr_with_sym): Likewise. (objdump_color_for_disassembler_style): New function. (objdump_styled_sprintf): New function. (fprintf_styled): New function. (disassemble_jumps): Use disassemble_set_printf, and reset disassembler_in_comment. (null_styled_print): New function. (disassemble_bytes): Use disassemble_set_printf, and reset disassembler_in_comment. (disassemble_data): Update init_disassemble_info call. (main): Handle --disassembler-color option. include/ChangeLog: * dis-asm.h (enum disassembler_style): New enum. (struct disassemble_info): Add fprintf_styled_func field, and created_styled_output field. (disassemble_set_printf): Declare. (init_disassemble_info): Add additional parameter. (INIT_DISASSEMBLE_INFO): Add additional parameter. opcodes/ChangeLog: * dis-init.c (init_disassemble_info): Take extra parameter, initialize the new fprintf_styled_func and created_styled_output fields. * disassembler.c (disassemble_set_printf): New function definition.
Diffstat (limited to 'opcodes')
-rw-r--r--opcodes/dis-init.c5
-rw-r--r--opcodes/disassemble.c13
2 files changed, 17 insertions, 1 deletions
diff --git a/opcodes/dis-init.c b/opcodes/dis-init.c
index 0b171c0..59039ef 100644
--- a/opcodes/dis-init.c
+++ b/opcodes/dis-init.c
@@ -25,7 +25,8 @@
void
init_disassemble_info (struct disassemble_info *info, void *stream,
- fprintf_ftype fprintf_func)
+ fprintf_ftype fprintf_func,
+ fprintf_styled_ftype fprintf_styled_func)
{
memset (info, 0, sizeof (*info));
@@ -35,6 +36,7 @@ init_disassemble_info (struct disassemble_info *info, void *stream,
info->endian_code = info->endian;
info->octets_per_byte = 1;
info->fprintf_func = fprintf_func;
+ info->fprintf_styled_func = fprintf_styled_func;
info->stream = stream;
info->read_memory_func = buffer_read_memory;
info->memory_error_func = perror_memory;
@@ -42,5 +44,6 @@ init_disassemble_info (struct disassemble_info *info, void *stream,
info->symbol_at_address_func = generic_symbol_at_address;
info->symbol_is_valid = generic_symbol_is_valid;
info->display_endian = BFD_ENDIAN_UNKNOWN;
+ info->created_styled_output = false;
}
diff --git a/opcodes/disassemble.c b/opcodes/disassemble.c
index 1f1fde7..7325394 100644
--- a/opcodes/disassemble.c
+++ b/opcodes/disassemble.c
@@ -858,3 +858,16 @@ opcodes_assert (const char *file, int line)
opcodes_error_handler (_("Please report this bug"));
abort ();
}
+
+/* Set the stream, and the styled and unstyled printf functions within
+ INFO. */
+
+void
+disassemble_set_printf (struct disassemble_info *info, void *stream,
+ fprintf_ftype unstyled_printf,
+ fprintf_styled_ftype styled_printf)
+{
+ info->stream = stream;
+ info->fprintf_func = unstyled_printf;
+ info->fprintf_styled_func = styled_printf;
+}