5

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.

Trygve Laugstøl
  • 1,410
  • 2
  • 19
  • 28
  • 1
    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. – Eugene Sh. Apr 27 '17 at 21:13
  • 1
    What are you using for your toolchain? – bitsmack Apr 27 '17 at 22:00
  • I'm using gcc with mainly my own startup code and linker scripts. – Trygve Laugstøl Apr 28 '17 at 08:10
  • 1
    @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. – Trygve Laugstøl Apr 28 '17 at 08:14
  • @TrygveLaugstøl Yes, it is a part of a compiler. If you want to know more, take a look [here](http://www.keil.com/support/man/docs/armcc/armcc_pge1358787046598.htm) for example. In that nice picture the "C Library Code" is exactly what the compiler is providing. – Eugene Sh. Apr 28 '17 at 13:11
  • 1
    "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. – Trygve Laugstøl Apr 30 '17 at 18:15

1 Answers1

3

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
}
swineone
  • 1,472
  • 1
  • 14
  • 21
  • 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. – Trygve Laugstøl May 15 '17 at 08:33
  • 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. – swineone May 15 '17 at 10:44
  • 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. – Trygve Laugstøl May 15 '17 at 11:02