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