implements PLN-0154
This commit is contained in:
@@ -28,6 +28,8 @@ pub struct HostDebugger {
|
||||
last_telemetry_frame: u64,
|
||||
/// Last fault summary sent to the debugger client.
|
||||
last_fault_summary: Option<String>,
|
||||
/// Debug cartridge metadata captured before the VM session exists.
|
||||
debug_cartridge: Option<HandshakeCartridge>,
|
||||
}
|
||||
|
||||
impl Default for HostDebugger {
|
||||
@@ -46,19 +48,25 @@ impl HostDebugger {
|
||||
last_log_seq: 0,
|
||||
last_telemetry_frame: 0,
|
||||
last_fault_summary: None,
|
||||
debug_cartridge: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Configures the debugger based on the boot target.
|
||||
/// If debug mode is enabled, it binds to the specified TCP port.
|
||||
pub fn setup_boot_target(&mut self, boot_target: &BootTarget, firmware: &mut Firmware) {
|
||||
pub fn setup_boot_target(&mut self, boot_target: &BootTarget, _firmware: &mut Firmware) {
|
||||
if let BootTarget::Cartridge { path, debug: true, debug_port } = boot_target {
|
||||
self.waiting_for_start = true;
|
||||
|
||||
// Pre-load cartridge metadata so the Handshake message can contain
|
||||
// valid information about the App being debugged.
|
||||
// valid information before the VM session is created.
|
||||
if let Ok(cartridge) = CartridgeLoader::load(path) {
|
||||
let _ = firmware.os.vm().initialize(&mut firmware.vm, &cartridge);
|
||||
self.debug_cartridge = Some(HandshakeCartridge {
|
||||
app_id: cartridge.app_id,
|
||||
title: cartridge.title,
|
||||
app_version: cartridge.app_version,
|
||||
app_mode: cartridge.app_mode,
|
||||
});
|
||||
}
|
||||
|
||||
match TcpListener::bind(format!("127.0.0.1:{}", debug_port)) {
|
||||
@@ -116,12 +124,7 @@ impl HostDebugger {
|
||||
let handshake = DebugResponse::Handshake {
|
||||
protocol_version: DEVTOOLS_PROTOCOL_VERSION,
|
||||
runtime_version: "0.1".to_string(),
|
||||
cartridge: HandshakeCartridge {
|
||||
app_id: firmware.os.vm().current_app_id(),
|
||||
title: firmware.os.vm().current_cartridge_title(),
|
||||
app_version: firmware.os.vm().current_cartridge_app_version(),
|
||||
app_mode: firmware.os.vm().current_cartridge_app_mode(),
|
||||
},
|
||||
cartridge: self.handshake_cartridge(firmware),
|
||||
};
|
||||
self.send_response(handshake);
|
||||
} else {
|
||||
@@ -201,7 +204,7 @@ impl HostDebugger {
|
||||
DebugCommand::Step => {
|
||||
// Execute exactly one instruction and keep paused.
|
||||
firmware.os.vm().set_paused(true);
|
||||
let _ = firmware.os.vm().debug_step_instruction(&mut firmware.vm, platform);
|
||||
let _ = firmware.os.vm().debug_step_active_session(platform);
|
||||
}
|
||||
DebugCommand::StepFrame => {
|
||||
// Execute until the end of the current logical frame.
|
||||
@@ -210,10 +213,10 @@ impl HostDebugger {
|
||||
}
|
||||
DebugCommand::GetState => {
|
||||
// Return detailed VM register and stack state.
|
||||
let stack_top = firmware.vm.operand_stack_top(10);
|
||||
let stack_top = firmware.os.vm().active_operand_stack_top(10);
|
||||
|
||||
let resp = DebugResponse::GetState {
|
||||
pc: firmware.vm.pc(),
|
||||
pc: firmware.os.vm().active_pc().unwrap_or(0),
|
||||
stack_top,
|
||||
frame_index: firmware.os.vm().logical_frame_index(),
|
||||
app_id: firmware.os.vm().current_app_id(),
|
||||
@@ -221,18 +224,37 @@ impl HostDebugger {
|
||||
self.send_response(resp);
|
||||
}
|
||||
DebugCommand::SetBreakpoint { pc } => {
|
||||
firmware.vm.insert_breakpoint(pc);
|
||||
let _ = firmware.os.vm().insert_active_breakpoint(pc);
|
||||
}
|
||||
DebugCommand::ListBreakpoints => {
|
||||
let pcs = firmware.vm.breakpoints_list();
|
||||
let pcs = firmware.os.vm().active_breakpoints_list();
|
||||
self.send_response(DebugResponse::Breakpoints { pcs });
|
||||
}
|
||||
DebugCommand::ClearBreakpoint { pc } => {
|
||||
firmware.vm.remove_breakpoint(pc);
|
||||
let _ = firmware.os.vm().remove_active_breakpoint(pc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handshake_cartridge(&self, firmware: &mut Firmware) -> HandshakeCartridge {
|
||||
let app_id = firmware.os.vm().current_app_id();
|
||||
if app_id != 0 {
|
||||
return HandshakeCartridge {
|
||||
app_id,
|
||||
title: firmware.os.vm().current_cartridge_title(),
|
||||
app_version: firmware.os.vm().current_cartridge_app_version(),
|
||||
app_mode: firmware.os.vm().current_cartridge_app_mode(),
|
||||
};
|
||||
}
|
||||
|
||||
self.debug_cartridge.clone().unwrap_or_else(|| HandshakeCartridge {
|
||||
app_id,
|
||||
title: firmware.os.vm().current_cartridge_title(),
|
||||
app_version: firmware.os.vm().current_cartridge_app_version(),
|
||||
app_mode: firmware.os.vm().current_cartridge_app_mode(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn cert_event_from_snapshot(
|
||||
tag: u16,
|
||||
telemetry: TelemetryFrame,
|
||||
@@ -315,7 +337,7 @@ impl HostDebugger {
|
||||
// Map specific internal log tags to protocol events.
|
||||
if event.tag == 0xDEB1 {
|
||||
self.send_event(DebugEvent::BreakpointHit {
|
||||
pc: firmware.vm.pc(),
|
||||
pc: firmware.os.vm().active_pc().unwrap_or(0),
|
||||
frame_index: firmware.os.vm().logical_frame_index(),
|
||||
});
|
||||
}
|
||||
@@ -409,6 +431,51 @@ mod tests {
|
||||
assert!(firmware.os.vm().debug_step_requested());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn breakpoint_commands_mutate_active_vm_session_not_legacy_vm() {
|
||||
let mut debugger = HostDebugger::new();
|
||||
let mut firmware = Firmware::new(None);
|
||||
let mut platform = TestPlatform::new();
|
||||
let task_id = firmware.os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
firmware.vm.insert_breakpoint(9);
|
||||
|
||||
debugger.handle_command(
|
||||
DebugCommand::SetBreakpoint { pc: 123 },
|
||||
&mut firmware,
|
||||
&mut platform,
|
||||
);
|
||||
|
||||
assert_eq!(firmware.vm.breakpoints_list(), vec![9]);
|
||||
assert_eq!(
|
||||
firmware
|
||||
.os
|
||||
.sessions()
|
||||
.vm_session_for_task(task_id)
|
||||
.expect("game VM session should exist")
|
||||
.vm
|
||||
.breakpoints_list(),
|
||||
vec![123]
|
||||
);
|
||||
|
||||
debugger.handle_command(
|
||||
DebugCommand::ClearBreakpoint { pc: 123 },
|
||||
&mut firmware,
|
||||
&mut platform,
|
||||
);
|
||||
|
||||
assert_eq!(firmware.vm.breakpoints_list(), vec![9]);
|
||||
assert!(
|
||||
firmware
|
||||
.os
|
||||
.sessions()
|
||||
.vm_session_for_task(task_id)
|
||||
.expect("game VM session should exist")
|
||||
.vm
|
||||
.breakpoints_list()
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handle_command_start_leaves_waiting_for_start_mode() {
|
||||
let mut debugger = HostDebugger::new();
|
||||
|
||||
Reference in New Issue
Block a user