-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
clrgc.enabled.cpp
199 lines (173 loc) · 5.8 KB
/
clrgc.enabled.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "common.h"
#define SKIP_TRACING_DEFINITIONS
#include "gcenv.h"
#undef SKIP_TRACING_DEFINITIONS
#include "gcheaputilities.h"
#include "gchandleutilities.h"
#include "gceventstatus.h"
#include "holder.h"
#include "RhConfig.h"
#include "gctoeeinterface.standalone.inl"
enum GC_LOAD_STATUS {
GC_LOAD_STATUS_BEFORE_START,
GC_LOAD_STATUS_START,
GC_LOAD_STATUS_DONE_LOAD,
GC_LOAD_STATUS_GET_VERSIONINFO,
GC_LOAD_STATUS_CALL_VERSIONINFO,
GC_LOAD_STATUS_DONE_VERSION_CHECK,
GC_LOAD_STATUS_GET_INITIALIZE,
GC_LOAD_STATUS_LOAD_COMPLETE
};
// Load status of the GC. If GC loading fails, the value of this
// global indicates where the failure occurred.
GC_LOAD_STATUS g_gc_load_status = GC_LOAD_STATUS_BEFORE_START;
// The version of the GC that we have loaded.
VersionInfo g_gc_version_info;
CrstStatic g_eventStashLock;
GCEventLevel g_stashedLevel = GCEventLevel_None;
GCEventKeyword g_stashedKeyword = GCEventKeyword_None;
GCEventLevel g_stashedPrivateLevel = GCEventLevel_None;
GCEventKeyword g_stashedPrivateKeyword = GCEventKeyword_None;
BOOL g_gcEventTracingInitialized = FALSE;
void InitializeGCEventLock()
{
g_eventStashLock.InitNoThrow(CrstGcEvent);
}
HRESULT InitializeStandaloneGC();
HRESULT InitializeGCSelector()
{
return InitializeStandaloneGC();
}
HRESULT InitializeStandaloneGC()
{
return GCHeapUtilities::InitializeStandaloneGC();
}
HRESULT GCHeapUtilities::InitializeStandaloneGC()
{
char* moduleName;
if (!RhConfig::Environment::TryGetStringValue("GCName", &moduleName))
{
return GCHeapUtilities::InitializeDefaultGC();
}
NewArrayHolder<char> moduleNameHolder(moduleName);
HANDLE executableModule = PalGetModuleHandleFromPointer((void*)&PalGetModuleHandleFromPointer);
const TCHAR * executableModulePath = NULL;
PalGetModuleFileName(&executableModulePath, executableModule);
char* convertedExecutableModulePath = PalCopyTCharAsChar(executableModulePath);
if (!convertedExecutableModulePath)
{
return E_OUTOFMEMORY;
}
NewArrayHolder<char> convertedExecutableModulePathHolder(convertedExecutableModulePath);
{
char* p = convertedExecutableModulePath;
char* q = nullptr;
while (*p != '\0')
{
if (*p == DIRECTORY_SEPARATOR_CHAR)
{
q = p;
}
p++;
}
assert(q != nullptr);
q++;
*q = '\0';
}
size_t folderLength = strlen(convertedExecutableModulePath);
size_t nameLength = strlen(moduleName);
char* moduleFullPath = new (nothrow) char[folderLength + nameLength + 1];
if (!moduleFullPath)
{
return E_OUTOFMEMORY;
}
NewArrayHolder<char> moduleFullPathHolder(moduleFullPath);
strcpy(moduleFullPath, convertedExecutableModulePath);
strcpy(moduleFullPath + folderLength, moduleName);
HANDLE hMod = PalLoadLibrary(moduleFullPath);
if (!hMod)
{
LOG((LF_GC, LL_FATALERROR, "GC initialization failed to load the Standalone GC library.\n"));
return E_FAIL;
}
IGCToCLR* gcToClr = new (nothrow) standalone::GCToEEInterface();
if (!gcToClr)
{
return E_OUTOFMEMORY;
}
GC_VersionInfoFunction versionInfo = (GC_VersionInfoFunction)PalGetProcAddress(hMod, "GC_VersionInfo");
if (!versionInfo)
{
LOG((LF_GC, LL_FATALERROR, "GC initialization failed with the GC_VersionInfo function not found.\n"));
return E_FAIL;
}
g_gc_load_status = GC_LOAD_STATUS_GET_VERSIONINFO;
g_gc_version_info.MajorVersion = EE_INTERFACE_MAJOR_VERSION;
g_gc_version_info.MinorVersion = 0;
g_gc_version_info.BuildVersion = 0;
versionInfo(&g_gc_version_info);
g_gc_load_status = GC_LOAD_STATUS_CALL_VERSIONINFO;
if (g_gc_version_info.MajorVersion < GC_INTERFACE_MAJOR_VERSION)
{
LOG((LF_GC, LL_FATALERROR, "GC initialization failed with the Standalone GC reported a major version lower than what the runtime requires.\n"));
return E_FAIL;
}
GC_InitializeFunction initFunc = (GC_InitializeFunction)PalGetProcAddress(hMod, "GC_Initialize");
if (!initFunc)
{
LOG((LF_GC, LL_FATALERROR, "GC initialization failed with the GC_Initialize function not found.\n"));
return E_FAIL;
}
g_gc_load_status = GC_LOAD_STATUS_GET_INITIALIZE;
IGCHeap* heap;
IGCHandleManager* manager;
HRESULT initResult = initFunc(gcToClr, &heap, &manager, &g_gc_dac_vars);
if (initResult == S_OK)
{
g_pGCHeap = heap;
{
CrstHolder lh(&g_eventStashLock);
g_pGCHeap->ControlEvents(g_stashedKeyword, g_stashedLevel);
g_pGCHeap->ControlPrivateEvents(g_stashedPrivateKeyword, g_stashedPrivateLevel);
g_gcEventTracingInitialized = TRUE;
}
g_pGCHandleManager = manager;
g_gcDacGlobals = &g_gc_dac_vars;
LOG((LF_GC, LL_INFO100, "GC load successful\n"));
}
else
{
LOG((LF_GC, LL_FATALERROR, "GC initialization failed with HR = 0x%X\n", initResult));
}
return initResult;
}
void GCHeapUtilities::RecordEventStateChange(bool isPublicProvider, GCEventKeyword keywords, GCEventLevel level)
{
CrstHolder lh(&g_eventStashLock);
if (g_gcEventTracingInitialized)
{
if (isPublicProvider)
{
g_pGCHeap->ControlEvents(keywords, level);
}
else
{
g_pGCHeap->ControlPrivateEvents(keywords, level);
}
}
else
{
if (isPublicProvider)
{
g_stashedKeyword = keywords;
g_stashedLevel = level;
}
else
{
g_stashedPrivateKeyword = keywords;
g_stashedPrivateLevel = level;
}
}
}