diff --git a/blog/content/second-edition/posts/03-vga-text-buffer/index.md b/blog/content/second-edition/posts/03-vga-text-buffer/index.md index 6dd6df34..2af8d68c 100644 --- a/blog/content/second-edition/posts/03-vga-text-buffer/index.md +++ b/blog/content/second-edition/posts/03-vga-text-buffer/index.md @@ -616,6 +616,28 @@ As expected, we now see a _“Hello World!”_ on the screen: ![QEMU printing “Hello World!”](vga-hello-world.png) +### Printing Panic Messages + +Now that we have a `println` macro, we can use it in our panic function to print the panic message and the location of the panic: + +```rust +// in main.rs + +/// This function is called on panic. +#[panic_implementation] +#[no_mangle] +pub fn panic(info: &PanicInfo) -> ! { + println!("{}", info); + loop {} +} +``` + +When we now insert `panic!("Some panic message");` in our `_start` function, we get the following output: + +![QEMU printing “panicked at 'Some panic message', src/main.rs:28:5](vga-panic.png) + +So we know not only that a panic has occurred, but also the panic message and where in the code it happened. + ## Summary In this post we learned about the structure of the VGA text buffer and how it can be written through the memory mapping at address `0xb8000`. We created a Rust module that encapsulates the unsafety of writing to this memory mapped buffer and presents a safe and convenient interface to the outside. diff --git a/blog/content/second-edition/posts/03-vga-text-buffer/vga-panic.png b/blog/content/second-edition/posts/03-vga-text-buffer/vga-panic.png new file mode 100644 index 00000000..70761281 Binary files /dev/null and b/blog/content/second-edition/posts/03-vga-text-buffer/vga-panic.png differ diff --git a/src/main.rs b/src/main.rs index a6bfbdb9..30879508 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ pub extern "C" fn _start() -> ! { /// This function is called on panic. #[panic_implementation] #[no_mangle] -pub fn panic(_info: &PanicInfo) -> ! { +pub fn panic(info: &PanicInfo) -> ! { + println!("{}", info); loop {} }