1 //===-- Block.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Block_h_
11 #define liblldb_Block_h_
15 #include "lldb/Core/AddressRange.h"
16 #include "lldb/Core/RangeMap.h"
17 #include "lldb/Symbol/CompilerType.h"
18 #include "lldb/Symbol/LineEntry.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/SymbolContextScope.h"
21 #include "lldb/Utility/Stream.h"
22 #include "lldb/Utility/UserID.h"
23 #include "lldb/lldb-private.h"
25 namespace lldb_private {
27 //----------------------------------------------------------------------
28 /// @class Block Block.h "lldb/Symbol/Block.h"
29 /// A class that describes a single lexical block.
31 /// A Function object owns a BlockList object which owns one or more
32 /// Block objects. The BlockList object contains a section offset address
33 /// range, and Block objects contain one or more ranges which are offsets into
34 /// that range. Blocks are can have discontiguous ranges within the BlockList
35 /// address range, and each block can contain child blocks each with their own
38 /// Each block has a variable list that represents local, argument, and static
39 /// variables that are scoped to the block.
41 /// Inlined functions are represented by attaching a InlineFunctionInfo shared
42 /// pointer object to a block. Inlined functions are represented as named
44 //----------------------------------------------------------------------
45 class Block : public UserID, public SymbolContextScope {
47 typedef RangeArray<uint32_t, uint32_t, 1> RangeList;
48 typedef RangeList::Entry Range;
50 //------------------------------------------------------------------
51 /// Construct with a User ID \a uid, \a depth.
53 /// Initialize this block with the specified UID \a uid. The \a depth in the
54 /// \a block_list is used to represent the parent, sibling, and child block
55 /// information and also allows for partial parsing at the block level.
58 /// The UID for a given block. This value is given by the
59 /// SymbolFile plug-in and can be any value that helps the
60 /// SymbolFile plug-in to match this block back to the debug
61 /// information data that it parses for further or more in
62 /// depth parsing. Common values would be the index into a
63 /// table, or an offset into the debug information.
66 /// The integer depth of this block in the block list hierarchy.
68 /// @param[in] block_list
69 /// The block list that this object belongs to.
72 //------------------------------------------------------------------
73 Block(lldb::user_id_t uid);
75 //------------------------------------------------------------------
77 //------------------------------------------------------------------
80 //------------------------------------------------------------------
81 /// Add a child to this object.
83 /// @param[in] child_block_sp
84 /// A shared pointer to a child block that will get added to
86 //------------------------------------------------------------------
87 void AddChild(const lldb::BlockSP &child_block_sp);
89 //------------------------------------------------------------------
90 /// Add a new offset range to this block.
92 /// @param[in] start_offset
93 /// An offset into this Function's address range that
94 /// describes the start address of a range for this block.
96 /// @param[in] end_offset
97 /// An offset into this Function's address range that
98 /// describes the end address of a range for this block.
99 //------------------------------------------------------------------
100 void AddRange(const Range &range);
102 void FinalizeRanges();
104 //------------------------------------------------------------------
105 /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
107 /// @see SymbolContextScope
108 //------------------------------------------------------------------
109 void CalculateSymbolContext(SymbolContext *sc) override;
111 lldb::ModuleSP CalculateSymbolContextModule() override;
113 CompileUnit *CalculateSymbolContextCompileUnit() override;
115 Function *CalculateSymbolContextFunction() override;
117 Block *CalculateSymbolContextBlock() override;
119 //------------------------------------------------------------------
120 /// Check if an offset is in one of the block offset ranges.
122 /// @param[in] range_offset
123 /// An offset into the Function's address range.
126 /// Returns \b true if \a range_offset falls in one of this
127 /// block's ranges, \b false otherwise.
128 //------------------------------------------------------------------
129 bool Contains(lldb::addr_t range_offset) const;
131 //------------------------------------------------------------------
132 /// Check if a offset range is in one of the block offset ranges.
135 /// An offset range into the Function's address range.
138 /// Returns \b true if \a range falls in one of this
139 /// block's ranges, \b false otherwise.
140 //------------------------------------------------------------------
141 bool Contains(const Range &range) const;
143 //------------------------------------------------------------------
144 /// Check if this object contains "block" as a child block at any depth.
147 /// A potential child block.
150 /// Returns \b true if \a block is a child of this block, \b
152 //------------------------------------------------------------------
153 bool Contains(const Block *block) const;
155 //------------------------------------------------------------------
156 /// Dump the block contents.
159 /// The stream to which to dump the object description.
161 /// @param[in] base_addr
162 /// The resolved start address of the Function's address
163 /// range. This should be resolved as the file or load address
164 /// prior to passing the value into this function for dumping.
167 /// Limit the number of levels deep that this function should
168 /// print as this block can contain child blocks. Specify
169 /// INT_MAX to dump all child blocks.
171 /// @param[in] show_context
172 /// If \b true, variables will dump their context information.
173 //------------------------------------------------------------------
174 void Dump(Stream *s, lldb::addr_t base_addr, int32_t depth,
175 bool show_context) const;
177 //------------------------------------------------------------------
178 /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
180 /// @see SymbolContextScope
181 //------------------------------------------------------------------
182 void DumpSymbolContext(Stream *s) override;
184 void DumpAddressRanges(Stream *s, lldb::addr_t base_addr);
186 void GetDescription(Stream *s, Function *function,
187 lldb::DescriptionLevel level, Target *target) const;
189 //------------------------------------------------------------------
190 /// Get the parent block.
193 /// The parent block pointer, or nullptr if this block has no
195 //------------------------------------------------------------------
196 Block *GetParent() const;
198 //------------------------------------------------------------------
199 /// Get the inlined block that contains this block.
202 /// If this block contains inlined function info, it will return
203 /// this block, else parent blocks will be searched to see if
204 /// any contain this block. nullptr will be returned if this block
205 /// nor any parent blocks are inlined function blocks.
206 //------------------------------------------------------------------
207 Block *GetContainingInlinedBlock();
209 //------------------------------------------------------------------
210 /// Get the inlined parent block for this block.
213 /// The parent block pointer, or nullptr if this block has no
215 //------------------------------------------------------------------
216 Block *GetInlinedParent();
218 //------------------------------------------------------------------
219 /// Get the sibling block for this block.
222 /// The sibling block pointer, or nullptr if this block has no
224 //------------------------------------------------------------------
225 Block *GetSibling() const;
227 //------------------------------------------------------------------
228 /// Get the first child block.
231 /// The first child block pointer, or nullptr if this block has no
233 //------------------------------------------------------------------
234 Block *GetFirstChild() const {
235 return (m_children.empty() ? nullptr : m_children.front().get());
238 //------------------------------------------------------------------
239 /// Get the variable list for this block only.
241 /// @param[in] can_create
242 /// If \b true, the variables can be parsed if they already
243 /// haven't been, else the current state of the block will be
247 /// A variable list shared pointer that contains all variables
249 //------------------------------------------------------------------
250 lldb::VariableListSP GetBlockVariableList(bool can_create);
252 //------------------------------------------------------------------
253 /// Get the variable list for this block and optionally all child blocks if
254 /// \a get_child_variables is \b true.
256 /// @param[in] get_child_variables
257 /// If \b true, all variables from all child blocks will be
258 /// added to the variable list.
260 /// @param[in] can_create
261 /// If \b true, the variables can be parsed if they already
262 /// haven't been, else the current state of the block will be
263 /// returned. Passing \b true for this parameter can be used
264 /// to see the current state of what has been parsed up to this
267 /// @param[in] add_inline_child_block_variables
268 /// If this is \b false, no child variables of child blocks
269 /// that are inlined functions will be gotten. If \b true then
270 /// all child variables will be added regardless of whether they
271 /// come from inlined functions or not.
274 /// A variable list shared pointer that contains all variables
276 //------------------------------------------------------------------
277 uint32_t AppendBlockVariables(bool can_create, bool get_child_block_variables,
278 bool stop_if_child_block_is_inlined_function,
279 const std::function<bool(Variable *)> &filter,
280 VariableList *variable_list);
282 //------------------------------------------------------------------
283 /// Appends the variables from this block, and optionally from all parent
284 /// blocks, to \a variable_list.
286 /// @param[in] can_create
287 /// If \b true, the variables can be parsed if they already
288 /// haven't been, else the current state of the block will be
289 /// returned. Passing \b true for this parameter can be used
290 /// to see the current state of what has been parsed up to this
293 /// @param[in] get_parent_variables
294 /// If \b true, all variables from all parent blocks will be
295 /// added to the variable list.
297 /// @param[in] stop_if_block_is_inlined_function
298 /// If \b true, all variables from all parent blocks will be
299 /// added to the variable list until there are no parent blocks
300 /// or the parent block has inlined function info.
302 /// @param[in,out] variable_list
303 /// All variables in this block, and optionally all parent
304 /// blocks will be added to this list.
307 /// The number of variable that were appended to \a
309 //------------------------------------------------------------------
310 uint32_t AppendVariables(bool can_create, bool get_parent_variables,
311 bool stop_if_block_is_inlined_function,
312 const std::function<bool(Variable *)> &filter,
313 VariableList *variable_list);
315 //------------------------------------------------------------------
316 /// Get const accessor for any inlined function information.
319 /// A const pointer to any inlined function information, or nullptr
320 /// if this is a regular block.
321 //------------------------------------------------------------------
322 const InlineFunctionInfo *GetInlinedFunctionInfo() const {
323 return m_inlineInfoSP.get();
326 //------------------------------------------------------------------
327 /// Get the symbol file which contains debug info for this block's
328 /// symbol context module.
330 /// @return A pointer to the symbol file or nullptr.
331 //------------------------------------------------------------------
332 SymbolFile *GetSymbolFile();
334 CompilerDeclContext GetDeclContext();
336 //------------------------------------------------------------------
337 /// Get the memory cost of this object.
339 /// Returns the cost of this object plus any owned objects from the ranges,
340 /// variables, and inline function information.
343 /// The number of bytes that this object occupies in memory.
344 //------------------------------------------------------------------
345 size_t MemorySize() const;
347 //------------------------------------------------------------------
348 /// Set accessor for any inlined function information.
351 /// The method name for the inlined function. This value should
354 /// @param[in] mangled
355 /// The mangled method name for the inlined function. This can
356 /// be nullptr if there is no mangled name for an inlined function
357 /// or if the name is the same as \a name.
359 /// @param[in] decl_ptr
360 /// A optional pointer to declaration information for the
361 /// inlined function information. This value can be nullptr to
362 /// indicate that no declaration information is available.
364 /// @param[in] call_decl_ptr
365 /// Optional calling location declaration information that
366 /// describes from where this inlined function was called.
367 //------------------------------------------------------------------
368 void SetInlinedFunctionInfo(const char *name, const char *mangled,
369 const Declaration *decl_ptr,
370 const Declaration *call_decl_ptr);
372 void SetParentScope(SymbolContextScope *parent_scope) {
373 m_parent_scope = parent_scope;
376 //------------------------------------------------------------------
377 /// Set accessor for the variable list.
379 /// Called by the SymbolFile plug-ins after they have parsed the variable
380 /// lists and are ready to hand ownership of the list over to this object.
382 /// @param[in] variable_list_sp
383 /// A shared pointer to a VariableList.
384 //------------------------------------------------------------------
385 void SetVariableList(lldb::VariableListSP &variable_list_sp) {
386 m_variable_list_sp = variable_list_sp;
389 bool BlockInfoHasBeenParsed() const { return m_parsed_block_info; }
391 void SetBlockInfoHasBeenParsed(bool b, bool set_children);
393 Block *FindBlockByID(lldb::user_id_t block_id);
395 size_t GetNumRanges() const { return m_ranges.GetSize(); }
397 bool GetRangeContainingOffset(const lldb::addr_t offset, Range &range);
399 bool GetRangeContainingAddress(const Address &addr, AddressRange &range);
401 bool GetRangeContainingLoadAddress(lldb::addr_t load_addr, Target &target,
402 AddressRange &range);
404 uint32_t GetRangeIndexContainingAddress(const Address &addr);
406 //------------------------------------------------------------------
407 // Since blocks might have multiple discontiguous address ranges, we need to
408 // be able to get at any of the address ranges in a block.
409 //------------------------------------------------------------------
410 bool GetRangeAtIndex(uint32_t range_idx, AddressRange &range);
412 bool GetStartAddress(Address &addr);
414 void SetDidParseVariables(bool b, bool set_children);
417 typedef std::vector<lldb::BlockSP> collection;
418 //------------------------------------------------------------------
420 //------------------------------------------------------------------
421 SymbolContextScope *m_parent_scope;
422 collection m_children;
424 lldb::InlineFunctionInfoSP m_inlineInfoSP; ///< Inlined function information.
425 lldb::VariableListSP m_variable_list_sp; ///< The variable list for all local,
426 ///static and parameter variables
427 ///scoped to this block.
428 bool m_parsed_block_info : 1, ///< Set to true if this block and it's children
429 ///have all been parsed
430 m_parsed_block_variables : 1, m_parsed_child_blocks : 1;
432 // A parent of child blocks can be asked to find a sibling block given
433 // one of its child blocks
434 Block *GetSiblingForChild(const Block *child_block) const;
437 DISALLOW_COPY_AND_ASSIGN(Block);
440 } // namespace lldb_private
442 #endif // liblldb_Block_h_