-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: add many safe wrapper functions #39
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d93ca79
feat: add many safe wrapper functions
anatawa12 87d7afc
fix: missing pub
anatawa12 19ed04e
docs: fix sentences
anatawa12 c43ae51
format: rustfmt
anatawa12 149f8de
feat: add ImageToken
anatawa12 77005e5
feat: allow AsRef<Path> for SBFileSpec
anatawa12 2eb1d9d
make ImageToken public
anatawa12 2abccb6
remove ModuleSymbols struct
anatawa12 4d51f87
fix error
anatawa12 c13a29f
fix clippy
anatawa12 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
|
||
use crate::{ | ||
lldb_addr_t, lldb_pid_t, lldb_tid_t, sys, Permissions, SBBroadcaster, SBError, SBEvent, | ||
SBMemoryRegionInfo, SBMemoryRegionInfoList, SBProcessInfo, SBQueue, SBStream, SBStructuredData, | ||
SBThread, StateType, | ||
SBFileSpec, SBMemoryRegionInfo, SBMemoryRegionInfoList, SBProcessInfo, SBQueue, SBStream, | ||
SBStructuredData, SBTarget, SBThread, StateType, | ||
}; | ||
use std::ffi::{CStr, CString}; | ||
use std::fmt; | ||
|
@@ -559,6 +559,85 @@ impl SBProcess { | |
pub fn get_memory_regions(&self) -> SBMemoryRegionInfoList { | ||
SBMemoryRegionInfoList::wrap(unsafe { sys::SBProcessGetMemoryRegions(self.raw) }) | ||
} | ||
|
||
/// Reads the memory at specified address in the process to the `buffer` | ||
pub fn read_memory(&self, addr: lldb_addr_t, buffer: &mut [u8]) -> Result<(), SBError> { | ||
// SBProcessReadMemory will return an error if the memory region is not allowed to read | ||
// and does not cause bad behavior so this method can be safe. | ||
let error = SBError::default(); | ||
unsafe { | ||
sys::SBProcessReadMemory( | ||
self.raw, | ||
addr, | ||
buffer.as_mut_ptr() as *mut _, | ||
buffer.len(), | ||
error.raw, | ||
); | ||
} | ||
if error.is_success() { | ||
Ok(()) | ||
} else { | ||
Err(error) | ||
} | ||
} | ||
|
||
/// Writes the `buffer` data to the memory at specified address in the process | ||
pub fn write_memory(&self, addr: lldb_addr_t, buffer: &[u8]) -> Result<(), SBError> { | ||
let error = SBError::default(); | ||
unsafe { | ||
sys::SBProcessWriteMemory( | ||
self.raw, | ||
addr, | ||
buffer.as_ptr() as *mut _, | ||
buffer.len(), | ||
error.raw, | ||
); | ||
} | ||
if error.is_success() { | ||
Ok(()) | ||
} else { | ||
Err(error) | ||
} | ||
} | ||
|
||
/// Returns the byte order of target process | ||
pub fn byte_order(&self) -> crate::ByteOrder { | ||
unsafe { sys::SBProcessGetByteOrder(self.raw) } | ||
} | ||
|
||
/// Loads the specified image into the process. | ||
pub fn load_image(&self, file: &SBFileSpec) -> Result<ImageToken, SBError> { | ||
let error = SBError::default(); | ||
let image_token = unsafe { sys::SBProcessLoadImage(self.raw, file.raw, error.raw) }; | ||
if error.is_failure() { | ||
Err(error) | ||
} else { | ||
Ok(ImageToken(image_token)) | ||
} | ||
} | ||
|
||
/// Unloads the image loaded with [`load_image`]. | ||
/// | ||
/// [`load_image`]: Self::load_image | ||
pub fn unload_image(&self, image_token: ImageToken) -> Result<(), SBError> { | ||
// the method returns error if image_token is not valid, instead of causing undefined behavior. | ||
let error = SBError::wrap(unsafe { sys::SBProcessUnloadImage(self.raw, image_token.0) }); | ||
if error.is_failure() { | ||
Err(error) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Returns the [`SBTarget`] corresponding to this `SBProcess`. | ||
/// | ||
/// This never return `None` if `self` is [`valid`]. | ||
/// | ||
/// [`SBTarget`]: SBTarget | ||
/// [`valid`]: Self::is_valid | ||
pub fn target(&self) -> Option<SBTarget> { | ||
SBTarget::maybe_wrap(unsafe { sys::SBProcessGetTarget(self.raw) }) | ||
} | ||
} | ||
|
||
/// Iterate over the [threads] in a [process]. | ||
|
@@ -621,6 +700,9 @@ impl<'d> Iterator for SBProcessQueueIter<'d> { | |
} | ||
} | ||
|
||
/// The token to unload image | ||
struct ImageToken(u32); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this need to be |
||
|
||
impl Clone for SBProcess { | ||
fn clone(&self) -> SBProcess { | ||
SBProcess { | ||
|
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this just be a
SBModuleSymbolsIter
like the other iterators?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should have direct indexed accessor and length access so I made it have intermediate struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious to know why... but not going to block on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I want to get first library (excluding exexutable), I think it should be possible to access 1th element without accessing 0th element, but I came up with implementing
nth
of iterator is enough so I'll do this instead of intermediate struct.