里面讲述如何 Debug 某个 Sample Driver
(C:\WINDDK\3790.1830\src\general\ioctl, 网路上都称它为 SIOCTL)
要实做这个 Kernel Debug, 条件是要双机, 对入门者是一大挑战
(Local Kernel Debug 没办法实做这次的实验)
没关系, 我们可以靠着虚拟机来帮我们完成
首先我们必须下载一些工具, VPC 和 WinXP 的 MODE
先下载 VPC , (Microsoft Virtual PC 2007), 用英文版的, 并把它安装好
http://www.microsoft.com/en-us/download/details.aspx?id=24439
然後下载 Windows XP Mode 繁体中文版
http://download.microsoft.com/do ... owsXPMode_zh-tw.exe
简体是这里 http://www.microsoft.com/zh-cn/download/details.aspx?id=8002 (不过要验证)
下载 WindowsXPMode_zh-tw.exe 完毕後,
用 WinRAR 开启并解压缩出里面的 xpm
将 xpm 档案加上副档名变成 xpm.rar
再把 xpm.rar 里面 VirtualXPVHD 解压缩出来,重新命名成VirtualXP.VHD,
建立虚拟机会用到, 限於页面篇幅, 我将设置的过程贴在下面网址
http://www.m5home.com/bbs/thread-6713-1-1.html
上面的事前工作都准备好後我们要开始进入重要的设定了
一. 首次启动虚拟机後, 会先经过一番 XP的安装, 完毕後
我们先设定 XP虚拟机 的 boot.ini
在 C:\ 底下的 boot.ini 档案, 先将它唯读的档案属性取消
用 notepad 打开 boot.ini, 里面内容如下
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect
请先将这内容备份起来, 储存到另一个文字档, 改天要恢复的时候比较方便
再来将 boot.ini 里面这一行
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect
改成
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Kernel Debug" /noguiboot /debug /debugport=COM1 /sos
注意喔, 这是改虚拟机里面的 boot.ini, 不是你真实的机子喔
设定完毕後, 关闭这 XP虚拟机 (不是将整个 Virtual PC 2007 关闭)
回到 Virtual PC Console, 设定 WinXP 虚拟机的 COM1
点选 Named pipe 的 option
并在下方的 TextBox 输入 \\.\pipe\com_debug
设定成如下图:


二. 一般在使用 WinDBG 都是用预设的 workspace (default workspace)
由於我们要进入真正实做的 Kernel Debug, 所以必须改变一下
启动 regedit, 到 [HKEY_CURRENT_USER\Software\Microsoft\Windbg]
先将它备份起来, 在该"机码"按滑鼠右键选 [汇出], 储存成 WinDBG.reg
储存备份完成後, 把 WinDBG 的机码删除 (不是删除刚刚备份的 Registry file WinDBG.reg),
不要怀疑, 就是删除, 不删除到後面会有很多後遗症, 会让你一个头两个大,
(当你要再次 Local Mode 的 Kernel Debug, 你再汇入 WinDBG.reg 就好了)
接下来用 explorer 浏览到 WinDBG 的安装目录下的 Themes
C:\Program Files\Debugging Tools for Windows (x86)\themes
里面有个 standardvs.reg , 执行它, 汇入後会有新设定的 workspace 让我们方便使用
现在你启动 WinDBG 後, 应该会是像下面这张图一样的 workspace

现在 workspace 弄好了
接下来要设定 COM, 到 [File] -> [Kernel Debug] ,
在 COM 页签的 Port 下面的 TextBox 输入 \\.\pipe\com_debug
顺便把 pipe 和 Reconnect 这两个 CheckBox 打勾
设定成如下图

按 [确定] 後, 会出现问你要不要储存 workspace 的对话盒

注意喔, 一定要把里面的 Don't ask again in this WinDBG session
的 CheckBox 打勾, 免得後面一直问同样问题
接下来你会在 Command 视窗看到如下图的提示

这时的 WinDBG 是处於等待连结的状态
现在让我们再次启动 WinXP虚拟机,
假如没有出错的话, 这时 WinDBG 和 WinXP虚拟机 已经在互相通讯了
你也会在 WinDBG 的 Command 视窗看到如下图的提示

完毕 WinXP虚拟机 的登入後, 回到 WinDBG 视窗,
在 WinDBG 视窗上的工具条的 Break 按钮按一下

这时的 Disassembly 视窗应该是停在 nt!DbgBreakPointWithStatus

你现在回到 WinXP虚拟机 的视窗用滑鼠点击任何地方都不回应吧
回到 WinDBG 视窗, 在 WinDBG 上的工具条的 Go 按钮, 按一下

三. 万事俱备, 只欠东风
我们的目标是调试 SIOCTL 这个 Driver
启动 WinXP DDK 的 Checked 的 Command Line 视窗

先进入 ioctl 目录
输入 cd C:\WINDDK\3790.1830\src\general\ioctl
[Enter]後, 再输入 build -ceZ , 没发生错误的话, 如下图

并会生成我们要的两个档案 ioctlapp.exe 和 sioctl.sys
档案的路径分别如下
C:\WINDDK\3790.1830\src\general\ioctl\exe\objchk_wxp_x86\i386\ioctlapp.exe
C:\WINDDK\3790.1830\src\general\ioctl\sys\objchk_wxp_x86\i386\sioctl.sys
假如你没有这些档案在这里下载
然後用 ISO Image 工具
将这两个档案装进 ISO File 里面, 如下图

[File] -> [Create ISO Image], 就會生成一個 ISO Image File 出來
我把它取名为 sioctl.iso
另外, ISO9660 这个 Source 是一个 VB 写的 ISO Image 工具,
它支持 Drag&Drop, 你有兴趣也可以研究看看, 功能当然比不上 UltraISO
回到主题
生成一个含有档案 ioctlapp.exe 和 sioctl.sys 的 Image File 後,
到 WinXP虚拟机 视窗, 在其上方的工具列
[CD] -> [Capture ISO Image], 浏览到刚刚你生成的 sioctl.iso, 并选择它
这时候 WinXP虚拟机 的光碟机 D: 应该会出现我们要的两个档案
再回到 WinDBG 视窗, 按一下 Break 按钮,
然後在 Command 视窗的命令列输入 bu sioctl!DriverEntry
再输入 bl, 应该会显示
kd> bl
0 eu 0001 (0001) (sioctl!DriverEntry)
好了之後再按一下 Go 按钮,
再回到 WinXP虚拟机 视窗, 执行在 D: 光碟机的 ioctlapp.exe
假如都正常的话, 这时 WinDBG 视窗里面的右上方视窗应该如下图

而左上方视窗应该如下图

好了, 现在你可以按键盘的 F10 (Step Over) 调试了
至此你应该了解如何利用虚拟机实行 Remote Debug (双机调试)
你也可以用这方法 Debug 自己写的 Driver Code