This is part one of a multipart series, here's our outline of whats in store:
- Part 1 - Installation, Interface, Symbols, Remote/Local Debugging, Help, Modules, and Registers
- Part 2 - Breakpoints
- Part 3 - Inspecting Memory, Stepping Through Programs, and General Tips and Tricks
InstallationMicrosoft has changed things slightly in WinDBG's installation from Windows 7 to Windows 8. In this section we'll walk through the install on both.
Windows 8Windows Driver Kit (WDK) You can install Visual Studio and the WDK or just install the standalone "Debugging Tools for Windows 8.1" package that includes WinDBG.
This is basically a thin installer that needs to download WinDBG after you walk through a few screens. The install will ask you if you'd like to install locally or download the development kit for another computer. The later will be the equivalent of an offline installer, which is my preference so that you can install on other systems easily in the future.
From there just Next your way to the features page and deselect everything but "Debugging Tools for Windows" and click "Download".
Once the installer completes you can navigate to your download directory, which is
c:\Users\Username\Downloads\Windows Kits\8.1\StandaloneSDKby default, and then next through that install. Then you're all ready to go!
Windows 7 and BelowFor Windows 7 and below, Microsoft offers WinDBG as part of the "Debugging Tools for Windows" package that is included within the Windows SDK and .Net Framework. This requires you to download the online/offline installer, then specifically choose the "Debugging Tools for Windows" install option.
My preference is to check the "Debugging Tools" option under "Redistributable Packages" and create a standalone installer which makes future debugging efforts a heck of lot easier. That's what I'll do here.
Once the installation completes, you'll should have the redistributable for various platforms (x86/x64) in the
c:\Program Files\Microsoft SDKs\Windows\v7.1\Redist\Debugging Tools for Windows\directory.
From there the installation is pretty simple, just copy the appropriate redistributable to the system you're debugging and then click through the installation.
The most basic thing about the interface you should know is the Command window. It's the default window opened once you're attached to a process. The Command window is mostly an output only window, with a small input field on the bottom which you'll enter commands into to control WinDBG.
SymbolsWinDBG doesn't really need much of a configuration, most things work right out of the box. The one important thing to do is set up Symbols. Symbols are basically special files that are generated with the program binary at compile time that provide debugging information such as function and variable names. This can really help demystify a lot of the functionality of an application when debugging or disassembling. Many Microsoft components are compiled with Symbols which are distributed via the Microsoft Symbol Server. For non-Microsoft binaries, you're usually out of luck - sometimes you'll find them laying around somewhere but mostly all companies keep that stuff protected.
To configure WinDBG to use the Microsoft Symbol server go to
File:Symbol File Pathand set the path appropriately to the one below. The syntax is a little weird, asterisks are the delimiter, so in the value below, we'll download symbols to the
WinDBG will automatically load Symbols for binaries that it has them for when needed. To add a file containing symbols you can just append it to the path:
Adding Symbols during DebuggingIf you do run into a situation where you have Symbols and would like to import them while the debugging, you can do so via the
.sympathcommand option within the command window (this requires you to be attached to a process). For instance to append
It's always good to reload the symbols after you make changes to the path:
Checking SymbolsTo view what modules have symbols loaded, you can use the
x*!command. However, WinDBG doesn't load Symbols until it needs them so
x*!will show most of the module symbols are deferred. We can force WinDBG to load symbols, with the
ld *command (which may take a little time, you can stop it by going to
Now we can view the symbols for each for the modules:
Debugging a Local ProcessYou have a couple options when debugging a local process. You can start the process then attach to it, or have WinDBG launch the process for you. I'm really sure of all the advantages/disadvantages of each - I know that when you launch a program with WinDBG, it enables some special debugging options (e.g. debug heap) that the program may not like, and it will crash. Additionally, those debugging options alters the code paths in which the programs takes which modifies how things are arranges in memory. That being said, there are also programs that will crash when you attach the debugger, so ymmv. Some applications (malware in particular) will look for the presence of the debugger at launch and may not later on, which would be a reason why you'd attach. And sometimes you're debugging a service that is controlled by Windows which sets up a variety of things during its launch, so to simplify things, you'd attach rather then launch via the debugger. Some people say there is a significant performance impact when launching a process via the debugger. Test it out yourself, and see what works best for you. If you have any particular reasons why you'd do one over the other, please let me know the comments!
Starting a ProcessIf you're debugging a self contained application that just runs locally and doesn't communicate via the network, you may want to have WinDBG start the application. However, that's not to say you can't attach to these programs after they've been launched.
Starting a process is pretty straight forward, go to "File:Open Executable". From there, select the executable you'd like to debug. You can also provide command line arguments and define the start directory:
Attaching to a ProcessAttaching to an already running process is just as simple. Note, that in some cases, you'll need to need to spend a little time identifying the true process you're looking to target. For instance, some web browsers will create one parent process, then an additional process for each tab. So depending on the crash you're debugging, you might want to attach to the tab process rather than the parent.
To attach to an already existing process, go to "File:Attach to a Process" then select the PID or process name to attach to. Keep in mind you'll also need the appropriate rights to attach to your target process.
If the program has stopped responding, you can noninvasively by using the "Noninvaise" checkbox.
Debugging a Remote ProcessNow there may be times where you have to debug a process on a remote system. For instance, it may just be more convenient to use a local debugger rather than one within a VM or via RDP. Or perhaps you are debugging
LoginUI.exe- which is only available while the system is locked. In these situations you can have a local WinDBG instance running then remotely connect to it. There are a couple ways to do this as well - we'll cover two of the most common ways.
Existing Debugging SessionsIf you've already started to debug the program locally (via attaching or launching mentioned above) you can use the command input field to have WinDBG launch a listener that a remote debugger can connect to. This is done with the
You'll likely get a security alert that you should allow:
Then a positive message within WinDBG telling you the server has started:
Then from the remote host, you can connect to the existing debugging session via "File:Connect to a Remote Session":
Once connected you'll get a confirmation on remote client:
and the locally debugging instance:
Remote ServerYou can also have a standalone WinDBG server running on a system, remotely connect to it, then have the ability to select what process to attach to. This can be done using the
executable on the system where the process is (or will be) running:
And you'll likely get a Windows Firewall notice, which you should allow:
From the remote system, you can connect by going to "File: Connect to Remote Stub" and defining the server:
You won't get any obvious indicator that you're connected, but when you go to "File:Attach to a Process", you'll see the process list of the system you're running
dbgsrv.exeon. Now you can attach to a process as you normally would as if the process was local.
HelpWinDBG's help system is awesome. As with all new things, you should become familiar with how to get help on a specific command or concept. From the command input you can use the
.hhcommand to access WinDBG's help:
You can also use
.hhon a specific command. For instance, to get more information on the
.reloadcommand, you can use:
Or just go to "Help:Contents".
ModulesAs program runs it pulls in a number of modules that provide functionality - thus if you're able to gain insight into what modules are imported by the application, it can help identify what the application does and how it may work. In many scenarios, you'll be debugging a particular module loaded by a program, rather than the program executable itself.
When you attach to process, WinDBG will automatically list the loaded modules, for instance, here's what WinDBG's output when I attached to
Later on in a debugging session you can reproduce these results with the
And you can get the load address for a specific module using the "lmf m" command:
To get the image header information you can use the
!dhextension (the exclamation mark denotes an extension) on a particular module.
Messages/ExceptionsWhen you attach to a process, the modules are displayed first, then WinDBG displays any applicable messages. When we attached to
calc.exe, WinDBG automatically sets a breakpoint (which is just a marker that tells the debugger uses to pause the execution of a program). So our message is:
This particular message is an exception, specifically a first chance exception. An exception is basically some special condition that occurred during the program's operation. The first chance means that the progam was paused right after the exception occurred. A second chance exception is when an exception has occurred, some programming logic to handle exception was executed, and the program has paused.
RegistersAfter the messages/exceptions, the debugger will output the state of the CPU's registers. Registers are basically special variables within the CPU that store a small amount of data or keep track of where something is in memory. The CPU can process the data in these registers very quickly, so its faster for the CPU to perform operations on the values in its registers rather then pulling information all the way down the bus from RAM.
WinDBG automatically outputted the following registers after we attached to
Later on down the line, we can reproduce this with the
And if we wanted to just retrieve a value of a specific register, we could by appending the register name:
and multiple registers:
Instruction PointerThe final line is instruction to be executed. This is outputted as part of the
rcommand and is what the EIP register contains. EIP is the instruction pointer, which is the register that contains the location of the next instruction for the CPU to execute. WinDBG's output is equivalent of the
u eip L1command that basically tells WinDBG to go to the memory location pointed to by EIP, treat that memory as assembly, and print out one line.