samlib: Add stubbed samlib.dll.
[wine] / dlls / kernel32 / tests / debugger.c
1 /*
2  * Unit tests for the debugger facility
3  *
4  * Copyright (c) 2007 Francois Gouget for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <assert.h>
23
24 #include <windows.h>
25 #include <winternl.h>
26 #include <winreg.h>
27 #include "wine/test.h"
28
29 #ifndef STATUS_DEBUGGER_INACTIVE
30 #define STATUS_DEBUGGER_INACTIVE         ((NTSTATUS) 0xC0000354)
31 #endif
32
33 #ifdef __GNUC__
34 #define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
35 #else
36 #define PRINTF_ATTR(fmt,args)
37 #endif
38
39 #define child_ok (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_child_ok
40
41 static int    myARGC;
42 static char** myARGV;
43
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);
49
50 static LONG child_failures;
51
52 static void PRINTF_ATTR(2, 3) test_child_ok(int condition, const char *msg, ...)
53 {
54     va_list valist;
55
56     va_start(valist, msg);
57     winetest_vok(condition, msg, valist);
58     va_end(valist);
59     if (!condition) ++child_failures;
60 }
61
62 /* Copied from the process test */
63 static void get_file_name(char* buf)
64 {
65     char path[MAX_PATH];
66
67     buf[0] = '\0';
68     GetTempPathA(sizeof(path), path);
69     GetTempFileNameA(path, "wt", 0, buf);
70 }
71
72 typedef struct tag_reg_save_value
73 {
74     const char *name;
75     DWORD type;
76     BYTE *data;
77     DWORD size;
78 } reg_save_value;
79
80 static DWORD save_value(HKEY hkey, const char *value, reg_save_value *saved)
81 {
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)
88     {
89         saved->data=HeapAlloc(GetProcessHeap(), 0, saved->size);
90         RegQueryValueExA(hkey, value, NULL, &saved->type, saved->data, &saved->size);
91     }
92     return ret;
93 }
94
95 static void restore_value(HKEY hkey, reg_save_value *saved)
96 {
97     if (saved->data)
98     {
99         RegSetValueExA(hkey, saved->name, 0, saved->type, saved->data, saved->size);
100         HeapFree(GetProcessHeap(), 0, saved->data);
101     }
102     else
103         RegDeleteValueA(hkey, saved->name);
104 }
105
106 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
107 {
108     const char* basename;
109     char* event_name;
110
111     basename=strrchr(name, '\\');
112     basename=(basename ? basename+1 : name);
113     event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
114
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);
120 }
121
122 static void save_blackbox(const char* logfile, void* blackbox, int size)
123 {
124     HANDLE hFile;
125     DWORD written;
126
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);
132 }
133
134 static int load_blackbox(const char* logfile, void* blackbox, int size)
135 {
136     HANDLE hFile;
137     DWORD read;
138     BOOL ret;
139
140     hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
141     if (hFile == INVALID_HANDLE_VALUE)
142     {
143         ok(0, "unable to open '%s'\n", logfile);
144         return 0;
145     }
146     ret=ReadFile(hFile, blackbox, size, &read, NULL);
147     ok(read == size, "wrong size for '%s': read=%d\n", logfile, read);
148     CloseHandle(hFile);
149     return 1;
150 }
151
152 typedef struct
153 {
154     DWORD pid;
155 } crash_blackbox_t;
156
157 static void doCrash(int argc,  char** argv)
158 {
159     char* p;
160
161     if (argc >= 4)
162     {
163         crash_blackbox_t blackbox;
164         blackbox.pid=GetCurrentProcessId();
165         save_blackbox(argv[3], &blackbox, sizeof(blackbox));
166     }
167
168     /* Just crash */
169     trace("child: crashing...\n");
170     p=NULL;
171     *p=0;
172 }
173
174 typedef struct
175 {
176     int argc;
177     DWORD pid;
178     BOOL debug_rc;
179     DWORD debug_err;
180     BOOL attach_rc;
181     DWORD attach_err;
182     BOOL nokill_rc;
183     DWORD nokill_err;
184     BOOL detach_rc;
185     DWORD detach_err;
186 } debugger_blackbox_t;
187
188 static void doDebugger(int argc, char** argv)
189 {
190     const char* logfile;
191     debugger_blackbox_t blackbox;
192     HANDLE start_event = 0, done_event = 0, debug_event;
193
194     blackbox.argc=argc;
195     logfile=(argc >= 4 ? argv[3] : NULL);
196     blackbox.pid=(argc >= 5 ? atol(argv[4]) : 0);
197
198     blackbox.attach_err=0;
199     if (strstr(myARGV[2], "attach"))
200     {
201         blackbox.attach_rc=DebugActiveProcess(blackbox.pid);
202         if (!blackbox.attach_rc)
203             blackbox.attach_err=GetLastError();
204     }
205     else
206         blackbox.attach_rc=TRUE;
207
208     debug_event=(argc >= 6 ? (HANDLE)(INT_PTR)atol(argv[5]) : NULL);
209     blackbox.debug_err=0;
210     if (debug_event && strstr(myARGV[2], "event"))
211     {
212         blackbox.debug_rc=SetEvent(debug_event);
213         if (!blackbox.debug_rc)
214             blackbox.debug_err=GetLastError();
215     }
216     else
217         blackbox.debug_rc=TRUE;
218
219     if (logfile)
220     {
221         get_events(logfile, &start_event, &done_event);
222     }
223
224     if (strstr(myARGV[2], "order"))
225     {
226         trace("debugger: waiting for the start signal...\n");
227         WaitForSingleObject(start_event, INFINITE);
228     }
229
230     blackbox.nokill_err=0;
231     if (strstr(myARGV[2], "nokill"))
232     {
233         blackbox.nokill_rc=pDebugSetProcessKillOnExit(FALSE);
234         if (!blackbox.nokill_rc)
235             blackbox.nokill_err=GetLastError();
236     }
237     else
238         blackbox.nokill_rc=TRUE;
239
240     blackbox.detach_err=0;
241     if (strstr(myARGV[2], "detach"))
242     {
243         blackbox.detach_rc=pDebugActiveProcessStop(blackbox.pid);
244         if (!blackbox.detach_rc)
245             blackbox.detach_err=GetLastError();
246     }
247     else
248         blackbox.detach_rc=TRUE;
249
250     if (logfile)
251     {
252         save_blackbox(logfile, &blackbox, sizeof(blackbox));
253     }
254     trace("debugger: done debugging...\n");
255     SetEvent(done_event);
256
257     /* Just exit with a known value */
258     ExitProcess(0xdeadbeef);
259 }
260
261 static void crash_and_debug(HKEY hkey, const char* argv0, const char* dbgtasks)
262 {
263     DWORD ret;
264     HANDLE start_event, done_event;
265     char* cmd;
266     char dbglog[MAX_PATH];
267     char childlog[MAX_PATH];
268     PROCESS_INFORMATION info;
269     STARTUPINFOA startup;
270     DWORD exit_code;
271     crash_blackbox_t crash_blackbox;
272     debugger_blackbox_t dbg_blackbox;
273
274     ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
275     ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
276
277     get_file_name(dbglog);
278     get_events(dbglog, &start_event, &done_event);
279     cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+34+1);
280     sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, dbgtasks, dbglog);
281     ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
282     ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
283     HeapFree(GetProcessHeap(), 0, cmd);
284
285     get_file_name(childlog);
286     cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
287     sprintf(cmd, "%s debugger crash %s", argv0, childlog);
288
289     memset(&startup, 0, sizeof(startup));
290     startup.cb = sizeof(startup);
291     startup.dwFlags = STARTF_USESHOWWINDOW;
292     startup.wShowWindow = SW_SHOWNORMAL;
293     ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
294     ok(ret, "CreateProcess: err=%d\n", GetLastError());
295     HeapFree(GetProcessHeap(), 0, cmd);
296     CloseHandle(info.hThread);
297
298     /* The process exits... */
299     trace("waiting for child exit...\n");
300     ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
301     ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
302     if (strstr(dbgtasks, "code2"))
303     {
304         /* If, after attaching to the debuggee, the debugger exits without
305          * detaching, then the debuggee gets a special exit code.
306          */
307         ok(exit_code == STATUS_DEBUGGER_INACTIVE ||
308            broken(exit_code == STATUS_ACCESS_VIOLATION) || /* Intermittent Vista+ */
309            broken(exit_code == 0xffffffff) || /* Win9x */
310            broken(exit_code == WAIT_ABANDONED), /* NT4, W2K */
311            "wrong exit code : %08x\n", exit_code);
312     }
313     else
314         ok(exit_code == STATUS_ACCESS_VIOLATION ||
315            broken(exit_code == WAIT_ABANDONED) || /* NT4, W2K, W2K3 */
316            broken(exit_code == 0xffffffff), /* Win9x, WinME */
317            "wrong exit code : %08x\n", exit_code);
318     CloseHandle(info.hProcess);
319
320     /* ...before the debugger */
321     if (strstr(dbgtasks, "order"))
322         ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
323
324     trace("waiting for the debugger...\n");
325     ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
326
327     assert(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)));
328     assert(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)));
329
330     ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
331     ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
332     ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
333     ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
334     ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
335     ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
336
337     assert(DeleteFileA(dbglog) != 0);
338     assert(DeleteFileA(childlog) != 0);
339 }
340
341 static void crash_and_winedbg(HKEY hkey, const char* argv0)
342 {
343     DWORD ret;
344     char* cmd;
345     PROCESS_INFORMATION info;
346     STARTUPINFOA startup;
347     DWORD exit_code;
348
349     ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
350     ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
351
352     cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
353     sprintf(cmd, "%s debugger crash", argv0);
354
355     memset(&startup, 0, sizeof(startup));
356     startup.cb = sizeof(startup);
357     startup.dwFlags = STARTF_USESHOWWINDOW;
358     startup.wShowWindow = SW_SHOWNORMAL;
359     ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
360     ok(ret, "CreateProcess: err=%d\n", GetLastError());
361     HeapFree(GetProcessHeap(), 0, cmd);
362     CloseHandle(info.hThread);
363
364     trace("waiting for child exit...\n");
365     ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
366     ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
367     ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
368     CloseHandle(info.hProcess);
369 }
370
371 static void test_ExitCode(void)
372 {
373     static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
374     static const char* WineDbg="Software\\Wine\\WineDbg";
375     char test_exe[MAX_PATH];
376     DWORD ret;
377     HKEY hkey;
378     DWORD disposition;
379     reg_save_value auto_value;
380     reg_save_value debugger_value;
381
382     GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
383     if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
384         strcat(test_exe, ".so");
385     if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
386     {
387         ok(0, "could not find the test executable '%s'\n", test_exe);
388         return;
389     }
390
391     ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
392     if (ret == ERROR_SUCCESS)
393     {
394         save_value(hkey, "auto", &auto_value);
395         save_value(hkey, "debugger", &debugger_value);
396         trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug, debugger_value.data);
397     }
398     else if (ret == ERROR_ACCESS_DENIED)
399     {
400         skip("not enough privileges to change the debugger\n");
401         return;
402     }
403     else if (ret != ERROR_FILE_NOT_FOUND)
404     {
405         ok(0, "could not open the AeDebug key: %d\n", ret);
406         return;
407     }
408
409     if (debugger_value.data && debugger_value.type == REG_SZ &&
410         strstr((char*)debugger_value.data, "winedbg --auto"))
411     {
412         HKEY hkeyWinedbg;
413         ret=RegCreateKeyA(HKEY_CURRENT_USER, WineDbg, &hkeyWinedbg);
414         if (ret == ERROR_SUCCESS)
415         {
416             static DWORD zero;
417             reg_save_value crash_dlg_value;
418             save_value(hkeyWinedbg, "ShowCrashDialog", &crash_dlg_value);
419             RegSetValueExA(hkeyWinedbg, "ShowCrashDialog", 0, REG_DWORD, (BYTE *)&zero, sizeof(DWORD));
420             crash_and_winedbg(hkey, test_exe);
421             restore_value(hkeyWinedbg, &crash_dlg_value);
422             RegCloseKey(hkeyWinedbg);
423         }
424         else
425             ok(0, "Couldn't access WineDbg Key - error %u\n", ret);
426     }
427
428     if (winetest_interactive)
429         /* Since the debugging process never sets the debug event, it isn't recognized
430            as a valid debugger and, after the debugger exits, Windows will show a dialog box
431            asking the user what to do */
432         crash_and_debug(hkey, test_exe, "dbg,none");
433     else
434         skip("\"none\" debugger test needs user interaction\n");
435     if (disposition == REG_CREATED_NEW_KEY)
436         win_skip("'dbg,event,order' test doesn't finish on Win9x/WinMe\n");
437     else
438         crash_and_debug(hkey, test_exe, "dbg,event,order");
439     crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
440     if (pDebugSetProcessKillOnExit)
441         crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
442     else
443         win_skip("DebugSetProcessKillOnExit is not available\n");
444     if (pDebugActiveProcessStop)
445         crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
446     else
447         win_skip("DebugActiveProcessStop is not available\n");
448
449     if (disposition == REG_CREATED_NEW_KEY)
450     {
451         RegCloseKey(hkey);
452         RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
453     }
454     else
455     {
456         restore_value(hkey, &auto_value);
457         restore_value(hkey, &debugger_value);
458         RegCloseKey(hkey);
459     }
460 }
461
462 static void test_RemoteDebugger(void)
463 {
464     BOOL bret, present;
465     if(!pCheckRemoteDebuggerPresent)
466     {
467         win_skip("CheckRemoteDebuggerPresent is not available\n");
468         return;
469     }
470     present = TRUE;
471     SetLastError(0xdeadbeef);
472     bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present);
473     ok(bret , "expected CheckRemoteDebuggerPresent to succeed\n");
474     ok(0xdeadbeef == GetLastError(),
475        "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());
476
477     present = TRUE;
478     SetLastError(0xdeadbeef);
479     bret = pCheckRemoteDebuggerPresent(NULL,&present);
480     ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
481     ok(present, "expected parameter to be unchanged\n");
482     ok(ERROR_INVALID_PARAMETER == GetLastError(),
483        "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
484
485     SetLastError(0xdeadbeef);
486     bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL);
487     ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
488     ok(ERROR_INVALID_PARAMETER == GetLastError(),
489        "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
490 }
491
492 struct child_blackbox
493 {
494     LONG failures;
495 };
496
497 static void doChild(int argc, char **argv)
498 {
499     struct child_blackbox blackbox;
500     const char *blackbox_file;
501     HANDLE parent;
502     DWORD ppid;
503     BOOL debug;
504     BOOL ret;
505
506     blackbox_file = argv[4];
507     sscanf(argv[3], "%08x", &ppid);
508
509     parent = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ppid);
510     child_ok(!!parent, "OpenProcess failed, last error %#x.\n", GetLastError());
511
512     ret = pCheckRemoteDebuggerPresent(parent, &debug);
513     child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
514     child_ok(!debug, "Expected debug == 0, got %#x.\n", debug);
515
516     ret = DebugActiveProcess(ppid);
517     child_ok(ret, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
518
519     ret = pCheckRemoteDebuggerPresent(parent, &debug);
520     child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
521     child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
522
523     ret = pDebugActiveProcessStop(ppid);
524     child_ok(ret, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());
525
526     ret = pCheckRemoteDebuggerPresent(parent, &debug);
527     child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
528     child_ok(!debug, "Expected debug == 0, got %#x.\n", debug);
529
530     ret = CloseHandle(parent);
531     child_ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
532
533     ret = pIsDebuggerPresent();
534     child_ok(ret, "Expected ret != 0, got %#x.\n", ret);
535     ret = pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug);
536     child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
537     child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
538
539     if (pNtCurrentTeb)
540     {
541         pNtCurrentTeb()->Peb->BeingDebugged = FALSE;
542
543         ret = pIsDebuggerPresent();
544         child_ok(!ret, "Expected ret != 0, got %#x.\n", ret);
545         ret = pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug);
546         child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
547         child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
548
549         pNtCurrentTeb()->Peb->BeingDebugged = TRUE;
550     }
551
552     blackbox.failures = child_failures;
553     save_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
554 }
555
556 static void test_debug_loop(int argc, char **argv)
557 {
558     const char *arguments = " debugger child ";
559     struct child_blackbox blackbox;
560     char blackbox_file[MAX_PATH];
561     PROCESS_INFORMATION pi;
562     STARTUPINFOA si;
563     BOOL debug;
564     DWORD pid;
565     char *cmd;
566     BOOL ret;
567
568     if (!pDebugActiveProcessStop || !pCheckRemoteDebuggerPresent)
569     {
570         win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
571         return;
572     }
573
574     pid = GetCurrentProcessId();
575     ret = DebugActiveProcess(pid);
576     ok(!ret, "DebugActiveProcess() succeeded on own process.\n");
577
578     get_file_name(blackbox_file);
579     cmd = HeapAlloc(GetProcessHeap(), 0, strlen(argv[0]) + strlen(arguments) + strlen(blackbox_file) + 10);
580     sprintf(cmd, "%s%s%08x %s", argv[0], arguments, pid, blackbox_file);
581
582     memset(&si, 0, sizeof(si));
583     si.cb = sizeof(si);
584     ret = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
585     ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
586
587     HeapFree(GetProcessHeap(), 0, cmd);
588
589     ret = pCheckRemoteDebuggerPresent(pi.hProcess, &debug);
590     ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
591     ok(debug, "Expected debug != 0, got %#x.\n", debug);
592
593     for (;;)
594     {
595         DEBUG_EVENT ev;
596
597         ret = WaitForDebugEvent(&ev, INFINITE);
598         ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
599         if (!ret) break;
600
601         if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
602
603         ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
604         ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
605         if (!ret) break;
606     }
607
608     ret = CloseHandle(pi.hThread);
609     ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
610     ret = CloseHandle(pi.hProcess);
611     ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
612
613     load_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
614     ok(!blackbox.failures, "Got %d failures from child process.\n", blackbox.failures);
615
616     ret = DeleteFileA(blackbox_file);
617     ok(ret, "DeleteFileA failed, last error %#x.\n", GetLastError());
618 }
619
620 START_TEST(debugger)
621 {
622     HMODULE hdll;
623
624     hdll=GetModuleHandle("kernel32.dll");
625     pCheckRemoteDebuggerPresent=(void*)GetProcAddress(hdll, "CheckRemoteDebuggerPresent");
626     pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
627     pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
628     pIsDebuggerPresent=(void*)GetProcAddress(hdll, "IsDebuggerPresent");
629     hdll=GetModuleHandle("ntdll.dll");
630     if (hdll) pNtCurrentTeb = (void*)GetProcAddress(hdll, "NtCurrentTeb");
631
632     myARGC=winetest_get_mainargs(&myARGV);
633     if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
634     {
635         doCrash(myARGC, myARGV);
636     }
637     else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
638     {
639         doDebugger(myARGC, myARGV);
640     }
641     else if (myARGC >= 5 && !strcmp(myARGV[2], "child"))
642     {
643         doChild(myARGC, myARGV);
644     }
645     else
646     {
647         test_ExitCode();
648         test_RemoteDebugger();
649         test_debug_loop(myARGC, myARGV);
650     }
651 }