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 <[email protected]>
Цей коміт міститься в:
ntbang0901 authored and GitHub committed 2026-11-11 17:10:21 +08:00
1 джерело 0ff3f188de
коміт a3d1e57f3e
125 files changed
+16949 -307

No files matched your search

+69 -8
Переглянути файл
@@ -3,10 +3,13 @@ import type { JavaLaunchInfo, RunConfig } from "@/platform/ipc";
import {
formatCompileErrors,
joinJavaArgs,
JDWP_AGENT,
jdwpListeningPort,
optsOutOfAttach,
parseJavaArgs,
resolveJavaCommand,
} from "@/platform/javaLaunch";
import { usePlatform } from "@/platform/platformInfo";
import { useProject } from "@/platform/project";
import { applicationConfig, jvmModuleDir, jvmTestConfig } from "./application";
import { buildRows } from "./ConfigChooser";
@@ -335,14 +338,21 @@ describe("debugging build tool configurations", () => {
it("adds a waiting JDWP agent to spring-boot:run, Surefire and Gradle", () => {
const boot = jdwpLaunch(cfg({ id: "b", kind: "maven", args: ["spring-boot:run"] }))!;
expect(boot.args[1]).toMatch(
/^-Dspring-boot\.run\.jvmArguments=-agentlib:jdwp=.*suspend=y.*address=127\.0\.0\.1:0$/,
/^-Dspring-boot\.run\.jvmArguments=-XX:\+StartAttachListener -agentlib:jdwp=.*suspend=y.*address=127\.0\.0\.1:0$/,
);
const merged = jdwpLaunch(
cfg({ id: "b", kind: "maven", args: ["spring-boot:run", "-Dspring-boot.run.jvmArguments=-Xmx1g"] }),
)!;
expect(merged.args[1]).toMatch(/^-Dspring-boot\.run\.jvmArguments=-Xmx1g -agentlib:jdwp=/);
expect(merged.args[1]).toMatch(
/^-Dspring-boot\.run\.jvmArguments=-Xmx1g -XX:\+StartAttachListener -agentlib:jdwp=/,
);
const test = jdwpLaunch(cfg({ id: "t", kind: "maven", args: ["test", "-Dtest=A#b"], isTest: true }))!;
expect(test.args.some((a) => a.startsWith("-Dmaven.surefire.debug=-agentlib:jdwp="))).toBe(true);
expect(test.args.some((a) => a.startsWith("-Dmaven.surefire.debug=-XX:+StartAttachListener -agentlib:jdwp="))).toBe(
true,
);
const failsafe = jdwpLaunch(cfg({ id: "v", kind: "maven", args: ["verify"] }))!;
for (const key of ["maven.surefire.debug", "maven.failsafe.debug"])
expect(failsafe.args.some((a) => a.startsWith(`-D${key}=-XX:+StartAttachListener -agentlib:jdwp=`))).toBe(true);
// A fork's "Listening for transport" line stays buffered until tests run — which suspend=y
// prevents — so a Maven test's port is pinned and attached without the line.
const pinned = jdwpLaunch(cfg({ id: "t2", kind: "maven", args: ["test"], isTest: true }), 50123)!;
@@ -370,14 +380,14 @@ describe("debugging build tool configurations", () => {
])("debugs Maven's own JVM for the in-process goal %s", (goal) => {
const out = jdwpLaunch(cfg({ id: "m", kind: "maven", args: ["clean", goal], env: { MAVEN_OPTS: "-Xmx1g" } }))!;
expect(out.args).toEqual(["clean", goal]);
expect(out.env.MAVEN_OPTS).toMatch(/^-Xmx1g -agentlib:jdwp=/);
expect(out.env.MAVEN_OPTS).toMatch(/^-Xmx1g -XX:\+StartAttachListener -agentlib:jdwp=/);
expect(out.env.MAVEN_OPTS).toMatch(AGENT);
});
it("honours Rebuild on a Maven debug command", () => {
const out = jdwpLaunch(cfg({ id: "r", kind: "maven", args: ["spring-boot:run"], buildPolicy: "rebuild" }))!;
expect(out.args[0]).toBe("clean");
expect(out.args.at(-1)).toMatch(/^-Dspring-boot\.run\.jvmArguments=-agentlib:jdwp=/);
expect(out.args.at(-1)).toMatch(/^-Dspring-boot\.run\.jvmArguments=-XX:\+StartAttachListener -agentlib:jdwp=/);
});
it("keeps plain Maven builds out of the debugger unless set to the Java debugger", () => {
@@ -395,23 +405,74 @@ describe("debugging build tool configurations", () => {
const out = jdwpLaunch(rc)!;
expect(out.command).toBe(command);
expect(out.args).toEqual(args);
expect(out.env.JAVA_TOOL_OPTIONS).toMatch(/^-XX:\+StartAttachListener -agentlib:jdwp=/);
expect(out.env.JAVA_TOOL_OPTIONS).toMatch(AGENT);
const merged = jdwpLaunch({ ...rc, env: { JAVA_TOOL_OPTIONS: "-Dfile.encoding=UTF-8" } })!;
expect(merged.env.JAVA_TOOL_OPTIONS).toMatch(/^-Dfile\.encoding=UTF-8 -agentlib:jdwp=/);
expect(merged.env.JAVA_TOOL_OPTIONS).toMatch(/^-Dfile\.encoding=UTF-8 -XX:\+StartAttachListener -agentlib:jdwp=/);
});
it("debugs a single Java file: the agent before the file, or for java only on a javac line", () => {
const source = jdwpLaunch(cfg({ id: "f", kind: "java-file", command: "/bin/java", args: ["Hi.java"] }))!;
expect(source.args).toEqual([expect.stringMatching(AGENT), "Hi.java"]);
expect(source.args).toEqual(["-XX:+StartAttachListener", expect.stringMatching(AGENT), "Hi.java"]);
const line = "mkdir -p /tmp/c && /bin/javac -g -d /tmp/c Test.java && /bin/java -cp /tmp/c Test";
const compiled = jdwpLaunch(
cfg({ id: "f", kind: "java-file", command: line, env: { JDK_JAVA_OPTIONS: "-Xmx1g" } }),
)!;
expect(compiled).toMatchObject({ command: line, args: [] });
expect(compiled.env.JDK_JAVA_OPTIONS).toMatch(/^-Xmx1g -agentlib:jdwp=/);
expect(compiled.env.JDK_JAVA_OPTIONS).toMatch(/^-Xmx1g -XX:\+StartAttachListener -agentlib:jdwp=/);
expect(compiled.env.JAVA_TOOL_OPTIONS).toBeUndefined();
});
it("adds the agent without the attach listener when the JVM runs on Windows", () => {
const os = usePlatform.getState().os;
usePlatform.setState({ os: "windows" });
try {
const script = cfg({ id: "s", kind: "shell", command: "run.cmd", debugAdapter: "java-debug" });
expect(jdwpLaunch(script)!.env.JAVA_TOOL_OPTIONS).toBe(JDWP_AGENT);
const boot = jdwpLaunch(cfg({ id: "b", kind: "maven", args: ["spring-boot:run"] }))!;
expect(boot.args[1]).toBe(`-Dspring-boot.run.jvmArguments=${JDWP_AGENT}`);
const test = jdwpLaunch(cfg({ id: "t", kind: "maven", args: ["verify"] }))!;
expect(test.args).toEqual([
"verify",
`-Dmaven.surefire.debug=${JDWP_AGENT}`,
`-Dmaven.failsafe.debug=${JDWP_AGENT}`,
]);
const maven = jdwpLaunch(cfg({ id: "m", kind: "maven", args: ["jetty:run"] }))!;
expect(maven.env.MAVEN_OPTS).toBe(JDWP_AGENT);
const file = jdwpLaunch(cfg({ id: "f", kind: "java-file", command: "java", args: ["Hi.java"] }))!;
expect(file.args).toEqual([JDWP_AGENT, "Hi.java"]);
} finally {
usePlatform.setState({ os });
}
});
it("keeps the user's attach opt-out: the agent is still added, the listener is not", () => {
const off = "-XX:+DisableAttachMechanism";
const script = cfg({ id: "s", kind: "shell", command: "./run.sh", debugAdapter: "java-debug" });
expect(jdwpLaunch({ ...script, env: { JAVA_TOOL_OPTIONS: off } })!.env.JAVA_TOOL_OPTIONS).toBe(
`${off} ${JDWP_AGENT}`,
);
const maven = cfg({ id: "m", kind: "maven", args: ["jetty:run"], env: { MAVEN_OPTS: "-XX:-StartAttachListener" } });
expect(jdwpLaunch(maven)!.env.MAVEN_OPTS).toBe(`-XX:-StartAttachListener ${JDWP_AGENT}`);
const javaFile = cfg({ id: "j", kind: "java-file", command: "java", env: { JDK_JAVA_OPTIONS: off } });
expect(jdwpLaunch(javaFile)!.env.JDK_JAVA_OPTIONS).toBe(`${off} ${JDWP_AGENT}`);
const file = cfg({ id: "f", kind: "java-file", command: "java", args: [off, "Hi.java"] });
expect(jdwpLaunch(file)!.args).toEqual([JDWP_AGENT, off, "Hi.java"]);
const boot = cfg({ id: "b", kind: "maven", args: ["spring-boot:run", `-Dspring-boot.run.jvmArguments=${off}`] });
expect(jdwpLaunch(boot)!.args[1]).toBe(`-Dspring-boot.run.jvmArguments=${off} ${JDWP_AGENT}`);
const surefire = jdwpLaunch(cfg({ id: "t", kind: "maven", args: ["test", `-DargLine=${off}`] }))!;
expect(surefire.args).toEqual(["test", `-DargLine=${off}`, `-Dmaven.surefire.debug=${JDWP_AGENT}`]);
// VM options of the configuration count as well as the channel's own value.
const withVm = jdwpLaunch({ ...script, params: { vmOptions: off } })!;
expect(withVm.env.JAVA_TOOL_OPTIONS).toBe(JDWP_AGENT);
});
it("detects VM options that decide about the attach mechanism", () => {
for (const text of ["-XX:+StartAttachListener", "-XX:-StartAttachListener", "-Xmx1g -XX:+DisableAttachMechanism"])
expect(optsOutOfAttach(text)).toBe(true);
for (const text of ["", "-Xmx1g -agentlib:jdwp=transport=dt_socket"]) expect(optsOutOfAttach(text)).toBe(false);
});
it("leaves Application configurations and other debuggers alone", () => {
for (const kind of ["application", "spring-boot"])
expect(jdwpLaunch(cfg({ id: "a", kind, command: "java", debugAdapter: "java-debug" }))).toBeNull();