I know the title is a bit ambitious, but I am wondering if there is a way, after displaying a previous state in the call stack window (Visual Studio 2010) when debugging a C# program, to restart (like when hitting "Continue"/F5) from there.

This would be particularly useful to debug a lambda expression that generates an exception, as there is no way to move outside.

For a real OO code time machine, the historical state of all objects would need to be stored in memory, so I doubt it is feasible at this stage. Now if the state of all objects has not changed much, then we could keep the current state and jump back in time (as a shortcut to doing the same thing with "edit and continue").

有帮助吗?

解决方案

Some times, you can right click on a stack frame and choose "Unwind to here" (or very similar wording). It's not always possible, and I'm not sure what the necessary conditions are, but I'm going to make a guess as to what might prevent it:

  • A native code frame on the call stack in the middle
  • Being halted at a StackOverflowException (obviously, death to a process in any case)
  • Maybe lambda expressions or other things that prevent Edit-and-Continue from working (?)

Basically, anything "unusual".

Other than that, if it works, then there you go!

其他提示

This is a bit kludgy but:

While viewing the previous state in the call stack window, open the disassembly window (Debug | Windows | Disassembly or CTRL+ALT+D). Now you should be able to create a breakpoint which will stop execution when you get back to that location.

If you don't care what else executes, press F5 and allow the code to run back to your new breakpoint.

Now right click on the statement you want to restart from and select Set Next Statement. Press F5 to restart from there.

If you do care about what else executes on your way back to the new breakpoint you could use Set Next Statement to set the program counter to the end of the function you are in and use Shift+F11 to step out of that function (thus not executing any of the remaining logic in that function). Repeat as needed until you get back to your new breakpoint.

Note the various dire warnings about using Set Next Statement

Edit 6/18 When I tested the above I was running Visual Studio 2010 Ultimate. I just checked Visual Studio Express C# and it does not support the Disassembly Debug Window. When I can I'll check Visual Studio 2010 Professional and update this answer again. If you have Visual Studio Ultimate then the Intellitrace suggestion by @Hans is maybe a better bet.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top