Skip to content

Commit 8a72074

Browse files
committed
Augment disassembly with source information
1 parent 8db294e commit 8a72074

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

src/cc65Debug.ts

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import * as path from "node:path";
1616
import { Logger, LoggingDebugSession, TerminatedEvent, logger } from "@vscode/debugadapter";
1717
import type { DebugProtocol } from "@vscode/debugprotocol";
1818
import type { DebugSession } from "vscode";
19-
import { type DbgMap, addressToSpans, readDebugFile, spansToSpanLines } from "./dbgService";
19+
import {
20+
type DbgFile,
21+
type DbgMap,
22+
addressToSpans,
23+
readDebugFile,
24+
spansToSpanLines,
25+
} from "./dbgService";
2026
import { normalizePath } from "./utils";
2127

2228
export enum ErrorCodes {
@@ -296,22 +302,63 @@ export class Cc65DebugSession extends LoggingDebugSession {
296302
frame.line = dbgLine.line;
297303
const dbgFile = this._debugData.file.find((f) => f.id === dbgLine.file);
298304
if (dbgFile) {
299-
const fileName = normalizePath(dbgFile.name);
300-
for (const base of this._debugPathBases) {
301-
if (fileName.startsWith(base)) {
302-
const name = fileName.slice(base.length);
303-
frame.source = {
304-
name,
305+
const wsFile = this.dbgFile2workspace(dbgFile);
306+
if (wsFile) {
307+
frame.source = {
308+
name: wsFile,
309+
path: path.resolve(
310+
this._session.workspaceFolder?.uri.fsPath || ".",
311+
wsFile,
312+
),
313+
presentationHint: "emphasize",
314+
};
315+
}
316+
}
317+
}
318+
}
319+
320+
return this.sendResponse(result);
321+
}
322+
case "disassemble": {
323+
const result = response as DebugProtocol.DisassembleResponse;
324+
const { instructions = [] } = result.body || {};
325+
326+
if (!this._debugData) {
327+
throw new Error("Cannot work without loaded .dbg file");
328+
}
329+
330+
for (const instruction of instructions) {
331+
if (instruction.presentationHint !== "invalid" && instruction.address != null) {
332+
const address = instruction.address.startsWith("0x")
333+
? Number.parseInt(instruction.address.slice(2), 16)
334+
: Number.parseInt(instruction.address, 10);
335+
336+
const spans = addressToSpans(this._debugData, address, true);
337+
const lines = spansToSpanLines(this._debugData, spans);
338+
const dbgLine = lines[0]?.line;
339+
if (dbgLine) {
340+
instruction.line = dbgLine.line;
341+
const dbgFile = this._debugData.file.find((f) => f.id === dbgLine.file);
342+
if (dbgFile) {
343+
const wsFile = this.dbgFile2workspace(dbgFile);
344+
if (wsFile) {
345+
instruction.location = {
346+
name: wsFile,
305347
path: path.resolve(
306348
this._session.workspaceFolder?.uri.fsPath || ".",
307-
name,
349+
wsFile,
308350
),
309-
presentationHint: "emphasize",
310351
};
311-
break;
312352
}
313353
}
314354
}
355+
356+
const dbgSym = this._debugData?.sym.find(
357+
({ val }) => address === Number(val),
358+
);
359+
if (dbgSym) {
360+
instruction.symbol = dbgSym.name;
361+
}
315362
}
316363
}
317364

@@ -590,4 +637,13 @@ export class Cc65DebugSession extends LoggingDebugSession {
590637

591638
return this.sendMessage(request);
592639
}
640+
641+
private dbgFile2workspace(dbgFile: DbgFile) {
642+
const fileName = normalizePath(dbgFile.name);
643+
for (const base of this._debugPathBases) {
644+
if (fileName.startsWith(base)) {
645+
return fileName.slice(base.length);
646+
}
647+
}
648+
}
593649
}

0 commit comments

Comments
 (0)