1 /*
2 * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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.
8 *
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).
14 *
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.
18 *
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
21 * questions.
22 *
23 */
24
25 #include "classfile/javaClasses.inline.hpp"
26 #include "classfile/modules.hpp"
27 #include "classfile/stringTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmClasses.hpp"
30 #include "classfile/vmSymbols.hpp"
31 #include "gc/shared/collectedHeap.hpp"
32 #include "interpreter/bytecodeStream.hpp"
33 #include "interpreter/interpreter.hpp"
34 #include "jfr/jfrEvents.hpp"
35 #include "jvmtifiles/jvmtiEnv.hpp"
36 #include "logging/log.hpp"
37 #include "logging/logConfiguration.hpp"
38 #include "memory/allocation.hpp"
39 #include "memory/resourceArea.hpp"
40 #include "memory/universe.hpp"
41 #include "oops/fieldStreams.inline.hpp"
42 #include "oops/instanceKlass.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "oops/objArrayOop.inline.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "prims/jniCheck.hpp"
47 #include "prims/jvm_misc.hpp"
48 #include "prims/jvmtiAgentThread.hpp"
49 #include "prims/jvmtiClassFileReconstituter.hpp"
50 #include "prims/jvmtiCodeBlobEvents.hpp"
51 #include "prims/jvmtiExtensions.hpp"
52 #include "prims/jvmtiGetLoadedClasses.hpp"
53 #include "prims/jvmtiImpl.hpp"
54 #include "prims/jvmtiManageCapabilities.hpp"
55 #include "prims/jvmtiRawMonitor.hpp"
56 #include "prims/jvmtiRedefineClasses.hpp"
57 #include "prims/jvmtiTagMap.hpp"
58 #include "prims/jvmtiThreadState.inline.hpp"
59 #include "prims/jvmtiUtil.hpp"
60 #include "runtime/arguments.hpp"
61 #include "runtime/deoptimization.hpp"
62 #include "runtime/fieldDescriptor.inline.hpp"
63 #include "runtime/handles.inline.hpp"
64 #include "runtime/interfaceSupport.inline.hpp"
65 #include "runtime/javaCalls.hpp"
66 #include "runtime/javaThread.inline.hpp"
67 #include "runtime/jfieldIDWorkaround.hpp"
68 #include "runtime/jniHandles.inline.hpp"
69 #include "runtime/mountUnmountDisabler.hpp"
70 #include "runtime/objectMonitor.inline.hpp"
71 #include "runtime/os.hpp"
72 #include "runtime/osThread.hpp"
73 #include "runtime/signature.hpp"
74 #include "runtime/threadHeapSampler.hpp"
75 #include "runtime/threads.hpp"
76 #include "runtime/threadSMR.hpp"
77 #include "runtime/timerTrace.hpp"
78 #include "runtime/vframe.inline.hpp"
79 #include "runtime/vmThread.hpp"
80 #include "services/threadService.hpp"
81 #include "utilities/exceptions.hpp"
82 #include "utilities/preserveException.hpp"
83 #include "utilities/utf8.hpp"
84
85
86 #define FIXLATER 0 // REMOVE this when completed.
87
88 // FIXLATER: hook into JvmtiTrace
89 #define TraceJVMTICalls false
90
91 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
92 }
93
94 JvmtiEnv::~JvmtiEnv() {
95 }
96
97 JvmtiEnv*
98 JvmtiEnv::create_a_jvmti(jint version) {
99 return new JvmtiEnv(version);
100 }
101
102 // VM operation class to copy jni function table at safepoint.
103 // More than one java threads or jvmti agents may be reading/
104 // modifying jni function tables. To reduce the risk of bad
105 // interaction b/w these threads it is copied at safepoint.
106 class VM_JNIFunctionTableCopier : public VM_Operation {
107 private:
108 const struct JNINativeInterface_ *_function_table;
109 public:
110 VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
111 _function_table = func_tbl;
112 };
113
114 VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
115 void doit() {
116 copy_jni_function_table(_function_table);
117 };
118 };
119
120 //
121 // Do not change the "prefix" marker below, everything above it is copied
122 // unchanged into the filled stub, everything below is controlled by the
123 // stub filler (only method bodies are carried forward, and then only for
124 // functionality still in the spec).
125 //
126 // end file prefix
127
128 //
129 // Memory Management functions
130 //
131
132 // mem_ptr - pre-checked for null
133 jvmtiError
134 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
135 return allocate(size, mem_ptr);
136 } /* end Allocate */
137
138
139 // mem - null is a valid value, must be checked
140 jvmtiError
141 JvmtiEnv::Deallocate(unsigned char* mem) {
142 return deallocate(mem);
143 } /* end Deallocate */
144
145 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
146 // data - null is a valid value, must be checked
147 jvmtiError
148 JvmtiEnv::SetThreadLocalStorage(jthread thread, const void* data) {
149 JavaThread* current = JavaThread::current();
150 JvmtiThreadState* state = nullptr;
151 MountUnmountDisabler disabler(thread);
152 ThreadsListHandle tlh(current);
153
154 JavaThread* java_thread = nullptr;
155 oop thread_obj = nullptr;
156 if (thread == nullptr) {
157 java_thread = current;
158 state = java_thread->jvmti_thread_state();
159 } else {
160 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
161 if (err != JVMTI_ERROR_NONE) {
162 return err;
163 }
164 state = java_lang_Thread::jvmti_thread_state(thread_obj);
165 }
166 if (state == nullptr) {
167 if (data == nullptr) {
168 // leaving state unset same as data set to null
169 return JVMTI_ERROR_NONE;
170 }
171 // otherwise, create the state
172 HandleMark hm(current);
173 Handle thread_handle(current, thread_obj);
174 state = JvmtiThreadState::state_for(java_thread, thread_handle);
175 if (state == nullptr) {
176 return JVMTI_ERROR_THREAD_NOT_ALIVE;
177 }
178 }
179 state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
180 return JVMTI_ERROR_NONE;
181 } /* end SetThreadLocalStorage */
182
183
184 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
185 // data_ptr - pre-checked for null
186 jvmtiError
187 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
188 JavaThread* current_thread = JavaThread::current();
189 if (thread == nullptr) {
190 JvmtiThreadState* state = current_thread->jvmti_thread_state();
191 *data_ptr = (state == nullptr) ? nullptr :
192 state->env_thread_state(this)->get_agent_thread_local_storage_data();
193 } else {
194 // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
195 // the thread to _thread_in_vm. However, when the TLS for a thread
196 // other than the current thread is required we need to transition
197 // from native so as to resolve the jthread.
198
199 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
200 ThreadInVMfromNative __tiv(current_thread);
201 VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
202 DEBUG_ONLY(VMNativeEntryWrapper __vew;)
203
204 MountUnmountDisabler disabler(thread);
205 ThreadsListHandle tlh(current_thread);
206
207 JavaThread* java_thread = nullptr;
208 oop thread_obj = nullptr;
209 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
210 if (err != JVMTI_ERROR_NONE) {
211 return err;
212 }
213
214 HandleMark hm(current_thread);
215 Handle thread_handle(current_thread, thread_obj);
216 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread, thread_handle);
217 *data_ptr = (state == nullptr) ? nullptr :
218 state->env_thread_state(this)->get_agent_thread_local_storage_data();
219 }
220 return JVMTI_ERROR_NONE;
221 } /* end GetThreadLocalStorage */
222
223 //
224 // Module functions
225 //
226
227 // module_count_ptr - pre-checked for null
228 // modules_ptr - pre-checked for null
229 jvmtiError
230 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
231 JvmtiModuleClosure jmc;
232
233 return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
234 } /* end GetAllModules */
235
236
237 // class_loader - null is a valid value, must be pre-checked
238 // package_name - pre-checked for null
239 // module_ptr - pre-checked for null
240 jvmtiError
241 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
242 JavaThread* THREAD = JavaThread::current(); // For exception macros.
243 ResourceMark rm(THREAD);
244
245 Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
246 // Check that loader is a subclass of java.lang.ClassLoader.
247 if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
248 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
249 }
250 oop module = Modules::get_named_module(h_loader, package_name);
251 *module_ptr = module != nullptr ? JNIHandles::make_local(THREAD, module) : nullptr;
252 return JVMTI_ERROR_NONE;
253 } /* end GetNamedModule */
254
255
256 // module - pre-checked for null
257 // to_module - pre-checked for null
258 jvmtiError
259 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
260 JavaThread* THREAD = JavaThread::current(); // For exception macros.
261
262 // check module
263 Handle h_module(THREAD, JNIHandles::resolve(module));
264 if (!java_lang_Module::is_instance(h_module())) {
265 return JVMTI_ERROR_INVALID_MODULE;
266 }
267 // check to_module
268 Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
269 if (!java_lang_Module::is_instance(h_to_module())) {
270 return JVMTI_ERROR_INVALID_MODULE;
271 }
272 return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
273 } /* end AddModuleReads */
274
275
276 // module - pre-checked for null
277 // pkg_name - pre-checked for null
278 // to_module - pre-checked for null
279 jvmtiError
280 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
281 JavaThread* THREAD = JavaThread::current(); // For exception macros.
282 Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
283
284 // check module
285 Handle h_module(THREAD, JNIHandles::resolve(module));
286 if (!java_lang_Module::is_instance(h_module())) {
287 return JVMTI_ERROR_INVALID_MODULE;
288 }
289 // check to_module
290 Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
291 if (!java_lang_Module::is_instance(h_to_module())) {
292 return JVMTI_ERROR_INVALID_MODULE;
293 }
294 return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
295 } /* end AddModuleExports */
296
297
298 // module - pre-checked for null
299 // pkg_name - pre-checked for null
300 // to_module - pre-checked for null
301 jvmtiError
302 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
303 JavaThread* THREAD = JavaThread::current(); // For exception macros.
304 Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
305
306 // check module
307 Handle h_module(THREAD, JNIHandles::resolve(module));
308 if (!java_lang_Module::is_instance(h_module())) {
309 return JVMTI_ERROR_INVALID_MODULE;
310 }
311 // check to_module
312 Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
313 if (!java_lang_Module::is_instance(h_to_module())) {
314 return JVMTI_ERROR_INVALID_MODULE;
315 }
316 return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
317 } /* end AddModuleOpens */
318
319
320 // module - pre-checked for null
321 // service - pre-checked for null
322 jvmtiError
323 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
324 JavaThread* THREAD = JavaThread::current(); // For exception macros.
325
326 // check module
327 Handle h_module(THREAD, JNIHandles::resolve(module));
328 if (!java_lang_Module::is_instance(h_module())) {
329 return JVMTI_ERROR_INVALID_MODULE;
330 }
331 // check service
332 Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
333 if (!java_lang_Class::is_instance(h_service()) ||
334 java_lang_Class::is_primitive(h_service())) {
335 return JVMTI_ERROR_INVALID_CLASS;
336 }
337 return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
338 } /* end AddModuleUses */
339
340
341 // module - pre-checked for null
342 // service - pre-checked for null
343 // impl_class - pre-checked for null
344 jvmtiError
345 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
346 JavaThread* THREAD = JavaThread::current(); // For exception macros.
347
348 // check module
349 Handle h_module(THREAD, JNIHandles::resolve(module));
350 if (!java_lang_Module::is_instance(h_module())) {
351 return JVMTI_ERROR_INVALID_MODULE;
352 }
353 // check service
354 Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
355 if (!java_lang_Class::is_instance(h_service()) ||
356 java_lang_Class::is_primitive(h_service())) {
357 return JVMTI_ERROR_INVALID_CLASS;
358 }
359 // check impl_class
360 Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
361 if (!java_lang_Class::is_instance(h_impl_class()) ||
362 java_lang_Class::is_primitive(h_impl_class())) {
363 return JVMTI_ERROR_INVALID_CLASS;
364 }
365 return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
366 } /* end AddModuleProvides */
367
368 // module - pre-checked for null
369 // is_modifiable_class_ptr - pre-checked for null
370 jvmtiError
371 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
372 JavaThread* current = JavaThread::current();
373
374 // check module
375 Handle h_module(current, JNIHandles::resolve(module));
376 if (!java_lang_Module::is_instance(h_module())) {
377 return JVMTI_ERROR_INVALID_MODULE;
378 }
379
380 *is_modifiable_module_ptr = JNI_TRUE;
381 return JVMTI_ERROR_NONE;
382 } /* end IsModifiableModule */
383
384
385 //
386 // Class functions
387 //
388
389 // class_count_ptr - pre-checked for null
390 // classes_ptr - pre-checked for null
391 jvmtiError
392 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
393 return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
394 } /* end GetLoadedClasses */
395
396
397 // initiating_loader - null is a valid value, must be checked
398 // class_count_ptr - pre-checked for null
399 // classes_ptr - pre-checked for null
400 jvmtiError
401 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
402 return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
403 class_count_ptr, classes_ptr);
404 } /* end GetClassLoaderClasses */
405
406 // k_mirror - may be primitive, this must be checked
407 // is_modifiable_class_ptr - pre-checked for null
408 jvmtiError
409 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
410 *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
411 JNI_TRUE : JNI_FALSE;
412 return JVMTI_ERROR_NONE;
413 } /* end IsModifiableClass */
414
415 // class_count - pre-checked to be greater than or equal to 0
416 // classes - pre-checked for null
417 jvmtiError
418 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
419 //TODO: add locking
420
421 int index;
422 JavaThread* current_thread = JavaThread::current();
423 ResourceMark rm(current_thread);
424
425 jvmtiClassDefinition* class_definitions =
426 NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
427 NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
428
429 for (index = 0; index < class_count; index++) {
430 HandleMark hm(current_thread);
431
432 jclass jcls = classes[index];
433 oop k_mirror = JNIHandles::resolve_external_guard(jcls);
434 if (k_mirror == nullptr) {
435 return JVMTI_ERROR_INVALID_CLASS;
436 }
437 if (!k_mirror->is_a(vmClasses::Class_klass())) {
438 return JVMTI_ERROR_INVALID_CLASS;
439 }
440
441 if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
442 return JVMTI_ERROR_UNMODIFIABLE_CLASS;
443 }
444
445 Klass* klass = java_lang_Class::as_Klass(k_mirror);
446
447 jint status = klass->jvmti_class_status();
448 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
449 return JVMTI_ERROR_INVALID_CLASS;
450 }
451
452 InstanceKlass* ik = InstanceKlass::cast(klass);
453 if (ik->get_cached_class_file_bytes() == nullptr) {
454 // Link the class to avoid races with the rewriter. This will call the verifier also
455 // on the class. Linking is also done in VM_RedefineClasses below, but we need
456 // to keep that for other VM_RedefineClasses callers.
457 JavaThread* THREAD = current_thread;
458 ik->link_class(THREAD);
459 if (HAS_PENDING_EXCEPTION) {
460 // Retransform/JVMTI swallows error messages. Using this class will rerun the verifier in a context
461 // that propagates the VerifyError, if thrown.
462 CLEAR_PENDING_EXCEPTION;
463 return JVMTI_ERROR_INVALID_CLASS;
464 }
465
466 // Not cached, we need to reconstitute the class file from the
467 // VM representation. We don't attach the reconstituted class
468 // bytes to the InstanceKlass here because they have not been
469 // validated and we're not at a safepoint.
470 JvmtiClassFileReconstituter reconstituter(ik);
471 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
472 return reconstituter.get_error();
473 }
474
475 class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
476 class_definitions[index].class_bytes = (unsigned char*)
477 reconstituter.class_file_bytes();
478 } else {
479 // it is cached, get it from the cache
480 class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
481 class_definitions[index].class_bytes = ik->get_cached_class_file_bytes();
482 }
483 class_definitions[index].klass = jcls;
484 }
485 EventRetransformClasses event;
486 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
487 VMThread::execute(&op);
488 jvmtiError error = op.check_error();
489 if (error == JVMTI_ERROR_NONE) {
490 event.set_classCount(class_count);
491 event.set_redefinitionId(op.id());
492 event.commit();
493 }
494 return error;
495 } /* end RetransformClasses */
496
497
498 // class_count - pre-checked to be greater than or equal to 0
499 // class_definitions - pre-checked for null
500 jvmtiError
501 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
502 //TODO: add locking
503 EventRedefineClasses event;
504 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
505 VMThread::execute(&op);
506 jvmtiError error = op.check_error();
507 if (error == JVMTI_ERROR_NONE) {
508 event.set_classCount(class_count);
509 event.set_redefinitionId(op.id());
510 event.commit();
511 }
512 return error;
513 } /* end RedefineClasses */
514
515
516 //
517 // Object functions
518 //
519
520 // size_ptr - pre-checked for null
521 jvmtiError
522 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
523 oop mirror = JNIHandles::resolve_external_guard(object);
524 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
525 *size_ptr = (jlong)mirror->size() * wordSize;
526 return JVMTI_ERROR_NONE;
527 } /* end GetObjectSize */
528
529 //
530 // Method functions
531 //
532
533 // prefix - null is a valid value, must be checked
534 jvmtiError
535 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
536 return prefix == nullptr?
537 SetNativeMethodPrefixes(0, nullptr) :
538 SetNativeMethodPrefixes(1, (char**)&prefix);
539 } /* end SetNativeMethodPrefix */
540
541
542 // prefix_count - pre-checked to be greater than or equal to 0
543 // prefixes - pre-checked for null
544 jvmtiError
545 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
546 // Have to grab JVMTI thread state lock to be sure that some thread
547 // isn't accessing the prefixes at the same time we are setting them.
548 // No locks during VM bring-up.
549 if (Threads::number_of_threads() == 0) {
550 return set_native_method_prefixes(prefix_count, prefixes);
551 } else {
552 MutexLocker mu(JvmtiThreadState_lock);
553 return set_native_method_prefixes(prefix_count, prefixes);
554 }
555 } /* end SetNativeMethodPrefixes */
556
557 //
558 // Event Management functions
559 //
560
561 // callbacks - null is a valid value, must be checked
562 // size_of_callbacks - pre-checked to be greater than or equal to 0
563 jvmtiError
564 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
565 MountUnmountDisabler disabler;
566 JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
567 return JVMTI_ERROR_NONE;
568 } /* end SetEventCallbacks */
569
570
571 // event_thread - null is a valid value, must be checked
572 jvmtiError
573 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
574 bool enabled = (mode == JVMTI_ENABLE);
575
576 // event_type must be valid
577 if (!JvmtiEventController::is_valid_event_type(event_type)) {
578 return JVMTI_ERROR_INVALID_EVENT_TYPE;
579 }
580
581 // assure that needed capabilities are present
582 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
583 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
584 }
585
586 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
587 record_class_file_load_hook_enabled();
588 }
589 MountUnmountDisabler disabler;
590
591 if (event_thread == nullptr) {
592 // Can be called at Agent_OnLoad() time with event_thread == nullptr
593 // when Thread::current() does not work yet so we cannot create a
594 // ThreadsListHandle that is common to both thread-specific and
595 // global code paths.
596
597 JvmtiEventController::set_user_enabled(this, nullptr, (oop) nullptr, event_type, enabled);
598 } else {
599 // We have a specified event_thread.
600 JavaThread* current = JavaThread::current();
601 ThreadsListHandle tlh(current);
602
603 JavaThread* java_thread = nullptr;
604 oop thread_obj = nullptr;
605 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), event_thread, current, &java_thread, &thread_obj);
606 if (err != JVMTI_ERROR_NONE) {
607 return err;
608 }
609
610 // global events cannot be controlled at thread level.
611 if (JvmtiEventController::is_global_event(event_type)) {
612 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
613 }
614
615 JvmtiEventController::set_user_enabled(this, java_thread, thread_obj, event_type, enabled);
616 }
617
618 return JVMTI_ERROR_NONE;
619 } /* end SetEventNotificationMode */
620
621 //
622 // Capability functions
623 //
624
625 // capabilities_ptr - pre-checked for null
626 jvmtiError
627 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
628 JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
629 get_prohibited_capabilities(),
630 capabilities_ptr);
631 return JVMTI_ERROR_NONE;
632 } /* end GetPotentialCapabilities */
633
634
635 // capabilities_ptr - pre-checked for null
636 jvmtiError
637 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
638 return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
639 get_prohibited_capabilities(),
640 capabilities_ptr,
641 get_capabilities());
642 } /* end AddCapabilities */
643
644
645 // capabilities_ptr - pre-checked for null
646 jvmtiError
647 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
648 JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
649 return JVMTI_ERROR_NONE;
650 } /* end RelinquishCapabilities */
651
652
653 // capabilities_ptr - pre-checked for null
654 jvmtiError
655 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
656 JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
657 return JVMTI_ERROR_NONE;
658 } /* end GetCapabilities */
659
660 //
661 // Class Loader Search functions
662 //
663
664 // segment - pre-checked for null
665 jvmtiError
666 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
667 jvmtiPhase phase = get_phase();
668 if (phase == JVMTI_PHASE_ONLOAD) {
669 Arguments::append_sysclasspath(segment);
670 return JVMTI_ERROR_NONE;
671 } else if (use_version_1_0_semantics()) {
672 // This JvmtiEnv requested version 1.0 semantics and this function
673 // is only allowed in the ONLOAD phase in version 1.0 so we need to
674 // return an error here.
675 return JVMTI_ERROR_WRONG_PHASE;
676 } else if (phase == JVMTI_PHASE_LIVE) {
677 // The phase is checked by the wrapper that called this function,
678 // but this thread could be racing with the thread that is
679 // terminating the VM so we check one more time.
680
681 // create the zip entry
682 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
683 if (zip_entry == nullptr) {
684 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
685 }
686
687 // add the jar file to the bootclasspath
688 log_info(class, load)("opened: %s", zip_entry->name());
689 #if INCLUDE_CDS
690 ClassLoader::append_boot_classpath(zip_entry);
691 #else
692 ClassLoader::add_to_boot_append_entries(zip_entry);
693 #endif
694 return JVMTI_ERROR_NONE;
695 } else {
696 return JVMTI_ERROR_WRONG_PHASE;
697 }
698
699 } /* end AddToBootstrapClassLoaderSearch */
700
701
702 // segment - pre-checked for null
703 jvmtiError
704 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
705 jvmtiPhase phase = get_phase();
706
707 if (phase == JVMTI_PHASE_ONLOAD) {
708 for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
709 if (strcmp("java.class.path", p->key()) == 0) {
710 p->append_value(segment);
711 break;
712 }
713 }
714 return JVMTI_ERROR_NONE;
715 } else if (phase == JVMTI_PHASE_LIVE) {
716 // The phase is checked by the wrapper that called this function,
717 // but this thread could be racing with the thread that is
718 // terminating the VM so we check one more time.
719 JavaThread* THREAD = JavaThread::current(); // For exception macros.
720 HandleMark hm(THREAD);
721
722 // create the zip entry (which will open the zip file and hence
723 // check that the segment is indeed a zip file).
724 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
725 if (zip_entry == nullptr) {
726 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
727 }
728 delete zip_entry; // no longer needed
729
730 Handle loader(THREAD, SystemDictionary::java_system_loader());
731
732 // need the path as java.lang.String
733 Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
734 if (HAS_PENDING_EXCEPTION) {
735 CLEAR_PENDING_EXCEPTION;
736 return JVMTI_ERROR_INTERNAL;
737 }
738
739 // Invoke the appendToClassPathForInstrumentation method - if the method
740 // is not found it means the loader doesn't support adding to the class path
741 // in the live phase.
742 {
743 JavaValue res(T_VOID);
744 JavaCalls::call_special(&res,
745 loader,
746 loader->klass(),
747 vmSymbols::appendToClassPathForInstrumentation_name(),
748 vmSymbols::appendToClassPathForInstrumentation_signature(),
749 path,
750 THREAD);
751 if (HAS_PENDING_EXCEPTION) {
752 Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
753 CLEAR_PENDING_EXCEPTION;
754
755 if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
756 return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
757 } else {
758 return JVMTI_ERROR_INTERNAL;
759 }
760 }
761 }
762
763 return JVMTI_ERROR_NONE;
764 } else {
765 return JVMTI_ERROR_WRONG_PHASE;
766 }
767 } /* end AddToSystemClassLoaderSearch */
768
769 //
770 // General functions
771 //
772
773 // phase_ptr - pre-checked for null
774 jvmtiError
775 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
776 *phase_ptr = phase();
777 return JVMTI_ERROR_NONE;
778 } /* end GetPhase */
779
780
781 jvmtiError
782 JvmtiEnv::DisposeEnvironment() {
783 dispose();
784 return JVMTI_ERROR_NONE;
785 } /* end DisposeEnvironment */
786
787
788 // data - null is a valid value, must be checked
789 jvmtiError
790 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
791 set_env_local_storage(data);
792 return JVMTI_ERROR_NONE;
793 } /* end SetEnvironmentLocalStorage */
794
795
796 // data_ptr - pre-checked for null
797 jvmtiError
798 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
799 *data_ptr = (void*)get_env_local_storage();
800 return JVMTI_ERROR_NONE;
801 } /* end GetEnvironmentLocalStorage */
802
803 // version_ptr - pre-checked for null
804 jvmtiError
805 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
806 *version_ptr = JVMTI_VERSION;
807 return JVMTI_ERROR_NONE;
808 } /* end GetVersionNumber */
809
810
811 // name_ptr - pre-checked for null
812 jvmtiError
813 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
814 if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
815 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
816 }
817 const char *name = JvmtiUtil::error_name(error);
818 if (name == nullptr) {
819 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
820 }
821 size_t len = strlen(name) + 1;
822 jvmtiError err = allocate(len, (unsigned char**)name_ptr);
823 if (err == JVMTI_ERROR_NONE) {
824 memcpy(*name_ptr, name, len);
825 }
826 return err;
827 } /* end GetErrorName */
828
829
830 jvmtiError
831 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
832 LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
833 switch (flag) {
834 case JVMTI_VERBOSE_OTHER:
835 // ignore
836 break;
837 case JVMTI_VERBOSE_CLASS:
838 LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
839 LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
840 break;
841 case JVMTI_VERBOSE_GC:
842 LogConfiguration::configure_stdout(level, true, LOG_TAGS(gc));
843 break;
844 case JVMTI_VERBOSE_JNI:
845 level = value == 0 ? LogLevel::Off : LogLevel::Debug;
846 LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
847 break;
848 default:
849 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
850 };
851 return JVMTI_ERROR_NONE;
852 } /* end SetVerboseFlag */
853
854
855 // format_ptr - pre-checked for null
856 jvmtiError
857 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
858 *format_ptr = JVMTI_JLOCATION_JVMBCI;
859 return JVMTI_ERROR_NONE;
860 } /* end GetJLocationFormat */
861
862 //
863 // Thread functions
864 //
865
866 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
867 // thread_state_ptr - pre-checked for null
868 jvmtiError
869 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
870 JavaThread* current_thread = JavaThread::current();
871 MountUnmountDisabler disabler(thread);
872 ThreadsListHandle tlh(current_thread);
873
874 JavaThread* java_thread = nullptr;
875 oop thread_oop = nullptr;
876 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
877 if (err != JVMTI_ERROR_NONE && err != JVMTI_ERROR_THREAD_NOT_ALIVE) {
878 // We got an error code so we don't have a JavaThread*, but only
879 // return an error from here if the error is not because the thread
880 // is a virtual thread.
881 return err;
882 }
883 *thread_state_ptr = JvmtiEnvBase::get_thread_or_vthread_state(thread_oop, java_thread);
884 return JVMTI_ERROR_NONE;
885 } /* end GetThreadState */
886
887
888 // thread_ptr - pre-checked for null
889 jvmtiError
890 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
891 JavaThread* cur_thread = JavaThread::current();
892 oop thread_oop = get_vthread_or_thread_oop(cur_thread);
893
894 *thread_ptr = (jthread)JNIHandles::make_local(cur_thread, thread_oop);
895 return JVMTI_ERROR_NONE;
896 } /* end GetCurrentThread */
897
898
899 // threads_count_ptr - pre-checked for null
900 // threads_ptr - pre-checked for null
901 jvmtiError
902 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
903 int nthreads = 0;
904 Handle *thread_objs = nullptr;
905 Thread* current_thread = Thread::current();
906 ResourceMark rm(current_thread);
907 HandleMark hm(current_thread);
908
909 // enumerate threads (including agent threads)
910 ThreadsListEnumerator tle(current_thread, true);
911 nthreads = tle.num_threads();
912 *threads_count_ptr = nthreads;
913
914 if (nthreads == 0) {
915 *threads_ptr = nullptr;
916 return JVMTI_ERROR_NONE;
917 }
918
919 thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
920 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
921
922 for (int i = 0; i < nthreads; i++) {
923 thread_objs[i] = Handle(tle.get_threadObj(i));
924 }
925
926 jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
927 NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
928
929 *threads_ptr = jthreads;
930 return JVMTI_ERROR_NONE;
931 } /* end GetAllThreads */
932
933
934 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
935 jvmtiError
936 JvmtiEnv::SuspendThread(jthread thread) {
937 JavaThread* current = JavaThread::current();
938 HandleMark hm(current);
939 Handle self_tobj;
940
941 jvmtiError err;
942 {
943 MountUnmountDisabler disabler(true);
944 ThreadsListHandle tlh(current);
945 JavaThread* java_thread = nullptr;
946 oop thread_oop = nullptr;
947
948 err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop);
949 if (err != JVMTI_ERROR_NONE) {
950 return err;
951 }
952
953 // Do not use MountUnmountDisabler in context of self suspend to avoid deadlocks.
954 if (java_thread != current) {
955 err = suspend_thread(thread_oop, java_thread, /* single_suspend */ true);
956 return err;
957 }
958 // protect thread_oop as a safepoint can be reached in disabler destructor
959 self_tobj = Handle(current, thread_oop);
960 }
961 // Do self suspend for current JavaThread.
962 err = suspend_thread(self_tobj(), current, /* single_suspend */ true);
963 return err;
964 } /* end SuspendThread */
965
966
967 // request_count - pre-checked to be greater than or equal to 0
968 // request_list - pre-checked for null
969 // results - pre-checked for null
970 jvmtiError
971 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
972 JavaThread* current = JavaThread::current();
973 HandleMark hm(current);
974 Handle self_tobj;
975 int self_idx = -1;
976
977 {
978 MountUnmountDisabler disabler(true);
979 ThreadsListHandle tlh(current);
980
981 for (int i = 0; i < request_count; i++) {
982 JavaThread *java_thread = nullptr;
983 oop thread_oop = nullptr;
984 jthread thread = request_list[i];
985 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
986
987 if (thread_oop != nullptr &&
988 java_lang_VirtualThread::is_instance(thread_oop) &&
989 !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
990 err = JVMTI_ERROR_THREAD_NOT_ALIVE;
991 }
992 if (err != JVMTI_ERROR_NONE) {
993 if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
994 results[i] = err;
995 continue;
996 }
997 }
998 if (java_thread == current) {
999 self_idx = i;
1000 self_tobj = Handle(current, thread_oop);
1001 continue; // self suspend after all other suspends
1002 }
1003 if (java_lang_VirtualThread::is_instance(thread_oop)) {
1004 oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_oop);
1005 java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread);
1006 }
1007 results[i] = suspend_thread(thread_oop, java_thread, /* single_suspend */ true);
1008 }
1009 }
1010 // Self suspend after all other suspends if necessary.
1011 // Do not use MountUnmountDisabler in context of self suspend to avoid deadlocks.
1012 if (self_tobj() != nullptr) {
1013 // there should not be any error for current java_thread
1014 results[self_idx] = suspend_thread(self_tobj(), current, /* single_suspend */ true);
1015 }
1016 // per-thread suspend results returned via results parameter
1017 return JVMTI_ERROR_NONE;
1018 } /* end SuspendThreadList */
1019
1020
1021 jvmtiError
1022 JvmtiEnv::SuspendAllVirtualThreads(jint except_count, const jthread* except_list) {
1023 if (get_capabilities()->can_support_virtual_threads == 0) {
1024 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1025 }
1026 JavaThread* current = JavaThread::current();
1027 HandleMark hm(current);
1028 Handle self_tobj;
1029
1030 {
1031 ResourceMark rm(current);
1032 MountUnmountDisabler disabler(true);
1033 ThreadsListHandle tlh(current);
1034 GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1035
1036 jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1037 if (err != JVMTI_ERROR_NONE) {
1038 return err;
1039 }
1040
1041 // Collect threads from except_list for which resumed status must be restored (only for VirtualThread case)
1042 for (int idx = 0; idx < except_count; idx++) {
1043 jthread thread = except_list[idx];
1044 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1045 if (java_lang_VirtualThread::is_instance(thread_oop) && !JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1046 // is not suspended, so its resumed status must be restored
1047 elist->append(except_list[idx]);
1048 }
1049 }
1050
1051 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1052 oop vt_oop = java_thread->jvmti_vthread();
1053 if (!java_thread->is_exiting() &&
1054 !java_thread->is_jvmti_agent_thread() &&
1055 !java_thread->is_hidden_from_external_view() &&
1056 vt_oop != nullptr &&
1057 ((java_lang_VirtualThread::is_instance(vt_oop) &&
1058 JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1059 !JvmtiVTSuspender::is_vthread_suspended(vt_oop)) ||
1060 (vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && !java_thread->is_suspended())) &&
1061 !is_in_thread_list(except_count, except_list, vt_oop)
1062 ) {
1063 if (java_thread == current) {
1064 self_tobj = Handle(current, vt_oop);
1065 continue; // self suspend after all other suspends
1066 }
1067 suspend_thread(vt_oop, java_thread, /* single_suspend */ false);
1068 }
1069 }
1070 JvmtiVTSuspender::register_all_vthreads_suspend();
1071
1072 // Restore resumed state for threads from except list that were not suspended before.
1073 for (int idx = 0; idx < elist->length(); idx++) {
1074 jthread thread = elist->at(idx);
1075 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1076 if (JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1077 JvmtiVTSuspender::register_vthread_resume(thread_oop);
1078 }
1079 }
1080 }
1081 // Self suspend after all other suspends if necessary.
1082 // Do not use MountUnmountDisabler in context of self suspend to avoid deadlocks.
1083 if (self_tobj() != nullptr) {
1084 suspend_thread(self_tobj(), current, /* single_suspend */ false);
1085 }
1086 return JVMTI_ERROR_NONE;
1087 } /* end SuspendAllVirtualThreads */
1088
1089
1090 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1091 jvmtiError
1092 JvmtiEnv::ResumeThread(jthread thread) {
1093 MountUnmountDisabler disabler(true);
1094 JavaThread* current = JavaThread::current();
1095 ThreadsListHandle tlh(current);
1096
1097 JavaThread* java_thread = nullptr;
1098 oop thread_oop = nullptr;
1099 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_oop);
1100 if (err != JVMTI_ERROR_NONE) {
1101 return err;
1102 }
1103 err = resume_thread(thread_oop, java_thread, /* single_resume */ true);
1104 return err;
1105 } /* end ResumeThread */
1106
1107
1108 // request_count - pre-checked to be greater than or equal to 0
1109 // request_list - pre-checked for null
1110 // results - pre-checked for null
1111 jvmtiError
1112 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1113 oop thread_oop = nullptr;
1114 JavaThread* java_thread = nullptr;
1115 MountUnmountDisabler disabler(true);
1116 ThreadsListHandle tlh;
1117
1118 for (int i = 0; i < request_count; i++) {
1119 jthread thread = request_list[i];
1120 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1121
1122 if (thread_oop != nullptr &&
1123 java_lang_VirtualThread::is_instance(thread_oop) &&
1124 !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1125 err = JVMTI_ERROR_THREAD_NOT_ALIVE;
1126 }
1127 if (err != JVMTI_ERROR_NONE) {
1128 if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1129 results[i] = err;
1130 continue;
1131 }
1132 }
1133 if (java_lang_VirtualThread::is_instance(thread_oop)) {
1134 oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_oop);
1135 java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread);
1136 }
1137 results[i] = resume_thread(thread_oop, java_thread, /* single_resume */ true);
1138 }
1139 // per-thread resume results returned via results parameter
1140 return JVMTI_ERROR_NONE;
1141 } /* end ResumeThreadList */
1142
1143
1144 jvmtiError
1145 JvmtiEnv::ResumeAllVirtualThreads(jint except_count, const jthread* except_list) {
1146 if (get_capabilities()->can_support_virtual_threads == 0) {
1147 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
1148 }
1149 jvmtiError err = JvmtiEnvBase::check_thread_list(except_count, except_list);
1150 if (err != JVMTI_ERROR_NONE) {
1151 return err;
1152 }
1153 ResourceMark rm;
1154 MountUnmountDisabler disabler(true);
1155 GrowableArray<jthread>* elist = new GrowableArray<jthread>(except_count);
1156
1157 // Collect threads from except_list for which suspended status must be restored (only for VirtualThread case)
1158 for (int idx = 0; idx < except_count; idx++) {
1159 jthread thread = except_list[idx];
1160 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1161 if (java_lang_VirtualThread::is_instance(thread_oop) && JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1162 // is suspended, so its suspended status must be restored
1163 elist->append(except_list[idx]);
1164 }
1165 }
1166
1167 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
1168 oop vt_oop = java_thread->jvmti_vthread();
1169 if (!java_thread->is_exiting() &&
1170 !java_thread->is_jvmti_agent_thread() &&
1171 !java_thread->is_hidden_from_external_view() &&
1172 vt_oop != nullptr &&
1173 ((java_lang_VirtualThread::is_instance(vt_oop) &&
1174 JvmtiEnvBase::is_vthread_alive(vt_oop) &&
1175 JvmtiVTSuspender::is_vthread_suspended(vt_oop)) ||
1176 (vt_oop->is_a(vmClasses::BoundVirtualThread_klass()) && java_thread->is_suspended())) &&
1177 !is_in_thread_list(except_count, except_list, vt_oop)
1178 ) {
1179 resume_thread(vt_oop, java_thread, /* single_resume */ false);
1180 }
1181 }
1182 JvmtiVTSuspender::register_all_vthreads_resume();
1183
1184 // Restore suspended state for threads from except list that were suspended before.
1185 for (int idx = 0; idx < elist->length(); idx++) {
1186 jthread thread = elist->at(idx);
1187 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1188 if (!JvmtiVTSuspender::is_vthread_suspended(thread_oop)) {
1189 JvmtiVTSuspender::register_vthread_suspend(thread_oop);
1190 }
1191 }
1192 return JVMTI_ERROR_NONE;
1193 } /* end ResumeAllVirtualThreads */
1194
1195
1196 jvmtiError
1197 JvmtiEnv::StopThread(jthread thread, jobject exception) {
1198 JavaThread* current_thread = JavaThread::current();
1199
1200 MountUnmountDisabler disabler(thread);
1201 ThreadsListHandle tlh(current_thread);
1202 JavaThread* java_thread = nullptr;
1203 oop thread_oop = nullptr;
1204
1205 NULL_CHECK(thread, JVMTI_ERROR_INVALID_THREAD);
1206
1207 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
1208
1209 bool is_virtual = thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1210
1211 if (is_virtual && !is_JavaThread_current(java_thread, thread_oop)) {
1212 if (!is_vthread_suspended(thread_oop, java_thread)) {
1213 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1214 }
1215 if (java_thread == nullptr) { // unmounted virtual thread
1216 return JVMTI_ERROR_OPAQUE_FRAME;
1217 }
1218 }
1219 if (err != JVMTI_ERROR_NONE) {
1220 return err;
1221 }
1222 oop e = JNIHandles::resolve_external_guard(exception);
1223 NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1224
1225 JavaThread::send_async_exception(java_thread, e);
1226
1227 return JVMTI_ERROR_NONE;
1228
1229 } /* end StopThread */
1230
1231
1232 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1233 jvmtiError
1234 JvmtiEnv::InterruptThread(jthread thread) {
1235 JavaThread* current_thread = JavaThread::current();
1236 HandleMark hm(current_thread);
1237
1238 MountUnmountDisabler disabler(thread);
1239 ThreadsListHandle tlh(current_thread);
1240
1241 JavaThread* java_thread = nullptr;
1242 oop thread_obj = nullptr;
1243 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
1244 if (err != JVMTI_ERROR_NONE) {
1245 return err;
1246 }
1247
1248 if (java_lang_VirtualThread::is_instance(thread_obj)) {
1249 // For virtual threads we have to call into Java to interrupt:
1250 Handle obj(current_thread, thread_obj);
1251 JvmtiJavaUpcallMark jjum(current_thread); // hide JVMTI events for Java upcall
1252 JavaValue result(T_VOID);
1253 JavaCalls::call_virtual(&result,
1254 obj,
1255 vmClasses::Thread_klass(),
1256 vmSymbols::interrupt_method_name(),
1257 vmSymbols::void_method_signature(),
1258 current_thread);
1259
1260 return JVMTI_ERROR_NONE;
1261 }
1262
1263 // Really this should be a Java call to Thread.interrupt to ensure the same
1264 // semantics, however historically this has not been done for some reason.
1265 // So we continue with that (which means we don't interact with any Java-level
1266 // Interruptible object) but we must set the Java-level interrupted state.
1267 java_lang_Thread::set_interrupted(thread_obj, true);
1268 java_thread->interrupt();
1269
1270 return JVMTI_ERROR_NONE;
1271 } /* end InterruptThread */
1272
1273
1274 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1275 // info_ptr - pre-checked for null
1276 jvmtiError
1277 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1278 JavaThread* current_thread = JavaThread::current();
1279 ResourceMark rm(current_thread);
1280 HandleMark hm(current_thread);
1281 JavaThread* java_thread = nullptr;
1282 oop thread_oop = nullptr;
1283
1284 MountUnmountDisabler disabler(thread);
1285 ThreadsListHandle tlh(current_thread);
1286
1287 // if thread is null the current thread is used
1288 if (thread == nullptr) {
1289 java_thread = JavaThread::current();
1290 thread_oop = get_vthread_or_thread_oop(java_thread);
1291 if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1292 return JVMTI_ERROR_INVALID_THREAD;
1293 }
1294 } else {
1295 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1296 if (err != JVMTI_ERROR_NONE) {
1297 // We got an error code so we don't have a JavaThread *, but
1298 // only return an error from here if we didn't get a valid
1299 // thread_oop.
1300 // In the virtual thread case the cv_external_thread_to_JavaThread is expected to correctly set
1301 // the thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1302 if (thread_oop == nullptr) {
1303 return err;
1304 }
1305 }
1306 }
1307 // We have a valid thread_oop so we can return some thread info.
1308
1309 Handle thread_obj(current_thread, thread_oop);
1310 Handle name;
1311 ThreadPriority priority;
1312 Handle thread_group;
1313 Handle context_class_loader;
1314 bool is_daemon;
1315
1316 name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1317
1318 if (java_lang_VirtualThread::is_instance(thread_obj())) {
1319 priority = (ThreadPriority)JVMTI_THREAD_NORM_PRIORITY;
1320 is_daemon = true;
1321 if (java_lang_VirtualThread::state(thread_obj()) == java_lang_VirtualThread::TERMINATED) {
1322 thread_group = Handle(current_thread, nullptr);
1323 } else {
1324 thread_group = Handle(current_thread, java_lang_Thread_Constants::get_VTHREAD_GROUP());
1325 }
1326 } else {
1327 priority = java_lang_Thread::priority(thread_obj());
1328 is_daemon = java_lang_Thread::is_daemon(thread_obj());
1329 if (java_lang_Thread::get_thread_status(thread_obj()) == JavaThreadStatus::TERMINATED) {
1330 thread_group = Handle(current_thread, nullptr);
1331 } else {
1332 thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1333 }
1334 }
1335
1336 oop loader = java_lang_Thread::context_class_loader(thread_obj());
1337 context_class_loader = Handle(current_thread, loader);
1338
1339 { const char *n;
1340
1341 if (name() != nullptr) {
1342 n = java_lang_String::as_utf8_string(name());
1343 } else {
1344 size_t utf8_length = 0;
1345 n = UNICODE::as_utf8((jchar*) nullptr, utf8_length);
1346 }
1347
1348 info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1349 if (info_ptr->name == nullptr)
1350 return JVMTI_ERROR_OUT_OF_MEMORY;
1351
1352 strcpy(info_ptr->name, n);
1353 }
1354 info_ptr->is_daemon = is_daemon;
1355 info_ptr->priority = priority;
1356
1357 info_ptr->context_class_loader = (context_class_loader.is_null()) ? nullptr :
1358 jni_reference(context_class_loader);
1359 info_ptr->thread_group = jni_reference(thread_group);
1360
1361 return JVMTI_ERROR_NONE;
1362 } /* end GetThreadInfo */
1363
1364
1365 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1366 // owned_monitor_count_ptr - pre-checked for null
1367 // owned_monitors_ptr - pre-checked for null
1368 jvmtiError
1369 JvmtiEnv::GetOwnedMonitorInfo(jthread thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1370 JavaThread* calling_thread = JavaThread::current();
1371 HandleMark hm(calling_thread);
1372
1373 MountUnmountDisabler disabler(thread);
1374 ThreadsListHandle tlh(calling_thread);
1375
1376 JavaThread* java_thread = nullptr;
1377 oop thread_oop = nullptr;
1378 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, calling_thread, &java_thread, &thread_oop);
1379 if (err != JVMTI_ERROR_NONE) {
1380 return err;
1381 }
1382
1383 // growable array of jvmti monitors info on the C-heap
1384 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1385 new (mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1386
1387 Handle thread_handle(calling_thread, thread_oop);
1388 EscapeBarrier eb(java_thread != nullptr, calling_thread, java_thread);
1389 if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1390 delete owned_monitors_list;
1391 return JVMTI_ERROR_OUT_OF_MEMORY;
1392 }
1393 // get owned monitors info with handshake
1394 GetOwnedMonitorInfoClosure op(this, calling_thread, owned_monitors_list);
1395 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1396 err = op.result();
1397
1398 jint owned_monitor_count = owned_monitors_list->length();
1399 if (err == JVMTI_ERROR_NONE) {
1400 if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1401 (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1402 // copy into the returned array
1403 for (int i = 0; i < owned_monitor_count; i++) {
1404 (*owned_monitors_ptr)[i] =
1405 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1406 }
1407 *owned_monitor_count_ptr = owned_monitor_count;
1408 }
1409 }
1410 // clean up.
1411 for (int i = 0; i < owned_monitor_count; i++) {
1412 deallocate((unsigned char*)owned_monitors_list->at(i));
1413 }
1414 delete owned_monitors_list;
1415
1416 return err;
1417 } /* end GetOwnedMonitorInfo */
1418
1419
1420 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1421 // monitor_info_count_ptr - pre-checked for null
1422 // monitor_info_ptr - pre-checked for null
1423 jvmtiError
1424 JvmtiEnv::GetOwnedMonitorStackDepthInfo(jthread thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1425 JavaThread* calling_thread = JavaThread::current();
1426 HandleMark hm(calling_thread);
1427
1428 MountUnmountDisabler disabler(thread);
1429 ThreadsListHandle tlh(calling_thread);
1430
1431 JavaThread* java_thread = nullptr;
1432 oop thread_oop = nullptr;
1433 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, calling_thread, &java_thread, &thread_oop);
1434 if (err != JVMTI_ERROR_NONE) {
1435 return err;
1436 }
1437
1438 // growable array of jvmti monitors info on the C-heap
1439 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1440 new (mtServiceability) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, mtServiceability);
1441
1442 Handle thread_handle(calling_thread, thread_oop);
1443 EscapeBarrier eb(java_thread != nullptr, calling_thread, java_thread);
1444 if (!eb.deoptimize_objects(MaxJavaStackTraceDepth)) {
1445 delete owned_monitors_list;
1446 return JVMTI_ERROR_OUT_OF_MEMORY;
1447 }
1448 // get owned monitors info with handshake
1449 GetOwnedMonitorInfoClosure op(this, calling_thread, owned_monitors_list);
1450 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1451 err = op.result();
1452
1453 jint owned_monitor_count = owned_monitors_list->length();
1454 if (err == JVMTI_ERROR_NONE) {
1455 if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1456 (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1457 // copy to output array.
1458 for (int i = 0; i < owned_monitor_count; i++) {
1459 (*monitor_info_ptr)[i].monitor =
1460 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1461 (*monitor_info_ptr)[i].stack_depth =
1462 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1463 }
1464 }
1465 *monitor_info_count_ptr = owned_monitor_count;
1466 }
1467
1468 // clean up.
1469 for (int i = 0; i < owned_monitor_count; i++) {
1470 deallocate((unsigned char*)owned_monitors_list->at(i));
1471 }
1472 delete owned_monitors_list;
1473
1474 return err;
1475 } /* end GetOwnedMonitorStackDepthInfo */
1476
1477
1478 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1479 // monitor_ptr - pre-checked for null
1480 jvmtiError
1481 JvmtiEnv::GetCurrentContendedMonitor(jthread thread, jobject* monitor_ptr) {
1482 JavaThread* current = JavaThread::current();
1483
1484 *monitor_ptr = nullptr;
1485
1486 // get contended monitor information with handshake
1487 GetCurrentContendedMonitorClosure op(this, current, monitor_ptr);
1488 JvmtiHandshake::execute(&op, thread);
1489 return op.result();
1490 } /* end GetCurrentContendedMonitor */
1491
1492
1493 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1494 // proc - pre-checked for null
1495 // arg - null is a valid value, must be checked
1496 jvmtiError
1497 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1498 JavaThread* current_thread = JavaThread::current();
1499
1500 JavaThread* java_thread = nullptr;
1501 oop thread_oop = nullptr;
1502 ThreadsListHandle tlh(current_thread);
1503 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1504 if (err != JVMTI_ERROR_NONE) {
1505 // We got an error code so we don't have a JavaThread *, but
1506 // only return an error from here if we didn't get a valid
1507 // thread_oop.
1508 if (thread_oop == nullptr) {
1509 return err;
1510 }
1511 // We have a valid thread_oop.
1512 }
1513
1514 if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1515 // No support for virtual threads.
1516 return JVMTI_ERROR_UNSUPPORTED_OPERATION;
1517 }
1518 if (java_thread != nullptr) {
1519 // 'thread' refers to an existing JavaThread.
1520 return JVMTI_ERROR_INVALID_THREAD;
1521 }
1522
1523 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1524 return JVMTI_ERROR_INVALID_PRIORITY;
1525 }
1526
1527 Handle thread_hndl(current_thread, thread_oop);
1528
1529 JvmtiAgentThread* new_thread = new JvmtiAgentThread(this, proc, arg);
1530
1531 // At this point it may be possible that no osthread was created for the
1532 // JavaThread due to lack of resources.
1533 if (new_thread->osthread() == nullptr) {
1534 // The new thread is not known to Thread-SMR yet so we can just delete.
1535 delete new_thread;
1536 return JVMTI_ERROR_OUT_OF_MEMORY;
1537 }
1538
1539 JavaThread::start_internal_daemon(current_thread, new_thread, thread_hndl,
1540 (ThreadPriority)priority);
1541
1542 return JVMTI_ERROR_NONE;
1543 } /* end RunAgentThread */
1544
1545 //
1546 // Thread Group functions
1547 //
1548
1549 // group_count_ptr - pre-checked for null
1550 // groups_ptr - pre-checked for null
1551 jvmtiError
1552 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1553 JavaThread* current_thread = JavaThread::current();
1554
1555 // Only one top level thread group now.
1556 *group_count_ptr = 1;
1557
1558 // Allocate memory to store global-refs to the thread groups.
1559 // Assume this area is freed by caller.
1560 *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1561
1562 NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1563
1564 // Convert oop to Handle, then convert Handle to global-ref.
1565 {
1566 HandleMark hm(current_thread);
1567 Handle system_thread_group(current_thread, Universe::system_thread_group());
1568 *groups_ptr[0] = jni_reference(system_thread_group);
1569 }
1570
1571 return JVMTI_ERROR_NONE;
1572 } /* end GetTopThreadGroups */
1573
1574
1575 // info_ptr - pre-checked for null
1576 jvmtiError
1577 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1578 Thread* current_thread = Thread::current();
1579 ResourceMark rm(current_thread);
1580 HandleMark hm(current_thread);
1581
1582 Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1583 NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1584
1585 const char* name;
1586 Handle parent_group;
1587 bool is_daemon;
1588 ThreadPriority max_priority;
1589
1590 name = java_lang_ThreadGroup::name(group_obj());
1591 parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1592 is_daemon = java_lang_ThreadGroup::is_daemon(group_obj());
1593 max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1594
1595 info_ptr->is_daemon = is_daemon;
1596 info_ptr->max_priority = max_priority;
1597 info_ptr->parent = jni_reference(parent_group);
1598
1599 if (name != nullptr) {
1600 info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1601 NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1602 strcpy(info_ptr->name, name);
1603 } else {
1604 info_ptr->name = nullptr;
1605 }
1606
1607 return JVMTI_ERROR_NONE;
1608 } /* end GetThreadGroupInfo */
1609
1610 // thread_count_ptr - pre-checked for null
1611 // threads_ptr - pre-checked for null
1612 // group_count_ptr - pre-checked for null
1613 // groups_ptr - pre-checked for null
1614 jvmtiError
1615 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1616 jvmtiError err;
1617 JavaThread* current_thread = JavaThread::current();
1618 oop group_obj = JNIHandles::resolve_external_guard(group);
1619 NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1620
1621 Handle *thread_objs = nullptr;
1622 objArrayHandle group_objs;
1623 jint nthreads = 0;
1624 jint ngroups = 0;
1625 int hidden_threads = 0;
1626
1627 ResourceMark rm(current_thread);
1628 HandleMark hm(current_thread);
1629
1630 Handle group_hdl(current_thread, group_obj);
1631
1632 err = get_live_threads(current_thread, group_hdl, &nthreads, &thread_objs);
1633 if (err != JVMTI_ERROR_NONE) {
1634 return err;
1635 }
1636 err = get_subgroups(current_thread, group_hdl, &ngroups, &group_objs);
1637 if (err != JVMTI_ERROR_NONE) {
1638 return err;
1639 }
1640
1641 *group_count_ptr = ngroups;
1642 *thread_count_ptr = nthreads;
1643 *threads_ptr = new_jthreadArray(nthreads, thread_objs);
1644 *groups_ptr = new_jthreadGroupArray(ngroups, group_objs);
1645 if (nthreads > 0 && *threads_ptr == nullptr) {
1646 return JVMTI_ERROR_OUT_OF_MEMORY;
1647 }
1648 if (ngroups > 0 && *groups_ptr == nullptr) {
1649 return JVMTI_ERROR_OUT_OF_MEMORY;
1650 }
1651
1652 return JVMTI_ERROR_NONE;
1653 } /* end GetThreadGroupChildren */
1654
1655
1656 //
1657 // Stack Frame functions
1658 //
1659
1660 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1661 // max_frame_count - pre-checked to be greater than or equal to 0
1662 // frame_buffer - pre-checked for null
1663 // count_ptr - pre-checked for null
1664 jvmtiError
1665 JvmtiEnv::GetStackTrace(jthread thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1666 GetStackTraceClosure op(this, start_depth, max_frame_count, frame_buffer, count_ptr);
1667 JvmtiHandshake::execute(&op, thread);
1668 return op.result();
1669 } /* end GetStackTrace */
1670
1671
1672 // max_frame_count - pre-checked to be greater than or equal to 0
1673 // stack_info_ptr - pre-checked for null
1674 // thread_count_ptr - pre-checked for null
1675 jvmtiError
1676 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1677 jvmtiError err = JVMTI_ERROR_NONE;
1678 JavaThread* calling_thread = JavaThread::current();
1679
1680 // JVMTI get stack traces at safepoint.
1681 VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1682 VMThread::execute(&op);
1683 *thread_count_ptr = op.final_thread_count();
1684 *stack_info_ptr = op.stack_info();
1685 err = op.result();
1686 return err;
1687 } /* end GetAllStackTraces */
1688
1689
1690 // thread_count - pre-checked to be greater than or equal to 0
1691 // thread_list - pre-checked for null
1692 // max_frame_count - pre-checked to be greater than or equal to 0
1693 // stack_info_ptr - pre-checked for null
1694 jvmtiError
1695 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1696 jvmtiError err = JVMTI_ERROR_NONE;
1697
1698 if (thread_count == 1) {
1699 // Use direct handshake if we need to get only one stack trace.
1700 JavaThread *current_thread = JavaThread::current();
1701
1702 jthread thread = thread_list[0];
1703
1704 GetSingleStackTraceClosure op(this, current_thread, thread, max_frame_count);
1705 JvmtiHandshake::execute(&op, thread);
1706 err = op.result();
1707 if (err == JVMTI_ERROR_NONE) {
1708 *stack_info_ptr = op.stack_info();
1709 }
1710 } else {
1711 MountUnmountDisabler disabler;
1712
1713 // JVMTI get stack traces at safepoint.
1714 VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1715 VMThread::execute(&op);
1716 err = op.result();
1717 if (err == JVMTI_ERROR_NONE) {
1718 *stack_info_ptr = op.stack_info();
1719 }
1720 }
1721 return err;
1722 } /* end GetThreadListStackTraces */
1723
1724
1725 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1726 // count_ptr - pre-checked for null
1727 jvmtiError
1728 JvmtiEnv::GetFrameCount(jthread thread, jint* count_ptr) {
1729 GetFrameCountClosure op(this, count_ptr);
1730 JvmtiHandshake::execute(&op, thread);
1731 return op.result();
1732 } /* end GetFrameCount */
1733
1734
1735 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1736 jvmtiError
1737 JvmtiEnv::PopFrame(jthread thread) {
1738 JavaThread* current_thread = JavaThread::current();
1739 HandleMark hm(current_thread);
1740
1741 if (thread == nullptr) {
1742 return JVMTI_ERROR_INVALID_THREAD;
1743 }
1744 MountUnmountDisabler disabler(thread);
1745 ThreadsListHandle tlh(current_thread);
1746
1747 JavaThread* java_thread = nullptr;
1748 oop thread_obj = nullptr;
1749 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
1750 Handle thread_handle(current_thread, thread_obj);
1751
1752 if (err != JVMTI_ERROR_NONE) {
1753 return err;
1754 }
1755 bool self = java_thread == current_thread;
1756
1757 err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self);
1758 if (err != JVMTI_ERROR_NONE) {
1759 return err;
1760 }
1761
1762 // retrieve or create the state
1763 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1764 if (state == nullptr) {
1765 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1766 }
1767
1768 // Eagerly reallocate scalar replaced objects.
1769 EscapeBarrier eb(true, current_thread, java_thread);
1770 if (!eb.deoptimize_objects(1)) {
1771 // Reallocation of scalar replaced objects failed -> return with error
1772 return JVMTI_ERROR_OUT_OF_MEMORY;
1773 }
1774
1775 MutexLocker mu(JvmtiThreadState_lock);
1776 UpdateForPopTopFrameClosure op(state);
1777 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1778 return op.result();
1779 } /* end PopFrame */
1780
1781
1782 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1783 // depth - pre-checked as non-negative
1784 // method_ptr - pre-checked for null
1785 // location_ptr - pre-checked for null
1786 jvmtiError
1787 JvmtiEnv::GetFrameLocation(jthread thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1788 GetFrameLocationClosure op(this, depth, method_ptr, location_ptr);
1789 JvmtiHandshake::execute(&op, thread);
1790 return op.result();
1791 } /* end GetFrameLocation */
1792
1793
1794 // Threads_lock NOT held, java_thread not protected by lock
1795 // depth - pre-checked as non-negative
1796 jvmtiError
1797 JvmtiEnv::NotifyFramePop(jthread thread, jint depth) {
1798 ResourceMark rm;
1799 MountUnmountDisabler disabler(thread);
1800 JavaThread* current = JavaThread::current();
1801 ThreadsListHandle tlh(current);
1802
1803 JavaThread* java_thread = nullptr;
1804 oop thread_obj = nullptr;
1805 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
1806 if (err != JVMTI_ERROR_NONE) {
1807 return err;
1808 }
1809
1810 HandleMark hm(current);
1811 Handle thread_handle(current, thread_obj);
1812 JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1813 if (state == nullptr) {
1814 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1815 }
1816
1817 SetOrClearFramePopClosure op(this, state, true /* set */, depth);
1818 MutexLocker mu(current, JvmtiThreadState_lock);
1819 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1820 return op.result();
1821 } /* end NotifyFramePop */
1822
1823 // Threads_lock NOT held, java_thread not protected by lock
1824 jvmtiError
1825 JvmtiEnv::ClearAllFramePops(jthread thread) {
1826 ResourceMark rm;
1827 MountUnmountDisabler disabler(thread);
1828 JavaThread* current = JavaThread::current();
1829 ThreadsListHandle tlh(current);
1830
1831 JavaThread* java_thread = nullptr;
1832 oop thread_obj = nullptr;
1833 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current, &java_thread, &thread_obj);
1834 if (err != JVMTI_ERROR_NONE) {
1835 return err;
1836 }
1837
1838 HandleMark hm(current);
1839 Handle thread_handle(current, thread_obj);
1840 JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread, thread_handle);
1841 if (state == nullptr) {
1842 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1843 }
1844
1845 SetOrClearFramePopClosure op(this, state, false /* clear all frame pops*/);
1846 MutexLocker mu(current, JvmtiThreadState_lock);
1847 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
1848 return op.result();
1849 } /* end ClearAllFramePops */
1850
1851 //
1852 // Force Early Return functions
1853 //
1854
1855 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1856 jvmtiError
1857 JvmtiEnv::ForceEarlyReturnObject(jthread thread, jobject value) {
1858 jvalue val;
1859 val.l = value;
1860 return force_early_return(thread, val, atos);
1861 } /* end ForceEarlyReturnObject */
1862
1863
1864 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1865 jvmtiError
1866 JvmtiEnv::ForceEarlyReturnInt(jthread thread, jint value) {
1867 jvalue val;
1868 val.i = value;
1869 return force_early_return(thread, val, itos);
1870 } /* end ForceEarlyReturnInt */
1871
1872
1873 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1874 jvmtiError
1875 JvmtiEnv::ForceEarlyReturnLong(jthread thread, jlong value) {
1876 jvalue val;
1877 val.j = value;
1878 return force_early_return(thread, val, ltos);
1879 } /* end ForceEarlyReturnLong */
1880
1881
1882 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1883 jvmtiError
1884 JvmtiEnv::ForceEarlyReturnFloat(jthread thread, jfloat value) {
1885 jvalue val;
1886 val.f = value;
1887 return force_early_return(thread, val, ftos);
1888 } /* end ForceEarlyReturnFloat */
1889
1890
1891 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1892 jvmtiError
1893 JvmtiEnv::ForceEarlyReturnDouble(jthread thread, jdouble value) {
1894 jvalue val;
1895 val.d = value;
1896 return force_early_return(thread, val, dtos);
1897 } /* end ForceEarlyReturnDouble */
1898
1899
1900 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
1901 jvmtiError
1902 JvmtiEnv::ForceEarlyReturnVoid(jthread thread) {
1903 jvalue val;
1904 val.j = 0L;
1905 return force_early_return(thread, val, vtos);
1906 } /* end ForceEarlyReturnVoid */
1907
1908
1909 //
1910 // Heap functions
1911 //
1912
1913 // klass - null is a valid value, must be checked
1914 // initial_object - null is a valid value, must be checked
1915 // callbacks - pre-checked for null
1916 // user_data - null is a valid value, must be checked
1917 jvmtiError
1918 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1919 // check klass if provided
1920 Klass* k = nullptr;
1921 if (klass != nullptr) {
1922 oop k_mirror = JNIHandles::resolve_external_guard(klass);
1923 if (k_mirror == nullptr) {
1924 return JVMTI_ERROR_INVALID_CLASS;
1925 }
1926 if (java_lang_Class::is_primitive(k_mirror)) {
1927 return JVMTI_ERROR_NONE;
1928 }
1929 k = java_lang_Class::as_Klass(k_mirror);
1930 if (klass == nullptr) {
1931 return JVMTI_ERROR_INVALID_CLASS;
1932 }
1933 }
1934
1935 if (initial_object != nullptr) {
1936 oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1937 if (init_obj == nullptr) {
1938 return JVMTI_ERROR_INVALID_OBJECT;
1939 }
1940 }
1941
1942 Thread *thread = Thread::current();
1943 HandleMark hm(thread);
1944
1945 TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1946 JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1947 return JVMTI_ERROR_NONE;
1948 } /* end FollowReferences */
1949
1950
1951 // klass - null is a valid value, must be checked
1952 // callbacks - pre-checked for null
1953 // user_data - null is a valid value, must be checked
1954 jvmtiError
1955 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1956 // check klass if provided
1957 Klass* k = nullptr;
1958 if (klass != nullptr) {
1959 oop k_mirror = JNIHandles::resolve_external_guard(klass);
1960 if (k_mirror == nullptr) {
1961 return JVMTI_ERROR_INVALID_CLASS;
1962 }
1963 if (java_lang_Class::is_primitive(k_mirror)) {
1964 return JVMTI_ERROR_NONE;
1965 }
1966 k = java_lang_Class::as_Klass(k_mirror);
1967 if (k == nullptr) {
1968 return JVMTI_ERROR_INVALID_CLASS;
1969 }
1970 }
1971
1972 TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1973 JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1974 return JVMTI_ERROR_NONE;
1975 } /* end IterateThroughHeap */
1976
1977
1978 // tag_ptr - pre-checked for null
1979 jvmtiError
1980 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1981 oop o = JNIHandles::resolve_external_guard(object);
1982 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1983 *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1984 return JVMTI_ERROR_NONE;
1985 } /* end GetTag */
1986
1987
1988 jvmtiError
1989 JvmtiEnv::SetTag(jobject object, jlong tag) {
1990 oop o = JNIHandles::resolve_external_guard(object);
1991 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1992 JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1993 return JVMTI_ERROR_NONE;
1994 } /* end SetTag */
1995
1996
1997 // tag_count - pre-checked to be greater than or equal to 0
1998 // tags - pre-checked for null
1999 // count_ptr - pre-checked for null
2000 // object_result_ptr - null is a valid value, must be checked
2001 // tag_result_ptr - null is a valid value, must be checked
2002 jvmtiError
2003 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
2004 TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
2005 return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
2006 } /* end GetObjectsWithTags */
2007
2008
2009 jvmtiError
2010 JvmtiEnv::ForceGarbageCollection() {
2011 Universe::heap()->collect(GCCause::_jvmti_force_gc);
2012 return JVMTI_ERROR_NONE;
2013 } /* end ForceGarbageCollection */
2014
2015
2016 //
2017 // Heap (1.0) functions
2018 //
2019
2020 // object_reference_callback - pre-checked for null
2021 // user_data - null is a valid value, must be checked
2022 jvmtiError
2023 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
2024 oop o = JNIHandles::resolve_external_guard(object);
2025 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
2026 JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
2027 return JVMTI_ERROR_NONE;
2028 } /* end IterateOverObjectsReachableFromObject */
2029
2030
2031 // heap_root_callback - null is a valid value, must be checked
2032 // stack_ref_callback - null is a valid value, must be checked
2033 // object_ref_callback - null is a valid value, must be checked
2034 // user_data - null is a valid value, must be checked
2035 jvmtiError
2036 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
2037 TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
2038 JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
2039 return JVMTI_ERROR_NONE;
2040 } /* end IterateOverReachableObjects */
2041
2042
2043 // heap_object_callback - pre-checked for null
2044 // user_data - null is a valid value, must be checked
2045 jvmtiError
2046 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2047 TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
2048 Thread *thread = Thread::current();
2049 HandleMark hm(thread);
2050 JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, nullptr, heap_object_callback, user_data);
2051 return JVMTI_ERROR_NONE;
2052 } /* end IterateOverHeap */
2053
2054
2055 // k_mirror - may be primitive, this must be checked
2056 // heap_object_callback - pre-checked for null
2057 // user_data - null is a valid value, must be checked
2058 jvmtiError
2059 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2060 if (java_lang_Class::is_primitive(k_mirror)) {
2061 // DO PRIMITIVE CLASS PROCESSING
2062 return JVMTI_ERROR_NONE;
2063 }
2064 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2065 if (klass == nullptr) {
2066 return JVMTI_ERROR_INVALID_CLASS;
2067 }
2068 TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2069 JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2070 return JVMTI_ERROR_NONE;
2071 } /* end IterateOverInstancesOfClass */
2072
2073
2074 //
2075 // Local Variable functions
2076 //
2077
2078 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2079 // depth - pre-checked as non-negative
2080 // value_ptr - pre-checked for null
2081 jvmtiError
2082 JvmtiEnv::GetLocalObject(jthread thread, jint depth, jint slot, 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 MountUnmountDisabler disabler(thread);
2089 ThreadsListHandle tlh(current_thread);
2090
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) {
2095 return err;
2096 }
2097 bool self = is_JavaThread_current(java_thread, thread_obj);
2098
2099 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2100 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2101 current_thread, depth, slot, self);
2102 VMThread::execute(&op);
2103 err = op.result();
2104 if (err == JVMTI_ERROR_NONE) {
2105 *value_ptr = op.value().l;
2106 }
2107 } else {
2108 // Support for ordinary threads
2109 VM_GetOrSetLocal op(java_thread, current_thread, depth, slot, self);
2110 VMThread::execute(&op);
2111 err = op.result();
2112 if (err == JVMTI_ERROR_NONE) {
2113 *value_ptr = op.value().l;
2114 }
2115 }
2116 return err;
2117 } /* end GetLocalObject */
2118
2119 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2120 // depth - pre-checked as non-negative
2121 // value - pre-checked for null
2122 jvmtiError
2123 JvmtiEnv::GetLocalInstance(jthread thread, jint depth, jobject* value_ptr){
2124 JavaThread* current_thread = JavaThread::current();
2125 // rm object is created to clean up the javaVFrame created in
2126 // doit_prologue(), but after doit() is finished with it.
2127 ResourceMark rm(current_thread);
2128 HandleMark hm(current_thread);
2129 MountUnmountDisabler disabler(thread);
2130 ThreadsListHandle tlh(current_thread);
2131
2132 JavaThread* java_thread = nullptr;
2133 oop thread_obj = nullptr;
2134 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2135 if (err != JVMTI_ERROR_NONE) {
2136 return err;
2137 }
2138 bool self = is_JavaThread_current(java_thread, thread_obj);
2139
2140 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2141 VM_VirtualThreadGetReceiver op(this, Handle(current_thread, thread_obj),
2142 current_thread, depth, self);
2143 VMThread::execute(&op);
2144 err = op.result();
2145 if (err == JVMTI_ERROR_NONE) {
2146 *value_ptr = op.value().l;
2147 }
2148 } else {
2149 // Support for ordinary threads
2150 VM_GetReceiver op(java_thread, current_thread, depth, self);
2151 VMThread::execute(&op);
2152 err = op.result();
2153 if (err == JVMTI_ERROR_NONE) {
2154 *value_ptr = op.value().l;
2155 }
2156 }
2157 return err;
2158 } /* end GetLocalInstance */
2159
2160
2161 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2162 // depth - pre-checked as non-negative
2163 // value_ptr - pre-checked for null
2164 jvmtiError
2165 JvmtiEnv::GetLocalInt(jthread thread, jint depth, jint slot, jint* value_ptr) {
2166 JavaThread* current_thread = JavaThread::current();
2167 // rm object is created to clean up the javaVFrame created in
2168 // doit_prologue(), but after doit() is finished with it.
2169 ResourceMark rm(current_thread);
2170 HandleMark hm(current_thread);
2171 MountUnmountDisabler disabler(thread);
2172 ThreadsListHandle tlh(current_thread);
2173
2174 JavaThread* java_thread = nullptr;
2175 oop thread_obj = nullptr;
2176 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2177 if (err != JVMTI_ERROR_NONE) {
2178 return err;
2179 }
2180 bool self = is_JavaThread_current(java_thread, thread_obj);
2181
2182 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2183 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2184 depth, slot, T_INT, self);
2185 VMThread::execute(&op);
2186 err = op.result();
2187 if (err == JVMTI_ERROR_NONE) {
2188 *value_ptr = op.value().i;
2189 }
2190 } else {
2191 // Support for ordinary threads
2192 VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, self);
2193 VMThread::execute(&op);
2194 err = op.result();
2195 if (err == JVMTI_ERROR_NONE) {
2196 *value_ptr = op.value().i;
2197 }
2198 }
2199 return err;
2200 } /* end GetLocalInt */
2201
2202
2203 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2204 // depth - pre-checked as non-negative
2205 // value_ptr - pre-checked for null
2206 jvmtiError
2207 JvmtiEnv::GetLocalLong(jthread thread, jint depth, jint slot, jlong* value_ptr) {
2208 JavaThread* current_thread = JavaThread::current();
2209 // rm object is created to clean up the javaVFrame created in
2210 // doit_prologue(), but after doit() is finished with it.
2211 ResourceMark rm(current_thread);
2212 HandleMark hm(current_thread);
2213 MountUnmountDisabler disabler(thread);
2214 ThreadsListHandle tlh(current_thread);
2215
2216 JavaThread* java_thread = nullptr;
2217 oop thread_obj = nullptr;
2218 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2219 if (err != JVMTI_ERROR_NONE) {
2220 return err;
2221 }
2222 bool self = is_JavaThread_current(java_thread, thread_obj);
2223
2224 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2225 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2226 depth, slot, T_LONG, self);
2227 VMThread::execute(&op);
2228 err = op.result();
2229 if (err == JVMTI_ERROR_NONE) {
2230 *value_ptr = op.value().j;
2231 }
2232 } else {
2233 // Support for ordinary threads
2234 VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, self);
2235 VMThread::execute(&op);
2236 err = op.result();
2237 if (err == JVMTI_ERROR_NONE) {
2238 *value_ptr = op.value().j;
2239 }
2240 }
2241 return err;
2242 } /* end GetLocalLong */
2243
2244
2245 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2246 // depth - pre-checked as non-negative
2247 // value_ptr - pre-checked for null
2248 jvmtiError
2249 JvmtiEnv::GetLocalFloat(jthread thread, jint depth, jint slot, jfloat* value_ptr) {
2250 JavaThread* current_thread = JavaThread::current();
2251 // rm object is created to clean up the javaVFrame created in
2252 // doit_prologue(), but after doit() is finished with it.
2253 ResourceMark rm(current_thread);
2254 HandleMark hm(current_thread);
2255 MountUnmountDisabler disabler(thread);
2256 ThreadsListHandle tlh(current_thread);
2257
2258 JavaThread* java_thread = nullptr;
2259 oop thread_obj = nullptr;
2260 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2261 if (err != JVMTI_ERROR_NONE) {
2262 return err;
2263 }
2264 bool self = is_JavaThread_current(java_thread, thread_obj);
2265
2266 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2267 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2268 depth, slot, T_FLOAT, self);
2269 VMThread::execute(&op);
2270 err = op.result();
2271 if (err == JVMTI_ERROR_NONE) {
2272 *value_ptr = op.value().f;
2273 }
2274 } else {
2275 // Support for ordinary threads
2276 VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, self);
2277 VMThread::execute(&op);
2278 err = op.result();
2279 if (err == JVMTI_ERROR_NONE) {
2280 *value_ptr = op.value().f;
2281 }
2282 }
2283 return err;
2284 } /* end GetLocalFloat */
2285
2286
2287 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2288 // depth - pre-checked as non-negative
2289 // value_ptr - pre-checked for null
2290 jvmtiError
2291 JvmtiEnv::GetLocalDouble(jthread thread, jint depth, jint slot, jdouble* value_ptr) {
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 MountUnmountDisabler disabler(thread);
2298 ThreadsListHandle tlh(current_thread);
2299
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) {
2304 return err;
2305 }
2306 bool self = is_JavaThread_current(java_thread, thread_obj);
2307
2308 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2309 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2310 depth, slot, T_DOUBLE, self);
2311 VMThread::execute(&op);
2312 err = op.result();
2313 if (err == JVMTI_ERROR_NONE) {
2314 *value_ptr = op.value().d;
2315 }
2316 } else {
2317 // Support for ordinary threads
2318 VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, self);
2319 VMThread::execute(&op);
2320 err = op.result();
2321 if (err == JVMTI_ERROR_NONE) {
2322 *value_ptr = op.value().d;
2323 }
2324 }
2325 return err;
2326 } /* end GetLocalDouble */
2327
2328
2329 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2330 // depth - pre-checked as non-negative
2331 jvmtiError
2332 JvmtiEnv::SetLocalObject(jthread thread, jint depth, jint slot, jobject value) {
2333 JavaThread* current_thread = JavaThread::current();
2334 // rm object is created to clean up the javaVFrame created in
2335 // doit_prologue(), but after doit() is finished with it.
2336 ResourceMark rm(current_thread);
2337 HandleMark hm(current_thread);
2338 MountUnmountDisabler disabler(thread);
2339 ThreadsListHandle tlh(current_thread);
2340
2341 JavaThread* java_thread = nullptr;
2342 oop thread_obj = nullptr;
2343 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2344 if (err != JVMTI_ERROR_NONE) {
2345 return err;
2346 }
2347 bool self = is_JavaThread_current(java_thread, thread_obj);
2348 jvalue val;
2349 val.l = value;
2350
2351 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2352 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2353 depth, slot, T_OBJECT, val, self);
2354 VMThread::execute(&op);
2355 err = op.result();
2356 } else {
2357 // Support for ordinary threads
2358 VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val, self);
2359 VMThread::execute(&op);
2360 err = op.result();
2361 }
2362 return err;
2363 } /* end SetLocalObject */
2364
2365
2366 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2367 // depth - pre-checked as non-negative
2368 jvmtiError
2369 JvmtiEnv::SetLocalInt(jthread thread, jint depth, jint slot, jint value) {
2370 JavaThread* current_thread = JavaThread::current();
2371 // rm object is created to clean up the javaVFrame created in
2372 // doit_prologue(), but after doit() is finished with it.
2373 ResourceMark rm(current_thread);
2374 HandleMark hm(current_thread);
2375 MountUnmountDisabler disabler(thread);
2376 ThreadsListHandle tlh(current_thread);
2377
2378 JavaThread* java_thread = nullptr;
2379 oop thread_obj = nullptr;
2380 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2381 if (err != JVMTI_ERROR_NONE) {
2382 return err;
2383 }
2384 bool self = is_JavaThread_current(java_thread, thread_obj);
2385 jvalue val;
2386 val.i = value;
2387
2388 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2389 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2390 depth, slot, T_INT, val, self);
2391 VMThread::execute(&op);
2392 err = op.result();
2393 } else {
2394 // Support for ordinary threads
2395 VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val, self);
2396 VMThread::execute(&op);
2397 err = op.result();
2398 }
2399 return err;
2400 } /* end SetLocalInt */
2401
2402
2403 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2404 // depth - pre-checked as non-negative
2405 jvmtiError
2406 JvmtiEnv::SetLocalLong(jthread thread, jint depth, jint slot, jlong value) {
2407 JavaThread* current_thread = JavaThread::current();
2408 // rm object is created to clean up the javaVFrame created in
2409 // doit_prologue(), but after doit() is finished with it.
2410 ResourceMark rm(current_thread);
2411 HandleMark hm(current_thread);
2412 MountUnmountDisabler disabler(thread);
2413 ThreadsListHandle tlh(current_thread);
2414
2415 JavaThread* java_thread = nullptr;
2416 oop thread_obj = nullptr;
2417 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2418 if (err != JVMTI_ERROR_NONE) {
2419 return err;
2420 }
2421 bool self = is_JavaThread_current(java_thread, thread_obj);
2422 jvalue val;
2423 val.j = value;
2424
2425 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2426 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2427 depth, slot, T_LONG, val, self);
2428 VMThread::execute(&op);
2429 err = op.result();
2430 } else {
2431 // Support for ordinary threads
2432 VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val, self);
2433 VMThread::execute(&op);
2434 err = op.result();
2435 }
2436 return err;
2437 } /* end SetLocalLong */
2438
2439
2440 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2441 // depth - pre-checked as non-negative
2442 jvmtiError
2443 JvmtiEnv::SetLocalFloat(jthread thread, jint depth, jint slot, jfloat value) {
2444 JavaThread* current_thread = JavaThread::current();
2445 // rm object is created to clean up the javaVFrame created in
2446 // doit_prologue(), but after doit() is finished with it.
2447 ResourceMark rm(current_thread);
2448 HandleMark hm(current_thread);
2449 MountUnmountDisabler disabler(thread);
2450 ThreadsListHandle tlh(current_thread);
2451
2452 JavaThread* java_thread = nullptr;
2453 oop thread_obj = nullptr;
2454 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2455 if (err != JVMTI_ERROR_NONE) {
2456 return err;
2457 }
2458 bool self = is_JavaThread_current(java_thread, thread_obj);
2459 jvalue val;
2460 val.f = value;
2461
2462 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2463 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2464 depth, slot, T_FLOAT, val, self);
2465 VMThread::execute(&op);
2466 err = op.result();
2467 } else {
2468 // Support for ordinary threads
2469 VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val, self);
2470 VMThread::execute(&op);
2471 err = op.result();
2472 }
2473 return err;
2474 } /* end SetLocalFloat */
2475
2476
2477 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2478 // depth - pre-checked as non-negative
2479 jvmtiError
2480 JvmtiEnv::SetLocalDouble(jthread thread, jint depth, jint slot, jdouble value) {
2481 JavaThread* current_thread = JavaThread::current();
2482 // rm object is created to clean up the javaVFrame created in
2483 // doit_prologue(), but after doit() is finished with it.
2484 ResourceMark rm(current_thread);
2485 HandleMark hm(current_thread);
2486 MountUnmountDisabler disabler(thread);
2487 ThreadsListHandle tlh(current_thread);
2488
2489 JavaThread* java_thread = nullptr;
2490 oop thread_obj = nullptr;
2491 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2492 if (err != JVMTI_ERROR_NONE) {
2493 return err;
2494 }
2495 bool self = is_JavaThread_current(java_thread, thread_obj);
2496 jvalue val;
2497 val.d = value;
2498
2499 if (java_lang_VirtualThread::is_instance(thread_obj)) {
2500 VM_VirtualThreadGetOrSetLocal op(this, Handle(current_thread, thread_obj),
2501 depth, slot, T_DOUBLE, val, self);
2502 VMThread::execute(&op);
2503 err = op.result();
2504 } else {
2505 // Support for ordinary threads
2506 VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val, self);
2507 VMThread::execute(&op);
2508 err = op.result();
2509 }
2510 return err;
2511 } /* end SetLocalDouble */
2512
2513
2514 //
2515 // Breakpoint functions
2516 //
2517
2518 // method - pre-checked for validity, but may be null meaning obsolete method
2519 jvmtiError
2520 JvmtiEnv::SetBreakpoint(Method* method, jlocation location) {
2521 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2522 if (location < 0) { // simple invalid location check first
2523 return JVMTI_ERROR_INVALID_LOCATION;
2524 }
2525 // verify that the breakpoint is not past the end of the method
2526 if (location >= (jlocation) method->code_size()) {
2527 return JVMTI_ERROR_INVALID_LOCATION;
2528 }
2529
2530 ResourceMark rm;
2531 JvmtiBreakpoint bp(method, location);
2532 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2533 if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2534 return JVMTI_ERROR_DUPLICATE;
2535
2536 if (TraceJVMTICalls) {
2537 jvmti_breakpoints.print();
2538 }
2539
2540 return JVMTI_ERROR_NONE;
2541 } /* end SetBreakpoint */
2542
2543
2544 // method - pre-checked for validity, but may be null meaning obsolete method
2545 jvmtiError
2546 JvmtiEnv::ClearBreakpoint(Method* method, jlocation location) {
2547 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
2548
2549 if (location < 0) { // simple invalid location check first
2550 return JVMTI_ERROR_INVALID_LOCATION;
2551 }
2552
2553 // verify that the breakpoint is not past the end of the method
2554 if (location >= (jlocation) method->code_size()) {
2555 return JVMTI_ERROR_INVALID_LOCATION;
2556 }
2557
2558 JvmtiBreakpoint bp(method, location);
2559
2560 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2561 if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2562 return JVMTI_ERROR_NOT_FOUND;
2563
2564 if (TraceJVMTICalls) {
2565 jvmti_breakpoints.print();
2566 }
2567
2568 return JVMTI_ERROR_NONE;
2569 } /* end ClearBreakpoint */
2570
2571
2572 //
2573 // Watched Field functions
2574 //
2575
2576 jvmtiError
2577 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2578 MountUnmountDisabler disabler;
2579 // make sure we haven't set this watch before
2580 if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2581 fdesc_ptr->set_is_field_access_watched(true);
2582
2583 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2584
2585 return JVMTI_ERROR_NONE;
2586 } /* end SetFieldAccessWatch */
2587
2588
2589 jvmtiError
2590 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2591 MountUnmountDisabler disabler;
2592 // make sure we have a watch to clear
2593 if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2594 fdesc_ptr->set_is_field_access_watched(false);
2595
2596 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2597
2598 return JVMTI_ERROR_NONE;
2599 } /* end ClearFieldAccessWatch */
2600
2601
2602 jvmtiError
2603 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2604 MountUnmountDisabler disabler;
2605 // make sure we haven't set this watch before
2606 if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2607 fdesc_ptr->set_is_field_modification_watched(true);
2608
2609 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2610
2611 return JVMTI_ERROR_NONE;
2612 } /* end SetFieldModificationWatch */
2613
2614
2615 jvmtiError
2616 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2617 MountUnmountDisabler disabler;
2618 // make sure we have a watch to clear
2619 if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2620 fdesc_ptr->set_is_field_modification_watched(false);
2621
2622 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2623
2624 return JVMTI_ERROR_NONE;
2625 } /* end ClearFieldModificationWatch */
2626
2627 //
2628 // Class functions
2629 //
2630
2631
2632 // k_mirror - may be primitive, this must be checked
2633 // signature_ptr - null is a valid value, must be checked
2634 // generic_ptr - null is a valid value, must be checked
2635 jvmtiError
2636 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2637 ResourceMark rm;
2638 bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2639 Klass* k = nullptr;
2640 if (!isPrimitive) {
2641 k = java_lang_Class::as_Klass(k_mirror);
2642 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2643 }
2644 if (signature_ptr != nullptr) {
2645 char* result = nullptr;
2646 if (isPrimitive) {
2647 char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2648 result = (char*) jvmtiMalloc(2);
2649 result[0] = tchar;
2650 result[1] = '\0';
2651 } else {
2652 const char* class_sig = k->signature_name();
2653 result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2654 strcpy(result, class_sig);
2655 }
2656 *signature_ptr = result;
2657 }
2658 if (generic_ptr != nullptr) {
2659 *generic_ptr = nullptr;
2660 if (!isPrimitive && k->is_instance_klass()) {
2661 Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2662 if (soo != nullptr) {
2663 const char *gen_sig = soo->as_C_string();
2664 if (gen_sig != nullptr) {
2665 char* gen_result;
2666 jvmtiError err = allocate(strlen(gen_sig) + 1,
2667 (unsigned char **)&gen_result);
2668 if (err != JVMTI_ERROR_NONE) {
2669 return err;
2670 }
2671 strcpy(gen_result, gen_sig);
2672 *generic_ptr = gen_result;
2673 }
2674 }
2675 }
2676 }
2677 return JVMTI_ERROR_NONE;
2678 } /* end GetClassSignature */
2679
2680
2681 // k_mirror - may be primitive, this must be checked
2682 // status_ptr - pre-checked for null
2683 jvmtiError
2684 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2685 jint result = 0;
2686 if (java_lang_Class::is_primitive(k_mirror)) {
2687 result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2688 } else {
2689 Klass* k = java_lang_Class::as_Klass(k_mirror);
2690 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2691 result = k->jvmti_class_status();
2692 }
2693 *status_ptr = result;
2694
2695 return JVMTI_ERROR_NONE;
2696 } /* end GetClassStatus */
2697
2698
2699 // k_mirror - may be primitive, this must be checked
2700 // source_name_ptr - pre-checked for null
2701 jvmtiError
2702 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2703 if (java_lang_Class::is_primitive(k_mirror)) {
2704 return JVMTI_ERROR_ABSENT_INFORMATION;
2705 }
2706 Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2707 NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2708
2709 if (!k_klass->is_instance_klass()) {
2710 return JVMTI_ERROR_ABSENT_INFORMATION;
2711 }
2712
2713 Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2714 NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2715 {
2716 JavaThread* current_thread = JavaThread::current();
2717 ResourceMark rm(current_thread);
2718 const char* sfncp = (const char*) sfnOop->as_C_string();
2719 *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2720 strcpy(*source_name_ptr, sfncp);
2721 }
2722
2723 return JVMTI_ERROR_NONE;
2724 } /* end GetSourceFileName */
2725
2726
2727 // k_mirror - may be primitive, this must be checked
2728 // modifiers_ptr - pre-checked for null
2729 jvmtiError
2730 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2731 jint result = java_lang_Class::modifiers(k_mirror);
2732 if (!java_lang_Class::is_primitive(k_mirror)) {
2733 // Reset the deleted ACC_SUPER bit (deleted in compute_modifier_flags()).
2734 result |= JVM_ACC_SUPER;
2735 }
2736 *modifiers_ptr = result;
2737
2738 return JVMTI_ERROR_NONE;
2739 } /* end GetClassModifiers */
2740
2741
2742 // k_mirror - may be primitive, this must be checked
2743 // method_count_ptr - pre-checked for null
2744 // methods_ptr - pre-checked for null
2745 jvmtiError
2746 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2747 JavaThread* current_thread = JavaThread::current();
2748 HandleMark hm(current_thread);
2749
2750 if (java_lang_Class::is_primitive(k_mirror)) {
2751 *method_count_ptr = 0;
2752 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2753 return JVMTI_ERROR_NONE;
2754 }
2755 Klass* k = java_lang_Class::as_Klass(k_mirror);
2756 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2757
2758 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2759 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2760 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2761 }
2762
2763 if (!k->is_instance_klass()) {
2764 *method_count_ptr = 0;
2765 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2766 return JVMTI_ERROR_NONE;
2767 }
2768 InstanceKlass* ik = InstanceKlass::cast(k);
2769 // Allocate the result and fill it in
2770 int result_length = ik->methods()->length();
2771 jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2772 int index;
2773 int skipped = 0; // skip overpass methods
2774
2775 // Make jmethodIDs for all non-overpass methods.
2776 ik->make_methods_jmethod_ids();
2777
2778 for (index = 0; index < result_length; index++) {
2779 Method* m = ik->methods()->at(index);
2780 // Depending on can_maintain_original_method_order capability use the original
2781 // method ordering indices stored in the class, so we can emit jmethodIDs in
2782 // the order they appeared in the class file or just copy in current order.
2783 int result_index = JvmtiExport::can_maintain_original_method_order() ? ik->method_ordering()->at(index) : index;
2784 assert(result_index >= 0 && result_index < result_length, "invalid original method index");
2785 if (m->is_overpass()) {
2786 result_list[result_index] = nullptr;
2787 skipped++;
2788 continue;
2789 }
2790 jmethodID id = m->find_jmethod_id_or_null();
2791 assert(id != nullptr, "should be created above");
2792 result_list[result_index] = id;
2793 }
2794
2795 // Fill in return value.
2796 if (skipped > 0) {
2797 // copy results skipping null methodIDs
2798 *methods_ptr = (jmethodID*)jvmtiMalloc((result_length - skipped) * sizeof(jmethodID));
2799 *method_count_ptr = result_length - skipped;
2800 for (index = 0, skipped = 0; index < result_length; index++) {
2801 if (result_list[index] == nullptr) {
2802 skipped++;
2803 } else {
2804 (*methods_ptr)[index - skipped] = result_list[index];
2805 }
2806 }
2807 deallocate((unsigned char *)result_list);
2808 } else {
2809 *method_count_ptr = result_length;
2810 *methods_ptr = result_list;
2811 }
2812
2813 return JVMTI_ERROR_NONE;
2814 } /* end GetClassMethods */
2815
2816
2817 // k_mirror - may be primitive, this must be checked
2818 // field_count_ptr - pre-checked for null
2819 // fields_ptr - pre-checked for null
2820 jvmtiError
2821 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2822 if (java_lang_Class::is_primitive(k_mirror)) {
2823 *field_count_ptr = 0;
2824 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2825 return JVMTI_ERROR_NONE;
2826 }
2827 JavaThread* current_thread = JavaThread::current();
2828 HandleMark hm(current_thread);
2829 Klass* k = java_lang_Class::as_Klass(k_mirror);
2830 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2831
2832 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2833 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2834 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2835 }
2836
2837 if (!k->is_instance_klass()) {
2838 *field_count_ptr = 0;
2839 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2840 return JVMTI_ERROR_NONE;
2841 }
2842
2843 InstanceKlass* ik = InstanceKlass::cast(k);
2844
2845 JavaFieldStream flds(ik);
2846
2847 int result_count = ik->java_fields_count();
2848
2849 // Allocate the result and fill it in.
2850 jfieldID* result_list = (jfieldID*)jvmtiMalloc(result_count * sizeof(jfieldID));
2851 for (int i = 0; i < result_count; i++, flds.next()) {
2852 result_list[i] = jfieldIDWorkaround::to_jfieldID(ik, flds.offset(),
2853 flds.access_flags().is_static());
2854 }
2855 assert(flds.done(), "just checking");
2856
2857 // Fill in the results
2858 *field_count_ptr = result_count;
2859 *fields_ptr = result_list;
2860
2861 return JVMTI_ERROR_NONE;
2862 } /* end GetClassFields */
2863
2864
2865 // k_mirror - may be primitive, this must be checked
2866 // interface_count_ptr - pre-checked for null
2867 // interfaces_ptr - pre-checked for null
2868 jvmtiError
2869 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2870 {
2871 if (java_lang_Class::is_primitive(k_mirror)) {
2872 *interface_count_ptr = 0;
2873 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2874 return JVMTI_ERROR_NONE;
2875 }
2876 JavaThread* current_thread = JavaThread::current();
2877 HandleMark hm(current_thread);
2878 Klass* k = java_lang_Class::as_Klass(k_mirror);
2879 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2880
2881 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2882 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2883 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2884
2885 if (!k->is_instance_klass()) {
2886 *interface_count_ptr = 0;
2887 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2888 return JVMTI_ERROR_NONE;
2889 }
2890
2891 Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2892 const int result_length = (interface_list == nullptr ? 0 : interface_list->length());
2893 jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2894 for (int i_index = 0; i_index < result_length; i_index += 1) {
2895 InstanceKlass* klass_at = interface_list->at(i_index);
2896 assert(klass_at->is_klass(), "interfaces must be Klass*s");
2897 assert(klass_at->is_interface(), "interfaces must be interfaces");
2898 oop mirror_at = klass_at->java_mirror();
2899 Handle handle_at = Handle(current_thread, mirror_at);
2900 result_list[i_index] = (jclass) jni_reference(handle_at);
2901 }
2902 *interface_count_ptr = result_length;
2903 *interfaces_ptr = result_list;
2904 }
2905
2906 return JVMTI_ERROR_NONE;
2907 } /* end GetImplementedInterfaces */
2908
2909
2910 // k_mirror - may be primitive, this must be checked
2911 // minor_version_ptr - pre-checked for null
2912 // major_version_ptr - pre-checked for null
2913 jvmtiError
2914 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2915 if (java_lang_Class::is_primitive(k_mirror)) {
2916 return JVMTI_ERROR_ABSENT_INFORMATION;
2917 }
2918 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2919
2920 jint status = klass->jvmti_class_status();
2921 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2922 return JVMTI_ERROR_INVALID_CLASS;
2923 }
2924 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2925 return JVMTI_ERROR_ABSENT_INFORMATION;
2926 }
2927
2928 InstanceKlass* ik = InstanceKlass::cast(klass);
2929 *minor_version_ptr = ik->minor_version();
2930 *major_version_ptr = ik->major_version();
2931
2932 return JVMTI_ERROR_NONE;
2933 } /* end GetClassVersionNumbers */
2934
2935
2936 // k_mirror - may be primitive, this must be checked
2937 // constant_pool_count_ptr - pre-checked for null
2938 // constant_pool_byte_count_ptr - pre-checked for null
2939 // constant_pool_bytes_ptr - pre-checked for null
2940 jvmtiError
2941 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2942 if (java_lang_Class::is_primitive(k_mirror)) {
2943 return JVMTI_ERROR_ABSENT_INFORMATION;
2944 }
2945
2946 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2947 Thread *thread = Thread::current();
2948 ResourceMark rm(thread);
2949
2950 jint status = klass->jvmti_class_status();
2951 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2952 return JVMTI_ERROR_INVALID_CLASS;
2953 }
2954 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2955 return JVMTI_ERROR_ABSENT_INFORMATION;
2956 }
2957
2958 InstanceKlass* ik = InstanceKlass::cast(klass);
2959 JvmtiConstantPoolReconstituter reconstituter(ik);
2960 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2961 return reconstituter.get_error();
2962 }
2963
2964 unsigned char *cpool_bytes;
2965 int cpool_size = reconstituter.cpool_size();
2966 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2967 return reconstituter.get_error();
2968 }
2969 jvmtiError res = allocate(cpool_size, &cpool_bytes);
2970 if (res != JVMTI_ERROR_NONE) {
2971 return res;
2972 }
2973 reconstituter.copy_cpool_bytes(cpool_bytes);
2974 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2975 return reconstituter.get_error();
2976 }
2977
2978 constantPoolHandle constants(thread, ik->constants());
2979 *constant_pool_count_ptr = constants->length();
2980 *constant_pool_byte_count_ptr = cpool_size;
2981 *constant_pool_bytes_ptr = cpool_bytes;
2982
2983 return JVMTI_ERROR_NONE;
2984 } /* end GetConstantPool */
2985
2986
2987 // k_mirror - may be primitive, this must be checked
2988 // is_interface_ptr - pre-checked for null
2989 jvmtiError
2990 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2991 {
2992 bool result = false;
2993 if (!java_lang_Class::is_primitive(k_mirror)) {
2994 Klass* k = java_lang_Class::as_Klass(k_mirror);
2995 if (k != nullptr && k->is_interface()) {
2996 result = true;
2997 }
2998 }
2999 *is_interface_ptr = result;
3000 }
3001
3002 return JVMTI_ERROR_NONE;
3003 } /* end IsInterface */
3004
3005
3006 // k_mirror - may be primitive, this must be checked
3007 // is_array_class_ptr - pre-checked for null
3008 jvmtiError
3009 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
3010 {
3011 bool result = false;
3012 if (!java_lang_Class::is_primitive(k_mirror)) {
3013 Klass* k = java_lang_Class::as_Klass(k_mirror);
3014 if (k != nullptr && k->is_array_klass()) {
3015 result = true;
3016 }
3017 }
3018 *is_array_class_ptr = result;
3019 }
3020
3021 return JVMTI_ERROR_NONE;
3022 } /* end IsArrayClass */
3023
3024
3025 // k_mirror - may be primitive, this must be checked
3026 // classloader_ptr - pre-checked for null
3027 jvmtiError
3028 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
3029 {
3030 if (java_lang_Class::is_primitive(k_mirror)) {
3031 *classloader_ptr = (jclass) jni_reference(Handle());
3032 return JVMTI_ERROR_NONE;
3033 }
3034 JavaThread* current_thread = JavaThread::current();
3035 HandleMark hm(current_thread);
3036 Klass* k = java_lang_Class::as_Klass(k_mirror);
3037 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3038
3039 oop result_oop = k->class_loader();
3040 if (result_oop == nullptr) {
3041 *classloader_ptr = (jclass) jni_reference(Handle());
3042 return JVMTI_ERROR_NONE;
3043 }
3044 Handle result_handle = Handle(current_thread, result_oop);
3045 jclass result_jnihandle = (jclass) jni_reference(result_handle);
3046 *classloader_ptr = result_jnihandle;
3047 }
3048 return JVMTI_ERROR_NONE;
3049 } /* end GetClassLoader */
3050
3051
3052 // k_mirror - may be primitive, this must be checked
3053 // source_debug_extension_ptr - pre-checked for null
3054 jvmtiError
3055 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
3056 {
3057 if (java_lang_Class::is_primitive(k_mirror)) {
3058 return JVMTI_ERROR_ABSENT_INFORMATION;
3059 }
3060 Klass* k = java_lang_Class::as_Klass(k_mirror);
3061 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3062 if (!k->is_instance_klass()) {
3063 return JVMTI_ERROR_ABSENT_INFORMATION;
3064 }
3065 const char* sde = InstanceKlass::cast(k)->source_debug_extension();
3066 NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
3067
3068 {
3069 *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
3070 strcpy(*source_debug_extension_ptr, sde);
3071 }
3072 }
3073
3074 return JVMTI_ERROR_NONE;
3075 } /* end GetSourceDebugExtension */
3076
3077 //
3078 // Object functions
3079 //
3080
3081 // hash_code_ptr - pre-checked for null
3082 jvmtiError
3083 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
3084 oop mirror = JNIHandles::resolve_external_guard(object);
3085 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
3086 NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
3087
3088 {
3089 jint result = (jint) mirror->identity_hash();
3090 *hash_code_ptr = result;
3091 }
3092 return JVMTI_ERROR_NONE;
3093 } /* end GetObjectHashCode */
3094
3095
3096 // info_ptr - pre-checked for null
3097 jvmtiError
3098 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
3099 // This needs to be performed at a safepoint to gather stable data
3100 // because monitor owner / waiters might not be suspended.
3101 VM_GetObjectMonitorUsage op(this, JavaThread::current(), object, info_ptr);
3102 VMThread::execute(&op);
3103 return op.result();
3104 } /* end GetObjectMonitorUsage */
3105
3106
3107 //
3108 // Field functions
3109 //
3110
3111 // name_ptr - null is a valid value, must be checked
3112 // signature_ptr - null is a valid value, must be checked
3113 // generic_ptr - null is a valid value, must be checked
3114 jvmtiError
3115 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3116 JavaThread* current_thread = JavaThread::current();
3117 ResourceMark rm(current_thread);
3118 if (name_ptr == nullptr) {
3119 // just don't return the name
3120 } else {
3121 const char* fieldName = fdesc_ptr->name()->as_C_string();
3122 *name_ptr = (char*) jvmtiMalloc(strlen(fieldName) + 1);
3123 if (*name_ptr == nullptr)
3124 return JVMTI_ERROR_OUT_OF_MEMORY;
3125 strcpy(*name_ptr, fieldName);
3126 }
3127 if (signature_ptr== nullptr) {
3128 // just don't return the signature
3129 } else {
3130 const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
3131 *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
3132 if (*signature_ptr == nullptr)
3133 return JVMTI_ERROR_OUT_OF_MEMORY;
3134 strcpy(*signature_ptr, fieldSignature);
3135 }
3136 if (generic_ptr != nullptr) {
3137 *generic_ptr = nullptr;
3138 Symbol* soop = fdesc_ptr->generic_signature();
3139 if (soop != nullptr) {
3140 const char* gen_sig = soop->as_C_string();
3141 if (gen_sig != nullptr) {
3142 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3143 if (err != JVMTI_ERROR_NONE) {
3144 return err;
3145 }
3146 strcpy(*generic_ptr, gen_sig);
3147 }
3148 }
3149 }
3150 return JVMTI_ERROR_NONE;
3151 } /* end GetFieldName */
3152
3153
3154 // declaring_class_ptr - pre-checked for null
3155 jvmtiError
3156 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
3157 // As for the GetFieldDeclaringClass method, the XSL generated C++ code that calls it has
3158 // a jclass of the relevant class or a subclass of it, which is fine in terms of ensuring
3159 // the holder is kept alive.
3160 *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
3161 return JVMTI_ERROR_NONE;
3162 } /* end GetFieldDeclaringClass */
3163
3164
3165 // modifiers_ptr - pre-checked for null
3166 jvmtiError
3167 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
3168
3169 AccessFlags resultFlags = fdesc_ptr->access_flags();
3170 jint result = resultFlags.as_field_flags();
3171 *modifiers_ptr = result;
3172
3173 return JVMTI_ERROR_NONE;
3174 } /* end GetFieldModifiers */
3175
3176
3177 // is_synthetic_ptr - pre-checked for null
3178 jvmtiError
3179 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
3180 *is_synthetic_ptr = fdesc_ptr->is_synthetic();
3181 return JVMTI_ERROR_NONE;
3182 } /* end IsFieldSynthetic */
3183
3184
3185 //
3186 // Method functions
3187 //
3188
3189 // method - pre-checked for validity, but may be null meaning obsolete method
3190 // name_ptr - null is a valid value, must be checked
3191 // signature_ptr - null is a valid value, must be checked
3192 // generic_ptr - null is a valid value, must be checked
3193 jvmtiError
3194 JvmtiEnv::GetMethodName(Method* method, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3195 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3196 JavaThread* current_thread = JavaThread::current();
3197
3198 ResourceMark rm(current_thread); // get the utf8 name and signature
3199 if (name_ptr == nullptr) {
3200 // just don't return the name
3201 } else {
3202 const char* utf8_name = (const char *) method->name()->as_utf8();
3203 *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3204 strcpy(*name_ptr, utf8_name);
3205 }
3206 if (signature_ptr == nullptr) {
3207 // just don't return the signature
3208 } else {
3209 const char* utf8_signature = (const char *) method->signature()->as_utf8();
3210 *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
3211 strcpy(*signature_ptr, utf8_signature);
3212 }
3213
3214 if (generic_ptr != nullptr) {
3215 *generic_ptr = nullptr;
3216 Symbol* soop = method->generic_signature();
3217 if (soop != nullptr) {
3218 const char* gen_sig = soop->as_C_string();
3219 if (gen_sig != nullptr) {
3220 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3221 if (err != JVMTI_ERROR_NONE) {
3222 return err;
3223 }
3224 strcpy(*generic_ptr, gen_sig);
3225 }
3226 }
3227 }
3228 return JVMTI_ERROR_NONE;
3229 } /* end GetMethodName */
3230
3231
3232 // method - pre-checked for validity, but may be null meaning obsolete method
3233 // declaring_class_ptr - pre-checked for null
3234 jvmtiError
3235 JvmtiEnv::GetMethodDeclaringClass(Method* method, jclass* declaring_class_ptr) {
3236 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3237 Klass* k = method->method_holder();
3238 Handle holder(Thread::current(), k->klass_holder()); // keep the klass alive
3239 (*declaring_class_ptr) = get_jni_class_non_null(k);
3240 return JVMTI_ERROR_NONE;
3241 } /* end GetMethodDeclaringClass */
3242
3243
3244 // method - pre-checked for validity, but may be null meaning obsolete method
3245 // modifiers_ptr - pre-checked for null
3246 jvmtiError
3247 JvmtiEnv::GetMethodModifiers(Method* method, jint* modifiers_ptr) {
3248 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3249 (*modifiers_ptr) = method->access_flags().as_method_flags();
3250 return JVMTI_ERROR_NONE;
3251 } /* end GetMethodModifiers */
3252
3253
3254 // method - pre-checked for validity, but may be null meaning obsolete method
3255 // max_ptr - pre-checked for null
3256 jvmtiError
3257 JvmtiEnv::GetMaxLocals(Method* method, jint* max_ptr) {
3258 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3259 // get max stack
3260 (*max_ptr) = method->max_locals();
3261 return JVMTI_ERROR_NONE;
3262 } /* end GetMaxLocals */
3263
3264
3265 // method - pre-checked for validity, but may be null meaning obsolete method
3266 // size_ptr - pre-checked for null
3267 jvmtiError
3268 JvmtiEnv::GetArgumentsSize(Method* method, jint* size_ptr) {
3269 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3270 // get size of arguments
3271
3272 (*size_ptr) = method->size_of_parameters();
3273 return JVMTI_ERROR_NONE;
3274 } /* end GetArgumentsSize */
3275
3276
3277 // method - pre-checked for validity, but may be null meaning obsolete method
3278 // entry_count_ptr - pre-checked for null
3279 // table_ptr - pre-checked for null
3280 jvmtiError
3281 JvmtiEnv::GetLineNumberTable(Method* method, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3282 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3283 if (!method->has_linenumber_table()) {
3284 return (JVMTI_ERROR_ABSENT_INFORMATION);
3285 }
3286
3287 // The line number table is compressed so we don't know how big it is until decompressed.
3288 // Decompression is really fast so we just do it twice.
3289
3290 // Compute size of table
3291 jint num_entries = 0;
3292 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3293 while (stream.read_pair()) {
3294 num_entries++;
3295 }
3296 jvmtiLineNumberEntry *jvmti_table =
3297 (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3298
3299 // Fill jvmti table
3300 if (num_entries > 0) {
3301 int index = 0;
3302 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3303 while (stream.read_pair()) {
3304 jvmti_table[index].start_location = (jlocation) stream.bci();
3305 jvmti_table[index].line_number = (jint) stream.line();
3306 index++;
3307 }
3308 assert(index == num_entries, "sanity check");
3309 }
3310
3311 // Set up results
3312 (*entry_count_ptr) = num_entries;
3313 (*table_ptr) = jvmti_table;
3314
3315 return JVMTI_ERROR_NONE;
3316 } /* end GetLineNumberTable */
3317
3318
3319 // method - pre-checked for validity, but may be null meaning obsolete method
3320 // start_location_ptr - pre-checked for null
3321 // end_location_ptr - pre-checked for null
3322 jvmtiError
3323 JvmtiEnv::GetMethodLocation(Method* method, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3324
3325 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3326 // get start and end location
3327 (*end_location_ptr) = (jlocation) (method->code_size() - 1);
3328 if (method->code_size() == 0) {
3329 // there is no code so there is no start location
3330 (*start_location_ptr) = (jlocation)(-1);
3331 } else {
3332 (*start_location_ptr) = (jlocation)(0);
3333 }
3334
3335 return JVMTI_ERROR_NONE;
3336 } /* end GetMethodLocation */
3337
3338
3339 // method - pre-checked for validity, but may be null meaning obsolete method
3340 // entry_count_ptr - pre-checked for null
3341 // table_ptr - pre-checked for null
3342 jvmtiError
3343 JvmtiEnv::GetLocalVariableTable(Method* method, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3344
3345 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3346 JavaThread* current_thread = JavaThread::current();
3347
3348 // does the klass have any local variable information?
3349 InstanceKlass* ik = method->method_holder();
3350 if (!ik->has_localvariable_table()) {
3351 return (JVMTI_ERROR_ABSENT_INFORMATION);
3352 }
3353
3354 ConstantPool* constants = method->constants();
3355 NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3356
3357 // in the vm localvariable table representation, 6 consecutive elements in the table
3358 // represent a 6-tuple of shorts
3359 // [start_pc, length, name_index, descriptor_index, signature_index, index]
3360 jint num_entries = method->localvariable_table_length();
3361 jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3362 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3363
3364 if (num_entries > 0) {
3365 LocalVariableTableElement* table = method->localvariable_table_start();
3366 for (int i = 0; i < num_entries; i++) {
3367 // get the 5 tuple information from the vm table
3368 jlocation start_location = (jlocation) table[i].start_bci;
3369 jint length = (jint) table[i].length;
3370 int name_index = (int) table[i].name_cp_index;
3371 int signature_index = (int) table[i].descriptor_cp_index;
3372 int generic_signature_index = (int) table[i].signature_cp_index;
3373 jint slot = (jint) table[i].slot;
3374
3375 // get utf8 name and signature
3376 char *name_buf = nullptr;
3377 char *sig_buf = nullptr;
3378 char *gen_sig_buf = nullptr;
3379 {
3380 ResourceMark rm(current_thread);
3381
3382 const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3383 name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3384 strcpy(name_buf, utf8_name);
3385
3386 const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3387 sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3388 strcpy(sig_buf, utf8_signature);
3389
3390 if (generic_signature_index > 0) {
3391 const char *utf8_gen_sign = (const char *)
3392 constants->symbol_at(generic_signature_index)->as_utf8();
3393 gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3394 strcpy(gen_sig_buf, utf8_gen_sign);
3395 }
3396 }
3397
3398 // fill in the jvmti local variable table
3399 jvmti_table[i].start_location = start_location;
3400 jvmti_table[i].length = length;
3401 jvmti_table[i].name = name_buf;
3402 jvmti_table[i].signature = sig_buf;
3403 jvmti_table[i].generic_signature = gen_sig_buf;
3404 jvmti_table[i].slot = slot;
3405 }
3406 }
3407
3408 // set results
3409 (*entry_count_ptr) = num_entries;
3410 (*table_ptr) = jvmti_table;
3411
3412 return JVMTI_ERROR_NONE;
3413 } /* end GetLocalVariableTable */
3414
3415
3416 // method - pre-checked for validity, but may be null meaning obsolete method
3417 // bytecode_count_ptr - pre-checked for null
3418 // bytecodes_ptr - pre-checked for null
3419 jvmtiError
3420 JvmtiEnv::GetBytecodes(Method* method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3421 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3422
3423 JavaThread* current_thread = JavaThread::current();
3424 methodHandle mh(current_thread, method);
3425 jint size = (jint)mh->code_size();
3426 jvmtiError err = allocate(size, bytecodes_ptr);
3427 if (err != JVMTI_ERROR_NONE) {
3428 return err;
3429 }
3430
3431 (*bytecode_count_ptr) = size;
3432 // get byte codes
3433 // Make sure the class is verified and rewritten first.
3434 JavaThread* THREAD = current_thread;
3435 mh->method_holder()->link_class(THREAD);
3436 if (HAS_PENDING_EXCEPTION) {
3437 CLEAR_PENDING_EXCEPTION;
3438 return JVMTI_ERROR_INVALID_CLASS;
3439 }
3440 JvmtiClassFileReconstituter::copy_bytecodes(mh, *bytecodes_ptr);
3441
3442 return JVMTI_ERROR_NONE;
3443 } /* end GetBytecodes */
3444
3445
3446 // method - pre-checked for validity, but may be null meaning obsolete method
3447 // is_native_ptr - pre-checked for null
3448 jvmtiError
3449 JvmtiEnv::IsMethodNative(Method* method, jboolean* is_native_ptr) {
3450 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3451 (*is_native_ptr) = method->is_native();
3452 return JVMTI_ERROR_NONE;
3453 } /* end IsMethodNative */
3454
3455
3456 // method - pre-checked for validity, but may be null meaning obsolete method
3457 // is_synthetic_ptr - pre-checked for null
3458 jvmtiError
3459 JvmtiEnv::IsMethodSynthetic(Method* method, jboolean* is_synthetic_ptr) {
3460 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3461 (*is_synthetic_ptr) = method->is_synthetic();
3462 return JVMTI_ERROR_NONE;
3463 } /* end IsMethodSynthetic */
3464
3465
3466 // method - pre-checked for validity, but may be null meaning obsolete method
3467 // is_obsolete_ptr - pre-checked for null
3468 jvmtiError
3469 JvmtiEnv::IsMethodObsolete(Method* method, jboolean* is_obsolete_ptr) {
3470 if (use_version_1_0_semantics() &&
3471 get_capabilities()->can_redefine_classes == 0) {
3472 // This JvmtiEnv requested version 1.0 semantics and this function
3473 // requires the can_redefine_classes capability in version 1.0 so
3474 // we need to return an error here.
3475 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3476 }
3477
3478 if (method == nullptr || method->is_obsolete()) {
3479 *is_obsolete_ptr = true;
3480 } else {
3481 *is_obsolete_ptr = false;
3482 }
3483 return JVMTI_ERROR_NONE;
3484 } /* end IsMethodObsolete */
3485
3486 //
3487 // Raw Monitor functions
3488 //
3489
3490 // name - pre-checked for null
3491 // monitor_ptr - pre-checked for null
3492 jvmtiError
3493 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3494 JvmtiRawMonitor* rmonitor = new (std::nothrow) JvmtiRawMonitor(name);
3495 NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3496
3497 *monitor_ptr = (jrawMonitorID)rmonitor;
3498
3499 return JVMTI_ERROR_NONE;
3500 } /* end CreateRawMonitor */
3501
3502
3503 // rmonitor - pre-checked for validity
3504 jvmtiError
3505 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3506 if (Threads::number_of_threads() == 0) {
3507 // Remove this monitor from pending raw monitors list
3508 // if it has entered in onload or start phase.
3509 JvmtiPendingMonitors::destroy(rmonitor);
3510 } else {
3511 Thread* thread = Thread::current();
3512 if (rmonitor->owner() == thread) {
3513 // The caller owns this monitor which we are about to destroy.
3514 // We exit the underlying synchronization object so that the
3515 // "delete monitor" call below can work without an assertion
3516 // failure on systems that don't like destroying synchronization
3517 // objects that are locked.
3518 int r;
3519 int recursion = rmonitor->recursions();
3520 for (int i = 0; i <= recursion; i++) {
3521 r = rmonitor->raw_exit(thread);
3522 assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked");
3523 if (r != JvmtiRawMonitor::M_OK) { // robustness
3524 return JVMTI_ERROR_INTERNAL;
3525 }
3526 }
3527 }
3528 if (rmonitor->owner() != nullptr) {
3529 // The caller is trying to destroy a monitor that is locked by
3530 // someone else. While this is not forbidden by the JVMTI
3531 // spec, it will cause an assertion failure on systems that don't
3532 // like destroying synchronization objects that are locked.
3533 // We indicate a problem with the error return (and leak the
3534 // monitor's memory).
3535 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3536 }
3537 }
3538
3539 delete rmonitor;
3540
3541 return JVMTI_ERROR_NONE;
3542 } /* end DestroyRawMonitor */
3543
3544
3545 // rmonitor - pre-checked for validity
3546 jvmtiError
3547 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3548 if (Threads::number_of_threads() == 0) {
3549 // No JavaThreads exist so JvmtiRawMonitor enter cannot be
3550 // used, add this raw monitor to the pending list.
3551 // The pending monitors will be actually entered when
3552 // the VM is setup.
3553 // See transition_pending_raw_monitors in create_vm()
3554 // in thread.cpp.
3555 JvmtiPendingMonitors::enter(rmonitor);
3556 } else {
3557 Thread* thread = Thread::current();
3558 // 8266889: raw_enter changes Java thread state, needs WXWrite
3559 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3560 rmonitor->raw_enter(thread);
3561 }
3562 return JVMTI_ERROR_NONE;
3563 } /* end RawMonitorEnter */
3564
3565
3566 // rmonitor - pre-checked for validity
3567 jvmtiError
3568 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3569 jvmtiError err = JVMTI_ERROR_NONE;
3570
3571 if (Threads::number_of_threads() == 0) {
3572 // No JavaThreads exist so just remove this monitor from the pending list.
3573 // Bool value from exit is false if rmonitor is not in the list.
3574 if (!JvmtiPendingMonitors::exit(rmonitor)) {
3575 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3576 }
3577 } else {
3578 Thread* thread = Thread::current();
3579 int r = rmonitor->raw_exit(thread);
3580 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3581 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3582 }
3583 }
3584 return err;
3585 } /* end RawMonitorExit */
3586
3587
3588 // rmonitor - pre-checked for validity
3589 jvmtiError
3590 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3591 Thread* thread = Thread::current();
3592 // 8266889: raw_wait changes Java thread state, needs WXWrite
3593 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3594 int r = rmonitor->raw_wait(millis, thread);
3595
3596 switch (r) {
3597 case JvmtiRawMonitor::M_INTERRUPTED:
3598 return JVMTI_ERROR_INTERRUPT;
3599 case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE:
3600 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3601 default:
3602 return JVMTI_ERROR_NONE;
3603 }
3604 } /* end RawMonitorWait */
3605
3606
3607 // rmonitor - pre-checked for validity
3608 jvmtiError
3609 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3610 Thread* thread = Thread::current();
3611 int r = rmonitor->raw_notify(thread);
3612
3613 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3614 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3615 }
3616 return JVMTI_ERROR_NONE;
3617 } /* end RawMonitorNotify */
3618
3619
3620 // rmonitor - pre-checked for validity
3621 jvmtiError
3622 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3623 Thread* thread = Thread::current();
3624 int r = rmonitor->raw_notifyAll(thread);
3625
3626 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3627 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3628 }
3629 return JVMTI_ERROR_NONE;
3630 } /* end RawMonitorNotifyAll */
3631
3632
3633 //
3634 // JNI Function Interception functions
3635 //
3636
3637
3638 // function_table - pre-checked for null
3639 jvmtiError
3640 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3641 // Copy jni function table at safepoint.
3642 VM_JNIFunctionTableCopier copier(function_table);
3643 VMThread::execute(&copier);
3644
3645 return JVMTI_ERROR_NONE;
3646 } /* end SetJNIFunctionTable */
3647
3648
3649 // function_table - pre-checked for null
3650 jvmtiError
3651 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3652 *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3653 if (*function_table == nullptr)
3654 return JVMTI_ERROR_OUT_OF_MEMORY;
3655 memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3656 return JVMTI_ERROR_NONE;
3657 } /* end GetJNIFunctionTable */
3658
3659
3660 //
3661 // Event Management functions
3662 //
3663
3664 jvmtiError
3665 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3666 // can only generate two event types
3667 if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3668 event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3669 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3670 }
3671
3672 // for compiled_method_load events we must check that the environment
3673 // has the can_generate_compiled_method_load_events capability.
3674 if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3675 if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3676 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3677 }
3678 return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3679 } else {
3680 return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3681 }
3682
3683 } /* end GenerateEvents */
3684
3685
3686 //
3687 // Extension Mechanism functions
3688 //
3689
3690 // extension_count_ptr - pre-checked for null
3691 // extensions - pre-checked for null
3692 jvmtiError
3693 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3694 return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3695 } /* end GetExtensionFunctions */
3696
3697
3698 // extension_count_ptr - pre-checked for null
3699 // extensions - pre-checked for null
3700 jvmtiError
3701 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3702 return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3703 } /* end GetExtensionEvents */
3704
3705
3706 // callback - null is a valid value, must be checked
3707 jvmtiError
3708 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3709 return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3710 } /* end SetExtensionEventCallback */
3711
3712 //
3713 // Timers functions
3714 //
3715
3716 // info_ptr - pre-checked for null
3717 jvmtiError
3718 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3719 os::current_thread_cpu_time_info(info_ptr);
3720 return JVMTI_ERROR_NONE;
3721 } /* end GetCurrentThreadCpuTimerInfo */
3722
3723
3724 // nanos_ptr - pre-checked for null
3725 jvmtiError
3726 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3727 Thread* thread = Thread::current();
3728
3729 // Surprisingly the GetCurrentThreadCpuTime is used by non-JavaThread's.
3730 if (thread->is_Java_thread()) {
3731 if (JavaThread::cast(thread)->is_vthread_mounted()) {
3732 // No support for a VirtualThread (yet).
3733 return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3734 }
3735 }
3736 *nanos_ptr = os::current_thread_cpu_time();
3737 return JVMTI_ERROR_NONE;
3738 } /* end GetCurrentThreadCpuTime */
3739
3740
3741 // info_ptr - pre-checked for null
3742 jvmtiError
3743 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3744 os::thread_cpu_time_info(info_ptr);
3745 return JVMTI_ERROR_NONE;
3746 } /* end GetThreadCpuTimerInfo */
3747
3748
3749 // nanos_ptr - pre-checked for null
3750 jvmtiError
3751 JvmtiEnv::GetThreadCpuTime(jthread thread, jlong* nanos_ptr) {
3752 JavaThread* current_thread = JavaThread::current();
3753 ThreadsListHandle tlh(current_thread);
3754 JavaThread* java_thread = nullptr;
3755 oop thread_oop = nullptr;
3756
3757 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
3758
3759 if (thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
3760 // No support for virtual threads (yet).
3761 return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3762 }
3763 if (err != JVMTI_ERROR_NONE) {
3764 return err;
3765 }
3766 NULL_CHECK(nanos_ptr, JVMTI_ERROR_NULL_POINTER);
3767
3768 *nanos_ptr = os::thread_cpu_time(java_thread);
3769 return JVMTI_ERROR_NONE;
3770 } /* end GetThreadCpuTime */
3771
3772
3773 // info_ptr - pre-checked for null
3774 jvmtiError
3775 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3776 os::javaTimeNanos_info(info_ptr);
3777 return JVMTI_ERROR_NONE;
3778 } /* end GetTimerInfo */
3779
3780
3781 // nanos_ptr - pre-checked for null
3782 jvmtiError
3783 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3784 *nanos_ptr = os::javaTimeNanos();
3785 return JVMTI_ERROR_NONE;
3786 } /* end GetTime */
3787
3788
3789 // processor_count_ptr - pre-checked for null
3790 jvmtiError
3791 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3792 *processor_count_ptr = os::active_processor_count();
3793 return JVMTI_ERROR_NONE;
3794 } /* end GetAvailableProcessors */
3795
3796 jvmtiError
3797 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3798 if (sampling_interval < 0) {
3799 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3800 }
3801 ThreadHeapSampler::set_sampling_interval(sampling_interval);
3802 return JVMTI_ERROR_NONE;
3803 } /* end SetHeapSamplingInterval */
3804
3805 //
3806 // System Properties functions
3807 //
3808
3809 // count_ptr - pre-checked for null
3810 // property_ptr - pre-checked for null
3811 jvmtiError
3812 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3813 jvmtiError err = JVMTI_ERROR_NONE;
3814
3815 // Get the number of readable properties.
3816 *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3817
3818 // Allocate memory to hold the exact number of readable properties.
3819 err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3820 if (err != JVMTI_ERROR_NONE) {
3821 return err;
3822 }
3823 int readable_count = 0;
3824 // Loop through the system properties until all the readable properties are found.
3825 for (SystemProperty* p = Arguments::system_properties(); p != nullptr && readable_count < *count_ptr; p = p->next()) {
3826 if (p->readable()) {
3827 const char *key = p->key();
3828 char **tmp_value = *property_ptr+readable_count;
3829 readable_count++;
3830 err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3831 if (err == JVMTI_ERROR_NONE) {
3832 strcpy(*tmp_value, key);
3833 } else {
3834 // clean up previously allocated memory.
3835 for (int j = 0; j < readable_count; j++) {
3836 Deallocate((unsigned char*)*property_ptr+j);
3837 }
3838 Deallocate((unsigned char*)property_ptr);
3839 break;
3840 }
3841 }
3842 }
3843 assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3844 return err;
3845 } /* end GetSystemProperties */
3846
3847
3848 // property - pre-checked for null
3849 // value_ptr - pre-checked for null
3850 jvmtiError
3851 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3852 jvmtiError err = JVMTI_ERROR_NONE;
3853 const char *value;
3854
3855 // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3856 value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3857 if (value == nullptr) {
3858 err = JVMTI_ERROR_NOT_AVAILABLE;
3859 } else {
3860 err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3861 if (err == JVMTI_ERROR_NONE) {
3862 strcpy(*value_ptr, value);
3863 }
3864 }
3865 return err;
3866 } /* end GetSystemProperty */
3867
3868
3869 // property - pre-checked for null
3870 // value - null is a valid value, must be checked
3871 jvmtiError
3872 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3873 for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
3874 if (strcmp(property, p->key()) == 0) {
3875 if (p->writeable()) {
3876 if (p->set_value(value_ptr, AllocFailStrategy::RETURN_NULL)) {
3877 return JVMTI_ERROR_NONE;
3878 } else {
3879 return JVMTI_ERROR_OUT_OF_MEMORY;
3880 }
3881 } else {
3882 // We found a property, but it's not writeable
3883 return JVMTI_ERROR_NOT_AVAILABLE;
3884 }
3885 }
3886 }
3887
3888 // We cannot find a property of the given name
3889 return JVMTI_ERROR_NOT_AVAILABLE;
3890 } /* end SetSystemProperty */