-
Notifications
You must be signed in to change notification settings - Fork 126
Adds support for kernel symbol dumping via kallsyms #837
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
Changes from all commits
587b989
bed9fea
4cc930a
c89acc4
ac6e064
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,15 @@ use core::{ | |
| sync::atomic::{AtomicUsize, Ordering}, | ||
| }; | ||
|
|
||
| use ax_lazyinit::LazyInit; | ||
| use ax_memory_addr::PAGE_SIZE_4K; | ||
| use ax_runtime::hal::{ | ||
| paging::MappingFlags, | ||
| time::{monotonic_time, wall_time}, | ||
| }; | ||
| use ax_task::{AxCpuMask, AxTaskRef, TaskState, WeakAxTaskRef, current}; | ||
| use axfs_ng_vfs::{DeviceId, Filesystem, NodePermission, NodeType, VfsError, VfsResult}; | ||
| use ksym::KallsymsMapped; | ||
| use starry_process::{Pid, Process}; | ||
|
|
||
| use crate::{ | ||
|
|
@@ -41,6 +43,36 @@ use crate::{ | |
| static IRQ_CNT: AtomicUsize = AtomicUsize::new(0); | ||
| const PROCFS_INIT_PID: Pid = 1; | ||
|
|
||
| pub static KALLSYMS: LazyInit<KallsymsMapped<'static>> = LazyInit::new(); | ||
|
|
||
| fn read_kallsyms() -> KallsymsMapped<'static> { | ||
| unsafe extern "C" { | ||
| fn _stext(); | ||
| fn _etext(); | ||
| fn __kallsyms_start(); | ||
| fn __kallsyms_end(); | ||
| } | ||
|
|
||
| let kallsyms_start = __kallsyms_start as *const () as usize; | ||
| let kallsyms_end = __kallsyms_end as *const () as usize; | ||
| let kallsyms_sec_size = kallsyms_end - kallsyms_start; | ||
| let kallsyms_sec = | ||
| unsafe { core::slice::from_raw_parts(__kallsyms_start as *const u8, kallsyms_sec_size) }; | ||
|
|
||
| let total_size = | ||
|
Godones marked this conversation as resolved.
|
||
| KallsymsMapped::check_total_bytes(kallsyms_sec).expect("Invalid kallsyms format"); | ||
|
Godones marked this conversation as resolved.
|
||
|
|
||
| let kallsyms = &kallsyms_sec[..total_size as usize]; | ||
| // TODO: recycle unused space in .kallsyms section | ||
| info!("Read kallsyms, size: {}KB", kallsyms.len() / 1024); | ||
|
Godones marked this conversation as resolved.
|
||
| KallsymsMapped::from_blob( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 非阻塞建议:此处 |
||
| kallsyms, | ||
|
Godones marked this conversation as resolved.
|
||
| _stext as *const () as u64, | ||
| _etext as *const () as u64, | ||
| ) | ||
| .expect("Failed to create KallsymsMapped") | ||
|
Godones marked this conversation as resolved.
Godones marked this conversation as resolved.
|
||
| } | ||
|
|
||
| fn procfs_visible_pid(proc: &Arc<Process>) -> Pid { | ||
| if proc.is_init() { | ||
| PROCFS_INIT_PID | ||
|
|
@@ -961,6 +993,23 @@ fn builder(fs: Arc<SimpleFs>) -> DirMaker { | |
| SimpleDir::new_maker(fs.clone(), Arc::new(dynamic_debug)) | ||
| }); | ||
|
|
||
| static ALL_SYMS: LazyInit<String> = LazyInit::new(); | ||
|
|
||
| let ksym = read_kallsyms(); | ||
| KALLSYMS.init_once(ksym); | ||
|
|
||
| root.add("kallsyms", { | ||
| if !ALL_SYMS.is_inited() { | ||
| ALL_SYMS.init_once(KALLSYMS.dump_all_symbols()); | ||
| } | ||
| let seq_obj = SeqObject::new(|| Ok(ALL_SYMS.as_str())); | ||
| SpecialFsFile::new_regular_with_perm( | ||
| fs.clone(), | ||
| seq_obj, | ||
| NodePermission::from_bits_truncate(0o444), | ||
| ) | ||
| }); | ||
|
|
||
|
Godones marked this conversation as resolved.
|
||
| let proc_dir = ProcFsHandler(fs.clone()); | ||
| SimpleDir::new_maker(fs, Arc::new(proc_dir.chain(root))) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,12 @@ SECTIONS { | |
| KEEP(*(.tracepoint .tracepoint.*)) | ||
| __stop_tracepoint = .; | ||
| } | ||
|
|
||
| .kallsyms : ALIGN(4K) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| __kallsyms_start = .; | ||
| . += 8M; /* reserve space for kallsyms, can be recycled */ | ||
| __kallsyms_end = .; | ||
| } | ||
|
Godones marked this conversation as resolved.
|
||
| } | ||
|
|
||
| INSERT AFTER .data; | ||
| INSERT AFTER .data; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #!/usr/bin/env sh | ||
| set -eu | ||
|
|
||
| auto_install_enabled() { | ||
| case "${AXBUILD_STARRY_KALLSYMS_AUTO_INSTALL:-1}" in | ||
| 0 | n | no | false | off) return 1 ;; | ||
| *) return 0 ;; | ||
| esac | ||
|
Godones marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 非阻塞建议: |
||
| } | ||
|
|
||
| install_rust_binutils() { | ||
| if ! auto_install_enabled; then | ||
| echo "rust-nm and rust-objcopy are required for Starry kallsyms generation" >&2 | ||
| echo "install them with: rustup component add llvm-tools-preview && cargo install cargo-binutils" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Installing rust-nm and rust-objcopy (cargo-binutils)..." >&2 | ||
| if command -v rustup >/dev/null 2>&1; then | ||
| rustup component add llvm-tools-preview | ||
| fi | ||
| cargo install cargo-binutils | ||
| } | ||
|
|
||
| ensure_llvm_tools() { | ||
| if command -v rust-nm >/dev/null 2>&1 \ | ||
| && command -v rust-objcopy >/dev/null 2>&1 \ | ||
| && rust-nm --version >/dev/null 2>&1 \ | ||
| && rust-objcopy --version >/dev/null 2>&1; then | ||
| return | ||
| fi | ||
|
|
||
| if ! command -v rustup >/dev/null 2>&1; then | ||
| return | ||
| fi | ||
| if rustup component list --installed | grep -q '^llvm-tools'; then | ||
| return | ||
| fi | ||
|
|
||
| if ! auto_install_enabled; then | ||
| echo "llvm-tools-preview is required for rust-nm and rust-objcopy" >&2 | ||
| echo "install it with: rustup component add llvm-tools-preview" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Installing llvm-tools-preview via rustup..." >&2 | ||
| rustup component add llvm-tools-preview | ||
| } | ||
|
|
||
| install_ksym() { | ||
| if ! auto_install_enabled; then | ||
| echo "gen_ksym is required for Starry kallsyms generation" >&2 | ||
| echo "install it with: cargo install ksym" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Installing ksym (gen_ksym) via cargo..." >&2 | ||
| cargo install ksym | ||
| } | ||
|
|
||
| ensure_tools() { | ||
| ensure_llvm_tools | ||
|
|
||
| if ! command -v rust-nm >/dev/null 2>&1 || ! command -v rust-objcopy >/dev/null 2>&1; then | ||
| install_rust_binutils | ||
| fi | ||
| if ! command -v gen_ksym >/dev/null 2>&1; then | ||
| install_ksym | ||
| fi | ||
|
|
||
| command -v rust-nm >/dev/null 2>&1 | ||
| command -v rust-objcopy >/dev/null 2>&1 | ||
| command -v gen_ksym >/dev/null 2>&1 | ||
| } | ||
|
|
||
| generate_kallsyms() { | ||
| symbols=$(mktemp "${KERNEL_ELF}.symbols.XXXXXX") | ||
| kallsyms=$(mktemp "${KERNEL_ELF}.kallsyms.XXXXXX") | ||
| trap 'rm -f "$symbols" "$kallsyms"' EXIT | ||
|
|
||
| rust-nm -n "$KERNEL_ELF" > "$symbols" | ||
| grep ' [TtDBR] ' "$symbols" \ | ||
| | awk '$3 !~ /^\.L/' \ | ||
| | awk '$3 != "$x"' \ | ||
|
Godones marked this conversation as resolved.
Godones marked this conversation as resolved.
Godones marked this conversation as resolved.
Godones marked this conversation as resolved.
Godones marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 非阻塞建议: |
||
| | gen_ksym > "$kallsyms" | ||
|
Godones marked this conversation as resolved.
|
||
|
|
||
| rust-objcopy --update-section .kallsyms="$kallsyms" "$KERNEL_ELF" | ||
| } | ||
|
|
||
| refresh_bin_if_present() { | ||
| bin="${KERNEL_ELF%.elf}.bin" | ||
| if [ -f "$bin" ]; then | ||
| rust-objcopy --strip-all -O binary "$KERNEL_ELF" "$bin" | ||
| fi | ||
| } | ||
|
|
||
| if [ -z "${KERNEL_ELF:-}" ]; then | ||
| echo "KERNEL_ELF is required for Starry kallsyms generation" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| ensure_tools | ||
| generate_kallsyms | ||
| refresh_bin_if_present | ||
Uh oh!
There was an error while loading. Please reload this page.