Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 20 additions & 56 deletions btrace-agent/src/main/java/org/openjdk/btrace/agent/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,73 +719,37 @@ private static void parseArgs() {
}

private static void processClasspaths(String libs) {
// Try to find JAR via Loader.class (unmasked bootstrap class)
// Main.class won't work because it's loaded from .classdata
String bootPath = null;
try {
Class<?> loaderClass = Class.forName("org.openjdk.btrace.boot.Loader");
URL loaderResource = loaderClass.getResource("Loader.class");
if (loaderResource != null) {
bootPath = loaderResource.toString();
if (bootPath.startsWith("jar:file:")) {
// Extract JAR path from jar:file:/path/to/btrace.jar!/org/openjdk/btrace/boot/Loader.class
bootPath = bootPath.substring("jar:file:".length());
int idx = bootPath.indexOf("!");
if (idx > -1) {
bootPath = bootPath.substring(0, idx);
}
}
}
} catch (ClassNotFoundException e) {
// Fall back to Main.class if Loader not found (shouldn't happen)
URL agentJar = Main.class.getResource("Main.class");
if (agentJar != null) {
bootPath = agentJar.toString().replace("jar:file:", "");
int idx = bootPath.indexOf("btrace-agent.jar");
if (idx > -1) {
bootPath = bootPath.substring(0, idx) + "btrace-boot.jar";
}
}
}

String bootClassPath = argMap.get(BOOT_CLASS_PATH);
if (bootClassPath == null && bootPath != null) {
bootClassPath = bootPath;
} else if (bootClassPath != null && bootPath != null) {
if (".".equals(bootClassPath)) {
bootClassPath = bootPath;
} else {
bootClassPath = bootPath + File.pathSeparator + bootClassPath;
}
}
log.debug("Bootstrap ClassPath: {}", bootClassPath);

StringTokenizer tokenizer = new StringTokenizer(bootClassPath, File.pathSeparator);
try {
while (tokenizer.hasMoreTokens()) {
String path = tokenizer.nextToken();
File f = new File(path);
if (!f.exists()) {
log.debug("BTrace bootstrap classpath resource [{}] does not exist", path);
} else {
if (f.isFile() && f.getName().toLowerCase().endsWith(".jar")) {
JarFile jf = asJarFile(f);
log.debug("Adding jar: {}", jf);
inst.appendToBootstrapClassLoaderSearch(jf);
if (bootClassPath != null) {
log.debug("Bootstrap ClassPath: {}", bootClassPath);
Comment on lines 721 to +725
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The agent-class masking/AgentClassLoader approach means Main is loaded from .classdata, but processClasspaths() still relies on addPreconfLibs(libs) which locates the agent JAR via Main.class resource. In the produced agentJar, Main.class won't exist (it becomes Main.classdata), so the preconfigured btrace-libs enhancement will silently stop working. Consider locating the jar via Agent.class/ProtectionDomain (or another unmasked .class) instead of Main.class resources.

Copilot uses AI. Check for mistakes.
StringTokenizer tokenizer = new StringTokenizer(bootClassPath, File.pathSeparator);
try {
while (tokenizer.hasMoreTokens()) {
String path = tokenizer.nextToken();
File f = new File(path);
if (!f.exists()) {
log.warn("BTrace bootstrap classpath resource [{}] does not exist", path);
} else {
log.debug("ignoring boot classpath element '{}' - only jar files allowed", path);
if (f.isFile() && f.getName().toLowerCase().endsWith(".jar")) {
JarFile jf = asJarFile(f);
log.debug("Adding jar: {}", jf);
inst.appendToBootstrapClassLoaderSearch(jf);
} else {
log.debug("ignoring boot classpath element '{}' - only jar files allowed", path);
}
}
}
} catch (IOException ex) {
log.debug("adding to boot classpath failed!", ex);
return;
}
} catch (IOException ex) {
log.debug("adding to boot classpath failed!", ex);
return;
}

String systemClassPath = argMap.get(SYSTEM_CLASS_PATH);
if (systemClassPath != null) {
log.debug("System ClassPath: {}", systemClassPath);
tokenizer = new StringTokenizer(systemClassPath, File.pathSeparator);
StringTokenizer tokenizer = new StringTokenizer(systemClassPath, File.pathSeparator);
try {
while (tokenizer.hasMoreTokens()) {
String path = tokenizer.nextToken();
Expand Down
Loading