Featured image of post Kernel Device Driver Debugging with WinDbg

Kernel Device Driver Debugging with WinDbg

How to Use WinDbg to Debug Kernel Device Drivers in Windows

How to Use WinDbg to Debug Kernel Device Drivers in Windows

So, you’ve written a shiny new kernel-mode device driver, and now it’s acting like a rebellious teenager—crashing, hanging, or just giving you the silent treatment. Fear not! WinDbg, Microsoft’s gift to kernel debuggers (and source of many headaches), is here to help you wrestle your code into submission. Grab some caffeine, breathe deeply, and let’s get you debugging like a pro.

Step 1: Setting Up Your Debugging Environment

Before you can start whispering sweet debug commands into WinDbg’s ears, you need to set up the environment. Here’s the high-level checklist:

  1. Install WinDbg: You can get it via the Windows SDK. Just search for “WinDbg Preview” in the Microsoft Store for the newer UI.
  2. Setup a Debugger and Target Machine: Kernel debugging usually requires two machines—one for debugging (host) and one running the suspect driver (target). You can use a VM if you’re short on hardware.
  3. Configure Debugging Connections: Serial, network, or USB debugging are your go-tos.

Pro tip: Network debugging is easiest these days—USB might make you want to smash your keyboard.

Configuring Network Debugging

Run this on the target machine:

1
2
bcdedit /debug on
bcdedit /dbgsettings net hostip:192.168.1.100 port:50000 key:123456789

On the host machine, in WinDbg:

1
2
File -> Kernel Debug -> NET
Enter the port and key you set earlier.

If it connects, you’re ready to roll. If not, prepare for a journey down the rabbit hole of firewalls and network issues.

Step 2: Crash, Burn, and Analyze

Now let’s cause some intentional havoc and debug it.

  1. Trigger a Crash: Install your buggy driver and poke it with your test app.
  2. WinDbg Magic: Open WinDbg on the host machine, connect to the target, and wait for the crash.
  3. Analyze the Crash: Once the machine crashes, WinDbg will scream with a screen full of cryptic text. Start with:
1
!analyze -v

Understanding the Output

The command above vomits out a bunch of technical jargon. Here’s what to look for:

  • BugCheck Code: The BSOD’s reason.
  • Stack Trace: The execution path before the crash.
  • Driver: The file likely responsible for the mess.

When in doubt, blame the driver. It’s always the driver.

Step 3: Inspecting Memory

If the crash points to your driver, dive into its memory:

1
!process 0 7

To inspect a specific variable:

1
dd address

Breakpoints: Your New Best Friends

  • Software Breakpoint: Stops when the code hits a line.
  • Conditional Breakpoint: Stops only if certain conditions are met.

Example:

1
bp mydriver!MyFunction

Step 4: Surviving Debugging Nightmares

WinDbg isn’t just a debugger—it’s a cryptic oracle. If things go sideways:

  • Recheck Network Settings: Firewalls love messing with WinDbg.
  • Update Symbols: Missing symbols = useless debugging.
1
2
.symfix
.reload
  • Stay Calm: Debugging is 80% frustration and 20% victory dance.

Bonus: Useful WinDbg Commands

CommandDescription
!analyze -vAnalyze crash in detail
lmList loaded modules
kShow current stack trace
dxDebugging with the data model
!drvobjShow driver object details

Conclusion

Kernel debugging with WinDbg can feel like performing brain surgery with a spoon, but once you get the hang of it, you’ll feel like a wizard. Remember: patience, caffeine, and lots of !analyze -v.


Key Ideas

IdeaDetails
Environment SetupTwo machines, WinDbg installed, network debug
Crash AnalysisUse !analyze -v to find crash causes
Memory InspectionCommands like dd, !process
BreakpointsSet with bp, ba, or bm
TroubleshootingSymbols, network issues

References

  1. WinDbg Documentation - Microsoft
  2. Windows Kernel Debugging Basics
  3. Debugging Tools for Windows

Now go forth and debug fearlessly—but remember, WinDbg doesn’t care about your feelings. Good luck!