дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
feat(debugger): native Memory View for C/C++ debugging (#6674)
Native debug sessions whose adapter supports readMemory get a read-only Memory View tab: an address or expression field, address/hex/ASCII rows aligned to 16 bytes with the target byte marked, paging and a re-read after each stop. View Memory is offered on variables and watches that carry a memoryReference. The client now declares supportsMemoryReferences in initialize and keeps memoryReference on variables and evaluation results; adapter errors are shown without ending the session. Closes #5921 Co-authored-by: bertyhell <[email protected]>
Цей коміт міститься в:
1 джерело
68abe345cc
коміт
5fb8f21c60
22 files changed
+1311
-11
No files matched your search
@@ -0,0 +1,109 @@
|
||||
.root {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
flex: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-1);
|
||||
padding: 4px 8px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.toolbar .address {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
max-width: 480px;
|
||||
margin-right: var(--space-1);
|
||||
}
|
||||
|
||||
.note,
|
||||
.error {
|
||||
flex: none;
|
||||
padding: 4px 12px;
|
||||
font-size: var(--fs-ui);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
.note {
|
||||
color: var(--fg-subtle);
|
||||
}
|
||||
.error {
|
||||
color: var(--fg);
|
||||
background: var(--bg-feedback-error);
|
||||
border-bottom-color: var(--border-feedback-error);
|
||||
-webkit-user-select: text;
|
||||
user-select: text;
|
||||
}
|
||||
|
||||
.empty {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
/* Every column is in the code font, so the hex and the ASCII of a row line up with the rows above. */
|
||||
.grid {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
padding: 4px 0;
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--fs-code);
|
||||
line-height: var(--lh-code);
|
||||
white-space: pre;
|
||||
-webkit-user-select: text;
|
||||
user-select: text;
|
||||
}
|
||||
.dimmed {
|
||||
color: var(--fg-muted);
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
gap: 2ch;
|
||||
padding: 0 12px;
|
||||
width: max-content;
|
||||
}
|
||||
.head {
|
||||
position: sticky;
|
||||
top: -4px;
|
||||
color: var(--fg-subtle);
|
||||
background: var(--bg-app);
|
||||
}
|
||||
.row:not(.head):hover {
|
||||
background: var(--bg-hover);
|
||||
}
|
||||
|
||||
.row > .address {
|
||||
color: var(--fg-subtle);
|
||||
min-width: 18ch;
|
||||
}
|
||||
|
||||
.hex {
|
||||
display: flex;
|
||||
gap: 1ch;
|
||||
}
|
||||
/* A wider gap halfway, like a hex editor's two groups of eight. */
|
||||
.hex > .byte:nth-child(8) {
|
||||
margin-right: 1ch;
|
||||
}
|
||||
|
||||
/* Spaces and dots stand for bytes, so keep every one. */
|
||||
.ascii {
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
.target {
|
||||
color: var(--fg-strong);
|
||||
background: var(--bg-selected);
|
||||
border-radius: 2px;
|
||||
}
|
||||
.unreadable {
|
||||
color: var(--fg-faint);
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/** Memory View tab of the Debug tool window: a native program's memory as address, hex and ASCII columns. */
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import clsx from "clsx";
|
||||
import { EmptyState, IconButton, Input, T } from "@/ui";
|
||||
import { formatMessage, useTx } from "@/platform/i18n";
|
||||
import type { DebugSession } from "../state";
|
||||
import {
|
||||
ROW_BYTES,
|
||||
addressWidth,
|
||||
asciiChar,
|
||||
formatAddress,
|
||||
hexByte,
|
||||
memoryRows,
|
||||
parseAddress,
|
||||
type MemoryRow,
|
||||
} from "./bytes";
|
||||
import { goToExpression, readPage, turnPage, useMemoryView } from "./memoryViewState";
|
||||
import styles from "./MemoryViewPane.module.css";
|
||||
|
||||
const COLUMN_OFFSETS = Array.from({ length: ROW_BYTES }, (_, i) => i.toString(16).toUpperCase().padStart(2, "0"));
|
||||
|
||||
function Row({ row, width, target }: { row: MemoryRow; width: number; target: bigint | null }) {
|
||||
const marked = target !== null && target >= row.address && target < row.address + BigInt(ROW_BYTES);
|
||||
const at = marked ? Number(target - row.address) : -1;
|
||||
return (
|
||||
<div className={styles.row} role="row">
|
||||
<span role="rowheader" className={styles.address}>
|
||||
{formatAddress(row.address, width)}
|
||||
</span>
|
||||
<span role="gridcell" className={styles.hex}>
|
||||
{COLUMN_OFFSETS.map((_, i) => {
|
||||
const byte = row.bytes[i];
|
||||
return (
|
||||
<span key={i} className={clsx(styles.byte, i === at && styles.target, byte === null && styles.unreadable)}>
|
||||
{byte === undefined ? " " : hexByte(byte)}
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
</span>
|
||||
<span role="gridcell" className={styles.ascii}>
|
||||
{row.bytes.map((byte, i) => (
|
||||
<span key={i} className={clsx(i === at && styles.target, byte === null && styles.unreadable)}>
|
||||
{asciiChar(byte)}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function MemoryViewPane({ session }: { session: DebugSession }) {
|
||||
const tx = useTx();
|
||||
const id = session.id;
|
||||
const entry = useMemoryView((s) => s.bySession[id]);
|
||||
const [text, setText] = useState(entry?.expression ?? "");
|
||||
const committed = entry?.expression ?? "";
|
||||
// View Memory on a variable fills the field with its expression.
|
||||
const [shown, setShown] = useState(committed);
|
||||
if (shown !== committed) {
|
||||
setShown(committed);
|
||||
setText(committed);
|
||||
}
|
||||
const paused = session.status === "paused";
|
||||
|
||||
// The memory may have changed at each stop: the page is read again (also when the tab comes back).
|
||||
const stale = !!entry?.reference && paused && !entry.loading && entry.readAtStop !== session.stops;
|
||||
useEffect(() => {
|
||||
if (stale) void readPage(id);
|
||||
}, [id, stale]);
|
||||
|
||||
const block = entry?.block ?? null;
|
||||
const rows = useMemo(() => {
|
||||
const start = block ? parseAddress(block.address) : null;
|
||||
return block && start !== null ? memoryRows(start, block.data, block.unreadableBytes) : [];
|
||||
}, [block]);
|
||||
const width = addressWidth(rows);
|
||||
const busy = !!entry?.loading;
|
||||
const canPage = paused && !!entry?.reference && !busy;
|
||||
|
||||
return (
|
||||
<div className={styles.root} aria-busy={busy || undefined}>
|
||||
<form
|
||||
className={styles.toolbar}
|
||||
role="toolbar"
|
||||
aria-label={tx("Memory View")}
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
void goToExpression(id, text);
|
||||
}}
|
||||
>
|
||||
<Input
|
||||
mono
|
||||
className={styles.address}
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
placeholder="Address or expression, such as &buffer[0]"
|
||||
aria-label="Address or expression"
|
||||
disabled={!paused}
|
||||
/>
|
||||
<IconButton
|
||||
icon="arrow-right"
|
||||
type="submit"
|
||||
title="Go to Address"
|
||||
size="small"
|
||||
disabled={!paused || !text.trim()}
|
||||
/>
|
||||
<IconButton
|
||||
icon="chevron-up"
|
||||
title="Previous Page"
|
||||
size="small"
|
||||
disabled={!canPage}
|
||||
onClick={() => void turnPage(id, -1)}
|
||||
/>
|
||||
<IconButton
|
||||
icon="chevron-down"
|
||||
title="Next Page"
|
||||
size="small"
|
||||
disabled={!canPage}
|
||||
onClick={() => void turnPage(id, 1)}
|
||||
/>
|
||||
<IconButton icon="refresh" title="Refresh" size="small" disabled={!canPage} onClick={() => void readPage(id)} />
|
||||
</form>
|
||||
{!paused && (
|
||||
<div className={styles.note} role="status">
|
||||
<T>Memory is read while the program is paused.</T>
|
||||
</div>
|
||||
)}
|
||||
{entry?.error && (
|
||||
<div className={styles.error} role="alert">
|
||||
{entry.error}
|
||||
</div>
|
||||
)}
|
||||
{block && block.data.length === 0 && (
|
||||
<div className={styles.error} role="alert">
|
||||
{formatMessage(tx("The memory at {address} cannot be read."), { address: block.address })}
|
||||
</div>
|
||||
)}
|
||||
{rows.length > 0 ? (
|
||||
<div className={clsx(styles.grid, !paused && styles.dimmed)} role="grid" aria-label={tx("Memory")}>
|
||||
<div className={clsx(styles.row, styles.head)} role="row">
|
||||
<span role="columnheader" className={styles.address}>
|
||||
{tx("Address")}
|
||||
</span>
|
||||
<span role="columnheader" className={styles.hex}>
|
||||
{COLUMN_OFFSETS.map((o) => (
|
||||
<span key={o} className={styles.byte}>
|
||||
{o}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
<span role="columnheader" className={styles.ascii}>
|
||||
{tx("ASCII")}
|
||||
</span>
|
||||
</div>
|
||||
{rows.map((row) => (
|
||||
<Row key={row.address.toString()} row={row} width={width} target={entry?.target ?? null} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
!entry?.error &&
|
||||
!busy && (
|
||||
<div className={styles.empty}>
|
||||
<EmptyState title="Memory View">
|
||||
<T>Enter an address or an expression, or choose View Memory on a variable.</T>
|
||||
</EmptyState>
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/** Rows of the native Memory View: aligned address, hexadecimal bytes and their ASCII text. */
|
||||
|
||||
/** Bytes per row; rows start at addresses that are multiples of it. */
|
||||
export const ROW_BYTES = 16;
|
||||
/** Bytes one page of the view reads. */
|
||||
export const PAGE_BYTES = 256;
|
||||
|
||||
export interface MemoryRow {
|
||||
/** Address of the row's first byte. */
|
||||
address: bigint;
|
||||
/** `null`: a byte the debugger could not read. */
|
||||
bytes: (number | null)[];
|
||||
}
|
||||
|
||||
/**
|
||||
* An address as DAP writes it (`readMemory`'s `address`: hexadecimal with `0x`, decimal otherwise),
|
||||
* or the address at the start of a value (`0x7ffd5c4a "Hello"`, `140737488346985`). `null` when the
|
||||
* text doesn't start with one.
|
||||
*/
|
||||
export function parseAddress(text: string): bigint | null {
|
||||
const m = /^\s*(0[xX][0-9a-fA-F]+|\d+)(?![\w.])/.exec(text);
|
||||
if (!m) return null;
|
||||
try {
|
||||
return BigInt(m[1]!);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** How many bytes `address` lies past the start of its row. */
|
||||
export function rowOffset(address: bigint): number {
|
||||
return Number(address % BigInt(ROW_BYTES));
|
||||
}
|
||||
|
||||
/** The bytes of a read that starts at `address`, followed by `unreadable` bytes that could not be read. */
|
||||
export function memoryRows(address: bigint, data: readonly number[], unreadable: number): MemoryRow[] {
|
||||
const bytes: (number | null)[] = [...data, ...Array<null>(unreadable).fill(null)];
|
||||
const rows: MemoryRow[] = [];
|
||||
for (let i = 0; i < bytes.length; i += ROW_BYTES) {
|
||||
rows.push({ address: address + BigInt(i), bytes: bytes.slice(i, i + ROW_BYTES) });
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** Hex digits the address column shows: 8 for 32-bit addresses, 16 once one doesn't fit. */
|
||||
export function addressWidth(rows: readonly MemoryRow[]): number {
|
||||
const last = rows.at(-1);
|
||||
return last && last.address + BigInt(ROW_BYTES - 1) > 0xffffffffn ? 16 : 8;
|
||||
}
|
||||
|
||||
export function formatAddress(address: bigint, width: number): string {
|
||||
return `0x${address.toString(16).toUpperCase().padStart(width, "0")}`;
|
||||
}
|
||||
|
||||
/** `48`, or `??` for a byte that could not be read. */
|
||||
export function hexByte(byte: number | null): string {
|
||||
return byte === null ? "??" : byte.toString(16).toUpperCase().padStart(2, "0");
|
||||
}
|
||||
|
||||
/** The byte as printable ASCII; `.` for any other byte, ` ` for one that could not be read. */
|
||||
export function asciiChar(byte: number | null): string {
|
||||
if (byte === null) return " ";
|
||||
return byte >= 0x20 && byte < 0x7f ? String.fromCharCode(byte) : ".";
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/** Native Memory View (DAP `readMemory`): what each session's view shows, and reading it. */
|
||||
import { create } from "zustand";
|
||||
import { call, commands, type DebugSessionId, type MemoryBlock } from "@/platform/ipc";
|
||||
import { formatMessage, tx } from "@/platform/i18n";
|
||||
import { evaluate, useDebug } from "../state";
|
||||
import { PAGE_BYTES, parseAddress, rowOffset } from "./bytes";
|
||||
|
||||
export interface MemoryViewEntry {
|
||||
/** What the address field shows. */
|
||||
expression: string;
|
||||
/** The memory reference pages are read relative to: an evaluation result's or a variable's. */
|
||||
reference: string | null;
|
||||
/** Address of `reference` once known: the byte the view marks. */
|
||||
target: bigint | null;
|
||||
/** Offset from `reference` of the page shown; it starts a row. */
|
||||
start: number;
|
||||
block: MemoryBlock | null;
|
||||
error: string | null;
|
||||
loading: boolean;
|
||||
/** The session's `stops` when `block` (or `error`) was read: a later stop reads the page again. */
|
||||
readAtStop: number;
|
||||
}
|
||||
|
||||
interface MemoryViewState {
|
||||
bySession: Record<DebugSessionId, MemoryViewEntry>;
|
||||
/** Set by View Memory until the Debug tool window has brought the Memory View to the front. */
|
||||
revealPending: boolean;
|
||||
}
|
||||
|
||||
export const useMemoryView = create<MemoryViewState>(() => ({ bySession: {}, revealPending: false }));
|
||||
|
||||
const emptyEntry = (): MemoryViewEntry => ({
|
||||
expression: "",
|
||||
reference: null,
|
||||
target: null,
|
||||
start: 0,
|
||||
block: null,
|
||||
error: null,
|
||||
loading: false,
|
||||
readAtStop: -1,
|
||||
});
|
||||
|
||||
/** The read each session waits for; a newer one (or `forgetMemoryView`) makes an older reply stale. */
|
||||
const inflight = new Map<DebugSessionId, symbol>();
|
||||
|
||||
function patchEntry(id: DebugSessionId, patch: Partial<MemoryViewEntry>) {
|
||||
useMemoryView.setState((s) => ({
|
||||
bySession: { ...s.bySession, [id]: { ...(s.bySession[id] ?? emptyEntry()), ...patch } },
|
||||
}));
|
||||
}
|
||||
|
||||
const message = (e: unknown) => (e instanceof Error ? e.message : String(e));
|
||||
|
||||
/**
|
||||
* Reads the page at the entry's `start`. Before the reference's address is known (references
|
||||
* are opaque in DAP), a first read at the reference itself tells it, and the page then moves back
|
||||
* to the start of its row.
|
||||
*/
|
||||
export async function readPage(id: DebugSessionId): Promise<void> {
|
||||
const entry = useMemoryView.getState().bySession[id];
|
||||
const session = useDebug.getState().sessions[id];
|
||||
if (!entry?.reference || !session || session.status !== "paused") return;
|
||||
const reference = entry.reference;
|
||||
const token = Symbol("memory view");
|
||||
inflight.set(id, token);
|
||||
const stop = session.stops;
|
||||
patchEntry(id, { loading: true });
|
||||
const read = (offset: number) => call(commands.debugReadMemory(id, reference, offset, PAGE_BYTES));
|
||||
try {
|
||||
let { target, start } = entry;
|
||||
let block = await read(start);
|
||||
if (target === null && inflight.get(id) === token) {
|
||||
target = parseAddress(block.address);
|
||||
const back = target === null ? 0 : rowOffset(target);
|
||||
if (back) block = await read((start = -back));
|
||||
}
|
||||
if (inflight.get(id) !== token) return;
|
||||
patchEntry(id, { target, start, block, error: null, readAtStop: stop });
|
||||
} catch (e) {
|
||||
if (inflight.get(id) !== token) return;
|
||||
patchEntry(id, { block: null, error: message(e), readAtStop: stop });
|
||||
} finally {
|
||||
if (inflight.get(id) === token) {
|
||||
inflight.delete(id);
|
||||
patchEntry(id, { loading: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Shows the memory at `reference` (a variable's, or an evaluation result's); the address field shows `expression`. */
|
||||
export function showMemoryAt(id: DebugSessionId, reference: string, expression: string): Promise<void> {
|
||||
// Adapters that write references as addresses (lldb-dap, CodeLLDB, cppdbg, GDB) need no read to align the page.
|
||||
const target = parseAddress(reference);
|
||||
patchEntry(id, {
|
||||
expression,
|
||||
reference,
|
||||
target,
|
||||
start: target === null ? 0 : 0 - rowOffset(target),
|
||||
block: null,
|
||||
error: null,
|
||||
});
|
||||
return readPage(id);
|
||||
}
|
||||
|
||||
/** Address field: evaluates `expression` (`&msg[0]`, `buffer`, `0x7ffd5c4a`) and shows the memory it points to. */
|
||||
export async function goToExpression(id: DebugSessionId, expression: string): Promise<void> {
|
||||
const text = expression.trim();
|
||||
const session = useDebug.getState().sessions[id];
|
||||
if (!text || !session) return;
|
||||
patchEntry(id, { expression: text });
|
||||
let reference: string | null;
|
||||
try {
|
||||
const value = await evaluate(text, "watch", session);
|
||||
// A plain number (`0x601040`, an integer variable) has no reference: its value is the address.
|
||||
const address = value.memoryReference ? null : parseAddress(value.value);
|
||||
reference = value.memoryReference ?? (address === null ? null : `0x${address.toString(16)}`);
|
||||
if (!reference) {
|
||||
patchEntry(id, {
|
||||
reference: null,
|
||||
block: null,
|
||||
error: formatMessage(
|
||||
tx("“{expression}” is not an address. Enter a pointer or an address, such as &buffer[0]."),
|
||||
{
|
||||
expression: text,
|
||||
},
|
||||
),
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
patchEntry(id, { reference: null, block: null, error: message(e) });
|
||||
return;
|
||||
}
|
||||
if (useMemoryView.getState().bySession[id]?.expression !== text) return;
|
||||
await showMemoryAt(id, reference, text);
|
||||
}
|
||||
|
||||
/** Previous (-1) or next (1) page. */
|
||||
export function turnPage(id: DebugSessionId, pages: number): Promise<void> {
|
||||
const entry = useMemoryView.getState().bySession[id];
|
||||
if (!entry?.reference) return Promise.resolve();
|
||||
patchEntry(id, { start: entry.start + pages * PAGE_BYTES });
|
||||
return readPage(id);
|
||||
}
|
||||
|
||||
export function revealMemoryView(): void {
|
||||
useMemoryView.setState({ revealPending: true });
|
||||
}
|
||||
|
||||
/** The Debug tool window shows the Memory View: the request is done. */
|
||||
export function memoryViewRevealed(): void {
|
||||
useMemoryView.setState({ revealPending: false });
|
||||
}
|
||||
|
||||
export function forgetMemoryView(id: DebugSessionId): void {
|
||||
inflight.delete(id);
|
||||
useMemoryView.setState((s) => {
|
||||
if (!(id in s.bySession)) return s;
|
||||
const { [id]: _gone, ...rest } = s.bySession;
|
||||
return { bySession: rest };
|
||||
});
|
||||
}
|
||||
Reference in new issue
Заблокувати користувача