Skip to content

Commit b8ab016

Browse files
authored
Merge pull request #5959 from keithc-ca/management
(0.14.3) Explicitly load the jdk.management.agent module
2 parents d90beea + fa8611d commit b8ab016

File tree

3 files changed

+81
-31
lines changed

3 files changed

+81
-31
lines changed

jcl/src/java.base/share/classes/com/ibm/tools/attach/target/Attachment.java

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,51 @@ final class Attachment extends Thread implements Response {
7070
private static final class MethodRefsHolder {
7171
static Method startLocalManagementAgentMethod = null;
7272
static Method startRemoteManagementAgentMethod = null;
73+
static final Throwable managementAgentMethodThrowable;
7374
static {
74-
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
75-
Class<?> agentClass;
76-
Class<?> startRemoteArgumentType;
75+
managementAgentMethodThrowable = AccessController.doPrivileged((PrivilegedAction<Throwable>) () -> {
76+
String agentClassName =
77+
/*[IF Sidecar19-SE]*/
78+
"jdk.internal.agent.Agent"; //$NON-NLS-1$
79+
/*[ELSE] Sidecar19-SE
80+
"sun.management.Agent"; //$NON-NLS-1$
81+
/*[ENDIF] Sidecar19-SE */
82+
IPC.logMessage("Loading " + agentClassName); //$NON-NLS-1$
83+
Throwable mamtTemp = null;
7784
try {
78-
/*[IF Sidecar19-SE-OpenJ9]*/
79-
agentClass = Class.forName("jdk.internal.agent.Agent"); //$NON-NLS-1$
80-
/*[ELSE] Sidecar19-SE-OpenJ9 */
81-
agentClass = Class.forName("sun.management.Agent"); //$NON-NLS-1$
82-
/*[ENDIF] Sidecar19-SE-OpenJ9 */
83-
84-
/*[IF Sidecar19-SE-OpenJ9 | Sidecar18-SE-OpenJ9]*/
85+
Class<?> agentClass = null;
86+
Class<?> startRemoteArgumentType = null;
87+
/*[IF Sidecar19-SE]*/
88+
String jmaName = "jdk.management.agent"; //$NON-NLS-1$
89+
java.lang.Module jmaModule = jdk.internal.module.Modules.loadModule(jmaName);
90+
/* this should not happen because loadModule() should throw java.lang.module.FindException */
91+
if (null == jmaModule) {
92+
throw new ClassNotFoundException("Cannot load " + jmaName); //$NON-NLS-1$
93+
}
94+
/* This does not throw ClassNotFoundException. */
95+
agentClass = Class.forName(jmaModule, agentClassName);
96+
if (null == agentClass) {
97+
throw new ClassNotFoundException("Cannot load " + agentClassName); //$NON-NLS-1$
98+
}
99+
/*[ELSE] Sidecar19-SE */
100+
agentClass = Class.forName(agentClassName);
101+
/*[ENDIF] Sidecar19-SE */
102+
103+
/*[IF Sidecar19-SE | Sidecar18-SE-OpenJ9]*/
85104
startRemoteArgumentType = String.class;
86-
/*[ELSE] Sidecar19-SE-OpenJ9 | Sidecar18-SE-OpenJ9 */
105+
/*[ELSE] Sidecar19-SE | Sidecar18-SE-OpenJ9 */
87106
startRemoteArgumentType = Properties.class;
88-
/*[ENDIF] Sidecar19-SE-OpenJ9 | Sidecar18-SE-OpenJ9 */
107+
/*[ENDIF] Sidecar19-SE | Sidecar18-SE-OpenJ9 */
89108
startLocalManagementAgentMethod = agentClass.getDeclaredMethod(START_LOCAL_MANAGEMENT_AGENT);
90109
startRemoteManagementAgentMethod = agentClass.getDeclaredMethod(START_REMOTE_MANAGEMENT_AGENT, startRemoteArgumentType);
91110
startLocalManagementAgentMethod.setAccessible(true);
92111
startRemoteManagementAgentMethod.setAccessible(true);
93-
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
94-
startLocalManagementAgentMethod = null;
95-
startRemoteManagementAgentMethod = null;
112+
IPC.logMessage("Loaded " + agentClassName); //$NON-NLS-1$
113+
} catch (Throwable e) {
114+
IPC.logMessage("Error loading " + agentClassName, e); //$NON-NLS-1$
115+
mamtTemp = e;
96116
}
97-
return null;
117+
return mamtTemp;
98118
});
99119
}
100120
}
@@ -222,8 +242,8 @@ boolean doCommand(InputStream cmdStream, OutputStream respStream) {
222242
String serviceAddress = startLocalAgent();
223243
AttachmentConnection.streamSend(respStream, Response.ATTACH_RESULT + serviceAddress);
224244
} catch (IbmAttachOperationFailedException e) {
225-
AttachmentConnection.streamSend(respStream, Response.ERROR + " " //$NON-NLS-1$
226-
+ EXCEPTION_ATTACH_OPERATION_FAILED_EXCEPTION + " in startLocalManagementAgent: " + e.getMessage()); //$NON-NLS-1$
245+
AttachmentConnection.streamSend(respStream, String.format("%s: %s in startLocalManagementAgent: %s", //$NON-NLS-1$
246+
Response.ERROR, EXCEPTION_ATTACH_OPERATION_FAILED_EXCEPTION, e.toString()));
227247
return false;
228248
}
229249
} else if (cmd.startsWith(Command.START_MANAGEMENT_AGENT)) {
@@ -416,13 +436,13 @@ private boolean startAgent(Properties agentProperties) {
416436
IPC.logMessage("startAgent"); //$NON-NLS-1$
417437
if (null != MethodRefsHolder.startRemoteManagementAgentMethod) {
418438
Object startArgument;
419-
/*[IF Sidecar19-SE-OpenJ9 | Sidecar18-SE-OpenJ9]*/
439+
/*[IF Sidecar19-SE | Sidecar18-SE-OpenJ9]*/
420440
startArgument = agentProperties.entrySet().stream()
421441
.map(entry -> entry.getKey() + "=" + entry.getValue()) //$NON-NLS-1$
422442
.collect(java.util.stream.Collectors.joining(",")); //$NON-NLS-1$
423-
/*[ELSE] Sidecar19-SE-OpenJ9 | Sidecar18-SE-OpenJ9 */
443+
/*[ELSE] Sidecar19-SE | Sidecar18-SE-OpenJ9 */
424444
startArgument = agentProperties;
425-
/*[ENDIF] Sidecar19-SE-OpenJ9 | Sidecar18-SE-OpenJ9 */
445+
/*[ENDIF] Sidecar19-SE | Sidecar18-SE-OpenJ9 */
426446
MethodRefsHolder.startRemoteManagementAgentMethod.invoke(null, startArgument);
427447
return true;
428448
}
@@ -439,19 +459,24 @@ private boolean startAgent(Properties agentProperties) {
439459

440460
private static String startLocalAgent() throws IbmAttachOperationFailedException {
441461
IPC.logMessage("startLocalAgent"); //$NON-NLS-1$
442-
try {
443-
if (null != MethodRefsHolder.startLocalManagementAgentMethod) { /* forces initialization */
462+
if (null != MethodRefsHolder.startLocalManagementAgentMethod) { /* forces initialization */
463+
try {
444464
MethodRefsHolder.startLocalManagementAgentMethod.invoke(null);
445-
} else {
446-
throw new IbmAttachOperationFailedException("startLocalManagementAgent cannot access " + START_LOCAL_MANAGEMENT_AGENT); //$NON-NLS-1$
465+
} catch (Throwable exc) {
466+
IPC.logMessage("Exception starting management agent:", exc); //$NON-NLS-1$
467+
throw new IbmAttachOperationFailedException("startLocalManagementAgent error starting agent", exc); //$NON-NLS-1$
447468
}
448-
} catch (Throwable e) {
449-
throw new IbmAttachOperationFailedException("startLocalManagementAgent error starting agent:" + e.getClass() + " " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
469+
} else {
470+
Throwable exc = MethodRefsHolder.managementAgentMethodThrowable;
471+
String msg = "Target VM cannot access " + START_LOCAL_MANAGEMENT_AGENT; //$NON-NLS-1$
472+
IPC.logMessage(msg, exc);
473+
throw new IbmAttachOperationFailedException(msg, exc);
450474
}
451475

452476
String addr = saveLocalConnectorAddress();
453477
if (Objects.isNull(addr)) {
454-
throw new IbmAttachOperationFailedException("startLocalManagementAgent: " + LOCAL_CONNECTOR_ADDRESS + " not defined"); //$NON-NLS-1$ //$NON-NLS-2$
478+
throw new IbmAttachOperationFailedException(
479+
"startLocalManagementAgent: " + LOCAL_CONNECTOR_ADDRESS + " not defined"); //$NON-NLS-1$ //$NON-NLS-2$
455480
}
456481
return addr;
457482
}

jcl/src/java.base/share/classes/com/ibm/tools/attach/target/IPC.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,16 @@ public static void logMessage(String string1, int int1, String string2, String s
418418
* Print the information about a throwable, including the exact class,
419419
* message, and stack trace.
420420
* @param msg User supplied message
421-
* @param thrown throwable
421+
* @param thrown Throwable object or null
422422
* @note nothing is printed if logging is disabled
423423
*/
424424
public static void logMessage(String msg, Throwable thrown) {
425425
synchronized (accessorMutex) {
426426
if (isLoggingEnabled()) {
427427
printMessageWithHeader(msg, logStream);
428-
thrown.printStackTrace(logStream);
428+
if (null != thrown) {
429+
thrown.printStackTrace(logStream);
430+
}
429431
logStream.flush();
430432
}
431433
}

jcl/src/java.base/share/classes/com/ibm/tools/attach/target/IbmAttachOperationFailedException.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
@SuppressWarnings("serial")
1010
/*******************************************************************************
11-
* Copyright (c) 2015, 2015 IBM Corp. and others
11+
* Copyright (c) 2015, 2019 IBM Corp. and others
1212
*
1313
* This program and the accompanying materials are made available under
1414
* the terms of the Eclipse Public License 2.0 which accompanies this
@@ -39,6 +39,7 @@ public class IbmAttachOperationFailedException extends IOException {
3939
public IbmAttachOperationFailedException() {
4040
super("IbmAttachOperationFailedException"); //$NON-NLS-1$
4141
}
42+
4243
/**
4344
* Constructs a new instance of this class with its
4445
* walkback and message filled in.
@@ -48,4 +49,26 @@ public IbmAttachOperationFailedException() {
4849
public IbmAttachOperationFailedException(String message) {
4950
super(message);
5051
}
52+
53+
/**
54+
* Constructs the exception with a message and a nested Throwable.
55+
* @param message text of the message
56+
* @param cause underlying Throwable
57+
*/
58+
public IbmAttachOperationFailedException(String message, Throwable cause) {
59+
super(message, cause);
60+
}
61+
62+
@Override
63+
public String toString() {
64+
String result;
65+
Throwable myCause = getCause();
66+
if (null != myCause) {
67+
result = String.format("\"%s\"; Caused by: \"%s\"", super.toString(), myCause.toString()); //$NON-NLS-1$
68+
} else {
69+
result = super.toString();
70+
}
71+
return result;
72+
}
73+
5174
}

0 commit comments

Comments
 (0)