Developing RT-Thread with VS Code
This article provides a guide on how to develop the RT-Thread qemu-vexpress-a9 BSP project on the Windows platform using VS Code.
Introduction
VS Code (Visual Studio Code) is a lightweight yet powerful code editor that supports Windows, OS X, and Linux. It comes with built-in support for JavaScript, TypeScript, and Node.js, and boasts a rich ecosystem of extensions. By installing these extensions, you can add support for other languages such as C++, C#, Python, PHP, etc.
The resources prepared for this article are as follows:
Step One: Install Debugging Extension
Download and install the C/C++ debugging extension in VS Code Extensions:
After installation, make sure the extension is in the following state. If not, click to reload:
Step Two: Open VS Code Project
Enter the qemu-vexpress-a9 BSP root directory in the Env console, then type code .
(Note: there is a dot after code) to open VS Code. This command opens the current directory with VS Code.
After opening VS Code, it will automatically open the qemu-vexpress-a9 BSP folder as shown below.
Step Three: Compile RT-Thread
Click on “View -> Terminal” in VS Code to open the internal terminal of VS Code. Type scons
in the terminal to compile the project. The terminal will print out the compilation information.
After compilation, enter .\qemu.bat
command to run the project. The terminal will output RT-Thread startup logo information and QEMU will start running.
Note: Before debugging the BSP project, you need to compile the project to generate rtthread.elf file first. You can use scons --target=vsc -s
command to update C/C++ header file search path information required by VS Code. It’s not necessary to update every time, only when you have reconfigured RT-Thread using menuconfig or changed rtconfig.h header file.
Step Four: Modify qemu-dbg.bat File
Before starting debugging, you need to edit qemu-dbg.bat
file in qemu-vexpress-a9
directory. Add start before qemu-system-arm:
@echo off
if exist sd.bin goto run
qemu-img create -f raw sd.bin 64M
:run
start qemu-system-arm -M vexpress-a9 -kernel rtthread.elf -serial stdio -sd sd.bin -S -s
Step Five: Debug Project
As shown below, click on the debug menu (bug icon) in VS Code, select Windows as the debug platform, then press F5 to start QEMU debug mode. The breakpoint will stop at main function. The debug options of VS Code are shown below:
QEMU will also start running as shown below.
In VS Code, you can use GDB commands by adding -exec
at the beginning. For example, -exec info registers
command can be used to view the contents of registers:
Here are some other key commands:
View the contents of a memory address: x/
, with the parameters explained as follows:
- n is a positive integer, representing the number of memory units to be displayed, i.e., how many memory units to display from the current address, with the size of a memory unit defined by the following u.
- f represents the display format, as shown below. If the address points to a string, then the format can be s. Other formats are shown in the table below:
- u represents the number of bytes requested from the current address. If not specified, GDB defaults to 4 bytes. The u parameter can be replaced with the following characters: b represents a single byte, h represents two bytes, w represents four bytes, and g represents eight bytes. Once we specify the byte length, GDB will read and write the specified bytes from the specified memory address and take it out as a value.
- addr represents a memory address.
Note: Strictly distinguish between n and u. n represents the number of units, and u represents the size of each unit.
Example: x/3uh 0x54320
means to read content from memory address 0x54320, h means to use two bytes as a unit, 3 means to output three units, and u means to display in hexadecimal.
- View the current program stack content: x/10x $sp–> Print the first 10 elements of the stack
- View current program stack information: info frame — — list general info about the frame
- View current program stack parameters: info args — lists arguments to the function
- View current program stack local variables: info locals — list variables stored in the frame
- View current register values:info registers (does not include floating point registers) info all-registers (includes floating point registers)
- View exception handlers in current stack frame:info catch(exception handlers)
Tip: When entering commands, you can just enter the first letter of each command. For example:
info registers
can be entered asi r
.
Note: If you add extra folders in the VS Code directory, it will prevent debugging from starting. Each time you start debugging, you need to use Env tool in BSP root directory to open VS Code with code .
command for normal project debugging.