Using Semihosting for Debug Output with VisualGDB

This tutorial shows how to use semihosting to send printf()-style messages from the debugged program to VisualGDB. Semihosting is a mechanism of passing data from the debugged program to the debugger via a syscall-style interface. It is enabled by default in our ARM toolchain. In this tutorial we will create a basic project that sends some debug via semihosting and explain how it works.

You will need VisualGDB 4.3 or later.

  1. Begin with creating a new project with VisualGDB Embedded Project Wizard:01-project
  2. Select “Create a new project -> Embedded binary”:02-binary
  3. Select your device. In this example we will use the STM32F4Discovery board. Note that the semihosting interface does not require any special hardware, so it will work with many other devices as well:03-stm32dev
  4. Select the default sample. You can specify the LED settings that match your board, or proceed with the default ones, as LEDs are not vital for this example:04-sample
  5. Specify the debug interface you are using. In this example we will use OpenOCD with ST-Link:05-debug
  6. Include the <stdio.h> file and replace the for() loop inside the main() function with the following code:
    for (int i = 0;;i++)
    {
        GPIO_WriteBit(GPIOD, GPIO_Pin_12, Bit_SET);
        printf("LED ON (%d)\n", i);
        GPIO_WriteBit(GPIOD, GPIO_Pin_12, Bit_RESET);
        printf("LED OFF (%d)\n", i);
    }

    Ensure that you are using our latest ARM toolchain. Build your project by pressing Ctrl-Shift-B:06-build

  7. Start debugging by pressing F5. The default implementation of printf() will initiate a semihosting call and VisualGDB will detect it and ask if you want to enable the console:07-semihosting
  8. Select “yes”. VisualGDB will show a console window with the messages from your program:08-console
  9. Press Shift-F5 to stop debugging. Then start it again and select “no” in the semihosting prompt. VisualGDB will stop at the semihosting syscall as if it was a breakpoint. You can use the call stack to see the standard library functions involved in it:09-bkpt
  10. You can configure the semihosting console to be shown without a prompt via VisualGDB Project Properties:10-consoleYou can also route the output of printf() to another interface (e.g UART) by redefining low-level IO functions. See the UART tutorial for details.