Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / Win32
Article

Quick start to using WinDbg

Rate me:
Please Sign up or sign in to vote.
3.71/5 (21 votes)
18 Dec 2007CPOL3 min read 139K   672   61   14
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 <stdio.h> 
#include <stdlib.h> 

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)


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

Comments and Discussions

 
Generalawesome Pin
Member 148237086-Jun-20 4:33
Member 148237086-Jun-20 4:33 
Generalawesome Pin
Member 148237086-Jun-20 4:33
Member 148237086-Jun-20 4:33 
QuestionThere is something with the #include Pin
Derriclyns25-Jun-15 0:06
Derriclyns25-Jun-15 0:06 
Generalthanks. Pin
boki62725-Aug-14 16:43
boki62725-Aug-14 16:43 
Generalits very helpful Pin
satishdeevi31-Aug-13 10:56
satishdeevi31-Aug-13 10:56 
Generalthanks Pin
zhangyoung6-Sep-12 17:09
zhangyoung6-Sep-12 17:09 
QuestionThanks! Pin
Your Display Name Here17-Apr-12 9:01
Your Display Name Here17-Apr-12 9:01 
GeneralThe most simple way Pin
Bathula.Sreekaanth19-Jan-11 1:43
Bathula.Sreekaanth19-Jan-11 1:43 
GeneralMy vote of 4 Pin
Bathula.Sreekaanth19-Jan-11 1:40
Bathula.Sreekaanth19-Jan-11 1:40 
GeneralGreat WinDbg For Beginners article Pin
riverblues3-May-10 6:31
riverblues3-May-10 6:31 
GeneralGood article for Beginner on WDBG Pin
chidigam23-Mar-10 0:50
chidigam23-Mar-10 0:50 
GeneralThis is just what I need! Pin
_robin9-Sep-09 19:48
_robin9-Sep-09 19:48 
GeneralUse USB WinDbg on VISTA Pin
flyball123024-Feb-09 16:00
flyball123024-Feb-09 16:00 
Generalthanx Pin
AmbikaTK9-May-08 5:08
AmbikaTK9-May-08 5:08 

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

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