-
-
Notifications
You must be signed in to change notification settings - Fork 64
Support Host I/O operations #66
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 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4a814af
Support Host I/O operations
bet4it 6823d00
Fix argument types
bet4it 774e780
Add missing Host I/O operations
bet4it 1e750db
Some fixes
bet4it 2465d6d
Change return type to HostIoResult
bet4it 41751fe
Improve handle_hostio_result macro
bet4it dc1f56e
Implement real filesystem access in example
bet4it 72b9ce2
Supply example with a complete real filesystem access implementation
bet4it 85a6acc
Store files in Vec
bet4it e20d15d
Simplify code
bet4it 7c03939
Allow non-ASCII characters in packet
bet4it 641eb0c
Optimize away the bounds checks in decode_bin_buf
bet4it 2ba3129
Fix style
bet4it 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
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,70 @@ | ||
| use gdbstub::target; | ||
|
|
||
| use crate::emu::Emu; | ||
|
|
||
| use gdbstub::target::ext::host_io::{ | ||
| HostIoErrno, HostIoError, HostIoMode, HostIoOpenFlags, HostIoResult, PreadOutput, PreadToken, | ||
| }; | ||
|
|
||
| impl target::ext::host_io::HostIo for Emu { | ||
| #[inline(always)] | ||
| fn enable_open(&mut self) -> Option<target::ext::host_io::HostIoOpenOps<Self>> { | ||
| Some(self) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn enable_pread(&mut self) -> Option<target::ext::host_io::HostIoPreadOps<Self>> { | ||
| Some(self) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn enable_close(&mut self) -> Option<target::ext::host_io::HostIoCloseOps<Self>> { | ||
| Some(self) | ||
| } | ||
| } | ||
|
|
||
| impl target::ext::host_io::HostIoOpen for Emu { | ||
| fn open( | ||
| &mut self, | ||
| filename: &[u8], | ||
| _flags: HostIoOpenFlags, | ||
| _mode: HostIoMode, | ||
| ) -> HostIoResult<u32, Self> { | ||
| // Support `info proc mappings` command | ||
| if filename == b"/proc/1/maps" { | ||
bet4it marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Ok(1) | ||
| } else { | ||
| Err(HostIoError::Errno(HostIoErrno::EPERM)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl target::ext::host_io::HostIoPread for Emu { | ||
| fn pread<'a>( | ||
| &mut self, | ||
| fd: i32, | ||
| count: u32, | ||
| offset: u32, | ||
| output: PreadOutput<'a>, | ||
| ) -> HostIoResult<PreadToken<'a>, Self> { | ||
| if fd == 1 { | ||
| let maps = b"0x55550000-0x55550078 r-x 0 0 0\n"; | ||
| let len = maps.len(); | ||
| let count: usize = count as usize; | ||
| let offset: usize = offset as usize; | ||
| Ok(output.write(&maps[offset.min(len)..(offset + count).min(len)])) | ||
| } else { | ||
| Err(HostIoError::Errno(HostIoErrno::EPERM)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl target::ext::host_io::HostIoClose for Emu { | ||
| fn close(&mut self, fd: i32) -> HostIoResult<u32, Self> { | ||
| if fd == 1 { | ||
| Ok(0) | ||
| } else { | ||
| Err(HostIoError::Errno(HostIoErrno::EPERM)) | ||
| } | ||
bet4it marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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,158 @@ | ||
| use super::prelude::*; | ||
| use crate::arch::Arch; | ||
| use crate::protocol::commands::ext::HostIo; | ||
| use crate::target::ext::host_io::{HostIoError, HostStat, PreadOutput}; | ||
| use crate::GdbStubError; | ||
bet4it marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| macro_rules! handle_hostio_result { | ||
| ( $ret:ident, $res:ident, $callback:expr) => {{ | ||
| match $ret { | ||
| Ok(fd) => $callback(fd)?, | ||
| Err(HostIoError::Errno(errno)) => { | ||
| $res.write_str("F-1,")?; | ||
| $res.write_num(errno as i32)?; | ||
| } | ||
| Err(HostIoError::Fatal(e)) => return Err(GdbStubError::TargetError(e)), | ||
| } | ||
| }}; | ||
| } | ||
bet4it marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| impl<T: Target, C: Connection> GdbStubImpl<T, C> { | ||
| pub(crate) fn handle_host_io( | ||
| &mut self, | ||
| res: &mut ResponseWriter<C>, | ||
| target: &mut T, | ||
| command: HostIo, | ||
| ) -> Result<HandlerStatus, Error<T::Error, C::Error>> { | ||
| let ops = match target.host_io() { | ||
| Some(ops) => ops, | ||
| None => return Ok(HandlerStatus::Handled), | ||
| }; | ||
|
|
||
| crate::__dead_code_marker!("host_io", "impl"); | ||
|
|
||
| let handler_status = match command { | ||
| HostIo::vFileOpen(cmd) if ops.enable_open().is_some() => { | ||
| let ops = ops.enable_open().unwrap(); | ||
| let result = ops.open(cmd.filename, cmd.flags, cmd.mode); | ||
| handle_hostio_result!(result, res, |fd| -> Result<_, Error<T::Error, C::Error>> { | ||
| res.write_str("F")?; | ||
| res.write_num(fd)?; | ||
| Ok(()) | ||
| }); | ||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFileClose(cmd) if ops.enable_close().is_some() => { | ||
| let ops = ops.enable_close().unwrap(); | ||
| let result = ops.close(cmd.fd); | ||
| handle_hostio_result!(result, res, |ret| -> Result<_, Error<T::Error, C::Error>> { | ||
| res.write_str("F")?; | ||
| res.write_num(ret)?; | ||
| Ok(()) | ||
| }); | ||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFilePread(cmd) if ops.enable_pread().is_some() => { | ||
| let count = <T::Arch as Arch>::Usize::from_be_bytes(cmd.count) | ||
| .ok_or(Error::TargetMismatch)?; | ||
| let offset = <T::Arch as Arch>::Usize::from_be_bytes(cmd.offset) | ||
| .ok_or(Error::TargetMismatch)?; | ||
| let mut err: Result<_, Error<T::Error, C::Error>> = Ok(()); | ||
| let mut callback = |data: &[u8]| { | ||
| let e = (|| { | ||
| res.write_str("F")?; | ||
| res.write_num(data.len())?; | ||
bet4it marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| res.write_str(";")?; | ||
| res.write_binary(data)?; | ||
| Ok(()) | ||
| })(); | ||
|
|
||
| if let Err(e) = e { | ||
| err = Err(e) | ||
| } | ||
| }; | ||
|
|
||
| let ops = ops.enable_pread().unwrap(); | ||
| let result = ops.pread(cmd.fd, count, offset, PreadOutput::new(&mut callback)); | ||
| handle_hostio_result!(result, res, |_| -> Result<_, Error<T::Error, C::Error>> { | ||
| Ok(()) | ||
| }); | ||
| err?; | ||
|
|
||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFilePwrite(cmd) if ops.enable_pwrite().is_some() => { | ||
| let offset = <T::Arch as Arch>::Usize::from_be_bytes(cmd.offset) | ||
| .ok_or(Error::TargetMismatch)?; | ||
| let ops = ops.enable_pwrite().unwrap(); | ||
| let result = ops.pwrite(cmd.fd, offset, cmd.data); | ||
| handle_hostio_result!(result, res, |ret| -> Result<_, Error<T::Error, C::Error>> { | ||
| res.write_str("F")?; | ||
| res.write_num(ret)?; | ||
| Ok(()) | ||
| }); | ||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFileFstat(cmd) if ops.enable_fstat().is_some() => { | ||
| let ops = ops.enable_fstat().unwrap(); | ||
| let result = ops.fstat(cmd.fd); | ||
| handle_hostio_result!( | ||
| result, | ||
| res, | ||
| |stat: HostStat| -> Result<_, Error<T::Error, C::Error>> { | ||
| let size = core::mem::size_of::<HostStat>(); | ||
| res.write_str("F")?; | ||
| res.write_num(size)?; | ||
| res.write_str(";")?; | ||
| res.write_binary(&stat.st_dev.to_le_bytes())?; | ||
| res.write_binary(&stat.st_ino.to_le_bytes())?; | ||
| res.write_binary(&(stat.st_mode.bits()).to_le_bytes())?; | ||
| res.write_binary(&stat.st_nlink.to_le_bytes())?; | ||
| res.write_binary(&stat.st_uid.to_le_bytes())?; | ||
| res.write_binary(&stat.st_gid.to_le_bytes())?; | ||
| res.write_binary(&stat.st_rdev.to_le_bytes())?; | ||
| res.write_binary(&stat.st_size.to_le_bytes())?; | ||
| res.write_binary(&stat.st_blksize.to_le_bytes())?; | ||
| res.write_binary(&stat.st_blocks.to_le_bytes())?; | ||
| res.write_binary(&stat.st_atime.to_le_bytes())?; | ||
| res.write_binary(&stat.st_mtime.to_le_bytes())?; | ||
| res.write_binary(&stat.st_ctime.to_le_bytes())?; | ||
bet4it marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Ok(()) | ||
| } | ||
| ); | ||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFileUnlink(cmd) if ops.enable_unlink().is_some() => { | ||
| let ops = ops.enable_unlink().unwrap(); | ||
| let result = ops.unlink(cmd.filename); | ||
| handle_hostio_result!(result, res, |ret| -> Result<_, Error<T::Error, C::Error>> { | ||
| res.write_str("F")?; | ||
| res.write_num(ret)?; | ||
| Ok(()) | ||
| }); | ||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFileReadlink(cmd) if ops.enable_readlink().is_some() => { | ||
| let ops = ops.enable_readlink().unwrap(); | ||
| let result = ops.readlink(cmd.filename); | ||
| handle_hostio_result!(result, res, |ret| -> Result<_, Error<T::Error, C::Error>> { | ||
| res.write_str("F")?; | ||
| res.write_num(ret)?; | ||
| Ok(()) | ||
| }); | ||
| HandlerStatus::Handled | ||
| } | ||
| HostIo::vFileSetfs(cmd) if ops.enable_setfs().is_some() => { | ||
| let ops = ops.enable_setfs().unwrap(); | ||
| let result = ops.setfs(cmd.fs); | ||
| handle_hostio_result!(result, res, |_| -> Result<_, Error<T::Error, C::Error>> { | ||
| Ok(()) | ||
bet4it marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| HandlerStatus::Handled | ||
| } | ||
| _ => HandlerStatus::Handled, | ||
| }; | ||
|
|
||
| Ok(handler_status) | ||
| } | ||
| } | ||
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
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,23 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct vFileClose { | ||
| pub fd: i32, | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for vFileClose { | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let body = buf.into_body(); | ||
| if body.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| match body { | ||
| [b':', body @ ..] => { | ||
| let fd = decode_hex(body).ok()?; | ||
| Some(vFileClose{fd}) | ||
| }, | ||
| _ => None, | ||
| } | ||
| } | ||
| } |
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,24 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct vFileFstat { | ||
| pub fd: i32, | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for vFileFstat { | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let body = buf.into_body(); | ||
| if body.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| match body { | ||
| [b':', body @ ..] => { | ||
| let mut body = body.splitn_mut_no_panic(3, |b| *b == b','); | ||
| let fd = decode_hex(body.next()?).ok()?; | ||
| Some(vFileFstat{fd}) | ||
| }, | ||
| _ => None, | ||
| } | ||
| } | ||
| } |
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,30 @@ | ||
| use super::prelude::*; | ||
|
|
||
| use crate::target::ext::host_io::{HostIoOpenFlags, HostIoMode}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct vFileOpen<'a> { | ||
| pub filename: &'a [u8], | ||
| pub flags: HostIoOpenFlags, | ||
| pub mode: HostIoMode, | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for vFileOpen<'a> { | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let body = buf.into_body(); | ||
| if body.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| match body { | ||
| [b':', body @ ..] => { | ||
| let mut body = body.splitn_mut_no_panic(3, |b| *b == b','); | ||
| let filename = decode_hex_buf(body.next()?).ok()?; | ||
| let flags = HostIoOpenFlags::from_bits(decode_hex(body.next()?).ok()?).unwrap(); | ||
| let mode = HostIoMode::from_bits(decode_hex(body.next()?).ok()?).unwrap(); | ||
| Some(vFileOpen{filename, flags, mode}) | ||
| }, | ||
| _ => None, | ||
| } | ||
| } | ||
| } |
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.