Bug 520580 - Console view is mixing up stdin and gdb-traces
Summary: Console view is mixing up stdin and gdb-traces
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-debug (show other bugs)
Version: 9.3.0   Edit
Hardware: PC Windows 7
: P3 normal with 1 vote (vote)
Target Milestone: 9.4.0   Edit
Assignee: Jonah Graham CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
: 259986 527008 570357 (view as bug list)
Depends on: 521515
Blocks: 527252 173732
  Show dependency tree
 
Reported: 2017-08-04 11:33 EDT by Jon Beniston CLA
Modified: 2021-08-10 11:08 EDT (History)
5 users (show)

See Also:


Attachments
Add Cygwin PTY support (53.81 KB, patch)
2017-11-09 12:28 EST, Jon Beniston CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jon Beniston CLA 2017-08-04 11:33:40 EDT
If I run this program on Eclipse Oxygen 32-bit on Windows 7:

int main(void) {
	char buf[200];
	scanf("%s\n", buf);
	printf("%s\n", buf);
	return EXIT_SUCCESS;
}

Without typing anything in the Console View, I get:

41-thread-select

Or other GDB/MI commands.

It appears as though somehow the old gdb-traces console is getting mixed up with stdin.
Comment 1 Jon Beniston CLA 2017-08-04 11:36:50 EDT
Some other strange Console behaviour. If I step through

int main(void) {
	printf("hello1\n");
	printf("hello2\n");
	return EXIT_SUCCESS;
}

No output appears in the Console until the program has finished.
Comment 2 Jon Beniston CLA 2017-08-04 11:57:38 EDT
This appears to only be a problem on Windows. Linux works fine.

This worked fine on Windows Neon 9.0 release.

Also it only appears to be an issue when debugging. Run works ok.
Comment 3 Jonah Graham CLA 2017-08-07 10:50:49 EDT
I am not sure why you were not seeing this in Neon and are in Oxygen as little has changed in this area.

On Windows the stdin/out of the program under GDB inherits the stdin/out of GDB causing garbling between the two. (It can work if there are nice perfect newlines everywhere, but that isn't often the case.

There is a good blog post that shows a workaround in detail: http://nicolas.riousset.com/category/software-methodologies/eclipse-how-to-debug-a-c-console-application-using-a-native-dos-console-instead-of-the-ide-integrated-one/

The post has more details, but the summary is use "set new-console on" to cause a new console window to be created.



(In reply to Jon Beniston from comment #1)
> Some other strange Console behaviour. If I step through
> 
> int main(void) {
> 	printf("hello1\n");
> 	printf("hello2\n");
> 	return EXIT_SUCCESS;
> }
> 
> No output appears in the Console until the program has finished.

This is most likely buffering:
https://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows

It would also be solved by using the workaround.
Comment 4 Jonah Graham CLA 2017-08-07 10:55:02 EDT
This type of issue comes up too often in CDT for users on Windows. I think that we should add "set new-console on" as an option in the launch configuration and then on Windows set that as the default or in some way make it easier for users to find. 

I have assigned to myself for consideration in 9.4.

I have also lowered the priority to normal (from critical) because of the workaround. Jon, if you believe I have misunderstood and there is still something critical, please let me know and I can re-raise the priority.
Comment 5 Jon Beniston CLA 2017-08-07 12:21:15 EDT
Hi Jonah. Yep, you can ignore the second comment regarding no output, as that is due to buffering (I thought line buffering was the default, as is the case on my usual embedded target from Windows).

However, regarding the initial problem, one change between 9.0 and 9.3 is that there was no "Debugger Console" view before. It's perhaps not specifically related to that, but I've just tried 9.1 and 9.2 with Neon as well. 9.1 works OK. 9.2 doesn't, it has the same issue as 9.3. It seems 9.2 was when the Debugger Console was introduced.

There are a couple of things that I think aren't ideal with using set new-console on, as a default. Firstly, the console disappears when the program exits so you can't see the output (Perhaps there's a way to prevent this, besides setting breakpoints). Also, you then get different behaviour whether you Run or Debug your program (As Run will still use the Eclipse console - stdin seems to work ok in that case). Also, it's quite handy to have some of the features of the Eclipse console.

I'll see if I can track down what is the difference is between 9.1 and 9.2.

Thanks,
Jon
Comment 6 Jonah Graham CLA 2017-08-07 12:26:48 EDT
Thanks Jon for tracking this down more. The change from 9.1 to 9.2 was supposed to have no effect on Windows, other than to change the UI location of the console. This is because CDT on Windows does not take advantage to the new-ui feature in GDB yet. See Bug 516371 for that.

I take your point on changing the default. Perhaps just making it easier for users to workaround these problems (like buffering). I'll hold off on anything in this area pending your further investigation.
Comment 7 Jonah Graham CLA 2017-08-08 10:47:07 EDT
Hi Jon, I can reproduce this problem on CDT 9.0 (Neon.0). There is a timing issue that means that causing it to come in is different. But there is a fundamental problem that GDB's MI and GDB's inferior are using the same stdin/out. 

Bug 516371 should improve this problem because the MI and the inferior's GDB channels are separated. But the GDB's CLI input and the inferior will still share a channel.

On Linux, the -inferior-tty-set is used to connect a different input/output between CDT and GDB's inferior.
Comment 8 Jon Beniston CLA 2017-08-09 05:49:34 EDT
Hi Jonah. Yep, I think fixing Bug 516371 and using -inferior-tty-set is probably the way to resolve this.

I'm actually using a Cygwin build of GDB rather than mingw though. I've just written a pair of test programs to try out the new-ui command: one is Cygwin based that opens a Cygwin pty and then redirects the master-side to stdin/stdout. This gets run by a second pure Windows program, which uses pipes to the Cygwin prog to allow it to send and receive MI commands which seems to work. (I'll try integrating this in to the pty JNI DLL in Eclipse next). A different solution would be needed for mingw.

Unfortunately I can't yet get -inferior-tty-set to work fully in either Windows or Linux. In both Windows & Linux, it works for running native programs. However, it doesn't seem to work for remote targets or sim targets (which is what I'm personally interested in).
Comment 9 Jon Beniston CLA 2017-08-21 13:39:10 EDT
I've posted a patch for GDB that adds support for -inferior-tty-set for remote and simulator targets.

https://cygwin.com/ml/gdb-patches/2017-08/msg00403.html

Without this, their I/O always appears in the GDB console.

CDT will also need a patch here for remote targets. In StartOrRestartProcessSequence_7_0.stepInitializeInputOutput, a PTY is not created if SessionType is REMOTE. The comment says this is because gdbserver handles the I/O, but that isn't always the case. Some remote targets use GDB's remote-fileio to redirect I/O to GDB - and thus a PTY is needed here too.

I've written a JNI DLL that with the above patches has the Console view working. It just needs a bit more work to get the GDB Console working properly.
Comment 10 Jonah Graham CLA 2017-11-09 05:42:19 EST
(In reply to Jonah Graham from comment #4)
> This type of issue comes up too often in CDT for users on Windows. I think
> that we should add "set new-console on" as an option in the launch
> configuration and then on Windows set that as the default or in some way
> make it easier for users to find. 

I plan to add the "set new-console on" as an explicit option in the launch config in 9.4 (upcoming release).

I will not change the default on Windows at this time.

---

Longer term the solution is to use the new-ui feature of CDT, that requires changes on the CDT and GDB side to work.
Comment 11 Jonah Graham CLA 2017-11-09 05:45:49 EST
*** Bug 527008 has been marked as a duplicate of this bug. ***
Comment 12 Jonah Graham CLA 2017-11-09 05:46:58 EST
(In reply to Jonah Graham from comment #10)
> Longer term the solution is to use the new-ui feature of CDT, that requires
> changes on the CDT and GDB side to work.
Sorry, two halves to longer term solution. 1 is new-ui to separate CLI/program output from MI, second is having separate TTY as Jon has been looking into.
Comment 13 Jon Beniston CLA 2017-11-09 06:53:31 EST
I have this working with Cygwin GDB now and it also supports the GDB Console (Bug 516371). I'll post the patches in a bit.
Comment 14 Jon Beniston CLA 2017-11-09 12:28:13 EST
Created attachment 271400 [details]
Add Cygwin PTY support

This patch adds PTY support for Cygwin. 

It includes the Java classes (CygwinPTY and PersistentCygwinPTY) as well as the corresponding JNI code (core/org.eclipse.cdt.core.win32/library/pty/jni/src/cygwin_pty.cpp) and some helper utilities (cygwin_pty/cygwin_resize_pty/cygwin_exec_with_pty) that allow Win32 programs to interface to Cygwin PTYs.

At the moment, this doesn't include a patch to the code that instantiates the PTYs (I use my own launch delegates, so haven't tried to modify the standard versions). There would presumably need to be some way for a user to choose between a Cygwin PTY or the standard Windows PTY depending on the debugger they are running.

This PTY currently works well enough to support the gdb new-ui command and also supports the GDB Console on Windows.
Comment 15 Jonah Graham CLA 2017-11-09 13:21:43 EST
(In reply to Jon Beniston from comment #14)
> Created attachment 271400 [details]
Thanks for the patch, can you please make it a gerrit as all contributions need to come in that way these days -- https://wiki.eclipse.org/CDT/git#Using_Gerrit_for_CDT

The change looks interesting. AFAICT* this is a fix for Bug 516371, can you submit it to gerrit with "Bug 516371: ..." as the commit message so it connects there. 

*This looks like you are changing how to launch so that CLI uses the new-ui and the stdin/out of the inferior goes through that terminal too. Do you also have -inferior-tty-set for a new pty so that the stdin/out of the inferior is completely independent of GDB?
Comment 16 Jon Beniston CLA 2017-11-09 16:10:04 EST
It fixes Bug 516371, but only for Cygwin based GDB. It will not work for Ming based GDB.

Yes, I run gdb and used new-ui for mi stream and use -inferior-tty-set for stdio. This means we end up with 3 PTYs, one for GDB console, one for MI and one for stdio, so they are all completely separate.
Comment 17 Jonah Graham CLA 2017-11-09 16:28:51 EST
(In reply to Jon Beniston from comment #16)
> It fixes Bug 516371, but only for Cygwin based GDB. It will not work for
> Ming based GDB.
Do you know if it addresses Msys2?
Comment 18 Eclipse Genie CLA 2017-11-09 16:37:26 EST
New Gerrit change created: https://git.eclipse.org/r/111347
Comment 19 Jon Beniston CLA 2017-11-09 16:57:09 EST
(In reply to Jonah Graham from comment #17)
> (In reply to Jon Beniston from comment #16)
> > It fixes Bug 516371, but only for Cygwin based GDB. It will not work for
> > Ming based GDB.
> Do you know if it addresses Msys2?

I don't know - I've never used it. I understand it's a fork of Cygwin, so maybe.
Comment 21 Jonah Graham CLA 2017-11-14 09:16:09 EST
I have added the option for external console to N&N -- https://wiki.eclipse.org/CDT/User/NewIn94#Debug -- and created Bug 527252 for the ongoing work of using -inferior-tty-set
Comment 22 Jonah Graham CLA 2021-01-14 08:04:50 EST
*** Bug 570357 has been marked as a duplicate of this bug. ***
Comment 23 Jonah Graham CLA 2021-08-10 11:08:43 EDT
*** Bug 259986 has been marked as a duplicate of this bug. ***