Skip to content

Implement symbol names in backtrace functionality#11

Draft
0x6D70 with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-10
Draft

Implement symbol names in backtrace functionality#11
0x6D70 with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-10

Conversation

Copilot AI commented Jul 5, 2025

Copy link
Copy Markdown

This PR implements the TODO to print symbol names in the backtrace functionality located in kernel/src/utils/backtrace.rs.

Changes Made

1. Created Symbol Management Module (kernel/src/symbols.rs)

  • Symbol table storage: Uses BTreeMap for efficient symbol lookups by address
  • ELF symbol parsing: Attempts to load symbols from kernel ELF file via limine KernelFileRequest
  • Robust symbol resolution: Handles exact matches and offset-based lookups within symbol boundaries
  • Fallback mechanism: Provides basic placeholder symbols if ELF parsing fails

2. Enhanced Backtrace Output (kernel/src/utils/backtrace.rs)

  • Symbol resolution: Calls symbols::get_symbol(rip) for each stack frame
  • Improved formatting: Shows frame numbers and function names with offsets
  • Safety features: Limits to 32 frames to prevent infinite loops
  • Better UX: Much more informative than raw addresses alone

3. Integration and Testing

  • Boot initialization: Symbols module initialized during kernel startup
  • Comprehensive testing: Added test functions to verify symbol resolution
  • Error handling: Graceful fallbacks when symbols aren't available

Before vs After

Before (TODO):

let symbol = ""; // crate::symbols::get_symbol(rip);
dbg!("0x{:x} {}", rip, symbol);

After (Implemented):

let symbol = crate::symbols::get_symbol(rip).unwrap_or_else(|| "<unknown>".to_string());
dbg!("#{}: 0x{:x} {}", frame_count, rip, symbol);

Example backtrace output:

==================== BACKTRACE ====================
#0: 0x100500 kernel_main+0x500
#1: 0x101100 panic_handler+0x100  
#2: 0x102080 log_backtrace+0x80
==================================================

Technical Details

The implementation uses limine's KernelFileRequest to access the kernel ELF file at runtime, then parses the symbol table to extract function names and addresses. Symbol lookup uses efficient binary search through the sorted address space, with support for both exact matches and reasonable offset-based matches.

The symbol resolution algorithm:

  1. Checks for exact address matches within symbol boundaries
  2. Falls back to nearest symbol with reasonable offset (< symbol size + 1KB)
  3. Returns <unknown> for addresses that can't be resolved

Fixes #10.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits July 5, 2025 19:21
Co-authored-by: 0x6D70 <43414123+0x6D70@users.noreply.github.com>
Co-authored-by: 0x6D70 <43414123+0x6D70@users.noreply.github.com>
Co-authored-by: 0x6D70 <43414123+0x6D70@users.noreply.github.com>
Copilot AI changed the title [WIP] print symbol names Implement symbol names in backtrace functionality Jul 5, 2025
Copilot AI requested a review from 0x6D70 July 5, 2025 19:26
Copilot finished work on behalf of 0x6D70 July 5, 2025 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

print symbol names

2 participants