blob: a2193891cf7078d2b0a2277f861f975ba464d39a [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Block.cpp -----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/Block.h"
11#include "lldb/Symbol/Function.h"
12#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
14#include "lldb/Symbol/SymbolVendor.h"
15#include "lldb/Symbol/VariableList.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
Greg Clayton75ccf502010-08-21 02:22:51 +000020Block::Block(lldb::user_id_t uid) :
Chris Lattner24943d22010-06-08 16:52:24 +000021 UserID(uid),
Greg Clayton75ccf502010-08-21 02:22:51 +000022 m_parent_scope (NULL),
23 m_sibling (NULL),
24 m_children (),
25 m_ranges (),
26 m_inlineInfoSP (),
Greg Claytonb04e7a82010-08-24 21:05:24 +000027 m_variable_list_sp (),
Greg Clayton75ccf502010-08-21 02:22:51 +000028 m_parsed_block_info (false),
29 m_parsed_block_variables (false),
30 m_parsed_child_blocks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000031{
32}
33
Chris Lattner24943d22010-06-08 16:52:24 +000034Block::~Block ()
35{
36}
37
38void
Greg Claytoneea26402010-09-14 23:36:40 +000039Block::GetDescription(Stream *s, Function *function, lldb::DescriptionLevel level, Target *target) const
Greg Clayton12bec712010-06-28 21:30:43 +000040{
Greg Claytonc67b7d12010-09-10 01:30:46 +000041 *s << "id = " << ((const UserID&)*this);
42
Greg Clayton12bec712010-06-28 21:30:43 +000043 size_t num_ranges = m_ranges.size();
44 if (num_ranges)
45 {
46
47 addr_t base_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneea26402010-09-14 23:36:40 +000048 if (target)
49 base_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress(target);
Greg Clayton12bec712010-06-28 21:30:43 +000050 if (base_addr == LLDB_INVALID_ADDRESS)
Greg Clayton75ccf502010-08-21 02:22:51 +000051 base_addr = function->GetAddressRange().GetBaseAddress().GetFileAddress();
Greg Clayton12bec712010-06-28 21:30:43 +000052
Greg Claytonc67b7d12010-09-10 01:30:46 +000053 s->Printf(", range%s = ", num_ranges > 1 ? "s" : "");
Greg Clayton12bec712010-06-28 21:30:43 +000054 std::vector<VMRange>::const_iterator pos, end = m_ranges.end();
55 for (pos = m_ranges.begin(); pos != end; ++pos)
56 pos->Dump(s, base_addr, 4);
57 }
Greg Clayton12bec712010-06-28 21:30:43 +000058
59 if (m_inlineInfoSP.get() != NULL)
Greg Clayton1924e242010-09-15 05:51:24 +000060 {
61 bool show_fullpaths = (level == eDescriptionLevelVerbose);
62 m_inlineInfoSP->Dump(s, show_fullpaths);
63 }
Greg Clayton12bec712010-06-28 21:30:43 +000064}
65
66void
Chris Lattner24943d22010-06-08 16:52:24 +000067Block::Dump(Stream *s, addr_t base_addr, int32_t depth, bool show_context) const
68{
69 if (depth < 0)
70 {
Greg Clayton75ccf502010-08-21 02:22:51 +000071 Block *parent = GetParent();
72 if (parent)
73 {
74 // We have a depth that is less than zero, print our parent blocks
75 // first
76 parent->Dump(s, base_addr, depth + 1, show_context);
77 }
Chris Lattner24943d22010-06-08 16:52:24 +000078 }
79
80 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
81 s->Indent();
82 *s << "Block" << ((const UserID&)*this);
Greg Clayton12bec712010-06-28 21:30:43 +000083 const Block* parent_block = GetParent();
84 if (parent_block)
Chris Lattner24943d22010-06-08 16:52:24 +000085 {
Greg Clayton12bec712010-06-28 21:30:43 +000086 s->Printf(", parent = {0x%8.8x}", parent_block->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +000087 }
88 if (m_inlineInfoSP.get() != NULL)
Greg Clayton1924e242010-09-15 05:51:24 +000089 {
90 bool show_fullpaths = false;
91 m_inlineInfoSP->Dump(s, show_fullpaths);
92 }
Chris Lattner24943d22010-06-08 16:52:24 +000093
94 if (!m_ranges.empty())
95 {
96 *s << ", ranges =";
97 std::vector<VMRange>::const_iterator pos;
98 std::vector<VMRange>::const_iterator end = m_ranges.end();
99 for (pos = m_ranges.begin(); pos != end; ++pos)
100 {
101 if (parent_block != NULL && parent_block->Contains(*pos) == false)
102 *s << '!';
103 else
104 *s << ' ';
105 pos->Dump(s, base_addr);
106 }
107 }
108 s->EOL();
109
110 if (depth > 0)
111 {
112 s->IndentMore();
113
Greg Claytonb04e7a82010-08-24 21:05:24 +0000114 if (m_variable_list_sp.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000115 {
Greg Claytonb04e7a82010-08-24 21:05:24 +0000116 m_variable_list_sp->Dump(s, show_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000117 }
118
Greg Clayton75ccf502010-08-21 02:22:51 +0000119 for (Block *child_block = GetFirstChild(); child_block != NULL; child_block = child_block->GetSibling())
Chris Lattner24943d22010-06-08 16:52:24 +0000120 {
Greg Clayton75ccf502010-08-21 02:22:51 +0000121 child_block->Dump(s, base_addr, depth - 1, show_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000122 }
123
124 s->IndentLess();
125 }
126
127}
128
129
Greg Clayton75ccf502010-08-21 02:22:51 +0000130Block *
131Block::FindBlockByID (user_id_t block_id)
132{
133 if (block_id == GetID())
134 return this;
135
136 Block *matching_block = NULL;
137 for (Block *child_block = GetFirstChild(); child_block != NULL; child_block = child_block->GetSibling())
138 {
139 matching_block = child_block->FindBlockByID (block_id);
140 if (matching_block)
141 break;
142 }
143 return matching_block;
144}
145
Chris Lattner24943d22010-06-08 16:52:24 +0000146void
Greg Claytonb04e7a82010-08-24 21:05:24 +0000147Block::CalculateSymbolContext (SymbolContext* sc)
Chris Lattner24943d22010-06-08 16:52:24 +0000148{
Greg Clayton75ccf502010-08-21 02:22:51 +0000149 if (m_parent_scope)
150 m_parent_scope->CalculateSymbolContext(sc);
Chris Lattner24943d22010-06-08 16:52:24 +0000151 sc->block = this;
Chris Lattner24943d22010-06-08 16:52:24 +0000152}
153
154void
Greg Clayton69aa5d92010-09-07 04:20:48 +0000155Block::DumpStopContext
156(
157 Stream *s,
158 const SymbolContext *sc_ptr,
159 const Declaration *child_inline_call_site,
160 bool show_fullpaths,
161 bool show_inline_blocks)
Chris Lattner24943d22010-06-08 16:52:24 +0000162{
Greg Clayton12bec712010-06-28 21:30:43 +0000163 Block* parent_block = GetParent();
Chris Lattner24943d22010-06-08 16:52:24 +0000164
Greg Clayton69aa5d92010-09-07 04:20:48 +0000165 const InlineFunctionInfo* inline_info = GetInlinedFunctionInfo ();
166 const Declaration *inline_call_site = child_inline_call_site;
Chris Lattner24943d22010-06-08 16:52:24 +0000167 if (inline_info)
168 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000169 inline_call_site = &inline_info->GetCallSite();
170 if (sc_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000171 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000172 // First frame in a frame with inlined functions
Chris Lattner24943d22010-06-08 16:52:24 +0000173 s->PutCString (" [inlined]");
174 }
Greg Clayton69aa5d92010-09-07 04:20:48 +0000175 if (show_inline_blocks)
176 s->EOL();
177 else
178 s->PutChar(' ');
179
180 s->PutCString(inline_info->GetName ().AsCString());
181
182 if (child_inline_call_site && child_inline_call_site->IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000183 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000184 s->PutCString(" at ");
185 child_inline_call_site->DumpStopContext (s, show_fullpaths);
Chris Lattner24943d22010-06-08 16:52:24 +0000186 }
187 }
188
Greg Clayton69aa5d92010-09-07 04:20:48 +0000189 if (sc_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000190 {
191 // If we have any inlined functions, this will be the deepest most
192 // inlined location
Greg Clayton69aa5d92010-09-07 04:20:48 +0000193 if (sc_ptr->line_entry.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000194 {
195 s->PutCString(" at ");
Greg Clayton69aa5d92010-09-07 04:20:48 +0000196 sc_ptr->line_entry.DumpStopContext (s, show_fullpaths);
Chris Lattner24943d22010-06-08 16:52:24 +0000197 }
198 }
Greg Clayton69aa5d92010-09-07 04:20:48 +0000199
200 if (show_inline_blocks)
201 {
202 if (parent_block)
203 {
204 parent_block->Block::DumpStopContext (s,
205 NULL,
206 inline_call_site,
207 show_fullpaths,
208 show_inline_blocks);
209 }
210 else if (child_inline_call_site)
211 {
212 SymbolContext sc;
213 CalculateSymbolContext(&sc);
214 if (sc.function)
215 {
216 s->EOL();
217 s->Indent (sc.function->GetMangled().GetName().AsCString());
218 if (child_inline_call_site && child_inline_call_site->IsValid())
219 {
220 s->PutCString(" at ");
221 child_inline_call_site->DumpStopContext (s, show_fullpaths);
222 }
223 }
224 }
225 }
Chris Lattner24943d22010-06-08 16:52:24 +0000226}
227
228
229void
230Block::DumpSymbolContext(Stream *s)
231{
Greg Clayton75ccf502010-08-21 02:22:51 +0000232 SymbolContext sc;
233 CalculateSymbolContext(&sc);
234 if (sc.function)
235 sc.function->DumpSymbolContext(s);
Chris Lattner24943d22010-06-08 16:52:24 +0000236 s->Printf(", Block{0x%8.8x}", GetID());
237}
238
239bool
240Block::Contains (addr_t range_offset) const
241{
242 return VMRange::ContainsValue(m_ranges, range_offset);
243}
244
245bool
Greg Clayton72b71582010-09-02 21:44:10 +0000246Block::Contains (const Block *block) const
247{
Greg Clayton72b71582010-09-02 21:44:10 +0000248 if (this == block)
Greg Claytona357ecf2010-09-14 03:16:58 +0000249 return false; // This block doesn't contain itself...
Greg Clayton72b71582010-09-02 21:44:10 +0000250
Greg Claytona357ecf2010-09-14 03:16:58 +0000251 // Walk the parent chain for "block" and see if any if them match this block
Greg Clayton72b71582010-09-02 21:44:10 +0000252 const Block *block_parent;
253 for (block_parent = block->GetParent();
254 block_parent != NULL;
255 block_parent = block_parent->GetParent())
256 {
Greg Claytona357ecf2010-09-14 03:16:58 +0000257 if (this == block_parent)
258 return true; // One of the parents of "block" is this object!
Greg Clayton72b71582010-09-02 21:44:10 +0000259 }
260 return false;
261}
262
263bool
Chris Lattner24943d22010-06-08 16:52:24 +0000264Block::Contains (const VMRange& range) const
265{
266 return VMRange::ContainsRange(m_ranges, range);
267}
268
Greg Clayton75ccf502010-08-21 02:22:51 +0000269Block *
270Block::GetParent () const
Chris Lattner24943d22010-06-08 16:52:24 +0000271{
Greg Clayton75ccf502010-08-21 02:22:51 +0000272 if (m_parent_scope)
Chris Lattner24943d22010-06-08 16:52:24 +0000273 {
Greg Clayton75ccf502010-08-21 02:22:51 +0000274 SymbolContext sc;
275 m_parent_scope->CalculateSymbolContext(&sc);
276 if (sc.block)
277 return sc.block;
Chris Lattner24943d22010-06-08 16:52:24 +0000278 }
Greg Clayton75ccf502010-08-21 02:22:51 +0000279 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000280}
281
Greg Clayton33ed1702010-08-24 00:45:41 +0000282Block *
Greg Claytonb04e7a82010-08-24 21:05:24 +0000283Block::GetContainingInlinedBlock ()
284{
Greg Clayton69aa5d92010-09-07 04:20:48 +0000285 if (GetInlinedFunctionInfo())
Greg Claytonb04e7a82010-08-24 21:05:24 +0000286 return this;
287 return GetInlinedParent ();
288}
289
290Block *
291Block::GetInlinedParent ()
Greg Clayton33ed1702010-08-24 00:45:41 +0000292{
293 Block *parent_block = GetParent ();
294 if (parent_block)
295 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000296 if (parent_block->GetInlinedFunctionInfo())
Greg Clayton33ed1702010-08-24 00:45:41 +0000297 return parent_block;
298 else
299 return parent_block->GetInlinedParent();
300 }
301 return NULL;
302}
303
304
305bool
Greg Claytonb04e7a82010-08-24 21:05:24 +0000306Block::GetRangeContainingOffset (const addr_t offset, VMRange &range)
307{
308 uint32_t range_idx = VMRange::FindRangeIndexThatContainsValue (m_ranges, offset);
309 if (range_idx < m_ranges.size())
310 {
311 range = m_ranges[range_idx];
312 return true;
313 }
314 range.Clear();
315 return false;
316}
317
318
319bool
Greg Clayton33ed1702010-08-24 00:45:41 +0000320Block::GetRangeContainingAddress (const Address& addr, AddressRange &range)
321{
322 SymbolContext sc;
323 CalculateSymbolContext(&sc);
324 if (sc.function)
325 {
326 const AddressRange &func_range = sc.function->GetAddressRange();
327 if (addr.GetSection() == func_range.GetBaseAddress().GetSection())
328 {
329 const addr_t addr_offset = addr.GetOffset();
330 const addr_t func_offset = func_range.GetBaseAddress().GetOffset();
331 if (addr_offset >= func_offset && addr_offset < func_offset + func_range.GetByteSize())
332 {
333 addr_t offset = addr_offset - func_offset;
334
335 uint32_t range_idx = VMRange::FindRangeIndexThatContainsValue (m_ranges, offset);
336 if (range_idx < m_ranges.size())
337 {
338 range.GetBaseAddress() = func_range.GetBaseAddress();
339 range.GetBaseAddress().SetOffset(func_offset + m_ranges[range_idx].GetBaseAddress());
340 range.SetByteSize(m_ranges[range_idx].GetByteSize());
341 return true;
342 }
343 }
344 }
345 }
346 range.Clear();
347 return false;
348}
349
Chris Lattner24943d22010-06-08 16:52:24 +0000350void
351Block::AddRange(addr_t start_offset, addr_t end_offset)
352{
353 m_ranges.resize(m_ranges.size()+1);
354 m_ranges.back().Reset(start_offset, end_offset);
355}
356
Chris Lattner24943d22010-06-08 16:52:24 +0000357// Return the current number of bytes that this object occupies in memory
358size_t
359Block::MemorySize() const
360{
361 size_t mem_size = sizeof(Block) + m_ranges.size() * sizeof(VMRange);
362 if (m_inlineInfoSP.get())
363 mem_size += m_inlineInfoSP->MemorySize();
Greg Claytonb04e7a82010-08-24 21:05:24 +0000364 if (m_variable_list_sp.get())
365 mem_size += m_variable_list_sp->MemorySize();
Chris Lattner24943d22010-06-08 16:52:24 +0000366 return mem_size;
367
368}
369
Greg Clayton75ccf502010-08-21 02:22:51 +0000370void
371Block::AddChild(const BlockSP &child_block_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000372{
Greg Clayton75ccf502010-08-21 02:22:51 +0000373 if (child_block_sp)
374 {
375 Block *block_needs_sibling = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000376
Greg Clayton75ccf502010-08-21 02:22:51 +0000377 if (!m_children.empty())
378 block_needs_sibling = m_children.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000379
Greg Clayton75ccf502010-08-21 02:22:51 +0000380 child_block_sp->SetParentScope (this);
381 m_children.push_back (child_block_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000382
Greg Clayton75ccf502010-08-21 02:22:51 +0000383 if (block_needs_sibling)
384 block_needs_sibling->SetSibling (child_block_sp.get());
385 }
Chris Lattner24943d22010-06-08 16:52:24 +0000386}
387
388void
389Block::SetInlinedFunctionInfo(const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
390{
391 m_inlineInfoSP.reset(new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr));
392}
393
Jim Ingham7382a532010-08-18 19:29:16 +0000394
395
396VariableListSP
397Block::GetVariableList (bool get_child_variables, bool can_create)
398{
399 VariableListSP variable_list_sp;
Greg Clayton75ccf502010-08-21 02:22:51 +0000400 if (m_parsed_block_variables == false)
Jim Ingham7382a532010-08-18 19:29:16 +0000401 {
Greg Claytonb04e7a82010-08-24 21:05:24 +0000402 if (m_variable_list_sp.get() == NULL && can_create)
Greg Clayton75ccf502010-08-21 02:22:51 +0000403 {
404 m_parsed_block_variables = true;
405 SymbolContext sc;
406 CalculateSymbolContext(&sc);
407 assert(sc.module_sp);
408 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
409 }
Jim Ingham7382a532010-08-18 19:29:16 +0000410 }
411
Greg Claytonb04e7a82010-08-24 21:05:24 +0000412 if (m_variable_list_sp.get())
Jim Ingham7382a532010-08-18 19:29:16 +0000413 {
414 variable_list_sp.reset(new VariableList());
415 if (variable_list_sp.get())
Greg Claytonb04e7a82010-08-24 21:05:24 +0000416 variable_list_sp->AddVariables(m_variable_list_sp.get());
Jim Ingham7382a532010-08-18 19:29:16 +0000417
418 if (get_child_variables)
419 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000420 for (Block *child_block = GetFirstChild();
421 child_block != NULL;
422 child_block = child_block->GetSibling())
423 {
424 if (child_block->GetInlinedFunctionInfo() == NULL)
425 {
426 VariableListSP child_block_variable_list(child_block->GetVariableList(get_child_variables, can_create));
427 if (child_block_variable_list.get())
428 variable_list_sp->AddVariables(child_block_variable_list.get());
429 }
430
Jim Ingham7382a532010-08-18 19:29:16 +0000431 }
432 }
433 }
434
435 return variable_list_sp;
436}
437
438uint32_t
Greg Clayton33ed1702010-08-24 00:45:41 +0000439Block::AppendVariables
440(
441 bool can_create,
442 bool get_parent_variables,
443 bool stop_if_block_is_inlined_function,
444 VariableList *variable_list
445)
Jim Ingham7382a532010-08-18 19:29:16 +0000446{
447 uint32_t num_variables_added = 0;
448 VariableListSP variable_list_sp(GetVariableList(false, can_create));
449
Greg Clayton69aa5d92010-09-07 04:20:48 +0000450 bool is_inlined_function = GetInlinedFunctionInfo() != NULL;
Jim Ingham7382a532010-08-18 19:29:16 +0000451 if (variable_list_sp.get())
452 {
453 num_variables_added = variable_list_sp->GetSize();
454 variable_list->AddVariables(variable_list_sp.get());
455 }
Greg Clayton33ed1702010-08-24 00:45:41 +0000456
Jim Ingham7382a532010-08-18 19:29:16 +0000457 if (get_parent_variables)
458 {
Greg Clayton33ed1702010-08-24 00:45:41 +0000459 if (stop_if_block_is_inlined_function && is_inlined_function)
460 return num_variables_added;
461
Jim Ingham7382a532010-08-18 19:29:16 +0000462 Block* parent_block = GetParent();
463 if (parent_block)
Greg Clayton33ed1702010-08-24 00:45:41 +0000464 num_variables_added += parent_block->AppendVariables (can_create, get_parent_variables, stop_if_block_is_inlined_function, variable_list);
Jim Ingham7382a532010-08-18 19:29:16 +0000465 }
466 return num_variables_added;
467}
468
Chris Lattner24943d22010-06-08 16:52:24 +0000469void
Greg Clayton75ccf502010-08-21 02:22:51 +0000470Block::SetBlockInfoHasBeenParsed (bool b, bool set_children)
Chris Lattner24943d22010-06-08 16:52:24 +0000471{
Greg Clayton75ccf502010-08-21 02:22:51 +0000472 m_parsed_block_info = b;
473 if (set_children)
Chris Lattner24943d22010-06-08 16:52:24 +0000474 {
Greg Clayton75ccf502010-08-21 02:22:51 +0000475 m_parsed_child_blocks = true;
476 for (Block *child_block = GetFirstChild(); child_block != NULL; child_block = child_block->GetSibling())
477 child_block->SetBlockInfoHasBeenParsed (b, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000478 }
Chris Lattner24943d22010-06-08 16:52:24 +0000479}