Bug 1091608 - Attach the console if the process is running the GTest. r=ted

This commit is contained in:
Masatoshi Kimura 2014-11-03 22:22:39 +09:00
parent 972d8d67b1
commit b7148d9fc4
3 changed files with 42 additions and 18 deletions

View File

@ -3527,6 +3527,9 @@ XREMain::XRE_mainStartup(bool* aExitFlag)
if (PR_GetEnv("MOZ_RUN_GTEST")) {
int result;
#ifdef XP_WIN
UseParentConsole();
#endif
// RunGTest will only be set if we're in xul-unit
if (mozilla::RunGTest) {
result = mozilla::RunGTest();

View File

@ -96,6 +96,9 @@ void
WriteConsoleLog();
#ifdef XP_WIN
void
UseParentConsole();
BOOL
WinLaunchChild(const wchar_t *exePath, int argc,
char **argv, HANDLE userToken = nullptr,

View File

@ -331,6 +331,41 @@ NS_INTERFACE_MAP_END_INHERITING(nsNativeAppSupportBase)
NS_IMPL_ADDREF_INHERITED(nsNativeAppSupportWin, nsNativeAppSupportBase)
NS_IMPL_RELEASE_INHERITED(nsNativeAppSupportWin, nsNativeAppSupportBase)
void
UseParentConsole()
{
// Try to attach console to the parent process.
// It will succeed when the parent process is a command line,
// so that stdio will be displayed in it.
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Change std handles to refer to new console handles.
// Before doing so, ensure that stdout/stderr haven't been
// redirected to a valid file.
// The return value for _fileno(<a std handle>) for GUI apps was changed over.
// Until VC7, it was -1. Starting from VC8, it was changed to -2.
// http://msdn.microsoft.com/en-us/library/zs6wbdhx%28v=vs.80%29.aspx
// Starting from VC11, the return value was cahnged to 0 for stdin,
// 1 for stdout, 2 for stdout. Accroding to Microsoft, this is a bug
// which will be fixed in VC14.
// https://connect.microsoft.com/VisualStudio/feedback/details/785119/
// Although the document does not make it explicit, it looks like
// the return value from _get_osfhandle(_fileno(<a std handle>)) also
// changed to -2 and VC11 and 12 do not have a bug about _get_osfhandle().
// We support VC10 or later, so it's sufficient to compare the return
// value with -2.
if (_fileno(stdout) == -2 ||
_get_osfhandle(fileno(stdout)) == -2)
freopen("CONOUT$", "w", stdout);
// Merge stderr into CONOUT$ since there isn't any `CONERR$`.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
if (_fileno(stderr) == -2 ||
_get_osfhandle(fileno(stderr)) == -2)
freopen("CONOUT$", "w", stderr);
if (_fileno(stdin) == -2 || _get_osfhandle(fileno(stdin)) == -2)
freopen("CONIN$", "r", stdin);
}
}
void
nsNativeAppSupportWin::CheckConsole() {
for ( int i = 1; i < gArgc; i++ ) {
@ -396,24 +431,7 @@ nsNativeAppSupportWin::CheckConsole() {
} else if ( strcmp( "-attach-console", gArgv[i] ) == 0
||
strcmp( "/attach-console", gArgv[i] ) == 0 ) {
// Try to attach console to the parent process.
// It will succeed when the parent process is a command line,
// so that stdio will be displayed in it.
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Change std handles to refer to new console handles.
// Before doing so, ensure that stdout/stderr haven't been
// redirected to a valid file
if (_fileno(stdout) == -1 ||
_get_osfhandle(fileno(stdout)) == -1)
freopen("CONOUT$", "w", stdout);
// Merge stderr into CONOUT$ since there isn't any `CONERR$`.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
if (_fileno(stderr) == -1 ||
_get_osfhandle(fileno(stderr)) == -1)
freopen("CONOUT$", "w", stderr);
if (_fileno(stdin) == -1 || _get_osfhandle(fileno(stdin)) == -1)
freopen("CONIN$", "r", stdin);
}
UseParentConsole();
}
}