Skip to content

Commit

Permalink
refactor: prints command
Browse files Browse the repository at this point in the history
  • Loading branch information
winstonallo committed Mar 2, 2025
1 parent 3aa8047 commit dacbf76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
"terminal.integrated.shell.linux": "/bin/zsh"
},
"extensions": [
"rust-lang.rust-analyzer",
Expand Down
2 changes: 2 additions & 0 deletions src/printk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl fmt::Write for PrintkWriter {

/// Prints the formatted arguments to the screen. This macro needs to be wrapped in an unsafe
/// block, as we could inadvertedly run unchecked code through it otherwise.
///
/// `printk!` flushes when the buffer (`1KB`) fills up or when encountering a `\n`.
#[macro_export]
macro_rules! printk {
($($arg:tt)*) => {{
Expand Down
45 changes: 28 additions & 17 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,43 @@ fn printsb_cmd(_args: &[u8], _s: &mut Screen) {
unsafe { printk!("ESP: 0x{:#08x} STACK_TOP: 0x{:#08x}\n", get_stack_pointer(), &stack_top as *const u8 as u32) };
}

/// Dumps a row of 16 bytes in the following format:
///
/// ```
/// 001c503c-001c504b 20077007 72076907 6e077407 73072007 .p.r.i.n.t.s. .`
/// ^ ^ ^
/// address range hexdump ASCII dump
/// ```
fn dump_row(row: [u8; 16], ptr: *const u8) {
unsafe {
printk!("{:08x}-{:08x} ", ptr as u32, ptr as u32 + 15);
for word in row.chunks(4) {
// Reminder to future self: casting to u32 prints the bytes in little-endian.
for byte in word {
printk!("{:02x}", byte);
}
printk!(" ")
}
for byte in row {
printk!("{}", (if !(32..127).contains(&byte) { b'.' } else { byte }) as char);
}
printk!("\n");
};
}

/// Prints the stack from %esp to the stack top.
fn prints_cmd(_args: &[u8], s: &mut Screen) {
printsb_cmd(_args, s);
let sp_addr = get_stack_pointer();
let st = unsafe { &stack_top as *const u8 as u32 };
let mut bytes: [u8; 16];
let mut row: [u8; 16];

assert!(sp_addr <= st);

for row_idx in (sp_addr..st).step_by(16) {
let ptr = row_idx as *const u8;
bytes = unsafe { *(ptr as *const [u8; 16]) };

unsafe {
printk!("{:08x}-{:08x} ", ptr as u32, ptr as u32 + 15);
for word in bytes.chunks(4) {
// Reminder to future self: casting to u32 prints the bytes in little-endian.
for byte in word {
printk!("{:02x}", byte);
}
printk!(" ")
}
for byte in bytes {
printk!("{}", (if !(32..127).contains(&byte) { b'.' } else { byte }) as char);
}
printk!("\n");
};
row = unsafe { *(ptr as *const [u8; 16]) };
dump_row(row, ptr);
}
}

Expand Down

0 comments on commit dacbf76

Please sign in to comment.