3 * Copyright 2011 Alistair Leslie-Hughes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/list.h"
37 #include "mscoree_private.h"
38 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
43 typedef struct DebugProcess
45 ICorDebugProcess ICorDebugProcess_iface;
56 static inline CorDebug *impl_from_ICorDebug( ICorDebug *iface )
58 return CONTAINING_RECORD(iface, CorDebug, ICorDebug_iface);
61 static inline CorDebug *impl_from_ICorDebugProcessEnum( ICorDebugProcessEnum *iface )
63 return CONTAINING_RECORD(iface, CorDebug, ICorDebugProcessEnum_iface);
66 static inline DebugProcess *impl_from_ICorDebugProcess( ICorDebugProcess *iface )
68 return CONTAINING_RECORD(iface, DebugProcess, ICorDebugProcess_iface);
71 /* ICorDebugProcess Interface */
72 static HRESULT WINAPI cordebugprocess_QueryInterface(ICorDebugProcess *iface,
73 REFIID riid, void **ppvObject)
75 DebugProcess *This = impl_from_ICorDebugProcess(iface);
77 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
79 if ( IsEqualGUID( riid, &IID_ICorDebugProcess ) ||
80 IsEqualGUID( riid, &IID_ICorDebugController ) ||
81 IsEqualGUID( riid, &IID_IUnknown ) )
83 *ppvObject = &This->ICorDebugProcess_iface;
87 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
91 ICorDebugProcess_AddRef(iface);
96 static ULONG WINAPI cordebugprocess_AddRef(ICorDebugProcess *iface)
98 DebugProcess *This = impl_from_ICorDebugProcess(iface);
99 ULONG ref = InterlockedIncrement(&This->ref);
101 TRACE("%p ref=%u\n", This, ref);
106 static ULONG WINAPI cordebugprocess_Release(ICorDebugProcess *iface)
108 DebugProcess *This = impl_from_ICorDebugProcess(iface);
109 ULONG ref = InterlockedDecrement(&This->ref);
111 TRACE("%p ref=%u\n", This, ref);
116 CloseHandle(This->handle);
119 CloseHandle(This->thread);
122 ICorDebug_Release(&This->cordebug->ICorDebug_iface);
124 HeapFree(GetProcessHeap(), 0, This);
130 static HRESULT WINAPI cordebugprocess_Stop(ICorDebugProcess *iface, DWORD dwTimeoutIgnored)
132 DebugProcess *This = impl_from_ICorDebugProcess(iface);
133 FIXME("stub %p\n", This);
137 static HRESULT WINAPI cordebugprocess_Continue(ICorDebugProcess *iface, BOOL fIsOutOfBand)
139 DebugProcess *This = impl_from_ICorDebugProcess(iface);
143 ResumeThread(This->thread);
148 static HRESULT WINAPI cordebugprocess_IsRunning(ICorDebugProcess *iface, BOOL *pbRunning)
150 DebugProcess *This = impl_from_ICorDebugProcess(iface);
151 FIXME("stub %p\n", This);
155 static HRESULT WINAPI cordebugprocess_HasQueuedCallbacks(ICorDebugProcess *iface,
156 ICorDebugThread *pThread, BOOL *pbQueued)
158 DebugProcess *This = impl_from_ICorDebugProcess(iface);
159 FIXME("stub %p\n", This);
163 static HRESULT WINAPI cordebugprocess_EnumerateThreads(ICorDebugProcess *iface,
164 ICorDebugThreadEnum **ppThreads)
166 DebugProcess *This = impl_from_ICorDebugProcess(iface);
167 FIXME("stub %p\n", This);
171 static HRESULT WINAPI cordebugprocess_SetAllThreadsDebugState(ICorDebugProcess *iface,
172 CorDebugThreadState state, ICorDebugThread *pExceptThisThread)
174 DebugProcess *This = impl_from_ICorDebugProcess(iface);
175 FIXME("stub %p\n", This);
179 static HRESULT WINAPI cordebugprocess_Detach(ICorDebugProcess *iface)
181 DebugProcess *This = impl_from_ICorDebugProcess(iface);
182 FIXME("stub %p\n", This);
186 static HRESULT WINAPI cordebugprocess_Terminate(ICorDebugProcess *iface, UINT exitCode)
188 DebugProcess *This = impl_from_ICorDebugProcess(iface);
195 ret = TerminateProcess(This->handle, exitCode);
196 CloseHandle(This->handle);
199 return ret ? S_OK : E_FAIL;
202 static HRESULT WINAPI cordebugprocess_CanCommitChanges(ICorDebugProcess *iface,
203 ULONG cSnapshots, ICorDebugEditAndContinueSnapshot * pSnapshots[],
204 ICorDebugErrorInfoEnum **pError)
206 DebugProcess *This = impl_from_ICorDebugProcess(iface);
207 FIXME("stub %p\n", This);
211 static HRESULT WINAPI cordebugprocess_CommitChanges(ICorDebugProcess *iface,
212 ULONG cSnapshots, ICorDebugEditAndContinueSnapshot * pSnapshots[],
213 ICorDebugErrorInfoEnum **pError)
215 DebugProcess *This = impl_from_ICorDebugProcess(iface);
216 FIXME("stub %p\n", This);
220 static HRESULT WINAPI cordebugprocess_GetID(ICorDebugProcess *iface, DWORD *pdwProcessId)
222 DebugProcess *This = impl_from_ICorDebugProcess(iface);
228 *pdwProcessId = This->dwProcessID;
233 static HRESULT WINAPI cordebugprocess_GetHandle(ICorDebugProcess *iface, HPROCESS *phProcessHandle)
235 DebugProcess *This = impl_from_ICorDebugProcess(iface);
241 *phProcessHandle = This->handle;
246 static HRESULT WINAPI cordebugprocess_GetThread(ICorDebugProcess *iface, DWORD dwThreadId,
247 ICorDebugThread **ppThread)
249 DebugProcess *This = impl_from_ICorDebugProcess(iface);
250 FIXME("stub %p\n", This);
254 static HRESULT WINAPI cordebugprocess_EnumerateObjects(ICorDebugProcess *iface,
255 ICorDebugObjectEnum **ppObjects)
257 DebugProcess *This = impl_from_ICorDebugProcess(iface);
258 FIXME("stub %p\n", This);
262 static HRESULT WINAPI cordebugprocess_IsTransitionStub(ICorDebugProcess *iface,
263 CORDB_ADDRESS address, BOOL *pbTransitionStub)
265 DebugProcess *This = impl_from_ICorDebugProcess(iface);
266 FIXME("stub %p\n", This);
270 static HRESULT WINAPI cordebugprocess_IsOSSuspended(ICorDebugProcess *iface,
271 DWORD threadID, BOOL *pbSuspended)
273 DebugProcess *This = impl_from_ICorDebugProcess(iface);
274 FIXME("stub %p\n", This);
278 static HRESULT WINAPI cordebugprocess_GetThreadContext(ICorDebugProcess *iface,
279 DWORD threadID, ULONG32 contextSize, BYTE context[])
281 DebugProcess *This = impl_from_ICorDebugProcess(iface);
282 FIXME("stub %p\n", This);
286 static HRESULT WINAPI cordebugprocess_SetThreadContext(ICorDebugProcess *iface,
287 DWORD threadID, ULONG32 contextSize, BYTE context[])
289 DebugProcess *This = impl_from_ICorDebugProcess(iface);
290 FIXME("stub %p\n", This);
294 static HRESULT WINAPI cordebugprocess_ReadMemory(ICorDebugProcess *iface,
295 CORDB_ADDRESS address, DWORD size, BYTE buffer[],
298 DebugProcess *This = impl_from_ICorDebugProcess(iface);
299 FIXME("stub %p\n", This);
303 static HRESULT WINAPI cordebugprocess_WriteMemory(ICorDebugProcess *iface,
304 CORDB_ADDRESS address, DWORD size, BYTE buffer[],
307 DebugProcess *This = impl_from_ICorDebugProcess(iface);
308 FIXME("stub %p\n", This);
312 static HRESULT WINAPI cordebugprocess_ClearCurrentException(ICorDebugProcess *iface,
315 DebugProcess *This = impl_from_ICorDebugProcess(iface);
316 FIXME("stub %p\n", This);
320 static HRESULT WINAPI cordebugprocess_EnableLogMessages(ICorDebugProcess *iface,
323 DebugProcess *This = impl_from_ICorDebugProcess(iface);
324 FIXME("stub %p\n", This);
328 static HRESULT WINAPI cordebugprocess_ModifyLogSwitch(ICorDebugProcess *iface,
329 WCHAR *pLogSwitchName, LONG lLevel)
331 DebugProcess *This = impl_from_ICorDebugProcess(iface);
332 FIXME("stub %p\n", This);
336 static HRESULT WINAPI cordebugprocess_EnumerateAppDomains(ICorDebugProcess *iface,
337 ICorDebugAppDomainEnum **ppAppDomains)
339 DebugProcess *This = impl_from_ICorDebugProcess(iface);
340 FIXME("stub %p\n", This);
344 static HRESULT WINAPI cordebugprocess_GetObject(ICorDebugProcess *iface,
345 ICorDebugValue **ppObject)
347 DebugProcess *This = impl_from_ICorDebugProcess(iface);
348 FIXME("stub %p\n", This);
352 static HRESULT WINAPI cordebugprocess_ThreadForFiberCookie(ICorDebugProcess *iface,
353 DWORD fiberCookie, ICorDebugThread **ppThread)
355 DebugProcess *This = impl_from_ICorDebugProcess(iface);
356 FIXME("stub %p\n", This);
360 static HRESULT WINAPI cordebugprocess_GetHelperThreadID(ICorDebugProcess *iface,
363 DebugProcess *This = impl_from_ICorDebugProcess(iface);
364 FIXME("stub %p\n", This);
369 /***************************************/
370 static const ICorDebugProcessVtbl cordebugprocessVtbl = {
371 cordebugprocess_QueryInterface,
372 cordebugprocess_AddRef,
373 cordebugprocess_Release,
374 cordebugprocess_Stop,
375 cordebugprocess_Continue,
376 cordebugprocess_IsRunning,
377 cordebugprocess_HasQueuedCallbacks,
378 cordebugprocess_EnumerateThreads,
379 cordebugprocess_SetAllThreadsDebugState,
380 cordebugprocess_Detach,
381 cordebugprocess_Terminate,
382 cordebugprocess_CanCommitChanges,
383 cordebugprocess_CommitChanges,
384 cordebugprocess_GetID,
385 cordebugprocess_GetHandle,
386 cordebugprocess_GetThread,
387 cordebugprocess_EnumerateObjects,
388 cordebugprocess_IsTransitionStub,
389 cordebugprocess_IsOSSuspended,
390 cordebugprocess_GetThreadContext,
391 cordebugprocess_SetThreadContext,
392 cordebugprocess_ReadMemory,
393 cordebugprocess_WriteMemory,
394 cordebugprocess_ClearCurrentException,
395 cordebugprocess_EnableLogMessages,
396 cordebugprocess_ModifyLogSwitch,
397 cordebugprocess_EnumerateAppDomains,
398 cordebugprocess_GetObject,
399 cordebugprocess_ThreadForFiberCookie,
400 cordebugprocess_GetHelperThreadID
404 static HRESULT CorDebugProcess_Create(CorDebug *cordebug, IUnknown** ppUnk, LPPROCESS_INFORMATION lpProcessInformation)
408 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
410 return E_OUTOFMEMORY;
412 if(!DuplicateHandle(GetCurrentProcess(), lpProcessInformation->hProcess,
413 GetCurrentProcess(), &This->handle, 0, FALSE, DUPLICATE_SAME_ACCESS))
415 ERR("Failed to duplicate process handle\n");
416 HeapFree(GetProcessHeap(), 0, This);
419 if(!DuplicateHandle(GetCurrentProcess(), lpProcessInformation->hThread,
420 GetCurrentProcess(), &This->thread, 0, FALSE, DUPLICATE_SAME_ACCESS))
422 CloseHandle(This->handle);
424 ERR("Failed to duplicate thread handle\n");
425 HeapFree(GetProcessHeap(), 0, This);
429 This->ICorDebugProcess_iface.lpVtbl = &cordebugprocessVtbl;
431 This->cordebug = cordebug;
432 This->dwProcessID = lpProcessInformation->dwProcessId;
435 ICorDebug_AddRef(&This->cordebug->ICorDebug_iface);
437 *ppUnk = (IUnknown*)This;
442 /* ICorDebugProcessEnum Interface */
443 static HRESULT WINAPI process_enum_QueryInterface(ICorDebugProcessEnum *iface, REFIID riid, void **ppvObject)
445 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
447 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
449 if ( IsEqualGUID( riid, &IID_ICorDebugProcessEnum ) ||
450 IsEqualGUID( riid, &IID_ICorDebugEnum ) ||
451 IsEqualGUID( riid, &IID_IUnknown ) )
453 *ppvObject = &This->ICorDebugProcessEnum_iface;
457 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
458 return E_NOINTERFACE;
461 ICorDebugProcessEnum_AddRef(iface);
466 static ULONG WINAPI process_enum_AddRef(ICorDebugProcessEnum *iface)
468 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
469 TRACE("%p ref=%u\n", This, This->ref);
471 return ICorDebug_AddRef(&This->ICorDebug_iface);
474 static ULONG WINAPI process_enum_Release(ICorDebugProcessEnum *iface)
476 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
477 TRACE("%p ref=%u\n", This, This->ref);
479 return ICorDebug_Release(&This->ICorDebug_iface);
482 static HRESULT WINAPI process_enum_Skip(ICorDebugProcessEnum *iface, ULONG celt)
484 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
485 FIXME("stub %p\n", This);
489 static HRESULT WINAPI process_enum_Reset(ICorDebugProcessEnum *iface)
491 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
492 FIXME("stub %p\n", This);
496 static HRESULT WINAPI process_enum_Clone(ICorDebugProcessEnum *iface, ICorDebugEnum **ppEnum)
498 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
499 FIXME("stub %p %p\n", This, ppEnum);
503 static HRESULT WINAPI process_enum_GetCount(ICorDebugProcessEnum *iface, ULONG *pcelt)
505 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
506 TRACE("stub %p %p\n", This, pcelt);
511 *pcelt = list_count(&This->processes);
516 static HRESULT WINAPI process_enum_Next(ICorDebugProcessEnum *iface, ULONG celt,
517 ICorDebugProcess * processes[], ULONG *pceltFetched)
519 CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
520 FIXME("stub %p %d %p %p\n", This, celt, processes, pceltFetched);
524 static const struct ICorDebugProcessEnumVtbl processenum_vtbl =
526 process_enum_QueryInterface,
528 process_enum_Release,
532 process_enum_GetCount,
536 /*** IUnknown methods ***/
537 static HRESULT WINAPI CorDebug_QueryInterface(ICorDebug *iface, REFIID riid, void **ppvObject)
539 CorDebug *This = impl_from_ICorDebug( iface );
541 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
543 if ( IsEqualGUID( riid, &IID_ICorDebug ) ||
544 IsEqualGUID( riid, &IID_IUnknown ) )
546 *ppvObject = &This->ICorDebug_iface;
550 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
551 return E_NOINTERFACE;
554 ICorDebug_AddRef( iface );
559 static ULONG WINAPI CorDebug_AddRef(ICorDebug *iface)
561 CorDebug *This = impl_from_ICorDebug( iface );
562 ULONG ref = InterlockedIncrement(&This->ref);
564 TRACE("%p ref=%u\n", This, ref);
569 static ULONG WINAPI CorDebug_Release(ICorDebug *iface)
571 CorDebug *This = impl_from_ICorDebug( iface );
572 ULONG ref = InterlockedDecrement(&This->ref);
574 TRACE("%p ref=%u\n", This, ref);
578 if(!list_empty(&This->processes))
579 ERR("Processes haven't been removed Correctly\n");
581 if(This->runtimehost)
582 ICLRRuntimeHost_Release(This->runtimehost);
585 ICorDebugManagedCallback2_Release(This->pCallback2);
588 ICorDebugManagedCallback_Release(This->pCallback);
590 HeapFree(GetProcessHeap(), 0, This);
596 /*** ICorDebug methods ***/
597 static HRESULT WINAPI CorDebug_Initialize(ICorDebug *iface)
599 CorDebug *This = impl_from_ICorDebug( iface );
600 FIXME("stub %p\n", This);
604 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);
610 LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, &This->processes, struct CorProcess, entry)
614 ICorDebugProcess_Terminate(cursor->pProcess, 0);
615 ICorDebugProcess_Release(cursor->pProcess);
618 list_remove(&cursor->entry);
619 HeapFree(GetProcessHeap(), 0, cursor);
625 static HRESULT WINAPI CorDebug_SetManagedHandler(ICorDebug *iface, ICorDebugManagedCallback *pCallback)
627 CorDebug *This = impl_from_ICorDebug( iface );
629 ICorDebugManagedCallback2 *pCallback2;
631 TRACE("%p (%p)\n", This, pCallback);
636 hr = ICorDebugManagedCallback_QueryInterface(pCallback, &IID_ICorDebugManagedCallback2, (void**)&pCallback2);
640 ICorDebugManagedCallback2_Release(This->pCallback2);
643 ICorDebugManagedCallback_Release(This->pCallback);
645 This->pCallback = pCallback;
646 This->pCallback2 = pCallback2;
648 ICorDebugManagedCallback_AddRef(This->pCallback);
652 WARN("Debugging without interface ICorDebugManagedCallback2 is currently not supported.\n");
658 static HRESULT WINAPI CorDebug_SetUnmanagedHandler(ICorDebug *iface, ICorDebugUnmanagedCallback *pCallback)
660 CorDebug *This = impl_from_ICorDebug( iface );
661 FIXME("stub %p %p\n", This, pCallback);
665 static HRESULT WINAPI CorDebug_CreateProcess(ICorDebug *iface, LPCWSTR lpApplicationName,
666 LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
667 LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
668 DWORD dwCreationFlags, PVOID lpEnvironment,LPCWSTR lpCurrentDirectory,
669 LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation,
670 CorDebugCreateProcessFlags debuggingFlags, ICorDebugProcess **ppProcess)
672 CorDebug *This = impl_from_ICorDebug( iface );
673 ICorDebugProcess *pDebugProcess;
676 TRACE("stub %p %s %s %p %p %d %d %p %s %p %p %d %p\n", This, debugstr_w(lpApplicationName),
677 debugstr_w(lpCommandLine), lpProcessAttributes, lpThreadAttributes,
678 bInheritHandles, dwCreationFlags, lpEnvironment, debugstr_w(lpCurrentDirectory),
679 lpStartupInfo, lpProcessInformation, debuggingFlags, ppProcess);
681 if(CreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes,
682 bInheritHandles, dwCreationFlags | CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory,
683 lpStartupInfo, lpProcessInformation))
685 hr = CorDebugProcess_Create(This, (IUnknown**)&pDebugProcess, lpProcessInformation);
688 struct CorProcess *new_process = HeapAlloc( GetProcessHeap(), 0, sizeof(CorProcess) );
690 new_process->pProcess = pDebugProcess;
691 list_add_tail(&This->processes, &new_process->entry);
693 ICorDebugProcess_AddRef(pDebugProcess);
694 *ppProcess = pDebugProcess;
697 ICorDebugManagedCallback_CreateProcess(This->pCallback, pDebugProcess);
701 TerminateProcess(lpProcessInformation->hProcess, 0);
710 static HRESULT WINAPI CorDebug_DebugActiveProcess(ICorDebug *iface, DWORD id, BOOL win32Attach,
711 ICorDebugProcess **ppProcess)
713 CorDebug *This = impl_from_ICorDebug( iface );
714 FIXME("stub %p %d %d %p\n", This, id, win32Attach, ppProcess);
718 static HRESULT WINAPI CorDebug_EnumerateProcesses( ICorDebug *iface, ICorDebugProcessEnum **ppProcess)
720 CorDebug *This = impl_from_ICorDebug( iface );
721 TRACE("stub %p %p\n", This, ppProcess);
726 *ppProcess = &This->ICorDebugProcessEnum_iface;
727 ICorDebugProcessEnum_AddRef(*ppProcess);
732 static HRESULT WINAPI CorDebug_GetProcess(ICorDebug *iface, DWORD dwProcessId, ICorDebugProcess **ppProcess)
734 CorDebug *This = impl_from_ICorDebug( iface );
735 FIXME("stub %p %d %p\n", This, dwProcessId, ppProcess);
739 static HRESULT WINAPI CorDebug_CanLaunchOrAttach(ICorDebug *iface, DWORD dwProcessId,
740 BOOL win32DebuggingEnabled)
742 CorDebug *This = impl_from_ICorDebug( iface );
743 FIXME("stub %p %d %d\n", This, dwProcessId, win32DebuggingEnabled);
747 static const struct ICorDebugVtbl cordebug_vtbl =
749 CorDebug_QueryInterface,
754 CorDebug_SetManagedHandler,
755 CorDebug_SetUnmanagedHandler,
756 CorDebug_CreateProcess,
757 CorDebug_DebugActiveProcess,
758 CorDebug_EnumerateProcesses,
760 CorDebug_CanLaunchOrAttach
763 HRESULT CorDebug_Create(ICLRRuntimeHost *runtimehost, IUnknown** ppUnk)
767 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
769 return E_OUTOFMEMORY;
771 This->ICorDebug_iface.lpVtbl = &cordebug_vtbl;
772 This->ICorDebugProcessEnum_iface.lpVtbl = &processenum_vtbl;
774 This->pCallback = NULL;
775 This->pCallback2 = NULL;
776 This->runtimehost = runtimehost;
778 list_init(&This->processes);
780 if(This->runtimehost)
781 ICLRRuntimeHost_AddRef(This->runtimehost);
783 *ppUnk = (IUnknown*)This;