使用 bochs+vmware+windbg 分析調試 windows 7 核心

來源:互聯網
上載者:User

原文連結:http://www.mouseos.com/win7/windbg.html

  用 google 可以搜出很多“如何使用 windbg 調試 windows 核心的”的教程。基本都差不多

  我根據搜出的教程,再總結一下將自己實驗過程寫出來。

★ 我的 vmware 版本是: 6.0.4 build-93057

★ windbg 版本是:6.11.0001.402 AMD64 

★ bochs 版本是:2.4.2

★ 目標 windows 7 版本是:windows 7 ultimate x64 中文版

-------------------------------------------------------------------------------------------------------

  使用 vmware + windbg 就可以調試 windows 核心了,但是我喜歡還開上 bochs ,使用 bochs 很方便靈活。

  vmware 裡的 guest OS 作為被偵錯項目,Host OS 裡使用 windbg 作為調試者。

  guest OS 和 Host OS 通過 serial port 以 named pipe 方式作為聯線。

一、在我的 vmware 上裝上了最新的 windows 7 x64 中文旗艦版

  bochs 也裝上了同樣的 windows 7 x64 中文旗艦版。

1、vmware 的設定

  開啟相應 vmware 虛擬機器上的 “Virtaul Machine Settings”---> “Hardware”選項中 ----> 點擊 “Add” 添加一個串口裝置 Seiall Port ----> “Next” ----> 在 Serial Port 裡選中“Output to named pipe” ----> “next” ----> “Finish”

  最後,回到 “Virtual Machine Settings”頁面時,在“I/O Mode” 裡選中“Yield CPU on poll” 這樣 vmware 的設定就好了。

2、windbg 的使用

  在案頭上建一個便捷方式,命令列是:

  "C:\Program Files\Debugging Tools for Windows 64-bit\windbg.exe" -b -k com:port=\\.\pipe\com_1,baud=115200,pipe

  這樣在開啟 vmware 中的 windows 7 後運行這個便捷方式,windbg 與 guest OS “win7” 就建立成聯線方式。

二、guest OS - win7 中設定

1、在 bootmgr 中下斷點

  在 vista 及後續的 windows 已經取消了 ntldr 模組管理引導,轉而以 bootmgr 模組管理引導系統。

  在我的 windows 7中要設定在哪個部分進行調試。

  windws 7 可調試部分有 4 個:bootmgr 模組、winload 模組、WinResume 模組以及 windows 核心模組 Nt 模組

可以在上述的 4 個模組下斷點進行調式

例:在 bootmgr 下斷點的話,可使用以下命令進行:

(1) 以管理員身份運行“命令提示字元”

(2) 在“命令提示字元”視窗中,輸入以下命令:

bcdedit /set {bootmgr} bootdebug on
bcdedit /set {bootmgr} debugtype serial
bcdedit /set {bootmgr} debugport 1
bcdedit /set {bootmgr} baudrate 115200

在提示命令成功後,這樣就在 windows7 中的 bootmgr 模組建立起可調試的機制。
在運行 windows7 時到達 bootmgr 時就停止載入,vmware 中是黑屏,它正等待 serial port 的響應,
運行 windbg 後,windbg 就和 windws 7 中的 bootmgr 建立起聯線了

下面是我的 windbg 在 bootmgr 模式下斷點的響應資訊:

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

Opened \\.\pipe\com_1
Waiting to reconnect...
BD: Boot Debugger Initialized
Connected to Windows Boot Debugger 7600 x86 compatible target at (Thu Nov 26 21:05:14.787 2009 (GMT+8)), ptr64 FALSE
Kernel Debugger connection established.  (Initial Breakpoint requested)
Symbol search path is: *** Invalid ***
****************************************************************************
* Symbol loading may be unreliable without a symbol search path.           *
* Use .symfix to have the debugger choose a symbol path.                   *
* After setting your symbol path, use .reload to refresh symbol locations. *
****************************************************************************
Executable search path is: 
*********************************************************************
* Symbols can not be loaded because symbol path is not initialized. *
*                                                                   *
* The Symbol Path can be set by:                                    *
*   using the _NT_SYMBOL_PATH environment variable.                 *
*   using the -y <symbol_path> argument when starting the debugger. *
*   using .sympath and .sympath+                                    *
*********************************************************************
*** ERROR: Module load completed but symbols could not be loaded for bootmgr
Windows Boot Debugger Kernel Version 7600 UP Free x86 compatible
Machine Name:
Primary image base = 0x00400000 Loaded module list = 0x00491b80
System Uptime: not available
Break instruction exception - code 80000003 (first chance)
bootmgr+0x436bc:
004436bc cc              int     3


這段資訊中,看到:

★ Opened \\.\pipe\com_1

  windbg 開啟了 serial port

★ Symbol search path is: *** Invalid ***

  目前模式的符號不可用

★ Primary image base = 0x00400000 Loaded module list = 0x00491b80

  bootmgr 被載入到基地址是 0x00400000

★ bootmgr+0x436bc:

  目前處在 bootmgr 模組中 

2、在 winload 模組下斷點

同樣在“命令提示字元”中使用命令

bcdedit /enum

這條命令顯示當前可用模組的 GUID 

在我的系統上顯示是:

Windows 啟動管理器
------------------
標識符       {bootmgr}
device              partition=\Device\HarddiskVolume1
description         Windows Boot Manager
locale              zh-CN
inherit             {globalsettings}
debugtype           Serial
debugport           1
baudrate            115200
bootdebug           yes
default             {current}
resumeobject        {53a5f400-d7b9-11de-93c1-e9eb61f9eb4f}
displayorder        {current}
toolsdisplayorder   {memdiag}
timeout              30

Windows 啟動載入器
------------------
標識符        {current}
device              partition=C:
path                \Windows\system32\winload.exe
description         Windows 7
locale              zh-CN
inherit             {bootloadersettings}
recoverysequence    {53a5f402-d7b9-11de-93c1-e9eb61f9eb4f}
osdevice            partitions=C:
systemroot          \Windows
resumeobject        {53a5f400-d7b9-11de-93c1-e9eb61f9eb4f}

winload 的 GUID 為 current

所以,用以下命令:

bcdedit /set {current} bootdebug on                  
bcdedit /set {current} debugtype serial
bcdedit /set {current} debugport 1
bcdedit /set {current} baudrate 115200

這樣就在 winload 模組下了斷點

3、在核心模組 Nt 模組下斷點

  開啟 windws 7 的“控制台”----> “管理工具” ---> “系統配置” ---> 開啟“引導”頁面 ---> 點擊“進階選項”

  ----> 在進階啟動選項裡,選中“調試” ----> 點擊“確定”就行了。

  這樣就在 nt 模組下了斷點

三、 windbg 載入符號

  在 windbg 的 "File" ---> "Symbol File path" ---> 設定為: SRV*d:\symbols*http://msdl.microsoft.com/download/symbols

  回到 windbg 命令列,使用命令 .reload 命令,可以自動載入符號

四、用 bochs 配合 windbg 調試

  bochs 做單獨調試,與 windbg 無聯絡,但 bochs 可以在整個啟動過程中設斷點,這一點 windbg 做不到。

  windbg 的優點是可以查看符號。bochs 中看不符號,bochs+windbg 配合使用可以觀察 windows7 啟動的每個流程。

 

參考資料:一篇文章:http://advdbg.org/blogs/advdbg_system/articles/784.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.