lldb/
block.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, SBFileSpec, SBStream};
8use std::ffi::CStr;
9use std::fmt;
10
11/// A lexical block.
12pub struct SBBlock {
13    /// The underlying raw `SBBlockRef`.
14    pub raw: sys::SBBlockRef,
15}
16
17impl SBBlock {
18    /// Construct a new `SBBlock`.
19    pub(crate) fn wrap(raw: sys::SBBlockRef) -> SBBlock {
20        SBBlock { raw }
21    }
22
23    /// Construct a new `Some(SBBlock)` or `None`.
24    pub(crate) fn maybe_wrap(raw: sys::SBBlockRef) -> Option<SBBlock> {
25        if unsafe { sys::SBBlockIsValid(raw) } {
26            Some(SBBlock { raw })
27        } else {
28            None
29        }
30    }
31
32    /// Check whether or not this is a valid `SBBlock` value.
33    pub fn is_valid(&self) -> bool {
34        unsafe { sys::SBBlockIsValid(self.raw) }
35    }
36
37    /// Does this block represent an inlined function?
38    pub fn is_inlined(&self) -> bool {
39        unsafe { sys::SBBlockIsInlined(self.raw) }
40    }
41
42    /// Get the function name if this block represents an inlined function.
43    pub fn inlined_name(&self) -> &str {
44        unsafe {
45            match CStr::from_ptr(sys::SBBlockGetInlinedName(self.raw)).to_str() {
46                Ok(s) => s,
47                _ => panic!("Invalid string?"),
48            }
49        }
50    }
51
52    /// Get the call site file if this block represents an inlined function.
53    pub fn inlined_call_site_file(&self) -> Option<SBFileSpec> {
54        SBFileSpec::maybe_wrap(unsafe { sys::SBBlockGetInlinedCallSiteFile(self.raw) })
55    }
56
57    /// Get the call site line number if this block represents an inlined function.
58    pub fn inlined_call_site_line(&self) -> Option<u32> {
59        let line = unsafe { sys::SBBlockGetInlinedCallSiteLine(self.raw) };
60        if line > 0 {
61            Some(line)
62        } else {
63            None
64        }
65    }
66
67    /// Get the call site column number if this block represents an inlined function.
68    pub fn inlined_call_site_column(&self) -> Option<u32> {
69        let column = unsafe { sys::SBBlockGetInlinedCallSiteColumn(self.raw) };
70        if column > 0 {
71            Some(column)
72        } else {
73            None
74        }
75    }
76
77    /// Get the parent block
78    pub fn parent(&self) -> Option<SBBlock> {
79        SBBlock::maybe_wrap(unsafe { sys::SBBlockGetParent(self.raw) })
80    }
81
82    /// Get the inlined block that is or contains this block.
83    pub fn containing_inlined_block(&self) -> Option<SBBlock> {
84        SBBlock::maybe_wrap(unsafe { sys::SBBlockGetContainingInlinedBlock(self.raw) })
85    }
86
87    /// Get the sibling block for this block.
88    pub fn sibling(&self) -> Option<SBBlock> {
89        SBBlock::maybe_wrap(unsafe { sys::SBBlockGetSibling(self.raw) })
90    }
91
92    /// Get the first child block for this block.
93    pub fn first_child(&self) -> Option<SBBlock> {
94        SBBlock::maybe_wrap(unsafe { sys::SBBlockGetFirstChild(self.raw) })
95    }
96
97    /// The number of address ranges associated with this block.
98    pub fn num_ranges(&self) -> u32 {
99        unsafe { sys::SBBlockGetNumRanges(self.raw) }
100    }
101
102    /// Get the start address of an address range.
103    pub fn range_start_address(&self, idx: u32) -> SBAddress {
104        SBAddress {
105            raw: unsafe { sys::SBBlockGetRangeStartAddress(self.raw, idx) },
106        }
107    }
108
109    /// Get the end address of an address range.
110    pub fn range_end_address(&self, idx: u32) -> SBAddress {
111        SBAddress {
112            raw: unsafe { sys::SBBlockGetRangeEndAddress(self.raw, idx) },
113        }
114    }
115
116    /// Given an address, find out which address range it is part of.
117    pub fn range_index_for_block_address(&self, block_address: &SBAddress) -> u32 {
118        unsafe { sys::SBBlockGetRangeIndexForBlockAddress(self.raw, block_address.raw) }
119    }
120}
121
122impl Clone for SBBlock {
123    fn clone(&self) -> SBBlock {
124        SBBlock {
125            raw: unsafe { sys::CloneSBBlock(self.raw) },
126        }
127    }
128}
129
130impl fmt::Debug for SBBlock {
131    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
132        let stream = SBStream::new();
133        unsafe { sys::SBBlockGetDescription(self.raw, stream.raw) };
134        write!(fmt, "SBBlock {{ {} }}", stream.data())
135    }
136}
137
138impl Drop for SBBlock {
139    fn drop(&mut self) {
140        unsafe { sys::DisposeSBBlock(self.raw) };
141    }
142}
143
144unsafe impl Send for SBBlock {}
145unsafe impl Sync for SBBlock {}
146
147#[cfg(feature = "graphql")]
148#[juniper::graphql_object]
149impl SBBlock {
150    fn is_inlined() -> bool {
151        self.is_inlined()
152    }
153
154    fn inlined_name() -> &str {
155        self.inlined_name()
156    }
157
158    fn inlined_call_site_file() -> Option<SBFileSpec> {
159        self.inlined_call_site_file()
160    }
161
162    // TODO(bm) This should be u32
163    fn inlined_call_site_line() -> Option<i32> {
164        self.inlined_call_site_line().map(|i| i as i32)
165    }
166
167    // TODO(bm) This should be u32
168    fn inlined_call_site_column() -> Option<i32> {
169        self.inlined_call_site_column().map(|i| i as i32)
170    }
171}