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