Implement backtrace support for x86_64

master
Jiajie Chen 6 years ago
parent 6f1953b977
commit 671b7c7b54

@ -13,7 +13,9 @@ SECTIONS {
.text ALIGN(4K):
{
stext = .;
*(.text .text.*)
etext = .;
}
.data ALIGN(4K):

@ -1,13 +1,13 @@
use core::mem::size_of;
use rcore_memory::PAGE_SIZE;
extern "C" {
fn stext();
fn etext();
}
/// Returns the current frame pointer.
/// Returns the current frame pointer.or stack base pointer
#[inline(always)]
#[cfg(any(target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64"))]
pub fn fp() -> usize {
let ptr: usize;
#[cfg(target_arch = "aarch64")]
@ -18,13 +18,16 @@ pub fn fp() -> usize {
unsafe {
asm!("mv $0, s0" : "=r"(ptr));
}
#[cfg(target_arch = "x86_64")]
unsafe {
asm!("mov %rbp, $0" : "=r"(ptr));
}
ptr
}
/// Returns the current link register.
/// Returns the current link register.or return address
#[inline(always)]
#[cfg(any(target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64"))]
pub fn lr() -> usize {
let ptr: usize;
#[cfg(target_arch = "aarch64")]
@ -35,13 +38,16 @@ pub fn lr() -> usize {
unsafe {
asm!("mv $0, ra" : "=r"(ptr));
}
#[cfg(target_arch = "x86_64")]
unsafe {
asm!("movq 8(%rbp), $0" : "=r"(ptr));
}
ptr
}
// Print the backtrace starting from the caller
pub fn backtrace() {
#[cfg(any(target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64"))]
unsafe {
let mut current_pc = lr();
let mut current_fp = fp();
@ -61,6 +67,16 @@ pub fn backtrace() {
current_pc = *(current_fp as *const usize).offset(1);
}
}
#[cfg(target_arch = "x86_64")]
{
// Kernel stack at 0x0000_57ac_0000_0000 (defined in bootloader crate)
// size = 512 pages
current_fp = *(current_fp as *const usize).offset(0);
if (current_fp >= 0x0000_57ac_0000_0000 + 512 * PAGE_SIZE - size_of::<usize>()) {
break;
}
current_pc = *(current_fp as *const usize).offset(1);
}
}
}
}

Loading…
Cancel
Save