public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tankut Baris Aktemur <tankut.baris.aktemur@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out
Date: Tue,  6 Oct 2020 17:40:42 +0200	[thread overview]
Message-ID: <1601998842-22089-1-git-send-email-tankut.baris.aktemur@intel.com> (raw)

When executing commands from Python, the API provides an option to
capture the command output in a string and return this to the user.
See the `gdb.execute` function at

  https://sourceware.org/gdb/onlinedocs/gdb/Basic-Python.html#Basic-Python

When the `to_string` argument is True, the output of the executed
command is supposed to be captured and returned to the caller.  For
this, GDB temporarily redirects the ui-outs to a string file.

However, output does not get captured if the executed command prints
its output through the top-level interpreter's ui-out.  E.g., in the
following we still see the output being printed.

  (gdb) python gdb.execute("inferior 1", from_tty=False, to_string=True)
  [Switching to inferior 1 [<null>] (<noexec>)]
  (gdb)

To fix, also redirect the top-level interpreter's ui-out.

gdb/ChangeLog:
2020-10-06  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

	* cli/cli-script.c (execute_control_commands_to_string): Redirect
	top level interpreter's ui-out before running the command.
---
 gdb/cli/cli-script.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index f8ac610d4d6..21a4065a5f2 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -425,6 +425,10 @@ execute_control_commands_to_string (struct command_line *commands,
     current_uiout->redirect (&str_file);
     ui_out_redirect_pop redirect_popper (current_uiout);
 
+    ui_out *uiout = top_level_interpreter ()->interp_ui_out ();
+    uiout->redirect (&str_file);
+    ui_out_redirect_pop redirect_popper2 (uiout);
+
     scoped_restore save_stdout
       = make_scoped_restore (&gdb_stdout, &str_file);
     scoped_restore save_stderr
-- 
2.17.1


             reply	other threads:[~2020-10-06 15:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-06 15:40 Tankut Baris Aktemur [this message]
2020-10-21  6:56 ` Aktemur, Tankut Baris
2020-10-22 16:09 ` Pedro Alves
2020-11-27 13:19   ` Aktemur, Tankut Baris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1601998842-22089-1-git-send-email-tankut.baris.aktemur@intel.com \
    --to=tankut.baris.aktemur@intel.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).