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