Baremetal Backtracing on RISC-V

Overview

rvbt Baremetal Backtrace on RISC-V

中文介绍

What is it and why?

It provides a simple but useful backtrace in baremetal machine, basically it links DWARF info into the binary and print them when backtrace the program. Currently it only supports RISC-V architecture, but I did not see any barrier to make it available in other architectures like x86.

When I am doing research, there is not a very good way to debug on a baremetal machine. The only way my mentor tell me is to put log and use objdump to look for error. It is fairly inefficient, now with rvbt, you can directly know the exact file and line of exception.

DEMO

DEMO

How to use it?

Currently, it is pretty complex to use, but I will try to make it easy to use. If you want to use it in your project, you need the following steps.

1. Compiler configuration

First you will need to force the compiler to use fp the frame pointer, because rvbt reiles on it to backtrace. You also need to specify the link script. So that the compiler know to leave room for DWARF info in the final binary.

[target.riscv64imac-unknown-none-elf]
rustflags = [
  "-C", "force-frame-pointers=yes", "-C", "link-arg=-Tlink-qemu-64.ld",
  
]
[build]
target = "riscv64imac-unknown-none-elf"

2. Copy DWARF info

As linkscript does not support copy sections around and there is a 2M limit of pc relative addressing. So you need to use a script to copy DWARF info into the final binary.

for sec in 'abbrev' 'addr' 'aranges' 'info' 'line' 'line_str' 'ranges' 'rnglists' 'str' 'str_offsets'; do \
    rust-objcopy {{PAYLOAD}} --dump-section .debug_$sec=tmp_$sec; \
    riscv64-unknown-elf-objcopy {{PAYLOAD}} --update-section .rvbt_$sec=tmp_$sec; \
done
rm tmp*; 

Now the program can use extern "C" keywords to access these link symbol to get the DWARF info.

3. Runtime parsing

In src/init.rs, a bunch of symbols are defined.

extern "C" {
    /*...*/
    fn _rvbt_abbrev_start();
    fn _rvbt_abbrev_end();
    /*...*/
}

You just need to call debug_init to initialize the DWARF context and use the following code to perform backtrace.

    debug_init();
    trace(&mut |frame| {
        resolve_frame(frame, &|symbol| println!("{}", symbol));
        true
    });

How it may be used?

With rvbt, you now can print out a more human readable information when an exception/interrupt happends. You can put it in exception handler to help you debug your baremetal program. Or you can use it to debug supervisor mode kernel. As long as you can put the debugee program's DWARF info into rvbt.

Future Work

  • Meaningful backtrace
  • Cross-platform support
  • More debug functionality, memory probing, breakpoint
  • Space efficient DWARF storage
  • Easy-to-use script/package
  • crate.io publish

Limitation

It will put a bunch of information into the binary, so maybe it can only be used when you have sufficient memory. But the DWARF info may be put in other places, as long as the program can access it, this scheme can work.

You might also like...
An operating system kernel running on RISC-V arch. Developing...

An operating system kernel running on RISC-V arch How to build it Environmental requirement rustup ( =1.57.0-nightly) Qemu ( =5.0.0) Step Build loca

RCore-Tutorial-v3 - Let's write an OS which can run on RISC-V in Rust from zero!

rCore-Tutorial-v3 rCore-Tutorial version 3.5. See the Documentation in Chinese. Official QQ group number: 735045051 news 2021.11.20: Now we are updati

CKB's vm, based on open source RISC-V ISA

Nervos CKB VM About CKB VM CKB VM is a pure software implementation of the RISC-V instruction set used as scripting VM in CKB. Right now it implements

A RISC-V and unix-like operating system developed just for fun.

Orca 虎鲸 A RISC-V and unix-like operating system developed just for fun. Schedule [*] bootloader (use RustSBI-qemu) [ ] linker & stack [ ] memory alloc

A RISC-V emulator supoorting RV32I instruction set written in Rust

32-bit RISC-V Emulator in Rust This is a RISC-V emulator supoorting RV32I instruction set written in Rust, inspired by Francis Stokes's RISC-V Emulato

rusty-riscy is a performance testing and system resource monitoring tool written in Rust to benchmark RISC-V processors.

rusty-riscy rusty-riscy is a performance testing and system resource monitoring tool written in Rust to benchmark RISC-V processors. Objectives To cre

Message Signaled Interrupts for RISC-V

RISC-V MSI Test in Rust Testing the new MSIs added by the draft Advanced Interrupt Architecture (AIA) specification. Blog Posts First blog post: 30-Ju

How to run Rust user programs on Xv6-RISC-V

Rust on Xv6-RISC-V How to run user Rust program in Xv6-RISC-V Run make init to add rist-v target to rustc Run make build to build the program. It will

Parse RISC-V opcodes to provide more detailed structured data

riscv-opcodes-parser Parse RISC-V opcodes to provide more detailed structured data. License Licensed under either of Apache License, Version 2.0 (LICE

Risc-V assembly interpreter built with pure Rust
Risc-V assembly interpreter built with pure Rust

Risc-V Interpreter Interpreter for Risc-V assembly built with Rust Report bug · Request feature Table of contents Quick start Exemple Program Instruct

Following "ZK HACK III - Building On-chain Apps Off-chain Using RISC Zero"

RISC Zero Rust Starter Template Welcome to the RISC Zero Rust Starter Template! This template is intended to give you a starting point for building a

Minimal runtime / startup for RISC-V CPUs from Espressif

esp-riscv-rt Minimal runtime / startup for RISC-V CPUs from Espressif. Much of the code in this repository originated in the rust-embedded/riscv-rt re

CosmWasm + zkVM RISC-V EFI template

cosmwasm-risc0-example CosmWasm + RISC-V zkVM gm example Overview CosmWasm RISC Zero This example exists to explore the patterns of use of CosmWasm an

Simple RISC-V emulator presented at Rust Nation 2023

A tale of binary translation This repo contains the code for the RISC-V emulator that I presented at Rust Nation 2023. It is intended as a teaching ex

An experimental RISC-V recompiler

WARNING: All of this code is highly experimental and is a direct result of a two day hacking binge fueled by a truckload of tea. It's definitely not s

A RISC-V emulator written in Rust :crab:
A RISC-V emulator written in Rust :crab:

R2 A RISC-V emulator written in Rust 🦀 . Inspired cnlohr/mini-rv32ima. Capture You can run linux in your browser. Playground https://bokuweb.github.i

Risc-V hypervisor for TEE development

A micro hypervisor for RISC-V systems. Quick Start Building (using Bazel) git submodule update --init bazel build //:salus-all Running Prerequisites S

A fast and secure RISC-V based virtual machine

PolkaVM PolkaVM is a general purpose user-level RISC-V based virtual machine. This project is still unfinished and is a very heavy work-in-progress! D

Assured Confidential Execution (ACE) for RISC-V

Assured Confidential Execution (ACE) for RISC-V ACE-RISCV is an open-source project, whose goal is to deliver a confidential computing framework with

Owner
JohnWeston
JohnWeston
RCore-Tutorial-v3 - Let's write an OS which can run on RISC-V in Rust from zero!

rCore-Tutorial-v3 rCore-Tutorial version 3.5. See the Documentation in Chinese. Official QQ group number: 735045051 news 2021.11.20: Now we are updati

rCore OS 786 Jan 2, 2023
A RISC-V and unix-like operating system developed just for fun.

Orca 虎鲸 A RISC-V and unix-like operating system developed just for fun. Schedule [*] bootloader (use RustSBI-qemu) [ ] linker & stack [ ] memory alloc

Mr.Z 4 May 29, 2022
RISC Zero is a zero-knowledge verifiable general computing platform based on zk-STARKs and the RISC-V microarchitecture.

RISC Zero WARNING: This software is still experimental, we do not recommend it for production use (see Security section). RISC Zero is a zero-knowledg

RISC Zero 653 Jan 3, 2023
Example of a Baremetal program written for the Rasperry Pi 1 (BCM2845). Both the UART and the Framebuffer graphics working

This repository is aimed to provide a starting point to whoever is trying to build a BCM2835 program from scratch. This repository contains linker scr

Pietro 4 Nov 10, 2022
Portable linked-list allocator designed for baremetal systems

Palloc Portable linked-list allocator for embedded / baremetal systems. Using the crate Include this in the [dependencies] section of Cargo.toml pallo

Pietro 3 Jan 11, 2022
Trying embedded Rust on the Pinecil GD32VF103 RISC-V device.

Pinecil GD32VF103 RISC-V Rust Demos My personal collection of Rust demos running on the PINE64 Pinecil portable soldering iron, featuring a GD32VF103T

alvinhochun 39 Nov 28, 2022
Low level access to T-Head Xuantie RISC-V processors

XuanTie Low level access to T-Head XuanTie RISC-V processors Contributing We welcome contribution! Please send an issue or pull request if you are rea

Luo Jia 30 Aug 24, 2022
Rust library for emulating RISC-V rv32imac

This library can execute instructions against any memory and register file that implements the required primitives in the traits lib_rv32::traits::{Memory, RegisterFile}. This is to encourage usage with whatever frontend you desire.

Trevor McKay 14 Dec 7, 2022
Blink program on RISC L106 80Mhz 32bit CPU

esp8266-blink Blink program on RISC L106 80Mhz 32bit CPU Flashing Running rust on ESP* is sort of hard... We won't cover the installation process, ins

Spar 2 Oct 25, 2021
Cute tiny operating system for RISC-V. ฅ•ω•ฅ

MoeOS ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄ ٩(^ᴗ^)۶欢迎参观MoeOS的仓库,MoeOS是一个小巧可爱(并不)的操作系统,目前全力支持RISC-V中。 (*≧▽≦)因为还只是一个玩具操作系统,就别要求她能做太多事情啦!现在功能还不完善,会慢慢加的! 编译 呐,你想给我找个家么? 目前MoeOS

Rui Li 30 Nov 11, 2022