lldb/queueitem.rs
1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use crate::{sys, SBAddress, SBThread};
8use std::ffi::CString;
9
10/// A work item enqueued on a libdispatch aka Grand Central
11/// Dispatch (GCD) queue.
12///
13/// Most often, this will be a function or block.
14///
15/// "enqueued" here means that the work item has been added to a queue
16/// but it has not yet started executing. When it is "dequeued",
17/// execution of the item begins.
18pub struct SBQueueItem {
19 /// The underlying raw `SBQueueItemRef`.
20 pub raw: sys::SBQueueItemRef,
21}
22
23impl SBQueueItem {
24 /// Construct a new `SBQueueItem`.
25 pub(crate) fn wrap(raw: sys::SBQueueItemRef) -> SBQueueItem {
26 SBQueueItem { raw }
27 }
28
29 /// Construct a new `Some(SBQueueItem)` or `None`.
30 #[allow(dead_code)]
31 pub(crate) fn maybe_wrap(raw: sys::SBQueueItemRef) -> Option<SBQueueItem> {
32 if unsafe { sys::SBQueueItemIsValid(raw) } {
33 Some(SBQueueItem { raw })
34 } else {
35 None
36 }
37 }
38
39 /// Check whether or not this is a valid `SBQueueItem` value.
40 pub fn is_valid(&self) -> bool {
41 unsafe { sys::SBQueueItemIsValid(self.raw) }
42 }
43
44 /// The kind of this work item.
45 pub fn kind(&self) -> sys::QueueItemKind {
46 unsafe { sys::SBQueueItemGetKind(self.raw) }
47 }
48
49 /// The code address that will be executed when this work item
50 /// is executed.
51 ///
52 /// Not all queue items will have an address associated with them.
53 /// `QueueItemKind::Function` and `QueueItemKind::Block` work items
54 /// should have an address.
55 pub fn address(&self) -> Option<SBAddress> {
56 SBAddress::maybe_wrap(unsafe { sys::SBQueueItemGetAddress(self.raw) })
57 }
58
59 /// Get an extended backtrace thread for this queue item, if available
60 ///
61 /// If the backtrace/thread information was collected when this item
62 /// was enqueued, this call will provide it.
63 ///
64 /// The `thread_type` will typically be one of `"libdispatch"` or
65 /// `"pthread"`.
66 pub fn extended_backtrace_thread(&self, thread_type: &str) -> Option<SBThread> {
67 let thread_type = CString::new(thread_type).unwrap();
68 SBThread::maybe_wrap(unsafe {
69 sys::SBQueueItemGetExtendedBacktraceThread(self.raw, thread_type.as_ptr())
70 })
71 }
72}
73
74impl Clone for SBQueueItem {
75 fn clone(&self) -> SBQueueItem {
76 SBQueueItem {
77 raw: unsafe { sys::CloneSBQueueItem(self.raw) },
78 }
79 }
80}
81
82impl Drop for SBQueueItem {
83 fn drop(&mut self) {
84 unsafe { sys::DisposeSBQueueItem(self.raw) };
85 }
86}
87
88unsafe impl Send for SBQueueItem {}
89unsafe impl Sync for SBQueueItem {}
90
91#[cfg(feature = "graphql")]
92#[juniper::graphql_object]
93impl SBQueueItem {
94 fn address() -> Option<SBAddress> {
95 self.address()
96 }
97}