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