Move debugger up the stack in Visual Studio - c#

Visual studio (I'm using Community 2015) allows you to drag and drop the yellow execution pointer around in the same function.
Is there a way to move that pointer up the stack, and especially when the debugger is waiting on the user after an exception has been thrown?
EDIT: Ideally no user code should execute during that process, as it can mess things up with other variables

I found a way. This is maybe not the easiest way around, but it's always possible to
first drag and drop the yellow arrow to the end of the current function
the execute as many step-out (Shift-F11) and "drag-to-end-of-function" as required
Of course it will "execute" some code, but if carefully done, it's only stack managing code, not user code, thus should not interfere with user variables.

Use the 'Step Out' functionality.
Shift + F11
is the shortcut by default.
Just a caveat: stepping out will execute the rest of the code in the method before going back up the stack.

Related

Dragging the instruction pointer backward while debugging in Visual Studio

For a demonstrating, take a look at the following section of this video.
Basically, I know this is possible in Visual Studio Community Edition 2015. I was wondering:
a) Is this related to Intellitrace and "Historic debugging"?
b) Will there be any side-effect when I do this? Or is this just moving the instructions backwards and that's it?
It is just moving the instruction pointer backwards and that's it, to use your own words.
This means that:
Any side-effects already incurred between where you drag it to and where you dragged it from has already incurred and won't be reversed.
Any variables changed in the same section of instructions will still be changed, they are not reversed to whatever values they had at the point you you drag the instruction pointer to
So you can use this debugging aid to either force the program to take a path it didn't (for instance by dragging the instruction pointer inside an if-statement it skipped), to skip (by dragging the instruction pointer past some code you don't want to execute), or to rerun some code.
But you must be aware of the above limitations. If the code is not safe to be executed again, then doing so will likely not help you debug.
It's been doable for a long while now. Everything that happened to the point from where you are dragging the cursor already happened, so you will essentially be re-doing that bit.
There are no REAL consequences, unless you are processing something or saving to database etc., as writing the same existing data might throw an exception or mess with processing of some data.
All of the variables that have been set (even if you drag the cursor higher than they initialize) will retain the value they had from the spot you've dragged the cursor upward.

Prevent .net ide from jumping to current executing line when breaking

When breaking in .net, it always jumps to the currently executing line. I would rather just use shortcuts to bring up the stack and go there manually if that's what I want to do.
I could have sworn I saw an option to prevent it from doing this once upon a time but I can't seem to find it anywhere now. Anyone know how to do this?
C# Keyboard layout, when I hit a breakpoint (or halt with Ctrl+Alt+PauseBreak), I usually press Ctrl+Alt+C to bring up the CallStack window, then up/down keys and press enter to navigate the call stack.
With VS2010 I've seen some weird results (options) after installing various plugins and 3rd party components at different work places...

Call stack time machine in Visual Studio 2010

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.

Step through a program backwards after an Exception has occurred - Visual Studio

Is there a way to step back through a program from the point where an error/Exception has occurred? Or look at the sequence in which the methods were called before the error occurred?
In the Ultimate versions of Visual Studio 2010 you can use Inellitrace to go back in the execution, as Dan Puzey mentioned.
However, in any version of Visual Studio you can still get a lot of information just from the call stack. It doesn't only show the calls that lead up to the current position, by double clicking on points in the Call Stack window you can jump to the point in the code where each method was called. The state of the stack is preserved, so you can even still see the values of local variables in the calling method.
If you have VS2010 Ultimate, the "Intellitrace" functionality allows you to do exactly that. It basically logs a huge amount of execution data (up to and including every method call & parameter made) and allows you to step back in time to examine variable values at those points.
You will see the trace in the Stack Trace (Call Stack Window in Visual Studio).
If you are using debugger you can drag and drop the yellow arrow to the previous lines of code to repeat the scenario. However, it does not always work.
Right click on the line of code to which you want to get back and select "Set next statement"
Another option is to drag the yellow arrow to the desired line.

Debugging all events in Visual Studio 2010 without setting break points

I am trying to debug a windows form application which has a large number of events: button presses, timers, etc..
Is there a way to catch every line of code being executed by the application without setting a break point?
edit:
The program was not written by me, so I am unfamiliar with the code. I wish to step through the entire program, catching every line of code being executed. Setting break points in every event is impractical as various controls are created dynamically.
For debugging a button click without setting breakpoints:
Start the app with the debugger.
Get to the state immediately before the intended click.
Go back to the debugger and press Pause then F11 (Step Into) -- nothing will happen.
Go to the app and press the button -- the debugger should take over and drop you into the event handler.
Note: This will not work if Paint, any Mouse event, or probably some other events are handled. The debugger will drop you into those handlers any time you attempt the steps above.
If you're using the Ultimate edition of your Visual Studio 2010 you can use its new feature called IntelliTrace (previously Historical Debugger). This will allow you to do exactly what you want - be able to see all method calls and events that happened during execution of your program, and you'll be able to jump back to the event which you need.
To enable IntelliTrace, go to Tools → Options → IntelliTrace, and check the "Enable IntelliTrace" checkbox, and select one of two modes: "events only" or "events and call information", then run your application with a debugger (F5).
The difference between the two modes is that the latter uses the profiler to collect all runtime information, so you get a complete call stack, however you won't be able to use edit-and-continue features of the debugger.
You can find more in this series of articles, and of course, on MSDN.
You could also try a code coverage tool.
For example, if you have Resharper and dotCover, you can run your application (via the dotCover->Cover Application... menu item) and when the application finishes, dotCover will show you which lines of code were run in the VS IDE by highlighting them in green. Lines of code which where not run are coloured in red.
I don't know if there are other tools which do this, but it's an option.
I developed the Runtime Flow tool to solve exactly this problem - to understand a large unfamiliar .NET codebase via realtime function calls monitoring. It's similar to IntelliTrace, but places more emphasis on control flow than on debugging.
Why would you want to break on every line? This would be very onerous and time consuming. If you want to see the activity of your program as it executes, use a logging mechanism or Debug.Writeline to output information to the Immediate window.
You cannot trace lines of code, but you can use Trace.TraceInformation calls where you want to have an idea of what's executed. There's also Debug.Write. Both output will write in the output window of Visual Studio.
Another solution would be to add logging to your application, for example with log4net, but that may be overkill for your needs.
This isn't exactly what you're asking for, but in case you didn't know you can toggle an existing breakpoint on or off. In your case, you could add break points at key places throughout your code and just disable them when you don't want to debug them. That way, you'll be able to re-enable them later when you want to use them again.
Enabling/disabling is available through the Breakpoints window found under the Debug > Windows > Breakpoints menu (CTRL+D, B). You can also include "Function" and "File" columns in the window, which might help you identify which breakpoints are in the event handlers that you care about
Not really, but you can set one breakpoint and single-step (F10/F11) through the rest of the code.
Nope 'fraid not - you need to set each breakpoint yourself.
If it helps F9 is the shortcut key for assigning a breakpoint - just set a breakpoint on the start of each method and use step through (F10) / step into (F11) from there.

Categories

Resources