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