harriyott.com

Tuesday, May 23, 2006

Debugging return values in Visual Studio

Let us suppose that we're debugging an application in Visual Studio .NET 2005. Let us also suppose that we have a method similar to the following:

// Let us also suppose that this is good code.
public int GetFalseStapleWotsit()
{
return staple.GetWotsit(false);
}

Let us suppose that we had a breakpoint on the closing curly brace, and that we had reached said breakpoint during normal debugging. Let us assume also that we have no code or .pdb for the assembly containing the staple object's class definition, and thus we cannot step into it.

How would we know what value staple.GetWotsit(false) had returned? There are no variables to show up in the autos, locals or watch windows, and there is nothing to hover over in the code. The only solution I have come up with so far is to put staple.GetWotsit(false) into the immediate window (Ctrl+D+I) and press enter. This calls the function again, and puts the returned value on the next line. Surely this can't be the best way. I'll just wait for a big-brain to leave me a comment to tell me exactly where I'm going wrong.

[Tags: ]

4 Comments:

Anonymous Michael said...

Good question. I've been searching for an answer to the same question and ran across your post. No one's given you a suggestion in all this time?

February 08, 2007 1:49 AM  
Blogger Simon said...

No, I haven't had any suggestions, but then I'm hardly an A-lister!

February 08, 2007 9:45 AM  
Blogger Aldur said...

If the value returned is a float it will be in the memory registry ST0, if it is an int, or a pointer it will be in the memory registry EAX.

In VS 2005 these values show up in the Autos when you are right click the source and choose "Go to Disassembly".

While in disassembly view, step over (F10) until just after the instruction "call Staple::GetWotsit". The value of EAX will be a pointer to the returned type, or an int/byte/float (float is in st0). Make sure you are viewing the Value in hexadecimal mode. (Right click the Value box in your watch box and check Hexadecimal display) Open up a quick watch and enter "(Foo *)0xFEDBCA09" Where Foo is the name of the returned class/struct. You can get that from the method signature. FEDBCA09 should be the value of the memory registry EAX immediately after the call assembly statement.

Cheers,

Aldur

May 05, 2008 9:23 PM  
Blogger Simon said...

There we go. It took nearly two years to find out, but there was a better way. Thanks Aldur.

May 06, 2008 8:54 AM  

Post a Comment

Links to this post:

Create a Link

<< Home