Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stack backtrace #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion os/.cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ target = "riscv64gc-unknown-none-elf"

[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-arg=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes"
"-Clink-arg=-Tsrc/linker.ld"
]
2 changes: 1 addition & 1 deletion os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ lose-net-stack = { git = "https://github.com/yfblock/lose-net-stack", rev = "3f4
easy-fs = { path = "../easy-fs" }
embedded-graphics = "0.7.1"
tinybmp = "0.3.1"

trace_lib = {git = "https://github.com/os-module/rtrace"}
[profile.release]
debug = true
10 changes: 8 additions & 2 deletions os/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64

# Disassembly
DISASM ?= -x
DISASM ?= -X

# Run usertests or usershell
TEST ?=

build: env $(KERNEL_BIN) fs-img
build: env stack_trace $(KERNEL_BIN) fs-img

env:
(rustup target list | grep "riscv64gc-unknown-none-elf (installed)") || rustup target add $(TARGET)
Expand All @@ -53,11 +53,17 @@ fs-img: $(APPS)
@cd ../easy-fs-fuse && cargo run --release -- -s ../user/src/bin/ -t ../user/target/riscv64gc-unknown-none-elf/release/

$(APPS):
stack_trace:
@# install trace_exe to generate elf symbol info
@cargo install --git https://github.com/os-module/elfinfo

kernel:
@echo Platform: $(BOARD)
@touch src/trace/kernel_symbol.S && rm src/trace/kernel_symbol.S
@cp src/linker-$(BOARD).ld src/linker.ld
@cargo build --release
@(nm -n ${KERNEL_ELF} | trace_exe > src/trace/kernel_symbol.S)
@cargo build --release
@rm src/linker.ld

clean:
Expand Down
19 changes: 19 additions & 0 deletions os/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
use std::fs::File;
use std::path::Path;
use std::io::Write;
static TARGET_PATH: &str = "../user/target/riscv64gc-unknown-none-elf/release/";

fn main() {
println!("cargo:rerun-if-changed=../user/src/");
println!("cargo:rerun-if-changed={}", TARGET_PATH);
println!("cargo:rerun-if-changed={}", "src");
let path = Path::new("src/trace/kernel_symbol.S");
if !path.exists() {
let mut file = File::create(path).unwrap();
write!(file, ".section .rodata\n").unwrap();
write!(file, ".align 3\n").unwrap();
write!(file, ".global symbol_num\n").unwrap();
write!(file, ".global symbol_address\n").unwrap();
write!(file, ".global symbol_index\n").unwrap();
write!(file, ".global symbol_name\n").unwrap();
write!(file, "symbol_num:\n").unwrap();
write!(file, ".quad {}\n", 0).unwrap();
write!(file, "symbol_address:\n").unwrap();
write!(file, "symbol_index:\n").unwrap();
write!(file, "symbol_name:\n").unwrap();
}
}
22 changes: 8 additions & 14 deletions os/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,17 @@ fn panic(info: &PanicInfo) -> ! {
} else {
println!("[kernel] Panicked: {}", info.message().unwrap());
}
unsafe {
backtrace();
}
backtrace();
shutdown(255)
}

unsafe fn backtrace() {
let mut fp: usize;
let stop = current_kstack_top();
asm!("mv {}, s0", out(reg) fp);
#[no_mangle]
fn backtrace() {
println!("---START BACKTRACE---");
for i in 0..10 {
if fp == stop {
break;
}
println!("#{}:ra={:#x}", i, *((fp - 8) as *const usize));
fp = *((fp - 16) as *const usize);
}
let info = crate::trace::init_kernel_trace();
let func_info = unsafe { trace_lib::my_trace(info) };
func_info.iter().for_each(|x| {
println!("{}", x);
});
println!("---END BACKTRACE---");
}
20 changes: 20 additions & 0 deletions os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(naked_functions)]
#![feature(fn_align)]

use core::arch::global_asm;
//use crate::drivers::{GPU_DEVICE, KEYBOARD_DEVICE, MOUSE_DEVICE, INPUT_CONDVAR};
use crate::drivers::{GPU_DEVICE, KEYBOARD_DEVICE, MOUSE_DEVICE};
extern crate alloc;
Expand All @@ -29,6 +30,7 @@ mod task;
mod timer;
mod trap;
mod net;
mod trace;

use riscv::register::*;
// mod riscvreg;
Expand Down Expand Up @@ -251,6 +253,7 @@ use crate::drivers::chardev::CharDevice;
use crate::drivers::chardev::UART;
#[no_mangle]
pub fn rust_main() -> ! {
// global_asm!(include_str!("kernel_symbol.S"));

//clear_bss();
mm::init();
Expand All @@ -272,3 +275,20 @@ pub fn rust_main() -> ! {
task::run_tasks();
panic!("Unreachable in rust_main!");
}

#[no_mangle]
fn test_tp(){
let a = 33;
let b = 22;
let max = core::cmp::max(a,b);
// println!("max is {}",max);
test_tp_tp(max);
}


#[no_mangle]
fn test_tp_tp(max:i32){
let a = 33;
let max = core::cmp::max(a,max);
println!("test tp tp {}",max);
}
Loading