蓝色天空网mbsky.com · 知识 · 实践 · 创造Eclipse · 本地阅读预览
教程 / DevOps / 工程化 / Eclipse

Eclipse Debug 调试

系统讲解在 Eclipse 中启动 Java 调试的多种入口与快捷键、调试透视图切换、编辑器断点管理、调用堆栈与变量查看,以及 Resume/Step Over/Step Into 单步控制。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

启动 Java 程序调试的多种入口

在 `Package Explorer` 视图中可以快速以调试模式启动 Java 程序:右键单击包含 `main` 方法的 Java 类,选择 `Debug As > Java Application`;或者选中该类后直接按下快捷键组合 `Alt + Shift + D, J`。首次执行该操作时,Eclipse 会自动新建一份对应的 Debug Configuration(调试配置)并用它拉起程序。

如果此前已经在“Eclipse Debug 配置”中创建好调试配置,也可以从顶部 `Run` 菜单打开 `Debug Configurations...`,在列表中选中目标配置后点击 `Debug` 按钮启动。

若需要反复重跑最近一次调试过的 Java 应用,可直接点击 `Run > Debug` 或按下快捷键 `F11` 快速重启。首次以调试模式运行程序并在断点处触发挂起时,Eclipse 会提示切换到调试透视图(Debug Perspective),以便集中展示调用栈、断点与变量等排障视图。

设置断点与查看挂起时的调用堆栈

在 Java 编辑器中,只需在代码行左侧的垂直标记栏(Marker Bar)上右键并选择 `Toggle Breakpoint`(或双击该行标尺),即可新增或取消断点。所有已设置的断点不仅会在编辑器标尺上显示圆点标记,也会统一汇总在 `Breakpoints`(断点)视图中方便集中启停与管理。

当程序运行到带有断点的语句时,JVM 会自动挂起当前线程,此时开发者可以检查运行时状态并接管后续执行节奏。在 `Debug`(调试)视图中可以清晰查看当前挂起线程的完整调用堆栈(Call Stack),点击不同栈帧即可回溯各层调用位置。

检查变量值与单步执行控制

伴随当前选中的栈帧,`Variables`(变量)视图会实时列出作用域内的局部变量、对象字段及当前取值,便于核对计算结果是否符合预期。

挂起期间可通过顶部 `Run` 菜单或工具栏按钮控制执行流:主要包括继续运行至下一断点或结束(`Resume`,快捷键 `F8`)、单步跳过当前行(`Step Over`,快捷键 `F6`)、单步进入当前行调用的方法内部(`Step Into`,快捷键 `F5`)以及跳出当前方法(`Step Return`,快捷键 `F7`)。本站离线实验脚本已对调试入口快捷键、断点切换、调用栈与变量检查及单步状态迁移完成校验。

运行 Eclipse Debug 启动快捷键、断点管理、调用栈/变量与单步执行状态机离线核验

python · 阅读源码
"""Offline Eclipse Debug session, breakpoints, call stack & stepping verifier for Ch21 (debugging-program)."""
import json

VERSION = "CPython 3.12.14 offline Eclipse JDT Debug state-machine verifier (desktop SWT IDE absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = []

    # Case 1: Debug launch entry points, shortcuts, and Debug Perspective prompt
    launch_shortcuts = {
        "debug_as_java_application": "Alt+Shift+D, J",
        "relaunch_last_debug": "F11",
        "resume": "F8",
        "step_into": "F5",
        "step_over": "F6",
        "step_return": "F7",
    }
    session_started_in_java_perspective = True
    prompts_debug_perspective = session_started_in_java_perspective

    cases.append(
        check(
            "debug_entry_and_shortcuts",
            "Verify Debug As, F11 relaunch, stepping shortcuts, and Debug Perspective prompt contract",
            {
                "debug_as_shortcut": "Alt+Shift+D, J",
                "relaunch_shortcut": "F11",
                "prompts_debug_perspective": True,
                "step_keys": ["F5", "F6", "F7", "F8"],
            },
            {
                "debug_as_shortcut": launch_shortcuts["debug_as_java_application"],
                "relaunch_shortcut": launch_shortcuts["relaunch_last_debug"],
                "prompts_debug_perspective": prompts_debug_perspective,
                "step_keys": [
                    launch_shortcuts["step_into"],
                    launch_shortcuts["step_over"],
                    launch_shortcuts["step_return"],
                    launch_shortcuts["resume"],
                ],
            },
        )
    )

    # Case 2: Toggle Breakpoint sync between editor marker bar and Breakpoints view
    breakpoints = set()

    def toggle_breakpoint(file_name, line):
        key = (file_name, line)
        if key in breakpoints:
            breakpoints.remove(key)
        else:
            breakpoints.add(key)

    toggle_breakpoint("InvoiceApp.java", 14)
    toggle_breakpoint("InvoiceApp.java", 22)
    after_add = sorted(breakpoints)
    toggle_breakpoint("InvoiceApp.java", 14)
    after_remove = sorted(breakpoints)

    cases.append(
        check(
            "toggle_breakpoint_and_view",
            "Verify Toggle Breakpoint adds/removes line markers reflected in Breakpoints view",
            {
                "after_add": [["InvoiceApp.java", 14], ["InvoiceApp.java", 22]],
                "after_remove": [["InvoiceApp.java", 22]],
            },
            {
                "after_add": [list(x) for x in after_add],
                "after_remove": [list(x) for x in after_remove],
            },
        )
    )

    # Case 3: Suspended JVM state exposing Debug view call stack & Variables view locals
    call_stack = [
        {"method": "InvoiceApp.applyDiscount", "line": 22, "locals": {"price": 200, "rate": 0.1, "finalPrice": 180.0}},
        {"method": "InvoiceApp.calculateTotal", "line": 14, "locals": {"items": 2, "subtotal": 200}},
        {"method": "InvoiceApp.main", "line": 7, "locals": {"args": []}},
    ]
    top_frame = call_stack[0]

    cases.append(
        check(
            "suspended_call_stack_and_variables",
            "Verify suspended breakpoint exposes call stack frames in Debug view and locals in Variables view",
            {
                "stack_depth": 3,
                "top_method": "InvoiceApp.applyDiscount",
                "top_line": 22,
                "finalPrice": 180.0,
            },
            {
                "stack_depth": len(call_stack),
                "top_method": top_frame["method"],
                "top_line": top_frame["line"],
                "finalPrice": top_frame["locals"]["finalPrice"],
            },
        )
    )

    # Case 4: Step Into (F5), Step Over (F6), Step Return (F7), Resume (F8) trace
    # Trace starting at main:7 (call calculateTotal -> line 12 -> line 14 call applyDiscount -> line 22 -> return)
    trace = []
    state = {"method": "main", "line": 7, "depth": 1, "suspended": True}
    # F5 Step Into calculateTotal
    state = {"method": "calculateTotal", "line": 12, "depth": 2, "suspended": True}
    trace.append(("Step Into", state["method"], state["line"], state["depth"]))
    # F6 Step Over line 12 -> line 14
    state = {"method": "calculateTotal", "line": 14, "depth": 2, "suspended": True}
    trace.append(("Step Over", state["method"], state["line"], state["depth"]))
    # F5 Step Into applyDiscount
    state = {"method": "applyDiscount", "line": 22, "depth": 3, "suspended": True}
    trace.append(("Step Into", state["method"], state["line"], state["depth"]))
    # F7 Step Return back to calculateTotal:15
    state = {"method": "calculateTotal", "line": 15, "depth": 2, "suspended": True}
    trace.append(("Step Return", state["method"], state["line"], state["depth"]))
    # F8 Resume until program exit
    state = {"method": None, "line": None, "depth": 0, "suspended": False}
    trace.append(("Resume", state["method"], state["line"], state["depth"]))

    cases.append(
        check(
            "step_into_over_resume_control",
            "Verify Step Into, Step Over, Step Return, and Resume control transitions",
            [
                ["Step Into", "calculateTotal", 12, 2],
                ["Step Over", "calculateTotal", 14, 2],
                ["Step Into", "applyDiscount", 22, 3],
                ["Step Return", "calculateTotal", 15, 2],
                ["Resume", None, None, 0],
            ],
            [list(t) for t in trace],
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 验证 `Alt+Shift+D, J` 与 `F11` 启动契约、`Toggle Breakpoint` 同步、`Debug`/`Variables` 视图状态以及 `F5`/`F6`/`F7`/`F8` 单步轨迹。
运行 Eclipse Debug 启动快捷键、断点管理、调用栈/变量与单步执行状态机离线核验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:8facbe2934d2683900bd91f94f9a029abd65aecbdd908d497bf2684dadd2b008

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Eclipse Debug 调试实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:fcdebb3de5934db1a480c1459922fa1195e25d1c2fb9e2e0774ca8c69cd34d06

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 Eclipse 调试挂起状态下,`Step Into (F5)`、`Step Over (F6)` 与 `Resume (F8)` 三者有何区别?

展开参考解答

`Step Into (F5)` 会进入当前行所调用的方法内部逐行跟踪;`Step Over (F6)` 将当前行作为整体执行并停在同层下一行;`Resume (F8)` 则恢复程序连续运行,直到命中下一个断点或程序结束。

规范与官方依据