diff -r 468f0664cb39 src/hotspot/share/prims/jvmtiExport.cpp --- a/src/hotspot/share/prims/jvmtiExport.cpp Tue Sep 26 06:53:35 2017 -0400 +++ b/src/hotspot/share/prims/jvmtiExport.cpp Wed Sep 27 00:35:24 2017 +0900 @@ -2475,7 +2475,7 @@ jint JvmtiExport::load_agent_library(const char *agent, const char *absParam, const char *options, outputStream* st) { - char ebuf[1024]; + char ebuf[1024] = {0}; char buffer[JVM_MAXPATHLEN]; void* library = NULL; jint result = JNI_ERR; @@ -2525,6 +2525,8 @@ if (!agent_lib->is_static_lib()) { os::dll_unload(library); } + st->print_cr("%s is not available in %s", + on_attach_symbols[0], agent_lib->name()); delete agent_lib; } else { // Invoke the Agent_OnAttach function @@ -2539,6 +2541,8 @@ // Agent_OnAttach may have used JNI if (HAS_PENDING_EXCEPTION) { + java_lang_Throwable::print(PENDING_EXCEPTION, st); + st->cr(); CLEAR_PENDING_EXCEPTION; } @@ -2550,11 +2554,16 @@ delete agent_lib; } - // Agent_OnAttach executed so completion status is JNI_OK - st->print_cr("%d", result); + st->print_cr("return code: %d", result); result = JNI_OK; } + } else { + st->print_cr("%s was not loaded.", agent); + if (*ebuf != '\0') { + st->print_cr("%s", ebuf); + } } + return result; } diff -r 468f0664cb39 src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java --- a/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java Tue Sep 26 06:53:35 2017 -0400 +++ b/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java Wed Sep 27 00:35:24 2017 +0900 @@ -40,6 +40,8 @@ import java.security.PrivilegedAction; import java.util.Properties; import java.util.stream.Collectors; +import java.util.regex.Pattern; +import java.util.regex.Matcher; /* * The HotSpot implementation of com.sun.tools.attach.VirtualMachine. @@ -86,18 +88,26 @@ private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) throws AgentLoadException, AgentInitializationException, IOException { - InputStream in = execute("load", - agentLibrary, - isAbsolute ? "true" : "false", - options); - try { - int result = readInt(in); - if (result != 0) { - throw new AgentInitializationException("Agent_OnAttach failed", result); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader( + execute("load", agentLibrary, + Boolean.toString(isAbsolute), options)))) { + String result = reader.readLine(); + if (result == null) { + throw new AgentLoadException("Target VM did not respond"); + } else { + Matcher matcher = Pattern.compile("^return code: (\\d+)$") + .matcher(result); + if (matcher.matches()) { + int retCode = Integer.parseInt(matcher.group(1)); + if (retCode != 0) { + throw new AgentInitializationException( + "Agent_OnAttach failed", retCode); + } + } else { + throw new AgentLoadException(result); + } } - } finally { - in.close(); - } } diff -r 468f0664cb39 test/jdk/com/sun/tools/attach/StartManagementAgent.java --- a/test/jdk/com/sun/tools/attach/StartManagementAgent.java Tue Sep 26 06:53:35 2017 -0400 +++ b/test/jdk/com/sun/tools/attach/StartManagementAgent.java Wed Sep 27 00:35:24 2017 +0900 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -99,7 +99,7 @@ } catch(AttachOperationFailedException ex) { // We expect parsing of "apa" above to fail, but if the file path // can't be read we get a different exception message - if (!ex.getMessage().contains("Invalid com.sun.management.jmxremote.port number")) { + if (!ex.getMessage().contains("For input string: \"apa\"")) { throw ex; } ex.printStackTrace(System.err);