summaryrefslogtreecommitdiff
path: root/src/utilcode/dacutil.cpp
blob: 03aaa938a0ddf4371ca1d5af2d99b3bae9bd65ee (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
91
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//*****************************************************************************
//
// Internal data access functionality.
//

//*****************************************************************************

#include "stdafx.h"

#include <winwrap.h>
#include <utilcode.h>
#include <dacprivate.h>

//----------------------------------------------------------------------------
//
// LiveProcDataTarget.
//
//----------------------------------------------------------------------------

LiveProcDataTarget::LiveProcDataTarget(HANDLE process,
                                       DWORD processId,
                                       CLRDATA_ADDRESS baseAddressOfEngine)
{
    m_process = process;
    m_processId = processId;
    m_baseAddressOfEngine = baseAddressOfEngine;
}

STDMETHODIMP
LiveProcDataTarget::QueryInterface(
    THIS_
    IN REFIID InterfaceId,
    OUT PVOID* Interface
    )
{
    if (InterfaceId == IID_IUnknown ||
        InterfaceId == IID_ICLRDataTarget)
    {
        *Interface = (ICLRDataTarget*)this;
        // No need to refcount as this class is contained.
        return S_OK;
    }
    else
    {
        *Interface = NULL;
        return E_NOINTERFACE;
    }
}

STDMETHODIMP_(ULONG)
LiveProcDataTarget::AddRef(
    THIS
    )
{
    // No need to refcount as this class is contained.
    return 1;
}

STDMETHODIMP_(ULONG)
LiveProcDataTarget::Release(
    THIS
    )
{
    SUPPORTS_DAC_HOST_ONLY;
    // No need to refcount as this class is contained.
    return 0;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::GetMachineType( 
    /* [out] */ ULONG32 *machine)
{
    LIMITED_METHOD_CONTRACT;

#if defined(_TARGET_X86_)
    *machine = IMAGE_FILE_MACHINE_I386;
#elif defined(_TARGET_AMD64_)
    *machine = IMAGE_FILE_MACHINE_AMD64;
#elif defined(_TARGET_ARM_)
    *machine = IMAGE_FILE_MACHINE_ARMNT;
#else
    PORTABILITY_ASSERT("Unknown Processor");
#endif
    return S_OK;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::GetPointerSize( 
    /* [out] */ ULONG32 *size)
{
    LIMITED_METHOD_CONTRACT;

    *size = sizeof(void*);
    return S_OK;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::GetImageBase( 
    /* [string][in] */ LPCWSTR name,
    /* [out] */ CLRDATA_ADDRESS *base)
{
    //
    // The only image base that the access code cares
    // about right now is the base of mscorwks.  
    //
    
    if (wcscmp(name, MAIN_CLR_DLL_NAME_W))
    {
        return E_NOINTERFACE;
    }

    // 
    // If a base address was specified, use that
    //
    if (NULL != m_baseAddressOfEngine)
    {
        *base = m_baseAddressOfEngine;
        return S_OK;
    }

    // 
    // Our creator must have told us WHICH clr to work with.
    //
    return E_FAIL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::ReadVirtual( 
    /* [in] */ CLRDATA_ADDRESS address,
    /* [length_is][size_is][out] */ PBYTE buffer,
    /* [in] */ ULONG32 request,
    /* [optional][out] */ ULONG32 *done)
{
    // ReadProcessMemory will fail if any part of the
    // region to read does not have read access.  This
    // routine attempts to read the largest valid prefix
    // so it has to break up reads on page boundaries.

    HRESULT status = S_OK;
    ULONG32 totalDone = 0;
    SIZE_T read;
    ULONG32 readSize;

    while (request > 0)
    {
        // Calculate bytes to read and don't let read cross
        // a page boundary.
        readSize = GetOsPageSize() - (ULONG32)(address & (GetOsPageSize() - 1));
        readSize = min(request, readSize);

        if (!ReadProcessMemory(m_process, (PVOID)(ULONG_PTR)address,
                               buffer, readSize, &read))
        {
            if (totalDone == 0)
            {
                // If we haven't read anything indicate failure.
                status = E_FAIL;
            }
            break;
        }

        totalDone += (ULONG32)read;
        address += read;
        buffer += read;
        request -= (ULONG32)read;
    }

    *done = totalDone;
    return status;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::WriteVirtual( 
    /* [in] */ CLRDATA_ADDRESS address,
    /* [size_is][in] */ PBYTE buffer,
    /* [in] */ ULONG32 request,
    /* [optional][out] */ ULONG32 *done)
{
    // Not necessary yet.
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::GetTLSValue(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 index,
    /* [out] */ CLRDATA_ADDRESS* value)
{
    SUPPORTS_DAC;
    // Not necessary yet.
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::SetTLSValue(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 index,
    /* [in] */ CLRDATA_ADDRESS value)
{
    // Not necessary yet.
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::GetCurrentThreadID(
    /* [out] */ ULONG32* threadID)
{
    SUPPORTS_DAC;
    // Not necessary yet.
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::GetThreadContext(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 contextFlags,
    /* [in] */ ULONG32 contextSize,
    /* [out, size_is(contextSize)] */ PBYTE context)
{
    // Not necessary yet.
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::SetThreadContext(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 contextSize,
    /* [out, size_is(contextSize)] */ PBYTE context)
{
    // Not necessary yet.
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
LiveProcDataTarget::Request( 
    /* [in] */ ULONG32 reqCode,
    /* [in] */ ULONG32 inBufferSize,
    /* [size_is][in] */ BYTE *inBuffer,
    /* [in] */ ULONG32 outBufferSize,
    /* [size_is][out] */ BYTE *outBuffer)
{
    // None supported.
    return E_INVALIDARG;
}