views:

79

answers:

2

How can breakpoints be enabled or disabled at runtime? I'm writing a test workbench application that can run other .net code for the purposes of debugging plugins. The workbench app itself is not debuggable (DebuggableNonUserCodeAttribute) but the user code that it calls should be. But then, under certain circumstances at runtime, the user code breakpoints should not be honored.

I can imagine various ways of achieving this, many may not be possible:

1) Attach and detach the debugger programmatically
2) Enable and disable all breakpoints programmatically (preferably localized to an assembly or class)
3) Dynamically decorate and undecorate methods with the DebuggableNonUserCodeAttribute at runtime.
4) Somehow mark an entire code branch of a thread beyond a chosen stackframe as "non-breakpointable" for the debugger. DebuggableNonUserCodeAttribute only seems to work within a method or class, but does not affect downstream code debuggability.

EDIT: To clarify: during runtime, the same piece of end user code will be run multiple times by the hosting executable, some where breakpoints should be honored and other times when they shouldn't be. It would be inappropriate to require that the user modify their code for this feature or that they be required to set conditional breakpoints.

A: 

You could use System.Diagnostics.Debugger.Break() instead of setting breakpoints. Just put the method call inside whatever if blocks would be necessary.

Jon Howard
Thanks, but this won't work for this situation. It would be inappropriate to use this command in the user code.
uosɐſ
+1  A: 

You can do both #1 and #2 through the Debugger portion of the DTE automation object model.

How to enable/disable Breakpoints via DTE

How to attach the debugger via DTE

Aaron Marten