Implement backtrace support for RISCV64

master
Jiajie Chen 6 years ago
parent d8edd1a7db
commit 7d6856ceab

@ -5,44 +5,60 @@ extern "C" {
/// Returns the current frame pointer.
#[inline(always)]
#[cfg(target_arch = "aarch64")]
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub fn fp() -> usize {
let ptr: usize;
#[cfg(target_arch = "aarch64")]
unsafe {
asm!("mov $0, x29" : "=r"(ptr));
}
#[cfg(target_arch = "riscv64")]
unsafe {
asm!("mv $0, s0" : "=r"(ptr));
}
ptr
}
/// Returns the current link register.
#[inline(always)]
#[cfg(target_arch = "aarch64")]
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub fn lr() -> usize {
let ptr: usize;
#[cfg(target_arch = "aarch64")]
unsafe {
asm!("mov $0, x30" : "=r"(ptr));
}
#[cfg(target_arch = "riscv64")]
unsafe {
asm!("mv $0, ra" : "=r"(ptr));
}
ptr
}
// Print the backtrace starting from the caller
pub fn backtrace() {
#[cfg(target_arch = "aarch64")]
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
unsafe {
let mut current_pc = lr();
let mut current_fp = fp();
let mut stack_num = 0;
println!("pc {:#018X} fp {:#018X}", current_pc, current_fp);
while current_pc >= stext as usize && current_pc <= etext as usize && current_fp as usize != 0 {
println!("#{} {:#018X}", stack_num, current_pc);
println!("#{} {:#018X} fp {:#18X}", stack_num, current_pc - 4, current_fp);
stack_num = stack_num + 1;
current_fp = *(current_fp as *const usize);
if current_fp as usize != 0 {
current_pc = *(current_fp as *const usize).offset(1);
#[cfg(target_arch = "riscv64")]
{
current_fp = *((current_fp - 16) as *const usize);
current_pc = *(current_fp as *const usize).offset(-1);
}
#[cfg(target_arch = "aarch64")]
{
current_fp = *(current_fp as *const usize);
if current_fp != 0 {
current_pc = *(current_fp as *const usize).offset(1);
}
}
println!("pc {:#018X} fp {:#018X}", current_pc, current_fp);
}
}
}

@ -31,5 +31,6 @@
"ptx-kernel",
"msp430-interrupt",
"x86-interrupt"
]
],
"eliminate-frame-pointer": false
}

Loading…
Cancel
Save