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