Regardless of other options that may achieve the same result (i.e. adding breakpoints by hand), is it possible to programmatically add a breakpoint into the source code of a Visual Studio project?
Such as:
try
{
FunctionThatThrowsErrors(obj InscrutableParameters);
}
catch(Exception ex)
{
Log.LogTheError(ex);
AddBreakPointToCallingFunction();
}
That way when you run in debug the next time, it will automatically have set breakpoints at all the points that caused trouble during the last run.
I'm not saying that's a particularly useful way of debugging. I'm just wondering if the capability is there.
On the Debug menu, click Disable All Breakpoints. On the toolbar of the Breakpoints window, click the Disable All Breakpoints button.
Set breakpoints in source code To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint.
To enable or disable Just My Code in Visual Studio, under Tools > Options (or Debug > Options) > Debugging > General, select or deselect Enable Just My Code.
To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.
You can call System.Diagnostics.Debugger.Break()
.
You can also tell Visual Studio to break on all exceptions, even handled ones, by going on the menu to Debug->Exceptions...
and checking Thrown
everywhere that's currently only checked "User-unhandled".
You inspired me to poke around with this - thanks for keeping me awake all night. :) Here's one way you can do it.
Visual Studio has really great breakpoint support. One of the cooler features is that you can tell it to run a Visual Studio macro when the breakpoint is hit. These macros have full access to the development environment, i.e. they can do whatever you could do manually at the keyboard, including setting other breakpoints.
This solution is to 1) put a top-level try/catch in your program to catch all exceptions, 2) put a breakpoint in the catch block that runs your macro, and 3) have the macro look at the exception to figure out where it came from, and put a breakpoint there. When you run it in the debugger and an exception occurs, you'll have a new breakpoint at the offending line of code.
Take this sample program:
using System;
namespace ExceptionCallstack
{
class Program
{
static void Main(string[] args)
{
try
{
func1();
}
catch (Exception e)
{
Console.WriteLine("Oops");
Console.ReadKey();
}
}
static void func1()
{
func2();
}
static void func2()
{
func3();
}
static void func3()
{
throw new Exception("Boom!");
}
}
}
The objective is to programmatically set a breakpoint on that throw
in func3 when you run it in the debugger and get the error. To do this, first create a new Visual Studio macro (I called mine SetBreakpointOnException). Paste this into a new module MyDebuggerMacros or whatever:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Module DebuggerMacros
Sub SetBreakpointOnException()
Dim output As String = ""
Dim stackTrace As String = DTE.Debugger.GetExpression("e.StackTrace").Value
stackTrace = stackTrace.Trim(New Char() {""""c})
Dim stackFrames As String() = Regex.Split(stackTrace, "\\r\\n")
Dim r As New Regex("^\s+at .* in (?<file>.+):line (?<line>\d+)$", RegexOptions.Multiline)
Dim match As Match = r.Match(stackFrames(0))
Dim file As String = match.Groups("file").Value
Dim line As Integer = Integer.Parse(match.Groups("line").Value)
DTE.Debugger.Breakpoints.Add("", file, line)
End Sub
End Module
Once this macro is in place, go back to the catch
block and set a breakpoint with F9. Then right-click the red breakpoint circle and select "When Hit...". At the bottom of the resulting dialog there's an option to tell it to run a macro - drop down the list and pick your macro. Now you should get new breakpoints when your app throws unhandled exceptions.
Notes and caveats about this:
Hope this helps!
It's not really responsive to your question, but you can cause the debugger to break based on conditions you set using Debug.Assert. So, instead of saying "next time I run a function that caused an exception, break" you can add assertions to your function to break when conditions are not what they should be. After all, there is no guarantee that a function will throw an exception this time just because it threw an exception last time. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With