/** 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 (
{formatAddress(row.address, width)} {COLUMN_OFFSETS.map((_, i) => { const byte = row.bytes[i]; return ( {byte === undefined ? " " : hexByte(byte)} ); })} {row.bytes.map((byte, i) => ( {asciiChar(byte)} ))}
); } 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 (
{ e.preventDefault(); void goToExpression(id, text); }} > setText(e.target.value)} placeholder="Address or expression, such as &buffer[0]" aria-label="Address or expression" disabled={!paused} /> void turnPage(id, -1)} /> void turnPage(id, 1)} /> void readPage(id)} /> {!paused && (
Memory is read while the program is paused.
)} {entry?.error && (
{entry.error}
)} {block && block.data.length === 0 && (
{formatMessage(tx("The memory at {address} cannot be read."), { address: block.address })}
)} {rows.length > 0 ? (
{tx("Address")} {COLUMN_OFFSETS.map((o) => ( {o} ))} {tx("ASCII")}
{rows.map((row) => ( ))}
) : ( !entry?.error && !busy && (
Enter an address or an expression, or choose View Memory on a variable.
) )}
); }