3679 3680 // Give os specific code one last chance to start 3681 os::init_3(); 3682 3683 create_vm_timer.end(); 3684 #ifdef ASSERT 3685 _vm_complete = true; 3686 #endif 3687 return JNI_OK; 3688 } 3689 3690 // type for the Agent_OnLoad and JVM_OnLoad entry points 3691 extern "C" { 3692 typedef jint (JNICALL *OnLoadEntry_t)(JavaVM *, char *, void *); 3693 } 3694 // Find a command line agent library and return its entry point for 3695 // -agentlib: -agentpath: -Xrun 3696 // num_symbol_entries must be passed-in since only the caller knows the number of symbols in the array. 3697 static OnLoadEntry_t lookup_on_load(AgentLibrary* agent, const char *on_load_symbols[], size_t num_symbol_entries) { 3698 OnLoadEntry_t on_load_entry = NULL; 3699 void *library = agent->os_lib(); // check if we have looked it up before 3700 3701 if (library == NULL) { 3702 char buffer[JVM_MAXPATHLEN]; 3703 char ebuf[1024]; 3704 const char *name = agent->name(); 3705 const char *msg = "Could not find agent library "; 3706 3707 if (agent->is_absolute_path()) { 3708 library = os::dll_load(name, ebuf, sizeof ebuf); 3709 if (library == NULL) { 3710 const char *sub_msg = " in absolute path, with error: "; 3711 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3712 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread); 3713 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3714 // If we can't find the agent, exit. 3715 vm_exit_during_initialization(buf, NULL); 3716 FREE_C_HEAP_ARRAY(char, buf, mtThread); 3717 } 3718 } else { 3719 // Try to load the agent from the standard dll directory 3720 if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), 3721 name)) { 3722 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3723 } 3724 if (library == NULL) { // Try the local directory 3725 char ns[1] = {0}; 3726 if (os::dll_build_name(buffer, sizeof(buffer), ns, name)) { 3727 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3728 } 3729 if (library == NULL) { 3730 const char *sub_msg = " on the library path, with error: "; 3731 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3732 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread); 3733 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3734 // If we can't find the agent, exit. 3735 vm_exit_during_initialization(buf, NULL); 3736 FREE_C_HEAP_ARRAY(char, buf, mtThread); 3737 } 3738 } 3739 } 3740 agent->set_os_lib(library); 3741 } 3742 3743 // Find the OnLoad function. 3744 for (size_t symbol_index = 0; symbol_index < num_symbol_entries; symbol_index++) { 3745 on_load_entry = CAST_TO_FN_PTR(OnLoadEntry_t, os::dll_lookup(library, on_load_symbols[symbol_index])); 3746 if (on_load_entry != NULL) break; 3747 } 3748 return on_load_entry; 3749 } 3750 3751 // Find the JVM_OnLoad entry point 3752 static OnLoadEntry_t lookup_jvm_on_load(AgentLibrary* agent) { 3753 const char *on_load_symbols[] = JVM_ONLOAD_SYMBOLS; 3754 return lookup_on_load(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*)); 3755 } 3756 3757 // Find the Agent_OnLoad entry point 3758 static OnLoadEntry_t lookup_agent_on_load(AgentLibrary* agent) { 3759 const char *on_load_symbols[] = AGENT_ONLOAD_SYMBOLS; 3760 return lookup_on_load(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*)); 3761 } 3762 3763 // For backwards compatibility with -Xrun 3764 // Convert libraries with no JVM_OnLoad, but which have Agent_OnLoad to be 3765 // treated like -agentpath: 3766 // Must be called before agent libraries are created 3767 void Threads::convert_vm_init_libraries_to_agents() { 3802 if (on_load_entry != NULL) { 3803 // Invoke the Agent_OnLoad function 3804 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL); 3805 if (err != JNI_OK) { 3806 vm_exit_during_initialization("agent library failed to init", agent->name()); 3807 } 3808 } else { 3809 vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name()); 3810 } 3811 } 3812 JvmtiExport::enter_primordial_phase(); 3813 } 3814 3815 extern "C" { 3816 typedef void (JNICALL *Agent_OnUnload_t)(JavaVM *); 3817 } 3818 3819 void Threads::shutdown_vm_agents() { 3820 // Send any Agent_OnUnload notifications 3821 const char *on_unload_symbols[] = AGENT_ONUNLOAD_SYMBOLS; 3822 extern struct JavaVM_ main_vm; 3823 for (AgentLibrary* agent = Arguments::agents(); agent != NULL; agent = agent->next()) { 3824 3825 // Find the Agent_OnUnload function. 3826 for (uint symbol_index = 0; symbol_index < ARRAY_SIZE(on_unload_symbols); symbol_index++) { 3827 Agent_OnUnload_t unload_entry = CAST_TO_FN_PTR(Agent_OnUnload_t, 3828 os::dll_lookup(agent->os_lib(), on_unload_symbols[symbol_index])); 3829 3830 // Invoke the Agent_OnUnload function 3831 if (unload_entry != NULL) { 3832 JavaThread* thread = JavaThread::current(); 3833 ThreadToNativeFromVM ttn(thread); 3834 HandleMark hm(thread); 3835 (*unload_entry)(&main_vm); 3836 break; 3837 } 3838 } 3839 } 3840 } 3841 3842 // Called for after the VM is initialized for -Xrun libraries which have not been converted to agent libraries 3843 // Invokes JVM_OnLoad 3844 void Threads::create_vm_init_libraries() { 3845 extern struct JavaVM_ main_vm; 3846 AgentLibrary* agent; 3847 3848 for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) { 3849 OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent); 3850 3851 if (on_load_entry != NULL) { 3852 // Invoke the JVM_OnLoad function 3853 JavaThread* thread = JavaThread::current(); 3854 ThreadToNativeFromVM ttn(thread); 3855 HandleMark hm(thread); 3856 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL); 3857 if (err != JNI_OK) { | 3679 3680 // Give os specific code one last chance to start 3681 os::init_3(); 3682 3683 create_vm_timer.end(); 3684 #ifdef ASSERT 3685 _vm_complete = true; 3686 #endif 3687 return JNI_OK; 3688 } 3689 3690 // type for the Agent_OnLoad and JVM_OnLoad entry points 3691 extern "C" { 3692 typedef jint (JNICALL *OnLoadEntry_t)(JavaVM *, char *, void *); 3693 } 3694 // Find a command line agent library and return its entry point for 3695 // -agentlib: -agentpath: -Xrun 3696 // num_symbol_entries must be passed-in since only the caller knows the number of symbols in the array. 3697 static OnLoadEntry_t lookup_on_load(AgentLibrary* agent, const char *on_load_symbols[], size_t num_symbol_entries) { 3698 OnLoadEntry_t on_load_entry = NULL; 3699 void *library = NULL; 3700 3701 if (!agent->valid()) { 3702 char buffer[JVM_MAXPATHLEN]; 3703 char ebuf[1024]; 3704 const char *name = agent->name(); 3705 const char *msg = "Could not find agent library "; 3706 3707 // First check to see if agent is statcally linked into executable 3708 if (os::find_builtin_agent(agent, on_load_symbols, num_symbol_entries)) { 3709 library = agent->os_lib(); 3710 } else if (agent->is_absolute_path()) { 3711 library = os::dll_load(name, ebuf, sizeof ebuf); 3712 if (library == NULL) { 3713 const char *sub_msg = " in absolute path, with error: "; 3714 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3715 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread); 3716 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3717 // If we can't find the agent, exit. 3718 vm_exit_during_initialization(buf, NULL); 3719 FREE_C_HEAP_ARRAY(char, buf, mtThread); 3720 } 3721 } else { 3722 // Try to load the agent from the standard dll directory 3723 if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), 3724 name)) { 3725 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3726 } 3727 if (library == NULL) { // Try the local directory 3728 char ns[1] = {0}; 3729 if (os::dll_build_name(buffer, sizeof(buffer), ns, name)) { 3730 library = os::dll_load(buffer, ebuf, sizeof ebuf); 3731 } 3732 if (library == NULL) { 3733 const char *sub_msg = " on the library path, with error: "; 3734 size_t len = strlen(msg) + strlen(name) + strlen(sub_msg) + strlen(ebuf) + 1; 3735 char *buf = NEW_C_HEAP_ARRAY(char, len, mtThread); 3736 jio_snprintf(buf, len, "%s%s%s%s", msg, name, sub_msg, ebuf); 3737 // If we can't find the agent, exit. 3738 vm_exit_during_initialization(buf, NULL); 3739 FREE_C_HEAP_ARRAY(char, buf, mtThread); 3740 } 3741 } 3742 } 3743 agent->set_os_lib(library); 3744 agent->set_valid(); 3745 } 3746 3747 // Find the OnLoad function. 3748 on_load_entry = 3749 CAST_TO_FN_PTR(OnLoadEntry_t, os::find_agent_function(agent, 3750 false, 3751 on_load_symbols, 3752 num_symbol_entries)); 3753 return on_load_entry; 3754 } 3755 3756 // Find the JVM_OnLoad entry point 3757 static OnLoadEntry_t lookup_jvm_on_load(AgentLibrary* agent) { 3758 const char *on_load_symbols[] = JVM_ONLOAD_SYMBOLS; 3759 return lookup_on_load(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*)); 3760 } 3761 3762 // Find the Agent_OnLoad entry point 3763 static OnLoadEntry_t lookup_agent_on_load(AgentLibrary* agent) { 3764 const char *on_load_symbols[] = AGENT_ONLOAD_SYMBOLS; 3765 return lookup_on_load(agent, on_load_symbols, sizeof(on_load_symbols) / sizeof(char*)); 3766 } 3767 3768 // For backwards compatibility with -Xrun 3769 // Convert libraries with no JVM_OnLoad, but which have Agent_OnLoad to be 3770 // treated like -agentpath: 3771 // Must be called before agent libraries are created 3772 void Threads::convert_vm_init_libraries_to_agents() { 3807 if (on_load_entry != NULL) { 3808 // Invoke the Agent_OnLoad function 3809 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL); 3810 if (err != JNI_OK) { 3811 vm_exit_during_initialization("agent library failed to init", agent->name()); 3812 } 3813 } else { 3814 vm_exit_during_initialization("Could not find Agent_OnLoad function in the agent library", agent->name()); 3815 } 3816 } 3817 JvmtiExport::enter_primordial_phase(); 3818 } 3819 3820 extern "C" { 3821 typedef void (JNICALL *Agent_OnUnload_t)(JavaVM *); 3822 } 3823 3824 void Threads::shutdown_vm_agents() { 3825 // Send any Agent_OnUnload notifications 3826 const char *on_unload_symbols[] = AGENT_ONUNLOAD_SYMBOLS; 3827 size_t num_symbol_entries = ARRAY_SIZE(on_unload_symbols); 3828 extern struct JavaVM_ main_vm; 3829 for (AgentLibrary* agent = Arguments::agents(); agent != NULL; agent = agent->next()) { 3830 3831 // Find the Agent_OnUnload function. 3832 Agent_OnUnload_t unload_entry = CAST_TO_FN_PTR(Agent_OnUnload_t, 3833 os::find_agent_function(agent, 3834 false, 3835 on_unload_symbols, 3836 num_symbol_entries)); 3837 3838 // Invoke the Agent_OnUnload function 3839 if (unload_entry != NULL) { 3840 JavaThread* thread = JavaThread::current(); 3841 ThreadToNativeFromVM ttn(thread); 3842 HandleMark hm(thread); 3843 (*unload_entry)(&main_vm); 3844 } 3845 } 3846 } 3847 3848 // Called for after the VM is initialized for -Xrun libraries which have not been converted to agent libraries 3849 // Invokes JVM_OnLoad 3850 void Threads::create_vm_init_libraries() { 3851 extern struct JavaVM_ main_vm; 3852 AgentLibrary* agent; 3853 3854 for (agent = Arguments::libraries(); agent != NULL; agent = agent->next()) { 3855 OnLoadEntry_t on_load_entry = lookup_jvm_on_load(agent); 3856 3857 if (on_load_entry != NULL) { 3858 // Invoke the JVM_OnLoad function 3859 JavaThread* thread = JavaThread::current(); 3860 ThreadToNativeFromVM ttn(thread); 3861 HandleMark hm(thread); 3862 jint err = (*on_load_entry)(&main_vm, agent->options(), NULL); 3863 if (err != JNI_OK) { |