summaryrefslogtreecommitdiff
path: root/src/debug/createdump/datatarget.cpp
blob: 9609fa30a791d2ad461365545dc966c455edd46e (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// 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.

#include "createdump.h"

#define IMAGE_FILE_MACHINE_AMD64             0x8664  // AMD64 (K8)

DumpDataTarget::DumpDataTarget(pid_t pid) :
    m_ref(1),
    m_pid(pid),
    m_fd(-1),
    m_crashInfo(nullptr)
{
}

DumpDataTarget::~DumpDataTarget()
{
    if (m_fd != -1)
    {
        close(m_fd);
        m_fd = -1;
    }
}

bool
DumpDataTarget::Initialize(CrashInfo * crashInfo)
{
    char memPath[128];
    _snprintf_s(memPath, sizeof(memPath), sizeof(memPath), "/proc/%lu/mem", m_pid);

    m_fd = open(memPath, O_RDONLY);
    if (m_fd == -1)
    {
        fprintf(stderr, "open(%s) FAILED %d (%s)\n", memPath, errno, strerror(errno));
        return false;
    }
    m_crashInfo = crashInfo;
    return true;
}

STDMETHODIMP
DumpDataTarget::QueryInterface(
    ___in REFIID InterfaceId,
    ___out PVOID* Interface
    )
{
    if (InterfaceId == IID_IUnknown ||
        InterfaceId == IID_ICLRDataTarget)
    {
        *Interface = (ICLRDataTarget*)this;
        AddRef();
        return S_OK;
    }
    else if (InterfaceId == IID_ICorDebugDataTarget4)
    {
        *Interface = (ICorDebugDataTarget4*)this;
        AddRef();
        return S_OK;
    }
    else
    {
        *Interface = NULL;
        return E_NOINTERFACE;
    }
}

STDMETHODIMP_(ULONG)
DumpDataTarget::AddRef()
{
    LONG ref = InterlockedIncrement(&m_ref);    
    return ref;
}

STDMETHODIMP_(ULONG)
DumpDataTarget::Release()
{
    LONG ref = InterlockedDecrement(&m_ref);
    if (ref == 0)
    {
        delete this;
    }
    return ref;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::GetMachineType(
    /* [out] */ ULONG32 *machine)
{
#ifdef _AMD64_
    *machine = IMAGE_FILE_MACHINE_AMD64;
#elif _ARM_
    *machine = IMAGE_FILE_MACHINE_ARMNT;
#elif _ARM64_
    *machine = IMAGE_FILE_MACHINE_ARM64;
#elif _X86_
    *machine = IMAGE_FILE_MACHINE_I386;
#else
#error Unsupported architecture
#endif
    return S_OK;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::GetPointerSize(
    /* [out] */ ULONG32 *size)
{
#if defined(_AMD64_) || defined(_ARM64_)
    *size = 8;
#elif defined(_ARM_) || defined(_X86_)
    *size = 4;
#else
#error Unsupported architecture
#endif
    return S_OK;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::GetImageBase(
    /* [string][in] */ LPCWSTR moduleName,
    /* [out] */ CLRDATA_ADDRESS *baseAddress)
{
    assert(m_crashInfo != nullptr);
    *baseAddress = 0;

    char tempModuleName[MAX_PATH];
    int length = WideCharToMultiByte(CP_ACP, 0, moduleName, -1, tempModuleName, sizeof(tempModuleName), NULL, NULL);
    if (length > 0)
    {
        for (const MemoryRegion& image : m_crashInfo->ModuleMappings()) 
        {
            const char *name = strrchr(image.FileName(), '/');
            if (name != nullptr)
            {
                name++;
            }
            else
            {
                name = image.FileName();
            }
            if (strcmp(name, tempModuleName) == 0)
            {
                *baseAddress = image.StartAddress();
                return S_OK;
            }
        }
    }
    return E_FAIL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::ReadVirtual(
    /* [in] */ CLRDATA_ADDRESS address,
    /* [length_is][size_is][out] */ PBYTE buffer,
    /* [in] */ ULONG32 size,
    /* [optional][out] */ ULONG32 *done)
{
    assert(m_fd != -1);
    size_t read = pread64(m_fd, buffer, size, (off64_t)address);
    if (read == -1)
    {
        *done = 0;
        return E_FAIL;
    }
    *done = read;
    return S_OK;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::WriteVirtual(
    /* [in] */ CLRDATA_ADDRESS address,
    /* [size_is][in] */ PBYTE buffer,
    /* [in] */ ULONG32 size,
    /* [optional][out] */ ULONG32 *done)
{
    assert(false);
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::GetTLSValue(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 index,
    /* [out] */ CLRDATA_ADDRESS* value)
{
    assert(false);
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::SetTLSValue(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 index,
    /* [in] */ CLRDATA_ADDRESS value)
{
    assert(false);
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::GetCurrentThreadID(
    /* [out] */ ULONG32* threadID)
{
    assert(false);
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::GetThreadContext(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 contextFlags,
    /* [in] */ ULONG32 contextSize,
    /* [out, size_is(contextSize)] */ PBYTE context)
{
    assert(m_crashInfo != nullptr);
    if (contextSize < sizeof(CONTEXT)) 
    {
        assert(false);
        return E_INVALIDARG;
    }
    memset(context, 0, contextSize);
    for (const ThreadInfo* thread : m_crashInfo->Threads())
    {
        if (thread->Tid() == threadID)
        {
            thread->GetThreadContext(contextFlags, reinterpret_cast<CONTEXT*>(context));
            return S_OK;
        }
    }
    return E_FAIL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::SetThreadContext(
    /* [in] */ ULONG32 threadID,
    /* [in] */ ULONG32 contextSize,
    /* [out, size_is(contextSize)] */ PBYTE context)
{
    assert(false);
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE
DumpDataTarget::Request(
    /* [in] */ ULONG32 reqCode,
    /* [in] */ ULONG32 inBufferSize,
    /* [size_is][in] */ BYTE *inBuffer,
    /* [in] */ ULONG32 outBufferSize,
    /* [size_is][out] */ BYTE *outBuffer)
{
    assert(false);
    return E_NOTIMPL;
}

HRESULT STDMETHODCALLTYPE 
DumpDataTarget::VirtualUnwind(
    /* [in] */ DWORD threadId,
    /* [in] */ ULONG32 contextSize,
    /* [in, out, size_is(contextSize)] */ PBYTE context)
{
    return E_NOTIMPL;
}