From 35b61f70da2ba219d94717e1c430837d3d5db85e Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 11 Aug 2024 21:00:08 +0200
Subject: [PATCH] sdlprocdump: add option to print debug stream
---
test/CMakeLists.txt | 2 +-
test/win32/sdlprocdump.c | 75 ++++++++++++++++++++++++++++++++++++----
2 files changed, 70 insertions(+), 7 deletions(-)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7dfeaf9324b6c..92c531ecc039a 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -46,7 +46,7 @@ if(WIN32 AND NOT WINDOWS_STORE)
set_property(TARGET sdlprocdump PROPERTY C_STANDARD "90")
SDL_AddCommonCompilerFlags(sdlprocdump)
if(SDLTEST_PROCDUMP)
- set(CMAKE_TEST_LAUNCHER "$<TARGET_FILE:sdlprocdump>")
+ set(CMAKE_TEST_LAUNCHER "$<TARGET_FILE:sdlprocdump>;--")
else()
set_property(TARGET sdlprocdump PROPERTY EXCLUDE_FROM_ALL "1")
endif()
diff --git a/test/win32/sdlprocdump.c b/test/win32/sdlprocdump.c
index ce05b7baff4a2..f7e832cf5f48f 100644
--- a/test/win32/sdlprocdump.c
+++ b/test/win32/sdlprocdump.c
@@ -1,14 +1,21 @@
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
#include <windows.h>
#include <dbghelp.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#define DUMP_FOLDER "minidumps"
#define APPNAME "SDLPROCDUMP"
+#define PRODCUMP_MIN(A,B) (((A) < (B)) ? (A) : (B))
+
#if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)
#define SDLPROCDUMP_CPU_X86 1
#elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
@@ -463,8 +470,13 @@ static void GetMSCExceptionName(HANDLE hProcess, ULONG_PTR *parameters, DWORD co
#undef CHECKED_ReadProcessMemory
}
+static void log_usage(const char *argv0) {
+ fprintf(stderr, "Usage: %s [--help] [--debug-stream] [--] PROGRAM [ARG1 [ARG2 [ARG3 ... ]]]\n", argv0);
+}
+
int main(int argc, char *argv[]) {
int i;
+ int cmd_i = 1;
size_t command_line_len = 0;
char *command_line;
STARTUPINFOA startup_info;
@@ -473,13 +485,30 @@ int main(int argc, char *argv[]) {
BOOL debugger_present;
DWORD exit_code;
DWORD creation_flags;
+ BOOL log_debug_stream = FALSE;
+
+ for (i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "--") == 0) {
+ cmd_i = i + 1;
+ break;
+ } else if (strcmp(argv[i], "--debug-stream") == 0) {
+ log_debug_stream = TRUE;
+ continue;
+ } else if (strcmp(argv[i], "--help") == 0) {
+ log_usage(argv[0]);
+ return 0;
+ } else {
+ cmd_i = i;
+ break;
+ }
+ }
- if (argc < 2) {
- fprintf(stderr, "Usage: %s PROGRAM [ARG1 [ARG2 [ARG3 ... ]]]\n", argv[0]);
+ if (cmd_i >= argc) {
+ log_usage(argv[0]);
return 1;
}
- for (i = 1; i < argc; i++) {
+ for (i = cmd_i; i < argc; i++) {
command_line_len += strlen(argv[i]) + 1;
}
command_line = malloc(command_line_len + 1);
@@ -488,7 +517,7 @@ int main(int argc, char *argv[]) {
return 1;
}
command_line[0] = '\0';
- for (i = 1; i < argc; i++) {
+ for (i = cmd_i; i < argc; i++) {
strcat_s(command_line, command_line_len, argv[i]);
if (i != argc - 1) {
strcat_s(command_line, command_line_len, " ");
@@ -504,7 +533,7 @@ int main(int argc, char *argv[]) {
creation_flags |= DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS;
}
success = CreateProcessA(
- argv[1], /* LPCSTR lpApplicationName, */
+ argv[cmd_i], /* LPCSTR lpApplicationName, */
command_line, /* LPSTR lpCommandLine, */
NULL, /* LPSECURITY_ATTRIBUTES lpProcessAttributes, */
NULL, /* LPSECURITY_ATTRIBUTES lpThreadAttributes, */
@@ -516,7 +545,7 @@ int main(int argc, char *argv[]) {
&process_information); /* LPPROCESS_INFORMATION lpProcessInformation */
if (!success) {
- printf_windows_message("Failed to start application");
+ printf_windows_message("Failed to start application \"%s\"", argv[cmd_i]);
return 1;
}
@@ -533,6 +562,40 @@ int main(int argc, char *argv[]) {
return 1;
}
switch (event.dwDebugEventCode) {
+ case OUTPUT_DEBUG_STRING_EVENT:
+ {
+ if (log_debug_stream) {
+ size_t bytes_read = 0;
+ union {
+ char char_buffer[512];
+ WCHAR wchar_buffer[256];
+ } buffer;
+ if (ReadProcessMemory(process_information.hProcess, event.u.DebugString.lpDebugStringData, buffer.char_buffer, PRODCUMP_MIN(sizeof(buffer), event.u.DebugString.nDebugStringLength), &bytes_read) && bytes_read) {
+ if (event.u.DebugString.fUnicode) {
+ size_t len = bytes_read / 2;
+ buffer.wchar_buffer[255] = '\0';
+ while (len > 0 && (buffer.wchar_buffer[len - 1] == '\0' || buffer.wchar_buffer[len - 1] == '\n' || buffer.wchar_buffer[len - 1] == '\r')) {
+ buffer.wchar_buffer[len - 1] = '\0';
+ len -= 1;
+ }
+ if (len > 0) {
+ printf("[" APPNAME "] (debug) %S\n", buffer.wchar_buffer);
+ }
+ } else {
+ size_t len = bytes_read;
+ buffer.char_buffer[511] = '\0';
+ while (len > 0 && (buffer.char_buffer[len - 1] == '\0' || buffer.char_buffer[len - 1] == '\n' || buffer.char_buffer[len - 1] == '\r')) {
+ buffer.char_buffer[len - 1] = '\0';
+ len -= 1;
+ }
+ if (len > 0) {
+ printf("[" APPNAME "] (debug) %s\n", buffer.char_buffer);
+ }
+ }
+ }
+ }
+ break;
+ }
case EXCEPTION_DEBUG_EVENT:
{
const BOOL cxx_exception = IsCXXException(event.u.Exception.ExceptionRecord.ExceptionCode);