自动设置 KDNET 网络内核调试

Windows 调试工具 支持通过网络进行内核调试。 This article describes how to set up network debugging automatically by using the KDNET (kdnet.exe) setup tool.

The computer that runs the debugger is the host computer, and the computer being debugged is the target computer. The host computer must run Windows 7 or later, and the target computer must run Windows 8 or later.

确定主机的 IP 地址

使用以下步骤查找主机的 IP 地址:

  1. 确认目标电脑和主机电脑已连接到网络中心,或者使用适当的网络电缆进行交换机。

  2. On the host computer, open a command prompt and enter IPConfig to display the IP configuration.

  3. 在命令输出中,找到计算机的以太网适配器的 IPv4 地址: <Host-IP-Address>

    ...
    Ethernet adapter Ethernet:
    ...
    IPv4 Address. . . . . . . . . . . : <Host-IP-Address>
    ...
    

    Alternatively, use Settings>Network & internet>Ethernet to gather the IPv4 address information.

  4. 记下要用于调试的网络适配器的 IPv4 地址。

设置主机和目标计算机

使用 KDNET 实用工具通过以下步骤在目标电脑上自动配置调试器设置。

Note

此过程假定主机和目标计算都在运行 64 位版本的 Windows。 如果配置不同,最佳方法是在目标上运行的主机上运行相同工具的“位性”。 例如,如果目标运行的是 32 位 Windows,请在主机上运行 32 位版本的调试器。 有关详细信息,请参阅 选择 32 位或 64 位调试工具

  1. 确认主机计算机上安装了 Windows 调试工具。 有关下载和安装工具的信息,请参阅 适用于 Windows 的调试工具

  2. Locate the kdnet.exe and VerifiedNICList.xml files on the host computer. 默认情况下,这些文件位于 C:\Program Files (x86)\Windows Kits\10\Debuggers\x64 文件夹中。

  3. 将主计算机上的两个文件复制到网络共享或拇指驱动器,以便它们可用于目标计算机。

  4. On the target computer, create a C:\KDNET folder, and copy the kdnet.exe and VerifiedNICList.xml files to the folder.

    Important

    Before you use kdnet.exe to change boot information, you might need to temporarily suspend Windows security features on the test PC, such as BitLocker and Secure Boot. 完成测试后,可重新访问安全功能。 在禁用安全功能时,请务必适当地管理测试电脑。

  5. 在目标计算机上,以管理员身份打开命令提示符。 输入以下命令,验证目标计算机是否具有受支持的网络适配器:

    C:\KDNET>kdnet.exe
    Network debugging is supported on the following NICs:
    busparams=1.0.0, Broadcom NetXtreme Gigabit Ethernet, Plugged in.  
    This Microsoft hypervisor supports using KDNET in guest VMs.
    

    After the output from the KDNET (kdnet.exe) command shows the network adapter supported on the target, you can proceed.

  6. 输入以下命令以设置主机系统的 IP 地址并生成唯一的连接密钥。

    • <Host-Computer-IP-Address>对于该值,请使用主机系统的 IP 地址或名称。
    • <Debug-Port>对于值,请在建议的 50000-50039 范围内输入你处理的每个目标/主机对的唯一端口地址。
    C:\KDNET>kdnet.exe <Host-Computer-IP-Address> 
    
    Enabling network debugging on Intel(R) 82577LM Gigabit Network Connection.
    Key=2steg4fzbj2sz.23418vzkd4ko3.1g34ou07z4pev.1sp3yo9yz874p
    
  7. Save the key value returned by the KDNET command in a notepad .txt file.

将 WinDbg 连接到用于内核调试的目标

按照以下步骤将 WinDbg 连接到目标计算机进行内核调试:

  1. On the host computer, open WinDbg, and select File>Kernel Debug.

  2. In the Kernel Debugging dialog, select the Net tab.

  3. Paste the port number and key from the notepad.txt file into the tab fields, and select OK.

You can also start the WinDbg session from the command line by using the windbg.exe command and the values you saved in the notepad.txt file.

windbg.exe -k -d net:port=<Debug-Port>,key=<KDNET-Key> 

该值 <Debug-Port> 是之前选择的端口。 该值 <KDNET-Key> 是 KDNET 返回的键。 可选 -d 参数启用早期中断。 有关详细信息,请参阅 WinDbg 命令行选项

启动连接时,WinDbg 可能会提示你允许通过防火墙访问端口:

Windows 安全警报的屏幕截图,其中选择了允许访问所有三种网络类型。

In the message dialog, select each checkbox to enable access for all three network types, and select Allow access.

此时,调试器会等待目标重新连接并在调试器命令窗口中显示进度:

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

Using NET for debugging
Opened WinSock 2.0
Waiting to reconnect...

重启目标电脑

当调试器显示“正在等待重新连接...”时消息,重新启动目标计算机。

重启电脑的一种方法是在命令提示符下以管理员身份运行以下命令:

shutdown -r -t 0 

目标电脑重启后,调试器应自动连接。

Troubleshoot issues

如果在建立端口连接或启动调试会话时遇到问题,请查看以下故障排除提示。

允许通过防火墙调试应用

在运行调试器的主机端,必须启用所有类型的网络,以允许调试器通过防火墙与目标通信。

Use the Windows Control Panel to allow access through the firewall.

  1. Open Control Panel>System and Security and select the appropriate Allow an app through Windows Firewall options:

    • WinDbg: In the list of applications, locate the WinDbg engine process (TCP) (all) option.

    • WinDbg (Classic): In the list of applications, locate the Windows GUI Symbolic Debugger and Windows Kernel Debugger options.

  2. Select the checkboxes to allow all debugging applications through all three network types.

  3. Select OK to save the firewall changes, and then restart the debugger.

    控制面板防火墙配置的屏幕截图,其中显示了已启用所有三种网络类型的 Windows GUI 符号调试器和 Windows 内核调试器应用程序。

使用 ping 测试连接

如果调试器超时且未连接,请使用 ping 目标电脑上的命令验证连接性:

C:\>Ping <HostComputerIPAddress>

选择用于网络调试的端口

如果调试器超时且未连接,可能是因为默认端口号 50000 已在使用或被阻止。

可以选择 49152 和 65535 之间的任意端口号(含)。 建议的范围为 50000 到 50039。 选择的端口由在主计算机上运行的调试器进行独占访问。

公司网络策略可能会限制可用于网络调试的端口号范围。 若要确定公司的策略是否限制网络调试的端口范围,请与网络管理员联系。

查找支持的网络适配器

如果在运行 KDNET 时 看到此计算机消息中的任何 NIC 不支持网络调试 ,则不支持网络适配器。

主计算机可以使用任何网络适配器,但目标计算机必须使用 Windows 调试工具支持的网络适配器。 有关支持的网络适配器的列表,请参阅以下文章:

启用其他调试类型

Windows 10 2020 年 10 月更新(20H2)及更高版本支持四种类型的调试:

KDNET option Debugging type 等效集命令 More information
b Bootmgr debugging bcdedit /bootdebug {bootmgr} on BCDEdit /bootdebug
w Winload debugging bcdedit /bootdebug on BCDEdit /bootdebug
k Kernel debugging bcdedit /debug on WinDbg 入门(内核模式)
h Hypervisor debugging bcdedit /set hypervisordebug on BCDEdit /hypervisorsettings

下面是有关使用调试类型的详细信息:

  • 可以指定调试类型的任意组合。
  • 如果未指定调试类型,系统将启用内核调试。
  • 如果同时启用虚拟机监控程序和内核调试,虚拟机监控程序端口将设置为 <Debug-Port> + 1。

示例:启用 bootmgr、内核和 winload 调试

以下示例使用 - bkw 选项启用 bootmgr、内核和 winload 调试:

C:\>kdnet.exe <HostComputerIPAddress> <YourDebugPort> -bkw

Enabling network debugging on Intel(R) 82577LM Gigabit Network Connection.
Key=2steg4fzbj2sz.23418vzkd4ko3.1g34ou07z4pev.1sp3yo9yz874p

指定总线参数

如果 KDNET 无法自动确定传输的总线参数,则可以使用 /busparams 选项在命令行上指定参数:

kdnet.exe /busparams [b.d.f] [host] [port] [-[b][h][k][w]]

b.d.f 部分指定要配置的设备的总线参数。

Use Device Manager on the target computer to determine the PCI bus number (b), the device number (d), and the function number (f) of the adapter you want to use for debugging. These values are available in the General tab under the Location setting.

For example:

C:\>kdnet.exe /busparams 0.29.7 <Host-Computer-IP-Address> <Debug-Port> -bkw