49 #include "oops/oop.inline.hpp"
50 #include "prims/jvm_misc.hpp"
51 #include "prims/jvmtiExport.hpp"
52 #include "prims/jvmtiThreadState.hpp"
53 #include "prims/nativeLookup.hpp"
54 #include "prims/privilegedStack.hpp"
55 #include "prims/stackwalk.hpp"
56 #include "runtime/arguments.hpp"
57 #include "runtime/atomic.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/init.hpp"
60 #include "runtime/interfaceSupport.hpp"
61 #include "runtime/java.hpp"
62 #include "runtime/javaCalls.hpp"
63 #include "runtime/jfieldIDWorkaround.hpp"
64 #include "runtime/orderAccess.inline.hpp"
65 #include "runtime/os.inline.hpp"
66 #include "runtime/perfData.hpp"
67 #include "runtime/reflection.hpp"
68 #include "runtime/thread.inline.hpp"
69 #include "runtime/vframe.hpp"
70 #include "runtime/vm_operations.hpp"
71 #include "runtime/vm_version.hpp"
72 #include "services/attachListener.hpp"
73 #include "services/management.hpp"
74 #include "services/threadService.hpp"
75 #include "trace/tracing.hpp"
76 #include "utilities/copy.hpp"
77 #include "utilities/defaultStream.hpp"
78 #include "utilities/dtrace.hpp"
79 #include "utilities/events.hpp"
80 #include "utilities/histogram.hpp"
81 #include "utilities/macros.hpp"
82 #include "utilities/utf8.hpp"
83 #if INCLUDE_CDS
84 #include "classfile/sharedClassUtil.hpp"
85 #include "classfile/systemDictionaryShared.hpp"
86 #endif
87
88 #include <errno.h>
2737 va_end(args);
2738 return len;
2739 }
2740
2741
2742 // HotSpot specific jio method
2743 void jio_print(const char* s) {
2744 // Try to make this function as atomic as possible.
2745 if (Arguments::vfprintf_hook() != NULL) {
2746 jio_fprintf(defaultStream::output_stream(), "%s", s);
2747 } else {
2748 // Make an unused local variable to avoid warning from gcc 4.x compiler.
2749 size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s));
2750 }
2751 }
2752
2753 } // Extern C
2754
2755 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2756
2757 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
2758 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
2759 // OSThread objects. The exception to this rule is when the target object is the thread
2760 // doing the operation, in which case we know that the thread won't exit until the
2761 // operation is done (all exits being voluntary). There are a few cases where it is
2762 // rather silly to do operations on yourself, like resuming yourself or asking whether
2763 // you are alive. While these can still happen, they are not subject to deadlocks if
2764 // the lock is held while the operation occurs (this is not the case for suspend, for
2765 // instance), and are very unlikely. Because IsAlive needs to be fast and its
2766 // implementation is local to this file, we always lock Threads_lock for that one.
2767
2768 static void thread_entry(JavaThread* thread, TRAPS) {
2769 HandleMark hm(THREAD);
2770 Handle obj(THREAD, thread->threadObj());
2771 JavaValue result(T_VOID);
2772 JavaCalls::call_virtual(&result,
2773 obj,
2774 SystemDictionary::Thread_klass(),
2775 vmSymbols::run_method_name(),
2776 vmSymbols::void_method_signature(),
2777 THREAD);
2778 }
2779
2780
2781 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2782 JVMWrapper("JVM_StartThread");
2783 JavaThread *native_thread = NULL;
2784
2785 // We cannot hold the Threads_lock when we throw an exception,
2786 // due to rank ordering issues. Example: we might need to grab the
2821 // JavaThread due to lack of memory. Check for this situation and throw
2822 // an exception if necessary. Eventually we may want to change this so
2823 // that we only grab the lock if the thread was created successfully -
2824 // then we can also do this check and throw the exception in the
2825 // JavaThread constructor.
2826 if (native_thread->osthread() != NULL) {
2827 // Note: the current thread is not being used within "prepare".
2828 native_thread->prepare(jthread);
2829 }
2830 }
2831 }
2832
2833 if (throw_illegal_thread_state) {
2834 THROW(vmSymbols::java_lang_IllegalThreadStateException());
2835 }
2836
2837 assert(native_thread != NULL, "Starting null thread?");
2838
2839 if (native_thread->osthread() == NULL) {
2840 // No one should hold a reference to the 'native_thread'.
2841 delete native_thread;
2842 if (JvmtiExport::should_post_resource_exhausted()) {
2843 JvmtiExport::post_resource_exhausted(
2844 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
2845 os::native_thread_creation_failed_msg());
2846 }
2847 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
2848 os::native_thread_creation_failed_msg());
2849 }
2850
2851 Thread::start(native_thread);
2852
2853 JVM_END
2854
2855 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
2856 // before the quasi-asynchronous exception is delivered. This is a little obtrusive,
2857 // but is thought to be reliable and simple. In the case, where the receiver is the
2858 // same thread as the sender, no safepoint is needed.
2859 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
2860 JVMWrapper("JVM_StopThread");
2861
2862 oop java_throwable = JNIHandles::resolve(throwable);
2863 if (java_throwable == NULL) {
2864 THROW(vmSymbols::java_lang_NullPointerException());
2865 }
2866 oop java_thread = JNIHandles::resolve_non_null(jthread);
2867 JavaThread* receiver = java_lang_Thread::thread(java_thread);
2868 Events::log_exception(JavaThread::current(),
2869 "JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]",
2870 p2i(receiver), p2i((address)java_thread), p2i(throwable));
2871 // First check if thread is alive
2872 if (receiver != NULL) {
2873 // Check if exception is getting thrown at self (use oop equality, since the
2874 // target object might exit)
2875 if (java_thread == thread->threadObj()) {
2876 THROW_OOP(java_throwable);
2877 } else {
2878 // Enques a VM_Operation to stop all threads and then deliver the exception...
2879 Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable));
2880 }
2881 }
2882 else {
2883 // Either:
2884 // - target thread has not been started before being stopped, or
2885 // - target thread already terminated
2886 // We could read the threadStatus to determine which case it is
2887 // but that is overkill as it doesn't matter. We must set the
2888 // stillborn flag for the first case, and if the thread has already
2889 // exited setting this flag has no affect
2890 java_lang_Thread::set_stillborn(java_thread);
2891 }
2892 JVM_END
2893
2894
2895 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
2896 JVMWrapper("JVM_IsThreadAlive");
2897
2898 oop thread_oop = JNIHandles::resolve_non_null(jthread);
2899 return java_lang_Thread::is_alive(thread_oop);
2900 JVM_END
2901
2902
2903 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
2904 JVMWrapper("JVM_SuspendThread");
2905 oop java_thread = JNIHandles::resolve_non_null(jthread);
2906 JavaThread* receiver = java_lang_Thread::thread(java_thread);
2907
2908 if (receiver != NULL) {
2909 // thread has run and has not exited (still on threads list)
2910
2911 {
2912 MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag);
2913 if (receiver->is_external_suspend()) {
2914 // Don't allow nested external suspend requests. We can't return
2915 // an error from this interface so just ignore the problem.
2916 return;
2917 }
2918 if (receiver->is_exiting()) { // thread is in the process of exiting
2919 return;
2920 }
2921 receiver->set_external_suspend();
2922 }
2923
2924 // java_suspend() will catch threads in the process of exiting
2925 // and will ignore them.
2926 receiver->java_suspend();
2927
2928 // It would be nice to have the following assertion in all the
2929 // time, but it is possible for a racing resume request to have
2930 // resumed this thread right after we suspended it. Temporarily
2931 // enable this assertion if you are chasing a different kind of
2932 // bug.
2933 //
2934 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL ||
2935 // receiver->is_being_ext_suspended(), "thread is not suspended");
2936 }
2937 JVM_END
2938
2939
2940 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
2941 JVMWrapper("JVM_ResumeThread");
2942 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate.
2943 // We need to *always* get the threads lock here, since this operation cannot be allowed during
2944 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other
2945 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code
2946 // looks at it.
2947 MutexLocker ml(Threads_lock);
2948 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2949 if (thr != NULL) {
2950 // the thread has run and is not in the process of exiting
2951 thr->java_resume();
2952 }
2953 JVM_END
2954
2955
2956 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
2957 JVMWrapper("JVM_SetThreadPriority");
2958 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2959 MutexLocker ml(Threads_lock);
2960 oop java_thread = JNIHandles::resolve_non_null(jthread);
2961 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
2962 JavaThread* thr = java_lang_Thread::thread(java_thread);
2963 if (thr != NULL) { // Thread not yet started; priority pushed down when it is
2964 Thread::set_priority(thr, (ThreadPriority)prio);
2965 }
2966 JVM_END
2967
2968
2969 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
2970 JVMWrapper("JVM_Yield");
2971 if (os::dont_yield()) return;
2972 HOTSPOT_THREAD_YIELD();
2973 os::naked_yield();
2974 JVM_END
2975
2976
2977 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
2978 JVMWrapper("JVM_Sleep");
2979
2980 if (millis < 0) {
2981 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
2982 }
2983
2984 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
2985 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3016 thread->osthread()->set_state(old_state);
3017 }
3018 if (event.should_commit()) {
3019 event.set_time(millis);
3020 event.commit();
3021 }
3022 HOTSPOT_THREAD_SLEEP_END(0);
3023 JVM_END
3024
3025 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3026 JVMWrapper("JVM_CurrentThread");
3027 oop jthread = thread->threadObj();
3028 assert (thread != NULL, "no current thread!");
3029 return JNIHandles::make_local(env, jthread);
3030 JVM_END
3031
3032
3033 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread))
3034 JVMWrapper("JVM_CountStackFrames");
3035
3036 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
3037 oop java_thread = JNIHandles::resolve_non_null(jthread);
3038 bool throw_illegal_thread_state = false;
3039 int count = 0;
3040
3041 {
3042 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
3043 // We need to re-resolve the java_thread, since a GC might have happened during the
3044 // acquire of the lock
3045 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
3046
3047 if (thr == NULL) {
3048 // do nothing
3049 } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) {
3050 // Check whether this java thread has been suspended already. If not, throws
3051 // IllegalThreadStateException. We defer to throw that exception until
3052 // Threads_lock is released since loading exception class has to leave VM.
3053 // The correct way to test a thread is actually suspended is
3054 // wait_for_ext_suspend_completion(), but we can't call that while holding
3055 // the Threads_lock. The above tests are sufficient for our purposes
3056 // provided the walkability of the stack is stable - which it isn't
3057 // 100% but close enough for most practical purposes.
3058 throw_illegal_thread_state = true;
3059 } else {
3060 // Count all java activation, i.e., number of vframes
3061 for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) {
3062 // Native frames are not counted
3063 if (!vfst.method()->is_native()) count++;
3064 }
3065 }
3066 }
3067
3068 if (throw_illegal_thread_state) {
3069 THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(),
3070 "this thread is not suspended");
3071 }
3072 return count;
3073 JVM_END
3074
3075 // Consider: A better way to implement JVM_Interrupt() is to acquire
3076 // Threads_lock to resolve the jthread into a Thread pointer, fetch
3077 // Thread->platformevent, Thread->native_thr, Thread->parker, etc.,
3078 // drop Threads_lock, and the perform the unpark() and thr_kill() operations
3079 // outside the critical section. Threads_lock is hot so we want to minimize
3080 // the hold-time. A cleaner interface would be to decompose interrupt into
3081 // two steps. The 1st phase, performed under Threads_lock, would return
3082 // a closure that'd be invoked after Threads_lock was dropped.
3083 // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and
3084 // admit spurious wakeups.
3085
3086 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
3087 JVMWrapper("JVM_Interrupt");
3088
3089 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
3090 oop java_thread = JNIHandles::resolve_non_null(jthread);
3091 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
3092 // We need to re-resolve the java_thread, since a GC might have happened during the
3093 // acquire of the lock
3094 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
3095 if (thr != NULL) {
3096 Thread::interrupt(thr);
3097 }
3098 JVM_END
3099
3100
3101 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))
3102 JVMWrapper("JVM_IsInterrupted");
3103
3104 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
3105 oop java_thread = JNIHandles::resolve_non_null(jthread);
3106 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
3107 // We need to re-resolve the java_thread, since a GC might have happened during the
3108 // acquire of the lock
3109 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
3110 if (thr == NULL) {
3111 return JNI_FALSE;
3112 } else {
3113 return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0);
3114 }
3115 JVM_END
3116
3117
3118 // Return true iff the current thread has locked the object passed in
3119
3120 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3121 JVMWrapper("JVM_HoldsLock");
3122 assert(THREAD->is_Java_thread(), "sanity check");
3123 if (obj == NULL) {
3124 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3125 }
3126 Handle h_obj(THREAD, JNIHandles::resolve(obj));
3127 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj);
3128 JVM_END
3129
3130
3131 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
3132 JVMWrapper("JVM_DumpAllStacks");
3133 VM_PrintThreads op;
3134 VMThread::execute(&op);
3135 if (JvmtiExport::should_post_data_dump()) {
3136 JvmtiExport::post_data_dump();
3137 }
3138 JVM_END
3139
3140 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3141 JVMWrapper("JVM_SetNativeThreadName");
3142 ResourceMark rm(THREAD);
3143 oop java_thread = JNIHandles::resolve_non_null(jthread);
3144 JavaThread* thr = java_lang_Thread::thread(java_thread);
3145 // Thread naming only supported for the current thread, doesn't work for
3146 // target threads.
3147 if (Thread::current() == thr && !thr->has_attached_via_jni()) {
3148 // we don't set the name of an attached thread to avoid stepping
3149 // on other programs
3150 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3151 os::set_native_thread_name(thread_name);
3152 }
3153 JVM_END
3154
3155 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
3156
3157 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) {
3158 assert(jthread->is_Java_thread(), "must be a Java thread");
3159 if (jthread->privileged_stack_top() == NULL) return false;
3160 if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) {
3161 oop loader = jthread->privileged_stack_top()->class_loader();
3162 if (loader == NULL) return true;
3163 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
3164 if (trusted) return true;
3165 }
3166 return false;
3167 }
3168
3169 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env))
3671 // check if threads is non-empty array
3672 if (num_threads == 0) {
3673 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
3674 }
3675
3676 // check if threads is not an array of objects of Thread class
3677 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3678 if (k != SystemDictionary::Thread_klass()) {
3679 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
3680 }
3681
3682 ResourceMark rm(THREAD);
3683
3684 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3685 for (int i = 0; i < num_threads; i++) {
3686 oop thread_obj = ah->obj_at(i);
3687 instanceHandle h(THREAD, (instanceOop) thread_obj);
3688 thread_handle_array->append(h);
3689 }
3690
3691 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3692 return (jobjectArray)JNIHandles::make_local(env, stacktraces());
3693
3694 JVM_END
3695
3696 // JVM monitoring and management support
3697 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version))
3698 return Management::get_jmm_interface(version);
3699 JVM_END
3700
3701 // com.sun.tools.attach.VirtualMachine agent properties support
3702 //
3703 // Initialize the agent properties with the properties maintained in the VM
3704 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
3705 JVMWrapper("JVM_InitAgentProperties");
3706 ResourceMark rm;
3707
3708 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
3709
3710 PUTPROP(props, "sun.java.command", Arguments::java_command());
|
49 #include "oops/oop.inline.hpp"
50 #include "prims/jvm_misc.hpp"
51 #include "prims/jvmtiExport.hpp"
52 #include "prims/jvmtiThreadState.hpp"
53 #include "prims/nativeLookup.hpp"
54 #include "prims/privilegedStack.hpp"
55 #include "prims/stackwalk.hpp"
56 #include "runtime/arguments.hpp"
57 #include "runtime/atomic.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/init.hpp"
60 #include "runtime/interfaceSupport.hpp"
61 #include "runtime/java.hpp"
62 #include "runtime/javaCalls.hpp"
63 #include "runtime/jfieldIDWorkaround.hpp"
64 #include "runtime/orderAccess.inline.hpp"
65 #include "runtime/os.inline.hpp"
66 #include "runtime/perfData.hpp"
67 #include "runtime/reflection.hpp"
68 #include "runtime/thread.inline.hpp"
69 #include "runtime/threadSMR.hpp"
70 #include "runtime/vframe.hpp"
71 #include "runtime/vm_operations.hpp"
72 #include "runtime/vm_version.hpp"
73 #include "services/attachListener.hpp"
74 #include "services/management.hpp"
75 #include "services/threadService.hpp"
76 #include "trace/tracing.hpp"
77 #include "utilities/copy.hpp"
78 #include "utilities/defaultStream.hpp"
79 #include "utilities/dtrace.hpp"
80 #include "utilities/events.hpp"
81 #include "utilities/histogram.hpp"
82 #include "utilities/macros.hpp"
83 #include "utilities/utf8.hpp"
84 #if INCLUDE_CDS
85 #include "classfile/sharedClassUtil.hpp"
86 #include "classfile/systemDictionaryShared.hpp"
87 #endif
88
89 #include <errno.h>
2738 va_end(args);
2739 return len;
2740 }
2741
2742
2743 // HotSpot specific jio method
2744 void jio_print(const char* s) {
2745 // Try to make this function as atomic as possible.
2746 if (Arguments::vfprintf_hook() != NULL) {
2747 jio_fprintf(defaultStream::output_stream(), "%s", s);
2748 } else {
2749 // Make an unused local variable to avoid warning from gcc 4.x compiler.
2750 size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s));
2751 }
2752 }
2753
2754 } // Extern C
2755
2756 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2757
2758 // In most of the JVM thread support functions we need to access the
2759 // thread through a ThreadsListHandle to prevent it from exiting and
2760 // being reclaimed while we try to operate on it. The exceptions to this
2761 // rule are when operating on the current thread, or if the monitor of
2762 // the target java.lang.Thread is locked at the Java level - in both
2763 // cases the target cannot exit.
2764
2765 static void thread_entry(JavaThread* thread, TRAPS) {
2766 HandleMark hm(THREAD);
2767 Handle obj(THREAD, thread->threadObj());
2768 JavaValue result(T_VOID);
2769 JavaCalls::call_virtual(&result,
2770 obj,
2771 SystemDictionary::Thread_klass(),
2772 vmSymbols::run_method_name(),
2773 vmSymbols::void_method_signature(),
2774 THREAD);
2775 }
2776
2777
2778 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2779 JVMWrapper("JVM_StartThread");
2780 JavaThread *native_thread = NULL;
2781
2782 // We cannot hold the Threads_lock when we throw an exception,
2783 // due to rank ordering issues. Example: we might need to grab the
2818 // JavaThread due to lack of memory. Check for this situation and throw
2819 // an exception if necessary. Eventually we may want to change this so
2820 // that we only grab the lock if the thread was created successfully -
2821 // then we can also do this check and throw the exception in the
2822 // JavaThread constructor.
2823 if (native_thread->osthread() != NULL) {
2824 // Note: the current thread is not being used within "prepare".
2825 native_thread->prepare(jthread);
2826 }
2827 }
2828 }
2829
2830 if (throw_illegal_thread_state) {
2831 THROW(vmSymbols::java_lang_IllegalThreadStateException());
2832 }
2833
2834 assert(native_thread != NULL, "Starting null thread?");
2835
2836 if (native_thread->osthread() == NULL) {
2837 // No one should hold a reference to the 'native_thread'.
2838 native_thread->smr_delete();
2839 if (JvmtiExport::should_post_resource_exhausted()) {
2840 JvmtiExport::post_resource_exhausted(
2841 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
2842 os::native_thread_creation_failed_msg());
2843 }
2844 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
2845 os::native_thread_creation_failed_msg());
2846 }
2847
2848 Thread::start(native_thread);
2849
2850 JVM_END
2851
2852
2853 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
2854 // before the quasi-asynchronous exception is delivered. This is a little obtrusive,
2855 // but is thought to be reliable and simple. In the case, where the receiver is the
2856 // same thread as the sender, no VM_Operation is needed.
2857 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
2858 JVMWrapper("JVM_StopThread");
2859
2860 // A nested ThreadsListHandle will grab the Threads_lock so create
2861 // tlh before we resolve throwable.
2862 ThreadsListHandle tlh(thread);
2863 oop java_throwable = JNIHandles::resolve(throwable);
2864 if (java_throwable == NULL) {
2865 THROW(vmSymbols::java_lang_NullPointerException());
2866 }
2867 oop java_thread = NULL;
2868 JavaThread* receiver = NULL;
2869 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
2870 Events::log_exception(thread,
2871 "JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]",
2872 p2i(receiver), p2i((address)java_thread), p2i(throwable));
2873
2874 if (is_alive) {
2875 // jthread refers to a live JavaThread.
2876 if (thread == receiver) {
2877 // Exception is getting thrown at self so no VM_Operation needed.
2878 THROW_OOP(java_throwable);
2879 } else {
2880 // Use a VM_Operation to throw the exception.
2881 Thread::send_async_exception(java_thread, java_throwable);
2882 }
2883 } else {
2884 // Either:
2885 // - target thread has not been started before being stopped, or
2886 // - target thread already terminated
2887 // We could read the threadStatus to determine which case it is
2888 // but that is overkill as it doesn't matter. We must set the
2889 // stillborn flag for the first case, and if the thread has already
2890 // exited setting this flag has no effect.
2891 java_lang_Thread::set_stillborn(java_thread);
2892 }
2893 JVM_END
2894
2895
2896 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
2897 JVMWrapper("JVM_IsThreadAlive");
2898
2899 oop thread_oop = JNIHandles::resolve_non_null(jthread);
2900 return java_lang_Thread::is_alive(thread_oop);
2901 JVM_END
2902
2903
2904 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
2905 JVMWrapper("JVM_SuspendThread");
2906
2907 ThreadsListHandle tlh(thread);
2908 JavaThread* receiver = NULL;
2909 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
2910 if (is_alive) {
2911 // jthread refers to a live JavaThread.
2912 {
2913 MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag);
2914 if (receiver->is_external_suspend()) {
2915 // Don't allow nested external suspend requests. We can't return
2916 // an error from this interface so just ignore the problem.
2917 return;
2918 }
2919 if (receiver->is_exiting()) { // thread is in the process of exiting
2920 return;
2921 }
2922 receiver->set_external_suspend();
2923 }
2924
2925 // java_suspend() will catch threads in the process of exiting
2926 // and will ignore them.
2927 receiver->java_suspend();
2928
2929 // It would be nice to have the following assertion in all the
2930 // time, but it is possible for a racing resume request to have
2931 // resumed this thread right after we suspended it. Temporarily
2932 // enable this assertion if you are chasing a different kind of
2933 // bug.
2934 //
2935 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL ||
2936 // receiver->is_being_ext_suspended(), "thread is not suspended");
2937 }
2938 JVM_END
2939
2940
2941 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
2942 JVMWrapper("JVM_ResumeThread");
2943
2944 ThreadsListHandle tlh(thread);
2945 JavaThread* receiver = NULL;
2946 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
2947 if (is_alive) {
2948 // jthread refers to a live JavaThread.
2949
2950 // This is the original comment for this Threads_lock grab:
2951 // We need to *always* get the threads lock here, since this operation cannot be allowed during
2952 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other
2953 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code
2954 // looks at it.
2955 //
2956 // The above comment dates back to when we had both internal and
2957 // external suspend APIs that shared a common underlying mechanism.
2958 // External suspend is now entirely cooperative and doesn't share
2959 // anything with internal suspend. That said, there are some
2960 // assumptions in the VM that an external resume grabs the
2961 // Threads_lock. We can't drop the Threads_lock grab here until we
2962 // resolve the assumptions that exist elsewhere.
2963 //
2964 MutexLocker ml(Threads_lock);
2965 receiver->java_resume();
2966 }
2967 JVM_END
2968
2969
2970 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
2971 JVMWrapper("JVM_SetThreadPriority");
2972
2973 ThreadsListHandle tlh(thread);
2974 oop java_thread = NULL;
2975 JavaThread* receiver = NULL;
2976 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
2977 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
2978
2979 if (is_alive) {
2980 // jthread refers to a live JavaThread.
2981 Thread::set_priority(receiver, (ThreadPriority)prio);
2982 }
2983 // Implied else: If the JavaThread hasn't started yet, then the
2984 // priority set in the java.lang.Thread object above will be pushed
2985 // down when it does start.
2986 JVM_END
2987
2988
2989 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
2990 JVMWrapper("JVM_Yield");
2991 if (os::dont_yield()) return;
2992 HOTSPOT_THREAD_YIELD();
2993 os::naked_yield();
2994 JVM_END
2995
2996
2997 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
2998 JVMWrapper("JVM_Sleep");
2999
3000 if (millis < 0) {
3001 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
3002 }
3003
3004 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
3005 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3036 thread->osthread()->set_state(old_state);
3037 }
3038 if (event.should_commit()) {
3039 event.set_time(millis);
3040 event.commit();
3041 }
3042 HOTSPOT_THREAD_SLEEP_END(0);
3043 JVM_END
3044
3045 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3046 JVMWrapper("JVM_CurrentThread");
3047 oop jthread = thread->threadObj();
3048 assert (thread != NULL, "no current thread!");
3049 return JNIHandles::make_local(env, jthread);
3050 JVM_END
3051
3052
3053 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread))
3054 JVMWrapper("JVM_CountStackFrames");
3055
3056 uint32_t debug_bits = 0;
3057 ThreadsListHandle tlh(thread);
3058 JavaThread* receiver = NULL;
3059 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
3060 int count = 0;
3061 if (is_alive) {
3062 // jthread refers to a live JavaThread.
3063 if (receiver->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
3064 // Count all java activation, i.e., number of vframes.
3065 for (vframeStream vfst(receiver); !vfst.at_end(); vfst.next()) {
3066 // Native frames are not counted.
3067 if (!vfst.method()->is_native()) count++;
3068 }
3069 } else {
3070 THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(),
3071 "this thread is not suspended");
3072 }
3073 }
3074 // Implied else: if JavaThread is not alive simply return a count of 0.
3075
3076 return count;
3077 JVM_END
3078
3079
3080 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
3081 JVMWrapper("JVM_Interrupt");
3082
3083 ThreadsListHandle tlh(thread);
3084 JavaThread* receiver = NULL;
3085 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
3086 if (is_alive) {
3087 // jthread refers to a live JavaThread.
3088 Thread::interrupt(receiver);
3089 }
3090 JVM_END
3091
3092
3093 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))
3094 JVMWrapper("JVM_IsInterrupted");
3095
3096 ThreadsListHandle tlh(thread);
3097 JavaThread* receiver = NULL;
3098 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
3099 if (is_alive) {
3100 // jthread refers to a live JavaThread.
3101 return (jboolean) Thread::is_interrupted(receiver, clear_interrupted != 0);
3102 } else {
3103 return JNI_FALSE;
3104 }
3105 JVM_END
3106
3107
3108 // Return true iff the current thread has locked the object passed in
3109
3110 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3111 JVMWrapper("JVM_HoldsLock");
3112 assert(THREAD->is_Java_thread(), "sanity check");
3113 if (obj == NULL) {
3114 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3115 }
3116 Handle h_obj(THREAD, JNIHandles::resolve(obj));
3117 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj);
3118 JVM_END
3119
3120
3121 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
3122 JVMWrapper("JVM_DumpAllStacks");
3123 VM_PrintThreads op;
3124 VMThread::execute(&op);
3125 if (JvmtiExport::should_post_data_dump()) {
3126 JvmtiExport::post_data_dump();
3127 }
3128 JVM_END
3129
3130 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3131 JVMWrapper("JVM_SetNativeThreadName");
3132
3133 // We don't use a ThreadsListHandle here because the current thread
3134 // must be alive.
3135 oop java_thread = JNIHandles::resolve_non_null(jthread);
3136 JavaThread* thr = java_lang_Thread::thread(java_thread);
3137 if (thread == thr && !thr->has_attached_via_jni()) {
3138 // Thread naming is only supported for the current thread and
3139 // we don't set the name of an attached thread to avoid stepping
3140 // on other programs.
3141 ResourceMark rm(thread);
3142 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3143 os::set_native_thread_name(thread_name);
3144 }
3145 JVM_END
3146
3147 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
3148
3149 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) {
3150 assert(jthread->is_Java_thread(), "must be a Java thread");
3151 if (jthread->privileged_stack_top() == NULL) return false;
3152 if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) {
3153 oop loader = jthread->privileged_stack_top()->class_loader();
3154 if (loader == NULL) return true;
3155 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
3156 if (trusted) return true;
3157 }
3158 return false;
3159 }
3160
3161 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env))
3663 // check if threads is non-empty array
3664 if (num_threads == 0) {
3665 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
3666 }
3667
3668 // check if threads is not an array of objects of Thread class
3669 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3670 if (k != SystemDictionary::Thread_klass()) {
3671 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
3672 }
3673
3674 ResourceMark rm(THREAD);
3675
3676 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3677 for (int i = 0; i < num_threads; i++) {
3678 oop thread_obj = ah->obj_at(i);
3679 instanceHandle h(THREAD, (instanceOop) thread_obj);
3680 thread_handle_array->append(h);
3681 }
3682
3683 // The JavaThread references in thread_handle_array are validated
3684 // in VM_ThreadDump::doit().
3685 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3686 return (jobjectArray)JNIHandles::make_local(env, stacktraces());
3687
3688 JVM_END
3689
3690 // JVM monitoring and management support
3691 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version))
3692 return Management::get_jmm_interface(version);
3693 JVM_END
3694
3695 // com.sun.tools.attach.VirtualMachine agent properties support
3696 //
3697 // Initialize the agent properties with the properties maintained in the VM
3698 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
3699 JVMWrapper("JVM_InitAgentProperties");
3700 ResourceMark rm;
3701
3702 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
3703
3704 PUTPROP(props, "sun.java.command", Arguments::java_command());
|