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
);
47 static BOOL (WINAPI
*pIsDebuggerPresent
)(void);
48 static struct _TEB
* (WINAPI
*pNtCurrentTeb
)(void);
50 static LONG child_failures
;
52 static void PRINTF_ATTR(2, 3) test_child_ok(int condition
, const char *msg
, ...)
56 va_start(valist
, msg
);
57 winetest_vok(condition
, msg
, valist
);
59 if (!condition
) ++child_failures
;
62 /* Copied from the process test */
63 static void get_file_name(char* buf
)
68 GetTempPathA(sizeof(path
), path
);
69 GetTempFileNameA(path
, "wt", 0, buf
);
72 typedef struct tag_reg_save_value
80 static DWORD
save_value(HKEY hkey
, const char *value
, reg_save_value
*saved
)
86 ret
=RegQueryValueExA(hkey
, value
, NULL
, &saved
->type
, NULL
, &saved
->size
);
87 if (ret
== ERROR_SUCCESS
)
89 saved
->data
=HeapAlloc(GetProcessHeap(), 0, saved
->size
);
90 RegQueryValueExA(hkey
, value
, NULL
, &saved
->type
, saved
->data
, &saved
->size
);
95 static void restore_value(HKEY hkey
, reg_save_value
*saved
)
99 RegSetValueExA(hkey
, saved
->name
, 0, saved
->type
, saved
->data
, saved
->size
);
100 HeapFree(GetProcessHeap(), 0, saved
->data
);
103 RegDeleteValueA(hkey
, saved
->name
);
106 static void get_events(const char* name
, HANDLE
*start_event
, HANDLE
*done_event
)
108 const char* basename
;
111 basename
=strrchr(name
, '\\');
112 basename
=(basename
? basename
+1 : name
);
113 event_name
=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename
)+1);
115 sprintf(event_name
, "start_%s", basename
);
116 *start_event
=CreateEvent(NULL
, 0,0, event_name
);
117 sprintf(event_name
, "done_%s", basename
);
118 *done_event
=CreateEvent(NULL
, 0,0, event_name
);
119 HeapFree(GetProcessHeap(), 0, event_name
);
122 static void save_blackbox(const char* logfile
, void* blackbox
, int size
)
127 hFile
=CreateFileA(logfile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
128 if (hFile
== INVALID_HANDLE_VALUE
)
130 WriteFile(hFile
, blackbox
, size
, &written
, NULL
);
134 static int load_blackbox(const char* logfile
, void* blackbox
, int size
)
140 hFile
=CreateFileA(logfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
141 if (hFile
== INVALID_HANDLE_VALUE
)
143 ok(0, "unable to open '%s'\n", logfile
);
146 SetLastError(0xdeadbeef);
147 ret
=ReadFile(hFile
, blackbox
, size
, &read
, NULL
);
148 ok(ret
, "ReadFile failed: %d\n", GetLastError());
149 ok(read
== size
, "wrong size for '%s': read=%d\n", logfile
, read
);
159 static void doCrash(int argc
, char** argv
)
163 /* make sure the exception gets to the debugger */
165 SetUnhandledExceptionFilter( NULL
);
169 crash_blackbox_t blackbox
;
170 blackbox
.pid
=GetCurrentProcessId();
171 save_blackbox(argv
[3], &blackbox
, sizeof(blackbox
));
175 trace("child: crashing...\n");
192 } debugger_blackbox_t
;
194 static void doDebugger(int argc
, char** argv
)
197 debugger_blackbox_t blackbox
;
198 HANDLE start_event
= 0, done_event
= 0, debug_event
;
201 logfile
=(argc
>= 4 ? argv
[3] : NULL
);
202 blackbox
.pid
=(argc
>= 5 ? atol(argv
[4]) : 0);
204 blackbox
.attach_err
=0;
205 if (strstr(myARGV
[2], "attach"))
207 blackbox
.attach_rc
=DebugActiveProcess(blackbox
.pid
);
208 if (!blackbox
.attach_rc
)
209 blackbox
.attach_err
=GetLastError();
212 blackbox
.attach_rc
=TRUE
;
214 debug_event
=(argc
>= 6 ? (HANDLE
)(INT_PTR
)atol(argv
[5]) : NULL
);
215 blackbox
.debug_err
=0;
216 if (debug_event
&& strstr(myARGV
[2], "event"))
218 blackbox
.debug_rc
=SetEvent(debug_event
);
219 if (!blackbox
.debug_rc
)
220 blackbox
.debug_err
=GetLastError();
223 blackbox
.debug_rc
=TRUE
;
227 get_events(logfile
, &start_event
, &done_event
);
230 if (strstr(myARGV
[2], "order"))
232 trace("debugger: waiting for the start signal...\n");
233 WaitForSingleObject(start_event
, INFINITE
);
236 blackbox
.nokill_err
=0;
237 if (strstr(myARGV
[2], "nokill"))
239 blackbox
.nokill_rc
=pDebugSetProcessKillOnExit(FALSE
);
240 if (!blackbox
.nokill_rc
)
241 blackbox
.nokill_err
=GetLastError();
244 blackbox
.nokill_rc
=TRUE
;
246 blackbox
.detach_err
=0;
247 if (strstr(myARGV
[2], "detach"))
249 blackbox
.detach_rc
=pDebugActiveProcessStop(blackbox
.pid
);
250 if (!blackbox
.detach_rc
)
251 blackbox
.detach_err
=GetLastError();
254 blackbox
.detach_rc
=TRUE
;
258 save_blackbox(logfile
, &blackbox
, sizeof(blackbox
));
260 trace("debugger: done debugging...\n");
261 SetEvent(done_event
);
263 /* Just exit with a known value */
264 ExitProcess(0xdeadbeef);
267 static void crash_and_debug(HKEY hkey
, const char* argv0
, const char* dbgtasks
)
269 static BOOL skip_crash_and_debug
= FALSE
;
272 HANDLE start_event
, done_event
;
274 char dbglog
[MAX_PATH
];
275 char childlog
[MAX_PATH
];
276 PROCESS_INFORMATION info
;
277 STARTUPINFOA startup
;
279 crash_blackbox_t crash_blackbox
;
280 debugger_blackbox_t dbg_blackbox
;
283 if (skip_crash_and_debug
)
285 win_skip("Skipping crash_and_debug\n");
289 ret
=RegSetValueExA(hkey
, "auto", 0, REG_SZ
, (BYTE
*)"1", 2);
290 if (ret
== ERROR_ACCESS_DENIED
)
292 skip_crash_and_debug
= TRUE
;
293 skip("No write access to change the debugger\n");
297 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/auto: ret=%d\n", ret
);
299 get_file_name(dbglog
);
300 get_events(dbglog
, &start_event
, &done_event
);
301 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+10+strlen(dbgtasks
)+1+strlen(dbglog
)+2+34+1);
302 sprintf(cmd
, "%s debugger %s \"%s\" %%ld %%ld", argv0
, dbgtasks
, dbglog
);
303 ret
=RegSetValueExA(hkey
, "debugger", 0, REG_SZ
, (BYTE
*)cmd
, strlen(cmd
)+1);
304 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/debugger: ret=%d\n", ret
);
305 HeapFree(GetProcessHeap(), 0, cmd
);
307 get_file_name(childlog
);
308 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+16+strlen(dbglog
)+2+1);
309 sprintf(cmd
, "%s debugger crash \"%s\"", argv0
, childlog
);
311 memset(&startup
, 0, sizeof(startup
));
312 startup
.cb
= sizeof(startup
);
313 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
314 startup
.wShowWindow
= SW_SHOWNORMAL
;
315 ret
=CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
316 ok(ret
, "CreateProcess: err=%d\n", GetLastError());
317 HeapFree(GetProcessHeap(), 0, cmd
);
318 CloseHandle(info
.hThread
);
320 /* The process exits... */
321 trace("waiting for child exit...\n");
322 wait_code
= WaitForSingleObject(info
.hProcess
, 30000);
323 #if defined(_WIN64) && defined(__MINGW32__)
324 /* Mingw x64 doesn't output proper unwind info */
325 skip_crash_and_debug
= broken(wait_code
== WAIT_TIMEOUT
);
326 if (skip_crash_and_debug
)
328 TerminateProcess(info
.hProcess
, WAIT_TIMEOUT
);
329 WaitForSingleObject(info
.hProcess
, 5000);
330 CloseHandle(info
.hProcess
);
331 assert(DeleteFileA(dbglog
) != 0);
332 assert(DeleteFileA(childlog
) != 0);
333 win_skip("Giving up on child process\n");
337 ok(wait_code
== WAIT_OBJECT_0
, "Timed out waiting for the child to crash\n");
338 bRet
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
339 ok(bRet
, "GetExitCodeProcess failed: err=%d\n", GetLastError());
340 if (strstr(dbgtasks
, "code2"))
342 /* If, after attaching to the debuggee, the debugger exits without
343 * detaching, then the debuggee gets a special exit code.
345 ok(exit_code
== STATUS_DEBUGGER_INACTIVE
||
346 broken(exit_code
== STATUS_ACCESS_VIOLATION
) || /* Intermittent Vista+ */
347 broken(exit_code
== WAIT_ABANDONED
), /* NT4, W2K */
348 "wrong exit code : %08x\n", exit_code
);
351 ok(exit_code
== STATUS_ACCESS_VIOLATION
||
352 broken(exit_code
== WAIT_ABANDONED
), /* NT4, W2K, W2K3 */
353 "wrong exit code : %08x\n", exit_code
);
354 CloseHandle(info
.hProcess
);
356 /* ...before the debugger */
357 if (strstr(dbgtasks
, "order"))
358 ok(SetEvent(start_event
), "SetEvent(start_event) failed\n");
360 trace("waiting for the debugger...\n");
361 wait_code
= WaitForSingleObject(done_event
, 5000);
362 #if defined(_WIN64) && defined(__MINGW32__)
363 /* Mingw x64 doesn't output proper unwind info */
364 skip_crash_and_debug
= broken(wait_code
== WAIT_TIMEOUT
);
365 if (skip_crash_and_debug
)
367 assert(DeleteFileA(dbglog
) != 0);
368 assert(DeleteFileA(childlog
) != 0);
369 win_skip("Giving up on debugger\n");
373 ok(wait_code
== WAIT_OBJECT_0
, "Timed out waiting for the debugger\n");
375 assert(load_blackbox(childlog
, &crash_blackbox
, sizeof(crash_blackbox
)));
376 assert(load_blackbox(dbglog
, &dbg_blackbox
, sizeof(dbg_blackbox
)));
378 ok(dbg_blackbox
.argc
== 6, "wrong debugger argument count: %d\n", dbg_blackbox
.argc
);
379 ok(dbg_blackbox
.pid
== crash_blackbox
.pid
, "the child and debugged pids don't match: %d != %d\n", crash_blackbox
.pid
, dbg_blackbox
.pid
);
380 ok(dbg_blackbox
.debug_rc
, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox
.debug_err
);
381 ok(dbg_blackbox
.attach_rc
, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox
.pid
, dbg_blackbox
.attach_err
);
382 ok(dbg_blackbox
.nokill_rc
, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox
.nokill_err
);
383 ok(dbg_blackbox
.detach_rc
, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox
.pid
, dbg_blackbox
.detach_err
);
385 assert(DeleteFileA(dbglog
) != 0);
386 assert(DeleteFileA(childlog
) != 0);
389 static void crash_and_winedbg(HKEY hkey
, const char* argv0
)
394 PROCESS_INFORMATION info
;
395 STARTUPINFOA startup
;
398 ret
=RegSetValueExA(hkey
, "auto", 0, REG_SZ
, (BYTE
*)"1", 2);
399 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/auto: ret=%d\n", ret
);
401 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+15+1);
402 sprintf(cmd
, "%s debugger crash", argv0
);
404 memset(&startup
, 0, sizeof(startup
));
405 startup
.cb
= sizeof(startup
);
406 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
407 startup
.wShowWindow
= SW_SHOWNORMAL
;
408 ret
=CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
409 ok(ret
, "CreateProcess: err=%d\n", GetLastError());
410 HeapFree(GetProcessHeap(), 0, cmd
);
411 CloseHandle(info
.hThread
);
413 trace("waiting for child exit...\n");
414 ok(WaitForSingleObject(info
.hProcess
, 60000) == WAIT_OBJECT_0
, "Timed out waiting for the child to crash\n");
415 bRet
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
416 ok(bRet
, "GetExitCodeProcess failed: err=%d\n", GetLastError());
417 ok(exit_code
== STATUS_ACCESS_VIOLATION
, "exit code = %08x\n", exit_code
);
418 CloseHandle(info
.hProcess
);
421 static void test_ExitCode(void)
423 static const char* AeDebug
="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
424 static const char* WineDbg
="Software\\Wine\\WineDbg";
425 char test_exe
[MAX_PATH
];
429 reg_save_value auto_value
;
430 reg_save_value debugger_value
;
432 GetModuleFileNameA(GetModuleHandle(NULL
), test_exe
, sizeof(test_exe
));
433 if (GetFileAttributes(test_exe
) == INVALID_FILE_ATTRIBUTES
)
434 strcat(test_exe
, ".so");
435 if (GetFileAttributesA(test_exe
) == INVALID_FILE_ATTRIBUTES
)
437 ok(0, "could not find the test executable '%s'\n", test_exe
);
441 ret
=RegCreateKeyExA(HKEY_LOCAL_MACHINE
, AeDebug
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, &disposition
);
442 if (ret
== ERROR_SUCCESS
)
444 save_value(hkey
, "auto", &auto_value
);
445 save_value(hkey
, "debugger", &debugger_value
);
446 trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug
, debugger_value
.data
);
448 else if (ret
== ERROR_ACCESS_DENIED
)
450 skip("not enough privileges to change the debugger\n");
453 else if (ret
!= ERROR_FILE_NOT_FOUND
)
455 ok(0, "could not open the AeDebug key: %d\n", ret
);
458 else debugger_value
.data
= NULL
;
460 if (debugger_value
.data
&& debugger_value
.type
== REG_SZ
&&
461 strstr((char*)debugger_value
.data
, "winedbg --auto"))
464 ret
=RegCreateKeyA(HKEY_CURRENT_USER
, WineDbg
, &hkeyWinedbg
);
465 if (ret
== ERROR_SUCCESS
)
468 reg_save_value crash_dlg_value
;
469 save_value(hkeyWinedbg
, "ShowCrashDialog", &crash_dlg_value
);
470 RegSetValueExA(hkeyWinedbg
, "ShowCrashDialog", 0, REG_DWORD
, (BYTE
*)&zero
, sizeof(DWORD
));
471 crash_and_winedbg(hkey
, test_exe
);
472 restore_value(hkeyWinedbg
, &crash_dlg_value
);
473 RegCloseKey(hkeyWinedbg
);
476 ok(0, "Couldn't access WineDbg Key - error %u\n", ret
);
479 if (winetest_interactive
)
480 /* Since the debugging process never sets the debug event, it isn't recognized
481 as a valid debugger and, after the debugger exits, Windows will show a dialog box
482 asking the user what to do */
483 crash_and_debug(hkey
, test_exe
, "dbg,none");
485 skip("\"none\" debugger test needs user interaction\n");
486 ok(disposition
== REG_OPENED_EXISTING_KEY
, "expected REG_OPENED_EXISTING_KEY, got %d\n", disposition
);
487 crash_and_debug(hkey
, test_exe
, "dbg,event,order");
488 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,code2");
489 if (pDebugSetProcessKillOnExit
)
490 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,nokill");
492 win_skip("DebugSetProcessKillOnExit is not available\n");
493 if (pDebugActiveProcessStop
)
494 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,detach");
496 win_skip("DebugActiveProcessStop is not available\n");
498 if (disposition
== REG_CREATED_NEW_KEY
)
501 RegDeleteKeyA(HKEY_LOCAL_MACHINE
, AeDebug
);
505 restore_value(hkey
, &auto_value
);
506 restore_value(hkey
, &debugger_value
);
511 static void test_RemoteDebugger(void)
514 if(!pCheckRemoteDebuggerPresent
)
516 win_skip("CheckRemoteDebuggerPresent is not available\n");
520 SetLastError(0xdeadbeef);
521 bret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present
);
522 ok(bret
, "expected CheckRemoteDebuggerPresent to succeed\n");
523 ok(0xdeadbeef == GetLastError(),
524 "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());
527 SetLastError(0xdeadbeef);
528 bret
= pCheckRemoteDebuggerPresent(NULL
,&present
);
529 ok(!bret
, "expected CheckRemoteDebuggerPresent to fail\n");
530 ok(present
, "expected parameter to be unchanged\n");
531 ok(ERROR_INVALID_PARAMETER
== GetLastError(),
532 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
534 SetLastError(0xdeadbeef);
535 bret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL
);
536 ok(!bret
, "expected CheckRemoteDebuggerPresent to fail\n");
537 ok(ERROR_INVALID_PARAMETER
== GetLastError(),
538 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
541 struct child_blackbox
546 static void doChild(int argc
, char **argv
)
548 struct child_blackbox blackbox
;
549 const char *blackbox_file
;
555 blackbox_file
= argv
[4];
556 sscanf(argv
[3], "%08x", &ppid
);
558 parent
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, ppid
);
559 child_ok(!!parent
, "OpenProcess failed, last error %#x.\n", GetLastError());
561 ret
= pCheckRemoteDebuggerPresent(parent
, &debug
);
562 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
563 child_ok(!debug
, "Expected debug == 0, got %#x.\n", debug
);
565 ret
= DebugActiveProcess(ppid
);
566 child_ok(ret
, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
568 ret
= pCheckRemoteDebuggerPresent(parent
, &debug
);
569 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
570 child_ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
572 ret
= pDebugActiveProcessStop(ppid
);
573 child_ok(ret
, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());
575 ret
= pCheckRemoteDebuggerPresent(parent
, &debug
);
576 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
577 child_ok(!debug
, "Expected debug == 0, got %#x.\n", debug
);
579 ret
= CloseHandle(parent
);
580 child_ok(ret
, "CloseHandle failed, last error %#x.\n", GetLastError());
582 ret
= pIsDebuggerPresent();
583 child_ok(ret
, "Expected ret != 0, got %#x.\n", ret
);
584 ret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug
);
585 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
586 child_ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
590 pNtCurrentTeb()->Peb
->BeingDebugged
= FALSE
;
592 ret
= pIsDebuggerPresent();
593 child_ok(!ret
, "Expected ret != 0, got %#x.\n", ret
);
594 ret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug
);
595 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
596 child_ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
598 pNtCurrentTeb()->Peb
->BeingDebugged
= TRUE
;
601 blackbox
.failures
= child_failures
;
602 save_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
605 static void test_debug_loop(int argc
, char **argv
)
607 const char *arguments
= " debugger child ";
608 struct child_blackbox blackbox
;
609 char blackbox_file
[MAX_PATH
];
610 PROCESS_INFORMATION pi
;
617 if (!pDebugActiveProcessStop
|| !pCheckRemoteDebuggerPresent
)
619 win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
623 pid
= GetCurrentProcessId();
624 ret
= DebugActiveProcess(pid
);
625 ok(!ret
, "DebugActiveProcess() succeeded on own process.\n");
627 get_file_name(blackbox_file
);
628 cmd
= HeapAlloc(GetProcessHeap(), 0, strlen(argv
[0]) + strlen(arguments
) + strlen(blackbox_file
) + 2 + 10);
629 sprintf(cmd
, "%s%s%08x \"%s\"", argv
[0], arguments
, pid
, blackbox_file
);
631 memset(&si
, 0, sizeof(si
));
633 ret
= CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &si
, &pi
);
634 ok(ret
, "CreateProcess failed, last error %#x.\n", GetLastError());
636 HeapFree(GetProcessHeap(), 0, cmd
);
638 ret
= pCheckRemoteDebuggerPresent(pi
.hProcess
, &debug
);
639 ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
640 ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
646 ret
= WaitForDebugEvent(&ev
, INFINITE
);
647 ok(ret
, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
650 if (ev
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
) break;
652 ret
= ContinueDebugEvent(ev
.dwProcessId
, ev
.dwThreadId
, DBG_CONTINUE
);
653 ok(ret
, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
657 ret
= CloseHandle(pi
.hThread
);
658 ok(ret
, "CloseHandle failed, last error %#x.\n", GetLastError());
659 ret
= CloseHandle(pi
.hProcess
);
660 ok(ret
, "CloseHandle failed, last error %#x.\n", GetLastError());
662 load_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
663 ok(!blackbox
.failures
, "Got %d failures from child process.\n", blackbox
.failures
);
665 ret
= DeleteFileA(blackbox_file
);
666 ok(ret
, "DeleteFileA failed, last error %#x.\n", GetLastError());
669 static void doChildren(int argc
, char **argv
)
671 const char *arguments
= "debugger children last";
672 struct child_blackbox blackbox
;
673 const char *blackbox_file
, *p
;
674 char event_name
[MAX_PATH
];
675 PROCESS_INFORMATION pi
;
681 if (!strcmp(argv
[3], "last")) return;
683 blackbox_file
= argv
[3];
685 p
= strrchr(blackbox_file
, '\\');
686 p
= p
? p
+1 : blackbox_file
;
687 strcpy(event_name
, p
);
688 strcat(event_name
, "_init");
689 event
= OpenEvent(EVENT_ALL_ACCESS
, FALSE
, event_name
);
690 child_ok(event
!= NULL
, "OpenEvent failed, last error %d.\n", GetLastError());
694 p
= strrchr(blackbox_file
, '\\');
695 p
= p
? p
+1 : blackbox_file
;
696 strcpy(event_name
, p
);
697 strcat(event_name
, "_attach");
698 event
= OpenEvent(EVENT_ALL_ACCESS
, FALSE
, event_name
);
699 child_ok(event
!= NULL
, "OpenEvent failed, last error %d.\n", GetLastError());
700 WaitForSingleObject(event
, INFINITE
);
703 cmd
= HeapAlloc(GetProcessHeap(), 0, strlen(argv
[0]) + strlen(arguments
) + 2);
704 sprintf(cmd
, "%s %s", argv
[0], arguments
);
706 memset(&si
, 0, sizeof(si
));
708 ret
= CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
709 child_ok(ret
, "CreateProcess failed, last error %d.\n", GetLastError());
711 child_ok(WaitForSingleObject(pi
.hProcess
, 10000) == WAIT_OBJECT_0
,
712 "Timed out waiting for the child to exit\n");
714 ret
= CloseHandle(pi
.hThread
);
715 child_ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
716 ret
= CloseHandle(pi
.hProcess
);
717 child_ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
719 blackbox
.failures
= child_failures
;
720 save_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
723 static void test_debug_children(char *name
, DWORD flag
, BOOL debug_child
)
725 const char *arguments
= "debugger children";
726 struct child_blackbox blackbox
;
727 char blackbox_file
[MAX_PATH
], *p
;
728 char event_name
[MAX_PATH
];
729 PROCESS_INFORMATION pi
;
731 HANDLE event_init
, event_attach
;
734 BOOL got_child_event
= FALSE
;
736 if (!pDebugActiveProcessStop
|| !pCheckRemoteDebuggerPresent
)
738 win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
742 get_file_name(blackbox_file
);
743 cmd
= HeapAlloc(GetProcessHeap(), 0, strlen(name
) + strlen(arguments
) + strlen(blackbox_file
) + 5);
744 sprintf(cmd
, "%s %s \"%s\"", name
, arguments
, blackbox_file
);
746 p
= strrchr(blackbox_file
, '\\');
747 p
= p
? p
+1 : blackbox_file
;
748 strcpy(event_name
, p
);
749 strcat(event_name
, "_init");
750 event_init
= CreateEvent(NULL
, FALSE
, FALSE
, event_name
);
751 ok(event_init
!= NULL
, "OpenEvent failed, last error %d.\n", GetLastError());
753 p
= strrchr(blackbox_file
, '\\');
754 p
= p
? p
+1 : blackbox_file
;
755 strcpy(event_name
, p
);
756 strcat(event_name
, "_attach");
757 event_attach
= CreateEvent(NULL
, FALSE
, flag
!=0, event_name
);
758 ok(event_attach
!= NULL
, "CreateEvent failed, last error %d.\n", GetLastError());
760 memset(&si
, 0, sizeof(si
));
763 ret
= CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, flag
, NULL
, NULL
, &si
, &pi
);
764 ok(ret
, "CreateProcess failed, last error %d.\n", GetLastError());
765 HeapFree(GetProcessHeap(), 0, cmd
);
768 WaitForSingleObject(event_init
, INFINITE
);
769 ret
= DebugActiveProcess(pi
.dwProcessId
);
770 ok(ret
, "DebugActiveProcess failed, last error %d.\n", GetLastError());
771 ret
= SetEvent(event_attach
);
772 ok(ret
, "SetEvent failed, last error %d.\n", GetLastError());
775 ret
= pCheckRemoteDebuggerPresent(pi
.hProcess
, &debug
);
776 ok(ret
, "CheckRemoteDebuggerPresent failed, last error %d.\n", GetLastError());
777 ok(debug
, "Expected debug != 0, got %x.\n", debug
);
783 ret
= WaitForDebugEvent(&ev
, INFINITE
);
784 ok(ret
, "WaitForDebugEvent failed, last error %d.\n", GetLastError());
787 if (ev
.dwDebugEventCode
==EXIT_PROCESS_DEBUG_EVENT
&& ev
.dwProcessId
==pi
.dwProcessId
) break;
788 else if (ev
.dwProcessId
!= pi
.dwProcessId
) got_child_event
= TRUE
;
790 ret
= ContinueDebugEvent(ev
.dwProcessId
, ev
.dwThreadId
, DBG_CONTINUE
);
791 ok(ret
, "ContinueDebugEvent failed, last error %d.\n", GetLastError());
795 ok(got_child_event
, "didn't get any child events (flag: %x).\n", flag
);
797 ok(!got_child_event
, "got child event (flag: %x).\n", flag
);
798 CloseHandle(event_init
);
799 CloseHandle(event_attach
);
801 ret
= CloseHandle(pi
.hThread
);
802 ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
803 ret
= CloseHandle(pi
.hProcess
);
804 ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
806 load_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
807 ok(!blackbox
.failures
, "Got %d failures from child process.\n", blackbox
.failures
);
809 ret
= DeleteFileA(blackbox_file
);
810 ok(ret
, "DeleteFileA failed, last error %d.\n", GetLastError());
817 hdll
=GetModuleHandle("kernel32.dll");
818 pCheckRemoteDebuggerPresent
=(void*)GetProcAddress(hdll
, "CheckRemoteDebuggerPresent");
819 pDebugActiveProcessStop
=(void*)GetProcAddress(hdll
, "DebugActiveProcessStop");
820 pDebugSetProcessKillOnExit
=(void*)GetProcAddress(hdll
, "DebugSetProcessKillOnExit");
821 pIsDebuggerPresent
=(void*)GetProcAddress(hdll
, "IsDebuggerPresent");
822 hdll
=GetModuleHandle("ntdll.dll");
823 if (hdll
) pNtCurrentTeb
= (void*)GetProcAddress(hdll
, "NtCurrentTeb");
825 myARGC
=winetest_get_mainargs(&myARGV
);
826 if (myARGC
>= 3 && strcmp(myARGV
[2], "crash") == 0)
828 doCrash(myARGC
, myARGV
);
830 else if (myARGC
>= 3 && strncmp(myARGV
[2], "dbg,", 4) == 0)
832 doDebugger(myARGC
, myARGV
);
834 else if (myARGC
>= 5 && !strcmp(myARGV
[2], "child"))
836 doChild(myARGC
, myARGV
);
838 else if (myARGC
>= 4 && !strcmp(myARGV
[2], "children"))
840 doChildren(myARGC
, myARGV
);
845 test_RemoteDebugger();
846 test_debug_loop(myARGC
, myARGV
);
847 test_debug_children(myARGV
[0], DEBUG_PROCESS
, TRUE
);
848 test_debug_children(myARGV
[0], DEBUG_ONLY_THIS_PROCESS
, FALSE
);
849 test_debug_children(myARGV
[0], 0, FALSE
);