7.1 断点、步进与调用栈
调试器的核心不是界面,而是在哪一行停下来的判定:断点按文件加行号命中;步进三种模式各自给下一次到达设定停止条件——step in 任意新帧都停,step over 只在同层或更浅处停,step out 只在更浅处停。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 运行时 | 《游戏引擎实现原理》 | 引擎提供执行环境;本节在其上建立断点与步进语义 |
| Play 模式 | 《视图与子编辑器》 第 7 章 | 卷三做快照与运行态检查;本节做暂停与单步 |
| 全卷脉络 | 本卷目录 | 本节是调试器章的第一块 |
从零实现:断点、步进与调用栈
调用栈用数组,push 到达新行、pop 返回上层。停止规则记录模式与起点深度:in 无条件命中,over 在深度不大于起点时命中,out 在深度小于起点时命中。未命中时规则保留,下一次到达继续判定;命中后清除,避免停两次。
对标调试设施
Unreal 的 Blueprint Debugger 提供断点、单步和调用栈。helio 示例用显式栈深度和位置判断是否暂停。真实调试器还需由运行时在可暂停位置调用这套逻辑。 Unreal Blueprint Debugger
可运行产物:6 条用例
下面是 src/debugger.ts,层级 L1,完整可编译。运行命令 node --test test/07-1-breakpoint-step-callstack.test.ts:
// file: src/debugger.ts
// tier: L1
// verify: node --test test/07-1-breakpoint-step-callstack.test.ts
//
// 卷四(第七章):调试器集成——断点、步进与调用栈。
// 调试器的核心不是界面,而是「在哪一行停下来」的判定:断点按文件+行号
// 命中;步进三种模式各自给下一次到达设定停止条件——step in 任意新帧都停,
// step over 只在同层或更浅处停,step out 只在更浅处停。
export type StepMode = 'in' | 'over' | 'out';
export interface Frame {
file: string;
line: number;
name: string;
}
interface StopRule {
mode: StepMode;
depth: number;
}
export class Debugger {
#breakpoints = new Map<string, Set<number>>();
#stack: Frame[] = [];
#stop: StopRule | null = null;
#paused = false;
setBreakpoint(file: string, line: number): void {
const lines = this.#breakpoints.get(file) ?? new Set<number>();
lines.add(line);
this.#breakpoints.set(file, lines);
}
removeBreakpoint(file: string, line: number): boolean {
return this.#breakpoints.get(file)?.delete(line) ?? false;
}
hasBreakpoint(file: string, line: number): boolean {
return this.#breakpoints.get(file)?.has(line) ?? false;
}
/** 到达新一行(压入调用帧);返回是否在此暂停。 */
push(frame: Frame): boolean {
this.#stack.push({ ...frame });
const depth = this.#stack.length;
let stop = this.#breakpoints.get(frame.file)?.has(frame.line) ?? false;
if (this.#stop) {
const rule = this.#stop;
const reached =
rule.mode === 'in'
? true
: rule.mode === 'over'
? depth <= rule.depth
: depth < rule.depth;
// 未到达停止层时保留规则,下一次到达继续判定。
if (reached) {
stop = true;
this.#stop = null;
}
}
this.#paused = stop;
return stop;
}
pop(): Frame | undefined {
return this.#stack.pop();
}
depth(): number {
return this.#stack.length;
}
isPaused(): boolean {
return this.#paused;
}
stepIn(): void {
this.#stop = { mode: 'in', depth: this.#stack.length };
}
stepOver(): void {
this.#stop = { mode: 'over', depth: this.#stack.length };
}
stepOut(): void {
this.#stop = { mode: 'out', depth: this.#stack.length };
}
}
push 同时承担「到达新行」与「进入调用」两个语义,简化了模型;真实调试器里还有同一帧内的行移动,但停止条件仍然可以用深度加规则表达。规则延迟清除是关键,否则 step out 在第一次进入更深的帧时就被消耗掉。
下面是 test/07-1-breakpoint-step-callstack.test.ts,层级 L1:
// file: test/07-1-breakpoint-step-callstack.test.ts
// tier: L1
// verify: node --test test/07-1-breakpoint-step-callstack.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Debugger } from '../src/debugger.ts';
const main = (line: number) => ({ file: 'main.ts', line, name: 'main' });
test('断点在指定文件行命中', () => {
const d = new Debugger();
d.setBreakpoint('main.ts', 12);
assert.equal(d.push(main(12)), true);
assert.equal(d.isPaused(), true);
});
test('非断点行继续执行', () => {
const d = new Debugger();
d.setBreakpoint('main.ts', 12);
assert.equal(d.push(main(11)), false);
assert.equal(d.isPaused(), false);
});
test('step in 在下一帧暂停', () => {
const d = new Debugger();
d.push(main(5));
d.stepIn();
assert.equal(d.push({ file: 'other.ts', line: 1, name: 'helper' }), true);
});
test('step over 跳过被调帧、回到同层暂停', () => {
const d = new Debugger();
d.push(main(5));
d.push({ file: 'other.ts', line: 1, name: 'helper' });
d.stepOver();
assert.equal(d.push({ file: 'other.ts', line: 2, name: 'deeper' }), false);
d.pop();
d.pop();
assert.equal(d.push(main(6)), true);
});
test('step out 返回更浅层时暂停', () => {
const d = new Debugger();
d.push(main(5));
d.push({ file: 'other.ts', line: 1, name: 'helper' });
d.stepOut();
assert.equal(d.push({ file: 'other.ts', line: 2, name: 'deeper' }), false);
d.pop();
d.pop();
d.pop();
assert.equal(d.push(main(6)), true);
});
test('移除断点后不再命中且调用栈深度正确', () => {
const d = new Debugger();
d.setBreakpoint('main.ts', 3);
assert.equal(d.removeBreakpoint('main.ts', 3), true);
assert.equal(d.removeBreakpoint('main.ts', 3), false);
assert.equal(d.hasBreakpoint('main.ts', 3), false);
assert.equal(d.push(main(3)), false);
assert.equal(d.depth(), 1);
assert.deepEqual(d.pop(), main(3));
assert.equal(d.depth(), 0);
});
本机实测(Node v26.5.0,node --test test/07-1-breakpoint-step-callstack.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉断点命中与未命中;第三条钉 step in;第四、五条钉 step over 与 step out 的深度条件;第六条钉断点移除与栈深。
小结
- 断点判定只需要文件与行号。
- 三种步进统一表达为「下一次到达的深度条件」。
- 停止规则在未命中时保留,命中后才清除。
- 调用栈是显式数组,pop 返回被弹出帧的副本。
阅读导航:上一节:6.3 热重载安全与一致性 · 下一节:7.2 Watch、对象检查与即时求值 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。