Skip to content

Commit

Permalink
support rust panic backtrace
Browse files Browse the repository at this point in the history
mini-backtrace has llvm's unwind cpp source to support backtrace/unwind.
as unwind/backtrace needs dynamically allocates memory, mini-backtrace
uses stack memory to capture fixed number of backtrace to avoid heap
allocation.
as unwind library needed, it needs to turn on eh_frame_hdr
  • Loading branch information
Yao Zhao committed Oct 29, 2023
1 parent fbe6bec commit fef831d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
3 changes: 3 additions & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ acpi = { git = "https://git.mirrors.dragonos.org/DragonOS-Community/acpi-rs.git"
intertrait = { path = "src/libs/intertrait" }
linkme = "0.2"
ida = { path = "src/libs/ida" }
mini-backtrace = { path = "/home/yaozhao/github/mini-backtrace", version = "0.1.4" }

# 构建时依赖项
[build-dependencies]
Expand All @@ -56,3 +57,5 @@ debug = true # Controls whether the compiler passes `-g`
[profile.release]
debug = false

[features]
unwind = []
26 changes: 20 additions & 6 deletions kernel/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))

# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
PIC := _INTR_APIC_
CFLAGS = $(GLOBAL_CFLAGS) -fno-pie -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include

# unwind/backtrace related
UNWIND_ENABLE ?= yes
CFLAGS_UNWIND =
LDFLAGS_UNWIND =
RUSTFLAGS_UNWIND =
ifeq ($(UNWIND_ENABLE), yes)
CFLAGS_UNWIND = -funwind-tables
LDFLAGS_UNWIND = --eh-frame-hdr
RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
endif

CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include

export ASFLAGS := --64

Expand All @@ -32,12 +44,12 @@ main.o: main.c

kernel_rust:
rustup default nightly
cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json

RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json
all: kernel

@echo "Linking kernel..."
$(LD) -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
# 生成kallsyms
current_dir=$(pwd)

Expand All @@ -51,10 +63,12 @@ all: kernel
# 重新链接
@echo "Re-Linking kernel..."
@echo $(shell find . -name "*.o")
$(LD) -b elf64-x86-64 -z muldefs -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds --no-relax
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel head.o main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds --no-relax
@echo "Generating kernel ELF file..."
# 生成内核文件
$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 kernel ../../bin/kernel/kernel.elf
ifneq ($(UNWIND_ENABLE), yes)
$(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
endif
# $(OBJCOPY) -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../../bin/kernel/kernel.elf
@echo "Kernel Build Done."

Expand All @@ -76,4 +90,4 @@ clean:
echo "Clean in dir: $$subdir";\
cd $$subdir && $(MAKE) clean;\
cd .. ;\
done
done
2 changes: 1 addition & 1 deletion kernel/src/debug/traceback/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <common/printk.h>
#include <process/process.h>

static int lookup_kallsyms(uint64_t addr, int level)
int lookup_kallsyms(uint64_t addr, int level)
{
const char *str = (const char *)&kallsyms_names;

Expand Down
20 changes: 20 additions & 0 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ extern crate x86;
use crate::mm::allocator::kernel_allocator::KernelAllocator;

use crate::process::ProcessManager;
#[cfg(feature = "unwind")]
extern crate mini_backtrace;
use mini_backtrace::Backtrace;
extern "C" {
fn lookup_kallsyms(addr: u64, level: i32) -> i32;
}

// 声明全局的分配器
#[cfg_attr(not(test), global_allocator)]
Expand Down Expand Up @@ -107,6 +113,20 @@ pub fn panic(info: &PanicInfo) -> ! {
}
}


#[cfg(feature = "unwind")]
{
unsafe {
let bt = Backtrace::<16>::capture();
println!("Rust Panic Backtrace:");
let mut level = 0;
for frame in bt.frames {
lookup_kallsyms(frame as u64, level);
level += 1;
}
};
}

println!("Current PCB:\n\t{:?}", *(ProcessManager::current_pcb()));
ProcessManager::exit(usize::MAX);
}
14 changes: 13 additions & 1 deletion kernel/src/link.lds
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ SECTIONS
_ebss = .;
}

eh_frame = .;
.eh_frame (eh_frame): AT(eh_frame - KERNEL_VMA)
{
__eh_frame_hdr_start = .;
*(.eh_frame_hdr)
__eh_frame_hdr_end = .;
__eh_frame_start = .;
*(.eh_frame)
*(.rela.eh_frame)
__eh_frame_end = .;
}

_end = .;

/DISCARD/ : {
*(.eh_frame)
/* *(.eh_frame) */

}
}
19 changes: 17 additions & 2 deletions tools/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ install_ubuntu_debian_pkg()

}

install_archlinux_pkg()
{
pkgman="pacman"
echo "检测到 ArchLinux"
echo "正在更新包管理器的列表..."
sudo "${pkgman}" -Sy
echo "正在安装所需的包..."
sudo "${pkgman}" -S --needed --noconfirm \
curl wget bridge-utils dnsmasq \
diffutils pkgconf which unzip util-linux dosfstools \
gcc make flex texinfo gmp mpfr qemu-base \
libmpc libssl-dev

}

install_osx_pkg()
{
echo "Detected OSX! 暂不支持Mac OSX的一键安装!"
Expand Down Expand Up @@ -194,7 +209,7 @@ else
solus "$emulator" || exit 1
# Arch linux
elif hash 2>/dev/null pacman; then
archLinux "$emulator" || exit 1
install_archlinux_pkg || exit 1
# FreeBSD
elif hash 2>/dev/null pkg; then
freebsd "$emulator" || exit 1
Expand All @@ -221,4 +236,4 @@ USR=$USER
sudo adduser $USR kvm
sudo chown $USR /dev/kvm

congratulations
congratulations

0 comments on commit fef831d

Please sign in to comment.