mshtml: Wine Gecko 1.4 release.
[wine] / dlls / mscoree / cordebug.c
1 /*
2  *
3  * Copyright 2011 Alistair Leslie-Hughes
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #define COBJMACROS
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "shellapi.h"
32 #include "mscoree.h"
33 #include "corhdr.h"
34 #include "metahost.h"
35 #include "cordebug.h"
36 #include "wine/list.h"
37 #include "mscoree_private.h"
38 #include "wine/debug.h"
39
40
41 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
42
43 static inline CorDebug *impl_from_ICorDebug( ICorDebug *iface )
44 {
45     return CONTAINING_RECORD(iface, CorDebug, ICorDebug_iface);
46 }
47
48 static inline CorDebug *impl_from_ICorDebugProcessEnum( ICorDebugProcessEnum *iface )
49 {
50     return CONTAINING_RECORD(iface, CorDebug, ICorDebugProcessEnum_iface);
51 }
52
53 static HRESULT WINAPI process_enum_QueryInterface(ICorDebugProcessEnum *iface, REFIID riid, void **ppvObject)
54 {
55     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
56
57     TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
58
59     if ( IsEqualGUID( riid, &IID_ICorDebugProcessEnum ) ||
60          IsEqualGUID( riid, &IID_ICorDebugEnum ) ||
61          IsEqualGUID( riid, &IID_IUnknown ) )
62     {
63         *ppvObject = &This->ICorDebugProcessEnum_iface;
64     }
65     else
66     {
67         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
68         return E_NOINTERFACE;
69     }
70
71     ICorDebug_AddRef(iface);
72
73     return S_OK;
74 }
75
76 static ULONG WINAPI process_enum_AddRef(ICorDebugProcessEnum *iface)
77 {
78     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
79     TRACE("%p ref=%u\n", This, This->ref);
80
81     return ICorDebug_AddRef(&This->ICorDebug_iface);
82 }
83
84 static ULONG WINAPI process_enum_Release(ICorDebugProcessEnum *iface)
85 {
86     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
87     TRACE("%p ref=%u\n", This, This->ref);
88
89     return ICorDebug_Release(&This->ICorDebug_iface);
90 }
91
92 static HRESULT WINAPI process_enum_Skip(ICorDebugProcessEnum *iface, ULONG celt)
93 {
94     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
95     FIXME("stub %p\n", This);
96     return E_NOTIMPL;
97 }
98
99 static HRESULT WINAPI process_enum_Reset(ICorDebugProcessEnum *iface)
100 {
101     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
102     FIXME("stub %p\n", This);
103     return E_NOTIMPL;
104 }
105
106 static HRESULT WINAPI process_enum_Clone(ICorDebugProcessEnum *iface, ICorDebugEnum **ppEnum)
107 {
108     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
109     FIXME("stub %p %p\n", This, ppEnum);
110     return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI process_enum_GetCount(ICorDebugProcessEnum *iface, ULONG *pcelt)
114 {
115     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
116     TRACE("stub %p %p\n", This, pcelt);
117
118     if(!pcelt)
119         return E_INVALIDARG;
120
121     *pcelt = list_count(&This->processes);
122
123     return S_OK;
124 }
125
126 static HRESULT WINAPI process_enum_Next(ICorDebugProcessEnum *iface, ULONG celt,
127             ICorDebugProcess * processes[], ULONG *pceltFetched)
128 {
129     CorDebug *This = impl_from_ICorDebugProcessEnum(iface);
130     FIXME("stub %p %d %p %p\n", This, celt, processes, pceltFetched);
131     return E_NOTIMPL;
132 }
133
134 static const struct ICorDebugProcessEnumVtbl processenum_vtbl =
135 {
136     process_enum_QueryInterface,
137     process_enum_AddRef,
138     process_enum_Release,
139     process_enum_Skip,
140     process_enum_Reset,
141     process_enum_Clone,
142     process_enum_GetCount,
143     process_enum_Next
144 };
145
146 /*** IUnknown methods ***/
147 static HRESULT WINAPI CorDebug_QueryInterface(ICorDebug *iface, REFIID riid, void **ppvObject)
148 {
149     CorDebug *This = impl_from_ICorDebug( iface );
150
151     TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
152
153     if ( IsEqualGUID( riid, &IID_ICorDebug ) ||
154          IsEqualGUID( riid, &IID_IUnknown ) )
155     {
156         *ppvObject = &This->ICorDebug_iface;
157     }
158     else
159     {
160         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
161         return E_NOINTERFACE;
162     }
163
164     ICorDebug_AddRef( iface );
165
166     return S_OK;
167 }
168
169 static ULONG WINAPI CorDebug_AddRef(ICorDebug *iface)
170 {
171     CorDebug *This = impl_from_ICorDebug( iface );
172     ULONG ref = InterlockedIncrement(&This->ref);
173
174     TRACE("%p ref=%u\n", This, ref);
175
176     return ref;
177 }
178
179 static ULONG WINAPI CorDebug_Release(ICorDebug *iface)
180 {
181     CorDebug *This = impl_from_ICorDebug( iface );
182     ULONG ref = InterlockedDecrement(&This->ref);
183
184     TRACE("%p ref=%u\n", This, ref);
185
186     if (ref == 0)
187     {
188         if(!list_empty(&This->processes))
189             ERR("Processes haven't been removed Correctly\n");
190
191         if(This->runtimehost)
192             ICLRRuntimeHost_Release(This->runtimehost);
193
194         if(This->pCallback)
195             ICorDebugManagedCallback2_Release(This->pCallback2);
196
197         if(This->pCallback)
198             ICorDebugManagedCallback_Release(This->pCallback);
199
200         HeapFree(GetProcessHeap(), 0, This);
201     }
202
203     return ref;
204 }
205
206 /*** ICorDebug methods ***/
207 static HRESULT WINAPI CorDebug_Initialize(ICorDebug *iface)
208 {
209     CorDebug *This = impl_from_ICorDebug( iface );
210     FIXME("stub %p\n", This);
211     return S_OK;
212 }
213
214 static HRESULT WINAPI CorDebug_Terminate(ICorDebug *iface)
215 {
216     struct CorProcess *cursor, *cursor2;
217     CorDebug *This = impl_from_ICorDebug( iface );
218     FIXME("stub %p\n", This);
219
220     LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, &This->processes, struct CorProcess, entry)
221     {
222         if(cursor->pProcess)
223             ICorDebugProcess_Release(cursor->pProcess);
224
225         list_remove(&cursor->entry);
226         HeapFree(GetProcessHeap(), 0, cursor);
227     }
228
229     return E_NOTIMPL;
230 }
231
232 static HRESULT WINAPI CorDebug_SetManagedHandler(ICorDebug *iface, ICorDebugManagedCallback *pCallback)
233 {
234     CorDebug *This = impl_from_ICorDebug( iface );
235     HRESULT hr;
236     ICorDebugManagedCallback2 *pCallback2;
237
238     TRACE("%p (%p)\n", This, pCallback);
239
240     if(!pCallback)
241         return E_INVALIDARG;
242
243     hr = ICorDebugManagedCallback_QueryInterface(pCallback, &IID_ICorDebugManagedCallback2, (void**)&pCallback2);
244     if(hr == S_OK)
245     {
246         if(This->pCallback2)
247             ICorDebugManagedCallback2_Release(This->pCallback2);
248
249         if(This->pCallback)
250             ICorDebugManagedCallback_Release(This->pCallback);
251
252         This->pCallback = pCallback;
253         This->pCallback2 = pCallback2;
254
255         ICorDebugManagedCallback_AddRef(This->pCallback);
256     }
257     else
258     {
259         WARN("Debugging without interface ICorDebugManagedCallback2 is currently not supported.\n");
260     }
261
262     return hr;
263 }
264
265 static HRESULT WINAPI CorDebug_SetUnmanagedHandler(ICorDebug *iface, ICorDebugUnmanagedCallback *pCallback)
266 {
267     CorDebug *This = impl_from_ICorDebug( iface );
268     FIXME("stub %p %p\n", This, pCallback);
269     return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI CorDebug_CreateProcess(ICorDebug *iface, LPCWSTR lpApplicationName,
273             LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes,
274             LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
275             DWORD dwCreationFlags, PVOID lpEnvironment,LPCWSTR lpCurrentDirectory,
276             LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation,
277             CorDebugCreateProcessFlags debuggingFlags, ICorDebugProcess **ppProcess)
278 {
279     CorDebug *This = impl_from_ICorDebug( iface );
280     FIXME("stub %p %s %s %p %p %d %d %p %s %p %p %d %p\n", This, debugstr_w(lpApplicationName),
281             debugstr_w(lpCommandLine), lpProcessAttributes, lpThreadAttributes,
282             bInheritHandles, dwCreationFlags, lpEnvironment, debugstr_w(lpCurrentDirectory),
283             lpStartupInfo, lpProcessInformation, debuggingFlags, ppProcess);
284     return E_NOTIMPL;
285 }
286
287 static HRESULT WINAPI CorDebug_DebugActiveProcess(ICorDebug *iface, DWORD id, BOOL win32Attach,
288             ICorDebugProcess **ppProcess)
289 {
290     CorDebug *This = impl_from_ICorDebug( iface );
291     FIXME("stub %p %d %d %p\n", This, id, win32Attach, ppProcess);
292     return E_NOTIMPL;
293 }
294
295 static HRESULT WINAPI CorDebug_EnumerateProcesses( ICorDebug *iface, ICorDebugProcessEnum **ppProcess)
296 {
297     CorDebug *This = impl_from_ICorDebug( iface );
298     TRACE("stub %p %p\n", This, ppProcess);
299
300     if(!ppProcess)
301         return E_INVALIDARG;
302
303     *ppProcess = &This->ICorDebugProcessEnum_iface;
304     ICorDebugProcessEnum_AddRef(*ppProcess);
305
306     return S_OK;
307 }
308
309 static HRESULT WINAPI CorDebug_GetProcess(ICorDebug *iface, DWORD dwProcessId, ICorDebugProcess **ppProcess)
310 {
311     CorDebug *This = impl_from_ICorDebug( iface );
312     FIXME("stub %p %d %p\n", This, dwProcessId, ppProcess);
313     return E_NOTIMPL;
314 }
315
316 static HRESULT WINAPI CorDebug_CanLaunchOrAttach(ICorDebug *iface, DWORD dwProcessId,
317             BOOL win32DebuggingEnabled)
318 {
319     CorDebug *This = impl_from_ICorDebug( iface );
320     FIXME("stub %p %d %d\n", This, dwProcessId, win32DebuggingEnabled);
321     return S_OK;
322 }
323
324 static const struct ICorDebugVtbl cordebug_vtbl =
325 {
326     CorDebug_QueryInterface,
327     CorDebug_AddRef,
328     CorDebug_Release,
329     CorDebug_Initialize,
330     CorDebug_Terminate,
331     CorDebug_SetManagedHandler,
332     CorDebug_SetUnmanagedHandler,
333     CorDebug_CreateProcess,
334     CorDebug_DebugActiveProcess,
335     CorDebug_EnumerateProcesses,
336     CorDebug_GetProcess,
337     CorDebug_CanLaunchOrAttach
338 };
339
340 HRESULT CorDebug_Create(ICLRRuntimeHost *runtimehost, IUnknown** ppUnk)
341 {
342     CorDebug *This;
343
344     This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
345     if ( !This )
346         return E_OUTOFMEMORY;
347
348     This->ICorDebug_iface.lpVtbl = &cordebug_vtbl;
349     This->ICorDebugProcessEnum_iface.lpVtbl = &processenum_vtbl;
350     This->ref = 1;
351     This->pCallback = NULL;
352     This->pCallback2 = NULL;
353     This->runtimehost = runtimehost;
354
355     list_init(&This->processes);
356
357     if(This->runtimehost)
358         ICLRRuntimeHost_AddRef(This->runtimehost);
359
360     *ppUnk = (IUnknown*)This;
361
362     return S_OK;
363 }