--- old/src/share/vm/prims/jvmtiExport.cpp 2013-07-03 14:26:25.752568394 -0400 +++ new/src/share/vm/prims/jvmtiExport.cpp 2013-07-03 14:26:24.752510886 -0400 @@ -2190,6 +2190,8 @@ char buffer[JVM_MAXPATHLEN]; void* library = NULL; jint result = JNI_ERR; + const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS; + size_t num_symbol_entries = sizeof(on_attach_symbols) / sizeof(char*); // get agent name and options const char* agent = op->arg(0); @@ -2198,44 +2200,50 @@ // The abs paramter should be "true" or "false" bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0); + bool is_static_lib = false; + // Initially marked as invalid. It will be set to valid if we can find the agent + AgentLibrary *agentLib = new AgentLibrary(agent, options, is_absolute_path, NULL); - // If the path is absolute we attempt to load the library. Otherwise we try to - // load it from the standard dll directory. - - if (is_absolute_path) { - library = os::dll_load(agent, ebuf, sizeof ebuf); - } else { - // Try to load the agent from the standard dll directory - if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), - agent)) { - library = os::dll_load(buffer, ebuf, sizeof ebuf); - } - if (library == NULL) { - // not found - try local path - char ns[1] = {0}; - if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) { + // Check for builtin agent. If not then if the path is absolute we attempt + // to load the library. Otherwise we try to load it from the standard + // dll directory. + + if (!os::findBuiltinAgent(agentLib, on_attach_symbols, num_symbol_entries)) { + if (is_absolute_path) { + library = os::dll_load(agent, ebuf, sizeof ebuf); + } else { + // Try to load the agent from the standard dll directory + if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), + agent)) { library = os::dll_load(buffer, ebuf, sizeof ebuf); } + if (library == NULL) { + // not found - try local path + char ns[1] = {0}; + if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) { + library = os::dll_load(buffer, ebuf, sizeof ebuf); + } + } + } + if (library != NULL) { + agentLib->set_os_lib(library); + agentLib->set_valid(); } } - // If the library was loaded then we attempt to invoke the Agent_OnAttach // function - if (library != NULL) { - + if (agentLib->valid()) { // Lookup the Agent_OnAttach function OnAttachEntry_t on_attach_entry = NULL; - const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS; - for (uint symbol_index = 0; symbol_index < ARRAY_SIZE(on_attach_symbols); symbol_index++) { - on_attach_entry = - CAST_TO_FN_PTR(OnAttachEntry_t, os::dll_lookup(library, on_attach_symbols[symbol_index])); - if (on_attach_entry != NULL) break; - } - + on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t, + os::findAgentFunction(agentLib, false, on_attach_symbols, num_symbol_entries)); if (on_attach_entry == NULL) { // Agent_OnAttach missing - unload library - os::dll_unload(library); + if (!agentLib->is_static_lib()) { + os::dll_unload(library); + } + delete agentLib; } else { // Invoke the Agent_OnAttach function JavaThread* THREAD = JavaThread::current(); @@ -2255,7 +2263,9 @@ // If OnAttach returns JNI_OK then we add it to the list of // agent libraries so that we can call Agent_OnUnload later. if (result == JNI_OK) { - Arguments::add_loaded_agent(agent, (char*)options, is_absolute_path, library); + Arguments::add_loaded_agent(agentLib); + } else { + delete agentLib; } // Agent_OnAttach executed so completion status is JNI_OK