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
27 #include "wine/test.h"
29 #ifndef STATUS_DEBUGGER_INACTIVE
30 #define STATUS_DEBUGGER_INACTIVE ((NTSTATUS) 0xC0000354)
34 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
36 #define PRINTF_ATTR(fmt,args)
39 #define child_ok (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_child_ok
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
, ...)
54 va_start(valist
, msg
);
55 winetest_vok(condition
, msg
, valist
);
57 if (!condition
) ++child_failures
;
60 /* Copied from the process test */
61 static void get_file_name(char* buf
)
66 GetTempPathA(sizeof(path
), path
);
67 GetTempFileNameA(path
, "wt", 0, buf
);
70 typedef struct tag_reg_save_value
78 static DWORD
save_value(HKEY hkey
, const char *value
, reg_save_value
*saved
)
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
);
93 static void restore_value(HKEY hkey
, reg_save_value
*saved
)
97 RegSetValueExA(hkey
, saved
->name
, 0, saved
->type
, saved
->data
, saved
->size
);
98 HeapFree(GetProcessHeap(), 0, saved
->data
);
101 RegDeleteValueA(hkey
, saved
->name
);
104 static void get_events(const char* name
, HANDLE
*start_event
, HANDLE
*done_event
)
106 const char* basename
;
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
)
125 hFile
=CreateFileA(logfile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
126 if (hFile
== INVALID_HANDLE_VALUE
)
128 WriteFile(hFile
, blackbox
, size
, &written
, NULL
);
132 static int load_blackbox(const char* logfile
, void* blackbox
, int size
)
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
);
144 ret
=ReadFile(hFile
, blackbox
, size
, &read
, NULL
);
145 ok(read
== size
, "wrong size for '%s': read=%d\n", logfile
, read
);
155 static void doCrash(int argc
, char** argv
)
161 crash_blackbox_t blackbox
;
162 blackbox
.pid
=GetCurrentProcessId();
163 save_blackbox(argv
[3], &blackbox
, sizeof(blackbox
));
167 trace("child: crashing...\n");
184 } debugger_blackbox_t
;
186 static void doDebugger(int argc
, char** argv
)
189 debugger_blackbox_t blackbox
;
190 HANDLE start_event
= 0, done_event
= 0, debug_event
;
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();
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();
215 blackbox
.debug_rc
=TRUE
;
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();
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();
246 blackbox
.detach_rc
=TRUE
;
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
)
262 HANDLE start_event
, done_event
;
264 char dbglog
[MAX_PATH
];
265 char childlog
[MAX_PATH
];
266 PROCESS_INFORMATION info
;
267 STARTUPINFOA startup
;
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
);
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
)
343 PROCESS_INFORMATION info
;
344 STARTUPINFOA startup
;
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
];
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
);
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");
401 else if (ret
!= ERROR_FILE_NOT_FOUND
)
403 ok(0, "could not open the AeDebug key: %d\n", ret
);
407 if (debugger_value
.data
&& debugger_value
.type
== REG_SZ
&&
408 strstr((char*)debugger_value
.data
, "winedbg --auto"))
411 ret
=RegCreateKeyA(HKEY_CURRENT_USER
, WineDbg
, &hkeyWinedbg
);
412 if (ret
== ERROR_SUCCESS
)
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
);
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");
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");
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");
441 win_skip("DebugSetProcessKillOnExit is not available\n");
442 if (pDebugActiveProcessStop
)
443 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,detach");
445 win_skip("DebugActiveProcessStop is not available\n");
447 if (disposition
== REG_CREATED_NEW_KEY
)
450 RegDeleteKeyA(HKEY_LOCAL_MACHINE
, AeDebug
);
454 restore_value(hkey
, &auto_value
);
455 restore_value(hkey
, &debugger_value
);
460 static void test_RemoteDebugger(void)
463 if(!pCheckRemoteDebuggerPresent
)
465 win_skip("CheckRemoteDebuggerPresent is not available\n");
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());
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
495 static void doChild(int argc
, char **argv
)
497 struct child_blackbox blackbox
;
498 const char *blackbox_file
;
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
;
533 if (!pDebugActiveProcessStop
)
535 win_skip("DebugActiveProcessStop not available, skipping test.\n");
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
));
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
);
555 ret
= WaitForDebugEvent(&ev
, INFINITE
);
556 ok(ret
, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
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());
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());
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
);
603 test_RemoteDebugger();
604 test_debug_loop(myARGC
, myARGV
);