ChatGPT解决这个技术问题 Extra ChatGPT

Can I find out the return value before returning while debugging in Visual Studio?

Take the following function:

DataTable go() {
    return someTableAdapter.getSomeData();
}

When I set a breakpoint in this function, is there a possibility to inspect the returned value? go() is directly coupled to a datagrid in an .aspx page.

The only way to inspect the returned datatable is to use a temporary variable. However, that's a bit inconvenient. Isn't there another way?

You can add a watch if you move back up the call stack
You used to be able to do this in VB6, I seem to remember. But back then the syntax for functions involved setting the function's value to the return value...
Comment for Visual C++ users: Type $ReturnValue in the Immediate Window or the Watch Window. At least on my VS 2010 it worked!
For VS2015 use $ReturnValue1 .. incase you dont want to read through the 20 answers and 100 comments below!
What's the 2019 answer to all of this? These answers are super dated.

A
Apfelkuacha

Not that I know of. Note that if you do add a variable, it will get removed by the compiler in release builds anyway...

Update: This functionality has been added to VS2013. You can see the return values in the autos windows or use $ReturnValue in the watch/immediate window.

The value can only be seen directly after returning from the function, thus the easiest way to access it is by putting a breakpoint on the function call and step over (F10) the call.

Update for VS2015: boo! unfortunately, it doesn't appear to be in VS2015 (devenv v14) Update for VS2017: it's back. (devenv v15)


The reason to forgo the temp is readability & style, not efficiency, no?
It is possible since VS 2010 with IntelliTrace: blogs.msdn.com/b/habibh/archive/2009/10/23/…
@MarcGravell Your answer is wrong! Sure, it took me six years between your reply and MS releasing that feature in VS13, but still. If you'd only had added "for the time being" as a disclaimer... (No, I'm not retarded. It is a joke, of course. You're godlike, mate.)
@MarcGravell for VS2015: $ReturnValue1 works! (tested in ultimate version)
I'll point out that in VS2019 (at least), the intellisense suggests $returnvalue all lowercase. It is case sensitive however.
P
Peter Mortensen

This can be done in Visual Studio 2013 with CLR 4.5.1 according to the customer feedback site. It was not available in previous versions for C#.

(Visual Studio 2008 and earlier supported it for VB.NET. It has always been available to C/C++ developers.)


How do you do this in Visual Studio 2010 C++?
Microsoft Connect says there is a fundamental issue with managed code that prevents implementing this in a reliable manner:
@DanSolovay The words they use are "we couldn't do the right thing consistently" (for VS11) but they "want to bring this back" and "are looking at a number potential solutions to this problem".
The connect entry is stale. The feature seems to be ... abandoned :(((
It is possible since VS 2010 with IntelliTrace: blogs.msdn.com/b/habibh/archive/2009/10/23/…
D
Dror Helper

I agree that this is a very useful thing to have: not only seeing the return value of the method before stepping out of it, but also seeing the return value of methods I just stepped over. I implemented it as part of a commercial extension to Visual Studio called "OzCode".

With it, you can view method return values right on the code editor, as sort of a HUD-display:

https://i.stack.imgur.com/Kw7mp.gif

For more information, please see this video.


Looks great, but 100 dollar per year for the Visual Studio Extension is really too much for private users. For companies this can be different.
D
Dan Solovay

According to Microsoft, there is no way to implement this reliably with managed code. This is a problem they are aware of and are working on:

For those out there who have experience debugging native C++ or VB6 code, you may have used a feature where function return values are provided for you in the Autos window. Unfortunately, this functionality does not exist for managed code. While you can work around this issue by assigning the return values to a local variable, this is not as convenient because it requires modifying your code. In managed code, it’s a lot trickier to determine what the return value of a function you’ve stepped over. We realized that we couldn’t do the right thing consistently here and so we removed the feature rather than give you incorrect results in the debugger. However, we want to bring this back for you and our CLR and Debugger teams are looking at a number potential solutions to this problem. Unfortunately this is will not be part of Visual Studio 11.

https://connect.microsoft.com/VisualStudio/feedback/details/597933/add-a-return-pseudo-variable-to-the-visual-studio-debugger-for-net-code


Per @Alex above (stackoverflow.com/a/3714884/402949), this is available for VS2013 with CLR 4.5
S
StayOnTarget

Regarding Visual Studio 2015:

According to the currently accepted answer by Marc Gravell:

This functionality has been added to Visual Studio 2013. You can see the return values in the autos windows or use $ReturnValue in the watch/immediate window

That answer also stated that this functionality does not work in Visual Studio 2015. This is not (entirely) true. On Examine return values of method calls there is the following note:

You must have the legacy expression evaluators turned on for $ReturnValue to be recognized (Tools / Options / Debugging / Use the legacy C# and VB expression evaluators). Otherwise, you can use $ReturnValue1.

I tested this in Visual Studio 2015 Enterprise:

With legacy expression evaluators turned off: only $ReturnValue1 works

With legacy expression evaluators turned on: both $ReturnValue and $ReturnValue1 work


This no longer appears to be necessary. On VS 2015 Update 3, I have legacy evaluators disabled and $ReturnValue works. However, the return value does not appear anywhere if you have the Use managed compatibility mode debugging option enabled.
P
Peter Mortensen

If you go to menu Tools → Options, IntelliTrace, and change the setting to collect events and call information.

You can go back to the previous call event (Ctrl + Shift + F11) and see the temporary value returned from the method call in the autos window as a child of the method name.

This isn't showing you the return value for the method you are in. It just shows you the return value of the last method called in the current method.

So, it's fine for

DataTable go(){return someTableAdapter.getSomeData();}

as it shows you the return value for someTableAdapter.getSomeData().

But not for:

int go(){return 100 * 99;}

P
Peter Mortensen

Old trick from the pre .NET days: Open the Registers window and look at the value of the EAX register. This contains the return value of the last function called.


+1 for old school closer to the metal approach - this will, however, not work for all return values (and it's up to the JIT'er, obviously - who knows what crazy optimization it might decide on that won't use EAX?). For integral types it will (mostly?) work. Large value types are a different matter (and as far as I recall from some blog post, those won't be displayed in VS2013 either).
L
LeopardSkinPillBoxHat

Step out of the go() method using Shift-F11, and then in the "Autos" debug window it will show the return value of the method call which just popped off the stack (in this case, the go() method which is what you want). This is the behaviour in Visual Studio 2005; I haven't used Visual Studio 2008 so I don't know if this behaves the same way in that version.


I've tried this in both VS2005 and VS2008, but I don't really see it. I have the "Autos" window open, but when in the "go" function, the autos-window is just empty. Also when stepping out of the function (the closing curly brace of the function is yellow). Can you give me one more hint?
I would expect the Autos-window to be empty while INSIDE the go() function. You need to step COMPLETELY OUT of the function (i.e. the debug cursor should be pointing to the function which has CALLED go()) and then you should see the return value for go() in the Autos window.
@LeopardSkinPillBoxHat: can't get this to work, even with your extra hint. Are you trying this in Visual Basic? It appears to have better support for observing and changing return values...
@romkyns - What shows up in the "Autos" window for you? Doesn't it show a line indicating what the last called function returned?
@LeopardSkinPillBoxHat: no, it doesn't do that in C#. P.S. Wow, took me a while to see this again.
P
Peter Mortensen

Yes, there is a very nice way. One significant drawback is that you'd have to wait for 5, maybe 6 years. Since I see that you posted in November 2008, I suggest that you waaaaaa...

...aaaait. And voilà! Just for you, MS has released the latest Visual Studio 2013 where it's a default feature accessible from the menus while running in debug mode (menu Debug → Windows → Autos).


@Doug Because the question was asked November 2008 and my reply came September 2014. The original poster is probably satisfied and doesn't want to move the credit. But I do agree with you - I wouldn't mind a few more upsies on my answer. I like upsies and rep gain. :)
Had this issue today. Thanks for replying in 2014 even though the initial issue is of 2008. Your answer is what I was looking for.
@A.P. No problems. Feels a bit like a time machine to see this post. Blast from the past, hehe.
P
Peter Mortensen

There are a lot of workarounds, but none seems satisfactory.

To quote John Skeet below (comment on a now-deleted answer):

Still looks inconvenient to me - especially if you don't know which return value you're going to need before you start debugging. I really don't want to have to have a temporary variable cluttering up my code every time I ever return anything.t

In theory, the debugger could have a return-variable. After all: it's just a variable on the stack:

unsafe {
  int * sp = stackalloc int[1];
  try {
    return a+b;
  }
  finally {
    Trace.WriteLine("return is " + *(sp+3));
  }
}

So consider this a feature request for Visual Studio.


there's quite a big a difference between a variable (a well-defined local) and a value on the stack. It is a value on the stack, but it isn't a variable (=local).
@Marc: I'm not sure how the CLR works, but a lot of compilers put function arguments on the stack below the stack pointer (sp), and local variables on the stack, above the stack pointer. That is just what I'm trying to show. And OK, when the return value is a reference type, you just get some pointer value.
It's not necessarily on the stack. In fact, if you view Debug -> Registers you're apt to see it in EAX
P
Peter Mortensen

I wanted to expand upon PascalK's answer for getting this to work in Visual Studio 2015, because there is a hidden feature which is not documented in Examine return values of method calls.

If you have nested function calls, the pseudo-variables $ResultValueX are automatically created, where the X refers to the function call order. So if you have a call such as Multiply(Five(), Six()), the following pseudo-variables are created:

Five()     | $ResultValue1 = 5
Six()      | $ResultValue2 = 6
Multiply() | $ResultValue3 = 30

S
Sprintstar

Microsoft Visual C++ used to do this, but Visual Studio doesn't AFAIK.. :(


P
Peter Mortensen

The only way I know is to place a breakpoint on the return line and then call the Quick Watch window and enter the returned expression:

someTableAdapter.getSomeData();

But this only works if the call does not change the state of any object (since there will be a second call to the same method when you will resume the execution).


This also only works if your expression doesn't have lambdas.
B
Biri

You can also ask to evaluate the value in the intermediate window as well, if it does not set flags or other variables, but only returns something.


You need to include the lambda in the question, as I use the immediate window too sometimes
J
Joe Rattz

I think you can determine this by looking at the RAX register in the Registers window (Debug / Windows / Registers). After stepping out (SHIFT + F11) of the function, check the RAX register. I don't know for a fact, but once upon a moon you could check a register (pre .NET days) and see the return value there. It might even be a combination of RAX and RBX, etc.


P
Peter Mortensen

Opening the Debug → Autos window gets you close. It won't show the actual return value, but it will show what was evaluated in the return statement.


Couldn't get VS2008 autos window to show anything like that. Could you please clarify?
return x + y; What I meant was if you set a breakpoint on this line, then your Debug-Autos window will display the current values for x and y. As I said, it only get's you close. Just trying to be helpful. I don't think that deserves a downvote.
P
Peter Mortensen

Yeah, by switching to VB.NET. ;P (You did just say "Visual Studio". ;)

For as long as I can remember (from Visual Basic through all versions of VB.NET), you can simply query the function name. It "functions" like a local variable that's implicitly declared at the start of the function and its current value is also used as the return value whenever the function exits via non-return statement means (i.e. Exit Function or just falling through) and of course, when the return statement is used.

It is also set to the return statement's expression. Just like a local variable, its value can be inspected at any point of execution inside the function (including after the return statement is executed). C# doesn't have this and should.

That little VB.NET feature (plus the Exit Function statement which it enables - another feature C# doesn't have and should) is very useful in a form of defensive programming I practice where I always initialize the function name to the failure/default value as the first statement. Then, at any failure point (which normally occurs much more often than success points), I can simply call the Exit Function statement (i.e. without having to duplicate the failure / default expression or even a constant/variable name).


P
Peter Mortensen

The accepted answer doesn't work properly with Visual Studio 2015, but by placing a break point on the last line of the method and pressing F10, it will put all expressions of the return value into the locals window.


B
Behzad

In VS2019, Just go to Debug->Windows->Autos window. There, you see concat return value as shown below:

https://i.stack.imgur.com/ENccs.png


Should be the accepted answer for VS 2019 and above.
P
Peter Mortensen

You could try to select "someTableAdapter.getSomeData();", right click on it, and go for Quick Watch.


P
Peter Mortensen

Drag and drop the return expression into a watch window.

For example, in the statement

return someTableAdapter.getSomeData();

drag and drop

someTableAdapter.getSomeData()

into a watch window, and you'll see the value.

You can do this for any expression.


The problem with that: the expression is evaluated twice.
And watch expressions can't contain lambda expressions, which I use a fair bit.