views:

1390

answers:

7

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 !

A: 

Not going to happen I don't think. (At least I don't know of a way.)

One option, however, is something like the following. It at least keeps the original code in place, while letting you change the return value in debug mode.

   private int myMethod_1()
   {
#if DEBUG
      int x = 12;
      return x;
#else
      return 12;
#endif
   }
lc
This can be handy in some situation (when the return value is a complex expression). Most often, however, I would prefer to not touch the code since I want to reproduce something that happened in production. Note that the compiler is probably already replacing the first 2 lines by the third. Thank.
Sylvain
If you go this route you're much better off just having one version of the code. The intermediate variable is eliminated in Release builds anyway.
romkyns
A: 

I think you can do it in Visual Studio but that feels dirty to me.

My thoughts are you should really be looking into a unit testing framework and mocking so that you can fake the output of myMethod_2 as a 0 and see what effect it has on your code. IMO. This way you can leave the return 0 check in the unit test to make sure the ugly bug doesn't stick its head up again.

Nathan W
This could do the job I think. But it is a bit complicated : when I have to recreate something that happened in production code, I 1) must work quickly 2) should not change too much things. I have to investigate this. Thanks.
Sylvain
+1  A: 

Why do you want to change the return value in the called method? You can easily change it on-the-fly in the Calling method. As soon as your function returns, you get the value returned and there you can change it.

   private int myMethod_1()
   {
      return 12;
   }
//call would be something like this
int x = myMethod_1();
//Here you can change the value after the execution of the above line.

Even if you are not storing return value in a variable, and you are directly passing it to some other method, even then you can change the value by stepping into that method and changing the argument value there. Am I missing something here?

Aamir
You are right : we can do that very often. What is bugging me, are calls like this one : APICallIDontHaveTheCode( MyCallB(), MyCallC()) (this is very frequent). I should have point it out in the question. The question - how to change the return value of MyCallC() ? Thanks.
Sylvain
use a local variable to cache the value of MyCallC() before passing it in to APICAllIDontHaveTheCode
Gishu
Yes, that will definitely works. It still have been handy if we were able to change a value on the fly :o). Thank for you answers !
Sylvain
If your code is unmanaged, then you have an option. put eax in your watch window, this will always contain the last returned value. You can change it as per your needs. I am still finding a way to do this in Managed/Mixed code :).
Aamir
@Aamir - The last answer below is also about changing EAX's value. Sound interresting. I'm only using managed code for the moment, however :o(
Sylvain
-1 since this doesn't work in 64-bit... :(
romkyns
A: 

Don't think so.
You'd need to have a temp var in the callee or the caller so as to get a handle on the value to modify it in the IDE Debugger/QuickWatch window. So the simplest and fastest way to do it would be to comment existing code and make a temporary change for debug-what-if.

either

private int myMethod_1()
{
  var x = 12;
  return x;
}

Or

private int myMethod_2()
{
  var y = someCall( someValue);
  return y;
}

Don't see it worth the aggravation of 'avoiding a code change' to do this. If I FUBAR, I do a checkout and we're gold again.

Gishu
You are probably right... It is what I'm doing most of the time but is is cumbersome. Imagine : MyCall( MyCallA(), MyCallB()); Often, MyCallA() is just a serie of calls that call calls... Well, I would have been very nice to be able to change a return value on the fly. Thanks.
Sylvain
+2  A: 

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.

shoosh
In fact, I was looking for something like that since most of the time, I'm tring to replace the return value for a null value (should be 0 ?). Well, I just tried it out but I was not able to change the EAX value (even in dissasembly mode). Any hint ? Maybe my settings ? Thanks.
Sylvain
+1  A: 

It was so simple, I can't believe I did not see it : To look at the value a function will return : just place the pointer over the function's name. The value will be shown in a tool tip.

The change the value : just click on this tool tip, change the value and hit enter.

Visual Studio is very cool !

Note : I tested it in VB.NET on Visual Studio Team System 2008. Just tried using C#, but it does not work... :-(

Sylvain
Indeed, it doesn't work in C#...
romkyns
+1  A: 

What you need is Mocking. Checkout mocking frameworks like Rhinomock, Moq or Typemock.

For the kind of scenario you are describing, I am going to assume that you may not have clean contract - driven development and may need to return in fake behaviour/state. In this case, Typemock may be the best option for you although it is commercial

Very great hints, but not exactly for what I an looking for. I will test Rhinomock, Moq and Typemock ASAP. Tank for you answer !
Sylvain
-1 - the idea is to change this in the debugger. That's what the debugger is for. If you are going to change your code _just for this_ then a **much** easier transformation is `var result = blah; return result;`.
romkyns