binutils-gdb/gdb/cli/cli-style.h
Andrew Burgess e867795e8b 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.
2022-02-14 09:53:04 +00:00

138 lines
4.2 KiB
C++

/* CLI stylizing
Copyright (C) 2018-2022 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef CLI_CLI_STYLE_H
#define CLI_CLI_STYLE_H
#include "ui-file.h"
#include "command.h"
#include "gdbsupport/observable.h"
/* A single CLI style option. */
class cli_style_option
{
public:
/* Construct a CLI style option with a foreground color. */
cli_style_option (const char *name, ui_file_style::basic_color fg,
ui_file_style::intensity = ui_file_style::NORMAL);
/* Construct a CLI style option with an intensity. */
cli_style_option (const char *name, ui_file_style::intensity i);
/* Return a ui_file_style corresponding to the settings in this CLI
style. */
ui_file_style style () const;
/* Return the style name. */
const char *name () { return m_name; };
/* Call once to register this CLI style with the CLI engine. */
void add_setshow_commands (enum command_class theclass,
const char *prefix_doc,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list,
bool skip_intensity);
/* Return the 'set style NAME' command list, that can be used
to build a lambda DO_SET to call add_setshow_commands. */
struct cmd_list_element *set_list () { return m_set_list; };
/* Same as SET_LIST but for the show command list. */
struct cmd_list_element *show_list () { return m_show_list; };
/* This style can be observed for any changes. */
gdb::observers::observable<> changed;
private:
/* The style name. */
const char *m_name;
/* The foreground. */
const char *m_foreground;
/* The background. */
const char *m_background;
/* The intensity. */
const char *m_intensity;
/* Storage for command lists needed when registering
subcommands. */
struct cmd_list_element *m_set_list = nullptr;
struct cmd_list_element *m_show_list = nullptr;
/* Callback to notify the observable. */
static void do_set_value (const char *ignore, int from_tty,
struct cmd_list_element *cmd);
/* Callback to show the foreground. */
static void do_show_foreground (struct ui_file *file, int from_tty,
struct cmd_list_element *cmd,
const char *value);
/* Callback to show the background. */
static void do_show_background (struct ui_file *file, int from_tty,
struct cmd_list_element *cmd,
const char *value);
/* Callback to show the intensity. */
static void do_show_intensity (struct ui_file *file, int from_tty,
struct cmd_list_element *cmd,
const char *value);
};
/* The file name style. */
extern cli_style_option file_name_style;
/* The function name style. */
extern cli_style_option function_name_style;
/* The variable name style. */
extern cli_style_option variable_name_style;
/* The address style. */
extern cli_style_option address_style;
/* The highlight style. */
extern cli_style_option highlight_style;
/* The title style. */
extern cli_style_option title_style;
/* The metadata style. */
extern cli_style_option metadata_style;
/* The border style of a TUI window that does not have the focus. */
extern cli_style_option tui_border_style;
/* The border style of a TUI window that does have the focus. */
extern cli_style_option tui_active_border_style;
/* The style to use for the GDB version string. */
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;
#endif /* CLI_CLI_STYLE_H */