Click here to Skip to main content
Click here to Skip to main content

Quick start to using WinDbg

, 18 Dec 2007
Rate this:
Please Sign up or sign in to vote.
Step by step instructions for a newbie on how to use WinDbg.

Introduction

WinDbg is quite a powerful debugger, and it’s really easy to use it. It provides you with both UI and command prompt interfaces. The installation package is light, and it can be quickly installed on a target machine.

I noticed that for first timers, it is confusing to use WinDbg. One needs to point to the right location of the source files, the PDB file, and the executable before he/she can debug using WinDbg. It’s not very intuitive for a newbie with little debugging experience outside of Visual Studio. In this short demo, I would like in pictures, pedantically show a step by step of how to set WinDbg up for your debugging purposes.

Installation

First, one needs to install the debugging tools MSI package. The latest MSI package for this could be taken from the Microsoft site.

There are several MSI packages one could download and install, choose the 32-bit or 64-bit version and download the latest release. After the installation is complete, you can invoke WinDbg from your Start menu:

Screenshot - 1.jpg

Sample program

Now, let’s create a small C++ program to calculate the GCC – the greatest common divisor.

File main.cpp:

#include <span class="code-keyword"><stdio.h> </span>
#include <span class="code-keyword"><stdlib.h> </span>

int calculateGCD(int a, int b); 
void printHelp(); 

int main( int argc, const char* argv[] ) 
{ 
    int numA, numB, gcd; 

    if (argc != 3) 
    { 
         printHelp(); 
         return -1; 
    } 
    
    numA = atoi( argv[1] ); 
    numB = atoi( argv[2] ); 

    if(numA == 0 || numB == 0) 
    { 
         printf("Wrong input format, see help"); 
         printHelp(); 
         return -1; 
    } 
    
    // Make sure that numA is not less then numB 
    // otherwise change them places 
    if (numA < numB) 
    { 
        numA = numA ^ numB; // 0^1 = 1, 0^0 = 0, 1^1 = 0, 1^0=1; 
        numB = numA ^ numB; 
        numA = numA ^ numB; 
    } 

    gcd = calculateGCD(numA, numB); 
    printf("Common divisor is: %d", gcd); 
    return 0; 
} 

int calculateGCD(int biggerNum, int smallerNum) 
{ 
    int remainder = biggerNum % smallerNum; 
  
    if (remainder == 0) 
      return smallerNum; 
  
    return calculateGCD(smallerNum, remainder); 
} 

void printHelp() 
{ 
     printf("This tool calculates a common greatest divisor"); 
     printf("Usage: comdiv integerNumberA integerNumberB"); 
}

Now, compile and build it in debug mode so you can get the comdiv.exe and comdiv.pdb files. Let's put these files and the source file main.cpp into a dedicated folder like C:\windbgtest.

Screenshot - 2.jpg

WinDbg

Now, let's start WinDbg from the Start menu.

Screenshot - 3.jpg

Now, in order to start debugging, we need to point WinDbg to the symbol file comdiv.pdb, the source file main.cpp, and the executable comdiv.exe.

Go to File->Symbol File Path, and specify C:\windbgtest in the dialog.

Screenshot - 4.jpg

Do the same for File->Source File Path, and File->Image File Path.

Now, go to File->Open Executable, and specify the comdiv.exe file in the dialog.

Screenshot - 5.jpg

Also specify the arguments as in the picture. This will make comdiv.exe to find the GCC of 18 and 24. Once you hit the Open button, press No for the popped up dialog, suggesting to save the information for the workspace (it doesn’t really matter).

At this stage, you will see that WinDbg has created the comdiv.exe process:

Screenshot - 6.jpg

Now, WinDbg has just created the process to run comdiv.exe, but it has not yet run it. So, at this point, it’s your chance to put some breakpoints.

Let’s open the source file main.cpp in WinDbg and put breakpoints there. Go to File->Open Source File, and specify C:\windbgtest\main.cpp there.

Screenshot - 7.jpg

Once you push Open, you will see your source code:

Screenshot - 8.jpg

Now, put the cursor on line 11 (“if (argc != 3)”) and press F9 (or click the “hand” icon in the Toolbar).

You might get this dialog popped up:

Screenshot - 9.jpg

If you get this dialog, just press Yes, and you will have your breakpoint set up:

Screenshot - 10.jpg

Now, the red line means that your breakpoint is set up and ready to be hit.

Press F5 to run the program, and it will break on your breakpoint line (you will see that the red line becomes pink):

Screenshot - 11.jpg

Now, you can step through your source code using either the Toolbar or the standard F* keys: F10 – step over, F11 or F8 – step into, etc.

There are numerous standard debug commands available in the View and Debug menus, just explore and try them all.

You can also navigate your mouse over the local variables and see their values (like in the picture below; you can also see the current execution point, it's highlighted blue):

Screenshot - 12.jpg

Now, you are all set to go!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ganik
Software Developer
United States United States
I am a Software Developer based in Seattle, WA

Comments and Discussions

 
Generalits very helpful PinmemberMember 1018340431-Aug-13 10:56 
Generalthanks Pinmemberzhangyoung6-Sep-12 17:09 
QuestionThanks! Pinmemberxzz019517-Apr-12 9:01 
GeneralThe most simple way PinmemberBathula.Sreekaanth19-Jan-11 1:43 
GeneralMy vote of 4 PinmemberBathula.Sreekaanth19-Jan-11 1:40 
GeneralGreat WinDbg For Beginners article Pinmemberriverblues3-May-10 6:31 
GeneralGood article for Beginner on WDBG Pinmemberchidigam23-Mar-10 0:50 
GeneralThis is just what I need! Pinmember_robin9-Sep-09 19:48 
GeneralUse USB WinDbg on VISTA Pinmemberflyball123024-Feb-09 16:00 
Generalthanx PinmemberAmbikaTK9-May-08 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 19 Dec 2007
Article Copyright 2007 by Ganik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid