From 643b126809e945e02420d78fd1ef0e3cef195cc3 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Fri, 26 Nov 2021 15:13:43 +0000 Subject: gdb: add 'maint set/show gnu-source-highlight enabled' command In a later commit I want to address an issue with the Python pygments based code styling solution. As this approach is only used when the GNU Source Highlight library is not available, testing bugs in this area can be annoying, as it requires GDB to be rebuilt with use of GNU Source Highlight disabled. This commit adds a pair of new maintenance commands: maintenance set gnu-source-highlight enabled on|off maintenance show gnu-source-highlight enabled these commands can be used to disable use of the GNU Source Highlight library, allowing me, in a later commit, to easily test bugs that would otherwise be masked by GNU Source Highlight being used. I made this a maintenance command, rather than a general purpose command, as it didn't seem like this was something a general user would need to adjust. We can always convert the maintenance command to a general command later if needed. There's no test for this here, but this feature will be used in a later commit. --- gdb/source-cache.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'gdb/source-cache.c') diff --git a/gdb/source-cache.c b/gdb/source-cache.c index 7016476..373607f 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -47,6 +47,46 @@ source_cache g_source_cache; +/* When this is true we will use the GNU Source Highlight to add styling to + source code (assuming the library is available). This is initialized to + true (if appropriate) in _initialize_source_cache below. */ + +static bool use_gnu_source_highlight; + +/* The "maint show gnu-source-highlight enabled" command. */ + +static void +show_use_gnu_source_highlight_enabled (struct ui_file *file, int from_tty, + struct cmd_list_element *c, + const char *value) +{ + fprintf_filtered (file, + _("Use of GNU Source Highlight library is \"%s\".\n"), + value); +} + +/* The "maint set gnu-source-highlight enabled" command. */ + +static void +set_use_gnu_source_highlight_enabled (const char *ignore_args, + int from_tty, + struct cmd_list_element *c) +{ +#ifndef HAVE_SOURCE_HIGHLIGHT + /* If the library is not available and the user tried to enable use of + the library, then disable use of the library, and give an error. */ + if (use_gnu_source_highlight) + { + use_gnu_source_highlight = false; + error (_("the GNU Source Highlight library is not available")); + } +#else + /* We (might) have just changed how we style source code, discard any + previously cached contents. */ + forget_cached_source_info (); +#endif +} + /* See source-cache.h. */ std::string @@ -192,7 +232,7 @@ source_cache::ensure (struct symtab *s) #ifdef HAVE_SOURCE_HIGHLIGHT bool already_styled = false; const char *lang_name = get_language_name (SYMTAB_LANGUAGE (s)); - if (lang_name != nullptr) + if (lang_name != nullptr && use_gnu_source_highlight) { /* The global source highlight object, or null if one was never constructed. This is stored here rather than in @@ -363,6 +403,36 @@ _initialize_source_cache () _("Force gdb to flush its source code cache."), &maintenanceflushlist); + /* All the 'maint set|show gnu-source-highlight' sub-commands. */ + static struct cmd_list_element *maint_set_gnu_source_highlight_cmdlist; + static struct cmd_list_element *maint_show_gnu_source_highlight_cmdlist; + + /* Adds 'maint set|show gnu-source-highlight'. */ + add_setshow_prefix_cmd ("gnu-source-highlight", class_maintenance, + _("Set gnu-source-highlight specific variables."), + _("Show gnu-source-highlight specific variables."), + &maint_set_gnu_source_highlight_cmdlist, + &maint_show_gnu_source_highlight_cmdlist, + &maintenance_set_cmdlist, + &maintenance_show_cmdlist); + + /* Adds 'maint set|show gnu-source-highlight enabled'. */ + add_setshow_boolean_cmd ("enabled", class_maintenance, + &use_gnu_source_highlight, _("\ +Set whether the GNU Source Highlight library should be used."), _("\ +Show whether the GNU Source Highlight library is being used."),_("\ +When enabled, GDB will use the GNU Source Highlight library to apply\n\ +styling to source code lines that are shown."), + set_use_gnu_source_highlight_enabled, + show_use_gnu_source_highlight_enabled, + &maint_set_gnu_source_highlight_cmdlist, + &maint_show_gnu_source_highlight_cmdlist); + + /* Enable use of GNU Source Highlight library, if we have it. */ +#ifdef HAVE_SOURCE_HIGHLIGHT + use_gnu_source_highlight = true; +#endif + #if GDB_SELF_TEST selftests::register_test ("source-cache", selftests::extract_lines_test); #endif -- cgit v1.1