<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://linux-kvm.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Caobingbu</id>
	<title>KVM - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://linux-kvm.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Caobingbu"/>
	<link rel="alternate" type="text/html" href="https://linux-kvm.org/page/Special:Contributions/Caobingbu"/>
	<updated>2026-04-26T05:06:12Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.5</generator>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4450</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4450"/>
		<updated>2011-11-11T07:21:22Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
[[Image:build.png]]&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code&lt;br /&gt;
[[Image:breakpoint.png]]&lt;br /&gt;
also can watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
[[Image:variable.png]]&lt;br /&gt;
[[Image:stack.png]]&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
:The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;TBD.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4445</id>
		<title>WindowsGuestDrivers</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4445"/>
		<updated>2011-11-10T09:34:41Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Windows guest drivers =&lt;br /&gt;
This is a main page for the Windows guests drivers.&lt;br /&gt;
&lt;br /&gt;
Current drivers are base on [[Virtio|VirtIO]] interface.&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/kvmnet|kvmnet - NDIS miniport network driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/viostor|viostor - STORPORT miniport driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Download Drivers|Download binary drivers]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Repository|Repository]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Git_and_crlf|git and cr\lf]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/HOWTO|HOWTO]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/GuestDebugging|Windows Guest Drivers debugging]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/UpdatedGuestDebugging|Windows Guest Drivers debugging(with details)]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Links|Useful links]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Building the drivers|Building the drivers]]&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4444</id>
		<title>WindowsGuestDrivers</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4444"/>
		<updated>2011-11-10T09:34:28Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Windows guest drivers =&lt;br /&gt;
This is a main page for the Windows guests drivers.&lt;br /&gt;
&lt;br /&gt;
Current drivers are base on [[Virtio|VirtIO]] interface.&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/kvmnet|kvmnet - NDIS miniport network driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/viostor|viostor - STORPORT miniport driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Download Drivers|Download binary drivers]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Repository|Repository]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Git_and_crlf|git and cr\lf]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/HOWTO|HOWTO]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/GuestDebugging|Windows Guest Drivers debugging]]&lt;br /&gt;
[[WindowsGuestDrivers/UpdatedGuestDebugging|Windows Guest Drivers debugging(with details)]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Links|Useful links]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Building the drivers|Building the drivers]]&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4443</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4443"/>
		<updated>2011-11-10T09:32:19Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
[[Image:build.png]]&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code&lt;br /&gt;
[[Image:breakpoint.png]]&lt;br /&gt;
also can watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
[[Image:variable.png]]&lt;br /&gt;
[[Image:stack.png]]&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
:The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;TBD.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4442</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4442"/>
		<updated>2011-11-10T09:30:36Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
[[Image:build.png]]&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code&lt;br /&gt;
[[Image:sourcecode.png]]&lt;br /&gt;
also can watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
[[Image:variable.png]]&lt;br /&gt;
[[Image:stack.png]]&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
:The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;TBD.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4441</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4441"/>
		<updated>2011-11-10T09:29:37Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
[[Image:build.png]]&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
[[Image:breakpoint.png]]&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code&lt;br /&gt;
[[Image:sourcecode.png]]&lt;br /&gt;
also can watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
[[Image:variable.png]]&lt;br /&gt;
[[Image:stack.png]]&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
:The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;TBD.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=File:Stack.png&amp;diff=4440</id>
		<title>File:Stack.png</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=File:Stack.png&amp;diff=4440"/>
		<updated>2011-11-10T09:26:01Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=File:Sourcecode.png&amp;diff=4439</id>
		<title>File:Sourcecode.png</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=File:Sourcecode.png&amp;diff=4439"/>
		<updated>2011-11-10T09:25:25Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=File:Variable.png&amp;diff=4438</id>
		<title>File:Variable.png</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=File:Variable.png&amp;diff=4438"/>
		<updated>2011-11-10T09:18:26Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=File:Breakpoint.png&amp;diff=4437</id>
		<title>File:Breakpoint.png</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=File:Breakpoint.png&amp;diff=4437"/>
		<updated>2011-11-10T09:17:29Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=File:Build.png&amp;diff=4436</id>
		<title>File:Build.png</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=File:Build.png&amp;diff=4436"/>
		<updated>2011-11-10T09:16:06Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4435</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4435"/>
		<updated>2011-11-10T09:11:23Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
:The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;TBD.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4434</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4434"/>
		<updated>2011-11-10T09:10:23Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:red&amp;quot;&amp;gt;&lt;br /&gt;
TBD.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4433</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4433"/>
		<updated>2011-11-10T09:05:44Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4432</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4432"/>
		<updated>2011-11-10T09:00:42Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run WinDbg====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4431</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4431"/>
		<updated>2011-11-10T08:59:11Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging | Guest Debugging on this site]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4430</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4430"/>
		<updated>2011-11-10T08:58:17Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[WindowsGuestDrivers/GuestDebugging]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4429</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4429"/>
		<updated>2011-11-10T08:56:26Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).[[GuestDebugging]] &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4428</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4428"/>
		<updated>2011-11-10T08:55:40Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [mailto:mars@linux.vnet.ibm.com Cao,Bing Bu] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4427</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4427"/>
		<updated>2011-11-10T08:54:16Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool [DriverStudio] to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool [http://technet.microsoft.com/en-us/sysinternals/bb896647 DebugView],it can be used in target guest(without WinDbg) only environment to debug the driver with the &amp;lt;tt&amp;gt;DbgPrint()&amp;lt;/tt&amp;gt; debug message. &lt;br /&gt;
It is also a available method to debug the driver.&lt;br /&gt;
&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
:I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
:It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: [Cao,Bing Bu caobbu@cn.ibm.com] )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4426</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4426"/>
		<updated>2011-11-10T08:44:14Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool &amp;quot;DriverStudio&amp;quot; to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will also be show in the WinDbg:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool &amp;quot;DebugView&amp;quot;,it can be used in target guest(without WinDbg) only environment to debug the driver with the DbgPrint() debug message. It is also a available method to debug the driver.&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: caobbu@cn.ibm.com )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4425</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4425"/>
		<updated>2011-11-10T08:39:37Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
&lt;br /&gt;
==Compile and build==&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Load,install and debug the driver==&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set.&lt;br /&gt;
&amp;lt;br&amp;gt;F5,continue.&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool &amp;quot;DriverStudio&amp;quot; to load,start,stop the driver on target.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
The debug message will also be show in the WinDbg:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool &amp;quot;DebugView&amp;quot;,it can be used in target guest(without WinDbg) only environment to debug the driver with the DbgPrint() debug message. It is also a available method to debug the driver.&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: caobbu@cn.ibm.com )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4424</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4424"/>
		<updated>2011-11-10T08:34:49Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==HOWTO debug the guest driver==&lt;br /&gt;
Compile and build:&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
Load,install and debug the driver:&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set&lt;br /&gt;
 &lt;br /&gt;
F5,continue.&lt;br /&gt;
&lt;br /&gt;
GUEST:&lt;br /&gt;
&amp;lt;br&amp;gt;Using tool &amp;quot;DriverStudio&amp;quot; to load,start,stop the driver.&lt;br /&gt;
File-&amp;gt;Open select the &amp;lt;tt&amp;gt;viotest.sys&amp;lt;/tt&amp;gt;, and then start (Go)the driver.&lt;br /&gt;
&amp;lt;br&amp;gt;Meanwhile,the WinDbg will hit the breakpoint at &amp;lt;tt&amp;gt;viotest.c:DriverEntry()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;And the related source file will be opened to view.&lt;br /&gt;
&amp;lt;br&amp;gt;You can see the breakpoints position with highlight label in the source code and watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&amp;lt;br&amp;gt;The debug message will be show in the WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
The debug message will also be show in the WinDbg:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
The message can be viewed by another tool &amp;quot;DebugView&amp;quot;,it can be used in target guest(without WinDbg) only environment to debug the driver with the DbgPrint() debug message. It is also a available method to debug the driver.&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: caobbu@cn.ibm.com )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4423</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4423"/>
		<updated>2011-11-10T08:30:42Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host VM ===&lt;br /&gt;
====start the host VM and install Windbg====&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
====Set Symbols path====&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
====About the 3 different symbols====&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
====Creating local symbols server====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
&lt;br /&gt;
====Run Kernel Debug====&lt;br /&gt;
Then &amp;lt;tt&amp;gt;File-&amp;gt;Kernel Debug...&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;[Ctrl+k]&amp;lt;/tt&amp;gt; select &amp;lt;tt&amp;gt;COM&amp;lt;/tt&amp;gt; tab and set &amp;lt;tt&amp;gt;Baudrate&amp;lt;/tt&amp;gt; to 115200 and set port to &amp;lt;tt&amp;gt;COM1&amp;lt;/tt&amp;gt;,confirm and wait the target to connect.&lt;br /&gt;
===Setting up the target VM===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;-serial tcp:127.0.0.1:4445&amp;lt;/tt&amp;gt; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
&amp;lt;br&amp;gt;When start up,if Windows XP,2003 or 2000,edit &amp;lt;tt&amp;gt;c:\boot.ini&amp;lt;/tt&amp;gt; and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&amp;lt;tt&amp;gt;/debug /debugport=COM1 /baudrate=115200&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;The baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
&amp;lt;br&amp;gt;If Windows7,Vista or up,you must use &amp;lt;tt&amp;gt;BCDedit&amp;lt;/tt&amp;gt;(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&amp;lt;br&amp;gt;Change the boot opition:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=Debug=&lt;br /&gt;
When Host and target setup completely,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
&amp;lt;br&amp;gt;Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
&amp;lt;br&amp;gt;You can &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; or use the &amp;lt;tt&amp;gt;Pause&amp;lt;/tt&amp;gt; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
==HOWTO debug the guest driver==&lt;br /&gt;
Compile and build:&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
 #include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
 #include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;tt&amp;gt;Makefile&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SOURCES&amp;lt;/tt&amp;gt; file into the project&lt;br /&gt;
The driver only include &amp;lt;tt&amp;gt;DriverEntry()&amp;lt;/tt&amp;gt;,&amp;lt;tt&amp;gt;DriverUnload()&amp;lt;/tt&amp;gt; and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;lt;tt&amp;gt;&amp;quot;Windows XP x86 checked build&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;viotest&amp;gt;build -cCzg&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(&amp;lt;tt&amp;gt;viotest.sys,viotest.inf&amp;lt;/tt&amp;gt;) will be exported into the &amp;lt;tt&amp;gt;...\viotest\objchk_wxp_x86\i386\&amp;lt;/tt&amp;gt;.&lt;br /&gt;
Load,install and debug the driver:&lt;br /&gt;
&lt;br /&gt;
After the process mentioned below,we copy the &amp;lt;tt&amp;gt;viotest&amp;lt;/tt&amp;gt; directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;lt;tt&amp;gt;Ctrl+Break&amp;lt;/tt&amp;gt; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
&amp;lt;br&amp;gt;kd&amp;gt; bu !viotest:DriverUnload&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;&amp;quot;bl&amp;quot;&amp;lt;/tt&amp;gt; to watch breakpoints set&lt;br /&gt;
 &lt;br /&gt;
F5,continue.&lt;br /&gt;
&lt;br /&gt;
GUEST:&lt;br /&gt;
Using tool &amp;quot;DriverStudio&amp;quot; to load,start,stop the driver.&lt;br /&gt;
File-&amp;gt;Open select the &amp;quot;viotest.sys&amp;quot;, and then start (Go)the driver.&lt;br /&gt;
Meanwhile,the WinDbg will hit the breakpoint at viotest.c:DriverEntry().&lt;br /&gt;
And the related source file will be opened to view.&lt;br /&gt;
You can see the breakpoints position with highlight label in the source code.&lt;br /&gt;
&lt;br /&gt;
You can watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&lt;br /&gt;
The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
The debug message will also be show in the WinDbg:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
      The message can be viewed by another tool &amp;quot;DebugView&amp;quot;,it can be used in target guest(without WinDbg) only environment to debug the driver with the DbgPrint() debug message. It is also a available method to debug the driver.&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: caobbu@cn.ibm.com )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4422</id>
		<title>WindowsGuestDrivers/UpdatedGuestDebugging</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers/UpdatedGuestDebugging&amp;diff=4422"/>
		<updated>2011-11-10T08:11:17Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
This page mainly introduce HOWTO use WinDbg to debug windows guest driver based on qemu.&lt;br /&gt;
&lt;br /&gt;
=Setup=&lt;br /&gt;
&lt;br /&gt;
==About WinDbg==&lt;br /&gt;
WinDbg is a multi-purposed debugger for Microsoft Windows, distributed on the web by Microsoft. It can be used to debug user mode applications, drivers, and the operating system itself in kernel mode.&lt;br /&gt;
To debug windows guest in kernel mode,we generally need a host computer as a remote debugger which runs the WinDbg and a target computer as a debuggee.&lt;br /&gt;
&lt;br /&gt;
==Connection between the host and target==&lt;br /&gt;
To allow debug windows guest kernel on QEMU,we have to connect the two virtual machines by using a virtual non-modem serial cable.The QEMU serial redirection ability can help us.&lt;br /&gt;
The simplest way is creating two windows guest virtual machines (one host,the other as target)on a same physical machine.&lt;br /&gt;
It is feasible that the host virtual machine and target run on two different physical machines by connecting the virtual serial cable via TCP.&lt;br /&gt;
&lt;br /&gt;
==Setting up the host and target virtual machine==&lt;br /&gt;
===Setting up the host:===&lt;br /&gt;
 &amp;lt;tt&amp;gt;/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-host.img \-chardev stdio,id=mon0 \-mon chardev=mon0 \-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add the &amp;lt;tt&amp;gt;-serial tcp::4445,server,nowait&amp;lt;/tt&amp;gt; to enable the serial redirection ability.The port number 4445 can be any valid tcp port.&lt;br /&gt;
&amp;lt;br&amp;gt;Using the server option and &amp;lt;tt&amp;gt;nowait&amp;lt;/tt&amp;gt; option QEMU opens a port which a client socket application can connect to it,the host will continue running without any wait.&lt;br /&gt;
&amp;lt;br&amp;gt;After startup,get and install the latest version of the Debugging Tools for Windows.Get them from the website:&amp;lt;tt&amp;gt;http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx&amp;lt;/tt&amp;gt;  or a ISO image file.&lt;br /&gt;
&amp;lt;br&amp;gt;You may need restart the host virtual machine to complete the installation.&lt;br /&gt;
When debugging a windows driver especially kernel-mode driver,symbols typically should be installed.&lt;br /&gt;
===Set Symbols path===&lt;br /&gt;
You can set symbols file path by &amp;lt;tt&amp;gt;File-&amp;gt;Symbol File Path...&amp;lt;/tt&amp;gt;or &amp;lt;tt&amp;gt;[Ctrl+S]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
also by add windows system environment variable &amp;lt;tt&amp;gt;_NT_SYMBOL_PATH&amp;lt;/tt&amp;gt;,set the value to:&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;c:\symbols\local_symbols;c:\driver\symbols;SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===About the 3 different symbols===&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
 |&#039;&#039;&#039;symbol &#039;&#039;&#039;&lt;br /&gt;
 |&#039;&#039;&#039;info&#039;&#039;&#039;&lt;br /&gt;
 |-&lt;br /&gt;
 |&amp;lt;nowiki&amp;gt;*SRV*c:\symbols\websymbols*http://msdl.microsoft.com/download/symbols*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 |If symbols not found in local symbols,the WinDbg will automatic download symbols from the URL address http://msdl.microsoft.com/download/symbols . And the symbols download will be stored in the directory: c:\symbols\websymbols&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\symbols\local_symbols &lt;br /&gt;
 |The symbols of current windows version.They can be acquired by website: http://msdn.microsoft.com/en-us/windows/hardware/gg463028&lt;br /&gt;
 |-&lt;br /&gt;
 |c:\driver\symbols&lt;br /&gt;
 |This mean the symbols(*.pdb files) will be produced after build process of the driver. We can use the WinDDK debugging tool symstore.exe to create own symbols server.&lt;br /&gt;
 |}&lt;br /&gt;
&lt;br /&gt;
===Creating local symbols server===&lt;br /&gt;
&lt;br /&gt;
C:\WinDDK\7600.16385.1\Debuggers&amp;gt;symstore.exe add /r /f C:\development\virtio-test\kvm-guest-drivers-windows\viotest\objchk_wxp_x86\i386 /s C:\driver\symbols\ /t &amp;quot;DriverSymbols&amp;quot; /v &amp;quot;1.0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
You can also add that into a *.bat file and create symbols automatically when every building.&lt;br /&gt;
Then File-&amp;gt;Kernel Debug... or [Ctrl+k] select &amp;quot;COM&amp;quot; tab and set &amp;quot;Baudrate&amp;quot; to 115200 and set port to &amp;quot;COM1&amp;quot;,confirm and wait the target to connect.&lt;br /&gt;
etting up the target:&lt;br /&gt;
&lt;br /&gt;
/usr/local/bin/qemu-system-x86_64 \--enable-kvm \-m 1024 \-drive file=win-target.img \-chardev stdio,id=mon0 \-mdev=mon0 \-serial tcp:127.0.0.1:4445&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Add &amp;quot;-serial tcp:127.0.0.1:4445&amp;quot; to connect to the host by virtual serial,port is same to the server.&lt;br /&gt;
When start up,if Windows XP,2003 or 2000,edit c:\boot.ini and duplicate the default boot line,at the end of the duplicated boot line add&lt;br /&gt;
&lt;br /&gt;
/debug /debugport=COM1 /baudrate=115200&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
the baudrate and debugport must be identical to kernel mode configure of WinDbg.&lt;br /&gt;
If Windows7,Vista or up,you must use BCDedit(ref:http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx  and http://msdn.microsoft.com/en-us/library/ff542187.aspx ).&lt;br /&gt;
&lt;br /&gt;
Change the boot opition:&lt;br /&gt;
&lt;br /&gt;
bcdedit /dbgsettings SERIAL \[DEBUGPORT:COM1\] \[BAUDRATE:115200\]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
All completed,restart the target virtual machine and select the DEBUG boot option in the boot menu.&lt;br /&gt;
Then WinDbg on host will try to connect to the windows guest(target) kernel to debug.&lt;br /&gt;
You can &amp;quot;Ctrl+Break&amp;quot; or use the &amp;quot;Pause&amp;quot; button to break the guest running and also you can do anything which the debug tool support.&lt;br /&gt;
=Debug=&lt;br /&gt;
==HOWTO debug the guest driver==&lt;br /&gt;
Compile and build:&lt;br /&gt;
&lt;br /&gt;
We first create a simple windows driver for test,it is named viotest and added in the kvm-windows-guest-driver project to maintain.&lt;br /&gt;
The source code:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;virtio_test.h&amp;quot;&lt;br /&gt;
#include &amp;quot;virtio_test_utils.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload(IN PDRIVER_OBJECT DeviceObject);&lt;br /&gt;
&lt;br /&gt;
    ULONG; DriverEntry( IN PDRIVER_OBJECT&amp;amp;; DriverObject, IN PVOID; RegistryPath ) {&lt;br /&gt;
    ULONG initResult = 0;&lt;br /&gt;
&lt;br /&gt;
    DbgPrint(&amp;quot;Viostor driver started...built on %s %s\n&amp;quot;, \__DATE__, \__TIME__);&lt;br /&gt;
    DriverObject-&amp;gt;DriverUnload = DriverUnload;&lt;br /&gt;
    DbgPrint(&amp;quot;Initialize returned 0x%x\n&amp;quot;, initResult);&lt;br /&gt;
&lt;br /&gt;
    return initResult;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
VOID DriverUnload( IN PDRIVER_OBJECT DeviceObject ){&lt;br /&gt;
    DbgPrint(&amp;quot;Driver Unload successfully\! \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Add Makefile and SOURCES file into the project&lt;br /&gt;
The driver only include DriverEntry(),DriverUnload() and some message print.&lt;br /&gt;
The driver is not related to any specific device. All the simplification are done in order to increase the convenience of debug.&lt;br /&gt;
&lt;br /&gt;
Then using WinDDK build tool to build the driver,I use the &amp;quot;Windows XP x86 checked build&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
viotest&amp;gt;build -cCzg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After build completely,the driver(viotest.sys,viotest.inf) will be exported into the {{...\viotest\objchk_wxp_x86\i386\}}.&lt;br /&gt;
Load,install and debug the driver:&lt;br /&gt;
&lt;br /&gt;
After the process of II and III mentioned below,we copy the viotest directory to the target guest.&lt;br /&gt;
HOST:&lt;br /&gt;
WinDbg is now running in the host guest.&lt;br /&gt;
We &amp;quot;Ctrl+Break&amp;quot; to pause the debugger,and set breakpoints:&lt;br /&gt;
&lt;br /&gt;
kd&amp;gt; bu !viotest:DriverEntry&lt;br /&gt;
kd&amp;gt; bu !viotest:DriverUnload&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
You can use &amp;quot;bl&amp;quot; to watch breakpoints set&lt;br /&gt;
&lt;br /&gt;
.&lt;br /&gt;
 &lt;br /&gt;
F5,continue.&lt;br /&gt;
&lt;br /&gt;
GUEST:&lt;br /&gt;
Using tool &amp;quot;DriverStudio&amp;quot; to load,start,stop the driver.&lt;br /&gt;
File-&amp;gt;Open select the &amp;quot;viotest.sys&amp;quot;, and then start (Go)the driver.&lt;br /&gt;
Meanwhile,the WinDbg will hit the breakpoint at viotest.c:DriverEntry().&lt;br /&gt;
And the related source file will be opened to view.&lt;br /&gt;
You can see the breakpoints position with highlight label in the source code.&lt;br /&gt;
&lt;br /&gt;
You can watch the variables value,check the callstacks etc. All behaviors of WinDbg are similar to other debuggers.&lt;br /&gt;
&lt;br /&gt;
The debug message will be show in the WinDbg.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;viotest driver started...build on Nov 1 2011 05:04:18&amp;quot;&lt;br /&gt;
&amp;quot;Initialize return 0x0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When stop(unload) the driver,the breakpoint will also be hit at DriverUnload().&lt;br /&gt;
The debug message will also be show in the WinDbg:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Driver Unload sucessfully\!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Other Debug Method==&lt;br /&gt;
* Using Debug message(print):&lt;br /&gt;
      The message can be viewed by another tool &amp;quot;DebugView&amp;quot;,it can be used in target guest(without WinDbg) only environment to debug the driver with the DbgPrint() debug message. It is also a available method to debug the driver.&lt;br /&gt;
* Using crash dump (kernel memory dump)with WinDbg&lt;br /&gt;
TBD.&lt;br /&gt;
&lt;br /&gt;
==Problems may be encountered==&lt;br /&gt;
&lt;br /&gt;
* WinDbg somtimes exit unexpectedly when connecting to the target&lt;br /&gt;
I have not found the exact reason,but I found this problem often appears when changing the QEMU virtual hardware configuration or the target is not be shut off normally.It may be the Windows registry conflict cause this issue.&lt;br /&gt;
* WinDbg and virtual machine runs slowly&lt;br /&gt;
It is recommended by Avi Kivity( avi@redhat.com)using QEMU-KVM which is significantly faster,especially running Windows,but the WinDbg will almost exit everytime when using qemu-kvm git.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*1).http://www.linux-kvm.org/page/WindowsGuestDrivers/GuestDebugging &lt;br /&gt;
*2).http://msdn.microsoft.com/library/windows/hardware/gg507680 &lt;br /&gt;
*3).Debugging tools for Windows HELP&lt;br /&gt;
*4).Developing_drivers_with_the_Microsoft_Windows_Driver_Foundation by Penny Orwich and Guy Smith,Microsoft Press 2007&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;(author: caobbu@cn.ibm.com )&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4381</id>
		<title>WindowsGuestDrivers</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4381"/>
		<updated>2011-11-07T03:35:00Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Windows guest drivers =&lt;br /&gt;
This is a main page for the Windows guests drivers.&lt;br /&gt;
&lt;br /&gt;
Current drivers are base on [[Virtio|VirtIO]] interface.&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/kvmnet|kvmnet - NDIS miniport network driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/viostor|viostor - STORPORT miniport driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Download Drivers|Download binary drivers]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Repository|Repository]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Git_and_crlf|git and cr\lf]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/HOWTO|HOWTO]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/GuestDebugging|Windows Guest Drivers debugging]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Links|Useful links]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Building the drivers|Building the drivers]]&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4380</id>
		<title>WindowsGuestDrivers</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4380"/>
		<updated>2011-11-07T03:33:36Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Windows guest drivers =&lt;br /&gt;
This is a main page for the Windows guests drivers.&lt;br /&gt;
&lt;br /&gt;
Current drivers are base on [[Virtio|VirtIO]] interface.&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/kvmnet|kvmnet - NDIS miniport network driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/viostor|viostor - STORPORT miniport driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Download Drivers|Download binary drivers]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Repository|Repository]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Git_and_crlf|git and cr\lf]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/HOWTO|HOWTO]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/GuestDebugging|Guest debugging]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Links|Useful links]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Building the drivers|Building the drivers]]&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
	<entry>
		<id>https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4379</id>
		<title>WindowsGuestDrivers</title>
		<link rel="alternate" type="text/html" href="https://linux-kvm.org/index.php?title=WindowsGuestDrivers&amp;diff=4379"/>
		<updated>2011-11-07T03:27:54Z</updated>

		<summary type="html">&lt;p&gt;Caobingbu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Windows guest drivers =&lt;br /&gt;
This is a main page for the Windows guests drivers.&lt;br /&gt;
&lt;br /&gt;
Current drivers are base on [[Virtio|VirtIO]] interface.&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/kvmnet|kvmnet - NDIS miniport network driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/viostor|viostor - STORPORT miniport driver for Windows guests]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Download Drivers|Download binary drivers]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Repository|Repository]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Git_and_crlf|git and cr\lf]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/HOWTO|HOWTO]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/GuestDebugging|Guest(driver) debugging]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Links|Useful links]]&lt;br /&gt;
&lt;br /&gt;
[[WindowsGuestDrivers/Building the drivers|Building the drivers]]&lt;/div&gt;</summary>
		<author><name>Caobingbu</name></author>
	</entry>
</feed>