1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::{
lldb_tid_t, sys, SBError, SBEvent, SBFrame, SBProcess, SBQueue, SBStream, SBValue, StopReason,
};
use std::ffi::CStr;
use std::fmt;
/// A thread of execution.
///
/// `SBThread`s can be referred to by their ID, which maps to the system
/// specific thread identifier, or by `IndexID`. The ID may or may not
/// be unique depending on whether the system reuses its thread identifiers.
/// The `IndexID` is a monotonically increasing identifier that will always
/// uniquely reference a particular thread, and when that thread goes
/// away it will not be reused.
///
/// # Thread State
///
/// ...
///
/// # Execution Control
///
/// ...
///
/// # Frames
///
/// The thread contains [stack frames]. These can be iterated
/// over with [`SBThread::frames()`]:
///
/// ```no_run
/// # use lldb::{SBFrame, SBThread};
/// # fn look_at_frames(thread: SBThread) {
/// // Iterate over the frames...
/// for frame in thread.frames() {
/// println!("Hello {:?}!", frame);
/// }
/// // Or collect them into a vector!
/// let frames = thread.frames().collect::<Vec<SBFrame>>();
/// # }
/// ```
///
/// Some functions operate on the 'currently selected frame'. This can
/// retrieved via [`SBThread::selected_frame()`] and set via
/// [`SBThread::set_selected_frame()`].
///
///
/// # Events
///
/// ...
///
/// [stack frames]: SBFrame
pub struct SBThread {
/// The underlying raw `SBThreadRef`.
pub raw: sys::SBThreadRef,
}
impl SBThread {
/// Construct a new `SBThread`.
pub(crate) fn wrap(raw: sys::SBThreadRef) -> SBThread {
SBThread { raw }
}
/// Construct a new `Some(SBThread)` or `None`.
pub(crate) fn maybe_wrap(raw: sys::SBThreadRef) -> Option<SBThread> {
if unsafe { sys::SBThreadIsValid(raw) } {
Some(SBThread { raw })
} else {
None
}
}
/// Check whether or not this is a valid `SBThread` value.
pub fn is_valid(&self) -> bool {
unsafe { sys::SBThreadIsValid(self.raw) }
}
#[allow(missing_docs)]
pub fn broadcaster_class_name() -> &'static str {
unsafe {
match CStr::from_ptr(sys::SBThreadGetBroadcasterClassName()).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}
/// Get the stop reason for this thread.
pub fn stop_reason(&self) -> StopReason {
unsafe { sys::SBThreadGetStopReason(self.raw) }
}
/// The return value from the last stop if we just stopped due
/// to stepping out of a function
pub fn stop_return_value(&self) -> Option<SBValue> {
SBValue::maybe_wrap(unsafe { sys::SBThreadGetStopReturnValue(self.raw) })
}
/// Returns a unique thread identifier for the current `SBThread`
/// that will remain constant throughout the thread's lifetime in
/// this process and will not be reused by another thread during this
/// process lifetime. On macOS systems, this is a system-wide
/// unique thread identifier; this identifier is also used by
/// other tools like sample which helps to associate data from
/// those tools with lldb. See related [`SBThread::index_id`].
pub fn thread_id(&self) -> lldb_tid_t {
unsafe { sys::SBThreadGetThreadID(self.raw) }
}
/// Return the index number for this `SBThread`. The index
/// number is the same thing that a user gives as an argument
/// to `thread select` in the command line lldb.
///
/// These numbers start at `1` (for the first thread lldb sees
/// in a debug session) and increments up throughout the process
/// lifetime. An index number will not be reused for a different
/// thread later in a process - thread 1 will always be associated
/// with the same thread. See related [`SBThread::thread_id`].
pub fn index_id(&self) -> u32 {
unsafe { sys::SBThreadGetIndexID(self.raw) }
}
/// The name associated with the thread, if any.
pub fn name(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBThreadGetName(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}
/// Return the queue associated with this thread, if any.
///
/// If this `SBThread` is actually a history thread, then there may be
/// a queue ID and name available, but not a full [`SBQueue`] as the
/// individual attributes may have been saved, but without enough
/// information to reconstitute the entire `SBQueue` at that time.
pub fn queue(&self) -> Option<SBQueue> {
SBQueue::maybe_wrap(unsafe { sys::SBThreadGetQueue(self.raw) })
}
/// Return the queue name associated with this thread, if any.
///
/// For example, this would report a `libdispatch` (Grand Central Dispatch)
/// queue name.
pub fn queue_name(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBThreadGetQueueName(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}
/// Return the `dispatch_queue_id` for this thread, if any.
///
/// For example, this would report a `libdispatch` (Grand Central Dispatch)
/// queue ID.
pub fn queue_id(&self) -> u64 {
unsafe { sys::SBThreadGetQueueID(self.raw) }
}
/// Set the user resume state for this thread to suspend.
///
/// LLDB currently supports process centric debugging which means when any
/// thread in a process stops, all other threads are stopped. The `suspend`
/// call here tells our process to suspend a thread and not let it run when
/// the other threads in a process are allowed to run. So when
/// [`SBProcess::continue_execution()`] is called, any threads that
/// aren't suspended will be allowed to run. If any of the `SBThread`
/// functions for stepping are called (`step_over`, `step_into`,
/// `step_out`, `step_instruction`, `run_to_address`), the thread will
/// not be allowed to run and these functions will simply return.
pub fn suspend(&self) -> Result<(), SBError> {
let error: SBError = SBError::default();
unsafe { sys::SBThreadSuspend(self.raw, error.raw) };
error.into_result()
}
/// Set the user resume state for this to allow it to run again.
///
/// See the discussion on [`SBThread::suspend()`] for further details.
pub fn resume(&self) -> Result<(), SBError> {
let error: SBError = SBError::default();
unsafe { sys::SBThreadResume(self.raw, error.raw) };
error.into_result()
}
/// Is this thread set to the suspended user resume state?
///
/// See the discussion on [`SBThread::suspend()`] for further details.
pub fn is_suspended(&self) -> bool {
unsafe { sys::SBThreadIsSuspended(self.raw) }
}
/// Is this thread stopped?
pub fn is_stopped(&self) -> bool {
unsafe { sys::SBThreadIsStopped(self.raw) }
}
/// Get an iterator over the [frames] known to this thread instance.
///
/// [frames]: SBFrame
pub fn frames(&self) -> SBThreadFrameIter {
SBThreadFrameIter {
thread: self,
idx: 0,
}
}
/// Get the currently selected frame for this thread.
pub fn selected_frame(&self) -> SBFrame {
SBFrame::wrap(unsafe { sys::SBThreadGetSelectedFrame(self.raw) })
}
/// Set the currently selected frame for this thread. This takes a frame index.
pub fn set_selected_frame(&self, frame_index: u32) -> Option<SBFrame> {
SBFrame::maybe_wrap(unsafe { sys::SBThreadSetSelectedFrame(self.raw, frame_index) })
}
/// Get the process in which this thread is running.
pub fn process(&self) -> SBProcess {
SBProcess::wrap(unsafe { sys::SBThreadGetProcess(self.raw) })
}
/// If the given event is a thread event, return it as an
/// `SBThreadEvent`. Otherwise, return `None`.
pub fn event_as_thread_event(event: &SBEvent) -> Option<SBThreadEvent> {
if unsafe { sys::SBThreadEventIsThreadEvent(event.raw) } {
Some(SBThreadEvent::new(event))
} else {
None
}
}
}
/// Iterate over the [frames] in a [thread].
///
/// [frames]: SBFrame
/// [thread]: SBThread
pub struct SBThreadFrameIter<'d> {
thread: &'d SBThread,
idx: usize,
}
impl<'d> Iterator for SBThreadFrameIter<'d> {
type Item = SBFrame;
fn next(&mut self) -> Option<SBFrame> {
if self.idx < unsafe { sys::SBThreadGetNumFrames(self.thread.raw) as usize } {
let r = Some(SBFrame::wrap(unsafe {
sys::SBThreadGetFrameAtIndex(self.thread.raw, self.idx as u32)
}));
self.idx += 1;
r
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let sz = unsafe { sys::SBThreadGetNumFrames(self.thread.raw) } as usize;
(sz - self.idx, Some(sz))
}
}
impl<'d> ExactSizeIterator for SBThreadFrameIter<'d> {}
impl Clone for SBThread {
fn clone(&self) -> SBThread {
SBThread {
raw: unsafe { sys::CloneSBThread(self.raw) },
}
}
}
impl fmt::Debug for SBThread {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let stream = SBStream::new();
unsafe { sys::SBThreadGetDescription(self.raw, stream.raw) };
write!(fmt, "SBThread {{ {} }}", stream.data())
}
}
impl Drop for SBThread {
fn drop(&mut self) {
unsafe { sys::DisposeSBThread(self.raw) };
}
}
unsafe impl Send for SBThread {}
unsafe impl Sync for SBThread {}
/// A thread event.
pub struct SBThreadEvent<'e> {
event: &'e SBEvent,
}
impl<'e> SBThreadEvent<'e> {
/// Construct a new `SBThreadEvent`.
pub fn new(event: &'e SBEvent) -> Self {
SBThreadEvent { event }
}
/// Get the thread from this thread event.
pub fn thread(&self) -> SBThread {
SBThread::wrap(unsafe { sys::SBThreadGetThreadFromEvent(self.event.raw) })
}
/// Get the frame from this thread event.
pub fn frame(&self) -> Option<SBFrame> {
SBFrame::maybe_wrap(unsafe { sys::SBThreadGetStackFrameFromEvent(self.event.raw) })
}
}
#[cfg(feature = "graphql")]
#[graphql_object]
impl SBThread {
// TODO(bm): This should be u64
fn thread_id(&self) -> i32 {
self.thread_id() as i32
}
// TODO(bm) This should be u32
fn index_id() -> i32 {
self.index_id() as i32
}
fn frames() -> Vec<SBFrame> {
self.frames().collect()
}
fn selected_frame() -> SBFrame {
self.selected_frame()
}
fn process() -> SBProcess {
self.process()
}
}