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