debugger.c 53.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Unit tests for the debugger facility
 *
 * Copyright (c) 2007 Francois Gouget for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdio.h>
#include <assert.h>

#include <windows.h>
25
#include <winternl.h>
26 27
#include <winreg.h>
#include "wine/test.h"
28 29
#include "wine/heap.h"
#include "wine/rbtree.h"
30

31 32 33 34
#ifndef STATUS_DEBUGGER_INACTIVE
#define STATUS_DEBUGGER_INACTIVE         ((NTSTATUS) 0xC0000354)
#endif

35 36
#define child_ok (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_child_ok

37 38 39
static int    myARGC;
static char** myARGV;

40
static BOOL (WINAPI *pCheckRemoteDebuggerPresent)(HANDLE,PBOOL);
41 42
static BOOL (WINAPI *pDebugActiveProcessStop)(DWORD);
static BOOL (WINAPI *pDebugSetProcessKillOnExit)(BOOL);
43
static BOOL (WINAPI *pIsDebuggerPresent)(void);
44

45 46
static void (WINAPI *pDbgBreakPoint)(void);

47 48
static LONG child_failures;

49 50
static HMODULE ntdll;

51
static void WINAPIV WINETEST_PRINTF_ATTR(2, 3) test_child_ok(int condition, const char *msg, ...)
52
{
53
    __ms_va_list valist;
54

55
    __ms_va_start(valist, msg);
56
    winetest_vok(condition, msg, valist);
57
    __ms_va_end(valist);
58 59 60
    if (!condition) ++child_failures;
}

61 62 63 64 65 66 67 68 69 70
/* Copied from the process test */
static void get_file_name(char* buf)
{
    char path[MAX_PATH];

    buf[0] = '\0';
    GetTempPathA(sizeof(path), path);
    GetTempFileNameA(path, "wt", 0, buf);
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
typedef struct tag_reg_save_value
{
    const char *name;
    DWORD type;
    BYTE *data;
    DWORD size;
} reg_save_value;

static DWORD save_value(HKEY hkey, const char *value, reg_save_value *saved)
{
    DWORD ret;
    saved->name=value;
    saved->data=0;
    saved->size=0;
    ret=RegQueryValueExA(hkey, value, NULL, &saved->type, NULL, &saved->size);
    if (ret == ERROR_SUCCESS)
    {
        saved->data=HeapAlloc(GetProcessHeap(), 0, saved->size);
        RegQueryValueExA(hkey, value, NULL, &saved->type, saved->data, &saved->size);
    }
    return ret;
}

static void restore_value(HKEY hkey, reg_save_value *saved)
{
    if (saved->data)
    {
        RegSetValueExA(hkey, saved->name, 0, saved->type, saved->data, saved->size);
        HeapFree(GetProcessHeap(), 0, saved->data);
    }
    else
        RegDeleteValueA(hkey, saved->name);
}

105 106 107 108 109 110 111 112 113 114
static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
{
    const char* basename;
    char* event_name;

    basename=strrchr(name, '\\');
    basename=(basename ? basename+1 : name);
    event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);

    sprintf(event_name, "start_%s", basename);
115
    *start_event=CreateEventA(NULL, 0,0, event_name);
116
    sprintf(event_name, "done_%s", basename);
117
    *done_event=CreateEventA(NULL, 0,0, event_name);
118 119 120
    HeapFree(GetProcessHeap(), 0, event_name);
}

121
static void save_blackbox(const char* logfile, void* blackbox, int size, const char *dbgtrace)
122 123 124 125 126 127 128
{
    HANDLE hFile;
    DWORD written;

    hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
    if (hFile == INVALID_HANDLE_VALUE)
        return;
129
    WriteFile(hFile, blackbox, size, &written, NULL);
130 131
    if (dbgtrace && dbgtrace[0])
        WriteFile(hFile, dbgtrace, strlen(dbgtrace), &written, NULL);
132 133 134
    CloseHandle(hFile);
}

135
static int load_blackbox(const char* logfile, void* blackbox, int size)
136 137
{
    HANDLE hFile;
138
    DWORD read;
139
    BOOL ret;
140
    char buf[4096];
141 142 143 144 145 146 147

    hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        ok(0, "unable to open '%s'\n", logfile);
        return 0;
    }
148
    SetLastError(0xdeadbeef);
149
    ret=ReadFile(hFile, blackbox, size, &read, NULL);
150
    ok(ret, "ReadFile failed: %d\n", GetLastError());
151
    ok(read == size, "wrong size for '%s': read=%d\n", logfile, read);
152 153 154 155 156 157
    ret = ReadFile(hFile, buf, sizeof(buf) - 1, &read, NULL);
    if (ret && read)
    {
        buf[read] = 0;
        trace("debugger traces:\n%s", buf);
    }
158
    CloseHandle(hFile);
159
    return 1;
160 161
}

162 163 164 165 166 167 168 169 170 171 172
static DWORD WINAPI thread_proc(void *arg)
{
    Sleep(10000);
    trace("exiting\n");
    ExitThread(1);
}

static void run_background_thread(void)
{
    DWORD tid;
    HANDLE thread = CreateThread(NULL, 0, thread_proc, NULL, 0, &tid);
173
    ok(thread != NULL, "CreateThread failed\n");
174 175 176
    CloseHandle(thread);
}

177 178 179 180 181
typedef struct
{
    DWORD pid;
} crash_blackbox_t;

182 183
static void doCrash(int argc,  char** argv)
{
184
    volatile char* p;
185

186 187 188 189
    /* make sure the exception gets to the debugger */
    SetErrorMode( 0 );
    SetUnhandledExceptionFilter( NULL );

190 191
    run_background_thread();

192
    if (argc >= 4)
193 194 195
    {
        crash_blackbox_t blackbox;
        blackbox.pid=GetCurrentProcessId();
196
        save_blackbox(argv[3], &blackbox, sizeof(blackbox), NULL);
197
    }
198 199 200 201 202 203 204

    /* Just crash */
    trace("child: crashing...\n");
    p=NULL;
    *p=0;
}

205 206 207 208 209 210
typedef struct
{
    int argc;
    DWORD pid;
    BOOL debug_rc;
    DWORD debug_err;
211 212
    BOOL attach_rc;
    DWORD attach_err;
213 214 215 216
    BOOL nokill_rc;
    DWORD nokill_err;
    BOOL detach_rc;
    DWORD detach_err;
217
    DWORD failures;
218 219
} debugger_blackbox_t;

220 221 222 223 224 225 226
struct debugger_context
{
    DWORD pid;
    DEBUG_EVENT ev;
    unsigned process_cnt;
    unsigned dll_cnt;
    void *image_base;
227 228 229 230 231 232 233 234 235 236 237 238 239 240
    DWORD thread_tag;
    unsigned thread_cnt;
    struct wine_rb_tree threads;
    struct debuggee_thread *current_thread;
    struct debuggee_thread *main_thread;
};

struct debuggee_thread
{
    DWORD tid;
    DWORD tag;
    HANDLE handle;
    CONTEXT ctx;
    struct wine_rb_entry entry;
241 242
};

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
int debuggee_thread_compare(const void *key, const struct wine_rb_entry *entry)
{
    struct debuggee_thread *thread = WINE_RB_ENTRY_VALUE(entry, struct debuggee_thread, entry);
    return memcmp(key, &thread->tid, sizeof(thread->tid));
}

static void add_thread(struct debugger_context *ctx, DWORD tid)
{
    struct debuggee_thread *thread;
    if (!ctx->thread_cnt++) wine_rb_init(&ctx->threads, debuggee_thread_compare);
    thread = heap_alloc(sizeof(*thread));
    thread->tid = tid;
    thread->tag = ctx->thread_tag;
    thread->handle = NULL;
    wine_rb_put(&ctx->threads, &tid, &thread->entry);
    if (!ctx->main_thread) ctx->main_thread = thread;
}

static struct debuggee_thread *get_debuggee_thread(struct debugger_context *ctx, DWORD tid)
{
    struct wine_rb_entry *entry = wine_rb_get(&ctx->threads, &tid);
    ok(entry != NULL, "unknown thread %x\n", tid);
    return WINE_RB_ENTRY_VALUE(entry, struct debuggee_thread, entry);
}

static void remove_thread(struct debugger_context *ctx, DWORD tid)
{
    struct debuggee_thread *thread = get_debuggee_thread(ctx, tid);

    wine_rb_remove(&ctx->threads, &thread->entry);
    if (thread->handle) CloseHandle(thread->handle);
    heap_free(thread);
}

static void *get_ip(const CONTEXT *ctx)
{
#ifdef __i386__
    return (void *)ctx->Eip;
#elif defined(__x86_64__)
    return (void *)ctx->Rip;
#else
    return NULL;
#endif
}

#define fetch_thread_context(a) fetch_thread_context_(__LINE__,a)
static void fetch_thread_context_(unsigned line, struct debuggee_thread *thread)
{
    BOOL ret;

    if (!thread->handle)
    {
        thread->handle = OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_QUERY_INFORMATION,
                                    FALSE, thread->tid);
        ok_(__FILE__,line)(thread->handle != NULL, "OpenThread failed: %u\n", GetLastError());
    }

    memset(&thread->ctx, 0xaa, sizeof(thread->ctx));
    thread->ctx.ContextFlags = CONTEXT_FULL;
    ret = GetThreadContext(thread->handle, &thread->ctx);
    ok_(__FILE__,line)(ret, "GetThreadContext failed: %u\n", GetLastError());
}

#define set_thread_context(a,b) set_thread_context_(__LINE__,a,b)
static void set_thread_context_(unsigned line, struct debugger_context *ctx, struct debuggee_thread *thread)
{
    BOOL ret;
    ret = SetThreadContext(thread->handle, &thread->ctx);
    ok_(__FILE__,line)(ret, "SetThreadContext failed: %u\n", GetLastError());
}

314 315 316 317 318 319 320 321 322 323
static void fetch_process_context(struct debugger_context *ctx)
{
    struct debuggee_thread *thread;

    WINE_RB_FOR_EACH_ENTRY(thread, &ctx->threads, struct debuggee_thread, entry)
    {
        fetch_thread_context(thread);
    }
}

324 325 326
#define WAIT_EVENT_TIMEOUT 20000
#define POLL_EVENT_TIMEOUT 200

327 328 329 330 331
#define next_event(a,b) next_event_(__LINE__,a,b)
static void next_event_(unsigned line, struct debugger_context *ctx, unsigned timeout)
{
    BOOL ret;

332 333
    ctx->current_thread = NULL;

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    for (;;)
    {
        if (ctx->process_cnt && ctx->ev.dwDebugEventCode != -1)
        {
            ret = ContinueDebugEvent(ctx->ev.dwProcessId, ctx->ev.dwThreadId, DBG_CONTINUE);
            ok_(__FILE__,line)(ret, "ContinueDebugEvent failed, last error %d.\n", GetLastError());
        }

        ret = WaitForDebugEvent(&ctx->ev, timeout);
        if (!ret)
        {
            ok_(__FILE__,line)(GetLastError() == ERROR_SEM_TIMEOUT,
                               "WaitForDebugEvent failed, last error %d.\n", GetLastError());
            ctx->ev.dwDebugEventCode = -1;
            return;
        }

        if (ctx->ev.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
        {
            if (!ctx->process_cnt) ctx->pid = ctx->ev.dwProcessId;
            ctx->process_cnt++;
        }

        if (ctx->ev.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT) continue; /* ignore for now */
        if (ctx->ev.dwProcessId == ctx->pid) break;

        ok_(__FILE__,line)(ctx->process_cnt > 1, "unexpected event pid\n");
    }

    switch (ctx->ev.dwDebugEventCode)
    {
    case CREATE_PROCESS_DEBUG_EVENT:
366
        add_thread(ctx, ctx->ev.dwThreadId);
367 368
        ctx->image_base = ctx->ev.u.CreateProcessInfo.lpBaseOfImage;
        break;
369 370 371
    case EXIT_PROCESS_DEBUG_EVENT:
        remove_thread(ctx, ctx->ev.dwThreadId);
        return;
372
    case CREATE_THREAD_DEBUG_EVENT:
373
        add_thread(ctx, ctx->ev.dwThreadId);
374 375
        break;
    case EXIT_THREAD_DEBUG_EVENT:
376 377
        remove_thread(ctx, ctx->ev.dwThreadId);
        return;
378 379 380 381 382 383 384 385
    case LOAD_DLL_DEBUG_EVENT:
        ok(ctx->ev.u.LoadDll.lpBaseOfDll != ctx->image_base, "process image reported as DLL load event\n");
        ctx->dll_cnt++;
        break;
    case UNLOAD_DLL_DEBUG_EVENT:
        ctx->dll_cnt--;
        break;
    }
386 387 388 389 390 391 392 393 394 395 396 397 398 399

    ctx->current_thread = get_debuggee_thread(ctx, ctx->ev.dwThreadId);
}

#define wait_for_breakpoint(a) wait_for_breakpoint_(__LINE__,a)
static void wait_for_breakpoint_(unsigned line, struct debugger_context *ctx)
{
    do next_event_(line, ctx, 2000);
    while (ctx->ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT || ctx->ev.dwDebugEventCode == UNLOAD_DLL_DEBUG_EVENT
           || ctx->ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT);

    ok(ctx->ev.dwDebugEventCode == EXCEPTION_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx->ev.dwDebugEventCode);
    ok(ctx->ev.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode = %x\n",
       ctx->ev.u.Exception.ExceptionRecord.ExceptionCode);
400 401 402 403
}

static void process_attach_events(struct debugger_context *ctx)
{
404 405 406
    DEBUG_EVENT ev;
    BOOL ret;

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    ctx->ev.dwDebugEventCode = -1;
    next_event(ctx, 0);
    ok(ctx->ev.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx->ev.dwDebugEventCode);

    next_event(ctx, 0);
    if (ctx->ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT) /* Vista+ reports ntdll.dll before reporting threads */
    {
        ok(ctx->ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx->ev.dwDebugEventCode);
        ok(ctx->ev.u.LoadDll.lpBaseOfDll == ntdll, "The first reported DLL is not ntdll.dll\n");
        next_event(ctx, 0);
    }

    while (ctx->ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT)
        next_event(ctx, 0);

    do
    {
424 425 426 427
        /* even when there are more pending events, they are not reported until current event is continued */
        ret = WaitForDebugEvent(&ev, 10);
        ok(GetLastError() == ERROR_SEM_TIMEOUT, "WaitForDebugEvent returned %x(%u)\n", ret, GetLastError());

428
        next_event(ctx, WAIT_EVENT_TIMEOUT);
429 430 431 432 433 434 435 436 437 438
        if (ctx->ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT)
            ok(ctx->ev.u.LoadDll.lpBaseOfDll != ntdll, "ntdll.dll reported out of order\n");
    } while (ctx->ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT || ctx->ev.dwDebugEventCode == UNLOAD_DLL_DEBUG_EVENT);
    ok(ctx->dll_cnt > 2, "dll_cnt = %d\n", ctx->dll_cnt);

    /* a new thread is created and it executes DbgBreakPoint, which causes the exception */
    ok(ctx->ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx->ev.dwDebugEventCode);
    if (ctx->ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT)
    {
        DWORD last_thread = ctx->ev.dwThreadId;
439
        next_event(ctx, WAIT_EVENT_TIMEOUT);
440 441 442 443 444 445
        ok(ctx->ev.dwThreadId == last_thread, "unexpected thread\n");
    }

    ok(ctx->ev.dwDebugEventCode == EXCEPTION_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx->ev.dwDebugEventCode);
    ok(ctx->ev.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode = %x\n",
       ctx->ev.u.Exception.ExceptionRecord.ExceptionCode);
446
    ok(ctx->ev.u.Exception.ExceptionRecord.ExceptionAddress == pDbgBreakPoint, "ExceptionAddress != DbgBreakPoint\n");
447 448

    /* flush debug events */
449
    do next_event(ctx, POLL_EVENT_TIMEOUT);
450 451 452 453 454
    while (ctx->ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT || ctx->ev.dwDebugEventCode == UNLOAD_DLL_DEBUG_EVENT
           || ctx->ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT || ctx->ev.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT);
    ok(ctx->ev.dwDebugEventCode == -1, "dwDebugEventCode = %d\n", ctx->ev.dwDebugEventCode);
}

455 456 457
static void doDebugger(int argc, char** argv)
{
    const char* logfile;
458
    debugger_blackbox_t blackbox;
459
    HANDLE start_event = 0, done_event = 0, debug_event;
460 461
    char buf[4096] = "";
    struct debugger_context ctx = { 0 };
462

463
    blackbox.argc=argc;
464
    logfile=(argc >= 4 ? argv[3] : NULL);
465
    blackbox.pid=(argc >= 5 ? atol(argv[4]) : 0);
466

467
    blackbox.attach_err=0;
468 469 470 471 472 473 474 475 476
    if (strstr(myARGV[2], "attach"))
    {
        blackbox.attach_rc=DebugActiveProcess(blackbox.pid);
        if (!blackbox.attach_rc)
            blackbox.attach_err=GetLastError();
    }
    else
        blackbox.attach_rc=TRUE;

477 478 479 480 481 482
    if (strstr(myARGV[2], "process"))
    {
        strcat(buf, "processing debug messages\n");
        process_attach_events(&ctx);
    }

483
    debug_event=(argc >= 6 ? (HANDLE)(INT_PTR)atol(argv[5]) : NULL);
484
    blackbox.debug_err=0;
485
    if (debug_event && strstr(myARGV[2], "event"))
486
    {
487
        strcat(buf, "setting event\n");
488 489 490
        blackbox.debug_rc=SetEvent(debug_event);
        if (!blackbox.debug_rc)
            blackbox.debug_err=GetLastError();
491
    }
492 493
    else
        blackbox.debug_rc=TRUE;
494

495 496
    if (strstr(myARGV[2], "process"))
    {
497
        next_event(&ctx, WAIT_EVENT_TIMEOUT);
498 499 500 501 502
        ok(ctx.ev.dwDebugEventCode == EXCEPTION_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
        ok(ctx.ev.u.Exception.ExceptionRecord.ExceptionCode == STATUS_ACCESS_VIOLATION, "ExceptionCode = %x\n",
           ctx.ev.u.Exception.ExceptionRecord.ExceptionCode);
    }

503 504 505 506 507
    if (logfile)
    {
        get_events(logfile, &start_event, &done_event);
    }

508
    if (strstr(myARGV[2], "order"))
509
    {
510
        strcat(buf, "waiting for the start signal...\n");
511 512 513
        WaitForSingleObject(start_event, INFINITE);
    }

514
    blackbox.nokill_err=0;
515 516 517 518 519 520 521 522 523
    if (strstr(myARGV[2], "nokill"))
    {
        blackbox.nokill_rc=pDebugSetProcessKillOnExit(FALSE);
        if (!blackbox.nokill_rc)
            blackbox.nokill_err=GetLastError();
    }
    else
        blackbox.nokill_rc=TRUE;

524
    blackbox.detach_err=0;
525 526 527 528 529 530 531 532 533
    if (strstr(myARGV[2], "detach"))
    {
        blackbox.detach_rc=pDebugActiveProcessStop(blackbox.pid);
        if (!blackbox.detach_rc)
            blackbox.detach_err=GetLastError();
    }
    else
        blackbox.detach_rc=TRUE;

534 535 536 537 538 539 540 541 542
    if (debug_event && strstr(myARGV[2], "late"))
    {
        strcat(buf, "setting event\n");
        blackbox.debug_rc=SetEvent(debug_event);
        if (!blackbox.debug_rc)
            blackbox.debug_err=GetLastError();
    }

    strcat(buf, "done debugging...\n");
543 544
    if (logfile)
    {
545 546
        blackbox.failures = winetest_get_failures();
        save_blackbox(logfile, &blackbox, sizeof(blackbox), buf);
547
    }
548

549
    SetEvent(done_event);
550 551 552 553 554

    /* Just exit with a known value */
    ExitProcess(0xdeadbeef);
}

555
static void crash_and_debug(HKEY hkey, const char* argv0, const char* dbgtasks)
556
{
557
    static BOOL skip_crash_and_debug = FALSE;
558
    BOOL bRet;
559 560 561 562 563 564 565 566
    DWORD ret;
    HANDLE start_event, done_event;
    char* cmd;
    char dbglog[MAX_PATH];
    char childlog[MAX_PATH];
    PROCESS_INFORMATION	info;
    STARTUPINFOA startup;
    DWORD exit_code;
567 568
    crash_blackbox_t crash_blackbox;
    debugger_blackbox_t dbg_blackbox;
569 570 571 572 573 574 575
    DWORD wait_code;

    if (skip_crash_and_debug)
    {
        win_skip("Skipping crash_and_debug\n");
        return;
    }
576 577

    ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
578 579 580 581 582 583 584
    if (ret == ERROR_ACCESS_DENIED)
    {
        skip_crash_and_debug = TRUE;
        skip("No write access to change the debugger\n");
        return;
    }

585 586 587 588
    ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);

    get_file_name(dbglog);
    get_events(dbglog, &start_event, &done_event);
589 590
    cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+2+34+1);
    sprintf(cmd, "%s debugger %s \"%s\" %%ld %%ld", argv0, dbgtasks, dbglog);
591 592 593 594 595
    ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
    ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
    HeapFree(GetProcessHeap(), 0, cmd);

    get_file_name(childlog);
596 597
    cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+2+1);
    sprintf(cmd, "%s debugger crash \"%s\"", argv0, childlog);
598

599
    trace("running %s...\n", dbgtasks);
600 601 602 603 604 605 606 607 608 609 610
    memset(&startup, 0, sizeof(startup));
    startup.cb = sizeof(startup);
    startup.dwFlags = STARTF_USESHOWWINDOW;
    startup.wShowWindow = SW_SHOWNORMAL;
    ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
    ok(ret, "CreateProcess: err=%d\n", GetLastError());
    HeapFree(GetProcessHeap(), 0, cmd);
    CloseHandle(info.hThread);

    /* The process exits... */
    trace("waiting for child exit...\n");
611 612 613 614 615 616 617 618 619
    wait_code = WaitForSingleObject(info.hProcess, 30000);
#if defined(_WIN64) && defined(__MINGW32__)
    /* Mingw x64 doesn't output proper unwind info */
    skip_crash_and_debug = broken(wait_code == WAIT_TIMEOUT);
    if (skip_crash_and_debug)
    {
        TerminateProcess(info.hProcess, WAIT_TIMEOUT);
        WaitForSingleObject(info.hProcess, 5000);
        CloseHandle(info.hProcess);
620 621
        DeleteFileA(dbglog);
        DeleteFileA(childlog);
622 623 624 625 626
        win_skip("Giving up on child process\n");
        return;
    }
#endif
    ok(wait_code == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
627 628
    bRet = GetExitCodeProcess(info.hProcess, &exit_code);
    ok(bRet, "GetExitCodeProcess failed: err=%d\n", GetLastError());
629 630 631 632 633
    if (strstr(dbgtasks, "code2"))
    {
        /* If, after attaching to the debuggee, the debugger exits without
         * detaching, then the debuggee gets a special exit code.
         */
634
        ok(exit_code == STATUS_DEBUGGER_INACTIVE ||
635
           broken(exit_code == STATUS_ACCESS_VIOLATION) || /* Intermittent Vista+ */
636
           broken(exit_code == WAIT_ABANDONED), /* NT4, W2K */
637 638 639
           "wrong exit code : %08x\n", exit_code);
    }
    else
640
        ok(exit_code == STATUS_ACCESS_VIOLATION ||
641
           broken(exit_code == WAIT_ABANDONED), /* NT4, W2K, W2K3 */
642
           "wrong exit code : %08x\n", exit_code);
643 644 645
    CloseHandle(info.hProcess);

    /* ...before the debugger */
646
    if (strstr(dbgtasks, "order"))
647 648 649
        ok(SetEvent(start_event), "SetEvent(start_event) failed\n");

    trace("waiting for the debugger...\n");
650 651 652 653 654 655
    wait_code = WaitForSingleObject(done_event, 5000);
#if defined(_WIN64) && defined(__MINGW32__)
    /* Mingw x64 doesn't output proper unwind info */
    skip_crash_and_debug = broken(wait_code == WAIT_TIMEOUT);
    if (skip_crash_and_debug)
    {
656 657
        DeleteFileA(dbglog);
        DeleteFileA(childlog);
658 659 660 661 662
        win_skip("Giving up on debugger\n");
        return;
    }
#endif
    ok(wait_code == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
663

664 665
    ok(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)), "failed to open: %s\n", childlog);
    ok(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)), "failed to open: %s\n", dbglog);
666 667 668 669

    ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
    ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
    ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
670
    ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
671 672
    ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
    ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
673
    ok(!dbg_blackbox.failures, "debugger reported %u failures\n", dbg_blackbox.failures);
674

675 676
    DeleteFileA(dbglog);
    DeleteFileA(childlog);
677 678
}

679 680
static void crash_and_winedbg(HKEY hkey, const char* argv0)
{
681
    BOOL bRet;
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    DWORD ret;
    char* cmd;
    PROCESS_INFORMATION	info;
    STARTUPINFOA startup;
    DWORD exit_code;

    ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
    ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);

    cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
    sprintf(cmd, "%s debugger crash", argv0);

    memset(&startup, 0, sizeof(startup));
    startup.cb = sizeof(startup);
    startup.dwFlags = STARTF_USESHOWWINDOW;
    startup.wShowWindow = SW_SHOWNORMAL;
    ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
    ok(ret, "CreateProcess: err=%d\n", GetLastError());
    HeapFree(GetProcessHeap(), 0, cmd);
    CloseHandle(info.hThread);

    trace("waiting for child exit...\n");
    ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
705 706
    bRet = GetExitCodeProcess(info.hProcess, &exit_code);
    ok(bRet, "GetExitCodeProcess failed: err=%d\n", GetLastError());
707
    ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
708 709 710
    CloseHandle(info.hProcess);
}

711 712 713
static void test_ExitCode(void)
{
    static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
714
    static const char* WineDbg="Software\\Wine\\WineDbg";
715 716 717 718
    char test_exe[MAX_PATH];
    DWORD ret;
    HKEY hkey;
    DWORD disposition;
719 720
    reg_save_value auto_value;
    reg_save_value debugger_value;
721

722 723
    GetModuleFileNameA(GetModuleHandleA(NULL), test_exe, sizeof(test_exe));
    if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
724 725 726 727 728 729 730 731 732 733
        strcat(test_exe, ".so");
    if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
    {
        ok(0, "could not find the test executable '%s'\n", test_exe);
        return;
    }

    ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
    if (ret == ERROR_SUCCESS)
    {
734 735 736
        save_value(hkey, "auto", &auto_value);
        save_value(hkey, "debugger", &debugger_value);
        trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug, debugger_value.data);
737 738 739 740 741 742 743 744 745 746 747
    }
    else if (ret == ERROR_ACCESS_DENIED)
    {
        skip("not enough privileges to change the debugger\n");
        return;
    }
    else if (ret != ERROR_FILE_NOT_FOUND)
    {
        ok(0, "could not open the AeDebug key: %d\n", ret);
        return;
    }
748
    else debugger_value.data = NULL;
749

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    if (debugger_value.data && debugger_value.type == REG_SZ &&
        strstr((char*)debugger_value.data, "winedbg --auto"))
    {
        HKEY hkeyWinedbg;
        ret=RegCreateKeyA(HKEY_CURRENT_USER, WineDbg, &hkeyWinedbg);
        if (ret == ERROR_SUCCESS)
        {
            static DWORD zero;
            reg_save_value crash_dlg_value;
            save_value(hkeyWinedbg, "ShowCrashDialog", &crash_dlg_value);
            RegSetValueExA(hkeyWinedbg, "ShowCrashDialog", 0, REG_DWORD, (BYTE *)&zero, sizeof(DWORD));
            crash_and_winedbg(hkey, test_exe);
            restore_value(hkeyWinedbg, &crash_dlg_value);
            RegCloseKey(hkeyWinedbg);
        }
        else
            ok(0, "Couldn't access WineDbg Key - error %u\n", ret);
    }
768

769 770 771 772 773 774 775
    if (winetest_interactive)
        /* Since the debugging process never sets the debug event, it isn't recognized
           as a valid debugger and, after the debugger exits, Windows will show a dialog box
           asking the user what to do */
        crash_and_debug(hkey, test_exe, "dbg,none");
    else
        skip("\"none\" debugger test needs user interaction\n");
776 777
    ok(disposition == REG_OPENED_EXISTING_KEY, "expected REG_OPENED_EXISTING_KEY, got %d\n", disposition);
    crash_and_debug(hkey, test_exe, "dbg,event,order");
778
    crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
779 780
    if (pDebugSetProcessKillOnExit)
        crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
781 782
    else
        win_skip("DebugSetProcessKillOnExit is not available\n");
783
    if (pDebugActiveProcessStop)
784
    {
785
        crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
786 787
        crash_and_debug(hkey, test_exe, "dbg,attach,detach,late");
    }
788 789
    else
        win_skip("DebugActiveProcessStop is not available\n");
790
    crash_and_debug(hkey, test_exe, "dbg,attach,process,event,detach");
791 792 793 794 795 796 797 798

    if (disposition == REG_CREATED_NEW_KEY)
    {
        RegCloseKey(hkey);
        RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
    }
    else
    {
799 800
        restore_value(hkey, &auto_value);
        restore_value(hkey, &debugger_value);
801 802 803 804
        RegCloseKey(hkey);
    }
}

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static void test_RemoteDebugger(void)
{
    BOOL bret, present;
    if(!pCheckRemoteDebuggerPresent)
    {
        win_skip("CheckRemoteDebuggerPresent is not available\n");
        return;
    }
    present = TRUE;
    SetLastError(0xdeadbeef);
    bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present);
    ok(bret , "expected CheckRemoteDebuggerPresent to succeed\n");
    ok(0xdeadbeef == GetLastError(),
       "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());

    present = TRUE;
    SetLastError(0xdeadbeef);
    bret = pCheckRemoteDebuggerPresent(NULL,&present);
    ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
    ok(present, "expected parameter to be unchanged\n");
    ok(ERROR_INVALID_PARAMETER == GetLastError(),
       "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());

    SetLastError(0xdeadbeef);
    bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL);
    ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
    ok(ERROR_INVALID_PARAMETER == GetLastError(),
       "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
}

835 836 837 838 839 840 841 842 843 844 845
struct child_blackbox
{
    LONG failures;
};

static void doChild(int argc, char **argv)
{
    struct child_blackbox blackbox;
    const char *blackbox_file;
    HANDLE parent;
    DWORD ppid;
846
    BOOL debug;
847 848 849 850 851 852 853 854
    BOOL ret;

    blackbox_file = argv[4];
    sscanf(argv[3], "%08x", &ppid);

    parent = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ppid);
    child_ok(!!parent, "OpenProcess failed, last error %#x.\n", GetLastError());

855 856 857 858
    ret = pCheckRemoteDebuggerPresent(parent, &debug);
    child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
    child_ok(!debug, "Expected debug == 0, got %#x.\n", debug);

859 860 861
    ret = DebugActiveProcess(ppid);
    child_ok(ret, "DebugActiveProcess failed, last error %#x.\n", GetLastError());

862 863 864 865
    ret = pCheckRemoteDebuggerPresent(parent, &debug);
    child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
    child_ok(debug, "Expected debug != 0, got %#x.\n", debug);

866 867 868
    ret = pDebugActiveProcessStop(ppid);
    child_ok(ret, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());

869 870 871 872
    ret = pCheckRemoteDebuggerPresent(parent, &debug);
    child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
    child_ok(!debug, "Expected debug == 0, got %#x.\n", debug);

873 874 875
    ret = CloseHandle(parent);
    child_ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());

876
    ret = pIsDebuggerPresent();
877 878 879 880 881
    child_ok(ret, "Expected ret != 0, got %#x.\n", ret);
    ret = pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug);
    child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
    child_ok(debug, "Expected debug != 0, got %#x.\n", debug);

882
    NtCurrentTeb()->Peb->BeingDebugged = FALSE;
883

884 885 886 887 888
    ret = pIsDebuggerPresent();
    child_ok(!ret, "Expected ret != 0, got %#x.\n", ret);
    ret = pCheckRemoteDebuggerPresent(GetCurrentProcess(), &debug);
    child_ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
    child_ok(debug, "Expected debug != 0, got %#x.\n", debug);
889

890
    NtCurrentTeb()->Peb->BeingDebugged = TRUE;
891

892
    blackbox.failures = child_failures;
893
    save_blackbox(blackbox_file, &blackbox, sizeof(blackbox), NULL);
894 895 896 897 898 899 900 901 902
}

static void test_debug_loop(int argc, char **argv)
{
    const char *arguments = " debugger child ";
    struct child_blackbox blackbox;
    char blackbox_file[MAX_PATH];
    PROCESS_INFORMATION pi;
    STARTUPINFOA si;
903
    BOOL debug;
904 905 906 907
    DWORD pid;
    char *cmd;
    BOOL ret;

908
    if (!pDebugActiveProcessStop || !pCheckRemoteDebuggerPresent)
909
    {
910
        win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
911 912 913 914
        return;
    }

    pid = GetCurrentProcessId();
915 916 917
    ret = DebugActiveProcess(pid);
    ok(!ret, "DebugActiveProcess() succeeded on own process.\n");

918
    get_file_name(blackbox_file);
919 920
    cmd = HeapAlloc(GetProcessHeap(), 0, strlen(argv[0]) + strlen(arguments) + strlen(blackbox_file) + 2 + 10);
    sprintf(cmd, "%s%s%08x \"%s\"", argv[0], arguments, pid, blackbox_file);
921 922 923 924 925 926 927 928

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    ret = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
    ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());

    HeapFree(GetProcessHeap(), 0, cmd);

929 930 931 932
    ret = pCheckRemoteDebuggerPresent(pi.hProcess, &debug);
    ok(ret, "CheckRemoteDebuggerPresent failed, last error %#x.\n", GetLastError());
    ok(debug, "Expected debug != 0, got %#x.\n", debug);

933 934 935 936 937 938 939 940 941
    for (;;)
    {
        DEBUG_EVENT ev;

        ret = WaitForDebugEvent(&ev, INFINITE);
        ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
        if (!ret) break;

        if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
942 943 944 945 946 947 948 949 950
#if defined(__i386__) || defined(__x86_64__)
        if (ev.dwDebugEventCode == EXCEPTION_DEBUG_EVENT &&
            ev.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
        {
            BYTE byte = 0;
            NtReadVirtualMemory(pi.hProcess, ev.u.Exception.ExceptionRecord.ExceptionAddress, &byte, 1, NULL);
            ok(byte == 0xcc, "got %02x\n", byte);
        }
#endif
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
        ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
        ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
        if (!ret) break;
    }

    ret = CloseHandle(pi.hThread);
    ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
    ret = CloseHandle(pi.hProcess);
    ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());

    load_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
    ok(!blackbox.failures, "Got %d failures from child process.\n", blackbox.failures);

    ret = DeleteFileA(blackbox_file);
    ok(ret, "DeleteFileA failed, last error %#x.\n", GetLastError());
}

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
static void doChildren(int argc, char **argv)
{
    const char *arguments = "debugger children last";
    struct child_blackbox blackbox;
    const char *blackbox_file, *p;
    char event_name[MAX_PATH];
    PROCESS_INFORMATION pi;
    STARTUPINFOA si;
    HANDLE event;
    char *cmd;
    BOOL ret;

    if (!strcmp(argv[3], "last")) return;

    blackbox_file = argv[3];

984 985
    run_background_thread();

986 987 988 989
    p = strrchr(blackbox_file, '\\');
    p = p ? p+1 : blackbox_file;
    strcpy(event_name, p);
    strcat(event_name, "_init");
990
    event = OpenEventA(EVENT_ALL_ACCESS, FALSE, event_name);
991 992 993 994 995 996 997 998
    child_ok(event != NULL, "OpenEvent failed, last error %d.\n", GetLastError());
    SetEvent(event);
    CloseHandle(event);

    p = strrchr(blackbox_file, '\\');
    p = p ? p+1 : blackbox_file;
    strcpy(event_name, p);
    strcat(event_name, "_attach");
999
    event = OpenEventA(EVENT_ALL_ACCESS, FALSE, event_name);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    child_ok(event != NULL, "OpenEvent failed, last error %d.\n", GetLastError());
    WaitForSingleObject(event, INFINITE);
    CloseHandle(event);

    cmd = HeapAlloc(GetProcessHeap(), 0, strlen(argv[0]) + strlen(arguments) + 2);
    sprintf(cmd, "%s %s", argv[0], arguments);

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    ret = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    child_ok(ret, "CreateProcess failed, last error %d.\n", GetLastError());

    child_ok(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0,
            "Timed out waiting for the child to exit\n");

    ret = CloseHandle(pi.hThread);
    child_ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());
    ret = CloseHandle(pi.hProcess);
    child_ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());

    blackbox.failures = child_failures;
1021
    save_blackbox(blackbox_file, &blackbox, sizeof(blackbox), NULL);
1022 1023

    HeapFree(GetProcessHeap(), 0, cmd);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
}

static void test_debug_children(char *name, DWORD flag, BOOL debug_child)
{
    const char *arguments = "debugger children";
    struct child_blackbox blackbox;
    char blackbox_file[MAX_PATH], *p;
    char event_name[MAX_PATH];
    PROCESS_INFORMATION pi;
    STARTUPINFOA si;
    HANDLE event_init, event_attach;
    char *cmd;
    BOOL debug, ret;
1037
    struct debugger_context ctx = { 0 };
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

    if (!pDebugActiveProcessStop || !pCheckRemoteDebuggerPresent)
    {
        win_skip("DebugActiveProcessStop or CheckRemoteDebuggerPresent not available, skipping test.\n");
        return;
    }

    get_file_name(blackbox_file);
    cmd = HeapAlloc(GetProcessHeap(), 0, strlen(name) + strlen(arguments) + strlen(blackbox_file) + 5);
    sprintf(cmd, "%s %s \"%s\"", name, arguments, blackbox_file);

    p = strrchr(blackbox_file, '\\');
    p = p ? p+1 : blackbox_file;
    strcpy(event_name, p);
    strcat(event_name, "_init");
1053
    event_init = CreateEventA(NULL, FALSE, FALSE, event_name);
1054 1055 1056 1057 1058 1059
    ok(event_init != NULL, "OpenEvent failed, last error %d.\n", GetLastError());

    p = strrchr(blackbox_file, '\\');
    p = p ? p+1 : blackbox_file;
    strcpy(event_name, p);
    strcat(event_name, "_attach");
1060
    event_attach = CreateEventA(NULL, FALSE, flag!=0, event_name);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    ok(event_attach != NULL, "CreateEvent failed, last error %d.\n", GetLastError());

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);

    ret = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, flag, NULL, NULL, &si, &pi);
    ok(ret, "CreateProcess failed, last error %d.\n", GetLastError());
    HeapFree(GetProcessHeap(), 0, cmd);
    if (!flag)
    {
        WaitForSingleObject(event_init, INFINITE);
1072
        Sleep(100);
1073 1074 1075 1076 1077 1078 1079 1080
        ret = DebugActiveProcess(pi.dwProcessId);
        ok(ret, "DebugActiveProcess failed, last error %d.\n", GetLastError());
    }

    ret = pCheckRemoteDebuggerPresent(pi.hProcess, &debug);
    ok(ret, "CheckRemoteDebuggerPresent failed, last error %d.\n", GetLastError());
    ok(debug, "Expected debug != 0, got %x.\n", debug);

1081 1082 1083
    trace("starting debugger loop\n");

    if (flag)
1084
    {
1085
        next_event(&ctx, WAIT_EVENT_TIMEOUT);
1086 1087
        ok(ctx.ev.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
        ok(ctx.pid == pi.dwProcessId, "unexpected dwProcessId %x\n", ctx.ev.dwProcessId == ctx.pid);
1088

1089
        next_event(&ctx, WAIT_EVENT_TIMEOUT);
1090
        ok(ctx.ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
1091

1092
        wait_for_breakpoint(&ctx);
1093
        ok(ctx.dll_cnt > 2, "dll_cnt = %d\n", ctx.dll_cnt);
1094
    }
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    else
    {
        DWORD last_thread;

        process_attach_events(&ctx);
        ok(ctx.pid == pi.dwProcessId, "unexpected dwProcessId %x\n", ctx.pid);

        ret = DebugBreakProcess(pi.hProcess);
        ok(ret, "BreakProcess failed: %u\n", GetLastError());

1105 1106
        /* a new thread, which executes DbgBreakPoint, is created */
        next_event(&ctx, WAIT_EVENT_TIMEOUT);
1107 1108 1109 1110
        ok(ctx.ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
        last_thread = ctx.ev.dwThreadId;

        if (ctx.ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT)
1111
            next_event(&ctx, WAIT_EVENT_TIMEOUT);
1112 1113 1114 1115 1116

        ok(ctx.ev.dwDebugEventCode == EXCEPTION_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
        ok(ctx.ev.dwThreadId == last_thread, "unexpected thread\n");
        ok(ctx.ev.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode = %x\n",
           ctx.ev.u.Exception.ExceptionRecord.ExceptionCode);
1117
        ok(ctx.ev.u.Exception.ExceptionRecord.ExceptionAddress == pDbgBreakPoint, "ExceptionAddress != DbgBreakPoint\n");
1118 1119 1120 1121 1122

        ret = SetEvent(event_attach);
        ok(ret, "SetEvent failed, last error %d.\n", GetLastError());
    }

1123
    do next_event(&ctx, WAIT_EVENT_TIMEOUT);
1124 1125 1126 1127 1128 1129 1130
    while (ctx.ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT || ctx.ev.dwDebugEventCode == UNLOAD_DLL_DEBUG_EVENT
           || ctx.ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT || ctx.ev.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT);

    ok(ctx.ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
    ret = ContinueDebugEvent(ctx.ev.dwProcessId, ctx.ev.dwThreadId, DBG_CONTINUE);
    ok(ret, "ContinueDebugEvent failed, last error %d.\n", GetLastError());

1131
    if(debug_child)
1132
        ok(ctx.process_cnt == 2, "didn't get any child events (flag: %x).\n", flag);
1133
    else
1134
        ok(ctx.process_cnt == 1, "got child event (flag: %x).\n", flag);
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
    CloseHandle(event_init);
    CloseHandle(event_attach);

    ret = CloseHandle(pi.hThread);
    ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());
    ret = CloseHandle(pi.hProcess);
    ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());

    load_blackbox(blackbox_file, &blackbox, sizeof(blackbox));
    ok(!blackbox.failures, "Got %d failures from child process.\n", blackbox.failures);

    ret = DeleteFileA(blackbox_file);
    ok(ret, "DeleteFileA failed, last error %d.\n", GetLastError());
}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
static void wait_debugger(HANDLE event)
{
    WaitForSingleObject(event, INFINITE);
    ExitProcess(0);
}

#define expect_event(a,b) expect_event_(__LINE__,a,b)
static void expect_event_(unsigned line, struct debugger_context *ctx, DWORD event_code)
{
    next_event(ctx, WAIT_EVENT_TIMEOUT);
    ok_(__FILE__,line)(ctx->ev.dwDebugEventCode == event_code, "dwDebugEventCode = %d expected %d\n",
                       ctx->ev.dwDebugEventCode, event_code);
}

#define expect_exception(a,b) expect_exception_(__LINE__,a,b)
static void expect_exception_(unsigned line, struct debugger_context *ctx, DWORD exception_code)
{
    expect_event_(line, ctx, EXCEPTION_DEBUG_EVENT);
    ok_(__FILE__,line)(ctx->ev.u.Exception.ExceptionRecord.ExceptionCode == exception_code, "ExceptionCode = %x expected %x\n",
                       ctx->ev.u.Exception.ExceptionRecord.ExceptionCode, exception_code);
}

#define expect_breakpoint_exception(a,b) expect_breakpoint_exception_(__LINE__,a,b)
static void expect_breakpoint_exception_(unsigned line, struct debugger_context *ctx, const void *expect_addr)
{
    struct debuggee_thread *thread;
    expect_exception_(line, ctx, EXCEPTION_BREAKPOINT);
    if (!expect_addr) return;
    ok_(__FILE__,line)(ctx->ev.u.Exception.ExceptionRecord.ExceptionAddress == expect_addr,
                       "ExceptionAddress = %p expected %p\n", ctx->ev.u.Exception.ExceptionRecord.ExceptionAddress, expect_addr);
    thread = get_debuggee_thread(ctx, ctx->ev.dwThreadId);
    fetch_thread_context(thread);
    ok_(__FILE__,line)(get_ip(&thread->ctx) == (char*)expect_addr + 1, "unexpected instruction pointer %p expected %p\n",
                       get_ip(&thread->ctx), expect_addr);
}

#define single_step(a,b,c) single_step_(__LINE__,a,b,c)
static void single_step_(unsigned line, struct debugger_context *ctx, struct debuggee_thread *thread, void *expect_addr)
{
#if defined(__i386__) || defined(__x86_64__)
    fetch_thread_context(thread);
    thread->ctx.EFlags |= 0x100;
    set_thread_context(ctx, thread);
    expect_exception_(line, ctx, EXCEPTION_SINGLE_STEP);
    ok_(__FILE__,line)(ctx->ev.u.Exception.ExceptionRecord.ExceptionAddress == expect_addr,
                       "ExceptionAddress = %p expected %p\n", ctx->ev.u.Exception.ExceptionRecord.ExceptionAddress, expect_addr);
    fetch_thread_context(thread);
    ok_(__FILE__,line)(get_ip(&thread->ctx) == expect_addr, "unexpected instruction pointer %p expected %p\n",
                       get_ip(&thread->ctx), expect_addr);
    ok_(__FILE__,line)(!(thread->ctx.EFlags & 0x100), "EFlags = %x\n", thread->ctx.EFlags);
#endif
}

static const BYTE loop_code[] = {
#if defined(__i386__) || defined(__x86_64__)
    0x90,                         /* nop */
    0x90,                         /* nop */
    0x90,                         /* nop */
    0xe9, 0xf8, 0xff, 0xff, 0xff  /* jmp $-8 */
#endif
};

static const BYTE call_debug_service_code[] = {
#ifdef __i386__
    0x53,                         /* pushl %ebx */
    0x57,                         /* pushl %edi */
    0x8b, 0x44, 0x24, 0x0c,       /* movl 12(%esp),%eax */
    0xb9, 0x11, 0x11, 0x11, 0x11, /* movl $0x11111111,%ecx */
    0xba, 0x22, 0x22, 0x22, 0x22, /* movl $0x22222222,%edx */
    0xbb, 0x33, 0x33, 0x33, 0x33, /* movl $0x33333333,%ebx */
    0xbf, 0x44, 0x44, 0x44, 0x44, /* movl $0x44444444,%edi */
    0xcd, 0x2d,                   /* int $0x2d */
    0xeb,                         /* jmp $+17 */
    0x0f, 0x1f, 0x00,             /* nop */
    0x31, 0xc0,                   /* xorl %eax,%eax */
    0xeb, 0x0c,                   /* jmp $+14 */
    0x90, 0x90, 0x90, 0x90,       /* nop */
    0x90, 0x90, 0x90, 0x90,
    0x90,
    0x31, 0xc0,                   /* xorl %eax,%eax */
    0x40,                         /* incl %eax */
    0x5f,                         /* popl %edi */
    0x5b,                         /* popl %ebx */
    0xc3,                         /* ret */
#elif defined(__x86_64__)
    0x53,                         /* push %rbx */
    0x57,                         /* push %rdi */
    0x48, 0x89, 0xc8,             /* movl %rcx,%rax */
    0x48, 0xb9, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, /* movabs $0x1111111111111111,%rcx */
    0x48, 0xba, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* movabs $0x2222222222222222,%rdx */
    0x48, 0xbb, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, /* movabs $0x3333333333333333,%rbx */
    0x48, 0xbf, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, /* movabs $0x4444444444444444,%rdi */
    0xcd, 0x2d,                   /* int $0x2d */
    0xeb,                         /* jmp $+17 */
    0x0f, 0x1f, 0x00,             /* nop */
    0x48, 0x31, 0xc0,             /* xor %rax,%rax */
    0xeb, 0x0e,                   /* jmp $+16 */
    0x90, 0x90, 0x90, 0x90,       /* nop */
    0x90, 0x90, 0x90, 0x90,
    0x48, 0x31, 0xc0,             /* xor %rax,%rax */
    0x48, 0xff, 0xc0,             /* inc %rax */
    0x5f,                         /* pop %rdi */
    0x5b,                         /* pop %rbx */
    0xc3,                         /* ret */
#endif
};

#if defined(__i386__) || defined(__x86_64__)
#define OP_BP 0xcc
#else
#define OP_BP 0
#endif

static void test_debugger(const char *argv0)
{
    static const char arguments[] = " debugger wait ";
    SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE };
    struct debugger_context ctx = { 0 };
    PROCESS_INFORMATION pi;
    STARTUPINFOA si;
    HANDLE event, thread;
    BYTE *mem, buf[4096], *proc_code, *thread_proc, byte;
1272
    unsigned int i, worker_cnt, exception_cnt;
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    struct debuggee_thread *debuggee_thread;
    char *cmd;
    BOOL ret;

    event = CreateEventW(&sa, TRUE, FALSE, NULL);
    ok(event != NULL, "CreateEvent failed: %u\n", GetLastError());

    cmd = heap_alloc(strlen(argv0) + strlen(arguments) + 16);
    sprintf(cmd, "%s%s%x\n", argv0, arguments, (DWORD)(DWORD_PTR)event);

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    ret = CreateProcessA(NULL, cmd, NULL, NULL, TRUE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
    ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
    heap_free(cmd);

    next_event(&ctx, WAIT_EVENT_TIMEOUT);
    ok(ctx.ev.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
    wait_for_breakpoint(&ctx);
    do next_event(&ctx, POLL_EVENT_TIMEOUT);
    while(ctx.ev.dwDebugEventCode != -1);

    mem = VirtualAllocEx(pi.hProcess, NULL, sizeof(buf), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    ok(mem != NULL, "VirtualAllocEx failed: %u\n", GetLastError());
    proc_code   = buf + 1024;
    thread_proc = mem + 1024;

    if (sizeof(loop_code) > 1)
    {
        /* test single-step exceptions */
        memset(buf, OP_BP, sizeof(buf));
        memcpy(proc_code, &loop_code, sizeof(loop_code));
        proc_code[0] = OP_BP; /* set a breakpoint */
        ret = WriteProcessMemory(pi.hProcess, mem, buf, sizeof(buf), NULL);
        ok(ret, "WriteProcessMemory failed: %u\n", GetLastError());

        thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void*)thread_proc, NULL, 0, NULL);
        ok(thread != NULL, "CreateRemoteThread failed: %u\n", GetLastError());

        expect_event(&ctx, CREATE_THREAD_DEBUG_EVENT);
        debuggee_thread = get_debuggee_thread(&ctx, ctx.ev.dwThreadId);

        wait_for_breakpoint(&ctx);
        fetch_thread_context(debuggee_thread);
        ok(ctx.ev.u.Exception.ExceptionRecord.ExceptionAddress == thread_proc,
           "ExceptionAddress = %p\n", ctx.ev.u.Exception.ExceptionRecord.ExceptionAddress);
        ok(get_ip(&debuggee_thread->ctx) == thread_proc + 1, "unexpected instruction pointer %p\n",
           get_ip(&debuggee_thread->ctx));

        single_step(&ctx, debuggee_thread, thread_proc + 2);
        single_step(&ctx, debuggee_thread, thread_proc + 3);
        single_step(&ctx, debuggee_thread, thread_proc);

        byte = 0xc3; /* ret */
        ret = WriteProcessMemory(pi.hProcess, thread_proc, &byte, 1, NULL);
        ok(ret, "WriteProcessMemory failed: %u\n", GetLastError());

        expect_event(&ctx, EXIT_THREAD_DEBUG_EVENT);
    }
    else win_skip("loop_code not supported on this architecture\n");

    if (sizeof(call_debug_service_code) > 1)
    {
        /* test debug service exceptions */
        memset(buf, OP_BP, sizeof(buf));
        memcpy(proc_code, call_debug_service_code, sizeof(call_debug_service_code));
        ret = WriteProcessMemory(pi.hProcess, mem, buf, sizeof(buf), NULL);
        ok(ret, "WriteProcessMemory failed: %u\n", GetLastError());

        /* BREAKPOINT_PRINT */
        thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void*)thread_proc, (void*)2, 0, NULL);
        ok(thread != NULL, "CreateRemoteThread failed: %u\n", GetLastError());
        expect_event(&ctx, CREATE_THREAD_DEBUG_EVENT);
        expect_breakpoint_exception(&ctx, NULL);
        expect_event(&ctx, EXIT_THREAD_DEBUG_EVENT);

        /* BREAKPOINT_PROMPT */
        thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void*)thread_proc, (void*)1, 0, NULL);
        ok(thread != NULL, "CreateRemoteThread failed: %u\n", GetLastError());
        expect_event(&ctx, CREATE_THREAD_DEBUG_EVENT);
        next_event(&ctx, WAIT_EVENT_TIMEOUT);
        /* some 32-bit Windows versions report exception to the debugger */
        if (sizeof(void *) == 4 && ctx.ev.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) next_event(&ctx, WAIT_EVENT_TIMEOUT);
        ok(ctx.ev.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT, "unexpected debug event %u\n", ctx.ev.dwDebugEventCode);
    }
    else win_skip("call_debug_service_code not supported on this architecture\n");

1360
    if (sizeof(loop_code) > 1)
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    {
        memset(buf, OP_BP, sizeof(buf));
        memcpy(proc_code, &loop_code, sizeof(loop_code));
        ret = WriteProcessMemory(pi.hProcess, mem, buf, sizeof(buf), NULL);
        ok(ret, "WriteProcessMemory failed: %u\n", GetLastError());

        ctx.thread_tag = 1;

        worker_cnt = 20;
        for (i = 0; i < worker_cnt; i++)
        {
            thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void*)thread_proc, NULL, 0, NULL);
            ok(thread != NULL, "CreateRemoteThread failed: %u\n", GetLastError());

            next_event(&ctx, 20000);
            ok(ctx.ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT, "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);

            ret = CloseHandle(thread);
            ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());
        }

        byte = OP_BP;
        ret = WriteProcessMemory(pi.hProcess, thread_proc + 1, &byte, 1, NULL);
        ok(ret, "WriteProcessMemory failed: %u\n", GetLastError());

        expect_breakpoint_exception(&ctx, thread_proc + 1);
        exception_cnt = 1;

        debuggee_thread = ctx.current_thread;
        fetch_process_context(&ctx);
        ok(get_ip(&ctx.current_thread->ctx) == thread_proc + 2, "unexpected instruction pointer %p\n",
           get_ip(&ctx.current_thread->ctx));

        byte = 0xc3; /* ret */
        ret = WriteProcessMemory(pi.hProcess, thread_proc + 1, &byte, 1, NULL);
        ok(ret, "WriteProcessMemory failed: %u\n", GetLastError());

        for (;;)
        {
            DEBUG_EVENT ev;

            /* even when there are more pending events, they are not reported until current event is continued */
            ret = WaitForDebugEvent(&ev, 10);
            ok(GetLastError() == ERROR_SEM_TIMEOUT, "WaitForDebugEvent returned %x(%u)\n", ret, GetLastError());

            next_event(&ctx, 100);
            if (ctx.ev.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) break;
            trace("exception at %p in thread %04x\n", ctx.ev.u.Exception.ExceptionRecord.ExceptionAddress, ctx.ev.dwThreadId);
            ok(ctx.ev.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode = %x\n",
               ctx.ev.u.Exception.ExceptionRecord.ExceptionCode);
            ok(ctx.ev.u.Exception.ExceptionRecord.ExceptionAddress == thread_proc + 1,
               "ExceptionAddress = %p\n", ctx.ev.u.Exception.ExceptionRecord.ExceptionAddress);
            ok(get_ip(&ctx.current_thread->ctx) == thread_proc + 2
               || broken(get_ip(&ctx.current_thread->ctx) == thread_proc), /* sometimes observed on win10 */
               "unexpected instruction pointer %p\n",
               get_ip(&ctx.current_thread->ctx));
            exception_cnt++;
        }

        trace("received %u exceptions\n", exception_cnt);

        for (;;)
        {
            ok(ctx.ev.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT
               || broken(ctx.ev.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT), /* sometimes happens on vista */
               "dwDebugEventCode = %d\n", ctx.ev.dwDebugEventCode);
            if (ctx.ev.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT && !--worker_cnt) break;
            next_event(&ctx, 2000);
        }
    }

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    SetEvent(event);

    do
    {
        next_event(&ctx, WAIT_EVENT_TIMEOUT);
        ok (ctx.ev.dwDebugEventCode != EXCEPTION_DEBUG_EVENT, "got exception\n");
    }
    while (ctx.ev.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);

    ret = CloseHandle(event);
    ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());
    ret = CloseHandle(pi.hThread);
    ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());
    ret = CloseHandle(pi.hProcess);
    ok(ret, "CloseHandle failed, last error %d.\n", GetLastError());
}

1449 1450
START_TEST(debugger)
{
1451
    HMODULE hdll;
1452

1453
    hdll=GetModuleHandleA("kernel32.dll");
1454
    pCheckRemoteDebuggerPresent=(void*)GetProcAddress(hdll, "CheckRemoteDebuggerPresent");
1455 1456
    pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
    pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
1457
    pIsDebuggerPresent=(void*)GetProcAddress(hdll, "IsDebuggerPresent");
1458

1459 1460 1461
    ntdll = GetModuleHandleA("ntdll.dll");
    pDbgBreakPoint = (void*)GetProcAddress(ntdll, "DbgBreakPoint");

1462
    myARGC=winetest_get_mainargs(&myARGV);
1463 1464 1465 1466
    if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
    {
        doCrash(myARGC, myARGV);
    }
1467
    else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
1468 1469 1470
    {
        doDebugger(myARGC, myARGV);
    }
1471 1472 1473 1474
    else if (myARGC >= 5 && !strcmp(myARGV[2], "child"))
    {
        doChild(myARGC, myARGV);
    }
1475 1476 1477 1478
    else if (myARGC >= 4 && !strcmp(myARGV[2], "children"))
    {
        doChildren(myARGC, myARGV);
    }
1479 1480 1481 1482 1483 1484
    else if (myARGC >= 4 && !strcmp(myARGV[2], "wait"))
    {
        DWORD event;
        sscanf(myARGV[3], "%x", &event);
        wait_debugger((HANDLE)(DWORD_PTR)event);
    }
1485 1486 1487
    else
    {
        test_ExitCode();
1488
        test_RemoteDebugger();
1489
        test_debug_loop(myARGC, myARGV);
1490 1491
        test_debug_children(myARGV[0], DEBUG_PROCESS, TRUE);
        test_debug_children(myARGV[0], DEBUG_ONLY_THIS_PROCESS, FALSE);
1492
        test_debug_children(myARGV[0], DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS, FALSE);
1493
        test_debug_children(myARGV[0], 0, FALSE);
1494
        test_debugger(myARGV[0]);
1495 1496
    }
}