Bug 21506 - python execute(to_string=True) no longer captures certain output since e7ea3ec7
Summary: python execute(to_string=True) no longer captures certain output since e7ea3ec7
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-20 14:54 UTC by sourceware-bugzilla
Modified: 2018-08-23 18:00 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2017-09-03 00:00:00


Attachments
a hacky fix (832 bytes, patch)
2018-08-23 18:00 UTC, Andrey Utkin
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description sourceware-bugzilla 2017-05-20 14:54:59 UTC
python gdb.execute(to_string=True) no longer captures all output, in particular for the 'step' command, since f7c382926d. This is limits functionality, e.g. http://stackoverflow.com/questions/39602306/tracing-program-function-execution-on-source-line-level

Expected result (f7c382926d)

$ ./gdb/gdb ../a.out
GNU gdb (GDB) 7.11.50.20160331-git
Reading symbols from ../a.out...done.
(gdb) break main
Breakpoint 1 at 0x4004aa: file test.c, line 3.
(gdb) run
Starting program: /home/tilsche/code/gdb/a.out 

Breakpoint 1, main () at test.c:3
3	    return 4;
(gdb) python x = gdb.execute('step', to_string=True)
(gdb) python print(x)
4	}

Result since e7ea3ec7c6

$ ./gdb/gdb ../a.out                       
GNU gdb (GDB) 7.11.50.20160331-git
Reading symbols from ../a.out...done.
(gdb) break main
Breakpoint 1 at 0x4004aa: file test.c, line 3.
(gdb) run
Starting program: /home/tilsche/code/gdb/a.out 

Breakpoint 1, main () at test.c:3
3	    return 4;
(gdb) python x = gdb.execute('step', to_string=True)
4	}
(gdb) python print(x)

Test code:

int main()
{
    return 4;
}
Comment 1 Tom Tromey 2017-09-03 18:51:06 UTC
I've also noticed this when using my python gdb gui -- 
the gui uses to_string=True for some commands to suppress output,
but gdb still emits the output anyway now.

For example in my case it happens in tui_on_normal_stop, which
calls print_stop_event for each UI.

I'm not sure what can be done about this.  if nothing, it's worth
noting that the output is sub-par as well:

(top) 30	  args.argv = argv;
31	  args.interpreter_p = INTERP_CONSOLE;


... that is, it doesn't try to erase the prompt first.
Comment 2 Simon Marchi 2018-04-01 19:43:21 UTC
I don't understand all the interps/uiouts dynamics yet, so I'm not sure what the solution should be, but the problem seems to be that:

- We install the "console" interpreter's uiout in execute_gdb_command as the current_uiout.
- We set up the redirection on the "tui" interpreter's uiout (in execute_command_to_string).

They are two different cli_ui_out instances.  When the print is done, the active uiout is therefore the one that doesn't have the redirection.

It's probably not the right fix, but just to illustrate, this change makes it work for me (though probably break other stuff):

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9eae8a1aef2d..5ca58578040f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -587,7 +587,7 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
 
       /* Use the console interpreter uiout to have the same print format
 	for console or MI.  */
-      interp = interp_lookup (current_ui, "console");
+      interp = interp_lookup (current_ui, "tui");
       current_uiout = interp_ui_out (interp);
 
       scoped_restore preventer = prevent_dont_repeat ();
Comment 3 Andrey Utkin 2018-08-17 18:00:41 UTC
(In reply to Simon Marchi from comment #2)
> It's probably not the right fix, but just to illustrate, this change makes
> it work for me (though probably break other stuff):

Indeed, this patch makes one of our tests work, but causes segfaults in other cases.

Program terminated with signal SIGSEGV, Segmentation fault.
#0  ui_out::redirect (this=0x0, outstream=outstream@entry=0x7ffc20a6da90) at ui-out.c:611
611       do_redirect (outstream);
(gdb) bt
#0  ui_out::redirect (this=0x0, outstream=outstream@entry=0x7ffc20a6da90) at ui-out.c:611
#1  0x000055a450e2589f in execute_command_to_string[abi:cxx11](char const*, int) (p=0x7f8ab460bc24 "info proc", from_tty=from_tty@entry=0) at top.c:673
#2  0x000055a450c28368 in execute_gdb_command (self=<optimized out>, args=<optimized out>, kw=<optimized out>) at python/python.c:600
...
Comment 4 Andrey Utkin 2018-08-23 18:00:47 UTC
Created attachment 11206 [details]
a hacky fix

The patch which I won't recommend to upstream, but it happens to help in this specific situation while not causing noticeable issues in other tests I have run.

Alternative, and perhaps better, solutiuon may be to temporarily replace ui_list.