Skip to content

Commit

Permalink
exclude java native tool
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Dec 18, 2024
1 parent 65a851c commit a0c8b33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ private static synchronized void launch(String arguments, Instrumentation instru
shutdownOnError = ctx.shutdownOnError;
if (ctx.isInJavaToolOptions()) {
if (ctx.isExcludeApp()) {
logger.log(Level.INFO, "[LiveAgent] exit when excluding main class " + ctx.mainClass);
// logger.log(Level.INFO, "[LiveAgent] the agent will exit when excluding main class " + ctx.bootClass.name);
return;
} else {
logger.log(Level.INFO, "[LiveAgent] main class " + ctx.mainClass
} else if (ctx.bootClass != null) {
logger.log(Level.INFO, "[LiveAgent] main class " + ctx.bootClass.name
+ "\nif you do not want to enhance this application, "
+ "\nyou can append this main class to \"agent.enhance.excludeApp\" in "
+ new File(ctx.configDir, "bootstrap.properties")
Expand Down Expand Up @@ -213,7 +213,7 @@ private static class InstallContext {
private ConfigResolver configuration;
private String command;
private boolean shutdownOnError;
private String mainClass;
private BootClass bootClass;
private Set<String> excludeApps;

/**
Expand All @@ -232,7 +232,7 @@ public static InstallContext parse(String arguments, Instrumentation instrumenta
arg.args = createArgs(arguments);
arg.env = createEnv();
arg.command = (String) arg.args.get(ARG_COMMAND);
arg.mainClass = getMainClass();
arg.bootClass = getBootClass();

arg.root = LivePath.getRootPath(arg.env, arg.args);
if (arg.root != null) {
Expand Down Expand Up @@ -260,16 +260,18 @@ public static InstallContext parse(String arguments, Instrumentation instrumenta
*
* @return The main class as a string.
*/
private static String getMainClass() {
private static BootClass getBootClass() {
String command = System.getProperty(SUN_JAVA_COMMAND);
int index = command.indexOf(" ");
if (index != -1) {
command = command.substring(0, index);
}
if (command.endsWith(".jar")) {
return getMainClassByJar(command);
if (command != null) {
int index = command.indexOf(" ");
if (index != -1) {
command = command.substring(0, index);
}
if (command.endsWith(".jar")) {
command = getMainClassByJar(command);
}
}
return command;
return command == null ? null : new BootClass(command);
}

/**
Expand Down Expand Up @@ -305,7 +307,9 @@ private static String getMainClassByJar(String file) {
* @return true if the main class is excluded, false otherwise.
*/
public boolean isExcludeApp() {
return mainClass != null && excludeApps.contains(mainClass);
return bootClass != null && (bootClass.isJdkModule()
|| excludeApps.contains(bootClass.name)
|| excludeApps.contains(bootClass.className));
}

/**
Expand Down Expand Up @@ -588,6 +592,34 @@ private static Set<String> getExcludeApps(Function<String, Object> env) {

}

/**
* Represents a boot class in the Java Virtual Machine.
*/
private static class BootClass {
private final String name;
private final String module;
private final String className;

BootClass(String name) {
this.name = name;
// jdk.jcmd/sun.tools.jstack.JStack
// jdk.jcmd/sun.tools.jmap.JMap
// jdk.compiler/com.sun.tools.javac.Main
int pos = name.lastIndexOf('/');
this.module = pos > 0 ? name.substring(0, pos) : null;
this.className = pos > 0 ? name.substring(pos + 1) : name;
}

/**
* Checks if the boot class belongs to a JDK module.
*
* @return true if the boot class belongs to a JDK module, false otherwise.
*/
public boolean isJdkModule() {
return module != null && module.startsWith("jdk.");
}
}

private static class LogHandler extends Handler {
final com.jd.live.agent.bootstrap.logger.Logger delegate = LoggerFactory.getLogger(LogHandler.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ app.location.cell=${APPLICATION_LOCATION_CELL}
app.location.laneSpaceId=${APPLICATION_LOCATION_LANESPACE_ID}
app.location.lane=${APPLICATION_LOCATION_LANE}
agent.enhance.shutdownOnError=${CONFIG_ENHANCE_SHUTDOWN_ON_ERROR:true}
agent.enhance.excludeApp=${CONFIG_ENHANCE_EXCLUDE_APPS:com.taobao.arthas.boot.Bootstrap}
agent.enhance.excludeApp=${CONFIG_ENHANCE_EXCLUDE_APPS:com.taobao.arthas.boot.Bootstrap,org.netbeans.Main}
classloader.contextLoaderEnabled=${CLASSLOADER_CONTEXT_LOADER_ENABLED:true}
classloader.core.configExtensions=yaml,yml,xml,json,properties
#classloader.core.parentResources=
Expand Down

0 comments on commit a0c8b33

Please sign in to comment.