cordebug.c 22.3 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 *
 * Copyright 2011 Alistair Leslie-Hughes
 *
 * 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
 */

#define COBJMACROS

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"

#include "winuser.h"
#include "winnls.h"
#include "winreg.h"
#include "ole2.h"
#include "shellapi.h"
#include "mscoree.h"
#include "corhdr.h"
#include "metahost.h"
#include "cordebug.h"
#include "wine/list.h"
#include "mscoree_private.h"
#include "wine/debug.h"


WINE_DEFAULT_DEBUG_CHANNEL( mscoree );

43 44 45 46 47 48 49 50 51 52 53 54 55
typedef struct DebugProcess
{
    ICorDebugProcess ICorDebugProcess_iface;

    CorDebug *cordebug;

    DWORD dwProcessID;
    HANDLE handle;
    HANDLE thread;

    LONG ref;
} DebugProcess;

56
static inline CorDebug *impl_from_ICorDebug( ICorDebug *iface )
57
{
58
    return CONTAINING_RECORD(iface, CorDebug, ICorDebug_iface);
59 60
}

61 62 63 64 65
static inline CorDebug *impl_from_ICorDebugProcessEnum( ICorDebugProcessEnum *iface )
{
    return CONTAINING_RECORD(iface, CorDebug, ICorDebugProcessEnum_iface);
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static inline DebugProcess *impl_from_ICorDebugProcess( ICorDebugProcess *iface )
{
    return CONTAINING_RECORD(iface, DebugProcess, ICorDebugProcess_iface);
}

/* ICorDebugProcess Interface */
static HRESULT WINAPI cordebugprocess_QueryInterface(ICorDebugProcess *iface,
                REFIID riid, void **ppvObject)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);

    TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);

    if ( IsEqualGUID( riid, &IID_ICorDebugProcess ) ||
         IsEqualGUID( riid, &IID_ICorDebugController ) ||
         IsEqualGUID( riid, &IID_IUnknown ) )
    {
        *ppvObject = &This->ICorDebugProcess_iface;
    }
    else
    {
        FIXME("Unsupported interface %s\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

91
    ICorDebugProcess_AddRef(iface);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

    return S_OK;
}

static ULONG WINAPI cordebugprocess_AddRef(ICorDebugProcess *iface)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    ULONG ref = InterlockedIncrement(&This->ref);

    TRACE("%p ref=%u\n", This, ref);

    return ref;
}

static ULONG WINAPI cordebugprocess_Release(ICorDebugProcess *iface)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE("%p ref=%u\n", This, ref);

    if (ref == 0)
    {
        if(This->handle)
            CloseHandle(This->handle);

        if(This->thread)
            CloseHandle(This->thread);

        if(This->cordebug)
            ICorDebug_Release(&This->cordebug->ICorDebug_iface);

        HeapFree(GetProcessHeap(), 0, This);
    }

    return ref;
}

static HRESULT WINAPI cordebugprocess_Stop(ICorDebugProcess *iface, DWORD dwTimeoutIgnored)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_Continue(ICorDebugProcess *iface, BOOL fIsOutOfBand)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    TRACE("%p\n", This);

    if(This->thread)
        ResumeThread(This->thread);

    return S_OK;
}

static HRESULT WINAPI cordebugprocess_IsRunning(ICorDebugProcess *iface, BOOL *pbRunning)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_HasQueuedCallbacks(ICorDebugProcess *iface,
                ICorDebugThread *pThread, BOOL *pbQueued)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_EnumerateThreads(ICorDebugProcess *iface,
                ICorDebugThreadEnum **ppThreads)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_SetAllThreadsDebugState(ICorDebugProcess *iface,
                CorDebugThreadState state, ICorDebugThread *pExceptThisThread)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_Detach(ICorDebugProcess *iface)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_Terminate(ICorDebugProcess *iface, UINT exitCode)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    BOOL ret = TRUE;

    TRACE("%p\n", This);

    if(This->handle)
    {
        ret = TerminateProcess(This->handle, exitCode);
        CloseHandle(This->handle);
        This->handle = NULL;
    }
    return ret ? S_OK : E_FAIL;
}

static HRESULT WINAPI cordebugprocess_CanCommitChanges(ICorDebugProcess *iface,
                ULONG cSnapshots, ICorDebugEditAndContinueSnapshot * pSnapshots[],
                ICorDebugErrorInfoEnum **pError)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_CommitChanges(ICorDebugProcess *iface,
                ULONG cSnapshots, ICorDebugEditAndContinueSnapshot * pSnapshots[],
                ICorDebugErrorInfoEnum **pError)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_GetID(ICorDebugProcess *iface, DWORD *pdwProcessId)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    TRACE("%p\n", This);

    if(!pdwProcessId)
        return E_INVALIDARG;

    *pdwProcessId = This->dwProcessID;

    return S_OK;
}

static HRESULT WINAPI cordebugprocess_GetHandle(ICorDebugProcess *iface, HPROCESS *phProcessHandle)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    TRACE("%p\n", This);

    if(!phProcessHandle)
        return E_INVALIDARG;

    *phProcessHandle = This->handle;

    return S_OK;
}

static HRESULT WINAPI cordebugprocess_GetThread(ICorDebugProcess *iface, DWORD dwThreadId,
                ICorDebugThread **ppThread)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_EnumerateObjects(ICorDebugProcess *iface,
                ICorDebugObjectEnum **ppObjects)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_IsTransitionStub(ICorDebugProcess *iface,
                CORDB_ADDRESS address, BOOL *pbTransitionStub)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_IsOSSuspended(ICorDebugProcess *iface,
                DWORD threadID, BOOL *pbSuspended)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_GetThreadContext(ICorDebugProcess *iface,
                DWORD threadID, ULONG32 contextSize, BYTE context[])
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_SetThreadContext(ICorDebugProcess *iface,
                DWORD threadID, ULONG32 contextSize, BYTE context[])
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_ReadMemory(ICorDebugProcess *iface,
                CORDB_ADDRESS address, DWORD size, BYTE buffer[],
                SIZE_T *read)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_WriteMemory(ICorDebugProcess *iface,
                CORDB_ADDRESS address, DWORD size, BYTE buffer[],
                SIZE_T *written)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_ClearCurrentException(ICorDebugProcess *iface,
                DWORD threadID)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_EnableLogMessages(ICorDebugProcess *iface,
                BOOL fOnOff)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_ModifyLogSwitch(ICorDebugProcess *iface,
                WCHAR *pLogSwitchName, LONG lLevel)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_EnumerateAppDomains(ICorDebugProcess *iface,
                ICorDebugAppDomainEnum **ppAppDomains)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_GetObject(ICorDebugProcess *iface,
                ICorDebugValue **ppObject)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_ThreadForFiberCookie(ICorDebugProcess *iface,
                DWORD fiberCookie, ICorDebugThread **ppThread)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI cordebugprocess_GetHelperThreadID(ICorDebugProcess *iface,
                DWORD *pThreadID)
{
    DebugProcess *This = impl_from_ICorDebugProcess(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}


/***************************************/
static const ICorDebugProcessVtbl cordebugprocessVtbl = {
    cordebugprocess_QueryInterface,
    cordebugprocess_AddRef,
    cordebugprocess_Release,
    cordebugprocess_Stop,
    cordebugprocess_Continue,
    cordebugprocess_IsRunning,
    cordebugprocess_HasQueuedCallbacks,
    cordebugprocess_EnumerateThreads,
    cordebugprocess_SetAllThreadsDebugState,
    cordebugprocess_Detach,
    cordebugprocess_Terminate,
    cordebugprocess_CanCommitChanges,
    cordebugprocess_CommitChanges,
    cordebugprocess_GetID,
    cordebugprocess_GetHandle,
    cordebugprocess_GetThread,
    cordebugprocess_EnumerateObjects,
    cordebugprocess_IsTransitionStub,
    cordebugprocess_IsOSSuspended,
    cordebugprocess_GetThreadContext,
    cordebugprocess_SetThreadContext,
    cordebugprocess_ReadMemory,
    cordebugprocess_WriteMemory,
    cordebugprocess_ClearCurrentException,
    cordebugprocess_EnableLogMessages,
    cordebugprocess_ModifyLogSwitch,
    cordebugprocess_EnumerateAppDomains,
    cordebugprocess_GetObject,
    cordebugprocess_ThreadForFiberCookie,
    cordebugprocess_GetHelperThreadID
};


404
static HRESULT CorDebugProcess_Create(CorDebug *cordebug, IUnknown** ppUnk, LPPROCESS_INFORMATION lpProcessInformation)
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
{
    DebugProcess *This;

    This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
    if ( !This )
        return E_OUTOFMEMORY;

    if(!DuplicateHandle(GetCurrentProcess(), lpProcessInformation->hProcess,
                    GetCurrentProcess(), &This->handle, 0, FALSE, DUPLICATE_SAME_ACCESS))
    {
        ERR("Failed to duplicate process handle\n");
        HeapFree(GetProcessHeap(), 0, This);
        return E_FAIL;
    }
    if(!DuplicateHandle(GetCurrentProcess(), lpProcessInformation->hThread,
                    GetCurrentProcess(), &This->thread, 0, FALSE, DUPLICATE_SAME_ACCESS))
    {
        CloseHandle(This->handle);

        ERR("Failed to duplicate thread handle\n");
        HeapFree(GetProcessHeap(), 0, This);
        return E_FAIL;
    }

    This->ICorDebugProcess_iface.lpVtbl = &cordebugprocessVtbl;
    This->ref = 1;
    This->cordebug = cordebug;
    This->dwProcessID = lpProcessInformation->dwProcessId;

    if(This->cordebug)
        ICorDebug_AddRef(&This->cordebug->ICorDebug_iface);

437
    *ppUnk = (IUnknown*)&This->ICorDebugProcess_iface;
438 439 440 441 442

    return S_OK;
}

/* ICorDebugProcessEnum Interface */
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static HRESULT WINAPI process_enum_QueryInterface(ICorDebugProcessEnum *iface, REFIID riid, void **ppvObject)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);

    TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);

    if ( IsEqualGUID( riid, &IID_ICorDebugProcessEnum ) ||
         IsEqualGUID( riid, &IID_ICorDebugEnum ) ||
         IsEqualGUID( riid, &IID_IUnknown ) )
    {
        *ppvObject = &This->ICorDebugProcessEnum_iface;
    }
    else
    {
        FIXME("Unsupported interface %s\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

461
    ICorDebugProcessEnum_AddRef(iface);
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

    return S_OK;
}

static ULONG WINAPI process_enum_AddRef(ICorDebugProcessEnum *iface)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    TRACE("%p ref=%u\n", This, This->ref);

    return ICorDebug_AddRef(&This->ICorDebug_iface);
}

static ULONG WINAPI process_enum_Release(ICorDebugProcessEnum *iface)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    TRACE("%p ref=%u\n", This, This->ref);

    return ICorDebug_Release(&This->ICorDebug_iface);
}

static HRESULT WINAPI process_enum_Skip(ICorDebugProcessEnum *iface, ULONG celt)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI process_enum_Reset(ICorDebugProcessEnum *iface)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    FIXME("stub %p\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI process_enum_Clone(ICorDebugProcessEnum *iface, ICorDebugEnum **ppEnum)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    FIXME("stub %p %p\n", This, ppEnum);
    return E_NOTIMPL;
}

static HRESULT WINAPI process_enum_GetCount(ICorDebugProcessEnum *iface, ULONG *pcelt)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    TRACE("stub %p %p\n", This, pcelt);

    if(!pcelt)
        return E_INVALIDARG;

    *pcelt = list_count(&This->processes);

    return S_OK;
}

static HRESULT WINAPI process_enum_Next(ICorDebugProcessEnum *iface, ULONG celt,
            ICorDebugProcess * processes[], ULONG *pceltFetched)
{
    CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
    FIXME("stub %p %d %p %p\n", This, celt, processes, pceltFetched);
    return E_NOTIMPL;
}

static const struct ICorDebugProcessEnumVtbl processenum_vtbl =
{
    process_enum_QueryInterface,
    process_enum_AddRef,
    process_enum_Release,
    process_enum_Skip,
    process_enum_Reset,
    process_enum_Clone,
    process_enum_GetCount,
    process_enum_Next
};

536 537 538
/*** IUnknown methods ***/
static HRESULT WINAPI CorDebug_QueryInterface(ICorDebug *iface, REFIID riid, void **ppvObject)
{
539
    CorDebug *This = impl_from_ICorDebug( iface );
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

    TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);

    if ( IsEqualGUID( riid, &IID_ICorDebug ) ||
         IsEqualGUID( riid, &IID_IUnknown ) )
    {
        *ppvObject = &This->ICorDebug_iface;
    }
    else
    {
        FIXME("Unsupported interface %s\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    ICorDebug_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI CorDebug_AddRef(ICorDebug *iface)
{
561 562 563 564 565 566
    CorDebug *This = impl_from_ICorDebug( iface );
    ULONG ref = InterlockedIncrement(&This->ref);

    TRACE("%p ref=%u\n", This, ref);

    return ref;
567 568 569 570
}

static ULONG WINAPI CorDebug_Release(ICorDebug *iface)
{
571 572 573 574 575 576 577
    CorDebug *This = impl_from_ICorDebug( iface );
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE("%p ref=%u\n", This, ref);

    if (ref == 0)
    {
578 579
        if(!list_empty(&This->processes))
            ERR("Processes haven't been removed Correctly\n");
580

581 582 583 584 585 586 587 588 589 590 591 592 593
        if(This->runtimehost)
            ICLRRuntimeHost_Release(This->runtimehost);

        if(This->pCallback)
            ICorDebugManagedCallback2_Release(This->pCallback2);

        if(This->pCallback)
            ICorDebugManagedCallback_Release(This->pCallback);

        HeapFree(GetProcessHeap(), 0, This);
    }

    return ref;
594 595 596 597 598
}

/*** ICorDebug methods ***/
static HRESULT WINAPI CorDebug_Initialize(ICorDebug *iface)
{
599
    CorDebug *This = impl_from_ICorDebug( iface );
600
    FIXME("stub %p\n", This);
601
    return S_OK;
602 603 604 605
}

static HRESULT WINAPI CorDebug_Terminate(ICorDebug *iface)
{
606
    struct CorProcess *cursor, *cursor2;
607
    CorDebug *This = impl_from_ICorDebug( iface );
608
    TRACE("stub %p\n", This);
609 610 611 612

    LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, &This->processes, struct CorProcess, entry)
    {
        if(cursor->pProcess)
613 614
        {
            ICorDebugProcess_Terminate(cursor->pProcess, 0);
615
            ICorDebugProcess_Release(cursor->pProcess);
616
        }
617 618 619 620 621

        list_remove(&cursor->entry);
        HeapFree(GetProcessHeap(), 0, cursor);
    }

622
    return S_OK;
623 624 625 626
}

static HRESULT WINAPI CorDebug_SetManagedHandler(ICorDebug *iface, ICorDebugManagedCallback *pCallback)
{
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    CorDebug *This = impl_from_ICorDebug( iface );
    HRESULT hr;
    ICorDebugManagedCallback2 *pCallback2;

    TRACE("%p (%p)\n", This, pCallback);

    if(!pCallback)
        return E_INVALIDARG;

    hr = ICorDebugManagedCallback_QueryInterface(pCallback, &IID_ICorDebugManagedCallback2, (void**)&pCallback2);
    if(hr == S_OK)
    {
        if(This->pCallback2)
            ICorDebugManagedCallback2_Release(This->pCallback2);

        if(This->pCallback)
            ICorDebugManagedCallback_Release(This->pCallback);

        This->pCallback = pCallback;
        This->pCallback2 = pCallback2;

        ICorDebugManagedCallback_AddRef(This->pCallback);
    }
650 651 652 653
    else
    {
        WARN("Debugging without interface ICorDebugManagedCallback2 is currently not supported.\n");
    }
654 655

    return hr;
656 657 658 659
}

static HRESULT WINAPI CorDebug_SetUnmanagedHandler(ICorDebug *iface, ICorDebugUnmanagedCallback *pCallback)
{
660
    CorDebug *This = impl_from_ICorDebug( iface );
661 662 663 664 665 666 667 668 669 670 671
    FIXME("stub %p %p\n", This, pCallback);
    return E_NOTIMPL;
}

static HRESULT WINAPI CorDebug_CreateProcess(ICorDebug *iface, LPCWSTR lpApplicationName,
            LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
            LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
            DWORD dwCreationFlags, PVOID lpEnvironment,LPCWSTR lpCurrentDirectory,
            LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation,
            CorDebugCreateProcessFlags debuggingFlags, ICorDebugProcess **ppProcess)
{
672
    CorDebug *This = impl_from_ICorDebug( iface );
673 674 675 676
    ICorDebugProcess *pDebugProcess;
    HRESULT hr;

    TRACE("stub %p %s %s %p %p %d %d %p %s %p %p %d %p\n", This, debugstr_w(lpApplicationName),
677 678 679
            debugstr_w(lpCommandLine), lpProcessAttributes, lpThreadAttributes,
            bInheritHandles, dwCreationFlags, lpEnvironment, debugstr_w(lpCurrentDirectory),
            lpStartupInfo, lpProcessInformation, debuggingFlags, ppProcess);
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

    if(CreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes,
            bInheritHandles, dwCreationFlags | CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory,
            lpStartupInfo, lpProcessInformation))
    {
        hr = CorDebugProcess_Create(This, (IUnknown**)&pDebugProcess, lpProcessInformation);
        if(hr == S_OK)
        {
            struct CorProcess *new_process = HeapAlloc( GetProcessHeap(), 0, sizeof(CorProcess) );

            new_process->pProcess = pDebugProcess;
            list_add_tail(&This->processes, &new_process->entry);

            ICorDebugProcess_AddRef(pDebugProcess);
            *ppProcess = pDebugProcess;

            if(This->pCallback)
                ICorDebugManagedCallback_CreateProcess(This->pCallback, pDebugProcess);
        }
        else
        {
            TerminateProcess(lpProcessInformation->hProcess, 0);
        }
    }
    else
        hr = E_FAIL;

    return hr;
708 709 710 711 712
}

static HRESULT WINAPI CorDebug_DebugActiveProcess(ICorDebug *iface, DWORD id, BOOL win32Attach,
            ICorDebugProcess **ppProcess)
{
713
    CorDebug *This = impl_from_ICorDebug( iface );
714 715 716 717 718 719
    FIXME("stub %p %d %d %p\n", This, id, win32Attach, ppProcess);
    return E_NOTIMPL;
}

static HRESULT WINAPI CorDebug_EnumerateProcesses( ICorDebug *iface, ICorDebugProcessEnum **ppProcess)
{
720
    CorDebug *This = impl_from_ICorDebug( iface );
721 722 723 724 725 726 727 728 729
    TRACE("stub %p %p\n", This, ppProcess);

    if(!ppProcess)
        return E_INVALIDARG;

    *ppProcess = &This->ICorDebugProcessEnum_iface;
    ICorDebugProcessEnum_AddRef(*ppProcess);

    return S_OK;
730 731 732 733
}

static HRESULT WINAPI CorDebug_GetProcess(ICorDebug *iface, DWORD dwProcessId, ICorDebugProcess **ppProcess)
{
734
    CorDebug *This = impl_from_ICorDebug( iface );
735 736 737 738 739 740 741
    FIXME("stub %p %d %p\n", This, dwProcessId, ppProcess);
    return E_NOTIMPL;
}

static HRESULT WINAPI CorDebug_CanLaunchOrAttach(ICorDebug *iface, DWORD dwProcessId,
            BOOL win32DebuggingEnabled)
{
742
    CorDebug *This = impl_from_ICorDebug( iface );
743
    FIXME("stub %p %d %d\n", This, dwProcessId, win32DebuggingEnabled);
744
    return S_OK;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
}

static const struct ICorDebugVtbl cordebug_vtbl =
{
    CorDebug_QueryInterface,
    CorDebug_AddRef,
    CorDebug_Release,
    CorDebug_Initialize,
    CorDebug_Terminate,
    CorDebug_SetManagedHandler,
    CorDebug_SetUnmanagedHandler,
    CorDebug_CreateProcess,
    CorDebug_DebugActiveProcess,
    CorDebug_EnumerateProcesses,
    CorDebug_GetProcess,
    CorDebug_CanLaunchOrAttach
};

763
HRESULT CorDebug_Create(ICLRRuntimeHost *runtimehost, IUnknown** ppUnk)
764
{
765 766 767 768 769 770
    CorDebug *This;

    This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
    if ( !This )
        return E_OUTOFMEMORY;

771
    This->ICorDebug_iface.lpVtbl = &cordebug_vtbl;
772
    This->ICorDebugProcessEnum_iface.lpVtbl = &processenum_vtbl;
773 774 775 776 777
    This->ref = 1;
    This->pCallback = NULL;
    This->pCallback2 = NULL;
    This->runtimehost = runtimehost;

778 779
    list_init(&This->processes);

780 781 782
    if(This->runtimehost)
        ICLRRuntimeHost_AddRef(This->runtimehost);

783
    *ppUnk = (IUnknown*)&This->ICorDebug_iface;
784 785

    return S_OK;
786
}