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
23 #ifndef STATUS_DEBUGGER_INACTIVE
24 #define STATUS_DEBUGGER_INACTIVE ((NTSTATUS) 0xC0000354)
27 #define child_ok (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_child_ok
32 static BOOL (WINAPI
*pCheckRemoteDebuggerPresent
)(HANDLE
,PBOOL
);
33 static BOOL (WINAPI
*pDebugActiveProcessStop
)(DWORD
);
34 static BOOL (WINAPI
*pDebugSetProcessKillOnExit
)(BOOL
);
35 static BOOL (WINAPI
*pIsDebuggerPresent
)(void);
37 static LONG child_failures
;
39 static void WINETEST_PRINTF_ATTR(2, 3) test_child_ok(int condition
, const char *msg
, ...)
43 va_start(valist
, msg
);
44 winetest_vok(condition
, msg
, valist
);
46 if (!condition
) ++child_failures
;
49 /* Copied from the process test */
50 static void get_file_name(char* buf
)
55 GetTempPathA(sizeof(path
), path
);
56 GetTempFileNameA(path
, "wt", 0, buf
);
59 typedef struct tag_reg_save_value
67 static DWORD
save_value(HKEY hkey
, const char *value
, reg_save_value
*saved
)
73 ret
=RegQueryValueExA(hkey
, value
, NULL
, &saved
->type
, NULL
, &saved
->size
);
74 if (ret
== ERROR_SUCCESS
)
76 saved
->data
=HeapAlloc(GetProcessHeap(), 0, saved
->size
);
77 RegQueryValueExA(hkey
, value
, NULL
, &saved
->type
, saved
->data
, &saved
->size
);
82 static void restore_value(HKEY hkey
, reg_save_value
*saved
)
86 RegSetValueExA(hkey
, saved
->name
, 0, saved
->type
, saved
->data
, saved
->size
);
87 HeapFree(GetProcessHeap(), 0, saved
->data
);
90 RegDeleteValueA(hkey
, saved
->name
);
93 static void get_events(const char* name
, HANDLE
*start_event
, HANDLE
*done_event
)
98 basename
=strrchr(name
, '\\');
99 basename
=(basename
? basename
+1 : name
);
100 event_name
=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename
)+1);
102 sprintf(event_name
, "start_%s", basename
);
103 *start_event
=CreateEventA(NULL
, 0,0, event_name
);
104 sprintf(event_name
, "done_%s", basename
);
105 *done_event
=CreateEventA(NULL
, 0,0, event_name
);
106 HeapFree(GetProcessHeap(), 0, event_name
);
109 static void save_blackbox(const char* logfile
, void* blackbox
, int size
)
114 hFile
=CreateFileA(logfile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
115 if (hFile
== INVALID_HANDLE_VALUE
)
117 WriteFile(hFile
, blackbox
, size
, &written
, NULL
);
121 static int load_blackbox(const char* logfile
, void* blackbox
, int size
)
127 hFile
=CreateFileA(logfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
128 if (hFile
== INVALID_HANDLE_VALUE
)
130 ok(0, "unable to open '%s'\n", logfile
);
133 SetLastError(0xdeadbeef);
134 ret
=ReadFile(hFile
, blackbox
, size
, &read
, NULL
);
135 ok(ret
, "ReadFile failed: %d\n", GetLastError());
136 ok(read
== size
, "wrong size for '%s': read=%d\n", logfile
, read
);
146 static void doCrash(int argc
, char** argv
)
150 /* make sure the exception gets to the debugger */
152 SetUnhandledExceptionFilter( NULL
);
156 crash_blackbox_t blackbox
;
157 blackbox
.pid
=GetCurrentProcessId();
158 save_blackbox(argv
[3], &blackbox
, sizeof(blackbox
));
162 trace("child: crashing...\n");
179 } debugger_blackbox_t
;
181 static void doDebugger(int argc
, char** argv
)
184 debugger_blackbox_t blackbox
;
185 HANDLE start_event
= 0, done_event
= 0, debug_event
;
188 logfile
=(argc
>= 4 ? argv
[3] : NULL
);
189 blackbox
.pid
=(argc
>= 5 ? atol(argv
[4]) : 0);
191 blackbox
.attach_err
=0;
192 if (strstr(myARGV
[2], "attach"))
194 blackbox
.attach_rc
=DebugActiveProcess(blackbox
.pid
);
195 if (!blackbox
.attach_rc
)
196 blackbox
.attach_err
=GetLastError();
199 blackbox
.attach_rc
=TRUE
;
201 debug_event
=(argc
>= 6 ? (HANDLE
)(INT_PTR
)atol(argv
[5]) : NULL
);
202 blackbox
.debug_err
=0;
203 if (debug_event
&& strstr(myARGV
[2], "event"))
205 blackbox
.debug_rc
=SetEvent(debug_event
);
206 if (!blackbox
.debug_rc
)
207 blackbox
.debug_err
=GetLastError();
210 blackbox
.debug_rc
=TRUE
;
214 get_events(logfile
, &start_event
, &done_event
);
217 if (strstr(myARGV
[2], "order"))
219 trace("debugger: waiting for the start signal...\n");
220 WaitForSingleObject(start_event
, INFINITE
);
223 blackbox
.nokill_err
=0;
224 if (strstr(myARGV
[2], "nokill"))
226 blackbox
.nokill_rc
=pDebugSetProcessKillOnExit(FALSE
);
227 if (!blackbox
.nokill_rc
)
228 blackbox
.nokill_err
=GetLastError();
231 blackbox
.nokill_rc
=TRUE
;
233 blackbox
.detach_err
=0;
234 if (strstr(myARGV
[2], "detach"))
236 blackbox
.detach_rc
=pDebugActiveProcessStop(blackbox
.pid
);
237 if (!blackbox
.detach_rc
)
238 blackbox
.detach_err
=GetLastError();
241 blackbox
.detach_rc
=TRUE
;
245 save_blackbox(logfile
, &blackbox
, sizeof(blackbox
));
247 trace("debugger: done debugging...\n");
248 SetEvent(done_event
);
250 /* Just exit with a known value */
251 ExitProcess(0xdeadbeef);
254 static void crash_and_debug(HKEY hkey
, const char* argv0
, const char* dbgtasks
)
256 static BOOL skip_crash_and_debug
= FALSE
;
259 HANDLE start_event
, done_event
;
261 char dbglog
[MAX_PATH
];
262 char childlog
[MAX_PATH
];
263 PROCESS_INFORMATION info
;
264 STARTUPINFOA startup
;
266 crash_blackbox_t crash_blackbox
;
267 debugger_blackbox_t dbg_blackbox
;
270 if (skip_crash_and_debug
)
272 win_skip("Skipping crash_and_debug\n");
276 ret
=RegSetValueExA(hkey
, "auto", 0, REG_SZ
, (BYTE
*)"1", 2);
277 if (ret
== ERROR_ACCESS_DENIED
)
279 skip_crash_and_debug
= TRUE
;
280 skip("No write access to change the debugger\n");
284 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/auto: ret=%d\n", ret
);
286 get_file_name(dbglog
);
287 get_events(dbglog
, &start_event
, &done_event
);
288 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+10+strlen(dbgtasks
)+1+strlen(dbglog
)+2+34+1);
289 sprintf(cmd
, "%s debugger %s \"%s\" %%ld %%ld", argv0
, dbgtasks
, dbglog
);
290 ret
=RegSetValueExA(hkey
, "debugger", 0, REG_SZ
, (BYTE
*)cmd
, strlen(cmd
)+1);
291 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/debugger: ret=%d\n", ret
);
292 HeapFree(GetProcessHeap(), 0, cmd
);
294 get_file_name(childlog
);
295 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+16+strlen(dbglog
)+2+1);
296 sprintf(cmd
, "%s debugger crash \"%s\"", argv0
, childlog
);
298 memset(&startup
, 0, sizeof(startup
));
299 startup
.cb
= sizeof(startup
);
300 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
301 startup
.wShowWindow
= SW_SHOWNORMAL
;
302 ret
=CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
303 ok(ret
, "CreateProcess: err=%d\n", GetLastError());
304 HeapFree(GetProcessHeap(), 0, cmd
);
305 CloseHandle(info
.hThread
);
307 /* The process exits... */
308 trace("waiting for child exit...\n");
309 wait_code
= WaitForSingleObject(info
.hProcess
, 30000);
310 #if defined(_WIN64) && defined(__MINGW32__)
311 /* Mingw x64 doesn't output proper unwind info */
312 skip_crash_and_debug
= broken(wait_code
== WAIT_TIMEOUT
);
313 if (skip_crash_and_debug
)
315 TerminateProcess(info
.hProcess
, WAIT_TIMEOUT
);
316 WaitForSingleObject(info
.hProcess
, 5000);
317 CloseHandle(info
.hProcess
);
319 DeleteFileA(childlog
);
320 win_skip("Giving up on child process\n");
324 ok(wait_code
== WAIT_OBJECT_0
, "Timed out waiting for the child to crash\n");
325 bRet
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
326 ok(bRet
, "GetExitCodeProcess failed: err=%d\n", GetLastError());
327 if (strstr(dbgtasks
, "code2"))
329 /* If, after attaching to the debuggee, the debugger exits without
330 * detaching, then the debuggee gets a special exit code.
332 ok(exit_code
== STATUS_DEBUGGER_INACTIVE
||
333 broken(exit_code
== STATUS_ACCESS_VIOLATION
) || /* Intermittent Vista+ */
334 broken(exit_code
== WAIT_ABANDONED
), /* NT4, W2K */
335 "wrong exit code : %08x\n", exit_code
);
338 ok(exit_code
== STATUS_ACCESS_VIOLATION
||
339 broken(exit_code
== WAIT_ABANDONED
), /* NT4, W2K, W2K3 */
340 "wrong exit code : %08x\n", exit_code
);
341 CloseHandle(info
.hProcess
);
343 /* ...before the debugger */
344 if (strstr(dbgtasks
, "order"))
345 ok(SetEvent(start_event
), "SetEvent(start_event) failed\n");
347 trace("waiting for the debugger...\n");
348 wait_code
= WaitForSingleObject(done_event
, 5000);
349 #if defined(_WIN64) && defined(__MINGW32__)
350 /* Mingw x64 doesn't output proper unwind info */
351 skip_crash_and_debug
= broken(wait_code
== WAIT_TIMEOUT
);
352 if (skip_crash_and_debug
)
355 DeleteFileA(childlog
);
356 win_skip("Giving up on debugger\n");
360 ok(wait_code
== WAIT_OBJECT_0
, "Timed out waiting for the debugger\n");
362 ok(load_blackbox(childlog
, &crash_blackbox
, sizeof(crash_blackbox
)), "failed to open: %s\n", childlog
);
363 ok(load_blackbox(dbglog
, &dbg_blackbox
, sizeof(dbg_blackbox
)), "failed to open: %s\n", dbglog
);
365 ok(dbg_blackbox
.argc
== 6, "wrong debugger argument count: %d\n", dbg_blackbox
.argc
);
366 ok(dbg_blackbox
.pid
== crash_blackbox
.pid
, "the child and debugged pids don't match: %d != %d\n", crash_blackbox
.pid
, dbg_blackbox
.pid
);
367 ok(dbg_blackbox
.debug_rc
, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox
.debug_err
);
368 ok(dbg_blackbox
.attach_rc
, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox
.pid
, dbg_blackbox
.attach_err
);
369 ok(dbg_blackbox
.nokill_rc
, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox
.nokill_err
);
370 ok(dbg_blackbox
.detach_rc
, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox
.pid
, dbg_blackbox
.detach_err
);
373 DeleteFileA(childlog
);
376 static void crash_and_winedbg(HKEY hkey
, const char* argv0
)
381 PROCESS_INFORMATION info
;
382 STARTUPINFOA startup
;
385 ret
=RegSetValueExA(hkey
, "auto", 0, REG_SZ
, (BYTE
*)"1", 2);
386 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/auto: ret=%d\n", ret
);
388 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+15+1);
389 sprintf(cmd
, "%s debugger crash", argv0
);
391 memset(&startup
, 0, sizeof(startup
));
392 startup
.cb
= sizeof(startup
);
393 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
394 startup
.wShowWindow
= SW_SHOWNORMAL
;
395 ret
=CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
396 ok(ret
, "CreateProcess: err=%d\n", GetLastError());
397 HeapFree(GetProcessHeap(), 0, cmd
);
398 CloseHandle(info
.hThread
);
400 trace("waiting for child exit...\n");
401 ok(WaitForSingleObject(info
.hProcess
, 60000) == WAIT_OBJECT_0
, "Timed out waiting for the child to crash\n");
402 bRet
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
403 ok(bRet
, "GetExitCodeProcess failed: err=%d\n", GetLastError());
404 ok(exit_code
== STATUS_ACCESS_VIOLATION
, "exit code = %08x\n", exit_code
);
405 CloseHandle(info
.hProcess
);
408 static void test_ExitCode(void)
410 static const char* AeDebug
="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
411 static const char* WineDbg
="Software\\Wine\\WineDbg";
412 char test_exe
[MAX_PATH
];
416 reg_save_value auto_value
;
417 reg_save_value debugger_value
;
419 GetModuleFileNameA(GetModuleHandleA(NULL
), test_exe
, sizeof(test_exe
));
420 if (GetFileAttributesA(test_exe
) == INVALID_FILE_ATTRIBUTES
)
421 strcat(test_exe
, ".so");
422 if (GetFileAttributesA(test_exe
) == INVALID_FILE_ATTRIBUTES
)
424 ok(0, "could not find the test executable '%s'\n", test_exe
);
428 ret
=RegCreateKeyExA(HKEY_LOCAL_MACHINE
, AeDebug
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, &disposition
);
429 if (ret
== ERROR_SUCCESS
)
431 save_value(hkey
, "auto", &auto_value
);
432 save_value(hkey
, "debugger", &debugger_value
);
433 trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug
, debugger_value
.data
);
435 else if (ret
== ERROR_ACCESS_DENIED
)
437 skip("not enough privileges to change the debugger\n");
440 else if (ret
!= ERROR_FILE_NOT_FOUND
)
442 ok(0, "could not open the AeDebug key: %d\n", ret
);
445 else debugger_value
.data
= NULL
;
447 if (debugger_value
.data
&& debugger_value
.type
== REG_SZ
&&
448 strstr((char*)debugger_value
.data
, "winedbg --auto"))
451 ret
=RegCreateKeyA(HKEY_CURRENT_USER
, WineDbg
, &hkeyWinedbg
);
452 if (ret
== ERROR_SUCCESS
)
455 reg_save_value crash_dlg_value
;
456 save_value(hkeyWinedbg
, "ShowCrashDialog", &crash_dlg_value
);
457 RegSetValueExA(hkeyWinedbg
, "ShowCrashDialog", 0, REG_DWORD
, (BYTE
*)&zero
, sizeof(DWORD
));
458 crash_and_winedbg(hkey
, test_exe
);
459 restore_value(hkeyWinedbg
, &crash_dlg_value
);
460 RegCloseKey(hkeyWinedbg
);
463 ok(0, "Couldn't access WineDbg Key - error %u\n", ret
);
466 if (winetest_interactive
)
467 /* Since the debugging process never sets the debug event, it isn't recognized
468 as a valid debugger and, after the debugger exits, Windows will show a dialog box
469 asking the user what to do */
470 crash_and_debug(hkey
, test_exe
, "dbg,none");
472 skip("\"none\" debugger test needs user interaction\n");
473 ok(disposition
== REG_OPENED_EXISTING_KEY
, "expected REG_OPENED_EXISTING_KEY, got %d\n", disposition
);
474 crash_and_debug(hkey
, test_exe
, "dbg,event,order");
475 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,code2");
476 if (pDebugSetProcessKillOnExit
)
477 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,nokill");
479 win_skip("DebugSetProcessKillOnExit is not available\n");
480 if (pDebugActiveProcessStop
)
481 crash_and_debug(hkey
, test_exe
, "dbg,attach,event,detach");
483 win_skip("DebugActiveProcessStop is not available\n");
485 if (disposition
== REG_CREATED_NEW_KEY
)
488 RegDeleteKeyA(HKEY_LOCAL_MACHINE
, AeDebug
);
492 restore_value(hkey
, &auto_value
);
493 restore_value(hkey
, &debugger_value
);
498 static void test_RemoteDebugger(void)
501 if(!pCheckRemoteDebuggerPresent
)
503 win_skip("CheckRemoteDebuggerPresent is not available\n");
507 SetLastError(0xdeadbeef);
508 bret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present
);
509 ok(bret
, "expected CheckRemoteDebuggerPresent to succeed\n");
510 ok(0xdeadbeef == GetLastError(),
511 "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());
514 SetLastError(0xdeadbeef);
515 bret
= pCheckRemoteDebuggerPresent(NULL
,&present
);
516 ok(!bret
, "expected CheckRemoteDebuggerPresent to fail\n");
517 ok(present
, "expected parameter to be unchanged\n");
518 ok(ERROR_INVALID_PARAMETER
== GetLastError(),
519 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
521 SetLastError(0xdeadbeef);
522 bret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL
);
523 ok(!bret
, "expected CheckRemoteDebuggerPresent to fail\n");
524 ok(ERROR_INVALID_PARAMETER
== GetLastError(),
525 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
528 struct child_blackbox
533 static void doChild(int argc
, char **argv
)
535 struct child_blackbox blackbox
;
536 const char *blackbox_file
;
542 blackbox_file
= argv
[4];
543 sscanf(argv
[3], "%08x", &ppid
);
545 parent
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, ppid
);
546 child_ok(!!parent
, "OpenProcess failed, last error %#x.\n", GetLastError());
548 ret
= pCheckRemoteDebuggerPresent(parent
, &debug
);
549 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
550 child_ok(!debug
, "Expected debug == 0, got %#x.\n", debug
);
552 ret
= DebugActiveProcess(ppid
);
553 child_ok(ret
, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
555 ret
= pCheckRemoteDebuggerPresent(parent
, &debug
);
556 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
557 child_ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
559 ret
= pDebugActiveProcessStop(ppid
);
560 child_ok(ret
, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());
562 ret
= pCheckRemoteDebuggerPresent(parent
, &debug
);
563 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
564 child_ok(!debug
, "Expected debug == 0, got %#x.\n", debug
);
566 ret
= CloseHandle(parent
);
567 child_ok(ret
, "CloseHandle failed, last error %#x.\n", GetLastError());
569 ret
= pIsDebuggerPresent();
570 child_ok(ret
, "Expected ret != 0, got %#x.\n", ret
);
571 ret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug
);
572 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
573 child_ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
575 NtCurrentTeb()->Peb
->BeingDebugged
= FALSE
;
577 ret
= pIsDebuggerPresent();
578 child_ok(!ret
, "Expected ret != 0, got %#x.\n", ret
);
579 ret
= pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug
);
580 child_ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
581 child_ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
583 NtCurrentTeb()->Peb
->BeingDebugged
= TRUE
;
585 blackbox
.failures
= child_failures
;
586 save_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
589 static void test_debug_loop(int argc
, char **argv
)
591 const char *arguments
= " debugger child ";
592 struct child_blackbox blackbox
;
593 char blackbox_file
[MAX_PATH
];
594 PROCESS_INFORMATION pi
;
601 if (!pDebugActiveProcessStop
|| !pCheckRemoteDebuggerPresent
)
603 win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
607 pid
= GetCurrentProcessId();
608 ret
= DebugActiveProcess(pid
);
609 ok(!ret
, "DebugActiveProcess() succeeded on own process.\n");
611 get_file_name(blackbox_file
);
612 cmd
= HeapAlloc(GetProcessHeap(), 0, strlen(argv
[0]) + strlen(arguments
) + strlen(blackbox_file
) + 2 + 10);
613 sprintf(cmd
, "%s%s%08x \"%s\"", argv
[0], arguments
, pid
, blackbox_file
);
615 memset(&si
, 0, sizeof(si
));
617 ret
= CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &si
, &pi
);
618 ok(ret
, "CreateProcess failed, last error %#x.\n", GetLastError());
620 HeapFree(GetProcessHeap(), 0, cmd
);
622 ret
= pCheckRemoteDebuggerPresent(pi
.hProcess
, &debug
);
623 ok(ret
, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
624 ok(debug
, "Expected debug != 0, got %#x.\n", debug
);
630 ret
= WaitForDebugEvent(&ev
, INFINITE
);
631 ok(ret
, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
634 if (ev
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
) break;
636 ret
= ContinueDebugEvent(ev
.dwProcessId
, ev
.dwThreadId
, DBG_CONTINUE
);
637 ok(ret
, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
641 ret
= CloseHandle(pi
.hThread
);
642 ok(ret
, "CloseHandle failed, last error %#x.\n", GetLastError());
643 ret
= CloseHandle(pi
.hProcess
);
644 ok(ret
, "CloseHandle failed, last error %#x.\n", GetLastError());
646 load_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
647 ok(!blackbox
.failures
, "Got %d failures from child process.\n", blackbox
.failures
);
649 ret
= DeleteFileA(blackbox_file
);
650 ok(ret
, "DeleteFileA failed, last error %#x.\n", GetLastError());
653 static void doChildren(int argc
, char **argv
)
655 const char *arguments
= "debugger children last";
656 struct child_blackbox blackbox
;
657 const char *blackbox_file
, *p
;
658 char event_name
[MAX_PATH
];
659 PROCESS_INFORMATION pi
;
665 if (!strcmp(argv
[3], "last")) return;
667 blackbox_file
= argv
[3];
669 p
= strrchr(blackbox_file
, '\\');
670 p
= p
? p
+1 : blackbox_file
;
671 strcpy(event_name
, p
);
672 strcat(event_name
, "_init");
673 event
= OpenEventA(EVENT_ALL_ACCESS
, FALSE
, event_name
);
674 child_ok(event
!= NULL
, "OpenEvent failed, last error %d.\n", GetLastError());
678 p
= strrchr(blackbox_file
, '\\');
679 p
= p
? p
+1 : blackbox_file
;
680 strcpy(event_name
, p
);
681 strcat(event_name
, "_attach");
682 event
= OpenEventA(EVENT_ALL_ACCESS
, FALSE
, event_name
);
683 child_ok(event
!= NULL
, "OpenEvent failed, last error %d.\n", GetLastError());
684 WaitForSingleObject(event
, INFINITE
);
687 cmd
= HeapAlloc(GetProcessHeap(), 0, strlen(argv
[0]) + strlen(arguments
) + 2);
688 sprintf(cmd
, "%s %s", argv
[0], arguments
);
690 memset(&si
, 0, sizeof(si
));
692 ret
= CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
693 child_ok(ret
, "CreateProcess failed, last error %d.\n", GetLastError());
695 child_ok(WaitForSingleObject(pi
.hProcess
, 10000) == WAIT_OBJECT_0
,
696 "Timed out waiting for the child to exit\n");
698 ret
= CloseHandle(pi
.hThread
);
699 child_ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
700 ret
= CloseHandle(pi
.hProcess
);
701 child_ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
703 blackbox
.failures
= child_failures
;
704 save_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
706 HeapFree(GetProcessHeap(), 0, cmd
);
709 static void test_debug_children(char *name
, DWORD flag
, BOOL debug_child
)
711 const char *arguments
= "debugger children";
712 struct child_blackbox blackbox
;
713 char blackbox_file
[MAX_PATH
], *p
;
714 char event_name
[MAX_PATH
];
715 PROCESS_INFORMATION pi
;
717 HANDLE event_init
, event_attach
;
720 BOOL got_child_event
= FALSE
;
722 if (!pDebugActiveProcessStop
|| !pCheckRemoteDebuggerPresent
)
724 win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
728 get_file_name(blackbox_file
);
729 cmd
= HeapAlloc(GetProcessHeap(), 0, strlen(name
) + strlen(arguments
) + strlen(blackbox_file
) + 5);
730 sprintf(cmd
, "%s %s \"%s\"", name
, arguments
, blackbox_file
);
732 p
= strrchr(blackbox_file
, '\\');
733 p
= p
? p
+1 : blackbox_file
;
734 strcpy(event_name
, p
);
735 strcat(event_name
, "_init");
736 event_init
= CreateEventA(NULL
, FALSE
, FALSE
, event_name
);
737 ok(event_init
!= NULL
, "OpenEvent failed, last error %d.\n", GetLastError());
739 p
= strrchr(blackbox_file
, '\\');
740 p
= p
? p
+1 : blackbox_file
;
741 strcpy(event_name
, p
);
742 strcat(event_name
, "_attach");
743 event_attach
= CreateEventA(NULL
, FALSE
, flag
!=0, event_name
);
744 ok(event_attach
!= NULL
, "CreateEvent failed, last error %d.\n", GetLastError());
746 memset(&si
, 0, sizeof(si
));
749 ret
= CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, flag
, NULL
, NULL
, &si
, &pi
);
750 ok(ret
, "CreateProcess failed, last error %d.\n", GetLastError());
751 HeapFree(GetProcessHeap(), 0, cmd
);
754 WaitForSingleObject(event_init
, INFINITE
);
755 ret
= DebugActiveProcess(pi
.dwProcessId
);
756 ok(ret
, "DebugActiveProcess failed, last error %d.\n", GetLastError());
757 ret
= SetEvent(event_attach
);
758 ok(ret
, "SetEvent failed, last error %d.\n", GetLastError());
761 ret
= pCheckRemoteDebuggerPresent(pi
.hProcess
, &debug
);
762 ok(ret
, "CheckRemoteDebuggerPresent failed, last error %d.\n", GetLastError());
763 ok(debug
, "Expected debug != 0, got %x.\n", debug
);
769 ret
= WaitForDebugEvent(&ev
, INFINITE
);
770 ok(ret
, "WaitForDebugEvent failed, last error %d.\n", GetLastError());
773 if (ev
.dwDebugEventCode
==EXIT_PROCESS_DEBUG_EVENT
&& ev
.dwProcessId
==pi
.dwProcessId
) break;
774 else if (ev
.dwProcessId
!= pi
.dwProcessId
) got_child_event
= TRUE
;
776 ret
= ContinueDebugEvent(ev
.dwProcessId
, ev
.dwThreadId
, DBG_CONTINUE
);
777 ok(ret
, "ContinueDebugEvent failed, last error %d.\n", GetLastError());
781 ok(got_child_event
, "didn't get any child events (flag: %x).\n", flag
);
783 ok(!got_child_event
, "got child event (flag: %x).\n", flag
);
784 CloseHandle(event_init
);
785 CloseHandle(event_attach
);
787 ret
= CloseHandle(pi
.hThread
);
788 ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
789 ret
= CloseHandle(pi
.hProcess
);
790 ok(ret
, "CloseHandle failed, last error %d.\n", GetLastError());
792 load_blackbox(blackbox_file
, &blackbox
, sizeof(blackbox
));
793 ok(!blackbox
.failures
, "Got %d failures from child process.\n", blackbox
.failures
);
795 ret
= DeleteFileA(blackbox_file
);
796 ok(ret
, "DeleteFileA failed, last error %d.\n", GetLastError());
803 hdll
=GetModuleHandleA("kernel32.dll");
804 pCheckRemoteDebuggerPresent
=(void*)GetProcAddress(hdll
, "CheckRemoteDebuggerPresent");
805 pDebugActiveProcessStop
=(void*)GetProcAddress(hdll
, "DebugActiveProcessStop");
806 pDebugSetProcessKillOnExit
=(void*)GetProcAddress(hdll
, "DebugSetProcessKillOnExit");
807 pIsDebuggerPresent
=(void*)GetProcAddress(hdll
, "IsDebuggerPresent");
809 myARGC
=winetest_get_mainargs(&myARGV
);
810 if (myARGC
>= 3 && strcmp(myARGV
[2], "crash") == 0)
812 doCrash(myARGC
, myARGV
);
814 else if (myARGC
>= 3 && strncmp(myARGV
[2], "dbg,", 4) == 0)
816 doDebugger(myARGC
, myARGV
);
818 else if (myARGC
>= 5 && !strcmp(myARGV
[2], "child"))
820 doChild(myARGC
, myARGV
);
822 else if (myARGC
>= 4 && !strcmp(myARGV
[2], "children"))
824 doChildren(myARGC
, myARGV
);
829 test_RemoteDebugger();
830 test_debug_loop(myARGC
, myARGV
);
831 test_debug_children(myARGV
[0], DEBUG_PROCESS
, TRUE
);
832 test_debug_children(myARGV
[0], DEBUG_ONLY_THIS_PROCESS
, FALSE
);
833 test_debug_children(myARGV
[0], DEBUG_PROCESS
|DEBUG_ONLY_THIS_PROCESS
, FALSE
);
834 test_debug_children(myARGV
[0], 0, FALSE
);