Showing posts with label WinDBG. Show all posts
Showing posts with label WinDBG. Show all posts

Monday, June 01, 2009

Heap Debugging (Memory/Resource Leak) with WinDbg

I recently had to do some heap debugging to solve an issue at work and it was a bit of a pain in the butt because there are several steps that I needed to take to set everything up. Here's what I had to do...

  1. First, you need the Debugging Tools for Windows installed.
  2. Open a command prompt and navigate to the debugging tools folder (usually C:\Program Files\Debugging Tools for Windows (x86).
  3. Type "gflags.exe /i yourApplication.exe +ust" (without the quotes). This command creates a user mode stack trace database that will be used later.
  4. Launch the application in WinDbg.
  5. Recreate the error and then break into the debugger.
  6. Run the command "!heap -s". This will display a summary of all of the current heaps. Find the one that seems to be bloated.
  7. Run the command "!heap -stat -h " on the block that appears to be bloated. This will display statistics on the heap's allocations by allocation block size. My problem was that one resource type was being leaked so there was one block size that was used far more frequently than the others. Find that block size.
  8. Run the command "!heap -flt s ". This will filter the heap to only show heaps of that particular size.
  9. Now choose one of the UserPtr's in the list and run the command "!heap -p -a " on it. This will display information on that particular block. It should also include a stack trace from when that memory was allocated.
  10. Remember to remove the global flags by doing "gflags.exe /i -ust" when you're done.

Friday, April 27, 2007

Getting Started with WinDbg

I discovered WinDbg about a year ago and have found it to be the most powerful and complete debugger available for Windows PCs. Unfortunately, it can be a very intimidating debugger since most of the work is done via text commands. I'm going to use this blog entry as a place to dump most of my learnings about WinDbg. It will be updated quite often until I feel I've documented enough to use this as a reference to teach someone how to use it.

Setting WinDbg as the default Just-In-Time (JIT) Debugger


From the command line, run:
C:\> cd C:\Program Files\Debugging Tools for Windows
C:\Program Files\Debugging Tools for Windows> WinDbg -I

Adding Microsoft Symbol Server to the Symbols Path

Launch WinDbg, then go to File>Symbol File Path and add SRV*C:\Windows\Symbols*http://msdl.microsoft.com/download/symbols to the paths.

When you exit WinDbg, it will ask you if you wish to save changes for Workspace Base. You must select YES or it will not save your symbol path.

Monday, September 11, 2006

Remote Debugging with WinDBG

This is not really a tutorial but rather a quick summary of how to do this so I can find this information quickly in the future:

On the "debugging server" (the PC with the problem)
  1. Start WinDBG
  2. Type .server npipe:pipe=pipename
On the "debugging client" (the one you're sitting at)
  1. Start WinDBG
  2. Go to File>Connect to Remote Session
  3. For Connection String enter npipe:server=Server, pipe=PipeName [,password=Password] where Server is the hostname/IP and PipeName is the name that the server chose. The password section is optional.