Skip to content

Commit 9ce1324

Browse files
committed
Refactor flashlight-agent attachment code to simplify it.
1 parent 4a7d6ed commit 9ce1324

File tree

5 files changed

+52
-47
lines changed

5 files changed

+52
-47
lines changed

nucleus/flashlight/framework/src/main/java/org/glassfish/flashlight/FlashlightLoggerInfo.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ public static Logger getLogger() {
132132
level = "WARNING")
133133
public static final String NO_ATTACH_API = LOGMSG_PREFIX + "-00510";
134134

135-
@LogMessageInfo(
136-
message = "Error while getting Instrumentation object from ProbeAgentMain",
137-
level = "WARNING")
138-
public static final String NO_ATTACH_GET = LOGMSG_PREFIX + "-00511";
139-
140135
@LogMessageInfo(
141136
message = "DTrace is not available.",
142137
cause="This is caused if following are missing: \n1. JDK 7 is required to run DTrace\n2. glassfish-dtrace.jar value-add is required for DTrace",

nucleus/flashlight/framework/src/main/java/org/glassfish/flashlight/cli/EnableMonitoring.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void execute(AdminCommandContext context) {
126126
if (!AgentAttacher.attachAgent(pidInt, options)) {
127127
ActionReport.MessagePart part = report.getTopMessagePart().addChild();
128128
part.setMessage(localStrings.getLocalString("attach.agent.exception",
129-
"Can't attach the agent to the JVM. Restart with the flash-light agent attached by the -javaagent command line parameter"));
129+
"Can't attach the monitoring agent to the JVM. Restart with the flashlight-agent.jar attached by the -javaagent command line parameter"));
130130
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
131131
return;
132132
}

nucleus/flashlight/framework/src/main/java/org/glassfish/flashlight/impl/client/AgentAttacher.java

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,88 @@
1313
*
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
*/
16-
1716
package org.glassfish.flashlight.impl.client;
1817

18+
import java.lang.instrument.Instrumentation;
19+
import java.lang.reflect.Method;
20+
import java.util.Optional;
21+
import java.util.logging.Level;
22+
import java.util.logging.Logger;
23+
24+
import org.glassfish.flashlight.FlashlightLoggerInfo;
25+
26+
1927
/**
2028
* created May 26, 2011
29+
*
2130
* @author Byron Nevins
2231
*/
2332
public final class AgentAttacher {
33+
34+
private static final String AGENT_CLASSNAME = "org.glassfish.flashlight.agent.ProbeAgentMain";
35+
36+
private static final Logger logger = FlashlightLoggerInfo.getLogger();
37+
38+
public static Optional<Instrumentation> getInstrumentation() {
39+
try {
40+
Class agentMainClass = null;
41+
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
42+
43+
try {
44+
agentMainClass = classLoader.loadClass(AGENT_CLASSNAME);
45+
} catch (Throwable t) {
46+
// need throwable, not Exception - it may throw an Error!
47+
// try one more time after attempting to attach.
48+
AgentAttacher.attachAgent();
49+
// might throw
50+
agentMainClass = classLoader.loadClass(AGENT_CLASSNAME);
51+
}
52+
53+
Method mthd = agentMainClass.getMethod("getInstrumentation", null);
54+
return Optional.ofNullable((Instrumentation) mthd.invoke(null, null));
55+
} catch (Throwable throwable) {
56+
logger.log(Level.WARNING, "Error while getting Instrumentation object from ProbeAgentMain", throwable);
57+
return Optional.empty();
58+
}
59+
}
60+
2461
public synchronized static boolean canAttach() {
2562
return canAttach;
2663
}
2764

2865
public synchronized static boolean isAttached() {
2966
try {
30-
if (!canAttach)
67+
if (!canAttach) {
3168
return false;
69+
}
3270

3371
return AgentAttacherInternal.isAttached();
34-
}
35-
catch (Throwable t) {
72+
} catch (Throwable t) {
3673
return false;
3774
}
3875
}
3976

4077
public synchronized static boolean attachAgent() {
4178

4279
try {
43-
if (!canAttach)
80+
if (!canAttach) {
4481
return false;
82+
}
4583

4684
return attachAgent(-1, "");
47-
}
48-
catch (Throwable t) {
85+
} catch (Throwable t) {
4986
return false;
5087
}
5188
}
5289

5390
public synchronized static boolean attachAgent(int pid, String options) {
5491
try {
55-
if (!canAttach)
92+
if (!canAttach) {
5693
return false;
94+
}
5795

5896
return AgentAttacherInternal.attachAgent(pid, options);
59-
}
60-
catch (Throwable t) {
97+
} catch (Throwable t) {
6198
return false;
6299
}
63100
}
@@ -71,8 +108,7 @@ public synchronized static boolean attachAgent(int pid, String options) {
71108
// this is a distinct possibility in embedded mode.
72109
AgentAttacherInternal.isAttached();
73110
b = true;
74-
}
75-
catch (Throwable t) {
111+
} catch (Throwable t) {
76112
b = false;
77113
}
78114
canAttach = b;

nucleus/flashlight/framework/src/main/java/org/glassfish/flashlight/impl/client/AgentAttacherInternal.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static boolean attachAgent() {
4848
return attachAgent(-1, "");
4949
}
5050

51-
private static final String AGENT_CLASSNAME = "org.glassfish.flashlight.agent.ProbeAgentMain";
5251
static boolean attachAgent(long pid, String options) {
5352
try {
5453
if (isAttached) {
@@ -57,11 +56,9 @@ static boolean attachAgent(long pid, String options) {
5756

5857
if (pid < 0) {
5958

60-
try {
61-
ClassLoader.getSystemClassLoader().loadClass(AGENT_CLASSNAME);
59+
if (AgentAttacher.getInstrumentation().isPresent()) {
6260
isAttached = true;
6361
return true;
64-
} catch (Throwable t) {
6562
}
6663

6764
pid = ProcessHandle.current().pid();

nucleus/flashlight/framework/src/main/java/org/glassfish/flashlight/impl/client/ProbeProviderClassFileTransformer.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
import static org.glassfish.embeddable.GlassFishVariable.INSTALL_ROOT;
5050
import static org.glassfish.flashlight.FlashlightLoggerInfo.NO_ATTACH_API;
51-
import static org.glassfish.flashlight.FlashlightLoggerInfo.NO_ATTACH_GET;
5251
import static org.glassfish.flashlight.FlashlightLoggerInfo.REGISTRATION_ERROR;
5352
import static org.glassfish.flashlight.FlashlightLoggerInfo.RETRANSFORMATION_ERROR;
5453
import static org.glassfish.flashlight.FlashlightLoggerInfo.WRITE_ERROR;
@@ -82,7 +81,6 @@ public class ProbeProviderClassFileTransformer implements ClassFileTransformer {
8281
private static final Instrumentation instrumentation;
8382
private static boolean _debug = Boolean.parseBoolean(Utility.getEnvOrProp("AS_DEBUG"));
8483
private static boolean emittedAttachUnavailableMessageAlready = false;
85-
private static final String AGENT_CLASSNAME = "org.glassfish.flashlight.agent.ProbeAgentMain";
8684
private static final Logger logger = FlashlightLoggerInfo.getLogger();
8785

8886
private ProbeProviderClassFileTransformer(Class providerClass) {
@@ -494,28 +492,7 @@ private Method getMethod(FlashlightProbe probe) throws NoSuchMethodException {
494492
if (AgentAttacher.canAttach()) {
495493
canAttach = true;
496494

497-
try {
498-
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
499-
500-
try {
501-
agentMainClass = classLoader.loadClass(AGENT_CLASSNAME);
502-
}
503-
catch (Throwable t) {
504-
// need throwable, not Exception - it may throw an Error!
505-
// try one more time after attempting to attach.
506-
AgentAttacher.attachAgent();
507-
// might throw
508-
agentMainClass = classLoader.loadClass(AGENT_CLASSNAME);
509-
}
510-
511-
Method mthd = agentMainClass.getMethod("getInstrumentation", null);
512-
nonFinalInstrumentation = (Instrumentation) mthd.invoke(null, null);
513-
}
514-
catch (Throwable t) {
515-
nonFinalInstrumentation = null;
516-
// save it for nice neat message code below
517-
throwable = t;
518-
}
495+
nonFinalInstrumentation = AgentAttacher.getInstrumentation().orElse(null);
519496
}
520497
// set the final
521498
instrumentation = nonFinalInstrumentation;
@@ -525,7 +502,7 @@ private Method getMethod(FlashlightProbe probe) throws NoSuchMethodException {
525502
} else if (instrumentation != null) {
526503
Log.info("yes.attach.api", instrumentation);
527504
} else {
528-
logger.log(Level.WARNING, NO_ATTACH_GET, throwable);
505+
logger.log(Level.WARNING, "Could not attach agent");
529506
}
530507
}
531508
}

0 commit comments

Comments
 (0)