Files
servo/components/devtools/actors/thread.rs
eri 151074e9a1 devtools: Handle pause in the debugger (#42007)
Add an event listener for `pause` to `debugger.js` and the necessary
glue to access it from the `devtools` crate. This returns important
information to know where we are paused, such as the source location and
frame state.

Fix frame and object actor encoding into messages. Use information from
`debugger.js` to correctly fill the fields.

Add a new `frames` list to the thread actor and handle the `frames`
message.

Fix `getEnvironment` reply in the frame actor. It is out of form (has a
`type` field but it doesn't require a followup empty message, it already
counts as a reply), so we need to handle it specially.

Note: For now we are focusing on the protocol side of the debugger, and
this patch only shows where the pause would happen. Pausing Servo itself
will happen in a followup.

![Debugger showing the line where execution would be
paused](https://github.com/user-attachments/assets/c007f205-0ccd-47f1-ad0b-81b7415e8211)

Testing: `mach test-devtools` and manual testing. No errors (apart from
#42006).
Part of: #36027

---------

Signed-off-by: eri <eri@igalia.com>
Co-authored-by: atbrakhi <atbrakhi@igalia.com>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
2026-01-19 19:27:52 +00:00

214 lines
6.5 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashSet;
use atomic_refcell::AtomicRefCell;
use base::generic_channel::{GenericSender, channel};
use devtools_traits::DevtoolScriptControlMsg;
use serde::Serialize;
use serde_json::{Map, Value};
use super::source::{SourceManager, SourcesReply};
use crate::actor::{Actor, ActorError, ActorRegistry};
use crate::actors::frame::{FrameActor, FrameActorMsg};
use crate::actors::pause::PauseActor;
use crate::protocol::{ClientRequest, JsonPacketStream};
use crate::{EmptyReplyMsg, StreamId};
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ThreadAttached {
from: String,
#[serde(rename = "type")]
type_: String,
actor: String,
frame: u32,
error: u32,
recording_endpoint: u32,
execution_point: u32,
popped_frames: Vec<PoppedFrameMsg>,
why: WhyMsg,
}
#[derive(Serialize)]
enum PoppedFrameMsg {}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct WhyMsg {
#[serde(rename = "type")]
type_: String,
#[serde(skip_serializing_if = "Option::is_none")]
on_next: Option<bool>,
}
#[derive(Serialize)]
struct ThreadResumedReply {
from: String,
#[serde(rename = "type")]
type_: String,
}
#[derive(Serialize)]
struct ThreadInterruptedReply {
from: String,
#[serde(rename = "type")]
type_: String,
actor: String,
frame: FrameActorMsg,
why: WhyMsg,
}
#[derive(Serialize)]
struct GetAvailableEventBreakpointsReply {
from: String,
value: Vec<()>,
}
#[derive(Serialize)]
struct FramesReply {
from: String,
frames: Vec<FrameActorMsg>,
}
pub(crate) struct ThreadActor {
name: String,
pub source_manager: SourceManager,
script_sender: GenericSender<DevtoolScriptControlMsg>,
frames: AtomicRefCell<HashSet<String>>,
}
impl ThreadActor {
pub fn new(name: String, script_sender: GenericSender<DevtoolScriptControlMsg>) -> ThreadActor {
ThreadActor {
name: name.clone(),
source_manager: SourceManager::new(),
script_sender,
frames: Default::default(),
}
}
}
impl Actor for ThreadActor {
fn name(&self) -> String {
self.name.clone()
}
fn handle_message(
&self,
mut request: ClientRequest,
registry: &ActorRegistry,
msg_type: &str,
_msg: &Map<String, Value>,
_id: StreamId,
) -> Result<(), ActorError> {
match msg_type {
"attach" => {
let pause = registry.new_name::<PauseActor>();
registry.register(PauseActor {
name: pause.clone(),
});
let msg = ThreadAttached {
from: self.name(),
type_: "paused".to_owned(),
actor: pause,
frame: 0,
error: 0,
recording_endpoint: 0,
execution_point: 0,
popped_frames: vec![],
why: WhyMsg {
type_: "attached".to_owned(),
on_next: None,
},
};
request.write_json_packet(&msg)?;
request.reply_final(&EmptyReplyMsg { from: self.name() })?
},
"resume" => {
let msg = ThreadResumedReply {
from: self.name(),
type_: "resumed".to_owned(),
};
request.write_json_packet(&msg)?;
request.reply_final(&EmptyReplyMsg { from: self.name() })?
},
"interrupt" => {
let (tx, rx) = channel().ok_or(ActorError::Internal)?;
self.script_sender
.send(DevtoolScriptControlMsg::Pause(tx))
.map_err(|_| ActorError::Internal)?;
let result = rx.recv().map_err(|_| ActorError::Internal)?;
let pause = registry.new_name::<PauseActor>();
registry.register(PauseActor {
name: pause.clone(),
});
let source = self
.source_manager
.find_source(registry, &result.url)
.ok_or(ActorError::Internal)?;
let frame = FrameActor::register(registry, source.name(), result);
self.frames.borrow_mut().insert(frame.clone());
let msg = ThreadInterruptedReply {
from: self.name(),
type_: "paused".to_owned(),
actor: pause,
frame: registry.encode::<FrameActor, _>(&frame),
// TODO: Read the msg for on_next
why: WhyMsg {
type_: "interrupted".into(),
on_next: Some(true),
},
};
request.write_json_packet(&msg)?;
request.reply_final(&EmptyReplyMsg { from: self.name() })?
},
"reconfigure" => request.reply_final(&EmptyReplyMsg { from: self.name() })?,
"getAvailableEventBreakpoints" => {
// TODO: Send list of available event breakpoints (animation, clipboard, load...)
let msg = GetAvailableEventBreakpointsReply {
from: self.name(),
value: vec![],
};
request.reply_final(&msg)?
},
// Client has attached to the thread and wants to load script sources.
// <https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#loading-script-sources>
"sources" => {
let msg = SourcesReply {
from: self.name(),
sources: self.source_manager.source_forms(registry),
};
request.reply_final(&msg)?
},
"frames" => {
let msg = FramesReply {
from: self.name(),
frames: self
.frames
.borrow()
.iter()
.map(|frame| registry.encode::<FrameActor, _>(frame))
.collect(),
};
request.reply_final(&msg)?
},
_ => return Err(ActorError::UnrecognizedPacketType),
};
Ok(())
}
}