简体   繁体   中英

How to detect Visual Studio debug execution context from within code?

I'm looking for a way to detect how code is called during a debugging session in Visual Studio. Trying to differentiate these two contexts:

  • Ordinary debugging, whether running or single stepping.
  • Called via the debugger itself, such as my ToString being called from the watch window.

(I'm doing this because I have some non-release thread validation code I want to disable in the second case.)

Anyone have a way to do this, or ideas about things to pursue? I'm fine with hacky, tricksy methods, even if they are specific to a particular version of VS.

Things that don't work:

  • Grabbing a StackTrace and looking for something magic that means the debugger is calling in, rather than regular code. VS uses the current thread in its current state to call out of for the Watch window, so all the StackTrace is going to see is the current stack at the debugger breakpoint + the getter/ToString that the Watch window is calling, right on top of it.

This situation typically arises when methods that Visual Studio assumes are pure (ie evaluation has no impact on the state of the program) either have side effects or have preconditions that the debugger violates.

These situations are best solved using the following tools:

  • DebuggerBrowsableAttribute : Apply this attribute to properties:

     [DebuggerBrowsable(DebuggerBrowsableState.Never)] 
    • Apply to all properties whose get method is not pure, ie the getter alters the state of the program, either through setting a cached value or otherwise
    • Apply to all properties whose get method has preconditions the debugger could violate, such as threading restrictions, cross-thread invocations (don't forget implicit if you use COM interop)
  • DebuggerDisplayAttribute : Apply this attribute to all types whose ToString() implementation performs non-trivial operations (avoid debugger performance impact) or has preconditions the debugger might not meet (eg threading requirements). This attribute is used instead of calling ToString() for these types.

  • DebuggerTypeProxyAttribute : Apply this attribute to types that require special consideration for when, what, and how information is presented in the debugger. The .NET framework itself uses this attribute extensively to provide features like clear output for the List<T> and HashMap<T> types, and even to improve information available for types like Lazy<T> that are sometimes OK to evaluate and other times could affect the state of the program.

As far as ordinary debugging goes, I think this is what you want:

System.Diagnostics.Debugger.IsAttached

Here's the documentation .

This won't help with the watch window though. On this, maybe the StackTrace class can help but I haven't tried it myself. It's in the same namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM