2 * Unit tests for the debugger facility
4 * Copyright (c) 2007 Francois Gouget for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/test.h"
32 /* Copied from the process test */
33 static void get_file_name(char* buf)
38 GetTempPathA(sizeof(path), path);
39 GetTempFileNameA(path, "wt", 0, buf);
42 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
47 basename=strrchr(name, '\\');
48 basename=(basename ? basename+1 : name);
49 event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
51 sprintf(event_name, "start_%s", basename);
52 *start_event=CreateEvent(NULL, 0,0, event_name);
53 sprintf(event_name, "done_%s", basename);
54 *done_event=CreateEvent(NULL, 0,0, event_name);
55 HeapFree(GetProcessHeap(), 0, event_name);
58 static void log_pid(const char* logfile, DWORD pid)
63 hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
64 if (hFile == INVALID_HANDLE_VALUE)
66 WriteFile(hFile, &pid, sizeof(pid), &written, NULL);
70 static DWORD get_logged_pid(const char* logfile)
76 hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
77 if (hFile == INVALID_HANDLE_VALUE)
79 ok(0, "unable to open '%s'\n", logfile);
84 ret=ReadFile(hFile, &pid, sizeof(pid), &read, NULL);
85 ok(read == sizeof(pid), "wrong size for '%s': read=%d\n", logfile, read);
90 static void doCrash(int argc, char** argv)
95 logfile=(argc >= 4 ? argv[3] : NULL);
96 log_pid(logfile, GetCurrentProcessId());
99 trace("child: crashing...\n");
104 static void doDebugger(int argc, char** argv)
107 HANDLE start_event, done_event, debug_event;
110 ok(argc == 6, "wrong debugger argument count: %d\n", argc);
111 logfile=(argc >= 4 ? argv[3] : NULL);
112 pid=(argc >= 5 ? atol(argv[4]) : 0);
113 debug_event=(argc >= 6 ? (HANDLE)atol(argv[5]) : NULL);
114 if (debug_event && strcmp(myARGV[2], "dbgnoevent") != 0)
116 ok(SetEvent(debug_event), "debugger: SetEvent(debug_event) failed\n");
119 log_pid(logfile, pid);
120 get_events(logfile, &start_event, &done_event);
121 if (strcmp(myARGV[2], "dbgnoevent") != 0)
123 trace("debugger: waiting for the start signal...\n");
124 WaitForSingleObject(start_event, INFINITE);
127 ok(SetEvent(done_event), "debugger: SetEvent(done_event) failed\n");
128 trace("debugger: done debugging...\n");
130 /* Just exit with a known value */
131 ExitProcess(0xdeadbeef);
134 static void crash_and_debug(HKEY hkey, const char* argv0, const char* debugger)
137 HANDLE start_event, done_event;
139 char dbglog[MAX_PATH];
140 char childlog[MAX_PATH];
141 PROCESS_INFORMATION info;
142 STARTUPINFOA startup;
146 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
147 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
149 get_file_name(dbglog);
150 get_events(dbglog, &start_event, &done_event);
151 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(debugger)+1+strlen(dbglog)+34+1);
152 sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, debugger, dbglog);
153 ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
154 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
155 HeapFree(GetProcessHeap(), 0, cmd);
157 get_file_name(childlog);
158 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
159 sprintf(cmd, "%s debugger crash %s", argv0, childlog);
161 memset(&startup, 0, sizeof(startup));
162 startup.cb = sizeof(startup);
163 startup.dwFlags = STARTF_USESHOWWINDOW;
164 startup.wShowWindow = SW_SHOWNORMAL;
165 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
166 ok(ret, "CreateProcess: err=%d\n", GetLastError());
167 HeapFree(GetProcessHeap(), 0, cmd);
168 CloseHandle(info.hThread);
170 /* The process exits... */
171 trace("waiting for child exit...\n");
172 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
173 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
174 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
175 CloseHandle(info.hProcess);
177 /* ...before the debugger */
178 if (strcmp(debugger, "dbgnoevent") != 0)
179 ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
181 trace("waiting for the debugger...\n");
182 ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
184 pid1=get_logged_pid(dbglog);
185 pid2=get_logged_pid(childlog);
186 ok(pid1 == pid2, "the child and debugged pids don't match: %d != %d\n", pid1, pid2);
187 assert(DeleteFileA(dbglog) != 0);
188 assert(DeleteFileA(childlog) != 0);
191 static void test_ExitCode(void)
193 static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
194 char test_exe[MAX_PATH];
198 LPBYTE auto_val=NULL;
199 DWORD auto_size, auto_type;
200 LPBYTE debugger_val=NULL;
201 DWORD debugger_size, debugger_type;
203 GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
204 if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
205 strcat(test_exe, ".so");
206 if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
208 ok(0, "could not find the test executable '%s'\n", test_exe);
212 ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
213 if (ret == ERROR_SUCCESS)
216 ret=RegQueryValueExA(hkey, "auto", NULL, &auto_type, NULL, &auto_size);
217 if (ret == ERROR_SUCCESS)
219 auto_val=HeapAlloc(GetProcessHeap(), 0, auto_size);
220 RegQueryValueExA(hkey, "auto", NULL, &auto_type, auto_val, &auto_size);
224 ret=RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, NULL, &debugger_size);
225 if (ret == ERROR_SUCCESS)
227 debugger_val=HeapAlloc(GetProcessHeap(), 0, debugger_size);
228 RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, debugger_val, &debugger_size);
231 else if (ret == ERROR_ACCESS_DENIED)
233 skip("not enough privileges to change the debugger\n");
236 else if (ret != ERROR_FILE_NOT_FOUND)
238 ok(0, "could not open the AeDebug key: %d\n", ret);
242 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
243 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
244 crash_and_debug(hkey, test_exe, "dbgevent");
245 crash_and_debug(hkey, test_exe, "dbgnoevent");
247 if (disposition == REG_CREATED_NEW_KEY)
250 RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
256 RegSetValueExA(hkey, "auto", 0, auto_type, auto_val, auto_size);
257 HeapFree(GetProcessHeap(), 0, auto_val);
260 RegDeleteValueA(hkey, "auto");
263 RegSetValueExA(hkey, "debugger", 0, debugger_type, debugger_val, debugger_size);
264 HeapFree(GetProcessHeap(), 0, debugger_val);
267 RegDeleteValueA(hkey, "debugger");
275 myARGC=winetest_get_mainargs(&myARGV);
277 if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
279 doCrash(myARGC, myARGV);
281 else if (myARGC >= 3 &&
282 (strcmp(myARGV[2], "dbgevent") == 0 ||
283 strcmp(myARGV[2], "dbgnoevent") == 0))
285 doDebugger(myARGC, myARGV);