From a0c8b33c2667512133ce0beb41562c1499aa5f47 Mon Sep 17 00:00:00 2001 From: hexiaofeng Date: Wed, 18 Dec 2024 15:02:32 +0800 Subject: [PATCH] exclude java native tool --- .../jd/live/agent/bootstrap/LiveAgent.java | 60 ++++++++++++++----- .../main/assembly/config/bootstrap.properties | 2 +- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/joylive-bootstrap/joylive-bootstrap-premain/src/main/java/com/jd/live/agent/bootstrap/LiveAgent.java b/joylive-bootstrap/joylive-bootstrap-premain/src/main/java/com/jd/live/agent/bootstrap/LiveAgent.java index 39659195..7ba7d28f 100644 --- a/joylive-bootstrap/joylive-bootstrap-premain/src/main/java/com/jd/live/agent/bootstrap/LiveAgent.java +++ b/joylive-bootstrap/joylive-bootstrap-premain/src/main/java/com/jd/live/agent/bootstrap/LiveAgent.java @@ -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") @@ -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 excludeApps; /** @@ -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) { @@ -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); } /** @@ -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)); } /** @@ -588,6 +592,34 @@ private static Set getExcludeApps(Function 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); diff --git a/joylive-package/src/main/assembly/config/bootstrap.properties b/joylive-package/src/main/assembly/config/bootstrap.properties index 65eb1af3..0ef25b7f 100644 --- a/joylive-package/src/main/assembly/config/bootstrap.properties +++ b/joylive-package/src/main/assembly/config/bootstrap.properties @@ -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=