RVBacktrace RISC-V Minimal Stack Backtrace Component

RT-Thread IoT OS
4 min readSep 19, 2024

--

Component Overview
A minimal RISC-V stack backtrace component.

Component Source Code
Source code: RVBacktrace

Component Features

  • Call the only API provided by the component at the required point to start stack backtrace in the current environment.
  • Supports outputting commands needed for addr2line to perform stack backtrace using addr2line.
  • Supports combining disassembly with graphical visualization of stack backtrace information.

TODO List

  • Support printing stack backtrace information for a specified thread.
  • Support integration with RT_ASSERT.
  • Support outputting more symbol information.
  • Support file jumping.

How to Add the Component
This component is available as an RT-Thread package and can be found in the package marketplace. You can integrate it using RT-Thread Env or RT-Thread Studio to use it. If you encounter any issues or have feedback during the usage, feel free to submit an issue or discuss it in the community group.

Example of Usage
This component is very straightforward, offering just one API: void rvbacktrace(void). Simply call this API wherever needed to output the current call stack information. Here's an example (demonstrated with the HPM6750):

  1. Call rvbacktrace at the appropriate point in your code.

2. Run the code, and the call stack information will be displayed in the terminal.

3. Visualizing Stack Backtrace Information
The current shell output doesn’t include symbolic information, making it harder to interpret. This component offers two solutions:

  • Utilize the addr2line tool.
  • Combine call stack output with information from the disassembly file to display symbols.
    Improved methods are also in progress. Up next, we’ll explain how to use these two approaches in detail.

3.1 The addr2line Tool
You can see that the shell output includes the information required by addr2line. Copy this information, then use the tool in the current rtthread.elf directory. In my environment, I have the tool available in WSL, so I open it there. After launching the tool, paste the copied information into the terminal and run it. The output will be as follows:

You can see that the output is now much more detailed, which is quite helpful during the debugging process.

3.2 Visualizing with Disassembly
Configure RT-Studio to output disassembly commands for a more visual representation.

Create a text file in the same directory as the disassembly file, and copy the shell output into this text file.

The contents of rvbacktrace.txt are as follows:

Next, click on RVBacktrace.py in the project directory.

Run Interface:

Here, enter the paths for rvbacktrace.txt and rtthread.asm. To keep things simple, both files are placed in the same directory.
Copy their paths and enter them into the terminal as shown:

Execution Result:

An HTML file will be automatically generated, displaying the stack backtrace information in a table format. This file will open automatically once it’s created.

The above steps cover the first run. For future stack backtrace outputs, simply copy the new shell output into the previously created text file and save it. The chart will update automatically. So, after completing the initial setup, you only need to paste new shell output into the text file to refresh the information.

If you need to change the paths of the text file or the disassembly file, run clean.py from the source directory to clear the intermediate files, then follow the same steps as before.

--

--

RT-Thread IoT OS

An Open-Source Community-Powered Real-Time Operating System (RTOS) Project! Let’s develop, DIY, create, share, and explore this new IoT World together!