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"
28 #ifndef STATUS_DEBUGGER_INACTIVE
29 #define STATUS_DEBUGGER_INACTIVE ((NTSTATUS) 0xC0000354)
35 static BOOL (WINAPI *pDebugActiveProcessStop)(DWORD);
36 static BOOL (WINAPI *pDebugSetProcessKillOnExit)(BOOL);
38 /* Copied from the process test */
39 static void get_file_name(char* buf)
44 GetTempPathA(sizeof(path), path);
45 GetTempFileNameA(path, "wt", 0, buf);
48 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
53 basename=strrchr(name, '\\');
54 basename=(basename ? basename+1 : name);
55 event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
57 sprintf(event_name, "start_%s", basename);
58 *start_event=CreateEvent(NULL, 0,0, event_name);
59 sprintf(event_name, "done_%s", basename);
60 *done_event=CreateEvent(NULL, 0,0, event_name);
61 HeapFree(GetProcessHeap(), 0, event_name);
64 static void save_blackbox(const char* logfile, void* blackbox, int size)
69 hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
70 if (hFile == INVALID_HANDLE_VALUE)
72 WriteFile(hFile, blackbox, size, &written, NULL);
76 static int load_blackbox(const char* logfile, void* blackbox, int size)
82 hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
83 if (hFile == INVALID_HANDLE_VALUE)
85 ok(0, "unable to open '%s'\n", logfile);
88 ret=ReadFile(hFile, blackbox, size, &read, NULL);
89 ok(read == size, "wrong size for '%s': read=%d\n", logfile, read);
99 static void doCrash(int argc, char** argv)
105 crash_blackbox_t blackbox;
106 blackbox.pid=GetCurrentProcessId();
107 save_blackbox(argv[3], &blackbox, sizeof(blackbox));
111 trace("child: crashing...\n");
128 } debugger_blackbox_t;
130 static void doDebugger(int argc, char** argv)
133 debugger_blackbox_t blackbox;
134 HANDLE start_event, done_event, debug_event;
137 logfile=(argc >= 4 ? argv[3] : NULL);
138 blackbox.pid=(argc >= 5 ? atol(argv[4]) : 0);
140 blackbox.attach_err=0;
141 if (strstr(myARGV[2], "attach"))
143 blackbox.attach_rc=DebugActiveProcess(blackbox.pid);
144 if (!blackbox.attach_rc)
145 blackbox.attach_err=GetLastError();
148 blackbox.attach_rc=TRUE;
150 debug_event=(argc >= 6 ? (HANDLE)atol(argv[5]) : NULL);
151 blackbox.debug_err=0;
152 if (debug_event && strstr(myARGV[2], "event"))
154 blackbox.debug_rc=SetEvent(debug_event);
155 if (!blackbox.debug_rc)
156 blackbox.debug_err=GetLastError();
159 blackbox.debug_rc=TRUE;
161 get_events(logfile, &start_event, &done_event);
162 if (strstr(myARGV[2], "order"))
164 trace("debugger: waiting for the start signal...\n");
165 WaitForSingleObject(start_event, INFINITE);
168 blackbox.nokill_err=0;
169 if (strstr(myARGV[2], "nokill"))
171 blackbox.nokill_rc=pDebugSetProcessKillOnExit(FALSE);
172 if (!blackbox.nokill_rc)
173 blackbox.nokill_err=GetLastError();
176 blackbox.nokill_rc=TRUE;
178 blackbox.detach_err=0;
179 if (strstr(myARGV[2], "detach"))
181 blackbox.detach_rc=pDebugActiveProcessStop(blackbox.pid);
182 if (!blackbox.detach_rc)
183 blackbox.detach_err=GetLastError();
186 blackbox.detach_rc=TRUE;
188 save_blackbox(logfile, &blackbox, sizeof(blackbox));
189 trace("debugger: done debugging...\n");
190 SetEvent(done_event);
192 /* Just exit with a known value */
193 ExitProcess(0xdeadbeef);
196 static void crash_and_debug(HKEY hkey, const char* argv0, const char* dbgtasks)
199 HANDLE start_event, done_event;
201 char dbglog[MAX_PATH];
202 char childlog[MAX_PATH];
203 PROCESS_INFORMATION info;
204 STARTUPINFOA startup;
206 crash_blackbox_t crash_blackbox;
207 debugger_blackbox_t dbg_blackbox;
209 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
210 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
212 get_file_name(dbglog);
213 get_events(dbglog, &start_event, &done_event);
214 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+34+1);
215 sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, dbgtasks, dbglog);
216 ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
217 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
218 HeapFree(GetProcessHeap(), 0, cmd);
220 get_file_name(childlog);
221 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
222 sprintf(cmd, "%s debugger crash %s", argv0, childlog);
224 memset(&startup, 0, sizeof(startup));
225 startup.cb = sizeof(startup);
226 startup.dwFlags = STARTF_USESHOWWINDOW;
227 startup.wShowWindow = SW_SHOWNORMAL;
228 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
229 ok(ret, "CreateProcess: err=%d\n", GetLastError());
230 HeapFree(GetProcessHeap(), 0, cmd);
231 CloseHandle(info.hThread);
233 /* The process exits... */
234 trace("waiting for child exit...\n");
235 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
236 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
237 if (strstr(dbgtasks, "code2"))
239 /* If, after attaching to the debuggee, the debugger exits without
240 * detaching, then the debuggee gets a special exit code.
242 ok(exit_code == 0xffffffff || /* Win 9x */
243 exit_code == 0x80 || /* NT4 */
244 exit_code == STATUS_DEBUGGER_INACTIVE, /* Win >= XP */
245 "wrong exit code : %08x\n", exit_code);
248 ok(exit_code == STATUS_ACCESS_VIOLATION ||
249 exit_code == WAIT_ABANDONED, /* win2k3 */
250 "exit code = %08x instead of STATUS_ACCESS_VIOLATION or WAIT_ABANDONED\n", exit_code);
251 CloseHandle(info.hProcess);
253 /* ...before the debugger */
254 if (strstr(dbgtasks, "order"))
255 ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
257 trace("waiting for the debugger...\n");
258 ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
260 assert(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)));
261 assert(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)));
263 ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
264 ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
265 ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
266 ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
267 ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
268 ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
270 assert(DeleteFileA(dbglog) != 0);
271 assert(DeleteFileA(childlog) != 0);
274 static void crash_and_winedbg(HKEY hkey, const char* argv0)
278 PROCESS_INFORMATION info;
279 STARTUPINFOA startup;
282 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
283 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
285 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
286 sprintf(cmd, "%s debugger crash", argv0);
288 memset(&startup, 0, sizeof(startup));
289 startup.cb = sizeof(startup);
290 startup.dwFlags = STARTF_USESHOWWINDOW;
291 startup.wShowWindow = SW_SHOWNORMAL;
292 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
293 ok(ret, "CreateProcess: err=%d\n", GetLastError());
294 HeapFree(GetProcessHeap(), 0, cmd);
295 CloseHandle(info.hThread);
297 trace("waiting for child exit...\n");
298 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
299 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
300 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
301 CloseHandle(info.hProcess);
304 static void test_ExitCode(void)
306 static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
307 char test_exe[MAX_PATH];
311 LPBYTE auto_val=NULL;
312 DWORD auto_size, auto_type;
313 LPBYTE debugger_val=NULL;
314 DWORD debugger_size, debugger_type;
316 GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
317 if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
318 strcat(test_exe, ".so");
319 if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
321 ok(0, "could not find the test executable '%s'\n", test_exe);
325 ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
326 if (ret == ERROR_SUCCESS)
329 ret=RegQueryValueExA(hkey, "auto", NULL, &auto_type, NULL, &auto_size);
330 if (ret == ERROR_SUCCESS)
332 auto_val=HeapAlloc(GetProcessHeap(), 0, auto_size);
333 RegQueryValueExA(hkey, "auto", NULL, &auto_type, auto_val, &auto_size);
337 ret=RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, NULL, &debugger_size);
338 if (ret == ERROR_SUCCESS)
340 debugger_val=HeapAlloc(GetProcessHeap(), 0, debugger_size);
341 RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, debugger_val, &debugger_size);
342 trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug, debugger_val);
345 else if (ret == ERROR_ACCESS_DENIED)
347 skip("not enough privileges to change the debugger\n");
350 else if (ret != ERROR_FILE_NOT_FOUND)
352 ok(0, "could not open the AeDebug key: %d\n", ret);
356 if (debugger_val && debugger_type == REG_SZ &&
357 strstr((char*)debugger_val, "winedbg --auto"))
358 crash_and_winedbg(hkey, test_exe);
360 crash_and_debug(hkey, test_exe, "dbg,none");
361 crash_and_debug(hkey, test_exe, "dbg,event,order");
362 crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
363 if (pDebugSetProcessKillOnExit)
364 crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
365 if (pDebugActiveProcessStop)
366 crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
368 if (disposition == REG_CREATED_NEW_KEY)
371 RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
377 RegSetValueExA(hkey, "auto", 0, auto_type, auto_val, auto_size);
378 HeapFree(GetProcessHeap(), 0, auto_val);
381 RegDeleteValueA(hkey, "auto");
384 RegSetValueExA(hkey, "debugger", 0, debugger_type, debugger_val, debugger_size);
385 HeapFree(GetProcessHeap(), 0, debugger_val);
388 RegDeleteValueA(hkey, "debugger");
397 hdll=GetModuleHandle("kernel32.dll");
398 pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
399 pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
401 myARGC=winetest_get_mainargs(&myARGV);
402 if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
404 doCrash(myARGC, myARGV);
406 else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
408 doDebugger(myARGC, myARGV);