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

Control lldb via commands and read stdout #27

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Changes from all 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
37 changes: 37 additions & 0 deletions src/debugger.rs
Original file line number Diff line number Diff line change
@@ -199,6 +199,43 @@ impl SBDebugger {
SBCommandInterpreter::wrap(unsafe { sys::SBDebuggerGetCommandInterpreter(self.raw) })
}

/// Executes a command as lldb would run in the console and returns a result that contains a
/// string of the output if the command execution was successful. If there was an error, it
/// contains an error message.
///
/// (lldb) b main
/// => Is equal to `debugger.execute_command("b main")`
///
pub fn execute_command(&self, command: &str) -> Result<&str, String> {
let result = unsafe { sys::CreateSBCommandReturnObject() };

let interpreter = self.command_interpreter();
let command = CString::new(command).unwrap();

unsafe {
sys::SBCommandInterpreterHandleCommand(
interpreter.raw,
command.as_ptr(),
result,
false,
);
}

if unsafe { sys::SBCommandReturnObjectSucceeded(result) } {
let output = unsafe { sys::SBCommandReturnObjectGetOutput(result) };
return match unsafe { CStr::from_ptr(output).to_str() } {
Ok(s) => Ok(s),
Err(err_str) => Err(err_str.to_string()),
};
}

let err_str = unsafe { sys::SBCommandReturnObjectGetError(result) };
match unsafe { CStr::from_ptr(err_str).to_str() } {
Ok(s) => Err(s.to_string()),
Err(err_str) => Err(err_str.to_string()),
}
}

/// Enable logging (defaults to `stderr`).
///
/// `enable_log("lldb", &["default"])` is useful for troubleshooting in most
18 changes: 9 additions & 9 deletions src/listener.rs
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ impl SBListener {
}

#[allow(missing_docs)]
pub fn wait_for_event(&self, num_seconds: u32, event: &mut SBEvent) -> bool {
pub fn wait_for_event(&self, num_seconds: u32, event: &SBEvent) -> bool {
unsafe { sys::SBListenerWaitForEvent(self.raw, num_seconds, event.raw) }
}

@@ -95,7 +95,7 @@ impl SBListener {
&self,
num_seconds: u32,
broadcaster: &SBBroadcaster,
event: &mut SBEvent,
event: &SBEvent,
) -> bool {
unsafe {
sys::SBListenerWaitForEventForBroadcaster(
@@ -113,7 +113,7 @@ impl SBListener {
num_seconds: u32,
broadcaster: &SBBroadcaster,
event_type_mask: u32,
event: &mut SBEvent,
event: &SBEvent,
) -> bool {
unsafe {
sys::SBListenerWaitForEventForBroadcasterWithType(
@@ -127,15 +127,15 @@ impl SBListener {
}

#[allow(missing_docs)]
pub fn peek_at_next_event(&self, event: &mut SBEvent) -> bool {
pub fn peek_at_next_event(&self, event: &SBEvent) -> bool {
unsafe { sys::SBListenerPeekAtNextEvent(self.raw, event.raw) }
}

#[allow(missing_docs)]
pub fn peek_at_next_event_for_broadcaster(
&self,
broadcaster: &SBBroadcaster,
event: &mut SBEvent,
event: &SBEvent,
) -> bool {
unsafe {
sys::SBListenerPeekAtNextEventForBroadcaster(self.raw, broadcaster.raw, event.raw)
@@ -147,7 +147,7 @@ impl SBListener {
&self,
broadcaster: &SBBroadcaster,
event_type_mask: u32,
event: &mut SBEvent,
event: &SBEvent,
) -> bool {
unsafe {
sys::SBListenerPeekAtNextEventForBroadcasterWithType(
@@ -160,15 +160,15 @@ impl SBListener {
}

#[allow(missing_docs)]
pub fn get_next_event(&self, event: &mut SBEvent) -> bool {
pub fn get_next_event(&self, event: &SBEvent) -> bool {
unsafe { sys::SBListenerGetNextEvent(self.raw, event.raw) }
}

#[allow(missing_docs)]
pub fn get_next_event_for_broadcaster(
&self,
broadcaster: &SBBroadcaster,
event: &mut SBEvent,
event: &SBEvent,
) -> bool {
unsafe { sys::SBListenerGetNextEventForBroadcaster(self.raw, broadcaster.raw, event.raw) }
}
@@ -178,7 +178,7 @@ impl SBListener {
&self,
broadcaster: &SBBroadcaster,
event_type_mask: u32,
event: &mut SBEvent,
event: &SBEvent,
) -> bool {
unsafe {
sys::SBListenerGetNextEventForBroadcasterWithType(
30 changes: 30 additions & 0 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -306,6 +306,36 @@ impl SBProcess {
}
}

/// Reads data from the current process's stdout stream until the end ot the stream.
pub fn get_stdout_all(&self) -> Option<String> {
let dst_len = 0x1000;
let mut output = "".to_string();
let mut dst: Vec<u8> = Vec::with_capacity(dst_len);
loop {
let out_len =
unsafe { sys::SBProcessGetSTDOUT(self.raw, dst.as_mut_ptr() as *mut i8, dst_len) };
if out_len == 0 {
break;
}
unsafe { dst.set_len(out_len) };
output += std::str::from_utf8(&dst).ok()?;
}

Some(output)
}

/// Reads data from the current process's stdout stream.
pub fn get_stdout(&self) -> Option<String> {
let dst_len = 0x1000;
let mut dst: Vec<u8> = Vec::with_capacity(dst_len);

let out_len =
unsafe { sys::SBProcessGetSTDOUT(self.raw, dst.as_mut_ptr() as *mut i8, dst_len) };

unsafe { dst.set_len(out_len) };
String::from_utf8(dst).ok()
}

#[allow(missing_docs)]
pub fn broadcaster(&self) -> SBBroadcaster {
SBBroadcaster::wrap(unsafe { sys::SBProcessGetBroadcaster(self.raw) })