Age of Reason

Random musing of books and stuff I am reading.


Advanced Windows Debugging -- 1

This is a short tutorial for debugging C/C++ code on
Windows with VC++ (and windbg,ntsd,cdb) on x86.

You can download the free debugger windbg from msdn.
It is more powerful than VC++ in terms of scripting,
but less convinient GUI.

We will not cover gdb(linux), dbx(solaris), wdb(hp),
in this article, they also offer equivalent functionality.
If you use .Net or C# or Java or VB, this tutorial is not
for you.

The source was converted to html for the blog, by vim :TOHtml

> cat main.c

#include <stdio.h>
extern int errno;
int fx(int a, int b,int c){
int d;
d = a + b; // stop on 5th call.
return d+c;
void bad_open(void){
FILE *f,*g;
printf("f=%p g=%p, errno=%d\n",f,g,errno);
int main(){
int a=1,b=2,c=3,i,e;
if( i*i % 100 == 1 ) e = fx(a,b,c), e++, printf("fx=%d\n",e);
else a++, b++, c++;

When you have a cryptic error message, you can look at the
preprocessor output with:

> cl /E main.c | vim -

Now let's examine the assembly listing main.cod generated with:

> cl.exe /Fc main.c

Now is a good time to brush up your x86 assembly,
a good reference is "Art of Assembly" by Randall Hyde at UC Riverside.

> vim main.cod

Start with compiling your C program with Debugging
information on. In the next posting we will dissect and
debug it.

> cl.exe /ZI main.c

The /ZI switch creates main.pdb (program database).

To be continued.


Post a Comment

<< Home