< prev index next >

src/hotspot/share/prims/jvm.cpp

Print this page

  48 #include "interpreter/bytecode.hpp"
  49 #include "interpreter/bytecodeUtils.hpp"
  50 #include "jfr/jfrEvents.hpp"
  51 #include "logging/log.hpp"
  52 #include "memory/oopFactory.hpp"
  53 #include "memory/referenceType.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/access.inline.hpp"
  57 #include "oops/constantPool.hpp"
  58 #include "oops/fieldStreams.inline.hpp"
  59 #include "oops/instanceKlass.hpp"
  60 #include "oops/klass.inline.hpp"
  61 #include "oops/method.hpp"
  62 #include "oops/recordComponent.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "prims/jvm_misc.hpp"
  67 #include "prims/jvmtiExport.hpp"
  68 #include "prims/jvmtiThreadState.hpp"
  69 #include "prims/stackwalk.hpp"
  70 #include "runtime/arguments.hpp"
  71 #include "runtime/atomic.hpp"

  72 #include "runtime/globals_extension.hpp"
  73 #include "runtime/handles.inline.hpp"
  74 #include "runtime/init.hpp"
  75 #include "runtime/interfaceSupport.inline.hpp"
  76 #include "runtime/deoptimization.hpp"
  77 #include "runtime/handshake.hpp"
  78 #include "runtime/java.hpp"
  79 #include "runtime/javaCalls.hpp"
  80 #include "runtime/jfieldIDWorkaround.hpp"
  81 #include "runtime/jniHandles.inline.hpp"
  82 #include "runtime/os.inline.hpp"
  83 #include "runtime/osThread.hpp"
  84 #include "runtime/perfData.hpp"
  85 #include "runtime/reflection.hpp"
  86 #include "runtime/synchronizer.hpp"
  87 #include "runtime/thread.inline.hpp"
  88 #include "runtime/threadSMR.hpp"
  89 #include "runtime/vframe.inline.hpp"
  90 #include "runtime/vmOperations.hpp"
  91 #include "runtime/vm_version.hpp"

 512   if (!java_lang_Throwable::get_top_method_and_bci(exc, &method, &bci)) {
 513     return NULL;
 514   }
 515   if (method->is_native()) {
 516     return NULL;
 517   }
 518 
 519   stringStream ss;
 520   bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
 521   if (ok) {
 522     oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
 523     return (jstring) JNIHandles::make_local(THREAD, result);
 524   } else {
 525     return NULL;
 526   }
 527 JVM_END
 528 
 529 // java.lang.StackTraceElement //////////////////////////////////////////////
 530 
 531 
 532 JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject throwable))
 533   Handle exception(THREAD, JNIHandles::resolve(throwable));
 534   objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
 535   objArrayHandle stack_trace(THREAD, st);
 536   // Fill in the allocated stack trace
 537   java_lang_Throwable::get_stack_trace_elements(exception, stack_trace, CHECK);
 538 JVM_END
 539 
 540 
 541 JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
 542   Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
 543   Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
 544   java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, THREAD);
 545 JVM_END
 546 
 547 
 548 // java.lang.StackWalker //////////////////////////////////////////////////////
 549 
 550 
 551 JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jlong mode,
 552                                      jint skip_frames, jint frame_count, jint start_index,
 553                                      jobjectArray frames))
 554   if (!thread->has_last_Java_frame()) {
 555     THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", NULL);
 556   }
 557 
 558   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 559 

 560   // frames array is a Class<?>[] array when only getting caller reference,
 561   // and a StackFrameInfo[] array (or derivative) otherwise. It should never
 562   // be null.
 563   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 564   objArrayHandle frames_array_h(THREAD, fa);
 565 
 566   int limit = start_index + frame_count;
 567   if (frames_array_h->length() < limit) {
 568     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", NULL);
 569   }
 570 
 571   oop result = StackWalk::walk(stackStream_h, mode, skip_frames, frame_count,
 572                                start_index, frames_array_h, CHECK_NULL);
 573   return JNIHandles::make_local(THREAD, result);
 574 JVM_END
 575 
 576 
 577 JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jlong mode, jlong anchor,
 578                                   jint frame_count, jint start_index,
 579                                   jobjectArray frames))
 580   // frames array is a Class<?>[] array when only getting caller reference,
 581   // and a StackFrameInfo[] array (or derivative) otherwise. It should never
 582   // be null.
 583   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 584   objArrayHandle frames_array_h(THREAD, fa);
 585 
 586   int limit = start_index+frame_count;
 587   if (frames_array_h->length() < limit) {
 588     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
 589   }
 590 
 591   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 592   return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, frame_count,
 593                                    start_index, frames_array_h, THREAD);









 594 JVM_END
 595 
 596 // java.lang.Object ///////////////////////////////////////////////
 597 
 598 
 599 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
 600   // as implemented in the classic virtual machine; return 0 if object is NULL
 601   return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
 602 JVM_END
 603 
 604 
 605 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
 606   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 607   JavaThreadInObjectWaitState jtiows(thread, ms != 0);
 608   if (JvmtiExport::should_post_monitor_wait()) {
 609     JvmtiExport::post_monitor_wait(thread, obj(), ms);
 610 
 611     // The current thread already owns the monitor and it has not yet
 612     // been added to the wait queue so the current thread cannot be
 613     // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT

 664     new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
 665                                                    /* do_zero */ true, CHECK_NULL);
 666   } else {
 667     new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
 668   }
 669 
 670   HeapAccess<>::clone(obj(), new_obj_oop, size);
 671 
 672   Handle new_obj(THREAD, new_obj_oop);
 673   // Caution: this involves a java upcall, so the clone should be
 674   // "gc-robust" by this stage.
 675   if (klass->has_finalizer()) {
 676     assert(obj->is_instance(), "should be instanceOop");
 677     new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
 678     new_obj = Handle(THREAD, new_obj_oop);
 679   }
 680 
 681   return JNIHandles::make_local(THREAD, new_obj());
 682 JVM_END
 683 






 684 // java.io.File ///////////////////////////////////////////////////////////////
 685 
 686 JVM_LEAF(char*, JVM_NativePath(char* path))
 687   return os::native_path(path);
 688 JVM_END
 689 
 690 
 691 // Misc. class handling ///////////////////////////////////////////////////////////
 692 
 693 
 694 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env))
 695   // Getting the class of the caller frame.
 696   //
 697   // The call stack at this point looks something like this:
 698   //
 699   // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
 700   // [1] [ @CallerSensitive API.method                                   ]
 701   // [.] [ (skipped intermediate frames)                                 ]
 702   // [n] [ caller                                                        ]
 703   vframeStream vfst(thread);

3011   JavaThread* receiver = NULL;
3012   bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
3013   java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
3014 
3015   if (is_alive) {
3016     // jthread refers to a live JavaThread.
3017     Thread::set_priority(receiver, (ThreadPriority)prio);
3018   }
3019   // Implied else: If the JavaThread hasn't started yet, then the
3020   // priority set in the java.lang.Thread object above will be pushed
3021   // down when it does start.
3022 JVM_END
3023 
3024 
3025 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
3026   if (os::dont_yield()) return;
3027   HOTSPOT_THREAD_YIELD();
3028   os::naked_yield();
3029 JVM_END
3030 
3031 static void post_thread_sleep_event(EventThreadSleep* event, jlong millis) {
3032   assert(event != NULL, "invariant");
3033   assert(event->should_commit(), "invariant");
3034   event->set_time(millis);
3035   event->commit();
3036 }
3037 
3038 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
3039   if (millis < 0) {
3040     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
3041   }
3042 
3043   if (thread->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
3044     THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3045   }
3046 
3047   // Save current thread state and restore it at the end of this block.
3048   // And set new thread state to SLEEPING.
3049   JavaThreadSleepState jtss(thread);
3050 
3051   HOTSPOT_THREAD_SLEEP_BEGIN(millis);
3052   EventThreadSleep event;
3053 
3054   if (millis == 0) {
3055     os::naked_yield();
3056   } else {
3057     ThreadState old_state = thread->osthread()->get_state();
3058     thread->osthread()->set_state(SLEEPING);
3059     if (!thread->sleep(millis)) { // interrupted
3060       // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
3061       // us while we were sleeping. We do not overwrite those.
3062       if (!HAS_PENDING_EXCEPTION) {
3063         if (event.should_commit()) {
3064           post_thread_sleep_event(&event, millis);
3065         }
3066         HOTSPOT_THREAD_SLEEP_END(1);
3067 
3068         // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
3069         // to properly restore the thread state.  That's likely wrong.
3070         THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3071       }
3072     }
3073     thread->osthread()->set_state(old_state);
3074   }
3075   if (event.should_commit()) {
3076     post_thread_sleep_event(&event, millis);
3077   }
3078   HOTSPOT_THREAD_SLEEP_END(0);
3079 JVM_END
3080 
3081 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3082   oop jthread = thread->threadObj();
3083   assert(jthread != NULL, "no current thread!");
3084   return JNIHandles::make_local(THREAD, jthread);
3085 JVM_END
3086 
3087 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
3088   ThreadsListHandle tlh(thread);
3089   JavaThread* receiver = NULL;
3090   bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
3091   if (is_alive) {
3092     // jthread refers to a live JavaThread.
3093     receiver->interrupt();
3094   }
3095 JVM_END
3096 




























3097 
3098 // Return true iff the current thread has locked the object passed in
3099 
3100 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3101   if (obj == NULL) {
3102     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3103   }
3104   Handle h_obj(THREAD, JNIHandles::resolve(obj));
3105   return ObjectSynchronizer::current_thread_holds_lock(thread, h_obj);
3106 JVM_END
3107 




3108 
3109 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
3110   VM_PrintThreads op;
3111   VMThread::execute(&op);
3112   if (JvmtiExport::should_post_data_dump()) {
3113     JvmtiExport::post_data_dump();
3114   }
3115 JVM_END
3116 
3117 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3118   // We don't use a ThreadsListHandle here because the current thread
3119   // must be alive.
3120   oop java_thread = JNIHandles::resolve_non_null(jthread);
3121   JavaThread* thr = java_lang_Thread::thread(java_thread);
3122   if (thread == thr && !thr->has_attached_via_jni()) {
3123     // Thread naming is only supported for the current thread and
3124     // we don't set the name of an attached thread to avoid stepping
3125     // on other programs.
3126     ResourceMark rm(thread);
3127     const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));

3834 
3835   InstanceKlass* ik = vmClasses::String_klass();
3836   objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL);
3837   objArrayHandle result_h(THREAD, r);
3838 
3839   int index = 0;
3840   for (int j = 0; j < num_flags; j++, index++) {
3841     Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3842     result_h->obj_at_put(index, h());
3843   }
3844   for (int i = 0; i < num_args; i++, index++) {
3845     Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3846     result_h->obj_at_put(index, h());
3847   }
3848   return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
3849 JVM_END
3850 
3851 JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name))
3852   return os::get_signal_number(name);
3853 JVM_END












































































  48 #include "interpreter/bytecode.hpp"
  49 #include "interpreter/bytecodeUtils.hpp"
  50 #include "jfr/jfrEvents.hpp"
  51 #include "logging/log.hpp"
  52 #include "memory/oopFactory.hpp"
  53 #include "memory/referenceType.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/access.inline.hpp"
  57 #include "oops/constantPool.hpp"
  58 #include "oops/fieldStreams.inline.hpp"
  59 #include "oops/instanceKlass.hpp"
  60 #include "oops/klass.inline.hpp"
  61 #include "oops/method.hpp"
  62 #include "oops/recordComponent.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "prims/jvm_misc.hpp"
  67 #include "prims/jvmtiExport.hpp"
  68 #include "prims/jvmtiThreadState.inline.hpp"
  69 #include "prims/stackwalk.hpp"
  70 #include "runtime/arguments.hpp"
  71 #include "runtime/atomic.hpp"
  72 #include "runtime/continuation.hpp"
  73 #include "runtime/globals_extension.hpp"
  74 #include "runtime/handles.inline.hpp"
  75 #include "runtime/init.hpp"
  76 #include "runtime/interfaceSupport.inline.hpp"
  77 #include "runtime/deoptimization.hpp"
  78 #include "runtime/handshake.hpp"
  79 #include "runtime/java.hpp"
  80 #include "runtime/javaCalls.hpp"
  81 #include "runtime/jfieldIDWorkaround.hpp"
  82 #include "runtime/jniHandles.inline.hpp"
  83 #include "runtime/os.inline.hpp"
  84 #include "runtime/osThread.hpp"
  85 #include "runtime/perfData.hpp"
  86 #include "runtime/reflection.hpp"
  87 #include "runtime/synchronizer.hpp"
  88 #include "runtime/thread.inline.hpp"
  89 #include "runtime/threadSMR.hpp"
  90 #include "runtime/vframe.inline.hpp"
  91 #include "runtime/vmOperations.hpp"
  92 #include "runtime/vm_version.hpp"

 513   if (!java_lang_Throwable::get_top_method_and_bci(exc, &method, &bci)) {
 514     return NULL;
 515   }
 516   if (method->is_native()) {
 517     return NULL;
 518   }
 519 
 520   stringStream ss;
 521   bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
 522   if (ok) {
 523     oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
 524     return (jstring) JNIHandles::make_local(THREAD, result);
 525   } else {
 526     return NULL;
 527   }
 528 JVM_END
 529 
 530 // java.lang.StackTraceElement //////////////////////////////////////////////
 531 
 532 
 533 JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject backtrace, jint depth))
 534   Handle backtraceh(THREAD, JNIHandles::resolve(backtrace));
 535   objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
 536   objArrayHandle stack_trace(THREAD, st);
 537   // Fill in the allocated stack trace
 538   java_lang_Throwable::get_stack_trace_elements(depth, backtraceh, stack_trace, CHECK);
 539 JVM_END
 540 
 541 
 542 JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
 543   Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
 544   Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
 545   java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, THREAD);
 546 JVM_END
 547 
 548 
 549 // java.lang.StackWalker //////////////////////////////////////////////////////
 550 
 551 
 552 JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jlong mode,
 553                                      jint skip_frames, jobject contScope, jobject cont,
 554                                      jint frame_count, jint start_index, jobjectArray frames))
 555   if (!thread->has_last_Java_frame()) {
 556     THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", NULL);
 557   }
 558 
 559   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 560   Handle contScope_h(THREAD, JNIHandles::resolve(contScope));
 561   Handle cont_h(THREAD, JNIHandles::resolve(cont));
 562   // frames array is a Class<?>[] array when only getting caller reference,
 563   // and a StackFrameInfo[] array (or derivative) otherwise. It should never
 564   // be null.
 565   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 566   objArrayHandle frames_array_h(THREAD, fa);
 567 
 568   int limit = start_index + frame_count;
 569   if (frames_array_h->length() < limit) {
 570     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", NULL);
 571   }
 572 
 573   oop result = StackWalk::walk(stackStream_h, mode, skip_frames, contScope_h, cont_h,
 574                                frame_count, start_index, frames_array_h, CHECK_NULL);
 575   return JNIHandles::make_local(THREAD, result);
 576 JVM_END
 577 
 578 
 579 JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jlong mode, jlong anchor,
 580                                   jint frame_count, jint start_index,
 581                                   jobjectArray frames))
 582   // frames array is a Class<?>[] array when only getting caller reference,
 583   // and a StackFrameInfo[] array (or derivative) otherwise. It should never
 584   // be null.
 585   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 586   objArrayHandle frames_array_h(THREAD, fa);
 587 
 588   int limit = start_index+frame_count;
 589   if (frames_array_h->length() < limit) {
 590     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
 591   }
 592 
 593   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 594   return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, frame_count,
 595                                   start_index, frames_array_h, THREAD);
 596 JVM_END
 597 
 598 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
 599     objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 600     objArrayHandle frames_array_h(THREAD, fa);
 601     Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 602     Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
 603 
 604     StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
 605 JVM_END
 606 
 607 // java.lang.Object ///////////////////////////////////////////////
 608 
 609 
 610 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
 611   // as implemented in the classic virtual machine; return 0 if object is NULL
 612   return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
 613 JVM_END
 614 
 615 
 616 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
 617   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 618   JavaThreadInObjectWaitState jtiows(thread, ms != 0);
 619   if (JvmtiExport::should_post_monitor_wait()) {
 620     JvmtiExport::post_monitor_wait(thread, obj(), ms);
 621 
 622     // The current thread already owns the monitor and it has not yet
 623     // been added to the wait queue so the current thread cannot be
 624     // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT

 675     new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
 676                                                    /* do_zero */ true, CHECK_NULL);
 677   } else {
 678     new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
 679   }
 680 
 681   HeapAccess<>::clone(obj(), new_obj_oop, size);
 682 
 683   Handle new_obj(THREAD, new_obj_oop);
 684   // Caution: this involves a java upcall, so the clone should be
 685   // "gc-robust" by this stage.
 686   if (klass->has_finalizer()) {
 687     assert(obj->is_instance(), "should be instanceOop");
 688     new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
 689     new_obj = Handle(THREAD, new_obj_oop);
 690   }
 691 
 692   return JNIHandles::make_local(THREAD, new_obj());
 693 JVM_END
 694 
 695 // jdk.internal.vm.Continuation /////////////////////////////////////////////////////
 696 
 697 JVM_ENTRY(void, JVM_RegisterContinuationMethods(JNIEnv *env, jclass cls))
 698   CONT_RegisterNativeMethods(env, cls);
 699 JVM_END
 700 
 701 // java.io.File ///////////////////////////////////////////////////////////////
 702 
 703 JVM_LEAF(char*, JVM_NativePath(char* path))
 704   return os::native_path(path);
 705 JVM_END
 706 
 707 
 708 // Misc. class handling ///////////////////////////////////////////////////////////
 709 
 710 
 711 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env))
 712   // Getting the class of the caller frame.
 713   //
 714   // The call stack at this point looks something like this:
 715   //
 716   // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
 717   // [1] [ @CallerSensitive API.method                                   ]
 718   // [.] [ (skipped intermediate frames)                                 ]
 719   // [n] [ caller                                                        ]
 720   vframeStream vfst(thread);

3028   JavaThread* receiver = NULL;
3029   bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
3030   java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
3031 
3032   if (is_alive) {
3033     // jthread refers to a live JavaThread.
3034     Thread::set_priority(receiver, (ThreadPriority)prio);
3035   }
3036   // Implied else: If the JavaThread hasn't started yet, then the
3037   // priority set in the java.lang.Thread object above will be pushed
3038   // down when it does start.
3039 JVM_END
3040 
3041 
3042 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
3043   if (os::dont_yield()) return;
3044   HOTSPOT_THREAD_YIELD();
3045   os::naked_yield();
3046 JVM_END
3047 







3048 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
3049   if (millis < 0) {
3050     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
3051   }
3052 
3053   if (thread->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
3054     THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3055   }
3056 
3057   // Save current thread state and restore it at the end of this block.
3058   // And set new thread state to SLEEPING.
3059   JavaThreadSleepState jtss(thread);
3060 
3061   HOTSPOT_THREAD_SLEEP_BEGIN(millis);

3062 
3063   if (millis == 0) {
3064     os::naked_yield();
3065   } else {
3066     ThreadState old_state = thread->osthread()->get_state();
3067     thread->osthread()->set_state(SLEEPING);
3068     if (!thread->sleep(millis)) { // interrupted
3069       // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
3070       // us while we were sleeping. We do not overwrite those.
3071       if (!HAS_PENDING_EXCEPTION) {



3072         HOTSPOT_THREAD_SLEEP_END(1);
3073 
3074         // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
3075         // to properly restore the thread state.  That's likely wrong.
3076         THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3077       }
3078     }
3079     thread->osthread()->set_state(old_state);
3080   }



3081   HOTSPOT_THREAD_SLEEP_END(0);
3082 JVM_END
3083 
3084 JVM_ENTRY(jobject, JVM_CurrentThread0(JNIEnv* env, jclass threadClass))
3085   oop jthread = thread->threadObj();
3086   assert(jthread != NULL, "no current thread!");
3087   return JNIHandles::make_local(THREAD, jthread);
3088 JVM_END
3089 
3090 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
3091   ThreadsListHandle tlh(thread);
3092   JavaThread* receiver = NULL;
3093   bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
3094   if (is_alive) {
3095     // jthread refers to a live JavaThread.
3096     receiver->interrupt();
3097   }
3098 JVM_END
3099 
3100 JVM_ENTRY(jobject, JVM_ScopeLocalCache(JNIEnv* env, jclass threadClass))
3101   oop theCache = thread->scopeLocalCache();
3102   if (theCache) {
3103     arrayOop objs = arrayOop(theCache);
3104     assert(objs->length() == ScopeLocalCacheSize * 2, "wrong length");
3105   }
3106   return JNIHandles::make_local(THREAD, theCache);
3107 JVM_END
3108 
3109 JVM_ENTRY(void, JVM_SetScopeLocalCache(JNIEnv* env, jclass threadClass,
3110                                    jobject theCache))
3111   arrayOop objs = arrayOop(JNIHandles::resolve(theCache));
3112   if (objs != NULL) {
3113     assert(objs->length() == ScopeLocalCacheSize * 2, "wrong length");
3114   }
3115   thread->set_scopeLocalCache(objs);
3116 JVM_END
3117 
3118 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3119   oop theThread = thread->vthread();
3120   assert (theThread != (oop)NULL, "no current thread!");
3121   return JNIHandles::make_local(THREAD, theThread);
3122 JVM_END
3123 
3124 JVM_ENTRY(void, JVM_SetCurrentThread(JNIEnv* env, jclass threadClass,
3125                                      jobject theThread))
3126   thread->set_vthread(JNIHandles::resolve(theThread));
3127 JVM_END
3128 
3129 // Return true iff the current thread has locked the object passed in
3130 
3131 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3132   if (obj == NULL) {
3133     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3134   }
3135   Handle h_obj(THREAD, JNIHandles::resolve(obj));
3136   return ObjectSynchronizer::current_thread_holds_lock(thread, h_obj);
3137 JVM_END
3138 
3139 JVM_ENTRY(jobject, JVM_GetStackTrace(JNIEnv *env, jobject jthread))
3140   oop trace = java_lang_Thread::async_get_stack_trace(JNIHandles::resolve(jthread), THREAD);
3141   return JNIHandles::make_local(THREAD, trace);
3142 JVM_END
3143 
3144 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
3145   VM_PrintThreads op;
3146   VMThread::execute(&op);
3147   if (JvmtiExport::should_post_data_dump()) {
3148     JvmtiExport::post_data_dump();
3149   }
3150 JVM_END
3151 
3152 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3153   // We don't use a ThreadsListHandle here because the current thread
3154   // must be alive.
3155   oop java_thread = JNIHandles::resolve_non_null(jthread);
3156   JavaThread* thr = java_lang_Thread::thread(java_thread);
3157   if (thread == thr && !thr->has_attached_via_jni()) {
3158     // Thread naming is only supported for the current thread and
3159     // we don't set the name of an attached thread to avoid stepping
3160     // on other programs.
3161     ResourceMark rm(thread);
3162     const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));

3869 
3870   InstanceKlass* ik = vmClasses::String_klass();
3871   objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL);
3872   objArrayHandle result_h(THREAD, r);
3873 
3874   int index = 0;
3875   for (int j = 0; j < num_flags; j++, index++) {
3876     Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3877     result_h->obj_at_put(index, h());
3878   }
3879   for (int i = 0; i < num_args; i++, index++) {
3880     Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3881     result_h->obj_at_put(index, h());
3882   }
3883   return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
3884 JVM_END
3885 
3886 JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name))
3887   return os::get_signal_number(name);
3888 JVM_END
3889 
3890 JVM_ENTRY(void, JVM_VirtualThreadMountBegin(JNIEnv* env, jobject vthread, jboolean first_mount))
3891 #if INCLUDE_JVMTI
3892   JvmtiVTMTDisabler::start_VTMT(vthread, 0);
3893 #else
3894   fatal("Should only be called with JVMTI enabled");
3895 #endif
3896 JVM_END
3897 
3898 JVM_ENTRY(void, JVM_VirtualThreadMountEnd(JNIEnv* env, jobject vthread, jboolean first_mount))
3899 #if INCLUDE_JVMTI
3900   oop vt = JNIHandles::resolve(vthread);
3901 
3902   thread->rebind_to_jvmti_thread_state_of(vt);
3903 
3904   assert(thread->is_in_VTMT(), "VTMT sanity check");
3905   JvmtiVTMTDisabler::finish_VTMT(vthread, 0);
3906 
3907   if (first_mount) {
3908     // thread start
3909     if (JvmtiExport::can_support_virtual_threads()) {
3910       if (JvmtiExport::should_post_vthread_start()) {
3911         JvmtiExport::post_vthread_start(vthread);
3912       }
3913     } else { // compatibility for vthread unaware agents: legacy thread_start
3914       if (JvmtiExport::should_post_thread_life()) {
3915         JvmtiExport::post_thread_start(thread);
3916       }
3917     }
3918   }
3919   if (JvmtiExport::should_post_vthread_mount()) {
3920     JvmtiExport::post_vthread_mount(vthread);
3921   }
3922 #else
3923   fatal("Should only be called with JVMTI enabled");
3924 #endif
3925 JVM_END
3926 
3927 JVM_ENTRY(void, JVM_VirtualThreadUnmountBegin(JNIEnv* env, jobject vthread, jboolean last_unmount))
3928 #if INCLUDE_JVMTI
3929   HandleMark hm(thread);
3930   Handle ct(thread, thread->threadObj());
3931 
3932   if (JvmtiExport::should_post_vthread_unmount()) {
3933     JvmtiExport::post_vthread_unmount(vthread);
3934   }
3935   if (last_unmount) {
3936     if (JvmtiExport::can_support_virtual_threads()) {
3937       if (JvmtiExport::should_post_vthread_end()) {
3938         JvmtiExport::post_vthread_end(vthread);
3939       }
3940     } else { // compatibility for vthread unaware agents: legacy thread_end
3941       if (JvmtiExport::should_post_thread_life()) {
3942         JvmtiExport::post_thread_end(thread);
3943       }
3944     }
3945     thread->set_mounted_vthread(NULL);
3946   }
3947 
3948   assert(!thread->is_in_VTMT(), "VTMT sanity check");
3949   JvmtiVTMTDisabler::start_VTMT(vthread, 1);
3950   thread->rebind_to_jvmti_thread_state_of(ct());
3951 #else
3952   fatal("Should only be called with JVMTI enabled");
3953 #endif
3954 JVM_END
3955 
3956 JVM_ENTRY(void, JVM_VirtualThreadUnmountEnd(JNIEnv* env, jobject vthread, jboolean last_unmount))
3957 #if INCLUDE_JVMTI
3958   assert(thread->is_in_VTMT(), "VTMT sanity check");
3959   JvmtiVTMTDisabler::finish_VTMT(vthread, 1);
3960 #else
3961   fatal("Should only be called with JVMTI enabled");
3962 #endif
3963 JVM_END
< prev index next >