forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatedumppal.cpp
248 lines (220 loc) · 6.22 KB
/
createdumppal.cpp
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.
#include "createdump.h"
#include <time.h>
#include <sys/time.h>
#define INITGUID
#include <guiddef.h>
DEFINE_GUID(IID_IUnknown, 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
#if defined(__linux__)
#define PAL_FUNCTION_PREFIX "DAC_"
#else
#define PAL_FUNCTION_PREFIX
#endif
typedef int (*PFN_PAL_InitializeDLL)();
typedef void (*PFN_PAL_TerminateEx)(int);
typedef BOOL (*PFN_PAL_VirtualUnwindOutOfProc)(
CONTEXT *context,
KNONVOLATILE_CONTEXT_POINTERS *contextPointers,
PULONG64 functionStart,
SIZE_T baseAddress,
UnwindReadMemoryCallback readMemoryCallback);
typedef BOOL (*PFN_PAL_GetUnwindInfoSize)(
SIZE_T baseAddress,
ULONG64 ehFrameHdrAddr,
UnwindReadMemoryCallback readMemoryCallback,
PULONG64 ehFrameStart,
PULONG64 ehFrameSize);
bool g_initialized = false;
PFN_PAL_InitializeDLL g_PAL_InitializeDLL = nullptr;
PFN_PAL_TerminateEx g_PAL_TerminateEx = nullptr;
PFN_PAL_VirtualUnwindOutOfProc g_PAL_VirtualUnwindOutOfProc = nullptr;
PFN_PAL_GetUnwindInfoSize g_PAL_GetUnwindInfoSize = nullptr;
bool
InitializePAL()
{
if (g_initialized)
{
return true;
}
g_initialized = true;
// Get createdump's path and load the DAC next to it.
Dl_info info;
if (dladdr((PVOID)&InitializePAL, &info) == 0)
{
printf_error("InitializePAL: dladdr(&InitializePAL) FAILED %s\n", dlerror());
return false;
}
std::string dacPath;
dacPath.append(info.dli_fname);
dacPath = GetDirectory(dacPath);
dacPath.append(MAKEDLLNAME_A("mscordaccore"));
// Load the DAC next to createdump
void* dacModule = dlopen(dacPath.c_str(), RTLD_LAZY);
if (dacModule == nullptr)
{
printf_error("InitializePAL: dlopen(%s) FAILED %s\n", dacPath.c_str(), dlerror());
return false;
}
// Get the PAL entry points needed by createdump
g_PAL_InitializeDLL = (PFN_PAL_InitializeDLL)dlsym(dacModule, PAL_FUNCTION_PREFIX "PAL_InitializeDLL");
if (g_PAL_InitializeDLL == nullptr)
{
printf_error("InitializePAL: dlsym(PAL_InitializeDLL) FAILED %s\n", dlerror());
return false;
}
if (g_PAL_InitializeDLL() != 0)
{
printf_error("InitializePAL: PAL initialization FAILED\n");
return false;
}
g_PAL_TerminateEx = (PFN_PAL_TerminateEx)dlsym(dacModule, PAL_FUNCTION_PREFIX "PAL_TerminateEx");
g_PAL_VirtualUnwindOutOfProc = (PFN_PAL_VirtualUnwindOutOfProc)dlsym(dacModule, PAL_FUNCTION_PREFIX "PAL_VirtualUnwindOutOfProc");
g_PAL_GetUnwindInfoSize = (PFN_PAL_GetUnwindInfoSize)dlsym(dacModule, PAL_FUNCTION_PREFIX "PAL_GetUnwindInfoSize");
return true;
}
void
UninitializePAL(
int exitCode)
{
if (g_PAL_TerminateEx != nullptr)
{
g_PAL_TerminateEx(exitCode);
}
}
#define tccSecondsToNanoSeconds 1000000000 // 10^9
BOOL
PALAPI
QueryPerformanceCounter(
OUT LARGE_INTEGER* lpPerformanceCount)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
lpPerformanceCount->QuadPart = (LONGLONG)clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#elif HAVE_CLOCK_MONOTONIC
struct timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
if (result != 0)
{
return TRUE;
}
else
{
lpPerformanceCount->QuadPart = ((LONGLONG)(ts.tv_sec) * (LONGLONG)(tccSecondsToNanoSeconds)) + (LONGLONG)(ts.tv_nsec);
}
#else
#error "The createdump requires either mach_absolute_time() or clock_gettime(CLOCK_MONOTONIC) to be supported."
#endif
return TRUE;
}
BOOL
PALAPI
QueryPerformanceFrequency(
OUT LARGE_INTEGER* lpFrequency)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
lpFrequency->QuadPart = (LONGLONG)(tccSecondsToNanoSeconds);
#elif HAVE_CLOCK_MONOTONIC
// clock_gettime() returns a result in terms of nanoseconds rather than a count. This
// means that we need to either always scale the result by the actual resolution (to
// get a count) or we need to say the resolution is in terms of nanoseconds. We prefer
// the latter since it allows the highest throughput and should minimize error propagated
// to the user.
lpFrequency->QuadPart = (LONGLONG)(tccSecondsToNanoSeconds);
#else
#error "The createdump requires either mach_absolute_time() or clock_gettime(CLOCK_MONOTONIC) to be supported."
#endif
return TRUE;
}
#define TEMP_DIRECTORY_PATH "/tmp/"
DWORD
PALAPI
GetTempPathA(
IN DWORD nBufferLength,
OUT LPSTR lpBuffer)
{
DWORD dwPathLen = 0;
const char *tempDir = getenv("TMPDIR");
if (tempDir == nullptr)
{
tempDir = TEMP_DIRECTORY_PATH;
}
size_t tempDirLen = strlen(tempDir);
if (tempDirLen < nBufferLength)
{
dwPathLen = tempDirLen;
strcpy_s(lpBuffer, nBufferLength, tempDir);
}
else
{
// Get the required length
dwPathLen = tempDirLen + 1;
}
return dwPathLen;
}
BOOL
PALAPI
PAL_VirtualUnwindOutOfProc(
CONTEXT *context,
KNONVOLATILE_CONTEXT_POINTERS *contextPointers,
PULONG64 functionStart,
SIZE_T baseAddress,
UnwindReadMemoryCallback readMemoryCallback)
{
if (!InitializePAL() || g_PAL_VirtualUnwindOutOfProc == nullptr)
{
return FALSE;
}
return g_PAL_VirtualUnwindOutOfProc(context, contextPointers, functionStart, baseAddress, readMemoryCallback);
}
BOOL
PALAPI
PAL_GetUnwindInfoSize(
SIZE_T baseAddress,
ULONG64 ehFrameHdrAddr,
UnwindReadMemoryCallback readMemoryCallback,
PULONG64 ehFrameStart,
PULONG64 ehFrameSize)
{
if (!InitializePAL() || g_PAL_GetUnwindInfoSize == nullptr)
{
return FALSE;
}
return g_PAL_GetUnwindInfoSize(baseAddress, ehFrameHdrAddr, readMemoryCallback, ehFrameStart, ehFrameSize);
}
//
// Used in pal\inc\rt\safecrt.h's _invalid_parameter handler
//
VOID
PALAPI
RaiseException(
IN DWORD dwExceptionCode,
IN DWORD dwExceptionFlags,
IN DWORD nNumberOfArguments,
IN CONST ULONG_PTR* lpArguments)
{
throw;
}
size_t u16_strlen(const WCHAR* str)
{
size_t nChar = 0;
while (*str++)
nChar++;
return nChar;
}
//
// Used by _ASSERTE
//
#ifdef _DEBUG
DWORD
PALAPI
GetCurrentProcessId()
{
return getpid();
}
VOID
PALAPI
DebugBreak()
{
abort();
}
#endif // DEBUG