diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2021-10-25 17:26:57 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2022-02-14 09:53:04 +0000 |
commit | e867795e8bcd6571c785e5e1d872fff0a5c7b290 (patch) | |
tree | 7676974d2132823f012a95413cf12d52dfcd7efe /gdb/cli | |
parent | 20ea3acc727f3be6322dfbd881e506873535231d (diff) | |
download | fsf-binutils-gdb-e867795e8bcd6571c785e5e1d872fff0a5c7b290.zip fsf-binutils-gdb-e867795e8bcd6571c785e5e1d872fff0a5c7b290.tar.gz fsf-binutils-gdb-e867795e8bcd6571c785e5e1d872fff0a5c7b290.tar.bz2 |
gdb: use python to colorize disassembler output
This commit adds styling support to the disassembler output, as such
two new commands are added to GDB:
set style disassembler enabled on|off
show style disassembler enabled
In this commit I make use of the Python Pygments package to provide
the styling. I did investigate making use of libsource-highlight,
however, I found the highlighting results to be inferior to those of
Pygments; only some mnemonics were highlighted, and highlighting of
register names such as r9d and r8d (on x86-64) was incorrect.
To enable disassembler highlighting via Pygments, I've added a new
extension language hook, which is then implemented for Python. This
hook is very similar to the existing hook for source code
colorization.
One possibly odd choice I made with the new hook is to pass a
gdb.Architecture through, even though this is currently unused. The
reason this argument is not used is that, currently, styling is
performed identically for all architectures.
However, even though the Python function used to perform styling of
disassembly output is not part of any documented API, I don't want
to close the door on a user overriding this function to provide
architecture specific styling. To do this, the user would inevitably
require access to the gdb.Architecture, and so I decided to add this
field now.
The styling is applied within gdb_disassembler::print_insn, to achieve
this, gdb_disassembler now writes its output into a temporary buffer,
styling is then applied to the contents of this buffer. Finally the
gdb_disassembler buffer is copied out to its final destination stream.
There's a new test to check that the disassembler output includes some
escape sequences, though I don't check for specific colours; the
precise colors will depend on which instructions are in the
disassembler output, and, I guess, how pygments is configured.
The only negative change with this commit is how we currently style
addresses in GDB.
Currently, when the disassembler wants to print an address, we call
back into GDB, and GDB prints the address value using the `address`
styling, and the symbol name using `function` styling. After this
commit, if pygments is used, then all disassembler styling is done
through pygments, and this include the address and symbol name parts
of the disassembler output.
I don't know how much of an issue this will be for people. There's
already some precedent for this in GDB when we look at source styling.
For example, function names in styled source listings are not styled
using the `function` style, but instead, either GNU Source Highlight,
or pygments gets to decide how the function name should be styled.
If the Python pygments library is not present then GDB will continue
to behave as it always has, the disassembler output is mostly
unstyled, but the address and symbols are styled using the `address`
and `function` styles, as they are today.
However, if the user does `set style disassembler enabled off`, then
all disassembler styling is switched off. This obviously covers the
use of pygments, but also includes the minimal styling done by GDB
when pygments is not available.
Diffstat (limited to 'gdb/cli')
-rw-r--r-- | gdb/cli/cli-style.c | 44 | ||||
-rw-r--r-- | gdb/cli/cli-style.h | 3 |
2 files changed, 47 insertions, 0 deletions
diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c index 2fd00e9..6c1652d 100644 --- a/gdb/cli/cli-style.c +++ b/gdb/cli/cli-style.c @@ -38,6 +38,11 @@ bool cli_styling = true; bool source_styling = true; +/* True if disassembler styling is enabled. Note that this is only + consulted when cli_styling is true. */ + +bool disassembler_styling = true; + /* Name of colors; must correspond to ui_file_style::basic_color. */ static const char * const cli_colors[] = { "none", @@ -274,6 +279,14 @@ cli_style_option::add_setshow_commands (enum command_class theclass, static cmd_list_element *style_set_list; static cmd_list_element *style_show_list; +/* The command list for 'set style disassembler'. */ + +static cmd_list_element *style_disasm_set_list; + +/* The command list for 'show style disassembler'. */ + +static cmd_list_element *style_disasm_show_list; + static void set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c) { @@ -301,6 +314,18 @@ show_style_sources (struct ui_file *file, int from_tty, fprintf_filtered (file, _("Source code styling is disabled.\n")); } +/* Implement 'show style disassembler'. */ + +static void +show_style_disassembler (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + if (disassembler_styling) + fprintf_filtered (file, _("Disassembler output styling is enabled.\n")); + else + fprintf_filtered (file, _("Disassembler output styling is disabled.\n")); +} + void _initialize_cli_style (); void _initialize_cli_style () @@ -337,6 +362,25 @@ available if the appropriate extension is available at runtime." ), set_style_enabled, show_style_sources, &style_set_list, &style_show_list); + add_setshow_prefix_cmd ("disassembler", no_class, + _("\ +Style-specific settings for the disassembler.\n\ +Configure various disassembler style-related variables."), + _("\ +Style-specific settings for the disassembler.\n\ +Configure various disassembler style-related variables."), + &style_disasm_set_list, &style_disasm_show_list, + &style_set_list, &style_show_list); + + add_setshow_boolean_cmd ("enabled", no_class, &disassembler_styling, _("\ +Set whether disassembler output styling is enabled."), _("\ +Show whether disassembler output styling is enabled."), _("\ +If enabled, disassembler output is styled. Disassembler highlighting\n\ +requires the Python Pygments library, if this library is not available\n\ +then disassembler highlighting will not be possible." + ), set_style_enabled, show_style_disassembler, + &style_disasm_set_list, &style_disasm_show_list); + file_name_style.add_setshow_commands (no_class, _("\ Filename display styling.\n\ Configure filename colors and display intensity."), diff --git a/gdb/cli/cli-style.h b/gdb/cli/cli-style.h index 3333c72..f69df47 100644 --- a/gdb/cli/cli-style.h +++ b/gdb/cli/cli-style.h @@ -128,6 +128,9 @@ extern cli_style_option version_style; /* True if source styling is enabled. */ extern bool source_styling; +/* True if disassembler styling is enabled. */ +extern bool disassembler_styling; + /* True if styling is enabled. */ extern bool cli_styling; |