push 6e61d6ca5bcaf95ac09a664b4ba4f88238c927be
[wine/hacks.git] / dlls / kernel32 / tests / debugger.c
blobd37217a8bd30df6e21b391251eb931ac0879be81
1 /*
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
21 #include <stdio.h>
22 #include <assert.h>
24 #include <windows.h>
25 #include <winternl.h>
26 #include <winreg.h>
27 #include "wine/test.h"
29 #ifndef STATUS_DEBUGGER_INACTIVE
30 #define STATUS_DEBUGGER_INACTIVE ((NTSTATUS) 0xC0000354)
31 #endif
33 #ifdef __GNUC__
34 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
35 #else
36 #define PRINTF_ATTR(fmt,args)
37 #endif
39 #define child_ok (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_child_ok
41 static int myARGC;
42 static char** myARGV;
44 static BOOL (WINAPI *pCheckRemoteDebuggerPresent)(HANDLE,PBOOL);
45 static BOOL (WINAPI *pDebugActiveProcessStop)(DWORD);
46 static BOOL (WINAPI *pDebugSetProcessKillOnExit)(BOOL);
48 static LONG child_failures;
50 static void PRINTF_ATTR(2, 3) test_child_ok(int condition, const char *msg, ...)
52 va_list valist;
54 va_start(valist, msg);
55 winetest_vok(condition, msg, valist);
56 va_end(valist);
57 if (!condition) ++child_failures;
60 /* Copied from the process test */
61 static void get_file_name(char* buf)
63 char path[MAX_PATH];
65 buf[0] = '\0';
66 GetTempPathA(sizeof(path), path);
67 GetTempFileNameA(path, "wt", 0, buf);
70 typedef struct tag_reg_save_value
72 const char *name;
73 DWORD type;
74 BYTE *data;
75 DWORD size;
76 } reg_save_value;
78 static DWORD save_value(HKEY hkey, const char *value, reg_save_value *saved)
80 DWORD ret;
81 saved->name=value;
82 saved->data=0;
83 saved->size=0;
84 ret=RegQueryValueExA(hkey, value, NULL, &saved->type, NULL, &saved->size);
85 if (ret == ERROR_SUCCESS)
87 saved->data=HeapAlloc(GetProcessHeap(), 0, saved->size);
88 RegQueryValueExA(hkey, value, NULL, &saved->type, saved->data, &saved->size);
90 return ret;
93 static void restore_value(HKEY hkey, reg_save_value *saved)
95 if (saved->data)
97 RegSetValueExA(hkey, saved->name, 0, saved->type, saved->data, saved->size);
98 HeapFree(GetProcessHeap(), 0, saved->data);
100 else
101 RegDeleteValueA(hkey, saved->name);
104 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
106 const char* basename;
107 char* event_name;
109 basename=strrchr(name, '\\');
110 basename=(basename ? basename+1 : name);
111 event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
113 sprintf(event_name, "start_%s", basename);
114 *start_event=CreateEvent(NULL, 0,0, event_name);
115 sprintf(event_name, "done_%s", basename);
116 *done_event=CreateEvent(NULL, 0,0, event_name);
117 HeapFree(GetProcessHeap(), 0, event_name);
120 static void save_blackbox(const char* logfile, void* blackbox, int size)
122 HANDLE hFile;
123 DWORD written;
125 hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
126 if (hFile == INVALID_HANDLE_VALUE)
127 return;
128 WriteFile(hFile, blackbox, size, &written, NULL);
129 CloseHandle(hFile);
132 static int load_blackbox(const char* logfile, void* blackbox, int size)
134 HANDLE hFile;
135 DWORD read;
136 BOOL ret;
138 hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
139 if (hFile == INVALID_HANDLE_VALUE)
141 ok(0, "unable to open '%s'\n", logfile);
142 return 0;
144 ret=ReadFile(hFile, blackbox, size, &read, NULL);
145 ok(read == size, "wrong size for '%s': read=%d\n", logfile, read);
146 CloseHandle(hFile);
147 return 1;
150 typedef struct
152 DWORD pid;
153 } crash_blackbox_t;
155 static void doCrash(int argc, char** argv)
157 char* p;
159 if (argc >= 4)
161 crash_blackbox_t blackbox;
162 blackbox.pid=GetCurrentProcessId();
163 save_blackbox(argv[3], &blackbox, sizeof(blackbox));
166 /* Just crash */
167 trace("child: crashing...\n");
168 p=NULL;
169 *p=0;
172 typedef struct
174 int argc;
175 DWORD pid;
176 BOOL debug_rc;
177 DWORD debug_err;
178 BOOL attach_rc;
179 DWORD attach_err;
180 BOOL nokill_rc;
181 DWORD nokill_err;
182 BOOL detach_rc;
183 DWORD detach_err;
184 } debugger_blackbox_t;
186 static void doDebugger(int argc, char** argv)
188 const char* logfile;
189 debugger_blackbox_t blackbox;
190 HANDLE start_event = 0, done_event = 0, debug_event;
192 blackbox.argc=argc;
193 logfile=(argc >= 4 ? argv[3] : NULL);
194 blackbox.pid=(argc >= 5 ? atol(argv[4]) : 0);
196 blackbox.attach_err=0;
197 if (strstr(myARGV[2], "attach"))
199 blackbox.attach_rc=DebugActiveProcess(blackbox.pid);
200 if (!blackbox.attach_rc)
201 blackbox.attach_err=GetLastError();
203 else
204 blackbox.attach_rc=TRUE;
206 debug_event=(argc >= 6 ? (HANDLE)(INT_PTR)atol(argv[5]) : NULL);
207 blackbox.debug_err=0;
208 if (debug_event && strstr(myARGV[2], "event"))
210 blackbox.debug_rc=SetEvent(debug_event);
211 if (!blackbox.debug_rc)
212 blackbox.debug_err=GetLastError();
214 else
215 blackbox.debug_rc=TRUE;
217 if (logfile)
219 get_events(logfile, &start_event, &done_event);
222 if (strstr(myARGV[2], "order"))
224 trace("debugger: waiting for the start signal...\n");
225 WaitForSingleObject(start_event, INFINITE);
228 blackbox.nokill_err=0;
229 if (strstr(myARGV[2], "nokill"))
231 blackbox.nokill_rc=pDebugSetProcessKillOnExit(FALSE);
232 if (!blackbox.nokill_rc)
233 blackbox.nokill_err=GetLastError();
235 else
236 blackbox.nokill_rc=TRUE;
238 blackbox.detach_err=0;
239 if (strstr(myARGV[2], "detach"))
241 blackbox.detach_rc=pDebugActiveProcessStop(blackbox.pid);
242 if (!blackbox.detach_rc)
243 blackbox.detach_err=GetLastError();
245 else
246 blackbox.detach_rc=TRUE;
248 if (logfile)
250 save_blackbox(logfile, &blackbox, sizeof(blackbox));
252 trace("debugger: done debugging...\n");
253 SetEvent(done_event);
255 /* Just exit with a known value */
256 ExitProcess(0xdeadbeef);
259 static void crash_and_debug(HKEY hkey, const char* argv0, const char* dbgtasks)
261 DWORD ret;
262 HANDLE start_event, done_event;
263 char* cmd;
264 char dbglog[MAX_PATH];
265 char childlog[MAX_PATH];
266 PROCESS_INFORMATION info;
267 STARTUPINFOA startup;
268 DWORD exit_code;
269 crash_blackbox_t crash_blackbox;
270 debugger_blackbox_t dbg_blackbox;
272 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
273 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
275 get_file_name(dbglog);
276 get_events(dbglog, &start_event, &done_event);
277 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+34+1);
278 sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, dbgtasks, dbglog);
279 ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
280 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
281 HeapFree(GetProcessHeap(), 0, cmd);
283 get_file_name(childlog);
284 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
285 sprintf(cmd, "%s debugger crash %s", argv0, childlog);
287 memset(&startup, 0, sizeof(startup));
288 startup.cb = sizeof(startup);
289 startup.dwFlags = STARTF_USESHOWWINDOW;
290 startup.wShowWindow = SW_SHOWNORMAL;
291 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
292 ok(ret, "CreateProcess: err=%d\n", GetLastError());
293 HeapFree(GetProcessHeap(), 0, cmd);
294 CloseHandle(info.hThread);
296 /* The process exits... */
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 if (strstr(dbgtasks, "code2"))
302 /* If, after attaching to the debuggee, the debugger exits without
303 * detaching, then the debuggee gets a special exit code.
305 ok(exit_code == STATUS_DEBUGGER_INACTIVE ||
306 broken(exit_code == STATUS_ACCESS_VIOLATION) || /* Intermittent Vista+ */
307 broken(exit_code == 0xffffffff) || /* Win9x */
308 broken(exit_code == WAIT_ABANDONED), /* NT4, W2K */
309 "wrong exit code : %08x\n", exit_code);
311 else
312 ok(exit_code == STATUS_ACCESS_VIOLATION ||
313 broken(exit_code == WAIT_ABANDONED) || /* NT4, W2K, W2K3 */
314 broken(exit_code == 0xffffffff), /* Win9x, WinME */
315 "wrong exit code : %08x\n", exit_code);
316 CloseHandle(info.hProcess);
318 /* ...before the debugger */
319 if (strstr(dbgtasks, "order"))
320 ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
322 trace("waiting for the debugger...\n");
323 ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
325 assert(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)));
326 assert(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)));
328 ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
329 ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
330 ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
331 ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
332 ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
333 ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
335 assert(DeleteFileA(dbglog) != 0);
336 assert(DeleteFileA(childlog) != 0);
339 static void crash_and_winedbg(HKEY hkey, const char* argv0)
341 DWORD ret;
342 char* cmd;
343 PROCESS_INFORMATION info;
344 STARTUPINFOA startup;
345 DWORD exit_code;
347 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
348 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
350 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
351 sprintf(cmd, "%s debugger crash", argv0);
353 memset(&startup, 0, sizeof(startup));
354 startup.cb = sizeof(startup);
355 startup.dwFlags = STARTF_USESHOWWINDOW;
356 startup.wShowWindow = SW_SHOWNORMAL;
357 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
358 ok(ret, "CreateProcess: err=%d\n", GetLastError());
359 HeapFree(GetProcessHeap(), 0, cmd);
360 CloseHandle(info.hThread);
362 trace("waiting for child exit...\n");
363 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
364 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
365 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
366 CloseHandle(info.hProcess);
369 static void test_ExitCode(void)
371 static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
372 static const char* WineDbg="Software\\Wine\\WineDbg";
373 char test_exe[MAX_PATH];
374 DWORD ret;
375 HKEY hkey;
376 DWORD disposition;
377 reg_save_value auto_value;
378 reg_save_value debugger_value;
380 GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
381 if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
382 strcat(test_exe, ".so");
383 if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
385 ok(0, "could not find the test executable '%s'\n", test_exe);
386 return;
389 ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
390 if (ret == ERROR_SUCCESS)
392 save_value(hkey, "auto", &auto_value);
393 save_value(hkey, "debugger", &debugger_value);
394 trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug, debugger_value.data);
396 else if (ret == ERROR_ACCESS_DENIED)
398 skip("not enough privileges to change the debugger\n");
399 return;
401 else if (ret != ERROR_FILE_NOT_FOUND)
403 ok(0, "could not open the AeDebug key: %d\n", ret);
404 return;
407 if (debugger_value.data && debugger_value.type == REG_SZ &&
408 strstr((char*)debugger_value.data, "winedbg --auto"))
410 HKEY hkeyWinedbg;
411 ret=RegCreateKeyA(HKEY_CURRENT_USER, WineDbg, &hkeyWinedbg);
412 if (ret == ERROR_SUCCESS)
414 static DWORD zero;
415 reg_save_value crash_dlg_value;
416 save_value(hkeyWinedbg, "ShowCrashDialog", &crash_dlg_value);
417 RegSetValueExA(hkeyWinedbg, "ShowCrashDialog", 0, REG_DWORD, (BYTE *)&zero, sizeof(DWORD));
418 crash_and_winedbg(hkey, test_exe);
419 restore_value(hkeyWinedbg, &crash_dlg_value);
420 RegCloseKey(hkeyWinedbg);
422 else
423 ok(0, "Couldn't access WineDbg Key - error %u\n", ret);
426 if (winetest_interactive)
427 /* Since the debugging process never sets the debug event, it isn't recognized
428 as a valid debugger and, after the debugger exits, Windows will show a dialog box
429 asking the user what to do */
430 crash_and_debug(hkey, test_exe, "dbg,none");
431 else
432 skip("\"none\" debugger test needs user interaction\n");
433 if (disposition == REG_CREATED_NEW_KEY)
434 win_skip("'dbg,event,order' test doesn't finish on Win9x/WinMe\n");
435 else
436 crash_and_debug(hkey, test_exe, "dbg,event,order");
437 crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
438 if (pDebugSetProcessKillOnExit)
439 crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
440 else
441 win_skip("DebugSetProcessKillOnExit is not available\n");
442 if (pDebugActiveProcessStop)
443 crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
444 else
445 win_skip("DebugActiveProcessStop is not available\n");
447 if (disposition == REG_CREATED_NEW_KEY)
449 RegCloseKey(hkey);
450 RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
452 else
454 restore_value(hkey, &auto_value);
455 restore_value(hkey, &debugger_value);
456 RegCloseKey(hkey);
460 static void test_RemoteDebugger(void)
462 BOOL bret, present;
463 if(!pCheckRemoteDebuggerPresent)
465 win_skip("CheckRemoteDebuggerPresent is not available\n");
466 return;
468 present = TRUE;
469 SetLastError(0xdeadbeef);
470 bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present);
471 ok(bret , "expected CheckRemoteDebuggerPresent to succeed\n");
472 ok(0xdeadbeef == GetLastError(),
473 "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());
475 present = TRUE;
476 SetLastError(0xdeadbeef);
477 bret = pCheckRemoteDebuggerPresent(NULL,&present);
478 ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
479 ok(present, "expected parameter to be unchanged\n");
480 ok(ERROR_INVALID_PARAMETER == GetLastError(),
481 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
483 SetLastError(0xdeadbeef);
484 bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL);
485 ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
486 ok(ERROR_INVALID_PARAMETER == GetLastError(),
487 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
490 struct child_blackbox
492 LONG failures;
495 static void doChild(int argc, char **argv)
497 struct child_blackbox blackbox;
498 const char *blackbox_file;
499 HANDLE parent;
500 DWORD ppid;
501 BOOL ret;
503 blackbox_file = argv[4];
504 sscanf(argv[3], "%08x", &ppid);
506 parent = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ppid);
507 child_ok(!!parent, "OpenProcess failed, last error %#x.\n", GetLastError());
509 ret = DebugActiveProcess(ppid);
510 child_ok(ret, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
512 ret = pDebugActiveProcessStop(ppid);
513 child_ok(ret, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());
515 ret = CloseHandle(parent);
516 child_ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
518 blackbox.failures = child_failures;
519 save_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
522 static void test_debug_loop(int argc, char **argv)
524 const char *arguments = " debugger child ";
525 struct child_blackbox blackbox;
526 char blackbox_file[MAX_PATH];
527 PROCESS_INFORMATION pi;
528 STARTUPINFOA si;
529 DWORD pid;
530 char *cmd;
531 BOOL ret;
533 if (!pDebugActiveProcessStop)
535 win_skip("DebugActiveProcessStop not available, skipping test.\n");
536 return;
539 pid = GetCurrentProcessId();
540 get_file_name(blackbox_file);
541 cmd = HeapAlloc(GetProcessHeap(), 0, strlen(argv[0]) + strlen(arguments) + strlen(blackbox_file) + 10);
542 sprintf(cmd, "%s%s%08x %s", argv[0], arguments, pid, blackbox_file);
544 memset(&si, 0, sizeof(si));
545 si.cb = sizeof(si);
546 ret = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
547 ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
549 HeapFree(GetProcessHeap(), 0, cmd);
551 for (;;)
553 DEBUG_EVENT ev;
555 ret = WaitForDebugEvent(&ev, INFINITE);
556 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
557 if (!ret) break;
559 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
561 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
562 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
563 if (!ret) break;
566 ret = CloseHandle(pi.hThread);
567 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
568 ret = CloseHandle(pi.hProcess);
569 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
571 load_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
572 ok(!blackbox.failures, "Got %d failures from child process.\n", blackbox.failures);
574 ret = DeleteFileA(blackbox_file);
575 ok(ret, "DeleteFileA failed, last error %#x.\n", GetLastError());
578 START_TEST(debugger)
580 HMODULE hdll;
582 hdll=GetModuleHandle("kernel32.dll");
583 pCheckRemoteDebuggerPresent=(void*)GetProcAddress(hdll, "CheckRemoteDebuggerPresent");
584 pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
585 pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
587 myARGC=winetest_get_mainargs(&myARGV);
588 if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
590 doCrash(myARGC, myARGV);
592 else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
594 doDebugger(myARGC, myARGV);
596 else if (myARGC >= 5 && !strcmp(myARGV[2], "child"))
598 doChild(myARGC, myARGV);
600 else
602 test_ExitCode();
603 test_RemoteDebugger();
604 test_debug_loop(myARGC, myARGV);