两台 Windows 虚拟机之间的双机调试环境搭建

Table of Contents

1. 背景

日常使用的 Windows 虚拟开机过程中蓝屏了,无法进入系统,于是我想通过 WinDbg 远程调试来找出蓝屏原因。本文记录了如何在两台 Windows 虚拟机之间建立串口连接进行双机调试。

环境如下:

  • 宿主机:Fedora 33、VMware Workstation 16 Player、VirtualBox 6.1
  • 虚拟机:两台 Windows 10

2. 调试环境搭建

2.1. 虚拟机配置

Windows 双机调试主要通过串口连接的方式。

2.1.1. VMware

VMware 支持对虚拟机创建虚拟串口。将系统处于关闭状态后,在被调试机中选择 Virtual Machine Settings -> Hardware:

1、如果有打印机(Printer)存在,将它删除;

2、单击 Add 按钮,选择 Serial Port,注意串口的配置如下:

  • Connection 中选择 Use socket (name pipe),然后输入路径,如:/tmp/debug_com1。
  • From 中,选择 Server,To 中选择 An Application。

然后在调试机中重复上面的步骤并设置串口:

1、Connection 中选择 Use socket (name pipe),然后输入相同路径(/tmp/debug_com1)。

2、From 中,选择 Client,To 中选择 An Application。

这样两台主机之间就建立起了一条虚拟的串口通信线路,接下来配置系统。

注意:要明确好哪台虚拟机是调试端,哪台是被调试机,被调试机的 From 选为 Server。

2.1.2. VirtualBox

1、选择调试机的“设置”,找到“串口”,并选择一个串口启用,如“端口 1”。

2、“端口模式”选为“主机管道”,“路径/地址”中输入文件路径,如:/tmp/debug_com1。

然后在调试机中重复上面的步骤设置串口即可。

2.2. 被调试机配置

以管理员身份运行 cmd(像本例中无法正常启动系统,可以进入恢复模式,然后启动 cmd),依次执行:

Microsoft Windows [版本 10.0.19042.746]
(c) 2020 Microsoft Corporation. 保留所有权利。

C:\Windows\system32>bcdedit.exe /copy {current} /d "Debug Mode"
已将该项成功复制到 {b07907f9-6514-11eb-a172-b2b2f48ead72}。

C:\Windows\system32>bcdedit.exe /debug {b07907f9-6514-11eb-a172-b2b2f48ead72} on
操作成功完成。

C:\Windows\system32>bcdedit /dbgsettings serial DEBUGPORT:1 BAUDRATE:115200
操作成功完成。

C:\Windows\system32>bcdedit /bootdebug ON
操作成功完成。

2.3. 调试机中配置

运行 WinDbg,File > Kernel Debug,打开内核调试配置,选择 COM 选项卡,Baud Rate 和 Port 保持默认,勾选 Reconnect,点击 确定,WinDbg 会不断通过串口发送连接请求,等待被调试机的连接。

然后启动被调试机,在启动菜单中选择刚才新建的启动项:Debug Mode,成功的话 WinDbg 会中断,如:

Microsoft (R) Windows Debugger Version 10.0.19041.685 AMD64
Copyright (c) Microsoft Corporation. All rights reserved.

Opened \\.\com1
Waiting to reconnect...
Connected to Windows 10 19041 x64 target at (Tue Feb 16 01:31:48.496 2021 (UTC + 8:00)), ptr64 TRUE
Kernel Debugger connection established.

************* Path validation summary **************
Response                         Time (ms)     Location
Deferred                                       SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols
Symbol search path is: SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols
Executable search path is:
Windows 10 Kernel Version 19041 MP (1 procs) Free x64
Built by: 19041.1.amd64fre.vb_release.191206-1406
Machine Name:
Kernel base = 0xfffff807`5fa00000 PsLoadedModuleList = 0xfffff807`6062a2f0
System Uptime: 0 days 0:00:00.000
nt!DebugService2+0x5:
fffff807`5fdfd675 cc              int     3