5
\$\begingroup\$

The ARM Cortex cores has a built-in feature called semihosting which is a way to interact with the attached debugger, for example to do printf() debugging without using a peripheral unit.

However, the implementation of semihosting is to execute a BKPT instruction. If the semihosting is not enabled this will result in a DebugMonitor exception. Is there a better way to check if semihosting is enabled other than catching the exception? The annoying thing is that executing the BKPT instruction will halt the execution when the debugger is attached and I always have to press continue after every flash and restart.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ It's not a "built-in" feature. It is a mechanism implemented between the debugger and the compiler. And it can use the SVC/SWI mechanism rather than (outdated) bkpt. Given that, you can see if the semihosting is enabled in the settings of your compiler. Otherwise you might want to examine the SVC (or whatever your target is using) exception handler. \$\endgroup\$
    – Eugene Sh.
    Apr 27 '17 at 21:13
  • 1
    \$\begingroup\$ What are you using for your toolchain? \$\endgroup\$
    – bitsmack
    Apr 27 '17 at 22:00
  • \$\begingroup\$ I'm using gcc with mainly my own startup code and linker scripts. \$\endgroup\$ Apr 28 '17 at 8:10
  • 1
    \$\begingroup\$ @eugene the semihosting support is not a part of the compiler, and the whole point was to know if the debugger is attached at runtime. \$\endgroup\$ Apr 28 '17 at 8:14
  • 1
    \$\begingroup\$ "Library C code" does not imply that it is a part of the compiler. In my case I've written the on-target semihosting code myself, which work fine but I would still like to know if there is anyone on the other side, at runtime, not compile time. \$\endgroup\$ Apr 30 '17 at 18:15
3
\$\begingroup\$

I've used the following code to check for a connected debugger in the past with an STM32F4xx series MCU (when the only choice was the StdPeriph library -- perhaps this has changed with HAL/LL, but the hardware register and corresponding bit is obviously the same):

if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
    // Debugger is connected
}
\$\endgroup\$
3
  • \$\begingroup\$ Hm, this only checks if the debugger is connected, not if semihosting is enabled. From what I can tell this is the best one can get. \$\endgroup\$ May 15 '17 at 8:33
  • \$\begingroup\$ I guess so. At least in my case, I used it to prevent the code from locking up when running without being connected to a debugger. You can't be sure anyone is actually reading the messages on the other end, but at least it doesn't lock up. The core runs the BKPT instruction, the debugger processes it, but I don't think there's a way to know anything was actually done with it, unless there was some sort of API to interact with the JTAG/SWD adapter. \$\endgroup\$
    – swineone
    May 15 '17 at 10:44
  • \$\begingroup\$ That is what I want to do too, so I guess it is good enough. An option is to handle the BKPT myself and detect it that way but it is probably overkill. \$\endgroup\$ May 15 '17 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.