src/share/vm/prims/jni.cpp

Print this page
rev 13113 : 8182651: Add TRACE_ONLY conditional macro to support more fine-grained INCLUDE_TRACE programming
Reviewed-by:


  59 #include "prims/jvm_misc.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "runtime/atomic.hpp"
  63 #include "runtime/compilationPolicy.hpp"
  64 #include "runtime/fieldDescriptor.hpp"
  65 #include "runtime/fprofiler.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/interfaceSupport.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/jfieldIDWorkaround.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/reflection.hpp"
  73 #include "runtime/sharedRuntime.hpp"
  74 #include "runtime/signature.hpp"
  75 #include "runtime/thread.inline.hpp"
  76 #include "runtime/vm_operations.hpp"
  77 #include "services/memTracker.hpp"
  78 #include "services/runtimeService.hpp"
  79 #include "trace/traceMacros.hpp"
  80 #include "trace/tracing.hpp"
  81 #include "utilities/defaultStream.hpp"
  82 #include "utilities/dtrace.hpp"
  83 #include "utilities/events.hpp"
  84 #include "utilities/histogram.hpp"
  85 #include "utilities/internalVMTests.hpp"
  86 #include "utilities/macros.hpp"
  87 #if INCLUDE_ALL_GCS
  88 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  89 #endif // INCLUDE_ALL_GCS
  90 #if INCLUDE_JVMCI
  91 #include "jvmci/jvmciCompiler.hpp"
  92 #include "jvmci/jvmciRuntime.hpp"
  93 #endif




  94 
  95 static jint CurrentVersion = JNI_VERSION_9;
  96 
  97 #ifdef _WIN32
  98 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* );
  99 #endif
 100 
 101 // The DT_RETURN_MARK macros create a scoped object to fire the dtrace
 102 // '-return' probe regardless of the return path is taken out of the function.
 103 // Methods that have multiple return paths use this to avoid having to
 104 // instrument each return path.  Methods that use CHECK or THROW must use this
 105 // since those macros can cause an immedate uninstrumented return.
 106 //
 107 // In order to get the return value, a reference to the variable containing
 108 // the return value must be passed to the contructor of the object, and
 109 // the return value must be set before return (since the mark object has
 110 // a reference to it).
 111 //
 112 // Example:
 113 // DT_RETURN_MARK_DECL(SomeFunc, int);


3808     }
3809     func = JNI_FastGetField::generate_fast_get_int_field();
3810     if (func != (address)-1) {
3811       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3812     }
3813     func = JNI_FastGetField::generate_fast_get_long_field();
3814     if (func != (address)-1) {
3815       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3816     }
3817     func = JNI_FastGetField::generate_fast_get_float_field();
3818     if (func != (address)-1) {
3819       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3820     }
3821     func = JNI_FastGetField::generate_fast_get_double_field();
3822     if (func != (address)-1) {
3823       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3824     }
3825   }
3826 }
3827 











3828 // Returns the function structure
3829 struct JNINativeInterface_* jni_functions() {
3830 #if INCLUDE_JNI_CHECK
3831   if (CheckJNICalls) return jni_functions_check();
3832 #endif // INCLUDE_JNI_CHECK
3833   return &jni_NativeInterface;
3834 }
3835 
3836 // Returns the function structure
3837 struct JNINativeInterface_* jni_functions_nocheck() {
3838   return &jni_NativeInterface;
3839 }
3840 
3841 
3842 // Invocation API
3843 
3844 
3845 // Forward declaration
3846 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3847 


3953           JavaThread* THREAD = thread;
3954           JVMCICompiler* compiler = JVMCICompiler::instance(CATCH);
3955           compiler->bootstrap(THREAD);
3956           if (HAS_PENDING_EXCEPTION) {
3957             HandleMark hm;
3958             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3959           }
3960         }
3961       }
3962     }
3963 #endif
3964 
3965     // Tracks the time application was running before GC
3966     RuntimeService::record_application_start();
3967 
3968     // Notify JVMTI
3969     if (JvmtiExport::should_post_thread_life()) {
3970        JvmtiExport::post_thread_start(thread);
3971     }
3972 
3973     EventThreadStart event;
3974     if (event.should_commit()) {
3975       event.set_thread(THREAD_TRACE_ID(thread));
3976       event.commit();
3977     }
3978 
3979 #ifndef PRODUCT
3980     // Check if we should compile all classes on bootclasspath
3981     if (CompileTheWorld) ClassLoader::compile_the_world();
3982     if (ReplayCompiles) ciReplay::replay(thread);
3983 
3984     // Some platforms (like Win*) need a wrapper around these test
3985     // functions in order to properly handle error conditions.
3986     test_error_handler();
3987     if (ExecuteInternalVMTests) {
3988       InternalVMTests::run();
3989     }
3990 #endif
3991 
3992     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
3993     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
3994   } else {
3995     // If create_vm exits because of a pending exception, exit with that
3996     // exception.  In the future when we figure out how to reclaim memory,
3997     // we may be able to exit with JNI_ERR and allow the calling application


4173   if (attach_failed) {
4174     // Added missing cleanup
4175     thread->cleanup_failed_attach_current_thread();
4176     return JNI_ERR;
4177   }
4178 
4179   // mark the thread as no longer attaching
4180   // this uses a fence to push the change through so we don't have
4181   // to regrab the threads_lock
4182   thread->set_done_attaching_via_jni();
4183 
4184   // Set java thread status.
4185   java_lang_Thread::set_thread_status(thread->threadObj(),
4186               java_lang_Thread::RUNNABLE);
4187 
4188   // Notify the debugger
4189   if (JvmtiExport::should_post_thread_life()) {
4190     JvmtiExport::post_thread_start(thread);
4191   }
4192 
4193   EventThreadStart event;
4194   if (event.should_commit()) {
4195     event.set_thread(THREAD_TRACE_ID(thread));
4196     event.commit();
4197   }
4198 
4199   *(JNIEnv**)penv = thread->jni_environment();
4200 
4201   // Now leaving the VM, so change thread_state. This is normally automatically taken care
4202   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
4203   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
4204   // needed.
4205 
4206   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4207 
4208   // Perform any platform dependent FPU setup
4209   os::setup_fpu();
4210 
4211   return JNI_OK;
4212 }
4213 
4214 
4215 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4216   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4217   if (!vm_created) {




  59 #include "prims/jvm_misc.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "runtime/atomic.hpp"
  63 #include "runtime/compilationPolicy.hpp"
  64 #include "runtime/fieldDescriptor.hpp"
  65 #include "runtime/fprofiler.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/interfaceSupport.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/jfieldIDWorkaround.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/reflection.hpp"
  73 #include "runtime/sharedRuntime.hpp"
  74 #include "runtime/signature.hpp"
  75 #include "runtime/thread.inline.hpp"
  76 #include "runtime/vm_operations.hpp"
  77 #include "services/memTracker.hpp"
  78 #include "services/runtimeService.hpp"


  79 #include "utilities/defaultStream.hpp"
  80 #include "utilities/dtrace.hpp"
  81 #include "utilities/events.hpp"
  82 #include "utilities/histogram.hpp"
  83 #include "utilities/internalVMTests.hpp"
  84 #include "utilities/macros.hpp"
  85 #if INCLUDE_ALL_GCS
  86 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  87 #endif // INCLUDE_ALL_GCS
  88 #if INCLUDE_JVMCI
  89 #include "jvmci/jvmciCompiler.hpp"
  90 #include "jvmci/jvmciRuntime.hpp"
  91 #endif
  92 #if INCLUDE_TRACE
  93 #include "trace/traceMacros.hpp"
  94 #include "trace/tracing.hpp"
  95 #endif
  96 
  97 static jint CurrentVersion = JNI_VERSION_9;
  98 
  99 #ifdef _WIN32
 100 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* );
 101 #endif
 102 
 103 // The DT_RETURN_MARK macros create a scoped object to fire the dtrace
 104 // '-return' probe regardless of the return path is taken out of the function.
 105 // Methods that have multiple return paths use this to avoid having to
 106 // instrument each return path.  Methods that use CHECK or THROW must use this
 107 // since those macros can cause an immedate uninstrumented return.
 108 //
 109 // In order to get the return value, a reference to the variable containing
 110 // the return value must be passed to the contructor of the object, and
 111 // the return value must be set before return (since the mark object has
 112 // a reference to it).
 113 //
 114 // Example:
 115 // DT_RETURN_MARK_DECL(SomeFunc, int);


3810     }
3811     func = JNI_FastGetField::generate_fast_get_int_field();
3812     if (func != (address)-1) {
3813       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3814     }
3815     func = JNI_FastGetField::generate_fast_get_long_field();
3816     if (func != (address)-1) {
3817       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3818     }
3819     func = JNI_FastGetField::generate_fast_get_float_field();
3820     if (func != (address)-1) {
3821       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3822     }
3823     func = JNI_FastGetField::generate_fast_get_double_field();
3824     if (func != (address)-1) {
3825       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3826     }
3827   }
3828 }
3829 
3830 #if INCLUDE_TRACE
3831 static void post_thread_start_event(const JavaThread* jt) {
3832   assert(jt != NULL, "invariant");
3833   EventThreadStart event;
3834   if (event.should_commit()) {
3835     event.set_thread(THREAD_TRACE_ID(jt));
3836     event.commit();
3837   }
3838 }
3839 #endif
3840 
3841 // Returns the function structure
3842 struct JNINativeInterface_* jni_functions() {
3843 #if INCLUDE_JNI_CHECK
3844   if (CheckJNICalls) return jni_functions_check();
3845 #endif // INCLUDE_JNI_CHECK
3846   return &jni_NativeInterface;
3847 }
3848 
3849 // Returns the function structure
3850 struct JNINativeInterface_* jni_functions_nocheck() {
3851   return &jni_NativeInterface;
3852 }
3853 
3854 
3855 // Invocation API
3856 
3857 
3858 // Forward declaration
3859 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3860 


3966           JavaThread* THREAD = thread;
3967           JVMCICompiler* compiler = JVMCICompiler::instance(CATCH);
3968           compiler->bootstrap(THREAD);
3969           if (HAS_PENDING_EXCEPTION) {
3970             HandleMark hm;
3971             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3972           }
3973         }
3974       }
3975     }
3976 #endif
3977 
3978     // Tracks the time application was running before GC
3979     RuntimeService::record_application_start();
3980 
3981     // Notify JVMTI
3982     if (JvmtiExport::should_post_thread_life()) {
3983        JvmtiExport::post_thread_start(thread);
3984     }
3985 
3986     TRACE_ONLY(post_thread_start_event(thread);)




3987 
3988 #ifndef PRODUCT
3989     // Check if we should compile all classes on bootclasspath
3990     if (CompileTheWorld) ClassLoader::compile_the_world();
3991     if (ReplayCompiles) ciReplay::replay(thread);
3992 
3993     // Some platforms (like Win*) need a wrapper around these test
3994     // functions in order to properly handle error conditions.
3995     test_error_handler();
3996     if (ExecuteInternalVMTests) {
3997       InternalVMTests::run();
3998     }
3999 #endif
4000 
4001     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
4002     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4003   } else {
4004     // If create_vm exits because of a pending exception, exit with that
4005     // exception.  In the future when we figure out how to reclaim memory,
4006     // we may be able to exit with JNI_ERR and allow the calling application


4182   if (attach_failed) {
4183     // Added missing cleanup
4184     thread->cleanup_failed_attach_current_thread();
4185     return JNI_ERR;
4186   }
4187 
4188   // mark the thread as no longer attaching
4189   // this uses a fence to push the change through so we don't have
4190   // to regrab the threads_lock
4191   thread->set_done_attaching_via_jni();
4192 
4193   // Set java thread status.
4194   java_lang_Thread::set_thread_status(thread->threadObj(),
4195               java_lang_Thread::RUNNABLE);
4196 
4197   // Notify the debugger
4198   if (JvmtiExport::should_post_thread_life()) {
4199     JvmtiExport::post_thread_start(thread);
4200   }
4201 
4202   TRACE_ONLY(post_thread_start_event(thread);)




4203 
4204   *(JNIEnv**)penv = thread->jni_environment();
4205 
4206   // Now leaving the VM, so change thread_state. This is normally automatically taken care
4207   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
4208   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
4209   // needed.
4210 
4211   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4212 
4213   // Perform any platform dependent FPU setup
4214   os::setup_fpu();
4215 
4216   return JNI_OK;
4217 }
4218 
4219 
4220 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4221   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4222   if (!vm_created) {