diff options
author | Andrew Burgess <aburgess@redhat.com> | 2025-02-13 15:39:31 +0000 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2025-03-19 14:31:53 +0000 |
commit | e5348a7ab3f11f4c096ee4ebcdb9eb2663337357 (patch) | |
tree | 08553fbfbe84aa2c57cfdd646608b96f89b29bbb /gdb/cli | |
parent | 65addfb0e40e2057ab0bbfee017fa967e16f2e82 (diff) | |
download | binutils-e5348a7ab3f11f4c096ee4ebcdb9eb2663337357.zip binutils-e5348a7ab3f11f4c096ee4ebcdb9eb2663337357.tar.gz binutils-e5348a7ab3f11f4c096ee4ebcdb9eb2663337357.tar.bz2 |
gdb/python: new styling argument to gdb.execute
Currently, gdb.execute emits styled output when the command is sending
its output to GDB's stdout, and produces unstyled output when the
output is going to a string.
But it is not unreasonable that a user might wish to capture styled
output from a gdb.execute call, for example, the user might want to
display the styled output are part of some larger UI output block.
At the same time, I don't think it makes sense to always produce
styled output when capturing the output in a string; if what the user
wants is to parse the output, then the style escape sequences make
this far harder.
I propose that gdb.execute gain a new argument 'styling'. When False
we would always produce unstyled output, and when True we would
produce styled output if styling is not disabled by some other means.
For example, if GDB's 'set style enabled' is off, then I think
gdb.execute() should respect that. My assumption here is that
gdb.execute() might be executed by some extension. If the extension
thinks "styled output world work here", but the user hates styled
output, and has turned it off, then the extension should not be
forcing styled output on the user.
I chose 'styling' instead of 'styled' as the Python argument name
because we already use 'styling' in gdb.Value.format_string, and we
don't use 'styled' anywhere else. This is only a little bit of
consistency, but I still think it's a good thing.
The default for 'styling' will change depending on where the output is
going. When gdb.execute is sending the output to GDB's stdout then
the default for 'styling' is True. When the output is going to a
string, then the default for 'styling' will be False. Not only does
this match the existing behaviour, but I think this makes sense. By
default we assume that output captured in a string is going to be
parsed, and therefore styling markup is unhelpful, while output going
to stdout should receive styling.
This fixes part of the problem described in PR gdb/32676. That bug
tries to capture styled source listing in a string, which wasn't
previously possible.
There are some additional issues with capturing source code; GDB
caches the source code in the source code cache. However, GDB doesn't
check if the cached content is styled or not. As a consequence, if
the first time the source of a file is shown it is unstyled, then the
cached will hold the unstyled source code, and future requests will
return that unstyled source. I'll address this issue in a separate
patch.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32676
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/cli')
-rw-r--r-- | gdb/cli/cli-script.c | 4 | ||||
-rw-r--r-- | gdb/cli/cli-script.h | 6 | ||||
-rw-r--r-- | gdb/cli/cli-style.c | 21 | ||||
-rw-r--r-- | gdb/cli/cli-style.h | 19 |
4 files changed, 45 insertions, 5 deletions
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c index 9131768..5decf3b 100644 --- a/gdb/cli/cli-script.c +++ b/gdb/cli/cli-script.c @@ -422,14 +422,14 @@ execute_control_commands (struct command_line *cmdlines, int from_tty) std::string execute_control_commands_to_string (struct command_line *commands, - int from_tty) + int from_tty, bool term_out) { std::string result; execute_fn_to_string (result, [&] () { execute_control_commands (commands, from_tty); - }, false); + }, term_out); return result; } diff --git a/gdb/cli/cli-script.h b/gdb/cli/cli-script.h index 23bd83e..df7316e 100644 --- a/gdb/cli/cli-script.h +++ b/gdb/cli/cli-script.h @@ -143,10 +143,12 @@ extern void execute_control_commands (struct command_line *cmdlines, /* Run execute_control_commands for COMMANDS. Capture its output into the returned string, do not display it to the screen. BATCH_FLAG - will be temporarily set to true. */ + will be temporarily set to true. When TERM_OUT is true the output is + collected with terminal behavior (e.g. with styling). When TERM_OUT is + false raw output will be collected (e.g. no styling). */ extern std::string execute_control_commands_to_string - (struct command_line *commands, int from_tty); + (struct command_line *commands, int from_tty, bool term_out); /* Exported to gdb/breakpoint.c */ diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c index 3ca30a4..b436275 100644 --- a/gdb/cli/cli-style.c +++ b/gdb/cli/cli-style.c @@ -51,6 +51,25 @@ static const char * const cli_intensities[] = { nullptr }; +/* When true styling is being temporarily suppressed. */ + +static bool scoped_disable_styling_p = false; + +/* See cli/cli-style.h. */ + +scoped_disable_styling::scoped_disable_styling () +{ + m_old_value = scoped_disable_styling_p; + scoped_disable_styling_p = true; +} + +/* See cli/cli-style.h. */ + +scoped_disable_styling::~scoped_disable_styling () +{ + scoped_disable_styling_p = m_old_value; +} + /* Return true if GDB's output terminal should support styling, otherwise, return false. This function really checks for things that indicate styling might not be supported, so a return value of false indicates @@ -91,7 +110,7 @@ disable_cli_styling () bool term_cli_styling () { - return cli_styling; + return cli_styling && !scoped_disable_styling_p; } /* See cli/cli-style.h. */ diff --git a/gdb/cli/cli-style.h b/gdb/cli/cli-style.h index 18827ce..e94b48d 100644 --- a/gdb/cli/cli-style.h +++ b/gdb/cli/cli-style.h @@ -171,4 +171,23 @@ extern void disable_cli_styling (); /* Return true styled output is currently enabled. */ extern bool term_cli_styling (); +/* Allow styling to be temporarily suppressed without changing the value of + 'set style enabled' user setting. This is useful in, for example, the + Python gdb.execute() call which can produce unstyled output. */ +struct scoped_disable_styling +{ + /* Temporarily suppress styling without changing the value of 'set + style enabled' user setting. */ + scoped_disable_styling (); + + /* If the constructor started suppressing styling, then styling is + resumed after this destructor call. */ + ~scoped_disable_styling (); + +private: + + /* The value to restore in the destructor. */ + bool m_old_value; +}; + #endif /* GDB_CLI_CLI_STYLE_H */ |