Skip to contentSkip to conversation

vl: prevent SDL replacing 'main' on win32 platforms

PATCH New Daniel P. Berrange 1 participant 2 messages Aug 13, 2015 – Aug 14, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vl: prevent SDL replacing 'main' on win32 platforms

The SDL provided 'main' replacement on Win32 redirects stdout and stderr to text files, which in turn prevents QEMU from using the console for the monitor (eg -monitor stdio breaks), and also hides error messages and -help output from the user.

Block SDL's replacement by simply undefining the 'main' macro, so the QEMU binaries' 'main' symbol takes priority over that found in SDL.dll

The SDL replacement is left in place for other platforms, since it might be doing something more important on them. It would be worth investigating if it can be removed for OS-X though as a future cleanup.

---
 vl.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
vl.c +17 -1
index 0adbbd6..bb9ed8b 100644
@@ -39,7 +39,23 @@
 #endif
 
 #ifdef CONFIG_SDL
-#if defined(__APPLE__) || defined(main)
+#if defined(_WIN32)
+/*
+ * On Win32 SDL tries to rename 'main' to 'SDL_main' so it can
+ * insert itself into the startup sequence, by passing the
+ * compiler flag -Dmain=SDL_main. It does this so it can mess
+ * with startup, amongst other dubious things, replacing stdout
+ * and stderr with streams pointing to a file instead of the
+ * console. This in turn breaks ability of QEMU to use stdio for
+ * things like the monitor (eg no more -monitor stdio), and error
+ * reporting, even if SDL is not the current UI frontend.
+ *
+ * The SDL startup hacks appear to not be critical to the correct
+ * functioning of SDL on Win32, so block SDL's attempt to hijack
+ * 'main' and thus avoid broken console I/O
+ */
+#undef main
+#elif defined(__APPLE__) || defined(main)
 #include <SDL.h>
 int qemu_main(int argc, char **argv, char **envp);
 int main(int argc, char **argv)
Signature · 1 line
2.4.3
#1 lore raw

PATCH 1/1 vl: redirect stdio to a file in Windows GUI build

If linked to the windows subsystem (-mwindows gcc arg) then there will be no console available for stdout/err to send data to. Use the same approach as SDL by redirecting stdout/err to text files in the current directory. If linked to the console subsystem then leave stdout/err untouched.

The redirect can be disabled with QEMU_NO_STDIO_REDIRECT env variable

The result is that qemu-system-x86_64.exe can use stdio in the same manner as on any UNIX platform. The qemu-system-x86_64w.exe binary will log to text files and options like -monitor stdio will not be available.

---
 vl.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
vl.c +16 -0
index bb9ed8b..c9997bf 100644
@@ -2995,6 +2995,22 @@ int main(int argc, char **argv, char **envp)
     Error *main_loop_err = NULL;
     Error *err = NULL;
 
+#ifdef _WIN32
+    /*
+     * If we're linked with -nwindows GetConsoleWindow returns
+     * NULL, so we know stdout/err are not available, so lets
+     * redirect them to a file. If linked to console subsystem
+     * then we can use stdout/err as normal
+     */
+    if (GetConsoleWindow() == NULL &&
+        getenv("QEMU_NO_STDIO_REDIRECT") == NULL) {
+        freopen("stdout.txt", "w", stdout);
+        freopen("stderr.txt", "w", stderr);
+        setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffering */
+        setbuf(stderr, NULL); /* No buffering */
+    }
+#endif /* _WIN32 */
+
     qemu_init_cpu_loop();
     qemu_mutex_lock_iothread();
 
Signature · 1 line
2.4.3