public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out
@ 2020-10-06 15:40 Tankut Baris Aktemur
  2020-10-21  6:56 ` Aktemur, Tankut Baris
  2020-10-22 16:09 ` Pedro Alves
  0 siblings, 2 replies; 4+ messages in thread
From: Tankut Baris Aktemur @ 2020-10-06 15:40 UTC (permalink / raw)
  To: gdb-patches

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out
  2020-10-06 15:40 [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out Tankut Baris Aktemur
@ 2020-10-21  6:56 ` Aktemur, Tankut Baris
  2020-10-22 16:09 ` Pedro Alves
  1 sibling, 0 replies; 4+ messages in thread
From: Aktemur, Tankut Baris @ 2020-10-21  6:56 UTC (permalink / raw)
  To: gdb-patches

Kindly pinging.

Thanks.
-Baris

On Tuesday, October 6, 2020 5:41 PM, Aktemur, Tankut Baris wrote:
>
> 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



Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out
  2020-10-06 15:40 [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out Tankut Baris Aktemur
  2020-10-21  6:56 ` Aktemur, Tankut Baris
@ 2020-10-22 16:09 ` Pedro Alves
  2020-11-27 13:19   ` Aktemur, Tankut Baris
  1 sibling, 1 reply; 4+ messages in thread
From: Pedro Alves @ 2020-10-22 16:09 UTC (permalink / raw)
  To: Tankut Baris Aktemur, gdb-patches

On 10/6/20 4:40 PM, Tankut Baris Aktemur via Gdb-patches wrote:
> 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>)]

Note this output is not really considered output of the command.
It's asynchronous output sent to all UIs in reaction to the
inferior-switching event.

If e.g., the top interpreter is MI (say, you run that python from a breakpoint
command), then you get:

 (gdb) 
 python gdb.execute("inferior 1", from_tty=False, to_string=True)
 &"python gdb.execute(\"inferior 1\", from_tty=False, to_string=True)\n"
 ~"[Switching to inferior 1 [<null>] (/home/pedro/gdb/binutils-gdb/build/gdb/gdb)]\n"
 ^done
 (gdb) 

And your patch won't capture that:

 python str=gdb.execute("inferior 1", from_tty=False, to_string=True)
 &"python str=gdb.execute(\"inferior 1\", from_tty=False, to_string=True)\n"
 ~"[Switching to inferior 1 [<null>] (<noexec>)]\n"
 ^done
 (gdb) 
 python print(str)
 &"python print(str)\n"
 ~"\n"
 ^done
 (gdb) 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out
  2020-10-22 16:09 ` Pedro Alves
@ 2020-11-27 13:19   ` Aktemur, Tankut Baris
  0 siblings, 0 replies; 4+ messages in thread
From: Aktemur, Tankut Baris @ 2020-11-27 13:19 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On Thursday, October 22, 2020 6:09 PM, Pedro Alves wrote:
> On 10/6/20 4:40 PM, Tankut Baris Aktemur via Gdb-patches wrote:
> > 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>)]
> 
> Note this output is not really considered output of the command.
> It's asynchronous output sent to all UIs in reaction to the
> inferior-switching event.
> 
> If e.g., the top interpreter is MI (say, you run that python from a breakpoint
> command), then you get:
> 
>  (gdb)
>  python gdb.execute("inferior 1", from_tty=False, to_string=True)
>  &"python gdb.execute(\"inferior 1\", from_tty=False, to_string=True)\n"
>  ~"[Switching to inferior 1 [<null>] (/home/pedro/gdb/binutils-gdb/build/gdb/gdb)]\n"
>  ^done
>  (gdb)
> 
> And your patch won't capture that:
> 
>  python str=gdb.execute("inferior 1", from_tty=False, to_string=True)
>  &"python str=gdb.execute(\"inferior 1\", from_tty=False, to_string=True)\n"
>  ~"[Switching to inferior 1 [<null>] (<noexec>)]\n"
>  ^done
>  (gdb)
>  python print(str)
>  &"python print(str)\n"
>  ~"\n"
>  ^done
>  (gdb)

Sorry for the delayed response.  The motivation behind this patch is this: 
We have a Python script via which we create an additional inferior, connect
it to a target, and later remove this inferior.  For the actions for which a
dedicated Python API does not exist, we use gdb.execute().  Switching the
inferior as well as connecting to a target produces output that we want to
hide from the user.  In principle, the goal is to suppress the output on the
CLI to obtain clean command-line interaction, but additionally we would like
to avoid temporary context switches in an IDE, if connected via MI.

Potential solutions:
1.  Expose "cli_suppress_notification" and "mi_suppress_notification" via
maintenance commands.  Use these commands in the Python script via gdb.execute()
to suppress the CLI and MI outputs.

2.  Extend the "add-inferior" command to accept a target string so that an
explicit switch is not necessary to make the target connection.  This would
avoid context switch-related output, but the target connection's output would
still be visible.  Perhaps CLI/MI suppress mechanism has to be used here, again.

3.  Extend the Python API to allow the actions we need (e.g. add a new inferior,
connect it to a target, etc.) instead of gdb.execute().  The Python API suppresses
the output before performing the actual actions.

Suggestions are welcome and appreciated.

Thanks
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-27 13:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 15:40 [PATCH] gdb/cli: if redirecting output, also redirect top-level interpreter's ui-out Tankut Baris Aktemur
2020-10-21  6:56 ` Aktemur, Tankut Baris
2020-10-22 16:09 ` Pedro Alves
2020-11-27 13:19   ` Aktemur, Tankut Baris

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).