59 #include "prims/jvm_misc.hpp"
60 #include "prims/jvmtiExport.hpp"
61 #include "prims/jvmtiThreadState.hpp"
62 #include "runtime/compilationPolicy.hpp"
63 #include "runtime/fieldDescriptor.hpp"
64 #include "runtime/fprofiler.hpp"
65 #include "runtime/handles.inline.hpp"
66 #include "runtime/interfaceSupport.hpp"
67 #include "runtime/java.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/jfieldIDWorkaround.hpp"
70 #include "runtime/orderAccess.inline.hpp"
71 #include "runtime/reflection.hpp"
72 #include "runtime/sharedRuntime.hpp"
73 #include "runtime/signature.hpp"
74 #include "runtime/thread.inline.hpp"
75 #include "runtime/vm_operations.hpp"
76 #include "services/memTracker.hpp"
77 #include "services/runtimeService.hpp"
78 #include "trace/tracing.hpp"
79 #include "utilities/defaultStream.hpp"
80 #include "utilities/dtrace.hpp"
81 #include "utilities/events.hpp"
82 #include "utilities/histogram.hpp"
83 #ifdef TARGET_OS_FAMILY_linux
84 # include "os_linux.inline.hpp"
85 #endif
86 #ifdef TARGET_OS_FAMILY_solaris
87 # include "os_solaris.inline.hpp"
88 #endif
89 #ifdef TARGET_OS_FAMILY_windows
90 # include "os_windows.inline.hpp"
91 #endif
92 #ifdef TARGET_OS_FAMILY_bsd
93 # include "os_bsd.inline.hpp"
94 #endif
95
96 static jint CurrentVersion = JNI_VERSION_1_8;
97
98
5210 * prevent this method from being called again at a later time
5211 * (perhaps with different arguments). However, at a certain
5212 * point during initialization if an error occurs we cannot allow
5213 * this function to be called again (or it will crash). In those
5214 * situations, the 'canTryAgain' flag is set to false, which atomically
5215 * sets safe_to_recreate_vm to 1, such that any new call to
5216 * JNI_CreateJavaVM will immediately fail using the above logic.
5217 */
5218 bool can_try_again = true;
5219
5220 result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
5221 if (result == JNI_OK) {
5222 JavaThread *thread = JavaThread::current();
5223 /* thread is thread_in_vm here */
5224 *vm = (JavaVM *)(&main_vm);
5225 *(JNIEnv**)penv = thread->jni_environment();
5226
5227 // Tracks the time application was running before GC
5228 RuntimeService::record_application_start();
5229
5230 // Notify JVMTI
5231 if (JvmtiExport::should_post_thread_life()) {
5232 JvmtiExport::post_thread_start(thread);
5233 }
5234
5235 EventThreadStart event;
5236 if (event.should_commit()) {
5237 event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5238 event.commit();
5239 }
5240
5241 #ifndef PRODUCT
5242 #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED
5243 #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f()
5244 #endif
5245
5246 // Check if we should compile all classes on bootclasspath
5247 if (CompileTheWorld) ClassLoader::compile_the_world();
5248 if (ReplayCompiles) ciReplay::replay(thread);
5249
5425 // cleanup outside the handle mark.
5426 attach_failed = true;
5427 }
5428 }
5429
5430 if (attach_failed) {
5431 // Added missing cleanup
5432 thread->cleanup_failed_attach_current_thread();
5433 return JNI_ERR;
5434 }
5435
5436 // mark the thread as no longer attaching
5437 // this uses a fence to push the change through so we don't have
5438 // to regrab the threads_lock
5439 thread->set_done_attaching_via_jni();
5440
5441 // Set java thread status.
5442 java_lang_Thread::set_thread_status(thread->threadObj(),
5443 java_lang_Thread::RUNNABLE);
5444
5445 // Notify the debugger
5446 if (JvmtiExport::should_post_thread_life()) {
5447 JvmtiExport::post_thread_start(thread);
5448 }
5449
5450 EventThreadStart event;
5451 if (event.should_commit()) {
5452 event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5453 event.commit();
5454 }
5455
5456 *(JNIEnv**)penv = thread->jni_environment();
5457
5458 // Now leaving the VM, so change thread_state. This is normally automatically taken care
5459 // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
5460 // using ThreadStateTransition::transition, we do a callback to the safepoint code if
5461 // needed.
5462
5463 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5464
5518 JNI_OK);
5519 #endif /* USDT2 */
5520 return JNI_OK;
5521 }
5522
5523 JavaThread* thread = JavaThread::current();
5524 if (thread->has_last_Java_frame()) {
5525 #ifndef USDT2
5526 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_ERR);
5527 #else /* USDT2 */
5528 HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
5529 (uint32_t) JNI_ERR);
5530 #endif /* USDT2 */
5531 // Can't detach a thread that's running java, that can't work.
5532 return JNI_ERR;
5533 }
5534
5535 // Safepoint support. Have to do call-back to safepoint code, if in the
5536 // middel of a safepoint operation
5537 ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
5538
5539 // XXX: Note that JavaThread::exit() call below removes the guards on the
5540 // stack pages set up via enable_stack_{red,yellow}_zone() calls
5541 // above in jni_AttachCurrentThread. Unfortunately, while the setting
5542 // of the guards is visible in jni_AttachCurrentThread above,
5543 // the removal of the guards is buried below in JavaThread::exit()
5544 // here. The abstraction should be more symmetrically either exposed
5545 // or hidden (e.g. it could probably be hidden in the same
5546 // (platform-dependent) methods where we do alternate stack
5547 // maintenance work?)
5548 thread->exit(false, JavaThread::jni_detach);
5549 delete thread;
5550
5551 #ifndef USDT2
5552 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_OK);
5553 #else /* USDT2 */
5554 HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
5555 JNI_OK);
5556 #endif /* USDT2 */
5557 return JNI_OK;
|
59 #include "prims/jvm_misc.hpp"
60 #include "prims/jvmtiExport.hpp"
61 #include "prims/jvmtiThreadState.hpp"
62 #include "runtime/compilationPolicy.hpp"
63 #include "runtime/fieldDescriptor.hpp"
64 #include "runtime/fprofiler.hpp"
65 #include "runtime/handles.inline.hpp"
66 #include "runtime/interfaceSupport.hpp"
67 #include "runtime/java.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/jfieldIDWorkaround.hpp"
70 #include "runtime/orderAccess.inline.hpp"
71 #include "runtime/reflection.hpp"
72 #include "runtime/sharedRuntime.hpp"
73 #include "runtime/signature.hpp"
74 #include "runtime/thread.inline.hpp"
75 #include "runtime/vm_operations.hpp"
76 #include "services/memTracker.hpp"
77 #include "services/runtimeService.hpp"
78 #include "trace/tracing.hpp"
79 #include "evtrace/traceEvents.hpp"
80 #include "utilities/defaultStream.hpp"
81 #include "utilities/dtrace.hpp"
82 #include "utilities/events.hpp"
83 #include "utilities/histogram.hpp"
84 #ifdef TARGET_OS_FAMILY_linux
85 # include "os_linux.inline.hpp"
86 #endif
87 #ifdef TARGET_OS_FAMILY_solaris
88 # include "os_solaris.inline.hpp"
89 #endif
90 #ifdef TARGET_OS_FAMILY_windows
91 # include "os_windows.inline.hpp"
92 #endif
93 #ifdef TARGET_OS_FAMILY_bsd
94 # include "os_bsd.inline.hpp"
95 #endif
96
97 static jint CurrentVersion = JNI_VERSION_1_8;
98
99
5211 * prevent this method from being called again at a later time
5212 * (perhaps with different arguments). However, at a certain
5213 * point during initialization if an error occurs we cannot allow
5214 * this function to be called again (or it will crash). In those
5215 * situations, the 'canTryAgain' flag is set to false, which atomically
5216 * sets safe_to_recreate_vm to 1, such that any new call to
5217 * JNI_CreateJavaVM will immediately fail using the above logic.
5218 */
5219 bool can_try_again = true;
5220
5221 result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
5222 if (result == JNI_OK) {
5223 JavaThread *thread = JavaThread::current();
5224 /* thread is thread_in_vm here */
5225 *vm = (JavaVM *)(&main_vm);
5226 *(JNIEnv**)penv = thread->jni_environment();
5227
5228 // Tracks the time application was running before GC
5229 RuntimeService::record_application_start();
5230
5231 if (EnableEventTracing) {
5232 TraceEvents::write_thread_start();
5233 }
5234
5235 // Notify JVMTI
5236 if (JvmtiExport::should_post_thread_life()) {
5237 JvmtiExport::post_thread_start(thread);
5238 }
5239
5240 EventThreadStart event;
5241 if (event.should_commit()) {
5242 event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5243 event.commit();
5244 }
5245
5246 #ifndef PRODUCT
5247 #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED
5248 #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f()
5249 #endif
5250
5251 // Check if we should compile all classes on bootclasspath
5252 if (CompileTheWorld) ClassLoader::compile_the_world();
5253 if (ReplayCompiles) ciReplay::replay(thread);
5254
5430 // cleanup outside the handle mark.
5431 attach_failed = true;
5432 }
5433 }
5434
5435 if (attach_failed) {
5436 // Added missing cleanup
5437 thread->cleanup_failed_attach_current_thread();
5438 return JNI_ERR;
5439 }
5440
5441 // mark the thread as no longer attaching
5442 // this uses a fence to push the change through so we don't have
5443 // to regrab the threads_lock
5444 thread->set_done_attaching_via_jni();
5445
5446 // Set java thread status.
5447 java_lang_Thread::set_thread_status(thread->threadObj(),
5448 java_lang_Thread::RUNNABLE);
5449
5450 if (EnableEventTracing) {
5451 TraceEvents::write_thread_start();
5452 }
5453
5454 // Notify the debugger
5455 if (JvmtiExport::should_post_thread_life()) {
5456 JvmtiExport::post_thread_start(thread);
5457 }
5458
5459 EventThreadStart event;
5460 if (event.should_commit()) {
5461 event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5462 event.commit();
5463 }
5464
5465 *(JNIEnv**)penv = thread->jni_environment();
5466
5467 // Now leaving the VM, so change thread_state. This is normally automatically taken care
5468 // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
5469 // using ThreadStateTransition::transition, we do a callback to the safepoint code if
5470 // needed.
5471
5472 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5473
5527 JNI_OK);
5528 #endif /* USDT2 */
5529 return JNI_OK;
5530 }
5531
5532 JavaThread* thread = JavaThread::current();
5533 if (thread->has_last_Java_frame()) {
5534 #ifndef USDT2
5535 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_ERR);
5536 #else /* USDT2 */
5537 HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
5538 (uint32_t) JNI_ERR);
5539 #endif /* USDT2 */
5540 // Can't detach a thread that's running java, that can't work.
5541 return JNI_ERR;
5542 }
5543
5544 // Safepoint support. Have to do call-back to safepoint code, if in the
5545 // middel of a safepoint operation
5546 ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
5547
5548 if (EnableEventTracing) {
5549 TraceEvents::write_thread_exit();
5550 }
5551
5552 // XXX: Note that JavaThread::exit() call below removes the guards on the
5553 // stack pages set up via enable_stack_{red,yellow}_zone() calls
5554 // above in jni_AttachCurrentThread. Unfortunately, while the setting
5555 // of the guards is visible in jni_AttachCurrentThread above,
5556 // the removal of the guards is buried below in JavaThread::exit()
5557 // here. The abstraction should be more symmetrically either exposed
5558 // or hidden (e.g. it could probably be hidden in the same
5559 // (platform-dependent) methods where we do alternate stack
5560 // maintenance work?)
5561 thread->exit(false, JavaThread::jni_detach);
5562 delete thread;
5563
5564 #ifndef USDT2
5565 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_OK);
5566 #else /* USDT2 */
5567 HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
5568 JNI_OK);
5569 #endif /* USDT2 */
5570 return JNI_OK;
|