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