kernel32: When testing the debugger activation, don't use unitialized values (clang).
[wine/multimedia.git] / dlls / kernel32 / tests / debugger.c
blob7f7b052d98f0a9803c3603f15b41f3638fbc371e
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);
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, ...)
54 va_list valist;
56 va_start(valist, msg);
57 winetest_vok(condition, msg, valist);
58 va_end(valist);
59 if (!condition) ++child_failures;
62 /* Copied from the process test */
63 static void get_file_name(char* buf)
65 char path[MAX_PATH];
67 buf[0] = '\0';
68 GetTempPathA(sizeof(path), path);
69 GetTempFileNameA(path, "wt", 0, buf);
72 typedef struct tag_reg_save_value
74 const char *name;
75 DWORD type;
76 BYTE *data;
77 DWORD size;
78 } reg_save_value;
80 static DWORD save_value(HKEY hkey, const char *value, reg_save_value *saved)
82 DWORD ret;
83 saved->name=value;
84 saved->data=0;
85 saved->size=0;
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);
92 return ret;
95 static void restore_value(HKEY hkey, reg_save_value *saved)
97 if (saved->data)
99 RegSetValueExA(hkey, saved->name, 0, saved->type, saved->data, saved->size);
100 HeapFree(GetProcessHeap(), 0, saved->data);
102 else
103 RegDeleteValueA(hkey, saved->name);
106 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
108 const char* basename;
109 char* event_name;
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)
124 HANDLE hFile;
125 DWORD written;
127 hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
128 if (hFile == INVALID_HANDLE_VALUE)
129 return;
130 WriteFile(hFile, blackbox, size, &written, NULL);
131 CloseHandle(hFile);
134 static int load_blackbox(const char* logfile, void* blackbox, int size)
136 HANDLE hFile;
137 DWORD read;
138 BOOL ret;
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);
144 return 0;
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);
150 CloseHandle(hFile);
151 return 1;
154 typedef struct
156 DWORD pid;
157 } crash_blackbox_t;
159 static void doCrash(int argc, char** argv)
161 char* p;
163 /* make sure the exception gets to the debugger */
164 SetErrorMode( 0 );
165 SetUnhandledExceptionFilter( NULL );
167 if (argc >= 4)
169 crash_blackbox_t blackbox;
170 blackbox.pid=GetCurrentProcessId();
171 save_blackbox(argv[3], &blackbox, sizeof(blackbox));
174 /* Just crash */
175 trace("child: crashing...\n");
176 p=NULL;
177 *p=0;
180 typedef struct
182 int argc;
183 DWORD pid;
184 BOOL debug_rc;
185 DWORD debug_err;
186 BOOL attach_rc;
187 DWORD attach_err;
188 BOOL nokill_rc;
189 DWORD nokill_err;
190 BOOL detach_rc;
191 DWORD detach_err;
192 } debugger_blackbox_t;
194 static void doDebugger(int argc, char** argv)
196 const char* logfile;
197 debugger_blackbox_t blackbox;
198 HANDLE start_event = 0, done_event = 0, debug_event;
200 blackbox.argc=argc;
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();
211 else
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();
222 else
223 blackbox.debug_rc=TRUE;
225 if (logfile)
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();
243 else
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();
253 else
254 blackbox.detach_rc=TRUE;
256 if (logfile)
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;
270 BOOL bRet;
271 DWORD ret;
272 HANDLE start_event, done_event;
273 char* cmd;
274 char dbglog[MAX_PATH];
275 char childlog[MAX_PATH];
276 PROCESS_INFORMATION info;
277 STARTUPINFOA startup;
278 DWORD exit_code;
279 crash_blackbox_t crash_blackbox;
280 debugger_blackbox_t dbg_blackbox;
281 DWORD wait_code;
283 if (skip_crash_and_debug)
285 win_skip("Skipping crash_and_debug\n");
286 return;
289 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
290 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
292 get_file_name(dbglog);
293 get_events(dbglog, &start_event, &done_event);
294 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+2+34+1);
295 sprintf(cmd, "%s debugger %s \"%s\" %%ld %%ld", argv0, dbgtasks, dbglog);
296 ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
297 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
298 HeapFree(GetProcessHeap(), 0, cmd);
300 get_file_name(childlog);
301 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+2+1);
302 sprintf(cmd, "%s debugger crash \"%s\"", argv0, childlog);
304 memset(&startup, 0, sizeof(startup));
305 startup.cb = sizeof(startup);
306 startup.dwFlags = STARTF_USESHOWWINDOW;
307 startup.wShowWindow = SW_SHOWNORMAL;
308 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
309 ok(ret, "CreateProcess: err=%d\n", GetLastError());
310 HeapFree(GetProcessHeap(), 0, cmd);
311 CloseHandle(info.hThread);
313 /* The process exits... */
314 trace("waiting for child exit...\n");
315 wait_code = WaitForSingleObject(info.hProcess, 30000);
316 #if defined(_WIN64) && defined(__MINGW32__)
317 /* Mingw x64 doesn't output proper unwind info */
318 skip_crash_and_debug = broken(wait_code == WAIT_TIMEOUT);
319 if (skip_crash_and_debug)
321 TerminateProcess(info.hProcess, WAIT_TIMEOUT);
322 WaitForSingleObject(info.hProcess, 5000);
323 CloseHandle(info.hProcess);
324 assert(DeleteFileA(dbglog) != 0);
325 assert(DeleteFileA(childlog) != 0);
326 win_skip("Giving up on child process\n");
327 return;
329 #endif
330 ok(wait_code == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
331 bRet = GetExitCodeProcess(info.hProcess, &exit_code);
332 ok(bRet, "GetExitCodeProcess failed: err=%d\n", GetLastError());
333 if (strstr(dbgtasks, "code2"))
335 /* If, after attaching to the debuggee, the debugger exits without
336 * detaching, then the debuggee gets a special exit code.
338 ok(exit_code == STATUS_DEBUGGER_INACTIVE ||
339 broken(exit_code == STATUS_ACCESS_VIOLATION) || /* Intermittent Vista+ */
340 broken(exit_code == 0xffffffff) || /* Win9x */
341 broken(exit_code == WAIT_ABANDONED), /* NT4, W2K */
342 "wrong exit code : %08x\n", exit_code);
344 else
345 ok(exit_code == STATUS_ACCESS_VIOLATION ||
346 broken(exit_code == WAIT_ABANDONED) || /* NT4, W2K, W2K3 */
347 broken(exit_code == 0xffffffff), /* Win9x, WinME */
348 "wrong exit code : %08x\n", exit_code);
349 CloseHandle(info.hProcess);
351 /* ...before the debugger */
352 if (strstr(dbgtasks, "order"))
353 ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
355 trace("waiting for the debugger...\n");
356 wait_code = WaitForSingleObject(done_event, 5000);
357 #if defined(_WIN64) && defined(__MINGW32__)
358 /* Mingw x64 doesn't output proper unwind info */
359 skip_crash_and_debug = broken(wait_code == WAIT_TIMEOUT);
360 if (skip_crash_and_debug)
362 assert(DeleteFileA(dbglog) != 0);
363 assert(DeleteFileA(childlog) != 0);
364 win_skip("Giving up on debugger\n");
365 return;
367 #endif
368 ok(wait_code == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
370 assert(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)));
371 assert(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)));
373 ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
374 ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
375 ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
376 ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
377 ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
378 ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
380 assert(DeleteFileA(dbglog) != 0);
381 assert(DeleteFileA(childlog) != 0);
384 static void crash_and_winedbg(HKEY hkey, const char* argv0)
386 BOOL bRet;
387 DWORD ret;
388 char* cmd;
389 PROCESS_INFORMATION info;
390 STARTUPINFOA startup;
391 DWORD exit_code;
393 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
394 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
396 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
397 sprintf(cmd, "%s debugger crash", argv0);
399 memset(&startup, 0, sizeof(startup));
400 startup.cb = sizeof(startup);
401 startup.dwFlags = STARTF_USESHOWWINDOW;
402 startup.wShowWindow = SW_SHOWNORMAL;
403 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
404 ok(ret, "CreateProcess: err=%d\n", GetLastError());
405 HeapFree(GetProcessHeap(), 0, cmd);
406 CloseHandle(info.hThread);
408 trace("waiting for child exit...\n");
409 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
410 bRet = GetExitCodeProcess(info.hProcess, &exit_code);
411 ok(bRet, "GetExitCodeProcess failed: err=%d\n", GetLastError());
412 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
413 CloseHandle(info.hProcess);
416 static void test_ExitCode(void)
418 static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
419 static const char* WineDbg="Software\\Wine\\WineDbg";
420 char test_exe[MAX_PATH];
421 DWORD ret;
422 HKEY hkey;
423 DWORD disposition;
424 reg_save_value auto_value;
425 reg_save_value debugger_value;
427 GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
428 if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
429 strcat(test_exe, ".so");
430 if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
432 ok(0, "could not find the test executable '%s'\n", test_exe);
433 return;
436 ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
437 if (ret == ERROR_SUCCESS)
439 save_value(hkey, "auto", &auto_value);
440 save_value(hkey, "debugger", &debugger_value);
441 trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug, debugger_value.data);
443 else if (ret == ERROR_ACCESS_DENIED)
445 skip("not enough privileges to change the debugger\n");
446 return;
448 else if (ret != ERROR_FILE_NOT_FOUND)
450 ok(0, "could not open the AeDebug key: %d\n", ret);
451 return;
453 else debugger_value.data = NULL;
455 if (debugger_value.data && debugger_value.type == REG_SZ &&
456 strstr((char*)debugger_value.data, "winedbg --auto"))
458 HKEY hkeyWinedbg;
459 ret=RegCreateKeyA(HKEY_CURRENT_USER, WineDbg, &hkeyWinedbg);
460 if (ret == ERROR_SUCCESS)
462 static DWORD zero;
463 reg_save_value crash_dlg_value;
464 save_value(hkeyWinedbg, "ShowCrashDialog", &crash_dlg_value);
465 RegSetValueExA(hkeyWinedbg, "ShowCrashDialog", 0, REG_DWORD, (BYTE *)&zero, sizeof(DWORD));
466 crash_and_winedbg(hkey, test_exe);
467 restore_value(hkeyWinedbg, &crash_dlg_value);
468 RegCloseKey(hkeyWinedbg);
470 else
471 ok(0, "Couldn't access WineDbg Key - error %u\n", ret);
474 if (winetest_interactive)
475 /* Since the debugging process never sets the debug event, it isn't recognized
476 as a valid debugger and, after the debugger exits, Windows will show a dialog box
477 asking the user what to do */
478 crash_and_debug(hkey, test_exe, "dbg,none");
479 else
480 skip("\"none\" debugger test needs user interaction\n");
481 if (disposition == REG_CREATED_NEW_KEY)
482 win_skip("'dbg,event,order' test doesn't finish on Win9x/WinMe\n");
483 else
484 crash_and_debug(hkey, test_exe, "dbg,event,order");
485 crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
486 if (pDebugSetProcessKillOnExit)
487 crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
488 else
489 win_skip("DebugSetProcessKillOnExit is not available\n");
490 if (pDebugActiveProcessStop)
491 crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
492 else
493 win_skip("DebugActiveProcessStop is not available\n");
495 if (disposition == REG_CREATED_NEW_KEY)
497 RegCloseKey(hkey);
498 RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
500 else
502 restore_value(hkey, &auto_value);
503 restore_value(hkey, &debugger_value);
504 RegCloseKey(hkey);
508 static void test_RemoteDebugger(void)
510 BOOL bret, present;
511 if(!pCheckRemoteDebuggerPresent)
513 win_skip("CheckRemoteDebuggerPresent is not available\n");
514 return;
516 present = TRUE;
517 SetLastError(0xdeadbeef);
518 bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present);
519 ok(bret , "expected CheckRemoteDebuggerPresent to succeed\n");
520 ok(0xdeadbeef == GetLastError(),
521 "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());
523 present = TRUE;
524 SetLastError(0xdeadbeef);
525 bret = pCheckRemoteDebuggerPresent(NULL,&present);
526 ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
527 ok(present, "expected parameter to be unchanged\n");
528 ok(ERROR_INVALID_PARAMETER == GetLastError(),
529 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
531 SetLastError(0xdeadbeef);
532 bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL);
533 ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
534 ok(ERROR_INVALID_PARAMETER == GetLastError(),
535 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
538 struct child_blackbox
540 LONG failures;
543 static void doChild(int argc, char **argv)
545 struct child_blackbox blackbox;
546 const char *blackbox_file;
547 HANDLE parent;
548 DWORD ppid;
549 BOOL debug;
550 BOOL ret;
552 blackbox_file = argv[4];
553 sscanf(argv[3], "%08x", &ppid);
555 parent = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ppid);
556 child_ok(!!parent, "OpenProcess failed, last error %#x.\n", GetLastError());
558 ret = pCheckRemoteDebuggerPresent(parent, &debug);
559 child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
560 child_ok(!debug, "Expected debug == 0, got %#x.\n", debug);
562 ret = DebugActiveProcess(ppid);
563 child_ok(ret, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
565 ret = pCheckRemoteDebuggerPresent(parent, &debug);
566 child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
567 child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
569 ret = pDebugActiveProcessStop(ppid);
570 child_ok(ret, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());
572 ret = pCheckRemoteDebuggerPresent(parent, &debug);
573 child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
574 child_ok(!debug, "Expected debug == 0, got %#x.\n", debug);
576 ret = CloseHandle(parent);
577 child_ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
579 ret = pIsDebuggerPresent();
580 child_ok(ret, "Expected ret != 0, got %#x.\n", ret);
581 ret = pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug);
582 child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
583 child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
585 if (pNtCurrentTeb)
587 pNtCurrentTeb()->Peb->BeingDebugged = FALSE;
589 ret = pIsDebuggerPresent();
590 child_ok(!ret, "Expected ret != 0, got %#x.\n", ret);
591 ret = pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug);
592 child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
593 child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
595 pNtCurrentTeb()->Peb->BeingDebugged = TRUE;
598 blackbox.failures = child_failures;
599 save_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
602 static void test_debug_loop(int argc, char **argv)
604 const char *arguments = " debugger child ";
605 struct child_blackbox blackbox;
606 char blackbox_file[MAX_PATH];
607 PROCESS_INFORMATION pi;
608 STARTUPINFOA si;
609 BOOL debug;
610 DWORD pid;
611 char *cmd;
612 BOOL ret;
614 if (!pDebugActiveProcessStop || !pCheckRemoteDebuggerPresent)
616 win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
617 return;
620 pid = GetCurrentProcessId();
621 ret = DebugActiveProcess(pid);
622 ok(!ret, "DebugActiveProcess() succeeded on own process.\n");
624 get_file_name(blackbox_file);
625 cmd = HeapAlloc(GetProcessHeap(), 0, strlen(argv[0]) + strlen(arguments) + strlen(blackbox_file) + 2 + 10);
626 sprintf(cmd, "%s%s%08x \"%s\"", argv[0], arguments, pid, blackbox_file);
628 memset(&si, 0, sizeof(si));
629 si.cb = sizeof(si);
630 ret = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
631 ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
633 HeapFree(GetProcessHeap(), 0, cmd);
635 ret = pCheckRemoteDebuggerPresent(pi.hProcess, &debug);
636 ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
637 ok(debug, "Expected debug != 0, got %#x.\n", debug);
639 for (;;)
641 DEBUG_EVENT ev;
643 ret = WaitForDebugEvent(&ev, INFINITE);
644 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
645 if (!ret) break;
647 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
649 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
650 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
651 if (!ret) break;
654 ret = CloseHandle(pi.hThread);
655 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
656 ret = CloseHandle(pi.hProcess);
657 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
659 load_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
660 ok(!blackbox.failures, "Got %d failures from child process.\n", blackbox.failures);
662 ret = DeleteFileA(blackbox_file);
663 ok(ret, "DeleteFileA failed, last error %#x.\n", GetLastError());
666 START_TEST(debugger)
668 HMODULE hdll;
670 hdll=GetModuleHandle("kernel32.dll");
671 pCheckRemoteDebuggerPresent=(void*)GetProcAddress(hdll, "CheckRemoteDebuggerPresent");
672 pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
673 pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
674 pIsDebuggerPresent=(void*)GetProcAddress(hdll, "IsDebuggerPresent");
675 hdll=GetModuleHandle("ntdll.dll");
676 if (hdll) pNtCurrentTeb = (void*)GetProcAddress(hdll, "NtCurrentTeb");
678 myARGC=winetest_get_mainargs(&myARGV);
679 if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
681 doCrash(myARGC, myARGV);
683 else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
685 doDebugger(myARGC, myARGV);
687 else if (myARGC >= 5 && !strcmp(myARGV[2], "child"))
689 doChild(myARGC, myARGV);
691 else
693 test_ExitCode();
694 test_RemoteDebugger();
695 test_debug_loop(myARGC, myARGV);