blob: 65bf64fc06bf3e5158e73db053331e1301b6a7fe [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DWARFCompileUnit.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 "DWARFCompileUnit.h"
11
12#include "lldb/Core/Stream.h"
13#include "lldb/Core/Timer.h"
14
15#include "DWARFDebugAbbrev.h"
16#include "DWARFDebugAranges.h"
Greg Clayton12bec712010-06-28 21:30:43 +000017#include "DWARFDebugInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "DWARFDIECollection.h"
19#include "DWARFFormValue.h"
20#include "LogChannelDWARF.h"
21#include "SymbolFileDWARF.h"
22
23using namespace lldb_private;
24using namespace std;
25
26extern int g_verbose;
27
28DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* m_dwarf2Data) :
29 m_dwarf2Data ( m_dwarf2Data ),
30 m_offset ( DW_INVALID_OFFSET ),
31 m_length ( 0 ),
32 m_version ( 0 ),
33 m_abbrevs ( NULL ),
34 m_addr_size ( DWARFCompileUnit::GetDefaultAddressSize() ),
35 m_base_addr ( 0 ),
36 m_die_array (),
37 m_aranges_ap (),
38 m_user_data ( NULL )
39{
40}
41
42void
43DWARFCompileUnit::Clear()
44{
45 m_offset = DW_INVALID_OFFSET;
46 m_length = 0;
47 m_version = 0;
48 m_abbrevs = NULL;
49 m_addr_size = DWARFCompileUnit::GetDefaultAddressSize();
50 m_base_addr = 0;
51 m_die_array.clear();
52 m_aranges_ap.reset();
53 m_user_data = NULL;
54}
55
56bool
57DWARFCompileUnit::Extract(const DataExtractor &debug_info, uint32_t* offset_ptr)
58{
59 Clear();
60
61 m_offset = *offset_ptr;
62
63 if (debug_info.ValidOffset(*offset_ptr))
64 {
65 dw_offset_t abbr_offset;
66 const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
67 m_length = debug_info.GetU32(offset_ptr);
68 m_version = debug_info.GetU16(offset_ptr);
69 abbr_offset = debug_info.GetU32(offset_ptr);
70 m_addr_size = debug_info.GetU8 (offset_ptr);
71
72 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
73 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
74 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
75 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
76
77 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL)
78 {
79 m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
80 return true;
81 }
82
83 // reset the offset to where we tried to parse from if anything went wrong
84 *offset_ptr = m_offset;
85 }
86
87 return false;
88}
89
90
91dw_offset_t
92DWARFCompileUnit::Extract(dw_offset_t offset, const DataExtractor& debug_info_data, const DWARFAbbreviationDeclarationSet* abbrevs)
93{
94 Clear();
95
96 m_offset = offset;
97
98 if (debug_info_data.ValidOffset(offset))
99 {
100 m_length = debug_info_data.GetU32(&offset);
101 m_version = debug_info_data.GetU16(&offset);
102 bool abbrevs_OK = debug_info_data.GetU32(&offset) == abbrevs->GetOffset();
103 m_abbrevs = abbrevs;
104 m_addr_size = debug_info_data.GetU8 (&offset);
105
106 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
107 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
108
109 if (version_OK && addr_size_OK && abbrevs_OK && debug_info_data.ValidOffset(offset))
110 return offset;
111 }
112 return DW_INVALID_OFFSET;
113}
114
115void
116DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die)
117{
118 if (m_die_array.size() > 1)
119 {
120 // std::vectors never get any smaller when resized to a smaller size,
121 // or when clear() or erase() are called, the size will report that it
122 // is smaller, but the memory allocated remains intact (call capacity()
123 // to see this). So we need to create a temporary vector and swap the
124 // contents which will cause just the internal pointers to be swapped
125 // so that when "tmp_array" goes out of scope, it will destroy the
126 // contents.
127
128 // Save at least the compile unit DIE
129 DWARFDebugInfoEntry::collection tmp_array;
130 m_die_array.swap(tmp_array);
131 if (keep_compile_unit_die)
132 m_die_array.push_back(tmp_array.front());
133 }
134}
135
136//----------------------------------------------------------------------
137// ParseCompileUnitDIEsIfNeeded
138//
139// Parses a compile unit and indexes its DIEs if it already hasn't been
140// done.
141//----------------------------------------------------------------------
142size_t
143DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only)
144{
145 const size_t initial_die_array_size = m_die_array.size();
146 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
147 return 0; // Already parsed
148
149 Timer scoped_timer (__PRETTY_FUNCTION__,
150 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
151 m_offset,
152 cu_die_only);
153
154 // Set the offset to that of the first DIE
155 uint32_t offset = GetFirstDIEOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000156 DWARFDebugInfoEntry die;
157 // Keep a flat array of the DIE for binary lookup by DIE offset
158 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
159// if (log)
160// log->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x",
161// cu->GetOffset(),
162// cu->GetLength(),
163// cu->GetVersion(),
164// cu->GetAbbrevOffset(),
165// cu->GetAddressByteSize());
166
167 uint32_t depth = 0;
168 // We are in our compile unit, parse starting at the offset
169 // we were told to parse
170 while (die.Extract(m_dwarf2Data, this, &offset))
171 {
172 if (log)
173 log->Printf("0x%8.8x: %*.*s%s%s",
174 die.GetOffset(),
175 depth * 2, depth * 2, "",
176 DW_TAG_value_to_name (die.Tag()),
177 die.HasChildren() ? " *" : "");
178 if (cu_die_only)
179 {
180 AddDIE(die);
181 return 1;
182 }
183 else if (depth == 0 && initial_die_array_size == 1)
184 {
185 // Don't append the CU die as we already did that
186 }
187 else
188 {
189 AddDIE(die);
190 }
191
192 const DWARFAbbreviationDeclaration* abbrDecl = die.GetAbbreviationDeclarationPtr();
193 if (abbrDecl)
194 {
195 // Normal DIE
196 if (abbrDecl->HasChildren())
197 ++depth;
198 }
199 else
200 {
201 // NULL DIE.
202 if (depth > 0)
203 --depth;
204 else
205 break; // We are done with this compile unit!
206 }
207
Greg Clayton54e7afa2010-07-09 20:39:50 +0000208 assert(offset <= GetNextCompileUnitOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000209 }
210 SetDIERelations();
211 return m_die_array.size();
212}
213
214
215dw_offset_t
216DWARFCompileUnit::GetAbbrevOffset() const
217{
218 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
219}
220
221
222
223bool
224DWARFCompileUnit::Verify(Stream *s) const
225{
226 const DataExtractor& debug_info = m_dwarf2Data->get_debug_info_data();
227 bool valid_offset = debug_info.ValidOffset(m_offset);
228 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
229 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
230 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
231 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
232 bool verbose = s->GetVerbose();
233 if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK)
234 {
235 if (verbose)
236 s->Printf(" 0x%8.8x: OK\n", m_offset);
237 return true;
238 }
239 else
240 {
241 s->Printf(" 0x%8.8x: ", m_offset);
242
243 m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
244 s->EOL();
245 if (valid_offset)
246 {
247 if (!length_OK)
248 s->Printf(" The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length);
249 if (!version_OK)
250 s->Printf(" The 16 bit compile unit header version is not supported.\n");
251 if (!abbr_offset_OK)
252 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset());
253 if (!addr_size_OK)
254 s->Printf(" The address size is unsupported: 0x%2.2x\n", m_addr_size);
255 }
256 else
257 s->Printf(" The start offset of the compile unit header in the .debug_info is invalid.\n");
258 }
259 return false;
260}
261
262
263void
264DWARFCompileUnit::Dump(Stream *s) const
265{
266 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n",
267 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset());
268}
269
270
271static uint8_t g_default_addr_size = 4;
272
273uint8_t
274DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu)
275{
276 if (cu)
277 return cu->GetAddressByteSize();
278 return DWARFCompileUnit::GetDefaultAddressSize();
279}
280
281uint8_t
282DWARFCompileUnit::GetDefaultAddressSize()
283{
284 return g_default_addr_size;
285}
286
287void
288DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size)
289{
290 g_default_addr_size = addr_size;
291}
292
293bool
294DWARFCompileUnit::LookupAddress
295(
296 const dw_addr_t address,
297 DWARFDebugInfoEntry** function_die_handle,
298 DWARFDebugInfoEntry** block_die_handle
299)
300{
301 bool success = false;
302
303 if (function_die_handle != NULL && DIE())
304 {
305 if (m_aranges_ap.get() == NULL)
306 {
307 m_aranges_ap.reset(new DWARFDebugAranges());
308 m_die_array.front().BuildFunctionAddressRangeTable(m_dwarf2Data, this, m_aranges_ap.get());
309 }
310
311 // Re-check the aranges auto pointer contents in case it was created above
312 if (m_aranges_ap.get() != NULL)
313 {
314 *function_die_handle = GetDIEPtr(m_aranges_ap->FindAddress(address));
315 if (*function_die_handle != NULL)
316 {
317 success = true;
318 if (block_die_handle != NULL)
319 {
320 DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild();
321 while (child)
322 {
323 if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle))
324 break;
325 child = child->GetSibling();
326 }
327 }
328 }
329 }
330 }
331 return success;
332}
333
334//----------------------------------------------------------------------
335// SetDIERelations()
336//
337// We read in all of the DIE entries into our flat list of DIE entries
338// and now we need to go back through all of them and set the parent,
339// sibling and child pointers for quick DIE navigation.
340//----------------------------------------------------------------------
341void
342DWARFCompileUnit::SetDIERelations()
343{
344#if 0
345 // Compute average bytes per DIE
346 //
347 // We can figure out what the average number of bytes per DIE is
348 // to help us pre-allocate the correct number of m_die_array
349 // entries so we don't end up doing a lot of memory copies as we
350 // are creating our DIE array when parsing
351 //
352 // Enable this code by changing "#if 0" above to "#if 1" and running
353 // the dsymutil or dwarfdump with a bunch of dwarf files and see what
354 // the running average ends up being in the stdout log.
355 static size_t g_total_cu_debug_info_size = 0;
356 static size_t g_total_num_dies = 0;
357 static size_t g_min_bytes_per_die = UINT_MAX;
358 static size_t g_max_bytes_per_die = 0;
359 const size_t num_dies = m_die_array.size();
360 const size_t cu_debug_info_size = GetDebugInfoSize();
361 const size_t bytes_per_die = cu_debug_info_size / num_dies;
362 if (g_min_bytes_per_die > bytes_per_die)
363 g_min_bytes_per_die = bytes_per_die;
364 if (g_max_bytes_per_die < bytes_per_die)
365 g_max_bytes_per_die = bytes_per_die;
366 if (g_total_cu_debug_info_size == 0)
367 {
368 cout << " min max avg" << endl
369 << "n dies cu size bpd bpd bpd bpd" << endl
370 << "------ -------- --- === === ===" << endl;
371 }
372 g_total_cu_debug_info_size += cu_debug_info_size;
373 g_total_num_dies += num_dies;
374 const size_t avg_bytes_per_die = g_total_cu_debug_info_size / g_total_num_dies;
375 cout
376 << DECIMAL_WIDTH(6) << num_dies << ' '
377 << DECIMAL_WIDTH(8) << cu_debug_info_size << ' '
378 << DECIMAL_WIDTH(3) << bytes_per_die << ' '
379 << DECIMAL_WIDTH(3) << g_min_bytes_per_die << ' '
380 << DECIMAL_WIDTH(3) << g_max_bytes_per_die << ' '
381 << DECIMAL_WIDTH(3) << avg_bytes_per_die
382 << endl;
383#endif
384 if (m_die_array.empty())
385 return;
386 DWARFDebugInfoEntry* die_array_begin = &m_die_array.front();
387 DWARFDebugInfoEntry* die_array_end = &m_die_array.back();
388 DWARFDebugInfoEntry* curr_die;
389 // We purposely are skipping the last element in the array in the loop below
390 // so that we can always have a valid next item
391 for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die)
392 {
393 // Since our loop doesn't include the last element, we can always
394 // safely access the next die in the array.
395 DWARFDebugInfoEntry* next_die = curr_die + 1;
396
397 const DWARFAbbreviationDeclaration* curr_die_abbrev = curr_die->GetAbbreviationDeclarationPtr();
398
399 if (curr_die_abbrev)
400 {
401 // Normal DIE
402 if (curr_die_abbrev->HasChildren())
403 next_die->SetParent(curr_die);
404 else
405 curr_die->SetSibling(next_die);
406 }
407 else
408 {
409 // NULL DIE that terminates a sibling chain
410 DWARFDebugInfoEntry* parent = curr_die->GetParent();
411 if (parent)
412 parent->SetSibling(next_die);
413 }
414 }
415
416 // Since we skipped the last element, we need to fix it up!
417 if (die_array_begin < die_array_end)
418 curr_die->SetParent(die_array_begin);
419
420#if 0
421 // The code below will dump the DIE relations in case any modification
422 // is done to the above code. This dump can be used in a diff to make
423 // sure that no functionality is lost.
424 {
425 DWARFDebugInfoEntry::const_iterator pos;
426 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
427 puts("offset parent sibling child");
428 puts("-------- -------- -------- --------");
429 for (pos = m_die_array.begin(); pos != end; ++pos)
430 {
431 const DWARFDebugInfoEntry& die_ref = *pos;
432 const DWARFDebugInfoEntry* p = die_ref.GetParent();
433 const DWARFDebugInfoEntry* s = die_ref.GetSibling();
434 const DWARFDebugInfoEntry* c = die_ref.GetFirstChild();
435 printf("%.8x: %.8x %.8x %.8x\n", die_ref.GetOffset(),
436 p ? p->GetOffset() : 0,
437 s ? s->GetOffset() : 0,
438 c ? c->GetOffset() : 0);
439 }
440 }
441#endif
442
443}
444//----------------------------------------------------------------------
445// Compare function DWARFDebugAranges::Range structures
446//----------------------------------------------------------------------
447static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
448{
449 return die1.GetOffset() < die2.GetOffset();
450}
451
452//----------------------------------------------------------------------
453// GetDIEPtr()
454//
455// Get the DIE (Debug Information Entry) with the specified offset.
456//----------------------------------------------------------------------
457DWARFDebugInfoEntry*
458DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset)
459{
460 if (die_offset != DW_INVALID_OFFSET)
461 {
462 ExtractDIEsIfNeeded (false);
463 DWARFDebugInfoEntry compare_die;
464 compare_die.SetOffset(die_offset);
465 DWARFDebugInfoEntry::iterator end = m_die_array.end();
466 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
467 if (pos != end)
468 {
469 if (die_offset == (*pos).GetOffset())
470 return &(*pos);
471 }
472 }
473 return NULL; // Not found in any compile units
474}
475
476//----------------------------------------------------------------------
477// GetDIEPtrContainingOffset()
478//
479// Get the DIE (Debug Information Entry) that contains the specified
480// .debug_info offset.
481//----------------------------------------------------------------------
482const DWARFDebugInfoEntry*
483DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset)
484{
485 if (die_offset != DW_INVALID_OFFSET)
486 {
487 ExtractDIEsIfNeeded (false);
488 DWARFDebugInfoEntry compare_die;
489 compare_die.SetOffset(die_offset);
490 DWARFDebugInfoEntry::iterator end = m_die_array.end();
491 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
492 if (pos != end)
493 {
494 if (die_offset >= (*pos).GetOffset())
495 {
496 DWARFDebugInfoEntry::iterator next = pos + 1;
497 if (next != end)
498 {
499 if (die_offset < (*next).GetOffset())
500 return &(*pos);
501 }
502 }
503 }
504 }
505 return NULL; // Not found in any compile units
506}
507
508
509
510size_t
511DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const
512{
513 size_t old_size = dies.Size();
514 DWARFDebugInfoEntry::const_iterator pos;
515 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
516 for (pos = m_die_array.begin(); pos != end; ++pos)
517 {
518 if (pos->Tag() == tag)
519 dies.Insert(&(*pos));
520 }
521
522 // Return the number of DIEs added to the collection
523 return dies.Size() - old_size;
524}
525
526void
527DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
528{
529 m_global_die_indexes.push_back (die_idx);
530}
531
532
533void
534DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
535{
536 // Indexes to all file level global and static variables
537 m_global_die_indexes;
538
539 if (m_die_array.empty())
540 return;
541
542 const DWARFDebugInfoEntry* first_die = &m_die_array[0];
543 const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
544 if (first_die <= die && die < end)
545 m_global_die_indexes.push_back (die - first_die);
546}
547
548
549void
550DWARFCompileUnit::Index
551(
Greg Clayton12bec712010-06-28 21:30:43 +0000552 lldb_private::UniqueCStringMap<dw_offset_t>& base_name_to_function_die,
553 lldb_private::UniqueCStringMap<dw_offset_t>& full_name_to_function_die,
554 lldb_private::UniqueCStringMap<dw_offset_t>& method_name_to_function_die,
555 lldb_private::UniqueCStringMap<dw_offset_t>& selector_name_to_function_die,
556 lldb_private::UniqueCStringMap<dw_offset_t>& name_to_type_die,
557 lldb_private::UniqueCStringMap<dw_offset_t>& name_to_global_die
Chris Lattner24943d22010-06-08 16:52:24 +0000558)
559{
Chris Lattner24943d22010-06-08 16:52:24 +0000560 const DataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data();
561
562 DWARFDebugInfoEntry::const_iterator pos;
563 DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin();
564 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
565 for (pos = begin; pos != end; ++pos)
566 {
567 const DWARFDebugInfoEntry &die = *pos;
568
569 const dw_tag_t tag = die.Tag();
570
571 switch (tag)
572 {
573 case DW_TAG_subprogram:
574 case DW_TAG_inlined_subroutine:
575 case DW_TAG_base_type:
576 case DW_TAG_class_type:
577 case DW_TAG_constant:
578 case DW_TAG_enumeration_type:
579 case DW_TAG_string_type:
580 case DW_TAG_subroutine_type:
581 case DW_TAG_structure_type:
582 case DW_TAG_union_type:
583 case DW_TAG_typedef:
584 case DW_TAG_namespace:
585 case DW_TAG_variable:
586 break;
587
588 default:
589 continue;
590 }
591
592 DWARFDebugInfoEntry::Attributes attributes;
593 const char *name = NULL;
594 const char *mangled = NULL;
595 bool is_variable = false;
596 bool is_declaration = false;
597 bool is_artificial = false;
598 bool has_address = false;
599 bool has_location = false;
600 bool is_global_or_static_variable = false;
Greg Clayton12bec712010-06-28 21:30:43 +0000601 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
Chris Lattner24943d22010-06-08 16:52:24 +0000602 const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, attributes);
603 if (num_attributes > 0)
604 {
605 uint32_t i;
606
607 dw_tag_t tag = die.Tag();
608
609 is_variable = tag == DW_TAG_variable;
610
611 for (i=0; i<num_attributes; ++i)
612 {
613 dw_attr_t attr = attributes.AttributeAtIndex(i);
614 DWARFFormValue form_value;
615 switch (attr)
616 {
617 case DW_AT_name:
618 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
619 name = form_value.AsCString(debug_str);
620 break;
621
622 case DW_AT_declaration:
623 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
624 is_declaration = form_value.Unsigned() != 0;
625 break;
626
627 case DW_AT_artificial:
628 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
629 is_artificial = form_value.Unsigned() != 0;
630 break;
631
632 case DW_AT_MIPS_linkage_name:
633 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
634 mangled = form_value.AsCString(debug_str);
635 break;
636
637 case DW_AT_low_pc:
638 case DW_AT_ranges:
639 case DW_AT_entry_pc:
640 has_address = true;
641 break;
642
643 case DW_AT_location:
644 has_location = true;
645 if (tag == DW_TAG_variable)
646 {
647 const DWARFDebugInfoEntry* parent_die = die.GetParent();
648 while ( parent_die != NULL )
649 {
650 switch (parent_die->Tag())
651 {
652 case DW_TAG_subprogram:
653 case DW_TAG_lexical_block:
654 case DW_TAG_inlined_subroutine:
655 // Even if this is a function level static, we don't add it. We could theoretically
656 // add these if we wanted to by introspecting into the DW_AT_location and seeing
657 // if the location describes a hard coded address, but we dont want the performance
658 // penalty of that right now.
659 is_global_or_static_variable = false;
660// if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
661// {
662// // If we have valid block data, then we have location expression bytes
663// // that are fixed (not a location list).
664// const uint8_t *block_data = form_value.BlockData();
665// if (block_data)
666// {
667// uint32_t block_length = form_value.Unsigned();
668// if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
669// {
670// if (block_data[0] == DW_OP_addr)
671// add_die = true;
672// }
673// }
674// }
675 parent_die = NULL; // Terminate the while loop.
676 break;
677
678 case DW_TAG_compile_unit:
679 is_global_or_static_variable = true;
680 parent_die = NULL; // Terminate the while loop.
681 break;
682
683 default:
684 parent_die = parent_die->GetParent(); // Keep going in the while loop.
685 break;
686 }
687 }
688 }
689 break;
Greg Clayton12bec712010-06-28 21:30:43 +0000690
691 case DW_AT_specification:
692 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
693 specification_die_offset = form_value.Reference(this);
694 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000695 }
696 }
697 }
698
699 switch (tag)
700 {
701 case DW_TAG_subprogram:
702 if (has_address)
703 {
Greg Clayton12bec712010-06-28 21:30:43 +0000704 if (name)
Chris Lattner24943d22010-06-08 16:52:24 +0000705 {
706 if ((name[0] == '-' || name[0] == '+') && name[1] == '[')
707 {
708 int name_len = strlen (name);
709 // Objective C methods must have at least:
710 // "-[" or "+[" prefix
711 // One character for a class name
712 // One character for the space between the class name
713 // One character for the method name
714 // "]" suffix
715 if (name_len >= 6 && name[name_len - 1] == ']')
716 {
717 const char *method_name = strchr (name, ' ');
718 if (method_name)
719 {
720 // Skip the space
721 ++method_name;
722 // Extract the objective C basename and add it to the
723 // accelerator tables
724 size_t method_name_len = name_len - (method_name - name) - 1;
725 ConstString method_const_str (method_name, method_name_len);
Greg Clayton12bec712010-06-28 21:30:43 +0000726 selector_name_to_function_die.Append(method_const_str.AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000727 }
728 }
729 }
Greg Clayton12bec712010-06-28 21:30:43 +0000730 // If we have a mangled name, then the DW_AT_name attribute
731 // is usually the method name without the class or any parameters
732 const DWARFDebugInfoEntry *parent = die.GetParent();
733 bool is_method = false;
734 if (parent)
735 {
736 dw_tag_t tag = parent->Tag();
737 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
738 {
739 is_method = true;
740 }
741 else
742 {
743 if (mangled && specification_die_offset != DW_INVALID_OFFSET)
744 {
745 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL);
746 if (specification_die)
747 {
748 parent = specification_die->GetParent();
749 if (parent)
750 {
751 tag = parent->Tag();
752
753 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
754 is_method = true;
755 }
756 }
757 }
758 }
759 }
760
761 if (is_method)
762 method_name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset());
763 else
764 base_name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000765 }
Greg Clayton12bec712010-06-28 21:30:43 +0000766 if (mangled)
767 full_name_to_function_die.Append(ConstString(mangled).AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000768 }
769 break;
770
771 case DW_TAG_inlined_subroutine:
772 if (has_address)
773 {
Greg Clayton12bec712010-06-28 21:30:43 +0000774 if (name)
775 base_name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset());
776 if (mangled)
777 full_name_to_function_die.Append(ConstString(mangled).AsCString(), die.GetOffset());
Chris Lattner24943d22010-06-08 16:52:24 +0000778 }
779 break;
780
781 case DW_TAG_base_type:
782 case DW_TAG_class_type:
783 case DW_TAG_constant:
784 case DW_TAG_enumeration_type:
785 case DW_TAG_string_type:
786 case DW_TAG_subroutine_type:
787 case DW_TAG_structure_type:
788 case DW_TAG_union_type:
789 case DW_TAG_typedef:
790 case DW_TAG_namespace:
791 if (name && is_declaration == false)
792 {
793 name_to_type_die.Append(ConstString(name).AsCString(), die.GetOffset());
794 }
795 break;
796
797 case DW_TAG_variable:
798 if (name && has_location && is_global_or_static_variable)
799 {
800 AddGlobalDIEByIndex (std::distance (begin, pos));
801 name_to_global_die.Append(ConstString(name).AsCString(), die.GetOffset());
802 }
803 break;
804
805 default:
806 continue;
807 }
808 }
809}
810
811