What is Semihosting?

by LPCWARE : What is Semihosting?

Background to Semihosting

When creating a new embedded application, it can sometimes be useful during the early stages of development to be able to output debug status messages to indicate what is happening as your application executes.

Traditionally, this might be done by piping the messages over, say, a serial cable connected to a terminal program running on your PC. LPCXpresso also offers an alternative to this, called semihosting. Semihosting provides a mechanism for code running on the target board to use the facilities of the PC running the IDE. The most common example of this is for the strings passed to a printf being displayed in the IDE’s console view.

The term “semihosting” was originally termed by ARM in the early 1990s, and basically indicates that part of the functionality is carried out by the host (the PC with the debug tools running on it), and partly by the target (your board). The original intention was to provide i/o in a target environment where no real peripheral-based i/o was available at all.


Semihosting implementation

The way it is actually implemented by the tools depends upon which target CPU you are running on. With Cortex-M based MCUs, the bottom level of the C library contains a special BKPT instruction. The execution of this is trapped by the debug tools which determine what operation is being requested - in the case of a printf, for example, this will effectively be a “write character to stdout”. The debug tools will then read the character from the memory of the target board - and display it in the console window within the IDE.

Semihosting also provides support for a number of other I/O operations. For example it provides the ability for scanf to read its input from the IDE console. It also allows file operations, such that fopen can open a file on your PC’s hard drive, and fscanf can then be used to read from that file.

On ARM7/9 based targets the same principles of working are used, but implemented using the SWI / SVC instruction rather than the BKPT.


Semihosting Performance

It is fair to say that the semihosting mechanism does not provide a high performance i/o system. Each time a semihosting operation takes place, the processor is basically stopped whilst the data transfer takes place. The time this takes depends somewhat on the target CPU, the debug probe/link (thus Red Probe+ provides a much enhanced semihosting speed over LPC-Link), the PC hardware and the PC operating system. But it takes a definite period of time, which may make your code appear to run more slowly.


Important notes about using semihosting

When you have linked with the semihosting library, your application will no longer work standalone - it will only work when connected to the debugger.

Semihosting operations cause the CPU to drop into “debug state”, which means that for the duration of the data transfer between the target and the host PC no code (including interrupts) will get executed on the target. Thus if you application uses interrupts, then it is normally advisable to avoid the use of semihosting whilst interrupts are active. If you still need to use printf, then you can retarget the bottom level of the C library to use an alternative communication channel, such as a UART.

1. 当我们用半主机的库时,就和对应的调试器绑定了。就这一点来说,还不如重定向,至少只要串口久可以了,但是速度有待商榷。咦,不是说在出现性能问题之前,性能就不是问题么。
2. 不要在有中断的情况下使用Semihosting,最好把它关掉,不然就可能把你的中断给抢断了,不要怪我没提醒过你。
3. 如果用仅仅是用串口,用重定向就好了,其实就是串口+重定向的作用与调试器+Semihosting的作用是一样的,只是前者显示在串口窗口,后者显示在IDE的串口而已。不过成本显然前者更有优势,后者只不过是买调试器顺便送个串口。

Semihosting Specification

The semihosting mechanism used within LPCXpresso based on the specification contained in the following document available from ARM’s website…

=> ARM Developer Suite (ADS) v1.2 Debug Target Guide, Chapter 5. Semihosting

从这段文字中获得信息是,同学们啊,Semihosting 这个概念很老了,是ADS时代的产物了,速度也不行了,跟不上时代的发展了。但是,后来的那些各种碉堡了的调试设计思路都和我差不多,大家了解了解我,然后再去用其他的,如ITM, RTT,JLink-Scope就好了。

For more information…