Possible to detect connected debugger

Is it possible to detect if the debugger is connected in code?

This is useful because then you can choose to only enable the watchdog when the debugger is not attached.

Could also be useful to trigger watchdog activation on, for example, whether the device is plugged in over USB.

That is a good idea. Do you want to add an issue to GitHub?

1 Like

Done

1 Like

Debugger.IsAttached does not work ?

Debug class doesnt appear to have an IsAttached property, what am i missing?

  public static class Debug
  {
    public static TraceListenerCollection Listeners => Trace.Listeners;

    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern void EnableGCMessages(bool enable);

    [Conditional("DEBUG")]
    public static void WriteLine(string message)
    {
      foreach (TraceListener listener in Debug.Listeners)
        listener.WriteLine(message);
    }

    [Conditional("DEBUG")]
    public static void WriteLineIf(bool condition, string message)
    {
      if (!condition)
        return;
      Debug.WriteLine(message);
    }

    [Conditional("DEBUG")]
    public static void Assert(bool condition) => Debug.Assert(condition, string.Empty, string.Empty);

    [Conditional("DEBUG")]
    public static void Assert(bool condition, string message) => Debug.Assert(condition, message, string.Empty);

    [Conditional("DEBUG")]
    public static void Assert(bool condition, string message, string detailedMessage)
    {
      if (condition)
        return;
      Debug.WriteLineIf(message != string.Empty, message);
      Debug.WriteLineIf(detailedMessage != string.Empty, detailedMessage);
      Debugger.Break();
    }
  }

If you need something right away, you might try a simple # if def DEBUG vs RELEASE build. Using DEBUG when you intend to debug, and release otherwise.
Though i know switching between the two can be a small hassle.

you might try a simple # if def DEBUG vs RELEASE build. Using DEBUG when you intend to debug, >and release otherwise.

That’s what I’ve been doing. It’s kind of a pain, but it’s a small annoyance to deal with rather than have stuff freezing on my customers.

It’s Debugger.IsAttached not Debug.IsAttached.

1 Like