- 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
BreakpointsBreakpoints are markers associated with a particular memory address that tell the CPU to pause the program. Because programs can contain millions of assembly instructions, manually stepping through each of those instructions would take an incredibly long time. Breakpoints help speed up debugging time by allowing you to set a marker at a specific function which allows the CPU to automatically execute all the code leading up to that point. Once the breakpoint is reached, the program is paused and the debugging can commence.
Breakpoints can be set in software and within the CPU (hardware), let's take a look at both:
Software BreakpointsPrograms get loaded into memory and executed - which allows us to temporarily modify the memory associated with a program without affecting the actual executable. This is how software breakpoints work. The debugger records the assembly instruction where the breakpoint should be inserted, then silently replaces it with an
INT 3assembly instruction (
0xcc) that tells the CPU to pause execution. When the breakpoint is reached, the debugger looks at the current memory address, fetches the recorded instruction, and presents it to the user. To the user it appears that the program paused on that instruction however the CPU actually had no idea it ever existed.
Software breakpoints are set within WinDBG using the
bp(for Break Point) is arguably the most used breakpoint command. In its most basic use, its only argument is the address at which a breakpoint should be set:
bp, the address should be a memory location where executable code exists. While
bpworks on locations where data is stored, it can cause issues since the debugger is overwriting the data at that address. To be safe Microsoft suggests that if you want to break on a memory location where data is stored, you should use a different breakpoint command (
ba, discussed below).
Let's take a look at setting a software breakpoint. Here we'll launch
notepad.exewith WinDBG. By default, when the program is launched with WinDBG, it will insert a breakpoint before the entry point of the program is executed and pause the program. First we'll get the location in memory where
Next we'll determine the program's entry point by using
!dhwith the image load address:
Now we'll set a breakpoint at it's entry point (load address + 0x3689):
Finally we'll tell the program to run until it encounters a breakpoint using the
gcommand (more on this later), when the breakpoint is hit, we'll get a notice:
Most of your debugging will likely use software breakpoints, however there are certain scenarios (read-only memory locations, breaking on data access, etc..) where you need to use hardware breakpoints.
Hardware BreakpointsWithin most CPUs there are special debug registers that can be used to store the addresses of breakpoints and specific conditions on which the breakpoint should triggered (e.g. read, write, execute). Breakpoints stored here are called hardware (or processor) breakpoints. There is a very finite number of registers (usually 4) which limits the number of total hardware breakpoints that can be set. When the CPU reaches a memory address defined within the debug register and the access conditions are met, the program will pause execution.
Hardware breakpoints are set within WinDBG using the
ba(Break on Access) command. In its most basic usage, it takes 3 attributes:
This command would (we'll see soon why it doesn't) accomplish the same thing as the previous
bpexample, however now we're setting a hardware breakpoint. The first argument,
e, is the type of memory access to break on (execute), while the second is the size (always 1 for execute access). The final is the address. Let's take a look at setting a hardware breakpoint, keep in mind our load addresses are different because of the whole ASLR thing.
Due to the way Windows resets thread contexts and the place where WinDBG breaks after spawning a process, we wont be able to set a breakpoint in the same way we did in our earlier example. Previously we set our breakpoint on the program's entry point, however if we try to do that with WinDBG we get an error:
So in order to get around this, we'll need to use that
gcommand and tell it to run the program until it reaches a specific memory address. This is sort of like setting a software breakpoint in behavior but isn't exactly the same. So we'll tell WinDBG to execute until we enter the program's initial thread context, which will then allow us to set hardware breakpoints.
Now we can set our hardware breakpoint:
To confirm we actually set the breakpoint in CPU's registers, we can use the
rcommand (discussed later). We'll use the
Mattribute to apply a register mask of
You'll notice something doesn't look right here, all of the registers contain
0! This is because WinDBG hasn't actually set them yet. You can single step (discussed below) with the
pcommand. Once we do, the
dr0register will have our breakpoint defined:
In this specific example, we probably will never hit our breakpoint because it is in the entry point of the program that we've already reached. However if our breakpoint was on a function that was called a variety of times in the life of the program, or on a memory address where an often used variable was stored, we'd get a "Breakpoint Hit" message when the memory was accessed just as we would with a software breakpoint.
Common CommandsNow that you have the basics of setting breakpoints, there are a handful of other breakpoint related commands that will be useful. Let's look at a couple:
Viewing Set BreakpointsTo view each of the breakpoints that have been set, you can use the
bl(Breakpoint List) command.
Here we have one breakpoint defined, the entry is broken into a few columns:
0- Breakpoint ID
e- Breakpoint Status - Can be
00523689- Memory Address
e 1- Memory address access flags (execute) and size - For hardware breakpoints only
0001 (0001)- Number of times the breakpoint is hit until it becomes active with the total passes in parentheses (this is for a special use case)
0:****- Thread and process information, this defines it is not a thread-specific breakpoint
notepad!WinMainCRTStartup- The corresponding module and function offset associated with the memory address
Deleting BreakpointsTo remove a breakpoint, use the
The only attribute to
bcis the Breakpoint ID (learned from
bl). Optionally you can provide
*to delete all breakpoints.
Breakpoint TipsThere are a couple simple tips that I commonly use when setting breakpoints. Here are a few of them, please share any you have in the comments below!
Calculated AddressesThe simplest breakpoint tip, is just something that you'll learn when dealing with memory addresses within WinDBG. You can have WinDBG evaluate expressions to calculate address. For instance, in the above examples, we knew the module load address of
notepad.exeand the entry point was at offset
0x3689. Rather than calculating that address ourselves, we can have WinDBG do it for us:
Name and Offset AddressesOne of the great things about Symbols (covered in part 1 of this post) is that they give us the locations of known functions. So we can use the offsets to those known functions as addresses in our breakpoints. To figure out the offset, we can use the
u(Unassemble) command within WinDBG.
uwill take a memory address and interpret the data at that memory address as assembly and display the corresponding mnemonics. As part of its output,
uwill also provide the offset to the nearest symbol:
Now we know that
notepad!WinMainCRTStartupis a friendly name for
00770000 + 3689. Since there isn't a numeric offset at the end of this friendly name, we can also infer that Symbols exist for this function. Look what happens when we check out the second instruction in this function:
This time we got a function name,
notepad!_initterm_e, plus an offset (
+0x61). I'm not entirely sure why WinDBG gave the offset to
notepad!WinMainCRTStartup, probably a symbol search order thing - nonetheless, we could have used a
notepad!WinMainCRTStartupoffset to reference the same location:
The point is that now we can use this offset as a breakpoint and those offsets are always valid even if ASLR is enabled - so we don't have to waste time calculating addresses at every launch.
Breaking On Module LoadThere may be some occasions when you'd like to set a breakpoint when a module is being loaded. Unfortunately, there doesn't appear to be an obvious way within the standard breakpoint commands to do this (let know if you know of a way in the comments). Instead a sort of "hacky" way to do this is by defining that an exception be raised when a particular module is loaded using the
Here we've set up a first chance exception (
sxe) when a module is loaded (
ld) and defined
IMM32.DLLas the specific module which triggers the exception.
We can use
sx(Set Exceptions) to view the configured exceptions. If we look under the Load Module list, we'll see that we have a break on
To clear it we can use the
sxi(Set Exception Ignore) command:
Executing CommandsThere may be certain commands that we execute every time a breakpoint is reached. For instance, say we're always interested at what values are on the stack. We can automate this with WinDBG by building a list of commands and appending it to our breakpoint. In our example, we'll print out some information, and use the
ddcommand (discussed later) to show the stack. Notice how our command is referenced in the
bloutput as well:
Let's see what happens when we hit our breakpoint:
As expected, the commands were executed, showing the "
Here are the values on the stack" message and the stack. Commands are chained together with a semi-colon, and be sure to escape quotes within the outer-most quotes that contain the entire command. You can even append the
gcommand to have the commands be executed and the program to just continue. This allows you to inspect the state of the program as it runs rather than manually interrupting it every time a breakpoint is hit.