How do I discover the return value at the end of a function when debugging in VS2008?

心已入冬 提交于 2019-12-12 09:44:56

问题


Using C# in Visual Studio 2008 and stepping through a function in the debugger I get to the end of a function and am on the final curly brace } and about to return. Is there a way to find out what value the function is about to return?

This is necessary if the return value is calculated such as:

return (x.Func() > y.Func());

回答1:


It's a little low level, but if you switch to disassembly then you can single step through the instructions and see what the return value is being set to. It is typically set in the @eax register.

You can place a breakpoint on the ret instructions and inspect the register at that point if you don't want to single step through it.




回答2:


I made a commercial extension for Visual Studio called BugAid (currently in beta) that does exactly what you asked.

In your example, it will show you this:

The way it does it is by instrumenting your code as you debug it, allowing us to retrieve the return values of both calls to "Func", and thus conclude whether the expression x.Func() > y.Func() is true or not.

For more information, please see my blog post on the subject.




回答3:


You can put

(x.Func() > y.Func())

in a watch window to evaluate it, and see the result. Unless the statement is

return ValueChangesAfterEveryCall();

you should be fine.




回答4:


I am still using VS 2003 with C++, so this may or may not apply. If you use the "auto" tab (near the "locals" and "watch" tabs), it will tell you the return value of a function once you return.




回答5:


I'd actually recommend refactoring the code to put the individual function returns in local variables. That way, yourself and others don't have to jump through hoops when debugging the code to figure out a particular evaluation. Generally, this produces code that is easier to debug and, consequently, easier for others to understand and maintain.

int sumOfSomething = x.Func();
int pendingSomethings = y.Func();
return (sumOfSomething > pendingSomethings);


来源:https://stackoverflow.com/questions/164996/how-do-i-discover-the-return-value-at-the-end-of-a-function-when-debugging-in-vs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!