title: ARM Cortex M detect if debugger is in Run or Debug mode
body: <p>There is a proven way to detect whether a debugger is connected on Cortex-M, as seen <a href="https://electronics.stackexchange.com/a/302740/131847">here</a>.</p>
  
  <p>I used to as a way to automatically set a breakpoint when I am in a debug session:</p>
  
  <pre><code>void autobreak() {
    if (CoreDebug-&gt;DHCSR &amp; CoreDebug_DHCSR_C_DEBUGEN_Msk)  { 
        __asm__ __volatile__ ("bkpt #0"); 
    } 
  }
  </code></pre>
  
  <p>I am using J-Link and OpenOCD in CLion for debugging. With <code>autobreak()</code>, the J-Link halts program execution without me manually specifying breakpoints in <strong>debug mode</strong>. However, it seems that in <strong>run mode</strong> J-Link still halts program execution which is undesirable for my application.</p>
  
  <p>Let me quickly clarify what I mean by each mode:</p>
  
  <ul>
  <li><strong>Run Mode</strong>: Only flash program</li>
  <li><strong>Debug Mode</strong>: Flash program then allow user to pause program execution and manually set breakpoints</li>
  </ul>
  
  <p>Is there some way to check whether the J-Link is connected in <strong>debug mode</strong> or <strong>run mode</strong>?</p>