From cf016f0c8b28c7198ae6adf3e6c88c61b91b07d0 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Sat, 20 Aug 2022 23:15:52 -0700 Subject: Debug output TODO: Add more debugging output --- Cargo.toml | 1 + Makefile | 8 +++++++- src/kernel.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- src/mem/alloc.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/print.rs | 26 ++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 711d693..f7f496d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ lto = true [features] default = [] bsp_rpi2 = [] +verbose = [] [[bin]] name = "kernel" diff --git a/Makefile b/Makefile index f2e1fa6..0b54131 100644 --- a/Makefile +++ b/Makefile @@ -20,11 +20,14 @@ COMPILER_ARGS=--target=$(TARGET) $(FEATURES) --release RUSTC_CMD=cargo rustc $(COMPILER_ARGS) export LINKER_FILE -.PHONY: build doc clean run +.PHONY: build doc clean run debug run-debug build: @RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD) +debug: + @RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" $(RUSTC_CMD) --features verbose + doc: @RUSTFLAGS="$(RUSTFLAGS_PEDANTIC)" cargo doc $(COMPILER_ARGS) @@ -34,6 +37,9 @@ clean: run: build @qemu-system-arm -cpu cortex-a7 -m 1G -kernel target/armv7a-none-eabi/release/kernel -machine raspi2b -serial mon:stdio -nographic +run-debug: debug + @qemu-system-arm -cpu cortex-a7 -m 1G -kernel target/armv7a-none-eabi/release/kernel -machine raspi2b -serial mon:stdio -nographic + init: rustup target install $(TARGET) rustup target add $(TARGET) diff --git a/src/kernel.rs b/src/kernel.rs index 3d93e37..1c3bfac 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -27,7 +27,6 @@ mod print; mod sync; mod uart; use crate::console::console; -use crate::console::interface::Statistics; use crate::mem::alloc::alloc; /// # Initialization Code @@ -48,13 +47,49 @@ unsafe fn kernel_init() -> ! { /// /// TODO: Figure out what to do here fn kernel_main() -> ! { - draw::draw_ukraine_flag(); - println!(); - draw::draw_american_flag(); - println!(); - - println!("\x1b[91mInitialized\x1b[0m \x1b[92m{}\x1b[0m \x1b[93mv{}\x1b[0m", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); - println!("\x1b[94mAuthors:\x1b[0m \x1b[95m{}\x1b[0m", env!("CARGO_PKG_AUTHORS")); - println!("Characters written to UART: \x1b[91m{}\x1b[0m", console().chars_written()); + #[cfg(not(feature="verbose"))] + { + draw::draw_ukraine_flag(); + println!(); + draw::draw_american_flag(); + println!(); + + println!("\x1b[91mInitialized\x1b[0m \x1b[92m{}\x1b[0m \x1b[93mv{}\x1b[0m", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); + println!("\x1b[94mAuthors:\x1b[0m \x1b[95m{}\x1b[0m", env!("CARGO_PKG_AUTHORS")); + use crate::console::interface::Statistics; + println!("Characters written to UART: \x1b[91m{}\x1b[0m", console().chars_written()); + } + + #[cfg(feature="verbose")] + { + use alloc::boxed::Box; + { + let a: Box = Box::new(1); + println!("Box: {}", a); + } + { + let a: Box = Box::new(2); + let b: Box = Box::new(3); + let c: Box = Box::new(4); + println!("Boxes: {}, {}, {}", a, b, c); + } + { + let a: Box = Box::new(5); + let b: Box = Box::new(6); + let c: Box = Box::new(7); + println!("Boxes: {}, {}, {}", a, b, c); + } + println!("U8: {:?}", mem::alloc::U8_GRAND_ALLOC); + use alloc::string::String; + { + let mut s = String::new(); + for _ in 0..128 { + s += "TEST"; + } + println!("String: Length {}", s.capacity()); + } + use crate::console::interface::Statistics; + println!("Characters written to UART: \x1b[91m{}\x1b[0m", console().chars_written()); + } loop { } } diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs index 1a4ad77..e104293 100644 --- a/src/mem/alloc.rs +++ b/src/mem/alloc.rs @@ -8,6 +8,8 @@ use crate::sync::interface::Mutex; use core::fmt; use core::fmt::{Debug,Formatter}; +use crate::vprintln; + /// # Initialize Queue /// - Name: Symbol name /// - Size: Number of elements @@ -60,7 +62,11 @@ impl Debug for QueueItem<'_,T> { /// /// Output the encapsulated data fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{:?} {:x} {:?}", self.data, self as *const QueueItem<'_,T> as usize, self.next) + #[cfg(feature="verbose")] + return write!(f, "{:?} {:x} {:?}", self.data, self as *const QueueItem<'_,T> as usize, self.next); + + #[cfg(not(feature="verbose"))] + return write!(f, "{:?}", self.data); } } @@ -84,7 +90,9 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { /// All of the internal elements point to the next /// one and the final element points to `None` pub fn init(&self) { + vprintln!("QA: Initializing Queue Allocator!"); self.inner.lock(|queue| { + vprintln!("QA: Clearing internal references..."); for idx in 2..COUNT { if idx != COUNT-1 { queue[idx].next = Some(&mut queue[idx+1] as *mut QueueItem<'a, T>); @@ -92,9 +100,11 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { queue[idx].next = None; } } + vprintln!("QA: Initializing head and tail..."); queue[0].next = Some(&mut queue[2] as *mut QueueItem<'a, T>); queue[1].next = Some(&mut queue[COUNT-1] as *mut QueueItem<'a, T>); }); + vprintln!("QA: Initialized Queue Allocator!"); } /// # Allocate Data @@ -103,8 +113,10 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { /// return it, otherwise return `None` #[allow(dead_code)] pub fn alloc(&self) -> Option<&mut QueueItem<'a,T>> { + vprintln!("QA: Allocating chunk!"); return self.inner.lock(|pool| { if let Some(entry) = pool[0].next { + vprintln!("QA: Found chunk!"); pool[0].next = unsafe { (*entry).next }; unsafe { (*entry).next = None; @@ -115,8 +127,10 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { } _ => {} } + vprintln!("QA: Allocating {:x}", unsafe{(*entry).ptr() as usize}); return Some(unsafe{&mut *entry as &mut QueueItem<'a,T>}); } else { + vprintln!("QA: No chunks available!"); return None; } }); @@ -128,6 +142,7 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { /// If there were no items, set it as the head. #[allow(dead_code)] pub fn free(&self, freed_item: &mut QueueItem<'a,T>) { + vprintln!("QA: Deallocating chunk!"); self.inner.lock(|pool| { freed_item.next = None; match pool[1].next { @@ -141,6 +156,7 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { } } pool[1].next = Some(freed_item as *mut QueueItem<'a,T>); + vprintln!("QA: Deallocated {:x}", freed_item.ptr() as usize); }); } } @@ -152,7 +168,11 @@ impl Debug for QueueAllocator<'_,T,COUNT> { /// its debug formatter. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { self.inner.lock(|queue| { - write!(f, "{:?}", queue) + #[cfg(feature="verbose")] + return write!(f, "{:?}", queue); + + #[cfg(not(feature="verbose"))] + return write!(f, "{:?}", queue); }) } } @@ -237,16 +257,27 @@ init_queue!(U4096_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U4096::new()}, U4096); impl GrandAllocator { pub fn init(&self) -> Result<(), &'static str> { + vprintln!("GA: Init U8 Pool"); U8_GRAND_ALLOC.init(); + vprintln!("GA: Init U16 Pool"); U16_GRAND_ALLOC.init(); + vprintln!("GA: Init U32 Pool"); U32_GRAND_ALLOC.init(); + vprintln!("GA: Init U64 Pool"); U64_GRAND_ALLOC.init(); + vprintln!("GA: Init U128 Pool"); U128_GRAND_ALLOC.init(); + vprintln!("GA: Init U256 Pool"); U256_GRAND_ALLOC.init(); + vprintln!("GA: Init U512 Pool"); U512_GRAND_ALLOC.init(); + vprintln!("GA: Init U1024 Pool"); U1024_GRAND_ALLOC.init(); + vprintln!("GA: Init U2048 Pool"); U2048_GRAND_ALLOC.init(); + vprintln!("GA: Init U4096 Pool"); U4096_GRAND_ALLOC.init(); + vprintln!("GA: Pools Initialized!"); Ok(()) } } @@ -256,6 +287,7 @@ unsafe impl GlobalAlloc for GrandAllocator { /// /// Allocate the fixed size chunks unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + vprintln!("GA: Allocating chunk of size {}!", layout.size()); match layout.size() { 1 => { match U8_GRAND_ALLOC.alloc() { @@ -367,6 +399,7 @@ unsafe impl GlobalAlloc for GrandAllocator { /// /// Deallocate the fixed size chunks by searching for them unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + vprintln!("GA: Deallocating chunk of size {}!", layout.size()); match layout.size() { 1 => { U8_GRAND_ALLOC.inner.lock(|pool| { @@ -376,6 +409,7 @@ unsafe impl GlobalAlloc for GrandAllocator { assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE); assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing); U8_GRAND_ALLOC.free(&mut pool[index+2]); + vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); } 2 => { @@ -485,5 +519,6 @@ pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; /// /// Returns a borrow for the Global Allocator pub fn alloc() -> &'static crate::mem::alloc::GrandAllocator { + vprintln!("AL: Getting global allocator!"); &crate::mem::alloc::ALLOCATOR } diff --git a/src/print.rs b/src/print.rs index 88f39da..1598378 100644 --- a/src/print.rs +++ b/src/print.rs @@ -28,3 +28,29 @@ macro_rules! println { $crate::print::_print(format_args_nl!($($arg)*)); }) } + +/// # Debug print without newline +/// +/// Print formatted arguments without a newline but only with `verbose` feature +#[macro_export] +macro_rules! vprint { + ($($arg:tt)*) => ({ + #[cfg(feature="verbose")] + $crate::print::_print(format_args!($($arg)*)) + }); +} + +/// # Debug print with newline +/// +/// Print formatted arguments with a newline but only with `verbose` feature +#[macro_export] +macro_rules! vprintln { + () => ({ + #[cfg(feature="verbose")] + $crate::print!("\n") + }); + ($($arg:tt)*) => ({ + #[cfg(feature="verbose")] + $crate::print::_print(format_args_nl!($($arg)*)); + }) +} -- cgit v1.2.1