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 objArrayHandle 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 (!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 }
2841 assert(flds.done(), "just checking");
2842
2843 // Fill in the results
2844 *field_count_ptr = result_count;
2845 *fields_ptr = result_list;
2846
2847 return JVMTI_ERROR_NONE;
2848 } /* end GetClassFields */
2849
2850
2851 // k_mirror - may be primitive, this must be checked
2852 // interface_count_ptr - pre-checked for null
2853 // interfaces_ptr - pre-checked for null
2854 jvmtiError
2855 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2856 {
2857 if (java_lang_Class::is_primitive(k_mirror)) {
2858 *interface_count_ptr = 0;
2859 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2860 return JVMTI_ERROR_NONE;
2861 }
2862 JavaThread* current_thread = JavaThread::current();
2863 HandleMark hm(current_thread);
2864 Klass* k = java_lang_Class::as_Klass(k_mirror);
2865 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2866
2867 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2868 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2869 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2870
2871 if (!k->is_instance_klass()) {
2872 *interface_count_ptr = 0;
2873 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2874 return JVMTI_ERROR_NONE;
2875 }
2876
2877 Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2878 const int result_length = (interface_list == nullptr ? 0 : interface_list->length());
2879 jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2880 for (int i_index = 0; i_index < result_length; i_index += 1) {
2881 InstanceKlass* klass_at = interface_list->at(i_index);
2882 assert(klass_at->is_klass(), "interfaces must be Klass*s");
2883 assert(klass_at->is_interface(), "interfaces must be interfaces");
2884 oop mirror_at = klass_at->java_mirror();
2885 Handle handle_at = Handle(current_thread, mirror_at);
2886 result_list[i_index] = (jclass) jni_reference(handle_at);
2887 }
2888 *interface_count_ptr = result_length;
2889 *interfaces_ptr = result_list;
2890 }
2891
2892 return JVMTI_ERROR_NONE;
2893 } /* end GetImplementedInterfaces */
2894
2895
2896 // k_mirror - may be primitive, this must be checked
2897 // minor_version_ptr - pre-checked for null
2898 // major_version_ptr - pre-checked for null
2899 jvmtiError
2900 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2901 if (java_lang_Class::is_primitive(k_mirror)) {
2902 return JVMTI_ERROR_ABSENT_INFORMATION;
2903 }
2904 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2905
2906 jint status = klass->jvmti_class_status();
2907 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2908 return JVMTI_ERROR_INVALID_CLASS;
2909 }
2910 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2911 return JVMTI_ERROR_ABSENT_INFORMATION;
2912 }
2913
2914 InstanceKlass* ik = InstanceKlass::cast(klass);
2915 *minor_version_ptr = ik->minor_version();
2916 *major_version_ptr = ik->major_version();
2917
2918 return JVMTI_ERROR_NONE;
2919 } /* end GetClassVersionNumbers */
2920
2921
2922 // k_mirror - may be primitive, this must be checked
2923 // constant_pool_count_ptr - pre-checked for null
2924 // constant_pool_byte_count_ptr - pre-checked for null
2925 // constant_pool_bytes_ptr - pre-checked for null
2926 jvmtiError
2927 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2928 if (java_lang_Class::is_primitive(k_mirror)) {
2929 return JVMTI_ERROR_ABSENT_INFORMATION;
2930 }
2931
2932 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2933 Thread *thread = Thread::current();
2934 ResourceMark rm(thread);
2935
2936 jint status = klass->jvmti_class_status();
2937 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2938 return JVMTI_ERROR_INVALID_CLASS;
2939 }
2940 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2941 return JVMTI_ERROR_ABSENT_INFORMATION;
2942 }
2943
2944 InstanceKlass* ik = InstanceKlass::cast(klass);
2945 JvmtiConstantPoolReconstituter reconstituter(ik);
2946 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2947 return reconstituter.get_error();
2948 }
2949
2950 unsigned char *cpool_bytes;
2951 int cpool_size = reconstituter.cpool_size();
2952 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2953 return reconstituter.get_error();
2954 }
2955 jvmtiError res = allocate(cpool_size, &cpool_bytes);
2956 if (res != JVMTI_ERROR_NONE) {
2957 return res;
2958 }
2959 reconstituter.copy_cpool_bytes(cpool_bytes);
2960 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2961 return reconstituter.get_error();
2962 }
2963
2964 constantPoolHandle constants(thread, ik->constants());
2965 *constant_pool_count_ptr = constants->length();
2966 *constant_pool_byte_count_ptr = cpool_size;
2967 *constant_pool_bytes_ptr = cpool_bytes;
2968
2969 return JVMTI_ERROR_NONE;
2970 } /* end GetConstantPool */
2971
2972
2973 // k_mirror - may be primitive, this must be checked
2974 // is_interface_ptr - pre-checked for null
2975 jvmtiError
2976 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2977 {
2978 bool result = false;
2979 if (!java_lang_Class::is_primitive(k_mirror)) {
2980 Klass* k = java_lang_Class::as_Klass(k_mirror);
2981 if (k != nullptr && k->is_interface()) {
2982 result = true;
2983 }
2984 }
2985 *is_interface_ptr = result;
2986 }
2987
2988 return JVMTI_ERROR_NONE;
2989 } /* end IsInterface */
2990
2991
2992 // k_mirror - may be primitive, this must be checked
2993 // is_array_class_ptr - pre-checked for null
2994 jvmtiError
2995 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2996 {
2997 bool result = false;
2998 if (!java_lang_Class::is_primitive(k_mirror)) {
2999 Klass* k = java_lang_Class::as_Klass(k_mirror);
3000 if (k != nullptr && k->is_array_klass()) {
3001 result = true;
3002 }
3003 }
3004 *is_array_class_ptr = result;
3005 }
3006
3007 return JVMTI_ERROR_NONE;
3008 } /* end IsArrayClass */
3009
3010
3011 // k_mirror - may be primitive, this must be checked
3012 // classloader_ptr - pre-checked for null
3013 jvmtiError
3014 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
3015 {
3016 if (java_lang_Class::is_primitive(k_mirror)) {
3017 *classloader_ptr = (jclass) jni_reference(Handle());
3018 return JVMTI_ERROR_NONE;
3019 }
3020 JavaThread* current_thread = JavaThread::current();
3021 HandleMark hm(current_thread);
3022 Klass* k = java_lang_Class::as_Klass(k_mirror);
3023 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3024
3025 oop result_oop = k->class_loader();
3026 if (result_oop == nullptr) {
3027 *classloader_ptr = (jclass) jni_reference(Handle());
3028 return JVMTI_ERROR_NONE;
3029 }
3030 Handle result_handle = Handle(current_thread, result_oop);
3031 jclass result_jnihandle = (jclass) jni_reference(result_handle);
3032 *classloader_ptr = result_jnihandle;
3033 }
3034 return JVMTI_ERROR_NONE;
3035 } /* end GetClassLoader */
3036
3037
3038 // k_mirror - may be primitive, this must be checked
3039 // source_debug_extension_ptr - pre-checked for null
3040 jvmtiError
3041 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
3042 {
3043 if (java_lang_Class::is_primitive(k_mirror)) {
3044 return JVMTI_ERROR_ABSENT_INFORMATION;
3045 }
3046 Klass* k = java_lang_Class::as_Klass(k_mirror);
3047 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
3048 if (!k->is_instance_klass()) {
3049 return JVMTI_ERROR_ABSENT_INFORMATION;
3050 }
3051 const char* sde = InstanceKlass::cast(k)->source_debug_extension();
3052 NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
3053
3054 {
3055 *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
3056 strcpy(*source_debug_extension_ptr, sde);
3057 }
3058 }
3059
3060 return JVMTI_ERROR_NONE;
3061 } /* end GetSourceDebugExtension */
3062
3063 //
3064 // Object functions
3065 //
3066
3067 // hash_code_ptr - pre-checked for null
3068 jvmtiError
3069 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
3070 oop mirror = JNIHandles::resolve_external_guard(object);
3071 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
3072 NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
3073
3074 {
3075 jint result = (jint) mirror->identity_hash();
3076 *hash_code_ptr = result;
3077 }
3078 return JVMTI_ERROR_NONE;
3079 } /* end GetObjectHashCode */
3080
3081
3082 // info_ptr - pre-checked for null
3083 jvmtiError
3084 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
3085 // This needs to be performed at a safepoint to gather stable data
3086 // because monitor owner / waiters might not be suspended.
3087 VM_GetObjectMonitorUsage op(this, JavaThread::current(), object, info_ptr);
3088 VMThread::execute(&op);
3089 return op.result();
3090 } /* end GetObjectMonitorUsage */
3091
3092
3093 //
3094 // Field functions
3095 //
3096
3097 // name_ptr - null is a valid value, must be checked
3098 // signature_ptr - null is a valid value, must be checked
3099 // generic_ptr - null is a valid value, must be checked
3100 jvmtiError
3101 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3102 JavaThread* current_thread = JavaThread::current();
3103 ResourceMark rm(current_thread);
3104 if (name_ptr == nullptr) {
3105 // just don't return the name
3106 } else {
3107 const char* fieldName = fdesc_ptr->name()->as_C_string();
3108 *name_ptr = (char*) jvmtiMalloc(strlen(fieldName) + 1);
3109 if (*name_ptr == nullptr)
3110 return JVMTI_ERROR_OUT_OF_MEMORY;
3111 strcpy(*name_ptr, fieldName);
3112 }
3113 if (signature_ptr== nullptr) {
3114 // just don't return the signature
3115 } else {
3116 const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
3117 *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
3118 if (*signature_ptr == nullptr)
3119 return JVMTI_ERROR_OUT_OF_MEMORY;
3120 strcpy(*signature_ptr, fieldSignature);
3121 }
3122 if (generic_ptr != nullptr) {
3123 *generic_ptr = nullptr;
3124 Symbol* soop = fdesc_ptr->generic_signature();
3125 if (soop != nullptr) {
3126 const char* gen_sig = soop->as_C_string();
3127 if (gen_sig != nullptr) {
3128 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3129 if (err != JVMTI_ERROR_NONE) {
3130 return err;
3131 }
3132 strcpy(*generic_ptr, gen_sig);
3133 }
3134 }
3135 }
3136 return JVMTI_ERROR_NONE;
3137 } /* end GetFieldName */
3138
3139
3140 // declaring_class_ptr - pre-checked for null
3141 jvmtiError
3142 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
3143 // As for the GetFieldDeclaringClass method, the XSL generated C++ code that calls it has
3144 // a jclass of the relevant class or a subclass of it, which is fine in terms of ensuring
3145 // the holder is kept alive.
3146 *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
3147 return JVMTI_ERROR_NONE;
3148 } /* end GetFieldDeclaringClass */
3149
3150
3151 // modifiers_ptr - pre-checked for null
3152 jvmtiError
3153 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
3154
3155 AccessFlags resultFlags = fdesc_ptr->access_flags();
3156 jint result = resultFlags.as_field_flags();
3157 *modifiers_ptr = result;
3158
3159 return JVMTI_ERROR_NONE;
3160 } /* end GetFieldModifiers */
3161
3162
3163 // is_synthetic_ptr - pre-checked for null
3164 jvmtiError
3165 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
3166 *is_synthetic_ptr = fdesc_ptr->is_synthetic();
3167 return JVMTI_ERROR_NONE;
3168 } /* end IsFieldSynthetic */
3169
3170
3171 //
3172 // Method functions
3173 //
3174
3175 // method - pre-checked for validity, but may be null meaning obsolete method
3176 // name_ptr - null is a valid value, must be checked
3177 // signature_ptr - null is a valid value, must be checked
3178 // generic_ptr - null is a valid value, must be checked
3179 jvmtiError
3180 JvmtiEnv::GetMethodName(Method* method, char** name_ptr, char** signature_ptr, char** generic_ptr) {
3181 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3182 JavaThread* current_thread = JavaThread::current();
3183
3184 ResourceMark rm(current_thread); // get the utf8 name and signature
3185 if (name_ptr == nullptr) {
3186 // just don't return the name
3187 } else {
3188 const char* utf8_name = (const char *) method->name()->as_utf8();
3189 *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3190 strcpy(*name_ptr, utf8_name);
3191 }
3192 if (signature_ptr == nullptr) {
3193 // just don't return the signature
3194 } else {
3195 const char* utf8_signature = (const char *) method->signature()->as_utf8();
3196 *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
3197 strcpy(*signature_ptr, utf8_signature);
3198 }
3199
3200 if (generic_ptr != nullptr) {
3201 *generic_ptr = nullptr;
3202 Symbol* soop = method->generic_signature();
3203 if (soop != nullptr) {
3204 const char* gen_sig = soop->as_C_string();
3205 if (gen_sig != nullptr) {
3206 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
3207 if (err != JVMTI_ERROR_NONE) {
3208 return err;
3209 }
3210 strcpy(*generic_ptr, gen_sig);
3211 }
3212 }
3213 }
3214 return JVMTI_ERROR_NONE;
3215 } /* end GetMethodName */
3216
3217
3218 // method - pre-checked for validity, but may be null meaning obsolete method
3219 // declaring_class_ptr - pre-checked for null
3220 jvmtiError
3221 JvmtiEnv::GetMethodDeclaringClass(Method* method, jclass* declaring_class_ptr) {
3222 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3223 Klass* k = method->method_holder();
3224 Handle holder(Thread::current(), k->klass_holder()); // keep the klass alive
3225 (*declaring_class_ptr) = get_jni_class_non_null(k);
3226 return JVMTI_ERROR_NONE;
3227 } /* end GetMethodDeclaringClass */
3228
3229
3230 // method - pre-checked for validity, but may be null meaning obsolete method
3231 // modifiers_ptr - pre-checked for null
3232 jvmtiError
3233 JvmtiEnv::GetMethodModifiers(Method* method, jint* modifiers_ptr) {
3234 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3235 (*modifiers_ptr) = method->access_flags().as_method_flags();
3236 return JVMTI_ERROR_NONE;
3237 } /* end GetMethodModifiers */
3238
3239
3240 // method - pre-checked for validity, but may be null meaning obsolete method
3241 // max_ptr - pre-checked for null
3242 jvmtiError
3243 JvmtiEnv::GetMaxLocals(Method* method, jint* max_ptr) {
3244 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3245 // get max stack
3246 (*max_ptr) = method->max_locals();
3247 return JVMTI_ERROR_NONE;
3248 } /* end GetMaxLocals */
3249
3250
3251 // method - pre-checked for validity, but may be null meaning obsolete method
3252 // size_ptr - pre-checked for null
3253 jvmtiError
3254 JvmtiEnv::GetArgumentsSize(Method* method, jint* size_ptr) {
3255 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3256 // get size of arguments
3257
3258 (*size_ptr) = method->size_of_parameters();
3259 return JVMTI_ERROR_NONE;
3260 } /* end GetArgumentsSize */
3261
3262
3263 // method - pre-checked for validity, but may be null meaning obsolete method
3264 // entry_count_ptr - pre-checked for null
3265 // table_ptr - pre-checked for null
3266 jvmtiError
3267 JvmtiEnv::GetLineNumberTable(Method* method, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3268 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3269 if (!method->has_linenumber_table()) {
3270 return (JVMTI_ERROR_ABSENT_INFORMATION);
3271 }
3272
3273 // The line number table is compressed so we don't know how big it is until decompressed.
3274 // Decompression is really fast so we just do it twice.
3275
3276 // Compute size of table
3277 jint num_entries = 0;
3278 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3279 while (stream.read_pair()) {
3280 num_entries++;
3281 }
3282 jvmtiLineNumberEntry *jvmti_table =
3283 (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3284
3285 // Fill jvmti table
3286 if (num_entries > 0) {
3287 int index = 0;
3288 CompressedLineNumberReadStream stream(method->compressed_linenumber_table());
3289 while (stream.read_pair()) {
3290 jvmti_table[index].start_location = (jlocation) stream.bci();
3291 jvmti_table[index].line_number = (jint) stream.line();
3292 index++;
3293 }
3294 assert(index == num_entries, "sanity check");
3295 }
3296
3297 // Set up results
3298 (*entry_count_ptr) = num_entries;
3299 (*table_ptr) = jvmti_table;
3300
3301 return JVMTI_ERROR_NONE;
3302 } /* end GetLineNumberTable */
3303
3304
3305 // method - pre-checked for validity, but may be null meaning obsolete method
3306 // start_location_ptr - pre-checked for null
3307 // end_location_ptr - pre-checked for null
3308 jvmtiError
3309 JvmtiEnv::GetMethodLocation(Method* method, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3310
3311 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3312 // get start and end location
3313 (*end_location_ptr) = (jlocation) (method->code_size() - 1);
3314 if (method->code_size() == 0) {
3315 // there is no code so there is no start location
3316 (*start_location_ptr) = (jlocation)(-1);
3317 } else {
3318 (*start_location_ptr) = (jlocation)(0);
3319 }
3320
3321 return JVMTI_ERROR_NONE;
3322 } /* end GetMethodLocation */
3323
3324
3325 // method - pre-checked for validity, but may be null meaning obsolete method
3326 // entry_count_ptr - pre-checked for null
3327 // table_ptr - pre-checked for null
3328 jvmtiError
3329 JvmtiEnv::GetLocalVariableTable(Method* method, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3330
3331 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3332 JavaThread* current_thread = JavaThread::current();
3333
3334 // does the klass have any local variable information?
3335 InstanceKlass* ik = method->method_holder();
3336 if (!ik->has_localvariable_table()) {
3337 return (JVMTI_ERROR_ABSENT_INFORMATION);
3338 }
3339
3340 ConstantPool* constants = method->constants();
3341 NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3342
3343 // in the vm localvariable table representation, 6 consecutive elements in the table
3344 // represent a 6-tuple of shorts
3345 // [start_pc, length, name_index, descriptor_index, signature_index, index]
3346 jint num_entries = method->localvariable_table_length();
3347 jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3348 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3349
3350 if (num_entries > 0) {
3351 LocalVariableTableElement* table = method->localvariable_table_start();
3352 for (int i = 0; i < num_entries; i++) {
3353 // get the 5 tuple information from the vm table
3354 jlocation start_location = (jlocation) table[i].start_bci;
3355 jint length = (jint) table[i].length;
3356 int name_index = (int) table[i].name_cp_index;
3357 int signature_index = (int) table[i].descriptor_cp_index;
3358 int generic_signature_index = (int) table[i].signature_cp_index;
3359 jint slot = (jint) table[i].slot;
3360
3361 // get utf8 name and signature
3362 char *name_buf = nullptr;
3363 char *sig_buf = nullptr;
3364 char *gen_sig_buf = nullptr;
3365 {
3366 ResourceMark rm(current_thread);
3367
3368 const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3369 name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3370 strcpy(name_buf, utf8_name);
3371
3372 const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3373 sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3374 strcpy(sig_buf, utf8_signature);
3375
3376 if (generic_signature_index > 0) {
3377 const char *utf8_gen_sign = (const char *)
3378 constants->symbol_at(generic_signature_index)->as_utf8();
3379 gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3380 strcpy(gen_sig_buf, utf8_gen_sign);
3381 }
3382 }
3383
3384 // fill in the jvmti local variable table
3385 jvmti_table[i].start_location = start_location;
3386 jvmti_table[i].length = length;
3387 jvmti_table[i].name = name_buf;
3388 jvmti_table[i].signature = sig_buf;
3389 jvmti_table[i].generic_signature = gen_sig_buf;
3390 jvmti_table[i].slot = slot;
3391 }
3392 }
3393
3394 // set results
3395 (*entry_count_ptr) = num_entries;
3396 (*table_ptr) = jvmti_table;
3397
3398 return JVMTI_ERROR_NONE;
3399 } /* end GetLocalVariableTable */
3400
3401
3402 // method - pre-checked for validity, but may be null meaning obsolete method
3403 // bytecode_count_ptr - pre-checked for null
3404 // bytecodes_ptr - pre-checked for null
3405 jvmtiError
3406 JvmtiEnv::GetBytecodes(Method* method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3407 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3408
3409 JavaThread* current_thread = JavaThread::current();
3410 methodHandle mh(current_thread, method);
3411 jint size = (jint)mh->code_size();
3412 jvmtiError err = allocate(size, bytecodes_ptr);
3413 if (err != JVMTI_ERROR_NONE) {
3414 return err;
3415 }
3416
3417 (*bytecode_count_ptr) = size;
3418 // get byte codes
3419 // Make sure the class is verified and rewritten first.
3420 JavaThread* THREAD = current_thread;
3421 mh->method_holder()->link_class(THREAD);
3422 if (HAS_PENDING_EXCEPTION) {
3423 CLEAR_PENDING_EXCEPTION;
3424 return JVMTI_ERROR_INVALID_CLASS;
3425 }
3426 JvmtiClassFileReconstituter::copy_bytecodes(mh, *bytecodes_ptr);
3427
3428 return JVMTI_ERROR_NONE;
3429 } /* end GetBytecodes */
3430
3431
3432 // method - pre-checked for validity, but may be null meaning obsolete method
3433 // is_native_ptr - pre-checked for null
3434 jvmtiError
3435 JvmtiEnv::IsMethodNative(Method* method, jboolean* is_native_ptr) {
3436 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3437 (*is_native_ptr) = method->is_native();
3438 return JVMTI_ERROR_NONE;
3439 } /* end IsMethodNative */
3440
3441
3442 // method - pre-checked for validity, but may be null meaning obsolete method
3443 // is_synthetic_ptr - pre-checked for null
3444 jvmtiError
3445 JvmtiEnv::IsMethodSynthetic(Method* method, jboolean* is_synthetic_ptr) {
3446 NULL_CHECK(method, JVMTI_ERROR_INVALID_METHODID);
3447 (*is_synthetic_ptr) = method->is_synthetic();
3448 return JVMTI_ERROR_NONE;
3449 } /* end IsMethodSynthetic */
3450
3451
3452 // method - pre-checked for validity, but may be null meaning obsolete method
3453 // is_obsolete_ptr - pre-checked for null
3454 jvmtiError
3455 JvmtiEnv::IsMethodObsolete(Method* method, jboolean* is_obsolete_ptr) {
3456 if (use_version_1_0_semantics() &&
3457 get_capabilities()->can_redefine_classes == 0) {
3458 // This JvmtiEnv requested version 1.0 semantics and this function
3459 // requires the can_redefine_classes capability in version 1.0 so
3460 // we need to return an error here.
3461 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3462 }
3463
3464 if (method == nullptr || method->is_obsolete()) {
3465 *is_obsolete_ptr = true;
3466 } else {
3467 *is_obsolete_ptr = false;
3468 }
3469 return JVMTI_ERROR_NONE;
3470 } /* end IsMethodObsolete */
3471
3472 //
3473 // Raw Monitor functions
3474 //
3475
3476 // name - pre-checked for null
3477 // monitor_ptr - pre-checked for null
3478 jvmtiError
3479 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3480 JvmtiRawMonitor* rmonitor = new (std::nothrow) JvmtiRawMonitor(name);
3481 NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3482
3483 *monitor_ptr = (jrawMonitorID)rmonitor;
3484
3485 return JVMTI_ERROR_NONE;
3486 } /* end CreateRawMonitor */
3487
3488
3489 // rmonitor - pre-checked for validity
3490 jvmtiError
3491 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3492 if (Threads::number_of_threads() == 0) {
3493 // Remove this monitor from pending raw monitors list
3494 // if it has entered in onload or start phase.
3495 JvmtiPendingMonitors::destroy(rmonitor);
3496 } else {
3497 Thread* thread = Thread::current();
3498 if (rmonitor->owner() == thread) {
3499 // The caller owns this monitor which we are about to destroy.
3500 // We exit the underlying synchronization object so that the
3501 // "delete monitor" call below can work without an assertion
3502 // failure on systems that don't like destroying synchronization
3503 // objects that are locked.
3504 int r;
3505 int recursion = rmonitor->recursions();
3506 for (int i = 0; i <= recursion; i++) {
3507 r = rmonitor->raw_exit(thread);
3508 assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked");
3509 if (r != JvmtiRawMonitor::M_OK) { // robustness
3510 return JVMTI_ERROR_INTERNAL;
3511 }
3512 }
3513 }
3514 if (rmonitor->owner() != nullptr) {
3515 // The caller is trying to destroy a monitor that is locked by
3516 // someone else. While this is not forbidden by the JVMTI
3517 // spec, it will cause an assertion failure on systems that don't
3518 // like destroying synchronization objects that are locked.
3519 // We indicate a problem with the error return (and leak the
3520 // monitor's memory).
3521 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3522 }
3523 }
3524
3525 delete rmonitor;
3526
3527 return JVMTI_ERROR_NONE;
3528 } /* end DestroyRawMonitor */
3529
3530
3531 // rmonitor - pre-checked for validity
3532 jvmtiError
3533 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3534 if (Threads::number_of_threads() == 0) {
3535 // No JavaThreads exist so JvmtiRawMonitor enter cannot be
3536 // used, add this raw monitor to the pending list.
3537 // The pending monitors will be actually entered when
3538 // the VM is setup.
3539 // See transition_pending_raw_monitors in create_vm()
3540 // in thread.cpp.
3541 JvmtiPendingMonitors::enter(rmonitor);
3542 } else {
3543 Thread* thread = Thread::current();
3544 // 8266889: raw_enter changes Java thread state, needs WXWrite
3545 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3546 rmonitor->raw_enter(thread);
3547 }
3548 return JVMTI_ERROR_NONE;
3549 } /* end RawMonitorEnter */
3550
3551
3552 // rmonitor - pre-checked for validity
3553 jvmtiError
3554 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3555 jvmtiError err = JVMTI_ERROR_NONE;
3556
3557 if (Threads::number_of_threads() == 0) {
3558 // No JavaThreads exist so just remove this monitor from the pending list.
3559 // Bool value from exit is false if rmonitor is not in the list.
3560 if (!JvmtiPendingMonitors::exit(rmonitor)) {
3561 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3562 }
3563 } else {
3564 Thread* thread = Thread::current();
3565 int r = rmonitor->raw_exit(thread);
3566 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3567 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3568 }
3569 }
3570 return err;
3571 } /* end RawMonitorExit */
3572
3573
3574 // rmonitor - pre-checked for validity
3575 jvmtiError
3576 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3577 Thread* thread = Thread::current();
3578 // 8266889: raw_wait changes Java thread state, needs WXWrite
3579 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
3580 int r = rmonitor->raw_wait(millis, thread);
3581
3582 switch (r) {
3583 case JvmtiRawMonitor::M_INTERRUPTED:
3584 return JVMTI_ERROR_INTERRUPT;
3585 case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE:
3586 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3587 default:
3588 return JVMTI_ERROR_NONE;
3589 }
3590 } /* end RawMonitorWait */
3591
3592
3593 // rmonitor - pre-checked for validity
3594 jvmtiError
3595 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3596 Thread* thread = Thread::current();
3597 int r = rmonitor->raw_notify(thread);
3598
3599 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3600 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3601 }
3602 return JVMTI_ERROR_NONE;
3603 } /* end RawMonitorNotify */
3604
3605
3606 // rmonitor - pre-checked for validity
3607 jvmtiError
3608 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3609 Thread* thread = Thread::current();
3610 int r = rmonitor->raw_notifyAll(thread);
3611
3612 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3613 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3614 }
3615 return JVMTI_ERROR_NONE;
3616 } /* end RawMonitorNotifyAll */
3617
3618
3619 //
3620 // JNI Function Interception functions
3621 //
3622
3623
3624 // function_table - pre-checked for null
3625 jvmtiError
3626 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3627 // Copy jni function table at safepoint.
3628 VM_JNIFunctionTableCopier copier(function_table);
3629 VMThread::execute(&copier);
3630
3631 return JVMTI_ERROR_NONE;
3632 } /* end SetJNIFunctionTable */
3633
3634
3635 // function_table - pre-checked for null
3636 jvmtiError
3637 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3638 *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3639 if (*function_table == nullptr)
3640 return JVMTI_ERROR_OUT_OF_MEMORY;
3641 memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3642 return JVMTI_ERROR_NONE;
3643 } /* end GetJNIFunctionTable */
3644
3645
3646 //
3647 // Event Management functions
3648 //
3649
3650 jvmtiError
3651 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3652 // can only generate two event types
3653 if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3654 event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3655 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3656 }
3657
3658 // for compiled_method_load events we must check that the environment
3659 // has the can_generate_compiled_method_load_events capability.
3660 if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3661 if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3662 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3663 }
3664 return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3665 } else {
3666 return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3667 }
3668
3669 } /* end GenerateEvents */
3670
3671
3672 //
3673 // Extension Mechanism functions
3674 //
3675
3676 // extension_count_ptr - pre-checked for null
3677 // extensions - pre-checked for null
3678 jvmtiError
3679 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3680 return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3681 } /* end GetExtensionFunctions */
3682
3683
3684 // extension_count_ptr - pre-checked for null
3685 // extensions - pre-checked for null
3686 jvmtiError
3687 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3688 return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3689 } /* end GetExtensionEvents */
3690
3691
3692 // callback - null is a valid value, must be checked
3693 jvmtiError
3694 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3695 return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3696 } /* end SetExtensionEventCallback */
3697
3698 //
3699 // Timers functions
3700 //
3701
3702 // info_ptr - pre-checked for null
3703 jvmtiError
3704 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3705 os::current_thread_cpu_time_info(info_ptr);
3706 return JVMTI_ERROR_NONE;
3707 } /* end GetCurrentThreadCpuTimerInfo */
3708
3709
3710 // nanos_ptr - pre-checked for null
3711 jvmtiError
3712 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3713 Thread* thread = Thread::current();
3714
3715 // Surprisingly the GetCurrentThreadCpuTime is used by non-JavaThread's.
3716 if (thread->is_Java_thread()) {
3717 if (JavaThread::cast(thread)->is_vthread_mounted()) {
3718 // No support for a VirtualThread (yet).
3719 return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3720 }
3721 }
3722 *nanos_ptr = os::current_thread_cpu_time();
3723 return JVMTI_ERROR_NONE;
3724 } /* end GetCurrentThreadCpuTime */
3725
3726
3727 // info_ptr - pre-checked for null
3728 jvmtiError
3729 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3730 os::thread_cpu_time_info(info_ptr);
3731 return JVMTI_ERROR_NONE;
3732 } /* end GetThreadCpuTimerInfo */
3733
3734
3735 // nanos_ptr - pre-checked for null
3736 jvmtiError
3737 JvmtiEnv::GetThreadCpuTime(jthread thread, jlong* nanos_ptr) {
3738 JavaThread* current_thread = JavaThread::current();
3739 ThreadsListHandle tlh(current_thread);
3740 JavaThread* java_thread = nullptr;
3741 oop thread_oop = nullptr;
3742
3743 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_oop);
3744
3745 if (thread_oop != nullptr && thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
3746 // No support for virtual threads (yet).
3747 return JVMTI_ERROR_UNSUPPORTED_OPERATION;
3748 }
3749 if (err != JVMTI_ERROR_NONE) {
3750 return err;
3751 }
3752 NULL_CHECK(nanos_ptr, JVMTI_ERROR_NULL_POINTER);
3753
3754 *nanos_ptr = os::thread_cpu_time(java_thread);
3755 return JVMTI_ERROR_NONE;
3756 } /* end GetThreadCpuTime */
3757
3758
3759 // info_ptr - pre-checked for null
3760 jvmtiError
3761 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3762 os::javaTimeNanos_info(info_ptr);
3763 return JVMTI_ERROR_NONE;
3764 } /* end GetTimerInfo */
3765
3766
3767 // nanos_ptr - pre-checked for null
3768 jvmtiError
3769 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3770 *nanos_ptr = os::javaTimeNanos();
3771 return JVMTI_ERROR_NONE;
3772 } /* end GetTime */
3773
3774
3775 // processor_count_ptr - pre-checked for null
3776 jvmtiError
3777 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3778 *processor_count_ptr = os::active_processor_count();
3779 return JVMTI_ERROR_NONE;
3780 } /* end GetAvailableProcessors */
3781
3782 jvmtiError
3783 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3784 if (sampling_interval < 0) {
3785 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3786 }
3787 ThreadHeapSampler::set_sampling_interval(sampling_interval);
3788 return JVMTI_ERROR_NONE;
3789 } /* end SetHeapSamplingInterval */
3790
3791 //
3792 // System Properties functions
3793 //
3794
3795 // count_ptr - pre-checked for null
3796 // property_ptr - pre-checked for null
3797 jvmtiError
3798 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3799 jvmtiError err = JVMTI_ERROR_NONE;
3800
3801 // Get the number of readable properties.
3802 *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3803
3804 // Allocate memory to hold the exact number of readable properties.
3805 err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3806 if (err != JVMTI_ERROR_NONE) {
3807 return err;
3808 }
3809 int readable_count = 0;
3810 // Loop through the system properties until all the readable properties are found.
3811 for (SystemProperty* p = Arguments::system_properties(); p != nullptr && readable_count < *count_ptr; p = p->next()) {
3812 if (p->readable()) {
3813 const char *key = p->key();
3814 char **tmp_value = *property_ptr+readable_count;
3815 readable_count++;
3816 err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3817 if (err == JVMTI_ERROR_NONE) {
3818 strcpy(*tmp_value, key);
3819 } else {
3820 // clean up previously allocated memory.
3821 for (int j = 0; j < readable_count; j++) {
3822 Deallocate((unsigned char*)*property_ptr+j);
3823 }
3824 Deallocate((unsigned char*)property_ptr);
3825 break;
3826 }
3827 }
3828 }
3829 assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3830 return err;
3831 } /* end GetSystemProperties */
3832
3833
3834 // property - pre-checked for null
3835 // value_ptr - pre-checked for null
3836 jvmtiError
3837 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3838 jvmtiError err = JVMTI_ERROR_NONE;
3839 const char *value;
3840
3841 // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3842 value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3843 if (value == nullptr) {
3844 err = JVMTI_ERROR_NOT_AVAILABLE;
3845 } else {
3846 err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3847 if (err == JVMTI_ERROR_NONE) {
3848 strcpy(*value_ptr, value);
3849 }
3850 }
3851 return err;
3852 } /* end GetSystemProperty */
3853
3854
3855 // property - pre-checked for null
3856 // value - null is a valid value, must be checked
3857 jvmtiError
3858 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3859 for (SystemProperty* p = Arguments::system_properties(); p != nullptr; p = p->next()) {
3860 if (strcmp(property, p->key()) == 0) {
3861 if (p->writeable()) {
3862 if (p->set_value(value_ptr, AllocFailStrategy::RETURN_NULL)) {
3863 return JVMTI_ERROR_NONE;
3864 } else {
3865 return JVMTI_ERROR_OUT_OF_MEMORY;
3866 }
3867 } else {
3868 // We found a property, but it's not writeable
3869 return JVMTI_ERROR_NOT_AVAILABLE;
3870 }
3871 }
3872 }
3873
3874 // We cannot find a property of the given name
3875 return JVMTI_ERROR_NOT_AVAILABLE;
3876 } /* end SetSystemProperty */