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