[java-debugger][IDEA-375478] isolated the method of getting the path to the java agent into a separate method

GitOrigin-RevId: bddabab68df6b4624e00324084dfd9a9327b76e3
This commit is contained in:
Alexander.Glukhov
2025-07-09 18:19:45 +00:00
committed by intellij-monorepo-bot
parent 2d6d48a6cd
commit 3e7f0329bf
@@ -298,93 +298,99 @@ public final class AsyncStacksUtils {
@Nullable Project project,
boolean checkJdkVersion,
@Nullable Disposable disposable) {
if (isAgentEnabled()) {
String prefix = "-javaagent:";
ParametersList parametersList = parameters.getVMParametersList();
if (!ContainerUtil.exists(parametersList.getParameters(), p -> p.startsWith(prefix) && p.contains(AGENT_JAR_NAME))) {
Sdk jdk = parameters.getJdk();
if (checkJdkVersion && jdk == null) {
return;
if (!isAgentEnabled()) {
return;
}
String prefix = "-javaagent:";
ParametersList parametersList = parameters.getVMParametersList();
if (ContainerUtil.exists(parametersList.getParameters(), p -> p.startsWith(prefix) && p.contains(AGENT_JAR_NAME))) {
return;
}
Sdk jdk = parameters.getJdk();
if (checkJdkVersion && jdk == null) {
return;
}
JavaSdkVersion sdkVersion = jdk != null ? JavaSdk.getInstance().getVersion(jdk) : null;
if (checkJdkVersion && (sdkVersion == null || !sdkVersion.isAtLeast(JavaSdkVersion.JDK_1_7))) {
LOG.warn("Capture agent is not supported for JRE " + sdkVersion);
return;
}
Path agentArtifactPath = getAgentArtifactPath(disposable);
if (Files.exists(agentArtifactPath)) {
String agentPath = JavaExecutionUtil.handleSpacesInAgentPath(agentArtifactPath.toAbsolutePath().toString(),
"captureAgent", null,
f -> f.getName().startsWith("debugger-agent"));
if (agentPath != null) {
try (AccessToken ignore = SlowOperations.knownIssue("IDEA-307303, EA-835503")) {
parametersList.prepend(prefix + agentPath + generateAgentSettings(project));
}
JavaSdkVersion sdkVersion = jdk != null ? JavaSdk.getInstance().getVersion(jdk) : null;
if (checkJdkVersion && (sdkVersion == null || !sdkVersion.isAtLeast(JavaSdkVersion.JDK_1_7))) {
LOG.warn("Capture agent is not supported for JRE " + sdkVersion);
return;
}
Path agentArtifactPath;
String relevantJarsRoot = PathManager.getArchivedCompliedClassesLocation();
Path classesRoot = Path.of(PathUtil.getJarPathForClass(DebuggerManagerImpl.class));
// isDirectory(classesRoot) is used instead of `PluginManagerCore.isRunningFromSources()`
// because we want to use installer's layout when running "IDEA (dev build)" run configuration
// where the layout is quite the same as in installers.
// but `PluginManagerCore.isRunningFromSources()` still returns `true` in this case
if (Files.isDirectory(classesRoot) || (relevantJarsRoot != null && classesRoot.startsWith(relevantJarsRoot))) {
// Code runs from IDEA run configuration (code from .class file in out/ directory)
try {
// The agent file must have a fixed name (AGENT_JAR_NAME) which is mentioned in MANIFEST.MF inside
Path debuggerAgentDir =
FileUtil.createTempDirectory(new File(PathManager.getTempPath()), "debugger-agent", "", disposable == null).toPath();
if (disposable != null) {
Disposer.register(disposable, () -> {
try {
FileUtilRt.deleteRecursively(debuggerAgentDir);
}
catch (IOException ignored) {
}
});
}
agentArtifactPath = debuggerAgentDir.resolve(AGENT_JAR_NAME);
Path communityRoot = Path.of(PathManager.getCommunityHomePath());
Path iml = BuildDependenciesJps.getProjectModule(communityRoot, "intellij.java.debugger.agent.holder");
Path downloadedAgent = BuildDependenciesJps.INSTANCE.getModuleLibrarySingleRootSync(
iml,
"debugger-agent",
"https://cache-redirector.jetbrains.com/intellij-dependencies",
new BuildDependenciesCommunityRoot(Path.of(PathManager.getCommunityHomePath())));
Files.copy(downloadedAgent, agentArtifactPath);
if (Registry.is("debugger.async.stacks.coroutines", false)) {
parametersList.addProperty("kotlinx.coroutines.debug.enable.creation.stack.trace", "false");
parametersList.addProperty("debugger.agent.enable.coroutines", "true");
if (Registry.is("debugger.async.stacks.flows", false)) {
parametersList.addProperty("kotlinx.coroutines.debug.enable.flows.stack.trace", "true");
}
catch (IOException e) {
throw new RuntimeException(e);
if (Registry.is("debugger.async.stacks.state.flows", false)) {
parametersList.addProperty("kotlinx.coroutines.debug.enable.mutable.state.flows.stack.trace", "true");
}
}
else {
agentArtifactPath = classesRoot.resolveSibling("rt").resolve(AGENT_JAR_NAME);
if (!Registry.is("debugger.async.stack.trace.for.exceptions.printing", false)) {
parametersList.addProperty("debugger.agent.support.throwable", "false");
}
if (Files.exists(agentArtifactPath)) {
String agentPath = JavaExecutionUtil.handleSpacesInAgentPath(agentArtifactPath.toAbsolutePath().toString(),
"captureAgent", null,
f -> f.getName().startsWith("debugger-agent"));
if (agentPath != null) {
try (AccessToken ignore = SlowOperations.knownIssue("IDEA-307303, EA-835503")) {
parametersList.prepend(prefix + agentPath + generateAgentSettings(project));
}
if (Registry.is("debugger.async.stacks.coroutines", false)) {
parametersList.addProperty("kotlinx.coroutines.debug.enable.creation.stack.trace", "false");
parametersList.addProperty("debugger.agent.enable.coroutines", "true");
if (Registry.is("debugger.async.stacks.flows", false)) {
parametersList.addProperty("kotlinx.coroutines.debug.enable.flows.stack.trace", "true");
}
if (Registry.is("debugger.async.stacks.state.flows", false)) {
parametersList.addProperty("kotlinx.coroutines.debug.enable.mutable.state.flows.stack.trace", "true");
}
}
if (!Registry.is("debugger.async.stack.trace.for.exceptions.printing", false)) {
parametersList.addProperty("debugger.agent.support.throwable", "false");
}
if (Registry.is("debugger.async.stack.trace.for.all.threads")) {
parametersList.addProperty("debugger.async.stack.trace.for.all.threads", "true");
}
}
}
else {
LOG.error("Capture agent not found: " + agentArtifactPath);
if (Registry.is("debugger.async.stack.trace.for.all.threads")) {
parametersList.addProperty("debugger.async.stack.trace.for.all.threads", "true");
}
}
}
else {
LOG.error("Capture agent not found: " + agentArtifactPath);
}
}
private static @NotNull Path getAgentArtifactPath(@Nullable Disposable disposable) {
String relevantJarsRoot = PathManager.getArchivedCompliedClassesLocation();
Path classesRoot = Path.of(PathUtil.getJarPathForClass(DebuggerManagerImpl.class));
// isDirectory(classesRoot) is used instead of `PluginManagerCore.isRunningFromSources()`
// because we want to use installer's layout when running "IDEA (dev build)" run configuration
// where the layout is quite the same as in installers.
// but `PluginManagerCore.isRunningFromSources()` still returns `true` in this case
if (Files.isDirectory(classesRoot) || (relevantJarsRoot != null && classesRoot.startsWith(relevantJarsRoot))) {
// Code runs from IDEA run configuration (code from .class file in out/ directory)
try {
// The agent file must have a fixed name (AGENT_JAR_NAME) which is mentioned in MANIFEST.MF inside
Path debuggerAgentDir =
FileUtil.createTempDirectory(new File(PathManager.getTempPath()), "debugger-agent", "", disposable == null).toPath();
if (disposable != null) {
Disposer.register(disposable, () -> {
try {
FileUtilRt.deleteRecursively(debuggerAgentDir);
}
catch (IOException ignored) {
}
});
}
Path agentArtifactPath = debuggerAgentDir.resolve(AGENT_JAR_NAME);
Path communityRoot = Path.of(PathManager.getCommunityHomePath());
Path iml = BuildDependenciesJps.getProjectModule(communityRoot, "intellij.java.debugger.agent.holder");
Path downloadedAgent = BuildDependenciesJps.INSTANCE.getModuleLibrarySingleRootSync(
iml,
"debugger-agent",
"https://cache-redirector.jetbrains.com/intellij-dependencies",
new BuildDependenciesCommunityRoot(Path.of(PathManager.getCommunityHomePath())));
Files.copy(downloadedAgent, agentArtifactPath);
return agentArtifactPath;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
else {
return classesRoot.resolveSibling("rt").resolve(AGENT_JAR_NAME);
}
}
private static String generateAgentSettings(@Nullable Project project) {