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