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