2
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
26
#include "classfile/classLoaderExt.hpp"
27
#include "classfile/javaClasses.inline.hpp"
28
#include "classfile/stringTable.hpp"
29
#include "classfile/modules.hpp"
30
#include "classfile/systemDictionary.hpp"
31
#include "classfile/vmClasses.hpp"
32
#include "classfile/vmSymbols.hpp"
33
#include "gc/shared/collectedHeap.hpp"
34
#include "interpreter/bytecodeStream.hpp"
35
#include "interpreter/interpreter.hpp"
36
#include "jfr/jfrEvents.hpp"
37
#include "jvmtifiles/jvmtiEnv.hpp"
38
#include "logging/log.hpp"
39
#include "logging/logConfiguration.hpp"
40
#include "memory/allocation.hpp"
41
#include "memory/resourceArea.hpp"
42
#include "memory/universe.hpp"
43
#include "oops/instanceKlass.hpp"
44
#include "oops/klass.inline.hpp"
45
#include "oops/objArrayOop.inline.hpp"
46
#include "oops/oop.inline.hpp"
47
#include "prims/jniCheck.hpp"
48
#include "prims/jvm_misc.hpp"
49
#include "prims/jvmtiAgentThread.hpp"
50
#include "prims/jvmtiClassFileReconstituter.hpp"
51
#include "prims/jvmtiCodeBlobEvents.hpp"
52
#include "prims/jvmtiExtensions.hpp"
53
#include "prims/jvmtiGetLoadedClasses.hpp"
54
#include "prims/jvmtiImpl.hpp"
55
#include "prims/jvmtiManageCapabilities.hpp"
56
#include "prims/jvmtiRawMonitor.hpp"
57
#include "prims/jvmtiRedefineClasses.hpp"
58
#include "prims/jvmtiTagMap.hpp"
59
#include "prims/jvmtiThreadState.inline.hpp"
60
#include "prims/jvmtiUtil.hpp"
61
#include "runtime/arguments.hpp"
62
#include "runtime/deoptimization.hpp"
63
#include "runtime/fieldDescriptor.inline.hpp"
64
#include "runtime/handles.inline.hpp"
65
#include "runtime/interfaceSupport.inline.hpp"
66
#include "runtime/javaCalls.hpp"
67
#include "runtime/javaThread.inline.hpp"
68
#include "runtime/jfieldIDWorkaround.hpp"
69
#include "runtime/jniHandles.inline.hpp"
70
#include "runtime/objectMonitor.inline.hpp"
71
#include "runtime/os.hpp"
72
#include "runtime/osThread.hpp"
73
#include "runtime/reflectionUtils.hpp"
74
#include "runtime/signature.hpp"
75
#include "runtime/threadHeapSampler.hpp"
76
#include "runtime/threads.hpp"
77
#include "runtime/threadSMR.hpp"
78
#include "runtime/timerTrace.hpp"
79
#include "runtime/vframe.inline.hpp"
80
#include "runtime/vmThread.hpp"
81
#include "services/threadService.hpp"
82
#include "utilities/exceptions.hpp"
83
#include "utilities/preserveException.hpp"
84
#include "utilities/utf8.hpp"
87
#define FIXLATER 0 // REMOVE this when completed.
89
// FIXLATER: hook into JvmtiTrace
90
#define TraceJVMTICalls false
92
JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
95
JvmtiEnv::~JvmtiEnv() {
99
JvmtiEnv::create_a_jvmti(jint version) {
100
return new JvmtiEnv(version);
103
// VM operation class to copy jni function table at safepoint.
104
// More than one java threads or jvmti agents may be reading/
105
// modifying jni function tables. To reduce the risk of bad
106
// interaction b/w these threads it is copied at safepoint.
107
class VM_JNIFunctionTableCopier : public VM_Operation {
109
const struct JNINativeInterface_ *_function_table;
111
VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
112
_function_table = func_tbl;
115
VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
117
copy_jni_function_table(_function_table);
122
// Do not change the "prefix" marker below, everything above it is copied
123
// unchanged into the filled stub, everything below is controlled by the
124
// stub filler (only method bodies are carried forward, and then only for
125
// functionality still in the spec).
130
// Memory Management functions
133
// mem_ptr - pre-checked for null
135
JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
136
return allocate(size, mem_ptr);
140
// mem - null is a valid value, must be checked
142
JvmtiEnv::Deallocate(unsigned char* mem) {
143
return deallocate(mem);
144
} /* end Deallocate */
146
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
147
// data - null is a valid value, must be checked
149
JvmtiEnv::SetThreadLocalStorage(jthread thread, const void* data) {
150
JavaThread* current = JavaThread::current();
151
JvmtiThreadState* state = nullptr;
152
JvmtiVTMSTransitionDisabler disabler(thread);
153
ThreadsListHandle tlh(current);
155
JavaThread* java_thread = nullptr;
156
oop thread_obj = nullptr;
157
if (thread == nullptr) {
158
java_thread = current;
159
state = java_thread->jvmti_thread_state();
161
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
162
if (err != JVMTI_ERROR_NONE) {
165
state = java_lang_Thread::jvmti_thread_state(thread_obj);
167
if (state == nullptr) {
168
if (data == nullptr) {
169
// leaving state unset same as data set to null
170
return JVMTI_ERROR_NONE;
172
// otherwise, create the state
173
HandleMark hm(current);
174
Handle thread_handle(current, thread_obj);
175
state = JvmtiThreadState::state_for(java_thread, thread_handle);
176
if (state == nullptr) {
177
return JVMTI_ERROR_THREAD_NOT_ALIVE;
180
state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
181
return JVMTI_ERROR_NONE;
182
} /* end SetThreadLocalStorage */
185
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
186
// data_ptr - pre-checked for null
188
JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
189
JavaThread* current_thread = JavaThread::current();
190
if (thread == nullptr) {
191
JvmtiThreadState* state = current_thread->jvmti_thread_state();
192
*data_ptr = (state == nullptr) ? nullptr :
193
state->env_thread_state(this)->get_agent_thread_local_storage_data();
195
// jvmti_GetThreadLocalStorage is "in native" and doesn't transition
196
// the thread to _thread_in_vm. However, when the TLS for a thread
197
// other than the current thread is required we need to transition
198
// from native so as to resolve the jthread.
200
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
201
ThreadInVMfromNative __tiv(current_thread);
202
VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
203
debug_only(VMNativeEntryWrapper __vew;)
205
JvmtiVTMSTransitionDisabler disabler(thread);
206
ThreadsListHandle tlh(current_thread);
208
JavaThread* java_thread = nullptr;
209
oop thread_obj = nullptr;
210
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
211
if (err != JVMTI_ERROR_NONE) {
215
HandleMark hm(current_thread);
216
Handle thread_handle(current_thread, thread_obj);
217
JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread, thread_handle);
218
*data_ptr = (state == nullptr) ? nullptr :
219
state->env_thread_state(this)->get_agent_thread_local_storage_data();
221
return JVMTI_ERROR_NONE;
222
} /* end GetThreadLocalStorage */
228
// module_count_ptr - pre-checked for null
229
// modules_ptr - pre-checked for null
231
JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
232
JvmtiModuleClosure jmc;
234
return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
235
} /* end GetAllModules */
238
// class_loader - null is a valid value, must be pre-checked
239
// package_name - pre-checked for null
240
// module_ptr - pre-checked for null
242
JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
243
JavaThread* THREAD = JavaThread::current(); // For exception macros.
244
ResourceMark rm(THREAD);
246
Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
247
// Check that loader is a subclass of java.lang.ClassLoader.
248
if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
249
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
251
oop module = Modules::get_named_module(h_loader, package_name);
252
*module_ptr = module != nullptr ? JNIHandles::make_local(THREAD, module) : nullptr;
253
return JVMTI_ERROR_NONE;
254
} /* end GetNamedModule */
257
// module - pre-checked for null
258
// to_module - pre-checked for null
260
JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
261
JavaThread* THREAD = JavaThread::current(); // For exception macros.
264
Handle h_module(THREAD, JNIHandles::resolve(module));
265
if (!java_lang_Module::is_instance(h_module())) {
266
return JVMTI_ERROR_INVALID_MODULE;
269
Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
270
if (!java_lang_Module::is_instance(h_to_module())) {
271
return JVMTI_ERROR_INVALID_MODULE;
273
return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
274
} /* end AddModuleReads */
277
// module - pre-checked for null
278
// pkg_name - pre-checked for null
279
// to_module - pre-checked for null
281
JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
282
JavaThread* THREAD = JavaThread::current(); // For exception macros.
283
Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
286
Handle h_module(THREAD, JNIHandles::resolve(module));
287
if (!java_lang_Module::is_instance(h_module())) {
288
return JVMTI_ERROR_INVALID_MODULE;
291
Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
292
if (!java_lang_Module::is_instance(h_to_module())) {
293
return JVMTI_ERROR_INVALID_MODULE;
295
return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
296
} /* end AddModuleExports */
299
// module - pre-checked for null
300
// pkg_name - pre-checked for null
301
// to_module - pre-checked for null
303
JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
304
JavaThread* THREAD = JavaThread::current(); // For exception macros.
305
Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
308
Handle h_module(THREAD, JNIHandles::resolve(module));
309
if (!java_lang_Module::is_instance(h_module())) {
310
return JVMTI_ERROR_INVALID_MODULE;
313
Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
314
if (!java_lang_Module::is_instance(h_to_module())) {
315
return JVMTI_ERROR_INVALID_MODULE;
317
return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
318
} /* end AddModuleOpens */
321
// module - pre-checked for null
322
// service - pre-checked for null
324
JvmtiEnv::AddModuleUses(jobject module, jclass service) {
325
JavaThread* THREAD = JavaThread::current(); // For exception macros.
328
Handle h_module(THREAD, JNIHandles::resolve(module));
329
if (!java_lang_Module::is_instance(h_module())) {
330
return JVMTI_ERROR_INVALID_MODULE;
333
Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
334
if (!java_lang_Class::is_instance(h_service()) ||
335
java_lang_Class::is_primitive(h_service())) {
336
return JVMTI_ERROR_INVALID_CLASS;
338
return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
339
} /* end AddModuleUses */
342
// module - pre-checked for null
343
// service - pre-checked for null
344
// impl_class - pre-checked for null
346
JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
347
JavaThread* THREAD = JavaThread::current(); // For exception macros.
350
Handle h_module(THREAD, JNIHandles::resolve(module));
351
if (!java_lang_Module::is_instance(h_module())) {
352
return JVMTI_ERROR_INVALID_MODULE;
355
Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
356
if (!java_lang_Class::is_instance(h_service()) ||
357
java_lang_Class::is_primitive(h_service())) {
358
return JVMTI_ERROR_INVALID_CLASS;
361
Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
362
if (!java_lang_Class::is_instance(h_impl_class()) ||
363
java_lang_Class::is_primitive(h_impl_class())) {
364
return JVMTI_ERROR_INVALID_CLASS;
366
return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
367
} /* end AddModuleProvides */
369
// module - pre-checked for null
370
// is_modifiable_class_ptr - pre-checked for null
372
JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
373
JavaThread* current = JavaThread::current();
376
Handle h_module(current, JNIHandles::resolve(module));
377
if (!java_lang_Module::is_instance(h_module())) {
378
return JVMTI_ERROR_INVALID_MODULE;
381
*is_modifiable_module_ptr = JNI_TRUE;
382
return JVMTI_ERROR_NONE;
383
} /* end IsModifiableModule */
390
// class_count_ptr - pre-checked for null
391
// classes_ptr - pre-checked for null
393
JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
394
return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
395
} /* end GetLoadedClasses */
398
// initiating_loader - null is a valid value, must be checked
399
// class_count_ptr - pre-checked for null
400
// classes_ptr - pre-checked for null
402
JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
403
return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
404
class_count_ptr, classes_ptr);
405
} /* end GetClassLoaderClasses */
407
// k_mirror - may be primitive, this must be checked
408
// is_modifiable_class_ptr - pre-checked for null
410
JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
411
*is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
412
JNI_TRUE : JNI_FALSE;
413
return JVMTI_ERROR_NONE;
414
} /* end IsModifiableClass */
416
// class_count - pre-checked to be greater than or equal to 0
417
// classes - pre-checked for null
419
JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
423
JavaThread* current_thread = JavaThread::current();
424
ResourceMark rm(current_thread);
426
jvmtiClassDefinition* class_definitions =
427
NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
428
NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
430
for (index = 0; index < class_count; index++) {
431
HandleMark hm(current_thread);
433
jclass jcls = classes[index];
434
oop k_mirror = JNIHandles::resolve_external_guard(jcls);
435
if (k_mirror == nullptr) {
436
return JVMTI_ERROR_INVALID_CLASS;
438
if (!k_mirror->is_a(vmClasses::Class_klass())) {
439
return JVMTI_ERROR_INVALID_CLASS;
442
if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
443
return JVMTI_ERROR_UNMODIFIABLE_CLASS;
446
Klass* klass = java_lang_Class::as_Klass(k_mirror);
448
jint status = klass->jvmti_class_status();
449
if (status & (JVMTI_CLASS_STATUS_ERROR)) {
450
return JVMTI_ERROR_INVALID_CLASS;
453
InstanceKlass* ik = InstanceKlass::cast(klass);
454
if (ik->get_cached_class_file_bytes() == nullptr) {
455
// Not cached, we need to reconstitute the class file from the
456
// VM representation. We don't attach the reconstituted class
457
// bytes to the InstanceKlass here because they have not been
458
// validated and we're not at a safepoint.
459
JvmtiClassFileReconstituter reconstituter(ik);
460
if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
461
return reconstituter.get_error();
464
class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
465
class_definitions[index].class_bytes = (unsigned char*)
466
reconstituter.class_file_bytes();
468
// it is cached, get it from the cache
469
class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
470
class_definitions[index].class_bytes = ik->get_cached_class_file_bytes();
472
class_definitions[index].klass = jcls;
474
EventRetransformClasses event;
475
VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
476
VMThread::execute(&op);
477
jvmtiError error = op.check_error();
478
if (error == JVMTI_ERROR_NONE) {
479
event.set_classCount(class_count);
480
event.set_redefinitionId(op.id());
484
} /* end RetransformClasses */
487
// class_count - pre-checked to be greater than or equal to 0
488
// class_definitions - pre-checked for null
490
JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
492
EventRedefineClasses event;
493
VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
494
VMThread::execute(&op);
495
jvmtiError error = op.check_error();
496
if (error == JVMTI_ERROR_NONE) {
497
event.set_classCount(class_count);
498
event.set_redefinitionId(op.id());
502
} /* end RedefineClasses */
509
// size_ptr - pre-checked for null
511
JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
512
oop mirror = JNIHandles::resolve_external_guard(object);
513
NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
514
*size_ptr = (jlong)mirror->size() * wordSize;
515
return JVMTI_ERROR_NONE;
516
} /* end GetObjectSize */
522
// prefix - null is a valid value, must be checked
524
JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
525
return prefix == nullptr?
526
SetNativeMethodPrefixes(0, nullptr) :
527
SetNativeMethodPrefixes(1, (char**)&prefix);
528
} /* end SetNativeMethodPrefix */
531
// prefix_count - pre-checked to be greater than or equal to 0
532
// prefixes - pre-checked for null
534
JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
535
// Have to grab JVMTI thread state lock to be sure that some thread
536
// isn't accessing the prefixes at the same time we are setting them.
537
// No locks during VM bring-up.
538
if (Threads::number_of_threads() == 0) {
539
return set_native_method_prefixes(prefix_count, prefixes);
541
MutexLocker mu(JvmtiThreadState_lock);
542
return set_native_method_prefixes(prefix_count, prefixes);
544
} /* end SetNativeMethodPrefixes */
547
// Event Management functions
550
// callbacks - null is a valid value, must be checked
551
// size_of_callbacks - pre-checked to be greater than or equal to 0
553
JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
554
JvmtiVTMSTransitionDisabler disabler;
555
JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
556
return JVMTI_ERROR_NONE;
557
} /* end SetEventCallbacks */
560
// event_thread - null is a valid value, must be checked
562
JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
563
bool enabled = (mode == JVMTI_ENABLE);
565
// event_type must be valid
566
if (!JvmtiEventController::is_valid_event_type(event_type)) {
567
return JVMTI_ERROR_INVALID_EVENT_TYPE;
570
// assure that needed capabilities are present
571
if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
572
return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
575
if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
576
record_class_file_load_hook_enabled();
578
JvmtiVTMSTransitionDisabler disabler;
580
if (event_thread == nullptr) {
581
// Can be called at Agent_OnLoad() time with event_thread == nullptr
582
// when Thread::current() does not work yet so we cannot create a
583
// ThreadsListHandle that is common to both thread-specific and
584
// global code paths.
586
JvmtiEventController::set_user_enabled(this, nullptr, (oop) nullptr, event_type, enabled);
588
// We have a specified event_thread.
589
JavaThread* current = JavaThread::current();
590
ThreadsListHandle tlh(current);
592
JavaThread* java_thread = nullptr;
593
oop thread_obj = nullptr;
594
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), event_thread, current, &java_thread, &thread_obj);
595
if (err != JVMTI_ERROR_NONE) {
599
// global events cannot be controlled at thread level.
600
if (JvmtiEventController::is_global_event(event_type)) {
601
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
604
JvmtiEventController::set_user_enabled(this, java_thread, thread_obj, event_type, enabled);
607
return JVMTI_ERROR_NONE;
608
} /* end SetEventNotificationMode */
611
// Capability functions
614
// capabilities_ptr - pre-checked for null
616
JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
617
JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
618
get_prohibited_capabilities(),
620
return JVMTI_ERROR_NONE;
621
} /* end GetPotentialCapabilities */
624
// capabilities_ptr - pre-checked for null
626
JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
627
return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
628
get_prohibited_capabilities(),
631
} /* end AddCapabilities */
634
// capabilities_ptr - pre-checked for null
636
JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
637
JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
638
return JVMTI_ERROR_NONE;
639
} /* end RelinquishCapabilities */
642
// capabilities_ptr - pre-checked for null
644
JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
645
JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
646
return JVMTI_ERROR_NONE;
647
} /* end GetCapabilities */
650
// Class Loader Search functions
653
// segment - pre-checked for null
655
JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
656
jvmtiPhase phase = get_phase();
657
if (phase == JVMTI_PHASE_ONLOAD) {
658
Arguments::append_sysclasspath(segment);
659
return JVMTI_ERROR_NONE;
660
} else if (use_version_1_0_semantics()) {
661
// This JvmtiEnv requested version 1.0 semantics and this function
662
// is only allowed in the ONLOAD phase in version 1.0 so we need to
663
// return an error here.
664
return JVMTI_ERROR_WRONG_PHASE;
665
} else if (phase == JVMTI_PHASE_LIVE) {
666
// The phase is checked by the wrapper that called this function,
667
// but this thread could be racing with the thread that is
668
// terminating the VM so we check one more time.
670
// create the zip entry
671
ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
672
if (zip_entry == nullptr) {
673
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
676
// add the jar file to the bootclasspath
677
log_info(class, load)("opened: %s", zip_entry->name());
679
ClassLoaderExt::append_boot_classpath(zip_entry);
681
ClassLoader::add_to_boot_append_entries(zip_entry);
683
return JVMTI_ERROR_NONE;
685
return JVMTI_ERROR_WRONG_PHASE;
688
} /* end AddToBootstrapClassLoaderSearch */
691
// segment - pre-checked for null
693
JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
694
jvmtiPhase phase = get_phase();
696
if (phase == JVMTI_PHASE_ONLOAD) {
697
for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
698
if (strcmp("java.class.path", p->key()) == 0) {
699
p->append_value(segment);
703
return JVMTI_ERROR_NONE;
704
} else if (phase == JVMTI_PHASE_LIVE) {
705
// The phase is checked by the wrapper that called this function,
706
// but this thread could be racing with the thread that is
707
// terminating the VM so we check one more time.
708
JavaThread* THREAD = JavaThread::current(); // For exception macros.
709
HandleMark hm(THREAD);
711
// create the zip entry (which will open the zip file and hence
712
// check that the segment is indeed a zip file).
713
ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
714
if (zip_entry == nullptr) {
715
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
717
delete zip_entry; // no longer needed
719
Handle loader(THREAD, SystemDictionary::java_system_loader());
721
// need the path as java.lang.String
722
Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
723
if (HAS_PENDING_EXCEPTION) {
724
CLEAR_PENDING_EXCEPTION;
725
return JVMTI_ERROR_INTERNAL;
728
// Invoke the appendToClassPathForInstrumentation method - if the method
729
// is not found it means the loader doesn't support adding to the class path
730
// in the live phase.
732
JavaValue res(T_VOID);
733
JavaCalls::call_special(&res,
736
vmSymbols::appendToClassPathForInstrumentation_name(),
737
vmSymbols::appendToClassPathForInstrumentation_signature(),
740
if (HAS_PENDING_EXCEPTION) {
741
Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
742
CLEAR_PENDING_EXCEPTION;
744
if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
745
return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
747
return JVMTI_ERROR_INTERNAL;
752
return JVMTI_ERROR_NONE;
754
return JVMTI_ERROR_WRONG_PHASE;
756
} /* end AddToSystemClassLoaderSearch */
762
// phase_ptr - pre-checked for null
764
JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
765
*phase_ptr = phase();
766
return JVMTI_ERROR_NONE;
771
JvmtiEnv::DisposeEnvironment() {
773
return JVMTI_ERROR_NONE;
774
} /* end DisposeEnvironment */
777
// data - null is a valid value, must be checked
779
JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
780
set_env_local_storage(data);
781
return JVMTI_ERROR_NONE;
782
} /* end SetEnvironmentLocalStorage */
785
// data_ptr - pre-checked for null
787
JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
788
*data_ptr = (void*)get_env_local_storage();
789
return JVMTI_ERROR_NONE;
790
} /* end GetEnvironmentLocalStorage */
792
// version_ptr - pre-checked for null
794
JvmtiEnv::GetVersionNumber(jint* version_ptr) {
795
*version_ptr = JVMTI_VERSION;
796
return JVMTI_ERROR_NONE;
797
} /* end GetVersionNumber */
800
// name_ptr - pre-checked for null
802
JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
803
if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
804
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
806
const char *name = JvmtiUtil::error_name(error);
807
if (name == nullptr) {
808
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
810
size_t len = strlen(name) + 1;
811
jvmtiError err = allocate(len, (unsigned char**)name_ptr);
812
if (err == JVMTI_ERROR_NONE) {
813
memcpy(*name_ptr, name, len);
816
} /* end GetErrorName */
820
JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
821
LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
823
case JVMTI_VERBOSE_OTHER:
826
case JVMTI_VERBOSE_CLASS:
827
LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
828
LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
830
case JVMTI_VERBOSE_GC:
831
LogConfiguration::configure_stdout(level, true, LOG_TAGS(gc));
833
case JVMTI_VERBOSE_JNI:
834
level = value == 0 ? LogLevel::Off : LogLevel::Debug;
835
LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
838
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
840
return JVMTI_ERROR_NONE;
841
} /* end SetVerboseFlag */
844
// format_ptr - pre-checked for null
846
JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
847
*format_ptr = JVMTI_JLOCATION_JVMBCI;
848
return JVMTI_ERROR_NONE;
849
} /* end GetJLocationFormat */
855
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
856
// thread_state_ptr - pre-checked for null
858
JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
859
JavaThread* current_thread = JavaThread::current();
860
JvmtiVTMSTransitionDisabler disabler(thread);
861
ThreadsListHandle tlh(current_thread);
863
JavaThread* java_thread = nullptr;
864
oop thread_oop = nullptr;
865
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
866
if (err != JVMTI_ERROR_NONE && err != JVMTI_ERROR_THREAD_NOT_ALIVE) {
867
// We got an error code so we don't have a JavaThread*, but only
868
// return an error from here if the error is not because the thread
869
// is a virtual thread.
872
*thread_state_ptr = JvmtiEnvBase::get_thread_or_vthread_state(thread_oop, java_thread);
873
return JVMTI_ERROR_NONE;
874
} /* end GetThreadState */
877
// thread_ptr - pre-checked for null
879
JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
880
JavaThread* cur_thread = JavaThread::current();
881
oop thread_oop = get_vthread_or_thread_oop(cur_thread);
883
*thread_ptr = (jthread)JNIHandles::make_local(cur_thread, thread_oop);
884
return JVMTI_ERROR_NONE;
885
} /* end GetCurrentThread */
888
// threads_count_ptr - pre-checked for null
889
// threads_ptr - pre-checked for null
891
JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
893
Handle *thread_objs = nullptr;
894
Thread* current_thread = Thread::current();
895
ResourceMark rm(current_thread);
896
HandleMark hm(current_thread);
898
// enumerate threads (including agent threads)
899
ThreadsListEnumerator tle(current_thread, true);
900
nthreads = tle.num_threads();
901
*threads_count_ptr = nthreads;
904
*threads_ptr = nullptr;
905
return JVMTI_ERROR_NONE;
908
thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
909
NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
911
for (int i = 0; i < nthreads; i++) {
912
thread_objs[i] = Handle(tle.get_threadObj(i));
915
jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
916
NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
918
*threads_ptr = jthreads;
919
return JVMTI_ERROR_NONE;
920
} /* end GetAllThreads */
923
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
925
JvmtiEnv::SuspendThread(jthread thread) {
926
JavaThread* current = JavaThread::current();
927
HandleMark hm(current);
932
JvmtiVTMSTransitionDisabler disabler(true);
933
ThreadsListHandle tlh(current);
934
JavaThread* java_thread = nullptr;
935
oop thread_oop = nullptr;
937
err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop);
938
if (err != JVMTI_ERROR_NONE) {
942
// Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks.
943
if (java_thread != current) {
944
err = suspend_thread(thread_oop, java_thread, /* single_suspend */ true, nullptr);
947
// protect thread_oop as a safepoint can be reached in disabler destructor
948
self_tobj = Handle(current, thread_oop);
950
// Do self suspend for current JavaThread.
951
err = suspend_thread(self_tobj(), current, /* single_suspend */ true, nullptr);
953
} /* end SuspendThread */
956
// request_count - pre-checked to be greater than or equal to 0
957
// request_list - pre-checked for null
958
// results - pre-checked for null
960
JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
961
JavaThread* current = JavaThread::current();
962
HandleMark hm(current);
967
JvmtiVTMSTransitionDisabler disabler(true);
968
ThreadsListHandle tlh(current);
970
for (int i = 0; i < request_count; i++) {
971
JavaThread *java_thread = nullptr;
972
oop thread_oop = nullptr;
973
jthread thread = request_list[i];
974
jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
976
if (thread_oop != nullptr &&
977
java_lang_VirtualThread::is_instance(thread_oop) &&
978
!JvmtiEnvBase::is_vthread_alive(thread_oop)) {
979
err = JVMTI_ERROR_THREAD_NOT_ALIVE;
981
if (err != JVMTI_ERROR_NONE) {
982
if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
987
if (java_thread == current) {
989
self_tobj = Handle(current, thread_oop);
990
continue; // self suspend after all other suspends
992
results[i] = suspend_thread(thread_oop, java_thread, /* single_suspend */ true, nullptr);
995
// Self suspend after all other suspends if necessary.
996
// Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks.
997
if (self_tobj() != nullptr) {
998
// there should not be any error for current java_thread
999
results[self_idx] = suspend_thread(self_tobj(), current, /* single_suspend */ true, nullptr);
1001
// per-thread suspend results returned via results parameter
1002
return JVMTI_ERROR_NONE;
1003
} /* end SuspendThreadList */
1007
JvmtiEnv::SuspendAllVirtualThreads(jint except_count, const jthread* except_list) {
1008
if (get_capabilities()->can_support_virtual_threads == 0) {
1009
return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1011
JavaThread* current = JavaThread::current();
1012
HandleMark hm(current);
1016
ResourceMark rm(current);
1017
JvmtiVTMSTransitionDisabler disabler(true);
1018
ThreadsListHandle tlh(current);
1019
GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1021
jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1022
if (err != JVMTI_ERROR_NONE) {
1026
// Collect threads from except_list for which resumed status must be restored (only for VirtualThread case)
1027
for (int idx = 0; idx < except_count; idx++) {
1028
jthread thread = except_list[idx];
1029
oop thread_oop = JNIHandles::resolve_external_guard(thread);
1030
if (java_lang_VirtualThread::is_instance(thread_oop) && !JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1031
// is not suspended, so its resumed status must be restored
1032
elist->append(except_list[idx]);
1036
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1037
oop vt_oop = java_thread->jvmti_vthread();
1038
if (!java_thread->is_exiting() &&
1039
!java_thread->is_jvmti_agent_thread() &&
1040
!java_thread->is_hidden_from_external_view() &&
1041
vt_oop != nullptr &&
1042
((java_lang_VirtualThread::is_instance(vt_oop) &&
1043
JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1044
!JvmtiVTSuspender::is_vthread_suspended(vt_oop)) ||
1045
(vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && !java_thread->is_suspended())) &&
1046
!is_in_thread_list(except_count, except_list, vt_oop)
1048
if (java_thread == current) {
1049
self_tobj = Handle(current, vt_oop);
1050
continue; // self suspend after all other suspends
1052
suspend_thread(vt_oop, java_thread, /* single_suspend */ false, nullptr);
1055
JvmtiVTSuspender::register_all_vthreads_suspend();
1057
// Restore resumed state for threads from except list that were not suspended before.
1058
for (int idx = 0; idx < elist->length(); idx++) {
1059
jthread thread = elist->at(idx);
1060
oop thread_oop = JNIHandles::resolve_external_guard(thread);
1061
if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1062
JvmtiVTSuspender::register_vthread_resume(thread_oop);
1066
// Self suspend after all other suspends if necessary.
1067
// Do not use JvmtiVTMSTransitionDisabler in context of self suspend to avoid deadlocks.
1068
if (self_tobj() != nullptr) {
1069
suspend_thread(self_tobj(), current, /* single_suspend */ false, nullptr);
1071
return JVMTI_ERROR_NONE;
1072
} /* end SuspendAllVirtualThreads */
1075
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1077
JvmtiEnv::ResumeThread(jthread thread) {
1078
JvmtiVTMSTransitionDisabler disabler(true);
1079
JavaThread* current = JavaThread::current();
1080
ThreadsListHandle tlh(current);
1082
JavaThread* java_thread = nullptr;
1083
oop thread_oop = nullptr;
1084
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop);
1085
if (err != JVMTI_ERROR_NONE) {
1088
err = resume_thread(thread_oop, java_thread, /* single_resume */ true);
1090
} /* end ResumeThread */
1093
// request_count - pre-checked to be greater than or equal to 0
1094
// request_list - pre-checked for null
1095
// results - pre-checked for null
1097
JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1098
oop thread_oop = nullptr;
1099
JavaThread* java_thread = nullptr;
1100
JvmtiVTMSTransitionDisabler disabler(true);
1101
ThreadsListHandle tlh;
1103
for (int i = 0; i < request_count; i++) {
1104
jthread thread = request_list[i];
1105
jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1107
if (thread_oop != nullptr &&
1108
java_lang_VirtualThread::is_instance(thread_oop) &&
1109
!JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1110
err = JVMTI_ERROR_THREAD_NOT_ALIVE;
1112
if (err != JVMTI_ERROR_NONE) {
1113
if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1118
results[i] = resume_thread(thread_oop, java_thread, /* single_resume */ true);
1120
// per-thread resume results returned via results parameter
1121
return JVMTI_ERROR_NONE;
1122
} /* end ResumeThreadList */
1126
JvmtiEnv::ResumeAllVirtualThreads(jint except_count, const jthread* except_list) {
1127
if (get_capabilities()->can_support_virtual_threads == 0) {
1128
return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1130
jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1131
if (err != JVMTI_ERROR_NONE) {
1135
JvmtiVTMSTransitionDisabler disabler(true);
1136
GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1138
// Collect threads from except_list for which suspended status must be restored (only for VirtualThread case)
1139
for (int idx = 0; idx < except_count; idx++) {
1140
jthread thread = except_list[idx];
1141
oop thread_oop = JNIHandles::resolve_external_guard(thread);
1142
if (java_lang_VirtualThread::is_instance(thread_oop) && JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1143
// is suspended, so its suspended status must be restored
1144
elist->append(except_list[idx]);
1148
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1149
oop vt_oop = java_thread->jvmti_vthread();
1150
if (!java_thread->is_exiting() &&
1151
!java_thread->is_jvmti_agent_thread() &&
1152
!java_thread->is_hidden_from_external_view() &&
1153
vt_oop != nullptr &&
1154
((java_lang_VirtualThread::is_instance(vt_oop) &&
1155
JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1156
JvmtiVTSuspender::is_vthread_suspended(vt_oop)) ||
1157
(vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && java_thread->is_suspended())) &&
1158
!is_in_thread_list(except_count, except_list, vt_oop)
1160
resume_thread(vt_oop, java_thread, /* single_resume */ false);
1163
JvmtiVTSuspender::register_all_vthreads_resume();
1165
// Restore suspended state for threads from except list that were suspended before.
1166
for (int idx = 0; idx < elist->length(); idx++) {
1167
jthread thread = elist->at(idx);
1168
oop thread_oop = JNIHandles::resolve_external_guard(thread);
1169
if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1170
JvmtiVTSuspender::register_vthread_suspend(thread_oop);
1173
return JVMTI_ERROR_NONE;
1174
} /* end ResumeAllVirtualThreads */
1178
JvmtiEnv::StopThread(jthread thread, jobject exception) {
1179
JavaThread* current_thread = JavaThread::current();
1181
JvmtiVTMSTransitionDisabler disabler(thread);
1182
ThreadsListHandle tlh(current_thread);
1183
JavaThread* java_thread = nullptr;
1184
oop thread_oop = nullptr;
1186
NULL_CHECK(thread, JVMTI_ERROR_INVALID_THREAD);
1188
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
1190
bool is_virtual = thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1192
if (is_virtual && !is_JavaThread_current(java_thread, thread_oop)) {
1193
if (!is_vthread_suspended(thread_oop, java_thread)) {
1194
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1196
if (java_thread == nullptr) { // unmounted virtual thread
1197
return JVMTI_ERROR_OPAQUE_FRAME;
1200
if (err != JVMTI_ERROR_NONE) {
1203
oop e = JNIHandles::resolve_external_guard(exception);
1204
NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1206
JavaThread::send_async_exception(java_thread, e);
1208
return JVMTI_ERROR_NONE;
1210
} /* end StopThread */
1213
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1215
JvmtiEnv::InterruptThread(jthread thread) {
1216
JavaThread* current_thread = JavaThread::current();
1217
HandleMark hm(current_thread);
1219
JvmtiVTMSTransitionDisabler disabler(thread);
1220
ThreadsListHandle tlh(current_thread);
1222
JavaThread* java_thread = nullptr;
1223
oop thread_obj = nullptr;
1224
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
1225
if (err != JVMTI_ERROR_NONE) {
1229
if (java_lang_VirtualThread::is_instance(thread_obj)) {
1230
// For virtual threads we have to call into Java to interrupt:
1231
Handle obj(current_thread, thread_obj);
1232
JavaValue result(T_VOID);
1233
JavaCalls::call_virtual(&result,
1235
vmClasses::Thread_klass(),
1236
vmSymbols::interrupt_method_name(),
1237
vmSymbols::void_method_signature(),
1240
return JVMTI_ERROR_NONE;
1243
// Really this should be a Java call to Thread.interrupt to ensure the same
1244
// semantics, however historically this has not been done for some reason.
1245
// So we continue with that (which means we don't interact with any Java-level
1246
// Interruptible object) but we must set the Java-level interrupted state.
1247
java_lang_Thread::set_interrupted(thread_obj, true);
1248
java_thread->interrupt();
1250
return JVMTI_ERROR_NONE;
1251
} /* end InterruptThread */
1254
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1255
// info_ptr - pre-checked for null
1257
JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1258
JavaThread* current_thread = JavaThread::current();
1259
ResourceMark rm(current_thread);
1260
HandleMark hm(current_thread);
1261
JavaThread* java_thread = nullptr;
1262
oop thread_oop = nullptr;
1264
JvmtiVTMSTransitionDisabler disabler(thread);
1265
ThreadsListHandle tlh(current_thread);
1267
// if thread is null the current thread is used
1268
if (thread == nullptr) {
1269
java_thread = JavaThread::current();
1270
thread_oop = get_vthread_or_thread_oop(java_thread);
1271
if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1272
return JVMTI_ERROR_INVALID_THREAD;
1275
jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1276
if (err != JVMTI_ERROR_NONE) {
1277
// We got an error code so we don't have a JavaThread *, but
1278
// only return an error from here if we didn't get a valid
1280
// In the virtual thread case the cv_external_thread_to_JavaThread is expected to correctly set
1281
// the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1282
if (thread_oop == nullptr) {
1287
// We have a valid thread_oop so we can return some thread info.
1289
Handle thread_obj(current_thread, thread_oop);
1291
ThreadPriority priority;
1292
Handle thread_group;
1293
Handle context_class_loader;
1296
name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1298
if (java_lang_VirtualThread::is_instance(thread_obj())) {
1299
priority = (ThreadPriority)JVMTI_THREAD_NORM_PRIORITY;
1301
if (java_lang_VirtualThread::state(thread_obj()) == java_lang_VirtualThread::TERMINATED) {
1302
thread_group = Handle(current_thread, nullptr);
1304
thread_group = Handle(current_thread, java_lang_Thread_Constants::get_VTHREAD_GROUP());
1307
priority = java_lang_Thread::priority(thread_obj());
1308
is_daemon = java_lang_Thread::is_daemon(thread_obj());
1309
if (java_lang_Thread::get_thread_status(thread_obj()) == JavaThreadStatus::TERMINATED) {
1310
thread_group = Handle(current_thread, nullptr);
1312
thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1316
oop loader = java_lang_Thread::context_class_loader(thread_obj());
1317
context_class_loader = Handle(current_thread, loader);
1321
if (name() != nullptr) {
1322
n = java_lang_String::as_utf8_string(name());
1324
int utf8_length = 0;
1325
n = UNICODE::as_utf8((jchar*) nullptr, utf8_length);
1328
info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1329
if (info_ptr->name == nullptr)
1330
return JVMTI_ERROR_OUT_OF_MEMORY;
1332
strcpy(info_ptr->name, n);
1334
info_ptr->is_daemon = is_daemon;
1335
info_ptr->priority = priority;
1337
info_ptr->context_class_loader = (context_class_loader.is_null()) ? nullptr :
1338
jni_reference(context_class_loader);
1339
info_ptr->thread_group = jni_reference(thread_group);
1341
return JVMTI_ERROR_NONE;
1342
} /* end GetThreadInfo */
1345
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1346
// owned_monitor_count_ptr - pre-checked for null
1347
// owned_monitors_ptr - pre-checked for null
1349
JvmtiEnv::GetOwnedMonitorInfo(jthread thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1350
JavaThread* calling_thread = JavaThread::current();
1351
HandleMark hm(calling_thread);
1353
// growable array of jvmti monitors info on the C-heap
1354
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1355
new (mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1357
JvmtiVTMSTransitionDisabler disabler(thread);
1358
ThreadsListHandle tlh(calling_thread);
1360
JavaThread* java_thread = nullptr;
1361
oop thread_oop = nullptr;
1362
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, calling_thread, &java_thread, &thread_oop);
1363
if (err != JVMTI_ERROR_NONE) {
1364
delete owned_monitors_list;
1368
if (java_thread != nullptr) {
1369
Handle thread_handle(calling_thread, thread_oop);
1370
EscapeBarrier eb(true, calling_thread, java_thread);
1371
if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1372
delete owned_monitors_list;
1373
return JVMTI_ERROR_OUT_OF_MEMORY;
1375
// get owned monitors info with handshake
1376
GetOwnedMonitorInfoClosure op(this, calling_thread, owned_monitors_list);
1377
JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1381
jint owned_monitor_count = owned_monitors_list->length();
1382
if (err == JVMTI_ERROR_NONE) {
1383
if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1384
(unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1385
// copy into the returned array
1386
for (int i = 0; i < owned_monitor_count; i++) {
1387
(*owned_monitors_ptr)[i] =
1388
((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1390
*owned_monitor_count_ptr = owned_monitor_count;
1394
for (int i = 0; i < owned_monitor_count; i++) {
1395
deallocate((unsigned char*)owned_monitors_list->at(i));
1397
delete owned_monitors_list;
1400
} /* end GetOwnedMonitorInfo */
1403
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1404
// monitor_info_count_ptr - pre-checked for null
1405
// monitor_info_ptr - pre-checked for null
1407
JvmtiEnv::GetOwnedMonitorStackDepthInfo(jthread thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1408
JavaThread* calling_thread = JavaThread::current();
1409
HandleMark hm(calling_thread);
1411
// growable array of jvmti monitors info on the C-heap
1412
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1413
new (mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1415
JvmtiVTMSTransitionDisabler disabler(thread);
1416
ThreadsListHandle tlh(calling_thread);
1418
JavaThread* java_thread = nullptr;
1419
oop thread_oop = nullptr;
1420
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, calling_thread, &java_thread, &thread_oop);
1421
if (err != JVMTI_ERROR_NONE) {
1422
delete owned_monitors_list;
1426
if (java_thread != nullptr) {
1427
Handle thread_handle(calling_thread, thread_oop);
1428
EscapeBarrier eb(true, calling_thread, java_thread);
1429
if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1430
delete owned_monitors_list;
1431
return JVMTI_ERROR_OUT_OF_MEMORY;
1433
// get owned monitors info with handshake
1434
GetOwnedMonitorInfoClosure op(this, calling_thread, owned_monitors_list);
1435
JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1439
jint owned_monitor_count = owned_monitors_list->length();
1440
if (err == JVMTI_ERROR_NONE) {
1441
if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1442
(unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1443
// copy to output array.
1444
for (int i = 0; i < owned_monitor_count; i++) {
1445
(*monitor_info_ptr)[i].monitor =
1446
((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1447
(*monitor_info_ptr)[i].stack_depth =
1448
((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1451
*monitor_info_count_ptr = owned_monitor_count;
1455
for (int i = 0; i < owned_monitor_count; i++) {
1456
deallocate((unsigned char*)owned_monitors_list->at(i));
1458
delete owned_monitors_list;
1461
} /* end GetOwnedMonitorStackDepthInfo */
1464
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1465
// monitor_ptr - pre-checked for null
1467
JvmtiEnv::GetCurrentContendedMonitor(jthread thread, jobject* monitor_ptr) {
1468
JavaThread* current = JavaThread::current();
1470
*monitor_ptr = nullptr;
1472
// get contended monitor information with handshake
1473
GetCurrentContendedMonitorClosure op(this, current, monitor_ptr);
1474
JvmtiHandshake::execute(&op, thread);
1476
} /* end GetCurrentContendedMonitor */
1479
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1480
// proc - pre-checked for null
1481
// arg - null is a valid value, must be checked
1483
JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1484
JavaThread* current_thread = JavaThread::current();
1486
JavaThread* java_thread = nullptr;
1487
oop thread_oop = nullptr;
1488
ThreadsListHandle tlh(current_thread);
1489
jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1490
if (err != JVMTI_ERROR_NONE) {
1491
// We got an error code so we don't have a JavaThread *, but
1492
// only return an error from here if we didn't get a valid
1494
if (thread_oop == nullptr) {
1497
// We have a valid thread_oop.
1500
if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1501
// No support for virtual threads.
1502
return JVMTI_ERROR_UNSUPPORTED_OPERATION;
1504
if (java_thread != nullptr) {
1505
// 'thread' refers to an existing JavaThread.
1506
return JVMTI_ERROR_INVALID_THREAD;
1509
if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1510
return JVMTI_ERROR_INVALID_PRIORITY;
1513
Handle thread_hndl(current_thread, thread_oop);
1515
JvmtiAgentThread* new_thread = new JvmtiAgentThread(this, proc, arg);
1517
// At this point it may be possible that no osthread was created for the
1518
// JavaThread due to lack of resources.
1519
if (new_thread->osthread() == nullptr) {
1520
// The new thread is not known to Thread-SMR yet so we can just delete.
1522
return JVMTI_ERROR_OUT_OF_MEMORY;
1525
JavaThread::start_internal_daemon(current_thread, new_thread, thread_hndl,
1526
(ThreadPriority)priority);
1528
return JVMTI_ERROR_NONE;
1529
} /* end RunAgentThread */
1532
// Thread Group functions
1535
// group_count_ptr - pre-checked for null
1536
// groups_ptr - pre-checked for null
1538
JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1539
JavaThread* current_thread = JavaThread::current();
1541
// Only one top level thread group now.
1542
*group_count_ptr = 1;
1544
// Allocate memory to store global-refs to the thread groups.
1545
// Assume this area is freed by caller.
1546
*groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1548
NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1550
// Convert oop to Handle, then convert Handle to global-ref.
1552
HandleMark hm(current_thread);
1553
Handle system_thread_group(current_thread, Universe::system_thread_group());
1554
*groups_ptr[0] = jni_reference(system_thread_group);
1557
return JVMTI_ERROR_NONE;
1558
} /* end GetTopThreadGroups */
1561
// info_ptr - pre-checked for null
1563
JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1564
Thread* current_thread = Thread::current();
1565
ResourceMark rm(current_thread);
1566
HandleMark hm(current_thread);
1568
Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1569
NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1572
Handle parent_group;
1574
ThreadPriority max_priority;
1576
name = java_lang_ThreadGroup::name(group_obj());
1577
parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1578
is_daemon = java_lang_ThreadGroup::is_daemon(group_obj());
1579
max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1581
info_ptr->is_daemon = is_daemon;
1582
info_ptr->max_priority = max_priority;
1583
info_ptr->parent = jni_reference(parent_group);
1585
if (name != nullptr) {
1586
info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1587
NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1588
strcpy(info_ptr->name, name);
1590
info_ptr->name = nullptr;
1593
return JVMTI_ERROR_NONE;
1594
} /* end GetThreadGroupInfo */
1596
// thread_count_ptr - pre-checked for null
1597
// threads_ptr - pre-checked for null
1598
// group_count_ptr - pre-checked for null
1599
// groups_ptr - pre-checked for null
1601
JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1603
JavaThread* current_thread = JavaThread::current();
1604
oop group_obj = JNIHandles::resolve_external_guard(group);
1605
NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1607
Handle *thread_objs = nullptr;
1608
objArrayHandle group_objs;
1611
int hidden_threads = 0;
1613
ResourceMark rm(current_thread);
1614
HandleMark hm(current_thread);
1616
Handle group_hdl(current_thread, group_obj);
1618
err = get_live_threads(current_thread, group_hdl, &nthreads, &thread_objs);
1619
if (err != JVMTI_ERROR_NONE) {
1622
err = get_subgroups(current_thread, group_hdl, &ngroups, &group_objs);
1623
if (err != JVMTI_ERROR_NONE) {
1627
*group_count_ptr = ngroups;
1628
*thread_count_ptr = nthreads;
1629
*threads_ptr = new_jthreadArray(nthreads, thread_objs);
1630
*groups_ptr = new_jthreadGroupArray(ngroups, group_objs);
1631
if (nthreads > 0 && *threads_ptr == nullptr) {
1632
return JVMTI_ERROR_OUT_OF_MEMORY;
1634
if (ngroups > 0 && *groups_ptr == nullptr) {
1635
return JVMTI_ERROR_OUT_OF_MEMORY;
1638
return JVMTI_ERROR_NONE;
1639
} /* end GetThreadGroupChildren */
1643
// Stack Frame functions
1646
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1647
// max_frame_count - pre-checked to be greater than or equal to 0
1648
// frame_buffer - pre-checked for null
1649
// count_ptr - pre-checked for null
1651
JvmtiEnv::GetStackTrace(jthread thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1652
GetStackTraceClosure op(this, start_depth, max_frame_count, frame_buffer, count_ptr);
1653
JvmtiHandshake::execute(&op, thread);
1655
} /* end GetStackTrace */
1658
// max_frame_count - pre-checked to be greater than or equal to 0
1659
// stack_info_ptr - pre-checked for null
1660
// thread_count_ptr - pre-checked for null
1662
JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1663
jvmtiError err = JVMTI_ERROR_NONE;
1664
JavaThread* calling_thread = JavaThread::current();
1666
// JVMTI get stack traces at safepoint.
1667
VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1668
VMThread::execute(&op);
1669
*thread_count_ptr = op.final_thread_count();
1670
*stack_info_ptr = op.stack_info();
1673
} /* end GetAllStackTraces */
1676
// thread_count - pre-checked to be greater than or equal to 0
1677
// thread_list - pre-checked for null
1678
// max_frame_count - pre-checked to be greater than or equal to 0
1679
// stack_info_ptr - pre-checked for null
1681
JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1682
jvmtiError err = JVMTI_ERROR_NONE;
1684
if (thread_count == 1) {
1685
// Use direct handshake if we need to get only one stack trace.
1686
JavaThread *current_thread = JavaThread::current();
1688
jthread thread = thread_list[0];
1690
GetSingleStackTraceClosure op(this, current_thread, thread, max_frame_count);
1691
JvmtiHandshake::execute(&op, thread);
1693
if (err == JVMTI_ERROR_NONE) {
1694
*stack_info_ptr = op.stack_info();
1697
JvmtiVTMSTransitionDisabler disabler;
1699
// JVMTI get stack traces at safepoint.
1700
VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1701
VMThread::execute(&op);
1703
if (err == JVMTI_ERROR_NONE) {
1704
*stack_info_ptr = op.stack_info();
1708
} /* end GetThreadListStackTraces */
1711
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1712
// count_ptr - pre-checked for null
1714
JvmtiEnv::GetFrameCount(jthread thread, jint* count_ptr) {
1715
GetFrameCountClosure op(this, count_ptr);
1716
JvmtiHandshake::execute(&op, thread);
1718
} /* end GetFrameCount */
1721
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1723
JvmtiEnv::PopFrame(jthread thread) {
1724
JavaThread* current_thread = JavaThread::current();
1725
HandleMark hm(current_thread);
1727
if (thread == nullptr) {
1728
return JVMTI_ERROR_INVALID_THREAD;
1730
JvmtiVTMSTransitionDisabler disabler(thread);
1731
ThreadsListHandle tlh(current_thread);
1733
JavaThread* java_thread = nullptr;
1734
oop thread_obj = nullptr;
1735
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
1736
Handle thread_handle(current_thread, thread_obj);
1738
if (err != JVMTI_ERROR_NONE) {
1741
bool self = java_thread == current_thread;
1743
err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self);
1744
if (err != JVMTI_ERROR_NONE) {
1748
// retrieve or create the state
1749
JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1750
if (state == nullptr) {
1751
return JVMTI_ERROR_THREAD_NOT_ALIVE;
1754
// Eagerly reallocate scalar replaced objects.
1755
EscapeBarrier eb(true, current_thread, java_thread);
1756
if (!eb.deoptimize_objects(1)) {
1757
// Reallocation of scalar replaced objects failed -> return with error
1758
return JVMTI_ERROR_OUT_OF_MEMORY;
1761
MutexLocker mu(JvmtiThreadState_lock);
1762
UpdateForPopTopFrameClosure op(state);
1763
JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1768
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1769
// depth - pre-checked as non-negative
1770
// method_ptr - pre-checked for null
1771
// location_ptr - pre-checked for null
1773
JvmtiEnv::GetFrameLocation(jthread thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1774
GetFrameLocationClosure op(this, depth, method_ptr, location_ptr);
1775
JvmtiHandshake::execute(&op, thread);
1777
} /* end GetFrameLocation */
1780
// Threads_lock NOT held, java_thread not protected by lock
1781
// depth - pre-checked as non-negative
1783
JvmtiEnv::NotifyFramePop(jthread thread, jint depth) {
1785
JvmtiVTMSTransitionDisabler disabler(thread);
1786
JavaThread* current = JavaThread::current();
1787
ThreadsListHandle tlh(current);
1789
JavaThread* java_thread = nullptr;
1790
oop thread_obj = nullptr;
1791
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
1792
if (err != JVMTI_ERROR_NONE) {
1796
HandleMark hm(current);
1797
Handle thread_handle(current, thread_obj);
1798
JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1799
if (state == nullptr) {
1800
return JVMTI_ERROR_THREAD_NOT_ALIVE;
1803
SetFramePopClosure op(this, state, depth);
1804
MutexLocker mu(current, JvmtiThreadState_lock);
1805
JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1807
} /* end NotifyFramePop */
1811
// Force Early Return functions
1814
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1816
JvmtiEnv::ForceEarlyReturnObject(jthread thread, jobject value) {
1819
return force_early_return(thread, val, atos);
1820
} /* end ForceEarlyReturnObject */
1823
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1825
JvmtiEnv::ForceEarlyReturnInt(jthread thread, jint value) {
1828
return force_early_return(thread, val, itos);
1829
} /* end ForceEarlyReturnInt */
1832
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1834
JvmtiEnv::ForceEarlyReturnLong(jthread thread, jlong value) {
1837
return force_early_return(thread, val, ltos);
1838
} /* end ForceEarlyReturnLong */
1841
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1843
JvmtiEnv::ForceEarlyReturnFloat(jthread thread, jfloat value) {
1846
return force_early_return(thread, val, ftos);
1847
} /* end ForceEarlyReturnFloat */
1850
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1852
JvmtiEnv::ForceEarlyReturnDouble(jthread thread, jdouble value) {
1855
return force_early_return(thread, val, dtos);
1856
} /* end ForceEarlyReturnDouble */
1859
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
1861
JvmtiEnv::ForceEarlyReturnVoid(jthread thread) {
1864
return force_early_return(thread, val, vtos);
1865
} /* end ForceEarlyReturnVoid */
1872
// klass - null is a valid value, must be checked
1873
// initial_object - null is a valid value, must be checked
1874
// callbacks - pre-checked for null
1875
// user_data - null is a valid value, must be checked
1877
JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1878
// check klass if provided
1880
if (klass != nullptr) {
1881
oop k_mirror = JNIHandles::resolve_external_guard(klass);
1882
if (k_mirror == nullptr) {
1883
return JVMTI_ERROR_INVALID_CLASS;
1885
if (java_lang_Class::is_primitive(k_mirror)) {
1886
return JVMTI_ERROR_NONE;
1888
k = java_lang_Class::as_Klass(k_mirror);
1889
if (klass == nullptr) {
1890
return JVMTI_ERROR_INVALID_CLASS;
1894
if (initial_object != nullptr) {
1895
oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1896
if (init_obj == nullptr) {
1897
return JVMTI_ERROR_INVALID_OBJECT;
1901
Thread *thread = Thread::current();
1902
HandleMark hm(thread);
1904
TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1905
JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1906
return JVMTI_ERROR_NONE;
1907
} /* end FollowReferences */
1910
// klass - null is a valid value, must be checked
1911
// callbacks - pre-checked for null
1912
// user_data - null is a valid value, must be checked
1914
JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1915
// check klass if provided
1917
if (klass != nullptr) {
1918
oop k_mirror = JNIHandles::resolve_external_guard(klass);
1919
if (k_mirror == nullptr) {
1920
return JVMTI_ERROR_INVALID_CLASS;
1922
if (java_lang_Class::is_primitive(k_mirror)) {
1923
return JVMTI_ERROR_NONE;
1925
k = java_lang_Class::as_Klass(k_mirror);
1927
return JVMTI_ERROR_INVALID_CLASS;
1931
TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1932
JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1933
return JVMTI_ERROR_NONE;
1934
} /* end IterateThroughHeap */
1937
// tag_ptr - pre-checked for null
1939
JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1940
oop o = JNIHandles::resolve_external_guard(object);
1941
NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1942
*tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1943
return JVMTI_ERROR_NONE;
1948
JvmtiEnv::SetTag(jobject object, jlong tag) {
1949
oop o = JNIHandles::resolve_external_guard(object);
1950
NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1951
JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1952
return JVMTI_ERROR_NONE;
1956
// tag_count - pre-checked to be greater than or equal to 0
1957
// tags - pre-checked for null
1958
// count_ptr - pre-checked for null
1959
// object_result_ptr - null is a valid value, must be checked
1960
// tag_result_ptr - null is a valid value, must be checked
1962
JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1963
TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1964
return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1965
} /* end GetObjectsWithTags */
1969
JvmtiEnv::ForceGarbageCollection() {
1970
Universe::heap()->collect(GCCause::_jvmti_force_gc);
1971
return JVMTI_ERROR_NONE;
1972
} /* end ForceGarbageCollection */
1976
// Heap (1.0) functions
1979
// object_reference_callback - pre-checked for null
1980
// user_data - null is a valid value, must be checked
1982
JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1983
oop o = JNIHandles::resolve_external_guard(object);
1984
NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1985
JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1986
return JVMTI_ERROR_NONE;
1987
} /* end IterateOverObjectsReachableFromObject */
1990
// heap_root_callback - null is a valid value, must be checked
1991
// stack_ref_callback - null is a valid value, must be checked
1992
// object_ref_callback - null is a valid value, must be checked
1993
// user_data - null is a valid value, must be checked
1995
JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1996
TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1997
JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1998
return JVMTI_ERROR_NONE;
1999
} /* end IterateOverReachableObjects */
2002
// heap_object_callback - pre-checked for null
2003
// user_data - null is a valid value, must be checked
2005
JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2006
TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
2007
Thread *thread = Thread::current();
2008
HandleMark hm(thread);
2009
JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, nullptr, heap_object_callback, user_data);
2010
return JVMTI_ERROR_NONE;
2011
} /* end IterateOverHeap */
2014
// k_mirror - may be primitive, this must be checked
2015
// heap_object_callback - pre-checked for null
2016
// user_data - null is a valid value, must be checked
2018
JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2019
if (java_lang_Class::is_primitive(k_mirror)) {
2020
// DO PRIMITIVE CLASS PROCESSING
2021
return JVMTI_ERROR_NONE;
2023
Klass* klass = java_lang_Class::as_Klass(k_mirror);
2024
if (klass == nullptr) {
2025
return JVMTI_ERROR_INVALID_CLASS;
2027
TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2028
JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2029
return JVMTI_ERROR_NONE;
2030
} /* end IterateOverInstancesOfClass */
2034
// Local Variable functions
2037
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2038
// depth - pre-checked as non-negative
2039
// value_ptr - pre-checked for null
2041
JvmtiEnv::GetLocalObject(jthread thread, jint depth, jint slot, jobject* value_ptr) {
2042
JavaThread* current_thread = JavaThread::current();
2043
// rm object is created to clean up the javaVFrame created in
2044
// doit_prologue(), but after doit() is finished with it.
2045
ResourceMark rm(current_thread);
2046
HandleMark hm(current_thread);
2047
JvmtiVTMSTransitionDisabler disabler(thread);
2048
ThreadsListHandle tlh(current_thread);
2050
JavaThread* java_thread = nullptr;
2051
oop thread_obj = nullptr;
2052
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2053
if (err != JVMTI_ERROR_NONE) {
2056
bool self = is_JavaThread_current(java_thread, thread_obj);
2058
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2059
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2060
current_thread, depth, slot, self);
2061
VMThread::execute(&op);
2063
if (err == JVMTI_ERROR_NONE) {
2064
*value_ptr = op.value().l;
2067
// Support for ordinary threads
2068
VM_GetOrSetLocal op(java_thread, current_thread, depth, slot, self);
2069
VMThread::execute(&op);
2071
if (err == JVMTI_ERROR_NONE) {
2072
*value_ptr = op.value().l;
2076
} /* end GetLocalObject */
2078
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2079
// depth - pre-checked as non-negative
2080
// value - pre-checked for null
2082
JvmtiEnv::GetLocalInstance(jthread thread, jint depth, jobject* value_ptr){
2083
JavaThread* current_thread = JavaThread::current();
2084
// rm object is created to clean up the javaVFrame created in
2085
// doit_prologue(), but after doit() is finished with it.
2086
ResourceMark rm(current_thread);
2087
HandleMark hm(current_thread);
2088
JvmtiVTMSTransitionDisabler disabler(thread);
2089
ThreadsListHandle tlh(current_thread);
2091
JavaThread* java_thread = nullptr;
2092
oop thread_obj = nullptr;
2093
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2094
if (err != JVMTI_ERROR_NONE) {
2097
bool self = is_JavaThread_current(java_thread, thread_obj);
2099
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2100
VM_VirtualThreadGetReceiver op(this, Handle(current_thread, thread_obj),
2101
current_thread, depth, self);
2102
VMThread::execute(&op);
2104
if (err == JVMTI_ERROR_NONE) {
2105
*value_ptr = op.value().l;
2108
// Support for ordinary threads
2109
VM_GetReceiver op(java_thread, current_thread, depth, self);
2110
VMThread::execute(&op);
2112
if (err == JVMTI_ERROR_NONE) {
2113
*value_ptr = op.value().l;
2117
} /* end GetLocalInstance */
2120
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2121
// depth - pre-checked as non-negative
2122
// value_ptr - pre-checked for null
2124
JvmtiEnv::GetLocalInt(jthread thread, jint depth, jint slot, jint* value_ptr) {
2125
JavaThread* current_thread = JavaThread::current();
2126
// rm object is created to clean up the javaVFrame created in
2127
// doit_prologue(), but after doit() is finished with it.
2128
ResourceMark rm(current_thread);
2129
HandleMark hm(current_thread);
2130
JvmtiVTMSTransitionDisabler disabler(thread);
2131
ThreadsListHandle tlh(current_thread);
2133
JavaThread* java_thread = nullptr;
2134
oop thread_obj = nullptr;
2135
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2136
if (err != JVMTI_ERROR_NONE) {
2139
bool self = is_JavaThread_current(java_thread, thread_obj);
2141
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2142
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2143
depth, slot, T_INT, self);
2144
VMThread::execute(&op);
2146
if (err == JVMTI_ERROR_NONE) {
2147
*value_ptr = op.value().i;
2150
// Support for ordinary threads
2151
VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, self);
2152
VMThread::execute(&op);
2154
if (err == JVMTI_ERROR_NONE) {
2155
*value_ptr = op.value().i;
2159
} /* end GetLocalInt */
2162
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2163
// depth - pre-checked as non-negative
2164
// value_ptr - pre-checked for null
2166
JvmtiEnv::GetLocalLong(jthread thread, jint depth, jint slot, jlong* value_ptr) {
2167
JavaThread* current_thread = JavaThread::current();
2168
// rm object is created to clean up the javaVFrame created in
2169
// doit_prologue(), but after doit() is finished with it.
2170
ResourceMark rm(current_thread);
2171
HandleMark hm(current_thread);
2172
JvmtiVTMSTransitionDisabler disabler(thread);
2173
ThreadsListHandle tlh(current_thread);
2175
JavaThread* java_thread = nullptr;
2176
oop thread_obj = nullptr;
2177
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2178
if (err != JVMTI_ERROR_NONE) {
2181
bool self = is_JavaThread_current(java_thread, thread_obj);
2183
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2184
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2185
depth, slot, T_LONG, self);
2186
VMThread::execute(&op);
2188
if (err == JVMTI_ERROR_NONE) {
2189
*value_ptr = op.value().j;
2192
// Support for ordinary threads
2193
VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, self);
2194
VMThread::execute(&op);
2196
if (err == JVMTI_ERROR_NONE) {
2197
*value_ptr = op.value().j;
2201
} /* end GetLocalLong */
2204
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2205
// depth - pre-checked as non-negative
2206
// value_ptr - pre-checked for null
2208
JvmtiEnv::GetLocalFloat(jthread thread, jint depth, jint slot, jfloat* value_ptr) {
2209
JavaThread* current_thread = JavaThread::current();
2210
// rm object is created to clean up the javaVFrame created in
2211
// doit_prologue(), but after doit() is finished with it.
2212
ResourceMark rm(current_thread);
2213
HandleMark hm(current_thread);
2214
JvmtiVTMSTransitionDisabler disabler(thread);
2215
ThreadsListHandle tlh(current_thread);
2217
JavaThread* java_thread = nullptr;
2218
oop thread_obj = nullptr;
2219
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2220
if (err != JVMTI_ERROR_NONE) {
2223
bool self = is_JavaThread_current(java_thread, thread_obj);
2225
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2226
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2227
depth, slot, T_FLOAT, self);
2228
VMThread::execute(&op);
2230
if (err == JVMTI_ERROR_NONE) {
2231
*value_ptr = op.value().f;
2234
// Support for ordinary threads
2235
VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, self);
2236
VMThread::execute(&op);
2238
if (err == JVMTI_ERROR_NONE) {
2239
*value_ptr = op.value().f;
2243
} /* end GetLocalFloat */
2246
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2247
// depth - pre-checked as non-negative
2248
// value_ptr - pre-checked for null
2250
JvmtiEnv::GetLocalDouble(jthread thread, jint depth, jint slot, jdouble* value_ptr) {
2251
JavaThread* current_thread = JavaThread::current();
2252
// rm object is created to clean up the javaVFrame created in
2253
// doit_prologue(), but after doit() is finished with it.
2254
ResourceMark rm(current_thread);
2255
HandleMark hm(current_thread);
2256
JvmtiVTMSTransitionDisabler disabler(thread);
2257
ThreadsListHandle tlh(current_thread);
2259
JavaThread* java_thread = nullptr;
2260
oop thread_obj = nullptr;
2261
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2262
if (err != JVMTI_ERROR_NONE) {
2265
bool self = is_JavaThread_current(java_thread, thread_obj);
2267
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2268
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2269
depth, slot, T_DOUBLE, self);
2270
VMThread::execute(&op);
2272
if (err == JVMTI_ERROR_NONE) {
2273
*value_ptr = op.value().d;
2276
// Support for ordinary threads
2277
VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, self);
2278
VMThread::execute(&op);
2280
if (err == JVMTI_ERROR_NONE) {
2281
*value_ptr = op.value().d;
2285
} /* end GetLocalDouble */
2288
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2289
// depth - pre-checked as non-negative
2291
JvmtiEnv::SetLocalObject(jthread thread, jint depth, jint slot, jobject value) {
2292
JavaThread* current_thread = JavaThread::current();
2293
// rm object is created to clean up the javaVFrame created in
2294
// doit_prologue(), but after doit() is finished with it.
2295
ResourceMark rm(current_thread);
2296
HandleMark hm(current_thread);
2297
JvmtiVTMSTransitionDisabler disabler(thread);
2298
ThreadsListHandle tlh(current_thread);
2300
JavaThread* java_thread = nullptr;
2301
oop thread_obj = nullptr;
2302
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2303
if (err != JVMTI_ERROR_NONE) {
2306
bool self = is_JavaThread_current(java_thread, thread_obj);
2310
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2311
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2312
depth, slot, T_OBJECT, val, self);
2313
VMThread::execute(&op);
2316
// Support for ordinary threads
2317
VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val, self);
2318
VMThread::execute(&op);
2322
} /* end SetLocalObject */
2325
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2326
// depth - pre-checked as non-negative
2328
JvmtiEnv::SetLocalInt(jthread thread, jint depth, jint slot, jint value) {
2329
JavaThread* current_thread = JavaThread::current();
2330
// rm object is created to clean up the javaVFrame created in
2331
// doit_prologue(), but after doit() is finished with it.
2332
ResourceMark rm(current_thread);
2333
HandleMark hm(current_thread);
2334
JvmtiVTMSTransitionDisabler disabler(thread);
2335
ThreadsListHandle tlh(current_thread);
2337
JavaThread* java_thread = nullptr;
2338
oop thread_obj = nullptr;
2339
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2340
if (err != JVMTI_ERROR_NONE) {
2343
bool self = is_JavaThread_current(java_thread, thread_obj);
2347
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2348
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2349
depth, slot, T_INT, val, self);
2350
VMThread::execute(&op);
2353
// Support for ordinary threads
2354
VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val, self);
2355
VMThread::execute(&op);
2359
} /* end SetLocalInt */
2362
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2363
// depth - pre-checked as non-negative
2365
JvmtiEnv::SetLocalLong(jthread thread, jint depth, jint slot, jlong value) {
2366
JavaThread* current_thread = JavaThread::current();
2367
// rm object is created to clean up the javaVFrame created in
2368
// doit_prologue(), but after doit() is finished with it.
2369
ResourceMark rm(current_thread);
2370
HandleMark hm(current_thread);
2371
JvmtiVTMSTransitionDisabler disabler(thread);
2372
ThreadsListHandle tlh(current_thread);
2374
JavaThread* java_thread = nullptr;
2375
oop thread_obj = nullptr;
2376
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2377
if (err != JVMTI_ERROR_NONE) {
2380
bool self = is_JavaThread_current(java_thread, thread_obj);
2384
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2385
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2386
depth, slot, T_LONG, val, self);
2387
VMThread::execute(&op);
2390
// Support for ordinary threads
2391
VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val, self);
2392
VMThread::execute(&op);
2396
} /* end SetLocalLong */
2399
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2400
// depth - pre-checked as non-negative
2402
JvmtiEnv::SetLocalFloat(jthread thread, jint depth, jint slot, jfloat value) {
2403
JavaThread* current_thread = JavaThread::current();
2404
// rm object is created to clean up the javaVFrame created in
2405
// doit_prologue(), but after doit() is finished with it.
2406
ResourceMark rm(current_thread);
2407
HandleMark hm(current_thread);
2408
JvmtiVTMSTransitionDisabler disabler(thread);
2409
ThreadsListHandle tlh(current_thread);
2411
JavaThread* java_thread = nullptr;
2412
oop thread_obj = nullptr;
2413
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2414
if (err != JVMTI_ERROR_NONE) {
2417
bool self = is_JavaThread_current(java_thread, thread_obj);
2421
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2422
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2423
depth, slot, T_FLOAT, val, self);
2424
VMThread::execute(&op);
2427
// Support for ordinary threads
2428
VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val, self);
2429
VMThread::execute(&op);
2433
} /* end SetLocalFloat */
2436
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2437
// depth - pre-checked as non-negative
2439
JvmtiEnv::SetLocalDouble(jthread thread, jint depth, jint slot, jdouble value) {
2440
JavaThread* current_thread = JavaThread::current();
2441
// rm object is created to clean up the javaVFrame created in
2442
// doit_prologue(), but after doit() is finished with it.
2443
ResourceMark rm(current_thread);
2444
HandleMark hm(current_thread);
2445
JvmtiVTMSTransitionDisabler disabler(thread);
2446
ThreadsListHandle tlh(current_thread);
2448
JavaThread* java_thread = nullptr;
2449
oop thread_obj = nullptr;
2450
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2451
if (err != JVMTI_ERROR_NONE) {
2454
bool self = is_JavaThread_current(java_thread, thread_obj);
2458
if (java_lang_VirtualThread::is_instance(thread_obj)) {
2459
VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2460
depth, slot, T_DOUBLE, val, self);
2461
VMThread::execute(&op);
2464
// Support for ordinary threads
2465
VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val, self);
2466
VMThread::execute(&op);
2470
} /* end SetLocalDouble */
2474
// Breakpoint functions
2477
// method - pre-checked for validity, but may be null meaning obsolete method
2479
JvmtiEnv::SetBreakpoint(Method* method, jlocation location) {
2480
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2481
if (location < 0) { // simple invalid location check first
2482
return JVMTI_ERROR_INVALID_LOCATION;
2484
// verify that the breakpoint is not past the end of the method
2485
if (location >= (jlocation) method->code_size()) {
2486
return JVMTI_ERROR_INVALID_LOCATION;
2490
JvmtiBreakpoint bp(method, location);
2491
JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2492
if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2493
return JVMTI_ERROR_DUPLICATE;
2495
if (TraceJVMTICalls) {
2496
jvmti_breakpoints.print();
2499
return JVMTI_ERROR_NONE;
2500
} /* end SetBreakpoint */
2503
// method - pre-checked for validity, but may be null meaning obsolete method
2505
JvmtiEnv::ClearBreakpoint(Method* method, jlocation location) {
2506
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2508
if (location < 0) { // simple invalid location check first
2509
return JVMTI_ERROR_INVALID_LOCATION;
2512
// verify that the breakpoint is not past the end of the method
2513
if (location >= (jlocation) method->code_size()) {
2514
return JVMTI_ERROR_INVALID_LOCATION;
2517
JvmtiBreakpoint bp(method, location);
2519
JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2520
if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2521
return JVMTI_ERROR_NOT_FOUND;
2523
if (TraceJVMTICalls) {
2524
jvmti_breakpoints.print();
2527
return JVMTI_ERROR_NONE;
2528
} /* end ClearBreakpoint */
2532
// Watched Field functions
2536
JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2537
// make sure we haven't set this watch before
2538
if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2539
fdesc_ptr->set_is_field_access_watched(true);
2541
JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2543
return JVMTI_ERROR_NONE;
2544
} /* end SetFieldAccessWatch */
2548
JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2549
// make sure we have a watch to clear
2550
if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2551
fdesc_ptr->set_is_field_access_watched(false);
2553
JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2555
return JVMTI_ERROR_NONE;
2556
} /* end ClearFieldAccessWatch */
2560
JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2561
// make sure we haven't set this watch before
2562
if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2563
fdesc_ptr->set_is_field_modification_watched(true);
2565
JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2567
return JVMTI_ERROR_NONE;
2568
} /* end SetFieldModificationWatch */
2572
JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2573
// make sure we have a watch to clear
2574
if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2575
fdesc_ptr->set_is_field_modification_watched(false);
2577
JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2579
return JVMTI_ERROR_NONE;
2580
} /* end ClearFieldModificationWatch */
2587
// k_mirror - may be primitive, this must be checked
2588
// signature_ptr - null is a valid value, must be checked
2589
// generic_ptr - null is a valid value, must be checked
2591
JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2593
bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2596
k = java_lang_Class::as_Klass(k_mirror);
2597
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2599
if (signature_ptr != nullptr) {
2600
char* result = nullptr;
2602
char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2603
result = (char*) jvmtiMalloc(2);
2607
const char* class_sig = k->signature_name();
2608
result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2609
strcpy(result, class_sig);
2611
*signature_ptr = result;
2613
if (generic_ptr != nullptr) {
2614
*generic_ptr = nullptr;
2615
if (!isPrimitive && k->is_instance_klass()) {
2616
Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2617
if (soo != nullptr) {
2618
const char *gen_sig = soo->as_C_string();
2619
if (gen_sig != nullptr) {
2621
jvmtiError err = allocate(strlen(gen_sig) + 1,
2622
(unsigned char **)&gen_result);
2623
if (err != JVMTI_ERROR_NONE) {
2626
strcpy(gen_result, gen_sig);
2627
*generic_ptr = gen_result;
2632
return JVMTI_ERROR_NONE;
2633
} /* end GetClassSignature */
2636
// k_mirror - may be primitive, this must be checked
2637
// status_ptr - pre-checked for null
2639
JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2641
if (java_lang_Class::is_primitive(k_mirror)) {
2642
result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2644
Klass* k = java_lang_Class::as_Klass(k_mirror);
2645
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2646
result = k->jvmti_class_status();
2648
*status_ptr = result;
2650
return JVMTI_ERROR_NONE;
2651
} /* end GetClassStatus */
2654
// k_mirror - may be primitive, this must be checked
2655
// source_name_ptr - pre-checked for null
2657
JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2658
if (java_lang_Class::is_primitive(k_mirror)) {
2659
return JVMTI_ERROR_ABSENT_INFORMATION;
2661
Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2662
NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2664
if (!k_klass->is_instance_klass()) {
2665
return JVMTI_ERROR_ABSENT_INFORMATION;
2668
Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2669
NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2671
JavaThread* current_thread = JavaThread::current();
2672
ResourceMark rm(current_thread);
2673
const char* sfncp = (const char*) sfnOop->as_C_string();
2674
*source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2675
strcpy(*source_name_ptr, sfncp);
2678
return JVMTI_ERROR_NONE;
2679
} /* end GetSourceFileName */
2682
// k_mirror - may be primitive, this must be checked
2683
// modifiers_ptr - pre-checked for null
2685
JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2686
JavaThread* current_thread = JavaThread::current();
2688
if (!java_lang_Class::is_primitive(k_mirror)) {
2689
Klass* k = java_lang_Class::as_Klass(k_mirror);
2690
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2691
result = k->compute_modifier_flags();
2693
// Reset the deleted ACC_SUPER bit (deleted in compute_modifier_flags()).
2694
if (k->is_super()) {
2695
result |= JVM_ACC_SUPER;
2698
result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2700
*modifiers_ptr = result;
2702
return JVMTI_ERROR_NONE;
2703
} /* end GetClassModifiers */
2706
// k_mirror - may be primitive, this must be checked
2707
// method_count_ptr - pre-checked for null
2708
// methods_ptr - pre-checked for null
2710
JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2711
JavaThread* current_thread = JavaThread::current();
2712
HandleMark hm(current_thread);
2714
if (java_lang_Class::is_primitive(k_mirror)) {
2715
*method_count_ptr = 0;
2716
*methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2717
return JVMTI_ERROR_NONE;
2719
Klass* k = java_lang_Class::as_Klass(k_mirror);
2720
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2722
// Return CLASS_NOT_PREPARED error as per JVMTI spec.
2723
if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2724
return JVMTI_ERROR_CLASS_NOT_PREPARED;
2727
if (!k->is_instance_klass()) {
2728
*method_count_ptr = 0;
2729
*methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2730
return JVMTI_ERROR_NONE;
2732
InstanceKlass* ik = InstanceKlass::cast(k);
2733
// Allocate the result and fill it in
2734
int result_length = ik->methods()->length();
2735
jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2737
bool jmethodids_found = true;
2738
int skipped = 0; // skip overpass methods
2740
for (index = 0; index < result_length; index++) {
2741
Method* m = ik->methods()->at(index);
2742
// Depending on can_maintain_original_method_order capability use the original
2743
// method ordering indices stored in the class, so we can emit jmethodIDs in
2744
// the order they appeared in the class file or just copy in current order.
2745
int result_index = JvmtiExport::can_maintain_original_method_order() ? ik->method_ordering()->at(index) : index;
2746
assert(result_index >= 0 && result_index < result_length, "invalid original method index");
2747
if (m->is_overpass()) {
2748
result_list[result_index] = nullptr;
2753
if (jmethodids_found) {
2754
id = m->find_jmethod_id_or_null();
2755
if (id == nullptr) {
2756
// If we find an uninitialized value, make sure there is
2757
// enough space for all the uninitialized values we might
2759
ik->ensure_space_for_methodids(index);
2760
jmethodids_found = false;
2761
id = m->jmethod_id();
2764
id = m->jmethod_id();
2766
result_list[result_index] = id;
2769
// Fill in return value.
2771
// copy results skipping null methodIDs
2772
*methods_ptr = (jmethodID*)jvmtiMalloc((result_length - skipped) * sizeof(jmethodID));
2773
*method_count_ptr = result_length - skipped;
2774
for (index = 0, skipped = 0; index < result_length; index++) {
2775
if (result_list[index] == nullptr) {
2778
(*methods_ptr)[index - skipped] = result_list[index];
2781
deallocate((unsigned char *)result_list);
2783
*method_count_ptr = result_length;
2784
*methods_ptr = result_list;
2787
return JVMTI_ERROR_NONE;
2788
} /* end GetClassMethods */
2791
// k_mirror - may be primitive, this must be checked
2792
// field_count_ptr - pre-checked for null
2793
// fields_ptr - pre-checked for null
2795
JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2796
if (java_lang_Class::is_primitive(k_mirror)) {
2797
*field_count_ptr = 0;
2798
*fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2799
return JVMTI_ERROR_NONE;
2801
JavaThread* current_thread = JavaThread::current();
2802
HandleMark hm(current_thread);
2803
Klass* k = java_lang_Class::as_Klass(k_mirror);
2804
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2806
// Return CLASS_NOT_PREPARED error as per JVMTI spec.
2807
if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2808
return JVMTI_ERROR_CLASS_NOT_PREPARED;
2811
if (!k->is_instance_klass()) {
2812
*field_count_ptr = 0;
2813
*fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2814
return JVMTI_ERROR_NONE;
2817
InstanceKlass* ik = InstanceKlass::cast(k);
2819
FilteredJavaFieldStream flds(ik);
2821
int result_count = flds.field_count();
2823
// Allocate the result and fill it in.
2824
jfieldID* result_list = (jfieldID*)jvmtiMalloc(result_count * sizeof(jfieldID));
2825
for (int i = 0; i < result_count; i++, flds.next()) {
2826
result_list[i] = jfieldIDWorkaround::to_jfieldID(ik, flds.offset(),
2827
flds.access_flags().is_static());
2829
assert(flds.done(), "just checking");
2831
// Fill in the results
2832
*field_count_ptr = result_count;
2833
*fields_ptr = result_list;
2835
return JVMTI_ERROR_NONE;
2836
} /* end GetClassFields */
2839
// k_mirror - may be primitive, this must be checked
2840
// interface_count_ptr - pre-checked for null
2841
// interfaces_ptr - pre-checked for null
2843
JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2845
if (java_lang_Class::is_primitive(k_mirror)) {
2846
*interface_count_ptr = 0;
2847
*interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2848
return JVMTI_ERROR_NONE;
2850
JavaThread* current_thread = JavaThread::current();
2851
HandleMark hm(current_thread);
2852
Klass* k = java_lang_Class::as_Klass(k_mirror);
2853
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2855
// Return CLASS_NOT_PREPARED error as per JVMTI spec.
2856
if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2857
return JVMTI_ERROR_CLASS_NOT_PREPARED;
2859
if (!k->is_instance_klass()) {
2860
*interface_count_ptr = 0;
2861
*interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2862
return JVMTI_ERROR_NONE;
2865
Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2866
const int result_length = (interface_list == nullptr ? 0 : interface_list->length());
2867
jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2868
for (int i_index = 0; i_index < result_length; i_index += 1) {
2869
InstanceKlass* klass_at = interface_list->at(i_index);
2870
assert(klass_at->is_klass(), "interfaces must be Klass*s");
2871
assert(klass_at->is_interface(), "interfaces must be interfaces");
2872
oop mirror_at = klass_at->java_mirror();
2873
Handle handle_at = Handle(current_thread, mirror_at);
2874
result_list[i_index] = (jclass) jni_reference(handle_at);
2876
*interface_count_ptr = result_length;
2877
*interfaces_ptr = result_list;
2880
return JVMTI_ERROR_NONE;
2881
} /* end GetImplementedInterfaces */
2884
// k_mirror - may be primitive, this must be checked
2885
// minor_version_ptr - pre-checked for null
2886
// major_version_ptr - pre-checked for null
2888
JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2889
if (java_lang_Class::is_primitive(k_mirror)) {
2890
return JVMTI_ERROR_ABSENT_INFORMATION;
2892
Klass* klass = java_lang_Class::as_Klass(k_mirror);
2894
jint status = klass->jvmti_class_status();
2895
if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2896
return JVMTI_ERROR_INVALID_CLASS;
2898
if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2899
return JVMTI_ERROR_ABSENT_INFORMATION;
2902
InstanceKlass* ik = InstanceKlass::cast(klass);
2903
*minor_version_ptr = ik->minor_version();
2904
*major_version_ptr = ik->major_version();
2906
return JVMTI_ERROR_NONE;
2907
} /* end GetClassVersionNumbers */
2910
// k_mirror - may be primitive, this must be checked
2911
// constant_pool_count_ptr - pre-checked for null
2912
// constant_pool_byte_count_ptr - pre-checked for null
2913
// constant_pool_bytes_ptr - pre-checked for null
2915
JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2916
if (java_lang_Class::is_primitive(k_mirror)) {
2917
return JVMTI_ERROR_ABSENT_INFORMATION;
2920
Klass* klass = java_lang_Class::as_Klass(k_mirror);
2921
Thread *thread = Thread::current();
2922
ResourceMark rm(thread);
2924
jint status = klass->jvmti_class_status();
2925
if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2926
return JVMTI_ERROR_INVALID_CLASS;
2928
if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2929
return JVMTI_ERROR_ABSENT_INFORMATION;
2932
InstanceKlass* ik = InstanceKlass::cast(klass);
2933
JvmtiConstantPoolReconstituter reconstituter(ik);
2934
if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2935
return reconstituter.get_error();
2938
unsigned char *cpool_bytes;
2939
int cpool_size = reconstituter.cpool_size();
2940
if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2941
return reconstituter.get_error();
2943
jvmtiError res = allocate(cpool_size, &cpool_bytes);
2944
if (res != JVMTI_ERROR_NONE) {
2947
reconstituter.copy_cpool_bytes(cpool_bytes);
2948
if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2949
return reconstituter.get_error();
2952
constantPoolHandle constants(thread, ik->constants());
2953
*constant_pool_count_ptr = constants->length();
2954
*constant_pool_byte_count_ptr = cpool_size;
2955
*constant_pool_bytes_ptr = cpool_bytes;
2957
return JVMTI_ERROR_NONE;
2958
} /* end GetConstantPool */
2961
// k_mirror - may be primitive, this must be checked
2962
// is_interface_ptr - pre-checked for null
2964
JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2966
bool result = false;
2967
if (!java_lang_Class::is_primitive(k_mirror)) {
2968
Klass* k = java_lang_Class::as_Klass(k_mirror);
2969
if (k != nullptr && k->is_interface()) {
2973
*is_interface_ptr = result;
2976
return JVMTI_ERROR_NONE;
2977
} /* end IsInterface */
2980
// k_mirror - may be primitive, this must be checked
2981
// is_array_class_ptr - pre-checked for null
2983
JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2985
bool result = false;
2986
if (!java_lang_Class::is_primitive(k_mirror)) {
2987
Klass* k = java_lang_Class::as_Klass(k_mirror);
2988
if (k != nullptr && k->is_array_klass()) {
2992
*is_array_class_ptr = result;
2995
return JVMTI_ERROR_NONE;
2996
} /* end IsArrayClass */
2999
// k_mirror - may be primitive, this must be checked
3000
// classloader_ptr - pre-checked for null
3002
JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
3004
if (java_lang_Class::is_primitive(k_mirror)) {
3005
*classloader_ptr = (jclass) jni_reference(Handle());
3006
return JVMTI_ERROR_NONE;
3008
JavaThread* current_thread = JavaThread::current();
3009
HandleMark hm(current_thread);
3010
Klass* k = java_lang_Class::as_Klass(k_mirror);
3011
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3013
oop result_oop = k->class_loader();
3014
if (result_oop == nullptr) {
3015
*classloader_ptr = (jclass) jni_reference(Handle());
3016
return JVMTI_ERROR_NONE;
3018
Handle result_handle = Handle(current_thread, result_oop);
3019
jclass result_jnihandle = (jclass) jni_reference(result_handle);
3020
*classloader_ptr = result_jnihandle;
3022
return JVMTI_ERROR_NONE;
3023
} /* end GetClassLoader */
3026
// k_mirror - may be primitive, this must be checked
3027
// source_debug_extension_ptr - pre-checked for null
3029
JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
3031
if (java_lang_Class::is_primitive(k_mirror)) {
3032
return JVMTI_ERROR_ABSENT_INFORMATION;
3034
Klass* k = java_lang_Class::as_Klass(k_mirror);
3035
NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3036
if (!k->is_instance_klass()) {
3037
return JVMTI_ERROR_ABSENT_INFORMATION;
3039
const char* sde = InstanceKlass::cast(k)->source_debug_extension();
3040
NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
3043
*source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
3044
strcpy(*source_debug_extension_ptr, sde);
3048
return JVMTI_ERROR_NONE;
3049
} /* end GetSourceDebugExtension */
3055
// hash_code_ptr - pre-checked for null
3057
JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
3058
oop mirror = JNIHandles::resolve_external_guard(object);
3059
NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
3060
NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
3063
jint result = (jint) mirror->identity_hash();
3064
*hash_code_ptr = result;
3066
return JVMTI_ERROR_NONE;
3067
} /* end GetObjectHashCode */
3070
// info_ptr - pre-checked for null
3072
JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
3073
// This needs to be performed at a safepoint to gather stable data
3074
// because monitor owner / waiters might not be suspended.
3075
VM_GetObjectMonitorUsage op(this, JavaThread::current(), object, info_ptr);
3076
VMThread::execute(&op);
3078
} /* end GetObjectMonitorUsage */
3085
// name_ptr - null is a valid value, must be checked
3086
// signature_ptr - null is a valid value, must be checked
3087
// generic_ptr - null is a valid value, must be checked
3089
JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3090
JavaThread* current_thread = JavaThread::current();
3091
ResourceMark rm(current_thread);
3092
if (name_ptr == nullptr) {
3093
// just don't return the name
3095
const char* fieldName = fdesc_ptr->name()->as_C_string();
3096
*name_ptr = (char*) jvmtiMalloc(strlen(fieldName) + 1);
3097
if (*name_ptr == nullptr)
3098
return JVMTI_ERROR_OUT_OF_MEMORY;
3099
strcpy(*name_ptr, fieldName);
3101
if (signature_ptr== nullptr) {
3102
// just don't return the signature
3104
const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
3105
*signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
3106
if (*signature_ptr == nullptr)
3107
return JVMTI_ERROR_OUT_OF_MEMORY;
3108
strcpy(*signature_ptr, fieldSignature);
3110
if (generic_ptr != nullptr) {
3111
*generic_ptr = nullptr;
3112
Symbol* soop = fdesc_ptr->generic_signature();
3113
if (soop != nullptr) {
3114
const char* gen_sig = soop->as_C_string();
3115
if (gen_sig != nullptr) {
3116
jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3117
if (err != JVMTI_ERROR_NONE) {
3120
strcpy(*generic_ptr, gen_sig);
3124
return JVMTI_ERROR_NONE;
3125
} /* end GetFieldName */
3128
// declaring_class_ptr - pre-checked for null
3130
JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
3132
*declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
3133
return JVMTI_ERROR_NONE;
3134
} /* end GetFieldDeclaringClass */
3137
// modifiers_ptr - pre-checked for null
3139
JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
3141
AccessFlags resultFlags = fdesc_ptr->access_flags();
3142
jint result = resultFlags.as_int();
3143
*modifiers_ptr = result;
3145
return JVMTI_ERROR_NONE;
3146
} /* end GetFieldModifiers */
3149
// is_synthetic_ptr - pre-checked for null
3151
JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
3152
*is_synthetic_ptr = fdesc_ptr->is_synthetic();
3153
return JVMTI_ERROR_NONE;
3154
} /* end IsFieldSynthetic */
3161
// method - pre-checked for validity, but may be null meaning obsolete method
3162
// name_ptr - null is a valid value, must be checked
3163
// signature_ptr - null is a valid value, must be checked
3164
// generic_ptr - null is a valid value, must be checked
3166
JvmtiEnv::GetMethodName(Method* method, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3167
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3168
JavaThread* current_thread = JavaThread::current();
3170
ResourceMark rm(current_thread); // get the utf8 name and signature
3171
if (name_ptr == nullptr) {
3172
// just don't return the name
3174
const char* utf8_name = (const char *) method->name()->as_utf8();
3175
*name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3176
strcpy(*name_ptr, utf8_name);
3178
if (signature_ptr == nullptr) {
3179
// just don't return the signature
3181
const char* utf8_signature = (const char *) method->signature()->as_utf8();
3182
*signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
3183
strcpy(*signature_ptr, utf8_signature);
3186
if (generic_ptr != nullptr) {
3187
*generic_ptr = nullptr;
3188
Symbol* soop = method->generic_signature();
3189
if (soop != nullptr) {
3190
const char* gen_sig = soop->as_C_string();
3191
if (gen_sig != nullptr) {
3192
jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3193
if (err != JVMTI_ERROR_NONE) {
3196
strcpy(*generic_ptr, gen_sig);
3200
return JVMTI_ERROR_NONE;
3201
} /* end GetMethodName */
3204
// method - pre-checked for validity, but may be null meaning obsolete method
3205
// declaring_class_ptr - pre-checked for null
3207
JvmtiEnv::GetMethodDeclaringClass(Method* method, jclass* declaring_class_ptr) {
3208
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3209
(*declaring_class_ptr) = get_jni_class_non_null(method->method_holder());
3210
return JVMTI_ERROR_NONE;
3211
} /* end GetMethodDeclaringClass */
3214
// method - pre-checked for validity, but may be null meaning obsolete method
3215
// modifiers_ptr - pre-checked for null
3217
JvmtiEnv::GetMethodModifiers(Method* method, jint* modifiers_ptr) {
3218
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3219
(*modifiers_ptr) = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
3220
return JVMTI_ERROR_NONE;
3221
} /* end GetMethodModifiers */
3224
// method - pre-checked for validity, but may be null meaning obsolete method
3225
// max_ptr - pre-checked for null
3227
JvmtiEnv::GetMaxLocals(Method* method, jint* max_ptr) {
3228
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3230
(*max_ptr) = method->max_locals();
3231
return JVMTI_ERROR_NONE;
3232
} /* end GetMaxLocals */
3235
// method - pre-checked for validity, but may be null meaning obsolete method
3236
// size_ptr - pre-checked for null
3238
JvmtiEnv::GetArgumentsSize(Method* method, jint* size_ptr) {
3239
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3240
// get size of arguments
3242
(*size_ptr) = method->size_of_parameters();
3243
return JVMTI_ERROR_NONE;
3244
} /* end GetArgumentsSize */
3247
// method - pre-checked for validity, but may be null meaning obsolete method
3248
// entry_count_ptr - pre-checked for null
3249
// table_ptr - pre-checked for null
3251
JvmtiEnv::GetLineNumberTable(Method* method, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3252
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3253
if (!method->has_linenumber_table()) {
3254
return (JVMTI_ERROR_ABSENT_INFORMATION);
3257
// The line number table is compressed so we don't know how big it is until decompressed.
3258
// Decompression is really fast so we just do it twice.
3260
// Compute size of table
3261
jint num_entries = 0;
3262
CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3263
while (stream.read_pair()) {
3266
jvmtiLineNumberEntry *jvmti_table =
3267
(jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3270
if (num_entries > 0) {
3272
CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3273
while (stream.read_pair()) {
3274
jvmti_table[index].start_location = (jlocation) stream.bci();
3275
jvmti_table[index].line_number = (jint) stream.line();
3278
assert(index == num_entries, "sanity check");
3282
(*entry_count_ptr) = num_entries;
3283
(*table_ptr) = jvmti_table;
3285
return JVMTI_ERROR_NONE;
3286
} /* end GetLineNumberTable */
3289
// method - pre-checked for validity, but may be null meaning obsolete method
3290
// start_location_ptr - pre-checked for null
3291
// end_location_ptr - pre-checked for null
3293
JvmtiEnv::GetMethodLocation(Method* method, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3295
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3296
// get start and end location
3297
(*end_location_ptr) = (jlocation) (method->code_size() - 1);
3298
if (method->code_size() == 0) {
3299
// there is no code so there is no start location
3300
(*start_location_ptr) = (jlocation)(-1);
3302
(*start_location_ptr) = (jlocation)(0);
3305
return JVMTI_ERROR_NONE;
3306
} /* end GetMethodLocation */
3309
// method - pre-checked for validity, but may be null meaning obsolete method
3310
// entry_count_ptr - pre-checked for null
3311
// table_ptr - pre-checked for null
3313
JvmtiEnv::GetLocalVariableTable(Method* method, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3315
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3316
JavaThread* current_thread = JavaThread::current();
3318
// does the klass have any local variable information?
3319
InstanceKlass* ik = method->method_holder();
3320
if (!ik->has_localvariable_table()) {
3321
return (JVMTI_ERROR_ABSENT_INFORMATION);
3324
ConstantPool* constants = method->constants();
3325
NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3327
// in the vm localvariable table representation, 6 consecutive elements in the table
3328
// represent a 6-tuple of shorts
3329
// [start_pc, length, name_index, descriptor_index, signature_index, index]
3330
jint num_entries = method->localvariable_table_length();
3331
jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3332
jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3334
if (num_entries > 0) {
3335
LocalVariableTableElement* table = method->localvariable_table_start();
3336
for (int i = 0; i < num_entries; i++) {
3337
// get the 5 tuple information from the vm table
3338
jlocation start_location = (jlocation) table[i].start_bci;
3339
jint length = (jint) table[i].length;
3340
int name_index = (int) table[i].name_cp_index;
3341
int signature_index = (int) table[i].descriptor_cp_index;
3342
int generic_signature_index = (int) table[i].signature_cp_index;
3343
jint slot = (jint) table[i].slot;
3345
// get utf8 name and signature
3346
char *name_buf = nullptr;
3347
char *sig_buf = nullptr;
3348
char *gen_sig_buf = nullptr;
3350
ResourceMark rm(current_thread);
3352
const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3353
name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3354
strcpy(name_buf, utf8_name);
3356
const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3357
sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3358
strcpy(sig_buf, utf8_signature);
3360
if (generic_signature_index > 0) {
3361
const char *utf8_gen_sign = (const char *)
3362
constants->symbol_at(generic_signature_index)->as_utf8();
3363
gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3364
strcpy(gen_sig_buf, utf8_gen_sign);
3368
// fill in the jvmti local variable table
3369
jvmti_table[i].start_location = start_location;
3370
jvmti_table[i].length = length;
3371
jvmti_table[i].name = name_buf;
3372
jvmti_table[i].signature = sig_buf;
3373
jvmti_table[i].generic_signature = gen_sig_buf;
3374
jvmti_table[i].slot = slot;
3379
(*entry_count_ptr) = num_entries;
3380
(*table_ptr) = jvmti_table;
3382
return JVMTI_ERROR_NONE;
3383
} /* end GetLocalVariableTable */
3386
// method - pre-checked for validity, but may be null meaning obsolete method
3387
// bytecode_count_ptr - pre-checked for null
3388
// bytecodes_ptr - pre-checked for null
3390
JvmtiEnv::GetBytecodes(Method* method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3391
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3393
methodHandle mh(Thread::current(), method);
3394
jint size = (jint)mh->code_size();
3395
jvmtiError err = allocate(size, bytecodes_ptr);
3396
if (err != JVMTI_ERROR_NONE) {
3400
(*bytecode_count_ptr) = size;
3402
JvmtiClassFileReconstituter::copy_bytecodes(mh, *bytecodes_ptr);
3404
return JVMTI_ERROR_NONE;
3405
} /* end GetBytecodes */
3408
// method - pre-checked for validity, but may be null meaning obsolete method
3409
// is_native_ptr - pre-checked for null
3411
JvmtiEnv::IsMethodNative(Method* method, jboolean* is_native_ptr) {
3412
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3413
(*is_native_ptr) = method->is_native();
3414
return JVMTI_ERROR_NONE;
3415
} /* end IsMethodNative */
3418
// method - pre-checked for validity, but may be null meaning obsolete method
3419
// is_synthetic_ptr - pre-checked for null
3421
JvmtiEnv::IsMethodSynthetic(Method* method, jboolean* is_synthetic_ptr) {
3422
NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3423
(*is_synthetic_ptr) = method->is_synthetic();
3424
return JVMTI_ERROR_NONE;
3425
} /* end IsMethodSynthetic */
3428
// method - pre-checked for validity, but may be null meaning obsolete method
3429
// is_obsolete_ptr - pre-checked for null
3431
JvmtiEnv::IsMethodObsolete(Method* method, jboolean* is_obsolete_ptr) {
3432
if (use_version_1_0_semantics() &&
3433
get_capabilities()->can_redefine_classes == 0) {
3434
// This JvmtiEnv requested version 1.0 semantics and this function
3435
// requires the can_redefine_classes capability in version 1.0 so
3436
// we need to return an error here.
3437
return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3440
if (method == nullptr || method->is_obsolete()) {
3441
*is_obsolete_ptr = true;
3443
*is_obsolete_ptr = false;
3445
return JVMTI_ERROR_NONE;
3446
} /* end IsMethodObsolete */
3449
// Raw Monitor functions
3452
// name - pre-checked for null
3453
// monitor_ptr - pre-checked for null
3455
JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3456
JvmtiRawMonitor* rmonitor = new (std::nothrow) JvmtiRawMonitor(name);
3457
NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3459
*monitor_ptr = (jrawMonitorID)rmonitor;
3461
return JVMTI_ERROR_NONE;
3462
} /* end CreateRawMonitor */
3465
// rmonitor - pre-checked for validity
3467
JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3468
if (Threads::number_of_threads() == 0) {
3469
// Remove this monitor from pending raw monitors list
3470
// if it has entered in onload or start phase.
3471
JvmtiPendingMonitors::destroy(rmonitor);
3473
Thread* thread = Thread::current();
3474
if (rmonitor->owner() == thread) {
3475
// The caller owns this monitor which we are about to destroy.
3476
// We exit the underlying synchronization object so that the
3477
// "delete monitor" call below can work without an assertion
3478
// failure on systems that don't like destroying synchronization
3479
// objects that are locked.
3481
int recursion = rmonitor->recursions();
3482
for (int i = 0; i <= recursion; i++) {
3483
r = rmonitor->raw_exit(thread);
3484
assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked");
3485
if (r != JvmtiRawMonitor::M_OK) { // robustness
3486
return JVMTI_ERROR_INTERNAL;
3490
if (rmonitor->owner() != nullptr) {
3491
// The caller is trying to destroy a monitor that is locked by
3492
// someone else. While this is not forbidden by the JVMTI
3493
// spec, it will cause an assertion failure on systems that don't
3494
// like destroying synchronization objects that are locked.
3495
// We indicate a problem with the error return (and leak the
3496
// monitor's memory).
3497
return JVMTI_ERROR_NOT_MONITOR_OWNER;
3503
return JVMTI_ERROR_NONE;
3504
} /* end DestroyRawMonitor */
3507
// rmonitor - pre-checked for validity
3509
JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3510
if (Threads::number_of_threads() == 0) {
3511
// No JavaThreads exist so JvmtiRawMonitor enter cannot be
3512
// used, add this raw monitor to the pending list.
3513
// The pending monitors will be actually entered when
3515
// See transition_pending_raw_monitors in create_vm()
3517
JvmtiPendingMonitors::enter(rmonitor);
3519
Thread* thread = Thread::current();
3520
// 8266889: raw_enter changes Java thread state, needs WXWrite
3521
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3522
rmonitor->raw_enter(thread);
3524
return JVMTI_ERROR_NONE;
3525
} /* end RawMonitorEnter */
3528
// rmonitor - pre-checked for validity
3530
JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3531
jvmtiError err = JVMTI_ERROR_NONE;
3533
if (Threads::number_of_threads() == 0) {
3534
// No JavaThreads exist so just remove this monitor from the pending list.
3535
// Bool value from exit is false if rmonitor is not in the list.
3536
if (!JvmtiPendingMonitors::exit(rmonitor)) {
3537
err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3540
Thread* thread = Thread::current();
3541
int r = rmonitor->raw_exit(thread);
3542
if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3543
err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3547
} /* end RawMonitorExit */
3550
// rmonitor - pre-checked for validity
3552
JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3553
Thread* thread = Thread::current();
3554
// 8266889: raw_wait changes Java thread state, needs WXWrite
3555
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3556
int r = rmonitor->raw_wait(millis, thread);
3559
case JvmtiRawMonitor::M_INTERRUPTED:
3560
return JVMTI_ERROR_INTERRUPT;
3561
case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE:
3562
return JVMTI_ERROR_NOT_MONITOR_OWNER;
3564
return JVMTI_ERROR_NONE;
3566
} /* end RawMonitorWait */
3569
// rmonitor - pre-checked for validity
3571
JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3572
Thread* thread = Thread::current();
3573
int r = rmonitor->raw_notify(thread);
3575
if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3576
return JVMTI_ERROR_NOT_MONITOR_OWNER;
3578
return JVMTI_ERROR_NONE;
3579
} /* end RawMonitorNotify */
3582
// rmonitor - pre-checked for validity
3584
JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3585
Thread* thread = Thread::current();
3586
int r = rmonitor->raw_notifyAll(thread);
3588
if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3589
return JVMTI_ERROR_NOT_MONITOR_OWNER;
3591
return JVMTI_ERROR_NONE;
3592
} /* end RawMonitorNotifyAll */
3596
// JNI Function Interception functions
3600
// function_table - pre-checked for null
3602
JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3603
// Copy jni function table at safepoint.
3604
VM_JNIFunctionTableCopier copier(function_table);
3605
VMThread::execute(&copier);
3607
return JVMTI_ERROR_NONE;
3608
} /* end SetJNIFunctionTable */
3611
// function_table - pre-checked for null
3613
JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3614
*function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3615
if (*function_table == nullptr)
3616
return JVMTI_ERROR_OUT_OF_MEMORY;
3617
memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3618
return JVMTI_ERROR_NONE;
3619
} /* end GetJNIFunctionTable */
3623
// Event Management functions
3627
JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3628
// can only generate two event types
3629
if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3630
event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3631
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3634
// for compiled_method_load events we must check that the environment
3635
// has the can_generate_compiled_method_load_events capability.
3636
if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3637
if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3638
return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3640
return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3642
return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3645
} /* end GenerateEvents */
3649
// Extension Mechanism functions
3652
// extension_count_ptr - pre-checked for null
3653
// extensions - pre-checked for null
3655
JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3656
return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3657
} /* end GetExtensionFunctions */
3660
// extension_count_ptr - pre-checked for null
3661
// extensions - pre-checked for null
3663
JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3664
return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3665
} /* end GetExtensionEvents */
3668
// callback - null is a valid value, must be checked
3670
JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3671
return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3672
} /* end SetExtensionEventCallback */
3678
// info_ptr - pre-checked for null
3680
JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3681
os::current_thread_cpu_time_info(info_ptr);
3682
return JVMTI_ERROR_NONE;
3683
} /* end GetCurrentThreadCpuTimerInfo */
3686
// nanos_ptr - pre-checked for null
3688
JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3689
Thread* thread = Thread::current();
3691
// Surprisingly the GetCurrentThreadCpuTime is used by non-JavaThread's.
3692
if (thread->is_Java_thread()) {
3693
if (JavaThread::cast(thread)->is_vthread_mounted()) {
3694
// No support for a VirtualThread (yet).
3695
return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3698
*nanos_ptr = os::current_thread_cpu_time();
3699
return JVMTI_ERROR_NONE;
3700
} /* end GetCurrentThreadCpuTime */
3703
// info_ptr - pre-checked for null
3705
JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3706
os::thread_cpu_time_info(info_ptr);
3707
return JVMTI_ERROR_NONE;
3708
} /* end GetThreadCpuTimerInfo */
3711
// nanos_ptr - pre-checked for null
3713
JvmtiEnv::GetThreadCpuTime(jthread thread, jlong* nanos_ptr) {
3714
JavaThread* current_thread = JavaThread::current();
3715
ThreadsListHandle tlh(current_thread);
3716
JavaThread* java_thread = nullptr;
3717
oop thread_oop = nullptr;
3719
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
3721
if (thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
3722
// No support for virtual threads (yet).
3723
return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3725
if (err != JVMTI_ERROR_NONE) {
3728
NULL_CHECK(nanos_ptr, JVMTI_ERROR_NULL_POINTER);
3730
*nanos_ptr = os::thread_cpu_time(java_thread);
3731
return JVMTI_ERROR_NONE;
3732
} /* end GetThreadCpuTime */
3735
// info_ptr - pre-checked for null
3737
JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3738
os::javaTimeNanos_info(info_ptr);
3739
return JVMTI_ERROR_NONE;
3740
} /* end GetTimerInfo */
3743
// nanos_ptr - pre-checked for null
3745
JvmtiEnv::GetTime(jlong* nanos_ptr) {
3746
*nanos_ptr = os::javaTimeNanos();
3747
return JVMTI_ERROR_NONE;
3751
// processor_count_ptr - pre-checked for null
3753
JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3754
*processor_count_ptr = os::active_processor_count();
3755
return JVMTI_ERROR_NONE;
3756
} /* end GetAvailableProcessors */
3759
JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3760
if (sampling_interval < 0) {
3761
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3763
ThreadHeapSampler::set_sampling_interval(sampling_interval);
3764
return JVMTI_ERROR_NONE;
3765
} /* end SetHeapSamplingInterval */
3768
// System Properties functions
3771
// count_ptr - pre-checked for null
3772
// property_ptr - pre-checked for null
3774
JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3775
jvmtiError err = JVMTI_ERROR_NONE;
3777
// Get the number of readable properties.
3778
*count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3780
// Allocate memory to hold the exact number of readable properties.
3781
err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3782
if (err != JVMTI_ERROR_NONE) {
3785
int readable_count = 0;
3786
// Loop through the system properties until all the readable properties are found.
3787
for (SystemProperty* p = Arguments::system_properties(); p != nullptr && readable_count < *count_ptr; p = p->next()) {
3788
if (p->readable()) {
3789
const char *key = p->key();
3790
char **tmp_value = *property_ptr+readable_count;
3792
err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3793
if (err == JVMTI_ERROR_NONE) {
3794
strcpy(*tmp_value, key);
3796
// clean up previously allocated memory.
3797
for (int j = 0; j < readable_count; j++) {
3798
Deallocate((unsigned char*)*property_ptr+j);
3800
Deallocate((unsigned char*)property_ptr);
3805
assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3807
} /* end GetSystemProperties */
3810
// property - pre-checked for null
3811
// value_ptr - pre-checked for null
3813
JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3814
jvmtiError err = JVMTI_ERROR_NONE;
3817
// Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3818
value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3819
if (value == nullptr) {
3820
err = JVMTI_ERROR_NOT_AVAILABLE;
3822
err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3823
if (err == JVMTI_ERROR_NONE) {
3824
strcpy(*value_ptr, value);
3828
} /* end GetSystemProperty */
3831
// property - pre-checked for null
3832
// value - null is a valid value, must be checked
3834
JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3835
for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
3836
if (strcmp(property, p->key()) == 0) {
3837
if (p->writeable()) {
3838
if (p->set_value(value_ptr, AllocFailStrategy::RETURN_NULL)) {
3839
return JVMTI_ERROR_NONE;
3841
return JVMTI_ERROR_OUT_OF_MEMORY;
3844
// We found a property, but it's not writeable
3845
return JVMTI_ERROR_NOT_AVAILABLE;
3850
// We cannot find a property of the given name
3851
return JVMTI_ERROR_NOT_AVAILABLE;
3852
} /* end SetSystemProperty */