Skip to content
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 10 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl SBAddress {
/// Returns offset of the address in the section
///
/// See also:
/// - [`get_section`] for get the section corresponding to this address
/// - [`get_section`] for getting the section corresponding to this address
///
/// [`get_section`]: Self::get_section
pub fn get_offset(&self) -> lldb_addr_t {
Expand Down
6 changes: 5 additions & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ impl SBData {
}

/// Reads the data at specified offset to the buffer.
pub fn read_raw_data(&self, offset: sys::lldb_offset_t, buffer: &mut [u8]) -> Result<(), SBError> {
pub fn read_raw_data(
&self,
offset: sys::lldb_offset_t,
buffer: &mut [u8],
) -> Result<(), SBError> {
let error = SBError::default();
unsafe {
sys::SBDataReadRawData(
Expand Down
6 changes: 4 additions & 2 deletions src/filespec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use crate::{sys, SBStream};
use std::ffi::CStr;
use std::fmt;
use std::path::Path;

/// A file specification that divides the path into a
/// directory and basename.
Expand Down Expand Up @@ -34,8 +35,9 @@ impl SBFileSpec {
}

/// Create SBFileSpec from path
pub fn from_path(path: &str, resolve: bool) -> Self {
let path_cstring = std::ffi::CString::new(path).unwrap();
pub fn from_path<P: AsRef<Path>>(path: P, resolve: bool) -> Self {
let path_cstring =
std::ffi::CString::new(path.as_ref().as_os_str().as_encoded_bytes()).unwrap();
Self::wrap(unsafe { sys::CreateSBFileSpec3(path_cstring.as_ptr(), resolve) })
}

Expand Down
23 changes: 13 additions & 10 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl SBProcess {

/// 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 error the memory region is not allowed to read
// 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 {
Expand Down Expand Up @@ -605,33 +605,33 @@ impl SBProcess {
unsafe { sys::SBProcessGetByteOrder(self.raw) }
}

/// Loads the specified image to the process
pub fn load_image(&self, file: &SBFileSpec) -> Result<u32, SBError> {
/// 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(image_token)
Ok(ImageToken(image_token))
}
}

/// Unloads the image loaded with [`load_image`]
/// Unloads the image loaded with [`load_image`].
///
/// [`load_image`]: Self::load_image
pub fn unload_image(&self, image_token: u32) -> Result<(), SBError> {
// the method returns error if image_token is not valid, instead of cause undefined behavior
let error = SBError::wrap(unsafe { sys::SBProcessUnloadImage(self.raw, image_token) });
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`] corresponds to this SBProcess.
/// Returns the [`SBTarget`] corresponding to this `SBProcess`.
///
/// This never return None if `self` is [`valid`].
/// This never return `None` if `self` is [`valid`].
///
/// [`SBTarget`]: SBTarget
/// [`valid`]: Self::is_valid
Expand Down Expand Up @@ -700,6 +700,9 @@ impl<'d> Iterator for SBProcessQueueIter<'d> {
}
}

/// The token to unload image
struct ImageToken(u32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this need to be pub? And exported from lib.rs?


impl Clone for SBProcess {
fn clone(&self) -> SBProcess {
SBProcess {
Expand Down
Loading