[PATCH 05/16] Add output styles to gdb

Tom Tromey
2018-11-28T00:14:54Z

This adds some output styling to the CLI.

A style is currently a foreground color, a background color, and an
intensity (dim or bold). (This list could be expanded depending on
terminal capabilities.)

A style can be applied while printing. For ui-out, this is done by
passing the style constant as an argument. For low-level cases,
fprintf_styled and fputs_styled are provided.

Users can control the style via a number of new set/show commands. In
the interest of not typing many nearly-identical documentation
strings, I automated this. On the down side, this is not very
i18n-friendly.

I've chose some default colors to use. I think it would be good to
enable this by default, so that when users start the new gdb, they
will see the new feature.

Stylizing is done if TERM is set and is not "dumb". This could be
improved when the TUI is available by using the curses has_colors
call. That is, the lowest layer could call this without committing to
using curses everywhere; see my other patch for TUI colorizing.

I considered adding a new "set_style" method to ui_file. However,
because the implementation had to interact with the pager code, I
didn't take this approach. But, one idea might be to put the isatty
check there and then have it defer to the lower layers.

gdb/ChangeLog
2018-11-27 Tom Tromey <tom@tromey.com>

* utils.h (set_output_style, fprintf_styled)
(fputs_styled): Declare.
* utils.c (applied_style, desired_style): New globals.
(emit_style_escape, set_output_style): New function.
(prompt_for_continue): Emit style escapes.
(fputs_maybe_filtered): Likewise.
(fputs_styled, fprintf_styled): New functions.
* ui-out.h (enum class ui_out_style_kind): New.
(class ui_out) <field_string, field_stream, do_field_string>: Add
style parameter.
* ui-out.c (ui_out::field_stream, ui_out::field_string): Add style
parameter.
* tui/tui-out.h (class tui_ui_out) <do_field_string>: Add style
parameter.
* tui/tui-out.c (tui_ui_out::do_field_string): Add style
parameter.
(tui_ui_out::do_field_string): Update.
* tracepoint.c (print_one_static_tracepoint_marker): Style
output.
* stack.c (print_frame_info, print_frame): Style output.
* source.c (print_source_lines_base): Style output.
* skip.c (info_skip_command): Style output.
* record-btrace.c (btrace_call_history_src_line): Style output.
(btrace_call_history): Likewise.
* python/py-framefilter.c (py_print_frame): Style output.
* mi/mi-out.h (class mi_ui_out) <do_field_string>: Add style
parameter.
* mi/mi-out.c (mi_ui_out::do_table_header)
(mi_ui_out::do_field_int): Update.
(mi_ui_out::do_field_string): Update.
* disasm.c (gdb_pretty_print_disassembler::pretty_print_insn):
Style output.
* cli/cli-style.h: New file.
* cli/cli-style.c: New file.
* cli-out.h (class cli_ui_out) <do_field_string>: Add style
parameter.
* cli-out.c (cli_ui_out::do_table_header)
(cli_ui_out::do_field_int, cli_ui_out::do_field_skip): Update.
(cli_ui_out::do_field_string): Add style parameter. Style the
output.
* breakpoint.c (print_breakpoint_location): Style output.
(update_static_tracepoint): Likewise.
* Makefile.in (SUBDIR_CLI_SRCS): Add cli-style.c.
(HFILES_NO_SRCDIR): Add cli-style.h.

gdb/testsuite/ChangeLog
2018-11-27 Tom Tromey <tom@tromey.com>

* gdb.base/style.exp: New file.
* gdb.base/style.c: New file.
---
gdb/ChangeLog | 47 ++++++
gdb/Makefile.in | 2 +
gdb/breakpoint.c | 12 +-
gdb/cli-out.c | 36 ++++-
gdb/cli-out.h | 3 +-
gdb/cli/cli-style.c | 257 +++++++++++++++++++++++++++++++
gdb/cli/cli-style.h | 89 +++++++++++
gdb/disasm.c | 3 +-
gdb/mi/mi-out.c | 12 +-
gdb/mi/mi-out.h | 3 +-
gdb/python/py-framefilter.c | 5 +-
gdb/record-btrace.c | 12 +-
gdb/skip.c | 6 +-
gdb/source.c | 3 +-
gdb/stack.c | 13 +-
gdb/testsuite/ChangeLog | 5 +
gdb/testsuite/gdb.base/style.c | 22 +++
gdb/testsuite/gdb.base/style.exp | 41 +++++
gdb/tracepoint.c | 7 +-
gdb/tui/tui-out.c | 5 +-
gdb/tui/tui-out.h | 2 +-
gdb/ui-out.c | 10 +-
gdb/ui-out.h | 21 ++-
gdb/utils.c | 80 +++++++++-
gdb/utils.h | 16 ++
25 files changed, 667 insertions(+), 45 deletions(-)
create mode 100644 gdb/cli/cli-style.c
create mode 100644 gdb/cli/cli-style.h
create mode 100644 gdb/testsuite/gdb.base/style.c
create mode 100644 gdb/testsuite/gdb.base/style.exp

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index ce0d799b7d..54baf96ea3 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -239,6 +239,7 @@ SUBDIR_CLI_SRCS = \
cli/cli-logging.c \
cli/cli-script.c \
cli/cli-setshow.c \
+ cli/cli-style.c \
cli/cli-utils.c

SUBDIR_CLI_OBS = $(patsubst %.c,%.o,$(SUBDIR_CLI_SRCS))
@@ -1428,6 +1429,7 @@ HFILES_NO_SRCDIR = \
cli/cli-decode.h \
cli/cli-script.h \
cli/cli-setshow.h \
+ cli/cli-style.h \
cli/cli-utils.h \
common/buffer.h \
common/cleanups.h \
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 8af3d54a77..53763e0254 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5860,13 +5860,15 @@ print_breakpoint_location (struct breakpoint *b,
if (sym)
{
uiout->text ("in ");
- uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
+ uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
+ ui_out_style_kind::FUNCTION);
uiout->text (" ");
uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
uiout->text ("at ");
}
uiout->field_string ("file",
- symtab_to_filename_for_display (loc->symtab));
+ symtab_to_filename_for_display (loc->symtab),
+ ui_out_style_kind::FILE);
uiout->text (":");

if (uiout->is_mi_like_p ())
@@ -13427,11 +13429,13 @@ update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
uiout->text ("Now in ");
if (sym)
{
- uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
+ uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
+ ui_out_style_kind::FUNCTION);
uiout->text (" at ");
}
uiout->field_string ("file",
- symtab_to_filename_for_display (sal2.symtab));
+ symtab_to_filename_for_display (sal2.symtab),
+ ui_out_style_kind::FILE);
uiout->text (":");

if (uiout->is_mi_like_p ())
diff --git a/gdb/cli-out.c b/gdb/cli-out.c
index 7e3ee3e54c..4b5fc17390 100644
--- a/gdb/cli-out.c
+++ b/gdb/cli-out.c
@@ -25,6 +25,7 @@
#include "cli-out.h"
#include "completer.h"
#include "readline/readline.h"
+#include "cli/cli-style.h"

/* These are the CLI output functions */

@@ -71,7 +72,8 @@ cli_ui_out::do_table_header (int width, ui_align alignment,
if (m_suppress_output)
return;

- do_field_string (0, width, alignment, 0, col_hdr.c_str ());
+ do_field_string (0, width, alignment, 0, col_hdr.c_str (),
+ ui_out_style_kind::DEFAULT);
}

/* Mark beginning of a list */
@@ -99,7 +101,8 @@ cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,

std::string str = string_printf ("%d", value);

- do_field_string (fldno, width, alignment, fldname, str.c_str ());
+ do_field_string (fldno, width, alignment, fldname, str.c_str (),
+ ui_out_style_kind::DEFAULT);
}

/* used to omit a field */
@@ -111,7 +114,8 @@ cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
if (m_suppress_output)
return;

- do_field_string (fldno, width, alignment, fldname, "");
+ do_field_string (fldno, width, alignment, fldname, "",
+ ui_out_style_kind::DEFAULT);
}

/* other specific cli_field_* end up here so alignment and field
@@ -119,7 +123,8 @@ cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,

void
cli_ui_out::do_field_string (int fldno, int width, ui_align align,
- const char *fldname, const char *string)
+ const char *fldname, const char *string,
+ ui_out_style_kind style)
{
int before = 0;
int after = 0;
@@ -154,7 +159,25 @@ cli_ui_out::do_field_string (int fldno, int width, ui_align align,
spaces (before);

if (string)
- fputs_filtered (string, m_streams.back ());
+ {
+ ui_file_style fstyle;
+ switch (style)
+ {
+ case ui_out_style_kind::DEFAULT:
+ /* Nothing. */
+ break;
+ case ui_out_style_kind::FILE:
+ /* Nothing. */
+ fstyle = file_name_style.style ();
+ break;
+ case ui_out_style_kind::FUNCTION:
+ fstyle = function_name_style.style ();
+ break;
+ default:
+ gdb_assert_not_reached ("missing case");
+ }
+ fputs_styled (string, fstyle, m_streams.back ());
+ }

if (after)
spaces (after);
@@ -175,7 +198,8 @@ cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,

std::string str = string_vprintf (format, args);

- do_field_string (fldno, width, align, fldname, str.c_str ());
+ do_field_string (fldno, width, align, fldname, str.c_str (),
+ ui_out_style_kind::DEFAULT);
}

void
diff --git a/gdb/cli-out.h b/gdb/cli-out.h
index f701da7bf3..4fe10a40a2 100644
--- a/gdb/cli-out.h
+++ b/gdb/cli-out.h
@@ -51,7 +51,8 @@ protected:
const char *fldname) override;
virtual void do_field_string (int fldno, int width, ui_align align,
const char *fldname,
- const char *string) override;
+ const char *string,
+ ui_out_style_kind style) override;
virtual void do_field_fmt (int fldno, int width, ui_align align,
const char *fldname, const char *format,
va_list args)
diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c
new file mode 100644
index 0000000000..de2866ce75
--- /dev/null
+++ b/gdb/cli/cli-style.c
@@ -0,0 +1,257 @@
+/* CLI colorizing
+
+ Copyright (C) 2018 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/>. */
+
+#include "defs.h"
+#include "cli/cli-cmds.h"
+#include "cli/cli-style.h"
+
+/* True if styling is enabled. */
+
+#if defined(_WIN32) || defined (__CYGWIN__)
+int cli_styling = 0;
+#else
+int cli_styling = 1;
+#endif
+
+/* Name of colors; must correspond to ui_file_style::simple_color. */
+static const char * const cli_colors[] = {
+ "none",
+ "black",
+ "red",
+ "green",
+ "yellow",
+ "blue",
+ "magenta",
+ "cyan",
+ "white",
+ nullptr
+};
+
+/* Names of intensities; must correspond to
+ ui_file_style::intensity. */
+static const char * const cli_intensities[] = {
+ "normal",
+ "bold",
+ "dim",
+ nullptr
+};
+
+/* See cli-style.h. */
+
+cli_style_option file_name_style (ui_file_style::GREEN);
+
+/* See cli-style.h. */
+
+cli_style_option function_name_style (ui_file_style::YELLOW);
+
+/* See cli-style.h. */
+
+cli_style_option::cli_style_option (ui_file_style::simple_color fg)
+ : m_foreground (cli_colors[fg - ui_file_style::NONE]),
+ m_background (cli_colors[0]),
+ m_intensity (cli_intensities[ui_file_style::NORMAL])
+{
+}
+
+/* Return the color number corresponding to COLOR. */
+
+static int
+color_number (const char *color)
+{
+ for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
+ {
+ if (color == cli_colors[i])
+ return i - 1;
+ }
+ gdb_assert_not_reached ("color not found");
+}
+
+/* See cli-style.h. */
+
+ui_file_style
+cli_style_option::style () const
+{
+ int fg = color_number (m_foreground);
+ int bg = color_number (m_background);
+ ui_file_style::intensity intensity = ui_file_style::NORMAL;
+
+ for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
+ {
+ if (m_intensity == cli_intensities[i])
+ {
+ intensity = (ui_file_style::intensity) i;
+ break;
+ }
+ }
+
+ return ui_file_style (fg, bg, intensity);
+}
+
+/* See cli-style.h. */
+
+void
+cli_style_option::do_set (const char *args, int from_tty)
+{
+}
+
+/* See cli-style.h. */
+
+void
+cli_style_option::do_show (const char *args, int from_tty)
+{
+}
+
+/* See cli-style.h. */
+
+void
+cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
+ struct cmd_list_element *cmd,
+ const char *value)
+{
+ const char *name = (const char *) get_cmd_context (cmd);
+ fprintf_filtered (file, _("The \"%s\" foreground color is: %s\n"),
+ name, value);
+}
+
+/* See cli-style.h. */
+
+void
+cli_style_option::do_show_background (struct ui_file *file, int from_tty,
+ struct cmd_list_element *cmd,
+ const char *value)
+{
+ const char *name = (const char *) get_cmd_context (cmd);
+ fprintf_filtered (file, _("The \"%s\" background color is: %s\n"),
+ name, value);
+}
+
+/* See cli-style.h. */
+
+void
+cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
+ struct cmd_list_element *cmd,
+ const char *value)
+{
+ const char *name = (const char *) get_cmd_context (cmd);
+ fprintf_filtered (file, _("The \"%s\" display intensity is: %s\n"),
+ name, value);
+}
+
+/* See cli-style.h. */
+
+void
+cli_style_option::add_setshow_commands (const char *name,
+ enum command_class theclass,
+ const char *prefix_doc,
+ const char *prefixname,
+ struct cmd_list_element **set_list,
+ struct cmd_list_element **show_list)
+{
+ m_show_prefix = std::string ("set ") + prefixname + " ";
+ m_show_prefix = std::string ("show ") + prefixname + " ";
+
+ add_prefix_cmd (name, no_class, do_set, prefix_doc, &m_set_list,
+ m_show_prefix.c_str (), 0, set_list);
+ add_prefix_cmd (name, no_class, do_show, prefix_doc, &m_show_list,
+ m_set_prefix.c_str (), 0, show_list);
+
+ add_setshow_enum_cmd ("foreground", theclass, cli_colors,
+ &m_foreground,
+ _("Set the foreground color for this property"),
+ _("Show the foreground color for this property"),
+ nullptr,
+ nullptr,
+ do_show_foreground,
+ &m_set_list, &m_show_list, (void *) name);
+ add_setshow_enum_cmd ("background", theclass, cli_colors,
+ &m_background,
+ _("Set the background color for this property"),
+ _("Show the background color for this property"),
+ nullptr,
+ nullptr,
+ do_show_background,
+ &m_set_list, &m_show_list, (void *) name);
+ add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
+ &m_intensity,
+ _("Set the display intensity color for this property"),
+ _("\
+Show the display intensity color for this property"),
+ nullptr,
+ nullptr,
+ do_show_intensity,
+ &m_set_list, &m_show_list, (void *) name);
+}
+
+static void
+set_style (const char *arg, int from_tty)
+{
+}
+
+static void
+show_style (const char *arg, int from_tty)
+{
+}
+
+static void
+show_style_enabled (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ if (cli_styling)
+ fprintf_filtered (file, _("CLI output styling is enabled.\n"));
+ else
+ fprintf_filtered (file, _("CLI output styling is disabled.\n"));
+}
+
+void
+_initialize_cli_style ()
+{
+ static cmd_list_element *style_set_list;
+ static cmd_list_element *style_show_list;
+
+ add_prefix_cmd ("style", no_class, set_style, _("\
+Style-specific settings\n\
+Configure various style-related variables, such as colors"),
+ &style_set_list, "set style ", 0, &setlist);
+ add_prefix_cmd ("style", no_class, show_style, _("\
+Style-specific settings\n\
+Configure various style-related variables, such as colors"),
+ &style_show_list, "show style ", 0, &showlist);
+
+ add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
+Set whether CLI styling is enabled."), _("\
+Show whether CLI is enabled."), _("\
+If enabled, output to the terminal is styled."),
+ NULL, show_style_enabled,
+ &style_set_list, &style_show_list);
+
+ file_name_style.add_setshow_commands ("filename", no_class,
+ _("\
+Filename display styling\n\
+Configure filename colors and display intensity."),
+ "style filename",
+ &style_set_list,
+ &style_show_list);
+ function_name_style.add_setshow_commands ("function", no_class,
+ _("\
+Function name display styling\n\
+Configure function name colors and display intensity"),
+ "style function",
+ &style_set_list,
+ &style_show_list);
+}
diff --git a/gdb/cli/cli-style.h b/gdb/cli/cli-style.h
new file mode 100644
index 0000000000..36845b2581
--- /dev/null
+++ b/gdb/cli/cli-style.h
@@ -0,0 +1,89 @@
+/* CLI stylizing
+
+ Copyright (C) 2018 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_STYLE_H
+#define CLI_STYLE_H
+
+#include "ui-file.h"
+
+/* A single CLI style option. */
+class cli_style_option
+{
+public:
+
+ /* Construct a CLI style option with a foreground color. */
+ cli_style_option (ui_file_style::simple_color fg);
+
+ /* Return a ui_file_style corresponding to the settings in this CLI
+ style. */
+ ui_file_style style () const;
+
+ /* Call once to register this CLI style with the CLI engine. */
+ void add_setshow_commands (const char *name,
+ enum command_class theclass,
+ const char *prefix_doc,
+ const char *prefixname,
+ struct cmd_list_element **set_list,
+ struct cmd_list_element **show_list);
+
+private:
+
+ /* The foreground. */
+ const char *m_foreground;
+ /* The background. */
+ const char *m_background;
+ /* The intensity. */
+ const char *m_intensity;
+
+ /* Storage for prefixes needed when registering the commands. */
+ std::string m_show_prefix;
+ std::string m_set_prefix;
+ /* 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 set a value. */
+ static void do_set (const char *args, int from_tty);
+ /* Callback to show a value. */
+ static void do_show (const char *args, int from_tty);
+ /* 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;
+
+/* True if styling is enabled. */
+extern int cli_styling;
+
+#endif /* CLI_STYLE_H */
diff --git a/gdb/disasm.c b/gdb/disasm.c
index 128c3abfde..5e016be79f 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -244,7 +244,8 @@ gdb_pretty_print_disassembler::pretty_print_insn (struct ui_out *uiout,
the future. */
uiout->text (" <");
if ((flags & DISASSEMBLY_OMIT_FNAME) == 0)
- uiout->field_string ("func-name", name.c_str ());
+ uiout->field_string ("func-name", name.c_str (),
+ ui_out_style_kind::FUNCTION);
uiout->text ("+");
uiout->field_int ("offset", offset);
uiout->text (">:\t");
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index 39b18b03a0..4aa4a5c601 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -65,8 +65,10 @@ mi_ui_out::do_table_header (int width, ui_align alignment,
open (NULL, ui_out_type_tuple);
do_field_int (0, 0, ui_center, "width", width);
do_field_int (0, 0, ui_center, "alignment", alignment);
- do_field_string (0, 0, ui_center, "col_name", col_name.c_str ());
- do_field_string (0, width, alignment, "colhdr", col_hdr.c_str ());
+ do_field_string (0, 0, ui_center, "col_name", col_name.c_str (),
+ ui_out_style_kind::DEFAULT);
+ do_field_string (0, width, alignment, "colhdr", col_hdr.c_str (),
+ ui_out_style_kind::DEFAULT);
close (ui_out_type_tuple);
}

@@ -95,7 +97,8 @@ mi_ui_out::do_field_int (int fldno, int width, ui_align alignment,
char buffer[20]; /* FIXME: how many chars long a %d can become? */

xsnprintf (buffer, sizeof (buffer), "%d", value);
- do_field_string (fldno, width, alignment, fldname, buffer);
+ do_field_string (fldno, width, alignment, fldname, buffer,
+ ui_out_style_kind::DEFAULT);
}

/* Used to omit a field. */
@@ -111,7 +114,8 @@ mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,

void
mi_ui_out::do_field_string (int fldno, int width, ui_align align,
- const char *fldname, const char *string)
+ const char *fldname, const char *string,
+ ui_out_style_kind style)
{
ui_file *stream = m_streams.back ();
field_separator ();
diff --git a/gdb/mi/mi-out.h b/gdb/mi/mi-out.h
index 89ff88cd32..b576263d2b 100644
--- a/gdb/mi/mi-out.h
+++ b/gdb/mi/mi-out.h
@@ -57,7 +57,8 @@ protected:
virtual void do_field_skip (int fldno, int width, ui_align align,
const char *fldname) override;
virtual void do_field_string (int fldno, int width, ui_align align,
- const char *fldname, const char *string) override;
+ const char *fldname, const char *string,
+ ui_out_style_kind style) override;
virtual void do_field_fmt (int fldno, int width, ui_align align,
const char *fldname, const char *format, va_list args)
override ATTRIBUTE_PRINTF (6,0);
diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c
index fe17b70f53..53ff5c822e 100644
--- a/gdb/python/py-framefilter.c
+++ b/gdb/python/py-framefilter.c
@@ -898,7 +898,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
if (function == NULL)
out->field_skip ("func");
else
- out->field_string ("func", function);
+ out->field_string ("func", function, ui_out_style_kind::FUNCTION);
}
}

@@ -934,7 +934,8 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
out->wrap_hint (" ");
out->text (" at ");
annotate_frame_source_file ();
- out->field_string ("file", filename.get ());
+ out->field_string ("file", filename.get (),
+ ui_out_style_kind::FILE);
annotate_frame_source_file_end ();
}
}
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 814f080941..8e8ef15284 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -1091,7 +1091,8 @@ btrace_call_history_src_line (struct ui_out *uiout,
return;

uiout->field_string ("file",
- symtab_to_filename_for_display (symbol_symtab (sym)));
+ symtab_to_filename_for_display (symbol_symtab (sym)),
+ ui_out_style_kind::FILE);

btrace_compute_src_line_range (bfun, &begin, &end);
if (end < begin)
@@ -1182,11 +1183,14 @@ btrace_call_history (struct ui_out *uiout,
}

if (sym != NULL)
- uiout->field_string ("function", SYMBOL_PRINT_NAME (sym));
+ uiout->field_string ("function", SYMBOL_PRINT_NAME (sym),
+ ui_out_style_kind::FUNCTION);
else if (msym != NULL)
- uiout->field_string ("function", MSYMBOL_PRINT_NAME (msym));
+ uiout->field_string ("function", MSYMBOL_PRINT_NAME (msym),
+ ui_out_style_kind::FUNCTION);
else if (!uiout->is_mi_like_p ())
- uiout->field_string ("function", "??");
+ uiout->field_string ("function", "??",
+ ui_out_style_kind::FUNCTION);

if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
{
diff --git a/gdb/skip.c b/gdb/skip.c
index 77373e85e6..a8f8ee1962 100644
--- a/gdb/skip.c
+++ b/gdb/skip.c
@@ -413,7 +413,8 @@ info_skip_command (const char *arg, int from_tty)

current_uiout->field_string ("file",
e.file ().empty () ? "<none>"
- : e.file ().c_str ()); /* 4 */
+ : e.file ().c_str (),
+ ui_out_style_kind::FILE); /* 4 */
if (e.function_is_regexp ())
current_uiout->field_string ("regexp", "y"); /* 5 */
else
@@ -421,7 +422,8 @@ info_skip_command (const char *arg, int from_tty)

current_uiout->field_string ("function",
e.function ().empty () ? "<none>"
- : e.function ().c_str ()); /* 6 */
+ : e.function ().c_str (),
+ ui_out_style_kind::FUNCTION); /* 6 */

current_uiout->text ("\n");
}
diff --git a/gdb/source.c b/gdb/source.c
index e295fbf49e..7552496f78 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -1320,7 +1320,8 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
MI expects both fields. ui_source_list is set only for CLI,
not for TUI. */
if (uiout->is_mi_like_p () || uiout->test_flags (ui_source_list))
- uiout->field_string ("file", symtab_to_filename_for_display (s));
+ uiout->field_string ("file", symtab_to_filename_for_display (s),
+ ui_out_style_kind::FILE);
if (uiout->is_mi_like_p () || !uiout->test_flags (ui_source_list))
{
const char *s_fullname = symtab_to_fullname (s);
diff --git a/gdb/stack.c b/gdb/stack.c
index f34d7b2a17..a718bf8901 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -824,16 +824,19 @@ print_frame_info (struct frame_info *frame, int print_level,
if (get_frame_type (frame) == DUMMY_FRAME)
{
annotate_function_call ();
- uiout->field_string ("func", "<function called from gdb>");
+ uiout->field_string ("func", "<function called from gdb>",
+ ui_out_style_kind::FUNCTION);
}
else if (get_frame_type (frame) == SIGTRAMP_FRAME)
{
annotate_signal_handler_caller ();
- uiout->field_string ("func", "<signal handler called>");
+ uiout->field_string ("func", "<signal handler called>",
+ ui_out_style_kind::FUNCTION);
}
else if (get_frame_type (frame) == ARCH_FRAME)
{
- uiout->field_string ("func", "<cross-architecture call>");
+ uiout->field_string ("func", "<cross-architecture call>",
+ ui_out_style_kind::FUNCTION);
}
uiout->text ("\n");
annotate_frame_end ();
@@ -1182,7 +1185,7 @@ print_frame (struct frame_info *frame, int print_level,
string_file stb;
fprintf_symbol_filtered (&stb, funname ? funname.get () : "??",
funlang, DMGL_ANSI);
- uiout->field_stream ("func", stb);
+ uiout->field_stream ("func", stb, ui_out_style_kind::FUNCTION);
uiout->wrap_hint (" ");
annotate_frame_args ();

@@ -1225,7 +1228,7 @@ print_frame (struct frame_info *frame, int print_level,
uiout->wrap_hint (" ");
uiout->text (" at ");
annotate_frame_source_file ();
- uiout->field_string ("file", filename_display);
+ uiout->field_string ("file", filename_display, ui_out_style_kind::FILE);
if (uiout->is_mi_like_p ())
{
const char *fullname = symtab_to_fullname (sal.symtab);
diff --git a/gdb/testsuite/gdb.base/style.c b/gdb/testsuite/gdb.base/style.c
new file mode 100644
index 0000000000..a7820831fd
--- /dev/null
+++ b/gdb/testsuite/gdb.base/style.c
@@ -0,0 +1,22 @@
+/* Copyright 2018 Free Software Foundation, Inc.
+
+ 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/>. */
+
+#include <stdio.h>
+
+int
+main (int argc, char **argv)
+{
+ return 0; /* break here */
+}
diff --git a/gdb/testsuite/gdb.base/style.exp b/gdb/testsuite/gdb.base/style.exp
new file mode 100644
index 0000000000..859e50352f
--- /dev/null
+++ b/gdb/testsuite/gdb.base/style.exp
@@ -0,0 +1,41 @@
+# Copyright 2018 Free Software Foundation, Inc.
+
+# 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/>.
+
+# Test CLI output styling.
+
+standard_testfile
+
+save_vars { env(TERM) } {
+ # We need an ANSI-capable terminal to get the output.
+ setenv TERM ansi
+
+ if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
+ return -1
+ }
+
+ if {![runto_main]} {
+ fail "style tests failed"
+ return
+ }
+
+ gdb_test_no_output "set style enabled on"
+
+ set main_expr "\033\\\[33mmain\033\\\[m"
+ set file_expr "\033\\\[32m.*style\\.c\033\\\[m:\[0-9\]"
+
+ gdb_test "frame" \
+ "$main_expr.*$file_expr.*"
+ gdb_test "info breakpoints" "$main_expr at $file_expr.*"
+}
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 8cd53374f9..5c1111297c 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -3684,8 +3684,8 @@ print_one_static_tracepoint_marker (int count,
if (sym)
{
uiout->text ("in ");
- uiout->field_string ("func",
- SYMBOL_PRINT_NAME (sym));
+ uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
+ ui_out_style_kind::FUNCTION);
uiout->wrap_hint (wrap_indent);
uiout->text (" at ");
}
@@ -3695,7 +3695,8 @@ print_one_static_tracepoint_marker (int count,
if (sal.symtab != NULL)
{
uiout->field_string ("file",
- symtab_to_filename_for_display (sal.symtab));
+ symtab_to_filename_for_display (sal.symtab),
+ ui_out_style_kind::FILE);
uiout->text (":");

if (uiout->is_mi_like_p ())
diff --git a/gdb/tui/tui-out.c b/gdb/tui/tui-out.c
index 6d4e2363f3..29d7bfa84b 100644
--- a/gdb/tui/tui-out.c
+++ b/gdb/tui/tui-out.c
@@ -51,7 +51,8 @@ tui_ui_out::do_field_int (int fldno, int width, ui_align alignment,

void
tui_ui_out::do_field_string (int fldno, int width, ui_align align,
- const char *fldname, const char *string)
+ const char *fldname, const char *string,
+ ui_out_style_kind style)
{
if (suppress_output ())
return;
@@ -68,7 +69,7 @@ tui_ui_out::do_field_string (int fldno, int width, ui_align align,

m_start_of_line++;

- cli_ui_out::do_field_string (fldno, width, align, fldname, string);
+ cli_ui_out::do_field_string (fldno, width, align, fldname, string, style);
}

void
diff --git a/gdb/tui/tui-out.h b/gdb/tui/tui-out.h
index d191c65810..a4903eef61 100644
--- a/gdb/tui/tui-out.h
+++ b/gdb/tui/tui-out.h
@@ -31,7 +31,7 @@ protected:
void do_field_int (int fldno, int width, ui_align align, const char *fldname,
int value) override;
void do_field_string (int fldno, int width, ui_align align, const char *fldname,
- const char *string) override;
+ const char *string, ui_out_style_kind style) override;
void do_field_fmt (int fldno, int width, ui_align align, const char *fldname,
const char *format, va_list args) override
ATTRIBUTE_PRINTF (6,0);
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 3d74f78543..761d6c0745 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -473,10 +473,11 @@ ui_out::field_core_addr (const char *fldname, struct gdbarch *gdbarch,
}

void
-ui_out::field_stream (const char *fldname, string_file &stream)
+ui_out::field_stream (const char *fldname, string_file &stream,
+ ui_out_style_kind style)
{
if (!stream.empty ())
- field_string (fldname, stream.c_str ());
+ field_string (fldname, stream.c_str (), style);
else
field_skip (fldname);
stream.clear ();
@@ -497,7 +498,8 @@ ui_out::field_skip (const char *fldname)
}

void
-ui_out::field_string (const char *fldname, const char *string)
+ui_out::field_string (const char *fldname, const char *string,
+ ui_out_style_kind style)
{
int fldno;
int width;
@@ -505,7 +507,7 @@ ui_out::field_string (const char *fldname, const char *string)

verify_field (&fldno, &width, &align);

- do_field_string (fldno, width, align, fldname, string);
+ do_field_string (fldno, width, align, fldname, string, style);
}

void
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 81e2e0b20d..8604105c09 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -66,6 +66,18 @@ enum ui_out_type
ui_out_type_list
};

+/* Possible kinds of styling. */
+
+enum class ui_out_style_kind
+{
+ /* The default (plain) style. */
+ DEFAULT,
+ /* File name. */
+ FILE,
+ /* Function name. */
+ FUNCTION
+};
+
class ui_out
{
public:
@@ -95,9 +107,11 @@ class ui_out
int value);
void field_core_addr (const char *fldname, struct gdbarch *gdbarch,
CORE_ADDR address);
- void field_string (const char *fldname, const char *string);
+ void field_string (const char *fldname, const char *string,
+ ui_out_style_kind style = ui_out_style_kind::DEFAULT);
void field_string (const char *fldname, const std::string &string);
- void field_stream (const char *fldname, string_file &stream);
+ void field_stream (const char *fldname, string_file &stream,
+ ui_out_style_kind style = ui_out_style_kind::DEFAULT);
void field_skip (const char *fldname);
void field_fmt (const char *fldname, const char *format, ...)
ATTRIBUTE_PRINTF (3, 4);
@@ -141,7 +155,8 @@ class ui_out
virtual void do_field_skip (int fldno, int width, ui_align align,
const char *fldname) = 0;
virtual void do_field_string (int fldno, int width, ui_align align,
- const char *fldname, const char *string) = 0;
+ const char *fldname, const char *string,
+ ui_out_style_kind style) = 0;
virtual void do_field_fmt (int fldno, int width, ui_align align,
const char *fldname, const char *format,
va_list args)
diff --git a/gdb/utils.c b/gdb/utils.c
index 0f1953a66d..69c9e76576 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -71,6 +71,7 @@
#include "cp-support.h"
#include <algorithm>
#include "common/pathstuff.h"
+#include "cli/cli-style.h"

void (*deprecated_error_begin_hook) (void);

@@ -1422,6 +1423,46 @@ set_screen_width_and_height (int width, int height)
set_width ();
}

+/* The currently applied style. */
+
+static ui_file_style applied_style;
+
+/* The currently desired style. This can differ from the applied
+ style when showing the pagination prompt. */
+
+static ui_file_style desired_style;
+
+/* Emit an ANSI style escape for STYLE to the wrap buffer. */
+
+static void
+emit_style_escape (const ui_file_style &style)
+{
+ if (applied_style == style)
+ return;
+ applied_style = style;
+
+ wrap_buffer.append (style.to_ansi ());
+}
+
+/* Set the current output style. This will affect future uses of the
+ _filtered output functions. */
+
+static void
+set_output_style (struct ui_file *stream, const ui_file_style &style)
+{
+ if (stream != gdb_stdout
+ || !cli_styling
+ || style == desired_style
+ || !ui_file_isatty (stream))
+ return;
+ const char *term = getenv ("TERM");
+ if (term == nullptr || !strcmp (term, "dumb"))
+ return;
+
+ desired_style = style;
+ emit_style_escape (style);
+}
+
/* Wait, so the user can read what's on the screen. Prompt the user
to continue by pressing RETURN. 'q' is also provided because
telling users what to do in the prompt is more user-friendly than
@@ -1437,6 +1478,9 @@ prompt_for_continue (void)
steady_clock::time_point prompt_started = steady_clock::now ();
bool disable_pagination = pagination_disabled_for_command;

+ /* Clear the current styling. */
+ emit_style_escape (ui_file_style ());
+
if (annotation_level > 1)
printf_unfiltered (("\n\032\032pre-prompt-for-continue\n"));

@@ -1481,6 +1525,9 @@ prompt_for_continue (void)
reinitialize_more_filter ();
pagination_disabled_for_command = disable_pagination;

+ /* Restore the current styling. */
+ emit_style_escape (desired_style);
+
dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
}

@@ -1714,7 +1761,11 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
if chars_per_line is right, we probably just overflowed
anyway; if it's wrong, let us keep going. */
if (wrap_column)
- fputc_unfiltered ('\n', stream);
+ {
+ emit_style_escape (ui_file_style ());
+ flush_wrap_buffer (stream);
+ fputc_unfiltered ('\n', stream);
+ }

/* Possible new page. Note that
PAGINATION_DISABLED_FOR_COMMAND might be set during
@@ -1727,6 +1778,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
if (wrap_column)
{
fputs_unfiltered (wrap_indent, stream);
+ emit_style_escape (desired_style);
flush_wrap_buffer (stream);
/* FIXME, this strlen is what prevents wrap_indent from
containing tabs. However, if we recurse to print it
@@ -1759,6 +1811,17 @@ fputs_filtered (const char *linebuffer, struct ui_file *stream)
fputs_maybe_filtered (linebuffer, stream, 1);
}

+/* See utils.h. */
+
+void
+fputs_styled (const char *linebuffer, const ui_file_style &style,
+ struct ui_file *stream)
+{
+ set_output_style (stream, style);
+ fputs_maybe_filtered (linebuffer, stream, 1);
+ set_output_style (stream, ui_file_style ());
+}
+
int
putchar_unfiltered (int c)
{
@@ -1986,6 +2049,21 @@ fprintfi_filtered (int spaces, struct ui_file *stream, const char *format,
va_end (args);
}

+/* See utils.h. */
+
+void
+fprintf_styled (struct ui_file *stream, const ui_file_style &style,
+ const char *format, ...)
+{
+ va_list args;
+
+ set_output_style (stream, style);
+ va_start (args, format);
+ vfprintf_filtered (stream, format, args);
+ va_end (args);
+ set_output_style (stream, ui_file_style ());
+}
+

void
printf_filtered (const char *format, ...)
diff --git a/gdb/utils.h b/gdb/utils.h
index 08a29af1dc..9872a15fd7 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -423,6 +423,22 @@ extern void fputstrn_unfiltered (const char *str, int n, int quotr,
/* Return nonzero if filtered printing is initialized. */
extern int filtered_printing_initialized (void);

+/* Like fprintf_filtered, but styles the output according to STYLE,
+ when appropriate. */
+
+extern void fprintf_styled (struct ui_file *stream,
+ const ui_file_style &style,
+ const char *fmt,
+ ...)
+ ATTRIBUTE_PRINTF (3, 4);
+
+/* Like fputs_filtered, but styles the output according to STYLE, when
+ appropriate. */
+
+extern void fputs_styled (const char *linebuffer,
+ const ui_file_style &style,
+ struct ui_file *stream);
+
/* Display the host ADDR on STREAM formatted as ``0x%x''. */
extern void gdb_print_host_address_1 (const void *addr, struct ui_file *stream);

--
2.17.2