blob: 3a132f68f292c4195c1c97a5a4012160172291b0 [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
20Block::Block(user_id_t uid, uint32_t depth, BlockList* blocks) :
21 UserID(uid),
22 m_block_list(blocks),
23 m_depth(depth),
24 m_ranges(),
25 m_inlineInfoSP(),
26 m_variables()
27{
28}
29
30Block::Block(const Block& rhs) :
31 UserID(rhs),
32 m_block_list(rhs.m_block_list),
33 m_depth(rhs.m_depth),
34 m_ranges(rhs.m_ranges),
35 m_inlineInfoSP(rhs.m_inlineInfoSP),
36 m_variables(rhs.m_variables)
37{
38}
39
40const Block&
41Block::operator= (const Block& rhs)
42{
43 if (this != &rhs)
44 {
45 UserID::operator= (rhs);
46 m_block_list = rhs.m_block_list;
47 m_depth = rhs.m_depth;
48 m_ranges = rhs.m_ranges;
49 m_inlineInfoSP = rhs.m_inlineInfoSP;
50 m_variables = rhs.m_variables;
51 }
52 return *this;
53}
54
55Block::~Block ()
56{
57}
58
59void
Greg Clayton12bec712010-06-28 21:30:43 +000060Block::GetDescription(Stream *s, lldb::DescriptionLevel level, Process *process) const
61{
62 size_t num_ranges = m_ranges.size();
63 if (num_ranges)
64 {
65
66 addr_t base_addr = LLDB_INVALID_ADDRESS;
67 if (process)
68 base_addr = m_block_list->GetAddressRange().GetBaseAddress().GetLoadAddress(process);
69 if (base_addr == LLDB_INVALID_ADDRESS)
70 base_addr = m_block_list->GetAddressRange().GetBaseAddress().GetFileAddress();
71
72 s->Printf("range%s = ", num_ranges > 1 ? "s" : "");
73 std::vector<VMRange>::const_iterator pos, end = m_ranges.end();
74 for (pos = m_ranges.begin(); pos != end; ++pos)
75 pos->Dump(s, base_addr, 4);
76 }
77 *s << ", id = " << ((const UserID&)*this);
78
79 if (m_inlineInfoSP.get() != NULL)
80 m_inlineInfoSP->Dump(s);
81}
82
83void
Chris Lattner24943d22010-06-08 16:52:24 +000084Block::Dump(Stream *s, addr_t base_addr, int32_t depth, bool show_context) const
85{
86 if (depth < 0)
87 {
88 // We have a depth that is less than zero, print our parent blocks
89 // first
90 m_block_list->Dump(s, GetParentUID(), depth + 1, show_context);
91 }
92
93 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
94 s->Indent();
95 *s << "Block" << ((const UserID&)*this);
Greg Clayton12bec712010-06-28 21:30:43 +000096 const Block* parent_block = GetParent();
97 if (parent_block)
Chris Lattner24943d22010-06-08 16:52:24 +000098 {
Greg Clayton12bec712010-06-28 21:30:43 +000099 s->Printf(", parent = {0x%8.8x}", parent_block->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000100 }
101 if (m_inlineInfoSP.get() != NULL)
102 m_inlineInfoSP->Dump(s);
103
104 if (!m_ranges.empty())
105 {
106 *s << ", ranges =";
107 std::vector<VMRange>::const_iterator pos;
108 std::vector<VMRange>::const_iterator end = m_ranges.end();
109 for (pos = m_ranges.begin(); pos != end; ++pos)
110 {
111 if (parent_block != NULL && parent_block->Contains(*pos) == false)
112 *s << '!';
113 else
114 *s << ' ';
115 pos->Dump(s, base_addr);
116 }
117 }
118 s->EOL();
119
120 if (depth > 0)
121 {
122 s->IndentMore();
123
124 if (m_variables.get())
125 {
126 m_variables->Dump(s, show_context);
127 }
128
129 uint32_t blockID = m_block_list->GetFirstChild(GetID());
130 while (blockID != Block::InvalidID)
131 {
132 m_block_list->Dump(s, blockID, depth - 1, show_context);
133
134 blockID = m_block_list->GetSibling(blockID);
135 }
136
137 s->IndentLess();
138 }
139
140}
141
142
143void
144Block::CalculateSymbolContext(SymbolContext* sc)
145{
146 sc->block = this;
147 m_block_list->GetFunction()->CalculateSymbolContext(sc);
148}
149
150void
151Block::DumpStopContext (Stream *s, const SymbolContext *sc)
152{
Greg Clayton12bec712010-06-28 21:30:43 +0000153 Block* parent_block = GetParent();
Chris Lattner24943d22010-06-08 16:52:24 +0000154
155 InlineFunctionInfo* inline_info = InlinedFunctionInfo ();
156 if (inline_info)
157 {
158 const Declaration &call_site = inline_info->GetCallSite();
159 if (sc)
160 {
161 // First frame, dump the first inline call site
162// if (call_site.IsValid())
163// {
164// s->PutCString(" at ");
165// call_site.DumpStopContext (s);
166// }
167 s->PutCString (" [inlined]");
168 }
169 s->EOL();
170 inline_info->DumpStopContext (s);
171 if (sc == NULL)
172 {
173 if (call_site.IsValid())
174 {
175 s->PutCString(" at ");
176 call_site.DumpStopContext (s);
177 }
178 }
179 }
180
181 if (sc)
182 {
183 // If we have any inlined functions, this will be the deepest most
184 // inlined location
185 if (sc->line_entry.IsValid())
186 {
187 s->PutCString(" at ");
188 sc->line_entry.DumpStopContext (s);
189 }
190 }
191 if (parent_block)
192 parent_block->Block::DumpStopContext (s, NULL);
193}
194
195
196void
197Block::DumpSymbolContext(Stream *s)
198{
199 m_block_list->GetFunction()->DumpSymbolContext(s);
200 s->Printf(", Block{0x%8.8x}", GetID());
201}
202
203bool
204Block::Contains (addr_t range_offset) const
205{
206 return VMRange::ContainsValue(m_ranges, range_offset);
207}
208
209bool
210Block::Contains (const VMRange& range) const
211{
212 return VMRange::ContainsRange(m_ranges, range);
213}
214
215
216
217bool
218BlockList::BlockContainsBlockWithID (const user_id_t block_id, const user_id_t find_block_id) const
219{
220 if (block_id == Block::InvalidID)
221 return false;
222
223 if (block_id == find_block_id)
224 return true;
225 else
226 {
227 user_id_t child_block_id = GetFirstChild(block_id);
228 while (child_block_id != Block::InvalidID)
229 {
230 if (BlockContainsBlockWithID (child_block_id, find_block_id))
231 return true;
232 child_block_id = GetSibling(child_block_id);
233 }
234 }
235
236 return false;
237}
238
239bool
240Block::ContainsBlockWithID (user_id_t block_id) const
241{
242 return m_block_list->BlockContainsBlockWithID (GetID(), block_id);
243}
244
245
246void
247Block::AddRange(addr_t start_offset, addr_t end_offset)
248{
249 m_ranges.resize(m_ranges.size()+1);
250 m_ranges.back().Reset(start_offset, end_offset);
251}
252
253InlineFunctionInfo*
254Block::InlinedFunctionInfo ()
255{
256 return m_inlineInfoSP.get();
257}
258
259const InlineFunctionInfo*
260Block::InlinedFunctionInfo () const
261{
262 return m_inlineInfoSP.get();
263}
264
265// Return the current number of bytes that this object occupies in memory
266size_t
267Block::MemorySize() const
268{
269 size_t mem_size = sizeof(Block) + m_ranges.size() * sizeof(VMRange);
270 if (m_inlineInfoSP.get())
271 mem_size += m_inlineInfoSP->MemorySize();
272 if (m_variables.get())
273 mem_size += m_variables->MemorySize();
274 return mem_size;
275
276}
277
Greg Clayton12bec712010-06-28 21:30:43 +0000278Block *
279Block::GetParent () const
280{
281 return m_block_list->GetBlockByID (m_block_list->GetParent(GetID()));
282}
283
284Block *
285Block::GetSibling () const
286{
287 return m_block_list->GetBlockByID (m_block_list->GetSibling(GetID()));
288}
289
290Block *
291Block::GetFirstChild () const
292{
293 return m_block_list->GetBlockByID (m_block_list->GetFirstChild(GetID()));
294}
295
Chris Lattner24943d22010-06-08 16:52:24 +0000296user_id_t
297Block::GetParentUID() const
298{
299 return m_block_list->GetParent(GetID());
300}
301
302user_id_t
303Block::GetSiblingUID() const
304{
305 return m_block_list->GetSibling(GetID());
306}
307
308user_id_t
309Block::GetFirstChildUID() const
310{
311 return m_block_list->GetFirstChild(GetID());
312}
313
314user_id_t
315Block::AddChild(user_id_t userID)
316{
317 return m_block_list->AddChild(GetID(), userID);
318}
319
320void
321Block::SetInlinedFunctionInfo(const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
322{
323 m_inlineInfoSP.reset(new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr));
324}
325
Jim Ingham7382a532010-08-18 19:29:16 +0000326
327
328VariableListSP
329Block::GetVariableList (bool get_child_variables, bool can_create)
330{
331 VariableListSP variable_list_sp;
332 if (m_variables.get() == NULL && can_create)
333 {
334 SymbolContext sc;
335 CalculateSymbolContext(&sc);
336 assert(sc.module_sp);
337 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
338 }
339
340 if (m_variables.get())
341 {
342 variable_list_sp.reset(new VariableList());
343 if (variable_list_sp.get())
344 variable_list_sp->AddVariables(m_variables.get());
345
346 if (get_child_variables)
347 {
348 Block *child_block = GetFirstChild();
349 while (child_block)
350 {
351 VariableListSP child_block_variable_list(child_block->GetVariableList(get_child_variables, can_create));
352 if (child_block_variable_list.get())
353 variable_list_sp->AddVariables(child_block_variable_list.get());
354 child_block = child_block->GetSibling();
355 }
356 }
357 }
358
359 return variable_list_sp;
360}
361
362uint32_t
363Block::AppendVariables (bool can_create, bool get_parent_variables, VariableList *variable_list)
364{
365 uint32_t num_variables_added = 0;
366 VariableListSP variable_list_sp(GetVariableList(false, can_create));
367
368 if (variable_list_sp.get())
369 {
370 num_variables_added = variable_list_sp->GetSize();
371 variable_list->AddVariables(variable_list_sp.get());
372 }
373
374 if (get_parent_variables)
375 {
376 Block* parent_block = GetParent();
377 if (parent_block)
378 num_variables_added += parent_block->AppendVariables (can_create, get_parent_variables, variable_list);
379 }
380 return num_variables_added;
381}
382
383
384void
385Block::SetVariableList(VariableListSP& variables)
386{
387 m_variables = variables;
388}
389
390uint32_t
391Block::Depth () const
392{
393 return m_depth;
394}
395
Chris Lattner24943d22010-06-08 16:52:24 +0000396BlockList::BlockList(Function *function, const AddressRange& range) :
397 m_function(function),
398 m_range(range),
399 m_blocks()
400{
401}
402
403BlockList::~BlockList()
404{
405}
406
407AddressRange &
408BlockList::GetAddressRange()
409{
410 return m_range;
411}
412
413const AddressRange &
414BlockList::GetAddressRange() const
415{
416 return m_range;
417}
418
419void
420BlockList::Dump(Stream *s, user_id_t blockID, uint32_t depth, bool show_context) const
421{
422 const Block* block = GetBlockByID(blockID);
423 if (block)
424 block->Dump(s, m_range.GetBaseAddress().GetFileAddress(), depth, show_context);
425}
426
427Function *
428BlockList::GetFunction()
429{
430 return m_function;
431}
432
433
434const Function *
435BlockList::GetFunction() const
436{
437 return m_function;
438}
439
440user_id_t
441BlockList::GetParent(user_id_t blockID) const
442{
443 collection::const_iterator end = m_blocks.end();
444 collection::const_iterator begin = m_blocks.begin();
445 collection::const_iterator pos = std::find_if(begin, end, UserID::IDMatches(blockID));
446
447 if (pos != end && pos != begin && pos->Depth() > 0)
448 {
449 const uint32_t parent_depth = pos->Depth() - 1;
450
451 while (--pos >= begin)
452 {
453 if (pos->Depth() == parent_depth)
454 return pos->GetID();
455 }
456 }
457 return Block::InvalidID;
458}
459
460user_id_t
461BlockList::GetSibling(user_id_t blockID) const
462{
463 collection::const_iterator end = m_blocks.end();
464 collection::const_iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));
465
466 if (pos != end)
467 {
468 const uint32_t sibling_depth = pos->Depth();
469 while (++pos != end)
470 {
471 uint32_t depth = pos->Depth();
472 if (depth == sibling_depth)
473 return pos->GetID();
474 if (depth < sibling_depth)
475 break;
476 }
477 }
478 return Block::InvalidID;
479}
480
481user_id_t
482BlockList::GetFirstChild(user_id_t blockID) const
483{
484 if (!m_blocks.empty())
485 {
486 if (blockID == Block::RootID)
487 {
488 return m_blocks.front().GetID();
489 }
490 else
491 {
492 collection::const_iterator end = m_blocks.end();
493 collection::const_iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));
494
495 if (pos != end)
496 {
497 collection::const_iterator child_pos = pos + 1;
498 if (child_pos != end)
499 {
500 if (child_pos->Depth() == pos->Depth() + 1)
501 return child_pos->GetID();
502 }
503 }
504 }
505 }
506 return Block::InvalidID;
507}
508
509
510// Return the current number of bytes that this object occupies in memory
511size_t
512BlockList::MemorySize() const
513{
514 size_t mem_size = sizeof(BlockList);
515
516 collection::const_iterator pos, end = m_blocks.end();
517 for (pos = m_blocks.begin(); pos != end; ++pos)
518 mem_size += pos->MemorySize(); // Each block can vary in size
519
520 return mem_size;
521
522}
523
524user_id_t
525BlockList::AddChild (user_id_t parentID, user_id_t childID)
526{
527 bool added = false;
528 if (parentID == Block::RootID)
529 {
530 assert(m_blocks.empty());
531 Block block(childID, 0, this);
532 m_blocks.push_back(block);
533 added = true;
534 }
535 else
536 {
537 collection::iterator end = m_blocks.end();
538 collection::iterator parent_pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(parentID));
539 assert(parent_pos != end);
540 if (parent_pos != end)
541 {
542 const uint32_t parent_sibling_depth = parent_pos->Depth();
543
544 collection::iterator insert_pos = parent_pos;
545 collection::iterator prev_sibling = end;
546 while (++insert_pos != end)
547 {
548 if (insert_pos->Depth() <= parent_sibling_depth)
549 break;
550 }
551
552 Block child_block(childID, parent_pos->Depth() + 1, this);
553 collection::iterator child_pos = m_blocks.insert(insert_pos, child_block);
554 added = true;
555 }
556 }
557 if (added)
558 return childID;
559 return Block::InvalidID;
560}
561
562const Block *
563BlockList::GetBlockByID(user_id_t blockID) const
564{
Greg Clayton12bec712010-06-28 21:30:43 +0000565 if (m_blocks.empty() || blockID == Block::InvalidID)
Chris Lattner24943d22010-06-08 16:52:24 +0000566 return NULL;
567
568 if (blockID == Block::RootID)
569 blockID = m_blocks.front().GetID();
570
571 collection::const_iterator end = m_blocks.end();
572 collection::const_iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));
573 if (pos != end)
574 return &(*pos);
575 return NULL;
576}
577
578Block *
579BlockList::GetBlockByID(user_id_t blockID)
580{
Greg Clayton12bec712010-06-28 21:30:43 +0000581 if (m_blocks.empty() || blockID == Block::InvalidID)
Chris Lattner24943d22010-06-08 16:52:24 +0000582 return NULL;
583
584 if (blockID == Block::RootID)
585 blockID = m_blocks.front().GetID();
586
587 collection::iterator end = m_blocks.end();
588 collection::iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));
589 if (pos != end)
590 return &(*pos);
591 return NULL;
592}
593
594bool
595BlockList::AddRange(user_id_t blockID, addr_t start_offset, addr_t end_offset)
596{
597 Block *block = GetBlockByID(blockID);
598
599 if (block)
600 {
601 block->AddRange(start_offset, end_offset);
602 return true;
603 }
604 return false;
605}
606//
607//const Block *
608//BlockList::FindDeepestBlockForAddress (const Address &addr)
609//{
610// if (m_range.Contains(addr))
611// {
612// addr_t block_offset = addr.GetFileAddress() - m_range.GetBaseAddress().GetFileAddress();
613// collection::const_iterator pos, end = m_blocks.end();
614// collection::const_iterator deepest_match_pos = end;
615// for (pos = m_blocks.begin(); pos != end; ++pos)
616// {
617// if (pos->Contains (block_offset))
618// {
619// if (deepest_match_pos == end || deepest_match_pos->Depth() < pos->Depth())
620// deepest_match_pos = pos;
621// }
622// }
623// if (deepest_match_pos != end)
624// return &(*deepest_match_pos);
625// }
626// return NULL;
627//}
628//
629bool
630BlockList::SetInlinedFunctionInfo(user_id_t blockID, const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
631{
632 Block *block = GetBlockByID(blockID);
633
634 if (block)
635 {
636 block->SetInlinedFunctionInfo(name, mangled, decl_ptr, call_decl_ptr);
637 return true;
638 }
639 return false;
640}
641
642VariableListSP
643BlockList::GetVariableList(user_id_t blockID, bool get_child_variables, bool can_create)
644{
645 VariableListSP variable_list_sp;
646 Block *block = GetBlockByID(blockID);
647 if (block)
648 variable_list_sp = block->GetVariableList(get_child_variables, can_create);
649 return variable_list_sp;
650}
651
652bool
653BlockList::IsEmpty() const
654{
655 return m_blocks.empty();
656}
657
658
659
660bool
661BlockList::SetVariableList(user_id_t blockID, VariableListSP& variables)
662{
663 Block *block = GetBlockByID(blockID);
664 if (block)
665 {
666 block->SetVariableList(variables);
667 return true;
668 }
669 return false;
670
671}