SimpleKernel 1.17.0
Loading...
Searching...
No Matches
backtrace.cpp
Go to the documentation of this file.
1
5#include <cpu_io.h>
6#include <elf.h>
7
8#include <array>
9#include <cerrno>
10#include <cstdint>
11
12#include "arch.h"
13#include "basic_info.hpp"
14#include "kernel.h"
15#include "kernel_elf.hpp"
16#include "kernel_log.hpp"
17
18auto backtrace(std::array<uint64_t, kMaxFrameCount>& buffer) -> int {
19 auto* rbp = reinterpret_cast<uint64_t*>(cpu_io::Rbp::Read());
20 size_t count = 0;
21 while ((rbp != nullptr) && (*rbp != 0U) && count < buffer.max_size()) {
22 auto rip = *(rbp + 1);
23 if (rip < reinterpret_cast<uint64_t>(__executable_start) ||
24 rip > reinterpret_cast<uint64_t>(__etext)) {
25 break;
26 }
27 rbp = reinterpret_cast<uint64_t*>(*rbp);
28 buffer[count++] = rip;
29 }
30
31 return static_cast<int>(count);
32}
33
34auto DumpStack() -> void {
35 std::array<uint64_t, kMaxFrameCount> buffer{};
36
37 // 获取调用栈中的地址
38 auto num_frames = backtrace(buffer);
39
40 for (auto current_frame_idx = 0; current_frame_idx < num_frames;
41 current_frame_idx++) {
42 // 打印函数名
43 for (auto symtab : KernelElfSingleton::instance().symtab) {
44 if ((ELF64_ST_TYPE(symtab.st_info) == STT_FUNC) &&
45 (buffer[current_frame_idx] >= symtab.st_value) &&
46 (buffer[current_frame_idx] <= symtab.st_value + symtab.st_size)) {
47 klog::Err("[{}] {:#x}",
48 reinterpret_cast<const char*>(
49 KernelElfSingleton::instance().strtab + symtab.st_name),
50 buffer[current_frame_idx]);
51 }
52 }
53 }
54}
auto DumpStack() -> void
打印调用栈
Definition backtrace.cpp:34
__always_inline auto backtrace(std::array< uint64_t, kMaxFrameCount > &buffer) -> int
Definition backtrace.cpp:18
void * __etext[]
代码段结束
void * __executable_start[]
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志