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