-
-
Notifications
You must be signed in to change notification settings - Fork 64
Add support for Wasm-specific commands. #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac46530
Add support for Wasm-specific commands.
cfallin 2da4988
Review feedback.
cfallin 40937cc
Review feedback.
cfallin 8fbc6e9
Update src/target/mod.rs
cfallin eb4f543
Update src/target/ext/wasm.rs
cfallin 168f64b
Update src/target/ext/wasm.rs
cfallin 15dd0ec
Update src/target/ext/wasm.rs
cfallin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use super::prelude::*; | ||
| use crate::protocol::common::thread_id::ThreadId; | ||
| use crate::protocol::ConcreteThreadId; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct qWasmCallStack { | ||
| pub tid: ConcreteThreadId, | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for qWasmCallStack { | ||
| #[inline(always)] | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let body = buf.into_body(); | ||
| if body.is_empty() || body[0] != b':' { | ||
| return None; | ||
| } | ||
| let tid = &body[1..]; | ||
| let tid = ConcreteThreadId::try_from(ThreadId::try_from(tid).ok()?).ok()?; | ||
| Some(qWasmCallStack { tid }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct qWasmGlobal<'a> { | ||
| pub frame: usize, | ||
| pub global: usize, | ||
| pub buf: &'a mut [u8], | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for qWasmGlobal<'a> { | ||
| #[inline(always)] | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let (buf, body_range) = buf.into_raw_buf(); | ||
| let body = buf.get(body_range.start..body_range.end)?; | ||
|
|
||
| if body.is_empty() || body[0] != b':' { | ||
| return None; | ||
| } | ||
| let mut parts = body[1..].split(|b| *b == b';'); | ||
| let frame = parts.next()?; | ||
| let frame = str::from_utf8(frame).ok()?.parse::<usize>().ok()?; | ||
| let global = parts.next()?; | ||
| let global = str::from_utf8(global).ok()?.parse::<usize>().ok()?; | ||
| if parts.next().is_some() { | ||
| // Too many parameters. | ||
| return None; | ||
| } | ||
|
|
||
| Some(qWasmGlobal { | ||
| frame, | ||
| global, | ||
| buf, | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct qWasmLocal<'a> { | ||
| pub frame: usize, | ||
| pub local: usize, | ||
| pub buf: &'a mut [u8], | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for qWasmLocal<'a> { | ||
| #[inline(always)] | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let (buf, body_range) = buf.into_raw_buf(); | ||
| let body = buf.get(body_range.start..body_range.end)?; | ||
|
|
||
| if body.is_empty() || body[0] != b':' { | ||
| return None; | ||
| } | ||
| let mut parts = body[1..].split(|b| *b == b';'); | ||
| let frame = parts.next()?; | ||
| let frame = str::from_utf8(frame).ok()?.parse::<usize>().ok()?; | ||
| let local = parts.next()?; | ||
| let local = str::from_utf8(local).ok()?.parse::<usize>().ok()?; | ||
| if parts.next().is_some() { | ||
| // Too many parameters. | ||
| return None; | ||
| } | ||
|
|
||
| Some(qWasmLocal { | ||
| frame, | ||
| local, | ||
| buf, | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct qWasmStackValue<'a> { | ||
| pub frame: usize, | ||
| pub index: usize, | ||
| pub buf: &'a mut [u8], | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for qWasmStackValue<'a> { | ||
| #[inline(always)] | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let (buf, body_range) = buf.into_raw_buf(); | ||
| let body = buf.get(body_range.start..body_range.end)?; | ||
|
|
||
| if body.is_empty() || body[0] != b':' { | ||
| return None; | ||
| } | ||
| let mut parts = body[1..].split(|b| *b == b';'); | ||
| let frame = parts.next()?; | ||
| let frame = str::from_utf8(frame).ok()?.parse::<usize>().ok()?; | ||
| let index = parts.next()?; | ||
| let index = str::from_utf8(index).ok()?.parse::<usize>().ok()?; | ||
| if parts.next().is_some() { | ||
| // Too many parameters. | ||
| return None; | ||
| } | ||
|
|
||
| Some(qWasmStackValue { | ||
| frame, | ||
| index, | ||
| buf, | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| use super::prelude::*; | ||
| use crate::protocol::commands::ext::Wasm; | ||
|
|
||
| impl<T: Target, C: Connection> GdbStubImpl<T, C> { | ||
| pub(crate) fn handle_wasm( | ||
| &mut self, | ||
| res: &mut ResponseWriter<'_, C>, | ||
| target: &mut T, | ||
| command: Wasm<'_>, | ||
| ) -> Result<HandlerStatus, Error<T::Error, C::Error>> { | ||
| let ops = match target.support_wasm() { | ||
| Some(ops) => ops, | ||
| None => return Ok(HandlerStatus::Handled), | ||
| }; | ||
|
|
||
| crate::__dead_code_marker!("wasm", "impl"); | ||
|
|
||
| match command { | ||
| Wasm::qWasmCallStack(cmd) => { | ||
| let mut error: Result<(), Error<T::Error, C::Error>> = Ok(()); | ||
| ops.wasm_call_stack(cmd.tid.tid, &mut |pc| { | ||
| if let Err(e) = res.write_hex_buf(&pc.to_le_bytes()) { | ||
| error = Err(e.into()); | ||
| } | ||
| }) | ||
| .map_err(Error::TargetError)?; | ||
| error?; | ||
| } | ||
| Wasm::qWasmLocal(cmd) => { | ||
| let len = ops | ||
| .read_wasm_local(self.current_mem_tid, cmd.frame, cmd.local, cmd.buf) | ||
| .map_err(Error::TargetError)?; | ||
| res.write_hex_buf(&cmd.buf[0..len])?; | ||
| } | ||
| Wasm::qWasmGlobal(cmd) => { | ||
| let len = ops | ||
| .read_wasm_global(self.current_mem_tid, cmd.frame, cmd.global, cmd.buf) | ||
| .map_err(Error::TargetError)?; | ||
| res.write_hex_buf(&cmd.buf[0..len])?; | ||
| } | ||
| Wasm::qWasmStackValue(cmd) => { | ||
| let len = ops | ||
| .read_wasm_stack(self.current_mem_tid, cmd.frame, cmd.index, cmd.buf) | ||
| .map_err(Error::TargetError)?; | ||
| res.write_hex_buf(&cmd.buf[0..len])?; | ||
| } | ||
| }; | ||
|
|
||
| Ok(HandlerStatus::Handled) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| //! Provide Wasm-specific actions for the target. | ||
| //! | ||
| //! ### Address Encoding | ||
| //! | ||
| //! The gdbstub extension to the Wasm target architecture uses a | ||
cfallin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! specific encoding for addresses, both for commands in this | ||
| //! extension trait and for commands in the base protocol (e.g., for | ||
| //! reading and writing memory and setting breakpoints). The need for | ||
| //! this scheme arises from the fact that Wasm is natively | ||
| //! "multimemory": there can be many code modules, and many linear | ||
| //! memories, and each is a native entity (rather than mapped into a | ||
| //! larger single address space) in the VM definition. The gdbstub | ||
| //! protocol extensions map these native entities into an address | ||
| //! space where the upper 32 bits encode the index of a particular | ||
| //! Wasm code module or linear (data) memory and the lower 32 bits | ||
| //! encode an offset. | ||
| //! | ||
| //! See the [LLDB source code] (particularly `WasmAddressType` and | ||
| //! `wasm_addr_t`) for a description of the encoding of the PC values. | ||
| //! | ||
| //! [LLDB souce code]: https://github.com/llvm/llvm-project/blob/main/lldb/source/Plugins/Process/wasm/ProcessWasm.h | ||
| use crate::common::Tid; | ||
| use crate::target::Target; | ||
|
|
||
| /// Target Extension - perform Wasm-specific actions. | ||
cfallin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub trait Wasm: Target { | ||
cfallin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Get the Wasm call stack for a given thread. | ||
| /// | ||
| /// The addresses provided for the PC at each frame shouuld be | ||
| /// encoded as per the [Wasm address encoding]. | ||
| /// | ||
| /// To avoid allocation, the call stack PCs should be returned to | ||
| /// the caller by calling the given callback, in order from | ||
| /// innermost (most recently called) frame to outermost. | ||
| /// | ||
| /// [Wasm address encoding]: `self#Address_Encoding` | ||
| fn wasm_call_stack(&self, tid: Tid, next_pc: &mut dyn FnMut(u64)) -> Result<(), Self::Error>; | ||
cfallin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Get the Wasm local for a given thread, frame index, and local | ||
| /// index. | ||
| /// | ||
| /// The Wasm local's value should be placed into `buf`, and the | ||
| /// length should be returned. If the Wasm local or frame does not | ||
| /// exist, this method should return `0`. | ||
| /// | ||
| /// `buf` will be long enough to allow for the larget possible | ||
| /// supported Wasm value (i.e., at least a `v128` SIMD | ||
| /// value). Values should be encoded in little-endian format with | ||
| /// their native length (e.g., 4 bytes for a Wasm `i32` or `f32` | ||
| /// type, or 8 bytes for a Wasm `i64` or `f64` type). | ||
| fn read_wasm_local( | ||
| &self, | ||
| tid: Tid, | ||
| frame: usize, | ||
| local: usize, | ||
| buf: &mut [u8], | ||
| ) -> Result<usize, Self::Error>; | ||
|
|
||
| /// Get the Wasm operand-stack value for a given thread, frame | ||
| /// index, and stack index. Top-of-stack is index 0, and values | ||
| /// below that have incrementing indices. | ||
| /// | ||
| /// The Wasm operand's value should be placed into `buf`, and the | ||
| /// length should be returned. If the Wasm local or frame does not | ||
| /// exist, this method should return `0`. | ||
| /// | ||
| /// `buf` will be long enough to allow for the larget possible | ||
| /// supported Wasm value (i.e., at least a `v128` SIMD | ||
| /// value). Values should be encoded in little-endian format with | ||
| /// their native length (e.g., 4 bytes for a Wasm `i32` or `f32` | ||
| /// type, or 8 bytes for a Wasm `i64` or `f64` type). | ||
| fn read_wasm_stack( | ||
| &self, | ||
| tid: Tid, | ||
| frame: usize, | ||
| index: usize, | ||
| buf: &mut [u8], | ||
| ) -> Result<usize, Self::Error>; | ||
|
|
||
| /// Get the Wasm global value for a given thread, frame, and | ||
| /// global index. The global index is relative to the module whose | ||
| /// function corresponds to that frame. | ||
| /// | ||
| /// The Wasm global's value should be placed into `buf`, and the | ||
| /// length should be returned. If the Wasm local or frame does not | ||
| /// exist, this method should return `0`. | ||
| /// | ||
| /// `buf` will be long enough to allow for the larget possible | ||
| /// supported Wasm value (i.e., at least a `v128` SIMD | ||
| /// value). Values should be encoded in little-endian format with | ||
| /// their native length (e.g., 4 bytes for a Wasm `i32` or `f32` | ||
| /// type, or 8 bytes for a Wasm `i64` or `f64` type). | ||
| fn read_wasm_global( | ||
| &self, | ||
| tid: Tid, | ||
| frame: usize, | ||
| global: usize, | ||
| buf: &mut [u8], | ||
| ) -> Result<usize, Self::Error>; | ||
| } | ||
|
|
||
| define_ext!(WasmOps, Wasm); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.