mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(debugger): memory view for a paused JVM (#2648)
Adds a Memory tab to the Debug tool window for JVM sessions: loaded classes with instance counts and shallow sizes (with deltas between refreshes), read over the HotSpot attach listener (`GC.class_histogram`), falling back to counting reachable objects through the debugger on JDKs that cannot answer while suspended. While paused, a class's instances, an instance's fields and its referrers are listed through a JDWP link (the Kotlin adapter's path, and a jdtls bundle `java/jvm-inspect` for java-debug). Debug launches started by the IDE get `-XX:+StartAttachListener` unless the user's VM options decide otherwise (not on Windows). New crates: ide-jdwp, ide-jvm-attach, ide-jvm-inspect. The bundled jar rebuilds byte-for-byte from build.py against the pinned JDT LS plugins. Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>
This commit is contained in:
@@ -11,10 +11,17 @@
|
||||
*/
|
||||
import { executeAction } from "@/platform/actions";
|
||||
import { call, commands, type RunConfig } from "@/platform/ipc";
|
||||
import { isJvmApplication, JDWP_AGENT, jdwpListeningPort } from "@/platform/javaLaunch";
|
||||
import {
|
||||
ATTACH_LISTENER,
|
||||
isJvmApplication,
|
||||
JDWP_AGENT,
|
||||
jdwpListeningPort,
|
||||
optsOutOfAttach,
|
||||
} from "@/platform/javaLaunch";
|
||||
import { applyBuildPolicyToCommand } from "./buildCommand";
|
||||
import { startRun, useRun } from "./store";
|
||||
import { DELEGATED_DEBUG, DELEGATED_MAIN } from "./gradleRun";
|
||||
import { DELEGATED_ATTACH_LISTENER, DELEGATED_DEBUG, DELEGATED_MAIN } from "./gradleRun";
|
||||
import { isWindows } from "./jvmTypes";
|
||||
|
||||
const isMavenTestGoal = (a: string) =>
|
||||
/^(test|verify|integration-test|surefire:test|failsafe:integration-test)$/.test(a);
|
||||
@@ -28,6 +35,24 @@ function withOption(env: Record<string, string>, key: string, value: string): Re
|
||||
return { ...env, [key]: current ? `${current} ${value}` : value };
|
||||
}
|
||||
|
||||
/**
|
||||
* The agent, preceded by the attach listener unless a VM option of the configuration or the
|
||||
* channel's current value (`existing`) already decides about the attach mechanism. Not on
|
||||
* Windows, where the memory view that needs the listener is not available.
|
||||
*/
|
||||
function agentOptions(config: RunConfig, ...existing: (string | undefined)[]): string[] {
|
||||
const options = [config.params?.vmOptions, ...existing].filter(Boolean).join(" ");
|
||||
return isWindows() || optsOutOfAttach(options) ? [JDWP_AGENT] : [ATTACH_LISTENER, JDWP_AGENT];
|
||||
}
|
||||
|
||||
const agentText = (config: RunConfig, ...existing: (string | undefined)[]) =>
|
||||
agentOptions(config, ...existing).join(" ");
|
||||
|
||||
function propertyValue(args: readonly string[], key: string): string | undefined {
|
||||
const prefix = `-D${key}=`;
|
||||
return args.find((a) => a.startsWith(prefix))?.slice(prefix.length);
|
||||
}
|
||||
|
||||
function withProperty(args: readonly string[], key: string, value: string): string[] {
|
||||
const prefix = `-D${key}=`;
|
||||
const i = args.findIndex((a) => a.startsWith(prefix));
|
||||
@@ -51,26 +76,33 @@ export function jdwpLaunch(config: RunConfig, jdwpPort?: number): RunConfig | nu
|
||||
// (`javac … && java -cp …`): through `JDK_JAVA_OPTIONS`, which only the `java` launcher reads.
|
||||
if (config.kind === "java-file")
|
||||
return args.length
|
||||
? { ...config, args: [JDWP_AGENT, ...args] }
|
||||
: { ...config, env: withOption(config.env, "JDK_JAVA_OPTIONS", JDWP_AGENT) };
|
||||
? { ...config, args: [...agentOptions(config, args.join(" ")), ...args] }
|
||||
: { ...config, env: withOption(config.env, "JDK_JAVA_OPTIONS", agentText(config, config.env.JDK_JAVA_OPTIONS)) };
|
||||
if (config.kind === "maven" || /(^|\/)mvnw?(\.cmd)?$/.test(config.command)) {
|
||||
if (args.some((a) => a === "spring-boot:run" || /:spring-boot-maven-plugin:[^:]*:run$/.test(a)))
|
||||
return { ...config, args: withProperty(args, "spring-boot.run.jvmArguments", JDWP_AGENT) };
|
||||
if (args.some((a) => a === "spring-boot:run" || /:spring-boot-maven-plugin:[^:]*:run$/.test(a))) {
|
||||
const options = agentText(config, propertyValue(args, "spring-boot.run.jvmArguments"));
|
||||
return { ...config, args: withProperty(args, "spring-boot.run.jvmArguments", options) };
|
||||
}
|
||||
if (config.isTest || args.some(isMavenTestGoal)) {
|
||||
const failsafe = args.some((a) => /integration-test|verify|failsafe/.test(a));
|
||||
const agent = jdwpPort ? JDWP_AGENT.replace("address=127.0.0.1:0", `address=127.0.0.1:${jdwpPort}`) : JDWP_AGENT;
|
||||
// Forked-JVM options of the test goals come as `-DargLine=…` among the arguments.
|
||||
const options = agentText(config, args.join(" "));
|
||||
const agent = jdwpPort ? options.replace("address=127.0.0.1:0", `address=127.0.0.1:${jdwpPort}`) : options;
|
||||
let out = withProperty(args, "maven.surefire.debug", agent);
|
||||
if (failsafe) out = withProperty(out, "maven.failsafe.debug", agent);
|
||||
return { ...config, args: out };
|
||||
}
|
||||
if (JVM_ADAPTERS.includes(config.debugAdapter ?? "") || args.some(isMavenInProcessGoal))
|
||||
return { ...config, env: withOption(config.env, "MAVEN_OPTS", JDWP_AGENT) };
|
||||
return { ...config, env: withOption(config.env, "MAVEN_OPTS", agentText(config, config.env.MAVEN_OPTS)) };
|
||||
return null;
|
||||
}
|
||||
if (config.kind === "gradle" || /(^|\/)gradlew?(\.bat)?$/.test(config.command)) {
|
||||
// A delegated Application: its init script adds the agent (port 0) to the JavaExec JVM.
|
||||
if (args.some((a) => a.startsWith(DELEGATED_MAIN)))
|
||||
return args.includes(DELEGATED_DEBUG) ? config : { ...config, args: [...args, DELEGATED_DEBUG] };
|
||||
if (args.some((a) => a.startsWith(DELEGATED_MAIN))) {
|
||||
const flags = isWindows() ? [DELEGATED_DEBUG] : [DELEGATED_ATTACH_LISTENER, DELEGATED_DEBUG];
|
||||
const missing = flags.filter((f) => !args.includes(f));
|
||||
return missing.length ? { ...config, args: [...args, ...missing] } : config;
|
||||
}
|
||||
// Gradle Test forks test workers: the init script gives them a dynamic JDWP port.
|
||||
if (config.isTest || args.some((a) => /(^|:)(test|integrationTest)$/.test(a)))
|
||||
return args.includes(DELEGATED_DEBUG) ? config : { ...config, args: [...args, DELEGATED_DEBUG] };
|
||||
@@ -82,10 +114,29 @@ export function jdwpLaunch(config: RunConfig, jdwpPort?: number): RunConfig | nu
|
||||
}
|
||||
// Application / Spring Boot configurations are launched by java-debug itself.
|
||||
if (JVM_ADAPTERS.includes(config.debugAdapter ?? "") && !isJvmApplication(config))
|
||||
return { ...config, env: withOption(config.env, "JAVA_TOOL_OPTIONS", JDWP_AGENT) };
|
||||
return {
|
||||
...config,
|
||||
env: withOption(config.env, "JAVA_TOOL_OPTIONS", agentText(config, config.env.JAVA_TOOL_OPTIONS)),
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* `--debug-jvm` makes Gradle add the agent itself, so the attach listener reaches the forked
|
||||
* JVMs through an init script (the one VM options use). A failure only costs the memory view.
|
||||
*/
|
||||
async function withGradleAttachListener(launch: RunConfig): Promise<RunConfig> {
|
||||
if (isWindows() || !launch.args.includes("--debug-jvm") || launch.target) return launch;
|
||||
if (optsOutOfAttach([launch.params?.vmOptions, ...launch.args].join(" "))) return launch;
|
||||
try {
|
||||
const script = await call(commands.runGradleJvmArgsScript([ATTACH_LISTENER]));
|
||||
return { ...launch, args: ["--init-script", script, ...launch.args] };
|
||||
} catch (e) {
|
||||
console.warn("Could not add the attach listener to the Gradle debug run", e);
|
||||
return launch;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Surefire / Failsafe print this banner just before they start the forked test JVM. It comes from
|
||||
* Maven itself, so unlike the fork's "Listening" line it is not held back until the tests run.
|
||||
@@ -102,8 +153,9 @@ function needsPinnedPort(config: RunConfig): boolean {
|
||||
/** Runs `config` with a JDWP agent and attaches the Java debugger to every JVM it starts. */
|
||||
export async function debugWithAgent(config: RunConfig, show?: boolean): Promise<boolean> {
|
||||
const pinned = needsPinnedPort(config) ? await call(commands.debugFreePort()).catch(() => null) : null;
|
||||
const launch = jdwpLaunch(config, pinned ?? undefined);
|
||||
if (!launch) return false;
|
||||
const agent = jdwpLaunch(config, pinned ?? undefined);
|
||||
if (!agent) return false;
|
||||
const launch = await withGradleAttachListener(agent);
|
||||
let sessionId: string | null = null;
|
||||
const waiting: number[] = [];
|
||||
const attached = new Set<number>();
|
||||
|
||||
Reference in New Issue
Block a user