mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Rewrite breakpoint test
This commit is contained in:
@@ -404,65 +404,42 @@ Let's create an integration test that ensures that the above continues to work.
|
|||||||
```rust
|
```rust
|
||||||
// in src/bin/test-exception-breakpoint.rs
|
// in src/bin/test-exception-breakpoint.rs
|
||||||
|
|
||||||
[…]
|
#![no_std]
|
||||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
#![cfg_attr(not(test), no_main)]
|
||||||
|
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||||
|
|
||||||
static BREAKPOINT_HANDLER_CALLED: AtomicUsize = AtomicUsize::new(0);
|
use core::panic::PanicInfo;
|
||||||
|
use blog_os::{exit_qemu, serial_println};
|
||||||
|
|
||||||
|
#[cfg(not(test))]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
init_test_idt();
|
blog_os::interrupts::init_idt();
|
||||||
|
|
||||||
// invoke a breakpoint exception
|
|
||||||
x86_64::instructions::int3();
|
x86_64::instructions::int3();
|
||||||
|
|
||||||
match BREAKPOINT_HANDLER_CALLED.load(Ordering::SeqCst) {
|
serial_println!("ok");
|
||||||
1 => serial_println!("ok"),
|
|
||||||
0 => {
|
|
||||||
serial_println!("failed");
|
|
||||||
serial_println!("Breakpoint handler was not called.");
|
|
||||||
}
|
|
||||||
other => {
|
|
||||||
serial_println!("failed");
|
|
||||||
serial_println!("Breakpoint handler was called {} times", other);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe { exit_qemu(); }
|
unsafe { exit_qemu(); }
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lazy_static! {
|
#[cfg(not(test))]
|
||||||
static ref TEST_IDT: InterruptDescriptorTable = {
|
#[panic_handler]
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
serial_println!("failed");
|
||||||
idt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_test_idt() {
|
serial_println!("{}", info);
|
||||||
TEST_IDT.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn breakpoint_handler(
|
unsafe { exit_qemu(); }
|
||||||
_stack_frame: &mut ExceptionStackFrame)
|
loop {}
|
||||||
{
|
|
||||||
BREAKPOINT_HANDLER_CALLED.fetch_add(1, Ordering::SeqCst);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[…]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
For space reasons we don't show the full content here. You can find the full file [on Github](https://github.com/phil-opp/blog_os/blob/master/src/bin/test-exception-breakpoint.rs).
|
It is similar to our `main.rs`, but instead of printing "It did not crash!" to the VGA buffer, it prints "ok" to the serial output and calls `exit_qemu`. This allows the `bootimage` tool to detect that our code successfully continued after invoking the `int3` instruction. If our `panic_handler` is called, we instead print `failed` to signalize the failure to `bootimage`.
|
||||||
|
|
||||||
It is similar to our `main.rs`, but uses a custom IDT called `TEST_IDT` and different `_start` and `breakpoint_handler` functions. The most interesting part is the `BREAKPOINT_HANDLER_CALLED` static. It is an [`AtomicUsize`], an integer type that can be safely concurrently modified because all of its operations are atomic. We increment it when the `breakpoint_handler` is called and verify in our `_start` function that the handler was called exactly once.
|
You can try this new test by running `bootimage test`.
|
||||||
|
|
||||||
[`AtomicUsize`]: https://doc.rust-lang.org/core/sync/atomic/struct.AtomicUsize.html
|
|
||||||
|
|
||||||
The [`Ordering`] parameter specifies the desired guarantees of the atomic operations. The `SeqCst` ordering means “sequential consistent” and gives the strongest guarantees. It is a good default, because weaker orderings can have undesired effects.
|
|
||||||
|
|
||||||
[`Ordering`]: https://doc.rust-lang.org/core/sync/atomic/enum.Ordering.html
|
|
||||||
|
|
||||||
### Fixing `cargo test` on Windows
|
### Fixing `cargo test` on Windows
|
||||||
|
|
||||||
|
|||||||
@@ -1,70 +1,31 @@
|
|||||||
#![feature(abi_x86_interrupt)]
|
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![cfg_attr(not(test), no_main)]
|
#![cfg_attr(not(test), no_main)]
|
||||||
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||||
|
|
||||||
use blog_os::{exit_qemu, serial_println};
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
use blog_os::{exit_qemu, serial_println};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
static BREAKPOINT_HANDLER_CALLED: AtomicUsize = AtomicUsize::new(0);
|
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
init_test_idt();
|
blog_os::interrupts::init_idt();
|
||||||
|
|
||||||
// invoke a breakpoint exception
|
|
||||||
x86_64::instructions::int3();
|
x86_64::instructions::int3();
|
||||||
|
|
||||||
match BREAKPOINT_HANDLER_CALLED.load(Ordering::SeqCst) {
|
serial_println!("ok");
|
||||||
1 => serial_println!("ok"),
|
|
||||||
0 => {
|
|
||||||
serial_println!("failed");
|
|
||||||
serial_println!("Breakpoint handler was not called.");
|
|
||||||
}
|
|
||||||
other => {
|
|
||||||
serial_println!("failed");
|
|
||||||
serial_println!("Breakpoint handler was called {} times", other);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
exit_qemu();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
unsafe { exit_qemu(); }
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is called on panic.
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
serial_println!("failed");
|
serial_println!("failed");
|
||||||
|
|
||||||
serial_println!("{}", info);
|
serial_println!("{}", info);
|
||||||
|
|
||||||
unsafe {
|
unsafe { exit_qemu(); }
|
||||||
exit_qemu();
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable};
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref TEST_IDT: InterruptDescriptorTable = {
|
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
|
||||||
idt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_test_idt() {
|
|
||||||
TEST_IDT.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn breakpoint_handler(_stack_frame: &mut ExceptionStackFrame) {
|
|
||||||
BREAKPOINT_HANDLER_CALLED.fetch_add(1, Ordering::SeqCst);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user