[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[virt-tools-list] [PATCH virt-viewer 4/6] windows: add debug-helper



This is a simple program that will set some debug variable, and run
gdb and wait until it finished. This makes it possible to debug
"remote-viewer --spice-controller" easily, by setting the necessary
variables and keeping the parent process running (the activex whatches
its death)
---
 src/Makefile.am    |  5 ++++
 src/debug-helper.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 src/debug-helper.c

diff --git a/src/Makefile.am b/src/Makefile.am
index a62a8c0..fc357d3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -157,6 +157,11 @@ virt-viewer_rc.$(OBJEXT): $(VIRT_VIEWER_RES) $(ICONDIR)/virt-viewer.ico
 		-i $< -o $@
 LDADD += virt-viewer_rc.$(OBJEXT)
 MAINTAINERCLEANFILES += virt-viewer_rc.$(OBJEXT)
+
+bin_PROGRAMS += debug-helper
+debug_helper_SOURCES = debug-helper.c
+debug_helper_LDFLAGS = $(GLIB2_LIBS) -Wl,--subsystem,windows
+debug_helper_CFLAGS = $(GLIB2_CFLAGS)
 endif
 
 -include $(top_srcdir)/git.mk
diff --git a/src/debug-helper.c b/src/debug-helper.c
new file mode 100644
index 0000000..754f7cf
--- /dev/null
+++ b/src/debug-helper.c
@@ -0,0 +1,70 @@
+#include <windows.h>
+#include <stdio.h>
+#include <conio.h>
+#include <process.h>
+#include <glib.h>
+
+static gchar*
+get_program_path(void)
+{
+    gchar *utf8_buf, *path = NULL;
+    wchar_t buf[MAX_PATH+1];
+
+    if (GetModuleFileNameW(GetModuleHandle (NULL), buf, G_N_ELEMENTS (buf)) > 0) {
+        utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
+        path = g_path_get_dirname (utf8_buf);
+        g_free (utf8_buf);
+    }
+
+    return path;
+}
+
+int
+main(int argc, char *argv[])
+{
+    char pipe[2048];
+    STARTUPINFO si = { 0, };
+    PROCESS_INFORMATION pi = { 0, };
+    gchar *program_path = get_program_path();
+    gchar *command;
+    int rv = 0;
+
+    argv[0] = "gdb -ex run --args";
+    command = g_strjoinv(" ", argv);
+
+    snprintf(pipe, sizeof(pipe), "\\\\.\\pipe\\SpiceController-%lu", GetCurrentProcessId());
+
+    SetEnvironmentVariable("SPICE_DEBUG", "1");
+    SetEnvironmentVariable("G_MESSAGES_DEBUG", "all");
+    SetEnvironmentVariable("SPICE_XPI_NAMEDPIPE", pipe);
+
+    si.cb = sizeof(si);
+    if (!CreateProcess(NULL,
+                       command,
+                       NULL,
+                       NULL,
+                       FALSE,
+                       0,
+                       NULL,
+                       program_path,
+                       &si,
+                       &pi)) {
+        printf("CreateProcess failed (%ld).\n", GetLastError());
+        rv = 1;
+        goto end;
+    }
+
+    // Wait until child process exits
+    WaitForSingleObject(pi.hProcess, INFINITE);
+
+    // Close process and thread handles
+    CloseHandle(pi.hProcess);
+    CloseHandle(pi.hThread);
+
+end:
+    g_free(program_path);
+    g_free(command);
+
+    exit(rv);
+    return rv;
+}
-- 
1.7.11.7


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]