Skip to content

Commit

Permalink
fix(backtrace):Use more reasonable compile options
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Nov 21, 2024
1 parent 081428c commit 7ffc52e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = [
]

[features]
default = ["backtrace", "kvm", "fatfs", "fatfs-secure", "static_keys_test"]
default = ["kvm", "fatfs", "fatfs-secure", "static_keys_test"]
# 内核栈回溯
backtrace = ["dep:unwinding"]
# kvm
Expand Down
10 changes: 8 additions & 2 deletions kernel/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
DIR_LIB=libs
lib_patterns := *.a
LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))

EXT_FEATURES =

# unwind/backtrace related
UNWIND_ENABLE ?= yes
Expand All @@ -17,6 +17,9 @@ ifeq ($(UNWIND_ENABLE), yes)
CFLAGS_UNWIND = -funwind-tables
LDFLAGS_UNWIND = --eh-frame-hdr
RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
EXT_FEATURES += backtrace
else
RUSTFLAGS_UNWIND = -Cpanic=abort
endif

RUSTFLAGS += $(RUSTFLAGS_UNWIND)
Expand All @@ -38,7 +41,10 @@ kernel_subdirs := debug


kernel_rust:
RUSTFLAGS="$(RUSTFLAGS)" cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release --target $(TARGET_JSON)
RUSTFLAGS="$(RUSTFLAGS)" \
cargo +nightly-2024-11-05 $(CARGO_ZBUILD) build --release \
--target $(TARGET_JSON) \
--features "$(EXT_FEATURES)"


all: kernel
Expand Down
19 changes: 13 additions & 6 deletions kernel/src/arch/x86_64/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ pub extern "sysv64" fn syscall_handler(frame: &mut TrapFrame) {
}
_ => {}
}
syscall_return!(
Syscall::catch_handle(syscall_num, &args, frame)
.unwrap_or_else(|e| e.to_posix_errno() as usize) as u64,
frame,
show
);
let mut syscall_handle = || -> u64 {
#[cfg(feature = "backtrace")]
{
Syscall::catch_handle(syscall_num, &args, frame)
.unwrap_or_else(|e| e.to_posix_errno() as usize) as u64
}
#[cfg(not(feature = "backtrace"))]
{
Syscall::handle(syscall_num, &args, frame)
.unwrap_or_else(|e| e.to_posix_errno() as usize) as u64
}
};
syscall_return!(syscall_handle(), frame, show);
}

/// 系统调用初始化
Expand Down
49 changes: 24 additions & 25 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ extern crate wait_queue_macros;

use crate::mm::allocator::kernel_allocator::KernelAllocator;

#[cfg(feature = "backtrace")]
use unwinding::{
abi::{UnwindContext, UnwindReasonCode, _Unwind_GetIP},
panic::UserUnwindTrace,
};

extern "C" {
fn lookup_kallsyms(addr: u64, level: i32) -> i32;
}
Expand Down Expand Up @@ -159,9 +153,9 @@ pub fn panic(info: &PanicInfo) -> ! {
println!("Message:\n\t{}", info.message());
#[cfg(feature = "backtrace")]
{
let mut data = CallbackData { counter: 0 };
let mut data = hook::CallbackData { counter: 0 };
println!("Rust Panic Backtrace:");
let res = unwinding::panic::begin_panic_with_hook::<Tracer>(
let res = unwinding::panic::begin_panic_with_hook::<hook::Tracer>(
alloc::boxed::Box::new(()),
&mut data,
);
Expand All @@ -172,26 +166,31 @@ pub fn panic(info: &PanicInfo) -> ! {
process::ProcessManager::current_pcb()
);
process::ProcessManager::exit(usize::MAX);
loop {}
}
#[cfg(feature = "backtrace")]
mod hook {
use crate::lookup_kallsyms;
use unwinding::abi::{UnwindContext, UnwindReasonCode, _Unwind_GetIP};
use unwinding::panic::UserUnwindTrace;

/// User hook for unwinding
///
/// During stack backtrace, the user can print the function location of the current stack frame.
struct Tracer;
struct CallbackData {
counter: usize,
}
impl UserUnwindTrace for Tracer {
type Arg = CallbackData;
/// User hook for unwinding
///
/// During stack backtrace, the user can print the function location of the current stack frame.
pub struct Tracer;
pub struct CallbackData {
pub counter: usize,
}
impl UserUnwindTrace for Tracer {
type Arg = CallbackData;

fn trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode {
let data = unsafe { &mut *(arg) };
data.counter += 1;
let pc = _Unwind_GetIP(ctx);
unsafe {
lookup_kallsyms(pc as u64, data.counter as i32);
fn trace(ctx: &UnwindContext<'_>, arg: *mut Self::Arg) -> UnwindReasonCode {
let data = unsafe { &mut *(arg) };
data.counter += 1;
let pc = _Unwind_GetIP(ctx);
unsafe {
lookup_kallsyms(pc as u64, data.counter as i32);
}
UnwindReasonCode::NO_REASON
}
UnwindReasonCode::NO_REASON
}
}
5 changes: 2 additions & 3 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::{
ffi::{c_int, c_void},
hint::spin_loop,
sync::atomic::{AtomicBool, Ordering},
};

Expand All @@ -11,7 +10,6 @@ use crate::{
libs::{futex::constant::FutexFlag, rand::GRandFlags},
mm::{page::PAGE_4K_SIZE, syscall::MremapFlags},
net::syscall::MsgHdr,
process,
process::{
fork::KernelCloneArgs,
resource::{RLimit64, RUsage},
Expand Down Expand Up @@ -79,14 +77,15 @@ impl Syscall {
/// 系统调用分发器,用于分发系统调用。
///
/// 与[handle]不同,这个函数会捕获系统调用处理函数的panic,返回错误码。
#[cfg(feature = "backtrace")]
pub fn catch_handle(
syscall_num: usize,
args: &[usize],
frame: &mut TrapFrame,
) -> Result<usize, SystemError> {
let res = unwinding::panic::catch_unwind(|| Self::handle(syscall_num, args, frame));
res.unwrap_or_else(|_| loop {
spin_loop();
core::hint::spin_loop();
})
}
/// @brief 系统调用分发器,用于分发系统调用。
Expand Down

0 comments on commit 7ffc52e

Please sign in to comment.