Guide to Developing RT-Thread with VS Code
Overview
Visual Studio Code (VS Code) is a versatile and lightweight code editor available for Windows, macOS, and Linux. It comes with built-in support for JavaScript, TypeScript, and Node.js, and offers a vast ecosystem of extensions to enhance its functionality for other languages like C++, C#, Python, and PHP.
Resources for this Guide:
Step 1: Install the C/C++ Debugging Extension
To begin, download and install the C/C++ debugging extension from the VS Code Extensions marketplace.
After installation, ensure the plugin is in the correct state. If not, click “Reload.”
Step 2: Open the VS Code Project
In the Env console, navigate to the root directory of the qemu-vexpress-a9
BSP. Then, enter the command code .
(note the space and dot after code
) to launch VS Code and open the current directory.
VS Code will automatically open the qemu-vexpress-a9
BSP folder upon launch, as shown below.
Step 3: Compile RT-Thread
Open the internal terminal in VS Code by clicking “View -> Terminal.” In the terminal, type the command scons
to compile the project. The terminal will then display the compilation details.
Once the compilation is complete, run the project by typing the command .\qemu.bat
. The terminal will display the RT-Thread startup logo, indicating that QEMU is running.
Note:
- Before debugging the BSP project, make sure to compile the project to generate the
rtthread.elf
file. - To update the search path information for C/C++ header files needed by VS Code, you can use the command
scons --target=vsc -s
. You only need to run this command after reconfiguring RT-Thread usingmenuconfig
or after modifying thertconfig.h
header file.
Step 4: Modify the qemu-dbg.bat
File
Before starting the debugging process, edit the qemu-dbg.bat
file in the qemu-vexpress-a9
directory and add the word start
before qemu-system-arm
.
Step 5: Debug the Project
In VS Code, click on the debug menu (represented by the bug icon) as shown below. Select “Windows” as the debugging platform, then press F5 to start QEMU in debugging mode. This will set a breakpoint at the main
function. The debugging options in VS Code are illustrated below:
QEMU is now running, as shown below.
You can use GDB commands in VS Code by prefixing them with -exec
. For example, the command -exec info registers
allows you to view the contents of the registers.
Here are descriptions of some other key commands:
To view the contents of a memory address, use the command x/<n/f/u> <addr>
. Each parameter is explained as follows:
- n: A positive integer specifying the number of memory cells to display. This determines how many memory cells to show starting from the current address. The size of each memory cell is defined by u.
- f: The format for displaying the data. For example, if the address points to a string, the format can be
s
. Other display formats are listed in the table below.
Here are descriptions of some other key commands:
To view the contents of a memory address, use the command x/<n/f/u> <addr>
. Each parameter is explained as follows:
- n: A positive integer specifying the number of memory cells to display. This determines how many memory cells to show starting from the current address. The size of each memory cell is defined by u.
- f: The format for displaying the data. For example, if the address points to a string, the format can be
s
. Other display formats are listed in the table below.
- u: Specifies the number of bytes requested from the current address. By default, GDB reads 4 bytes if not specified. The u parameter can be replaced with the following characters:
b
for a single byteh
for two bytesw
for four bytesg
for eight bytes GDB will read and write the specified number of bytes from the given memory address and display the value accordingly.- addr: Represents the memory address.
Note: Distinguish between n and u: n is the number of units, while u is the size of each unit.
Example: x/3uh 0x54320
means to read from memory address 0x54320
, where h
indicates double bytes per unit, 3
specifies three units, and u
denotes hexadecimal display.
Additional Commands:
- View the contents of the current stack:
x/10x $sp
— Displays the first 10 elements of the stack. - View information about the current stack:
info frame
— Lists general information about the stack frame. - View arguments to the current stack:
info args
— Lists arguments for the current function. - View local variables of the current stack:
info locals
— Lists variables stored in the current frame. - View the values of the current registers:
info registers
(excluding floating-point registers) orinfo all-registers
(including floating-point registers). - View exception handlers in the current stack frame:
info catch
— Lists exception handlers.
Tip: You can use shorthand for commands by entering only the first letter of each command. For example, info registers
can be abbreviated to i r
.
Note: If additional folders are added to the VS Code directory, debugging may not start. Always open VS Code in the BSP root directory using the code .
command from the Env tool to ensure proper debugging.