Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - How to change the return value of a method in the debugger?

When I'm debugging, I often have to deal with methods that does not use an intermediate variable to store the return value :

   private int myMethod_1()
   {
      return 12;
   }

   private int myMethod_2()
   {
      return someCall( someValue );
   }

Well, in order to recreate a bug, I often have to change values on the fly. Here, I could want to see what happens when myMethode_1 return zero. Same thing for myMethod_2.

Is there a way to do that without modifying the code ? What I would like to do, it's to place a breakpoint on the return line (or on the closing bracket) and type-in a new return value.

Note : I'm using Visual Studio 2005 and Visual Studio 2008.

Thank !

like image 602
Sylvain Rodrigue Avatar asked Jan 19 '09 05:01

Sylvain Rodrigue


1 Answers

Return values from functions are usually returned in the EAX register.

If you set a breakpoint just at the end of the function then there's a chance that changing EAX would change the return value. You can change and view any register in visual studio simply by writing its name in the watch window.
This is likely to fail if you have optimization on or even if the function is something simple like return 12. it's probably also not going to work if you're returning something that doesn't fit in a 32 bit register. In the least it's worth trying.

like image 196
shoosh Avatar answered Oct 28 '22 23:10

shoosh