Using registers for return value debugging in Visual Studio

Often, I find myself desiring to know the return value of function.  Normally, I can set a print message breakpoint on the following line after the function call:

bool fFlag = pMyClass->FGetSomeFlag();
if( fFlag )  // <-- Set breakpoint here
  DoSomething( pMyClass );

However, this doesn't work in all cases.  Suppose we want to know what the system call to ::GetTickCount() returns.  We don't have a variable to output and calling the function in the debugger (if it were even possible) would return inaccurate values.  We can use our handy registers to output the value of GetTickCount().  We know that the return value will be stuffed into the EAX (32bit) and RAX (64bit) registers.  Note, you can usually get away with using EAX on 64bit machines.  But, it's handy to know that the full 64bit value is in RAX.  Let's look at a little program to illustrate:

DWORD GetCurrentTickCount()
   return ::GetTickCount();
} // <-- Set when-hit breakpoint here with:
  // TickCount32: {EAX} TickCount64: {RAX}
bool FOddTickCount()
   return GetCurrentTickCount() & 1;
void main()
   forint i = 0; i < 10; i++ )
      bool fOdd = FOddTickCount();

If you look at the assembly, you see that the debugger has the ability to set a breakpoint after the call to GetTickCount():

   return ::GetTickCount();
000000013FDF1045  call        qword ptr [__imp_GetTickCount (13FDFB300h)]  
} // <-- Set when-hit breakpoint here with:
000000013FDF104B  add         rsp,20h  
000000013FDF104F  pop         rdi  
000000013FDF1050  ret  

This allows us to set breakpoints after functions are called with return values.  When I run the small program, my debugging output window looks like:

TickCount32: 355088539 TickCount64: 355088539
TickCount32: 355088602 TickCount64: 355088602
TickCount32: 355088664 TickCount64: 355088664
TickCount32: 355088726 TickCount64: 355088726
TickCount32: 355088789 TickCount64: 355088789
TickCount32: 355088851 TickCount64: 355088851
TickCount32: 355088914 TickCount64: 355088914
TickCount32: 355088976 TickCount64: 355088976
TickCount32: 355089038 TickCount64: 355089038
TickCount32: 355089101 TickCount64: 355089101

I use this technique quite a bit, hopefully it helps.

Comments (0)

Skip to main content