Skip to content

Commit dc5f47a

Browse files
wrapper.c: Improve debugging to include the original command and processed command
1 parent 85794a9 commit dc5f47a

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SRCS = src/wrapper.c
99
CFLAGS = -O1 -fno-ident -fno-stack-protector -fomit-frame-pointer -fno-unwind-tables -fno-asynchronous-unwind-tables -falign-functions=1 -falign-jumps=1 -falign-loops=1 -fwhole-program -mconsole -municode -mno-stack-arg-probe -Xlinker --stack=0x200000,0x200000 -Wall -Wextra -ffreestanding
1010

1111
# Linker Flags
12-
LDFLAGS = -lurlmon -lkernel32 -lucrtbase -nostdlib -lshell32 -lshlwapi -luser32 -s
12+
LDFLAGS = -lurlmon -lkernel32 -lucrtbase -nostdlib -lshell32 -lshlwapi -s
1313

1414
# Build Directories
1515
BUILD_DIR64 = build/x64
@@ -35,6 +35,7 @@ all: $(TARGET32) $(TARGET64)
3535

3636
# Debug Build
3737
debug: CFLAGS += -DENABLE_DEBUG_LOG -g
38+
debug: LDFLAGS += -luser32
3839
debug: all
3940

4041
# 64-bit Executable

src/wrapper.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@ static inline BOOL is_single_or_last_option(WCHAR *opt) {
1313
}
1414

1515
#ifdef ENABLE_DEBUG_LOG
16-
// Function to log the Command Line to a File
17-
static void LogCommandLine(LPCWSTR cmdline) {
16+
// Function to log the Original and Processed Command Lines to a File
17+
static void LogCommandLine(LPCWSTR originalCmd, LPCWSTR processedCmd) {
1818
HANDLE hFile = CreateFileW(L"C:\\debug.log", FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE,
1919
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2020
if (hFile != INVALID_HANDLE_VALUE) {
2121
SetFilePointer(hFile, 0, NULL, FILE_END);
22-
WCHAR buffer[1024];
23-
wsprintfW(buffer, L"[%hs %hs] Command line: %ls\r\n", __DATE__, __TIME__, cmdline);
22+
WCHAR buffer[2048];
23+
wsprintfW(buffer, L"[%hs %hs] Original Command Line: %ls\r\n", __DATE__, __TIME__, originalCmd);
2424
DWORD written;
2525
WriteFile(hFile, buffer, lstrlenW(buffer) * sizeof(WCHAR), &written, NULL);
26+
27+
wsprintfW(buffer, L"[%hs %hs] Processed Command Line: %ls\r\n", __DATE__, __TIME__, processedCmd);
28+
WriteFile(hFile, buffer, lstrlenW(buffer) * sizeof(WCHAR), &written, NULL);
29+
2630
CloseHandle(hFile);
2731
}
2832
}
2933

30-
// Define a macro for logging
31-
#define LOG_CMDLINE(cmdline) LogCommandLine(cmdline)
34+
// Update the macro to log both original and processed command lines
35+
#define LOG_CMDLINE(original, processed) LogCommandLine(original, processed)
3236
#else
3337
// Define a no-op macro when debugging is disabled
34-
#define LOG_CMDLINE(cmdline) ((void)0)
38+
#define LOG_CMDLINE(original, processed) ((void)0)
3539
#endif
3640

3741
/*
@@ -106,7 +110,10 @@ int mainCRTStartup(void) {
106110
PROCESS_INFORMATION pi = {0};
107111
int i, j, argc;
108112

109-
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
113+
// Capture the original command line
114+
LPCWSTR originalCmd = GetCommandLineW();
115+
116+
argv = CommandLineToArgvW(originalCmd, &argc);
110117

111118
// Get the correct Program Files path based on the process architecture
112119
get_program_files_path(pwsh_pathW, MAX_PATH + 1);
@@ -229,8 +236,8 @@ int mainCRTStartup(void) {
229236

230237
fix_quotes(cmdlineW);
231238

232-
// **Debugging: Log the Command Line**
233-
LOG_CMDLINE(cmdlineW);
239+
// **Debugging: Log the Original and Processed Command Lines**
240+
LOG_CMDLINE(originalCmd, cmdlineW);
234241

235242
// Execute the command through pwsh.exe
236243
CreateProcessW(pwsh_pathW, cmdlineW, 0, 0, 0, 0, 0, 0, &si, &pi);

0 commit comments

Comments
 (0)