-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvmEntry.cpp
440 lines (383 loc) · 15.6 KB
/
vmEntry.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright 2016 Andrei Pangin
* Copyright 2021, 2023 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include "vmEntry.h"
#include "arguments.h"
#include "context.h"
#include "j9Ext.h"
#include "j9ObjectSampler.h"
#include "os.h"
#include "profiler.h"
#include "safeAccess.h"
#include "log.h"
#include "vmStructs.h"
#include "jniHelper.h"
// JVM TI agent return codes
const int ARGUMENTS_ERROR = 100;
const int COMMAND_ERROR = 200;
static Arguments _agent_args(true);
JavaVM* VM::_vm;
jvmtiEnv* VM::_jvmti = NULL;
int VM::_java_version = 0;
int VM::_java_update_version = 0;
bool VM::_openj9 = false;
bool VM::_hotspot = false;
bool VM::_zing = false;
bool VM::_can_sample_objects = false;
bool VM::_can_intercept_binding = false;
bool VM::_is_adaptive_gc_boundary_flag_set = false;
jvmtiError (JNICALL *VM::_orig_RedefineClasses)(jvmtiEnv*, jint, const jvmtiClassDefinition*);
jvmtiError (JNICALL *VM::_orig_RetransformClasses)(jvmtiEnv*, jint, const jclass* classes);
void* VM::_libjvm;
void* VM::_libjava;
AsyncGetCallTrace VM::_asyncGetCallTrace;
JVM_GetManagement VM::_getManagement;
static void wakeupHandler(int signo) {
// Dummy handler for interrupting syscalls
}
static bool isZeroInterpreterMethod(const char* blob_name) {
return strncmp(blob_name, "_ZN15ZeroInterpreter", 20) == 0
|| strncmp(blob_name, "_ZN19BytecodeInterpreter3run", 28) == 0;
}
static bool isOpenJ9InterpreterMethod(const char* blob_name) {
return strncmp(blob_name, "_ZN32VM_BytecodeInterpreter", 27) == 0
|| strncmp(blob_name, "_ZN26VM_BytecodeInterpreter", 27) == 0
|| strncmp(blob_name, "bytecodeLoop", 12) == 0
|| strcmp(blob_name, "cInterpreter") == 0;
}
static bool isOpenJ9JitStub(const char* blob_name) {
if (strncmp(blob_name, "jit", 3) == 0) {
blob_name += 3;
return strcmp(blob_name, "NewObject") == 0
|| strcmp(blob_name, "NewArray") == 0
|| strcmp(blob_name, "ANewArray") == 0;
}
return false;
}
static void* resolveMethodId(void** mid) {
return mid == NULL || *mid < (void*)4096 ? NULL : *mid;
}
static void resolveMethodIdEnd() {
}
bool VM::init(JavaVM* vm, bool attach) {
if (_jvmti != NULL) return true;
_vm = vm;
if (_vm->GetEnv((void**)&_jvmti, JVMTI_VERSION_1_0) != 0) {
return false;
}
#ifdef __APPLE__
Dl_info dl_info;
if (dladdr((const void*)wakeupHandler, &dl_info) && dl_info.dli_fname != NULL) {
// Make sure java-profiler DSO cannot be unloaded, since it contains JVM callbacks.
// On Linux, we use 'nodelete' linker option.
dlopen(dl_info.dli_fname, RTLD_LAZY | RTLD_NODELETE);
}
#endif
bool is_zero_vm = false;
char* prop;
if (_jvmti->GetSystemProperty("java.vm.name", &prop) == 0) {
_hotspot = strstr(prop, "OpenJDK") != NULL ||
strstr(prop, "HotSpot") != NULL ||
strstr(prop, "GraalVM") != NULL ||
strstr(prop, "Dynamic Code Evolution") != NULL;
is_zero_vm = strstr(prop, "Zero") != NULL;
_zing = !_hotspot && strstr(prop, "Zing") != NULL;
_openj9 = !_hotspot && strstr(prop, "OpenJ9") != NULL;
_jvmti->Deallocate((unsigned char*)prop);
prop = NULL;
}
_libjvm = getLibraryHandle("libjvm.so");
_asyncGetCallTrace = (AsyncGetCallTrace)dlsym(_libjvm, "AsyncGetCallTrace");
_getManagement = (JVM_GetManagement)dlsym(_libjvm, "JVM_GetManagement");
Profiler* profiler = Profiler::instance();
profiler->updateSymbols(false);
_openj9 = !_hotspot && J9Ext::initialize(_jvmti, profiler->resolveSymbol("j9thread_self*"));
if (_openj9) {
if (_jvmti->GetSystemProperty("jdk.extensions.version", &prop) == 0) {
// OpenJ9 Semeru will report the version here
// insert debug output here
} else {
if (prop != NULL) {
_jvmti->Deallocate((unsigned char*)prop);
prop = NULL;
}
if (_jvmti->GetSystemProperty("java.fullversion", &prop) == 0) {
// IBM JDK 8 will report something here
// The reported string contains JRE 1.8.0 and then later year and (possibly) hash code
// Although not very precise, this is best we can get :/
// insert debug output here
} else {
if (prop != NULL) {
_jvmti->Deallocate((unsigned char*)prop);
prop = NULL;
}
}
}
}
if (prop == NULL) {
if (_jvmti->GetSystemProperty("java.vm.version", &prop) == 0) {
// insert debug output here
} else {
if (prop != NULL) {
_jvmti->Deallocate((unsigned char*)prop);
prop = NULL;
}
}
}
if (prop != NULL) {
if (strncmp(prop, "1.8.0", 5) == 0) {
_java_version = 8;
_java_update_version = atoi(prop + 5);
} else if (strncmp(prop, "8.0.", 4) == 0) {
_java_version = 8;
_java_update_version = atoi(prop + 4);
} else if (strncmp(prop, "JRE 1.8.0", 9) == 0) {
// IBM JDK 8 does not report the 'real' version in any property accessible from JVMTI
// The only piece of info we can use has the following format
// `JRE 1.8.0 <some text> 20230313_47323 <some more text>`
// Considering that JDK 8.0.361 is the only release in 2023 we can use that part of the
// version string to pretend anything after year 2023 inclusive is 8.0.361.
// Not perfect, but this is the only thing we have.
_java_version = 8;
char* idx = strstr(prop, " 202");
if (idx != NULL) {
int year = atol(idx + 1);
if (year >= 2023) {
_java_update_version = 361;
} else {
_java_update_version = 351;
}
}
} else {
_java_version = atoi(prop);
if (_java_version < 9) {
_java_version = 9;
}
// format is 11.0.17+8
// this shortcut for parsing the update version should hold till Java 99
_java_update_version = atoi(prop + 5);
}
_jvmti->Deallocate((unsigned char*)prop);
}
_can_sample_objects = !_hotspot || java_version() >= 11;
CodeCache* lib = isOpenJ9()
? profiler->findJvmLibrary("libj9vm")
: profiler->findLibraryByAddress((const void*)_asyncGetCallTrace);
if (lib == NULL) {
return false;
}
VMStructs::init(lib);
if (is_zero_vm) {
lib->mark(isZeroInterpreterMethod);
} else if (isOpenJ9()) {
lib->mark(isOpenJ9InterpreterMethod);
CodeCache* libjit = profiler->findJvmLibrary("libj9jit");
if (libjit != NULL) {
libjit->mark(isOpenJ9JitStub);
}
}
if (!attach && java_version() == 8 && OS::isLinux()) {
// Workaround for JDK-8185348
char* func = (char*)lib->findSymbol("_ZN6Method26checked_resolve_jmethod_idEP10_jmethodID");
if (func != NULL) {
applyPatch(func, (const char*)resolveMethodId, (const char*)resolveMethodIdEnd);
}
}
if (attach) {
ready(jvmti(), jni());
}
jvmtiCapabilities potential_capabilities = {0};
_jvmti->GetPotentialCapabilities(&potential_capabilities);
_can_sample_objects =
potential_capabilities.can_generate_sampled_object_alloc_events
&& (!_hotspot || java_version() >= 11);
_can_intercept_binding =
potential_capabilities.can_generate_native_method_bind_events && HeapUsage::needsNativeBindingInterception();
jvmtiCapabilities capabilities = {0};
capabilities.can_generate_all_class_hook_events = 1;
capabilities.can_retransform_classes = 1;
capabilities.can_retransform_any_class = isOpenJ9() ? 0 : 1;
// capabilities.can_generate_vm_object_alloc_events = isOpenJ9() ? 1 : 0;
capabilities.can_generate_sampled_object_alloc_events = _can_sample_objects ? 1 : 0;
capabilities.can_generate_native_method_bind_events = _can_intercept_binding ? 1 : 0;
capabilities.can_generate_garbage_collection_events = 1;
capabilities.can_get_bytecodes = 1;
capabilities.can_get_constant_pool = 1;
capabilities.can_get_source_file_name = 1;
capabilities.can_get_line_numbers = 1;
capabilities.can_generate_compiled_method_load_events = 1;
capabilities.can_generate_monitor_events = 1;
capabilities.can_tag_objects = 1;
_jvmti->AddCapabilities(&capabilities);
jvmtiEventCallbacks callbacks = {0};
callbacks.VMDeath = VMDeath;
callbacks.ClassLoad = ClassLoad;
callbacks.ClassPrepare = ClassPrepare;
callbacks.CompiledMethodLoad = Profiler::CompiledMethodLoad;
callbacks.DynamicCodeGenerated = Profiler::DynamicCodeGenerated;
callbacks.ThreadStart = Profiler::ThreadStart;
callbacks.ThreadEnd = Profiler::ThreadEnd;
// callbacks.VMObjectAlloc = J9ObjectSampler::VMObjectAlloc;
callbacks.SampledObjectAlloc = ObjectSampler::SampledObjectAlloc;
callbacks.GarbageCollectionFinish = LivenessTracker::GarbageCollectionFinish;
callbacks.NativeMethodBind = VMStructs::NativeMethodBind;
_jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, NULL);
if (java_version() == 0 || !CodeHeap::available()) {
// Workaround for JDK-8173361: avoid CompiledMethodLoad events when possible
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
} else {
// DebugNonSafepoints is automatically enabled with CompiledMethodLoad,
// otherwise we set the flag manually
char* flag_addr = (char*)JVMFlag::find("DebugNonSafepoints");
if (flag_addr != NULL) {
*flag_addr = 1;
}
}
// if the user sets -XX:+UseAdaptiveGCBoundary we will just disable the profiler to avoid the risk of crashing
// flag was made obsolete (inert) in 15 (see JDK-8228991) and removed in 16 (see JDK-8231560)
if (java_version() < 15) {
char* flag_addr = (char*)JVMFlag::find("UseAdaptiveGCBoundary");
_is_adaptive_gc_boundary_flag_set = flag_addr != NULL && *flag_addr == 1;
}
if (attach) {
loadAllMethodIDs(jvmti(), jni());
_jvmti->GenerateEvents(JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
_jvmti->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD);
} else {
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
}
OS::installSignalHandler(WAKEUP_SIGNAL, NULL, wakeupHandler);
return true;
}
// Run late initialization when JVM is ready
void VM::ready(jvmtiEnv* jvmti, JNIEnv* jni) {
Profiler::setupSignalHandlers();
{
JitWriteProtection jit(true);
VMStructs::ready();
}
_libjava = getLibraryHandle("libjava.so");
// Make sure we reload method IDs upon class retransformation
JVMTIFunctions* functions = *(JVMTIFunctions**)_jvmti;
_orig_RedefineClasses = functions->RedefineClasses;
_orig_RetransformClasses = functions->RetransformClasses;
functions->RedefineClasses = RedefineClassesHook;
functions->RetransformClasses = RetransformClassesHook;
}
void VM::applyPatch(char* func, const char* patch, const char* end_patch) {
size_t size = end_patch - patch;
uintptr_t start_page = (uintptr_t)func & ~OS::page_mask;
uintptr_t end_page = ((uintptr_t)func + size + OS::page_mask) & ~OS::page_mask;
if (mprotect((void*)start_page, end_page - start_page, PROT_READ | PROT_WRITE | PROT_EXEC) == 0) {
memcpy(func, patch, size);
__builtin___clear_cache(func, func + size);
mprotect((void*)start_page, end_page - start_page, PROT_READ | PROT_EXEC);
}
}
void* VM::getLibraryHandle(const char* name) {
if (OS::isLinux()) {
void* handle = dlopen(name, RTLD_LAZY);
if (handle != NULL) {
return handle;
}
Log::warn("Failed to load %s: %s", name, dlerror());
}
// JVM symbols are globally visible on macOS
return RTLD_DEFAULT;
}
void VM::loadMethodIDs(jvmtiEnv* jvmti, JNIEnv* jni, jclass klass) {
if (VMStructs::hasClassLoaderData()) {
VMKlass* vmklass = VMKlass::fromJavaClass(jni, klass);
int method_count = vmklass->methodCount();
if (method_count > 0) {
ClassLoaderData* cld = vmklass->classLoaderData();
cld->lock();
// Workaround for JVM bug: preallocate space for jmethodIDs
// at the beginning of the list (rather than at the end)
for (int i = 0; i < method_count; i += MethodList::SIZE) {
*cld->methodList() = new MethodList(*cld->methodList());
}
cld->unlock();
}
}
jint method_count;
jmethodID* methods;
if (jvmti->GetClassMethods(klass, &method_count, &methods) == 0) {
jvmti->Deallocate((unsigned char*)methods);
}
}
void VM::loadAllMethodIDs(jvmtiEnv* jvmti, JNIEnv* jni) {
jint class_count;
jclass* classes;
if (jvmti->GetLoadedClasses(&class_count, &classes) == 0) {
for (int i = 0; i < class_count; i++) {
loadMethodIDs(jvmti, jni, classes[i]);
}
jvmti->Deallocate((unsigned char*)classes);
}
}
void JNICALL VM::VMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
Profiler::instance()->shutdown(_agent_args);
}
jvmtiError VM::RedefineClassesHook(jvmtiEnv* jvmti, jint class_count, const jvmtiClassDefinition* class_definitions) {
jvmtiError result = _orig_RedefineClasses(jvmti, class_count, class_definitions);
if (result == 0) {
// jmethodIDs are invalidated after RedefineClasses
JNIEnv* env = jni();
for (int i = 0; i < class_count; i++) {
if (class_definitions[i].klass != NULL) {
loadMethodIDs(jvmti, env, class_definitions[i].klass);
}
}
}
return result;
}
jvmtiError VM::RetransformClassesHook(jvmtiEnv* jvmti, jint class_count, const jclass* classes) {
jvmtiError result = _orig_RetransformClasses(jvmti, class_count, classes);
if (result == 0) {
// jmethodIDs are invalidated after RetransformClasses
JNIEnv* env = jni();
for (int i = 0; i < class_count; i++) {
if (classes[i] != NULL) {
loadMethodIDs(jvmti, env, classes[i]);
}
}
}
return result;
}
extern "C" DLLEXPORT jint JNICALL
JNI_OnLoad(JavaVM* vm, void* reserved) {
if (!VM::init(vm, true)) {
return 0;
}
return JNI_VERSION_1_6;
}
extern "C" DLLEXPORT void JNICALL
JNI_OnUnload(JavaVM* vm, void* reserved) {
Profiler* profiler = Profiler::instance();
if (profiler != NULL) {
profiler->stop();
}
}