How to detect if a debugger is attached on an STM32

Hi reader,

This is just a quick how-to for the google robots to find, nothing Mooshimeter related.  I didn’t see any good answers for this out there, but it’s very useful if you’re debugging firmware on an STM32 to know if a debugger is attached.  If an error condition arises while a debugger is attached, you can raise a breakpoint and halt to allow examining the system state at the time of error, and if no debugger is present, you can indicate the error in some other way and carry on.

My setup:

  • Using SW4STM32 IDE (frontend for ARM gcc/g++ compiler)
  • STM32F4xxx (though this should work on all STM32’s)

The debugger can be detected by examining the DBGMCU_CR, the control register for the debug peripheral within the MCU.  The bottom 3 bits default to 0 after a power-on reset, but the debugging session will set some or all of them high when it attaches (except in very specific circumstances where you’re changing the debugger settings).  So we can just examine these bits to infer if a debugger is attached:

Generally I use this in whatever assert_failed handler I set up.  If you’re using the STM32 HAL with USE_FULL_ASSERT on, you can override the assert_failed(uint8_t*, uint32_t) function so any assertion failures in the HAL layer also get caught. In the example below, debug_print is rigged to work like printf and DMAs the data out UART3, you’ll probably want to do something else with the information. Also I do a little string processing to grab just the filename instead of the full path to the file supplied by the preprocessor.

As a test, here is a code block being run within a debug session and after a power-on-reset (no debug session, debugger still physically attached):

Running this code in a debug session halts on the ASSERT(0) as you would expect.  Power cycling the board yields:

Before the code continues on its way. Which is what we expected. Hope this was useful!

One Response to “How to detect if a debugger is attached on an STM32”

  1. notsure April 28, 2021 at 5:18 am #

    Extremely useful. STM32 ecosystem is huge and even the simplest task can take days or weeks to understand, not due to difficulty but the amount of information in references manuals and not very well sorted really.
    Your explanation is simple and direct. Wonderful.

Leave a Reply

This site is protected by reCaptcha and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.