]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / source / Plugins / SymbolFile / DWARF / DWARFCompileUnit.cpp
1 //===-- 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/Mangled.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/Timer.h"
16 #include "lldb/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/LineTable.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Target/ObjCLanguageRuntime.h"
20
21 #include "DWARFDebugAbbrev.h"
22 #include "DWARFDebugAranges.h"
23 #include "DWARFDebugInfo.h"
24 #include "DWARFDIECollection.h"
25 #include "DWARFFormValue.h"
26 #include "LogChannelDWARF.h"
27 #include "NameToDIE.h"
28 #include "SymbolFileDWARF.h"
29 #include "SymbolFileDWARFDebugMap.h"
30
31 using namespace lldb;
32 using namespace lldb_private;
33 using namespace std;
34
35
36 extern int g_verbose;
37
38 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) :
39     m_dwarf2Data    (dwarf2Data),
40     m_abbrevs       (NULL),
41     m_user_data     (NULL),
42     m_die_array     (),
43     m_func_aranges_ap (),
44     m_base_addr     (0),
45     m_offset        (DW_INVALID_OFFSET),
46     m_length        (0),
47     m_version       (0),
48     m_addr_size     (DWARFCompileUnit::GetDefaultAddressSize()),
49     m_producer      (eProducerInvalid),
50     m_producer_version_major (0),
51     m_producer_version_minor (0),
52     m_producer_version_update (0)
53 {
54 }
55
56 void
57 DWARFCompileUnit::Clear()
58 {
59     m_offset        = DW_INVALID_OFFSET;
60     m_length        = 0;
61     m_version       = 0;
62     m_abbrevs       = NULL;
63     m_addr_size     = DWARFCompileUnit::GetDefaultAddressSize();
64     m_base_addr     = 0;
65     m_die_array.clear();
66     m_func_aranges_ap.reset();
67     m_user_data     = NULL;
68     m_producer      = eProducerInvalid;
69 }
70
71 bool
72 DWARFCompileUnit::Extract(const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr)
73 {
74     Clear();
75
76     m_offset = *offset_ptr;
77
78     if (debug_info.ValidOffset(*offset_ptr))
79     {
80         dw_offset_t abbr_offset;
81         const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
82         m_length        = debug_info.GetU32(offset_ptr);
83         m_version       = debug_info.GetU16(offset_ptr);
84         abbr_offset     = debug_info.GetU32(offset_ptr);
85         m_addr_size     = debug_info.GetU8 (offset_ptr);
86
87         bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
88         bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
89         bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
90         bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
91
92         if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL)
93         {
94             m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
95             return true;
96         }
97
98         // reset the offset to where we tried to parse from if anything went wrong
99         *offset_ptr = m_offset;
100     }
101
102     return false;
103 }
104
105
106 void
107 DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die)
108 {
109     if (m_die_array.size() > 1)
110     {
111         // std::vectors never get any smaller when resized to a smaller size,
112         // or when clear() or erase() are called, the size will report that it
113         // is smaller, but the memory allocated remains intact (call capacity()
114         // to see this). So we need to create a temporary vector and swap the
115         // contents which will cause just the internal pointers to be swapped
116         // so that when "tmp_array" goes out of scope, it will destroy the
117         // contents.
118
119         // Save at least the compile unit DIE
120         DWARFDebugInfoEntry::collection tmp_array;
121         m_die_array.swap(tmp_array);
122         if (keep_compile_unit_die)
123             m_die_array.push_back(tmp_array.front());
124     }
125 }
126
127 //----------------------------------------------------------------------
128 // ParseCompileUnitDIEsIfNeeded
129 //
130 // Parses a compile unit and indexes its DIEs if it hasn't already been
131 // done.
132 //----------------------------------------------------------------------
133 size_t
134 DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only)
135 {
136     const size_t initial_die_array_size = m_die_array.size();
137     if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
138         return 0; // Already parsed
139
140     Timer scoped_timer (__PRETTY_FUNCTION__,
141                         "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
142                         m_offset,
143                         cu_die_only);
144
145     // Set the offset to that of the first DIE and calculate the start of the
146     // next compilation unit header.
147     lldb::offset_t offset = GetFirstDIEOffset();
148     lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
149
150     DWARFDebugInfoEntry die;
151         // Keep a flat array of the DIE for binary lookup by DIE offset
152     if (!cu_die_only)
153     {
154         Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
155         if (log)
156         {
157             m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
158                                                                                     "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]",
159                                                                                     GetOffset());
160         }
161     }
162
163     uint32_t depth = 0;
164     // We are in our compile unit, parse starting at the offset
165     // we were told to parse
166     const DWARFDataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data();
167     std::vector<uint32_t> die_index_stack;
168     die_index_stack.reserve(32);
169     die_index_stack.push_back(0);
170     bool prev_die_had_children = false;
171     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize());
172     while (offset < next_cu_offset &&
173            die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset))
174     {
175 //        if (log)
176 //            log->Printf("0x%8.8x: %*.*s%s%s",
177 //                        die.GetOffset(),
178 //                        depth * 2, depth * 2, "",
179 //                        DW_TAG_value_to_name (die.Tag()),
180 //                        die.HasChildren() ? " *" : "");
181
182         const bool null_die = die.IsNULL();
183         if (depth == 0)
184         {
185             uint64_t base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
186             if (base_addr == LLDB_INVALID_ADDRESS)
187                 base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_entry_pc, 0);
188             SetBaseAddress (base_addr);
189             if (initial_die_array_size == 0)
190                 AddDIE (die);
191             if (cu_die_only)
192                 return 1;
193         }
194         else
195         {
196             if (null_die)
197             {
198                 if (prev_die_had_children)
199                 {
200                     // This will only happen if a DIE says is has children
201                     // but all it contains is a NULL tag. Since we are removing
202                     // the NULL DIEs from the list (saves up to 25% in C++ code),
203                     // we need a way to let the DIE know that it actually doesn't
204                     // have children.
205                     if (!m_die_array.empty())
206                         m_die_array.back().SetEmptyChildren(true);
207                 }
208             }
209             else
210             {
211                 die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]);
212
213                 if (die_index_stack.back())
214                     m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back());
215                 
216                 // Only push the DIE if it isn't a NULL DIE
217                     m_die_array.push_back(die);
218             }
219         }
220
221         if (null_die)
222         {
223             // NULL DIE.
224             if (!die_index_stack.empty())
225                 die_index_stack.pop_back();
226
227             if (depth > 0)
228                 --depth;
229             if (depth == 0)
230                 break;  // We are done with this compile unit!
231
232             prev_die_had_children = false;
233         }
234         else
235         {
236             die_index_stack.back() = m_die_array.size() - 1;
237             // Normal DIE
238             const bool die_has_children = die.HasChildren();
239             if (die_has_children)
240             {
241                 die_index_stack.push_back(0);
242                 ++depth;
243             }
244             prev_die_had_children = die_has_children;
245         }
246     }
247
248     // Give a little bit of info if we encounter corrupt DWARF (our offset
249     // should always terminate at or before the start of the next compilation
250     // unit header).
251     if (offset > next_cu_offset)
252     {
253         m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8" PRIx64 "\n",
254                                                                    GetOffset(), 
255                                                                    offset);
256     }
257
258     // Since std::vector objects will double their size, we really need to
259     // make a new array with the perfect size so we don't end up wasting
260     // space. So here we copy and swap to make sure we don't have any extra
261     // memory taken up.
262     
263     if (m_die_array.size () < m_die_array.capacity())
264     {
265         DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end());
266         exact_size_die_array.swap (m_die_array);
267     }
268     Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE));
269     if (log)
270     {
271         StreamString strm;
272         DWARFDebugInfoEntry::DumpDIECollection (strm, m_die_array);
273         log->PutCString (strm.GetString().c_str());
274     }
275
276     return m_die_array.size();
277 }
278
279
280 dw_offset_t
281 DWARFCompileUnit::GetAbbrevOffset() const
282 {
283     return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
284 }
285
286
287
288 bool
289 DWARFCompileUnit::Verify(Stream *s) const
290 {
291     const DWARFDataExtractor& debug_info = m_dwarf2Data->get_debug_info_data();
292     bool valid_offset = debug_info.ValidOffset(m_offset);
293     bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
294     bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
295     bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
296     bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
297     bool verbose = s->GetVerbose();
298     if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK)
299     {
300         if (verbose)
301             s->Printf("    0x%8.8x: OK\n", m_offset);
302         return true;
303     }
304     else
305     {
306         s->Printf("    0x%8.8x: ", m_offset);
307
308         m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
309         s->EOL();
310         if (valid_offset)
311         {
312             if (!length_OK)
313                 s->Printf("        The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length);
314             if (!version_OK)
315                 s->Printf("        The 16 bit compile unit header version is not supported.\n");
316             if (!abbr_offset_OK)
317                 s->Printf("        The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset());
318             if (!addr_size_OK)
319                 s->Printf("        The address size is unsupported: 0x%2.2x\n", m_addr_size);
320         }
321         else
322             s->Printf("        The start offset of the compile unit header in the .debug_info is invalid.\n");
323     }
324     return false;
325 }
326
327
328 void
329 DWARFCompileUnit::Dump(Stream *s) const
330 {
331     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",
332                 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset());
333 }
334
335
336 static uint8_t g_default_addr_size = 4;
337
338 uint8_t
339 DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu)
340 {
341     if (cu)
342         return cu->GetAddressByteSize();
343     return DWARFCompileUnit::GetDefaultAddressSize();
344 }
345
346 uint8_t
347 DWARFCompileUnit::GetDefaultAddressSize()
348 {
349     return g_default_addr_size;
350 }
351
352 void
353 DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size)
354 {
355     g_default_addr_size = addr_size;
356 }
357
358 void
359 DWARFCompileUnit::BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data,
360                                           DWARFDebugAranges* debug_aranges,
361                                           bool clear_dies_if_already_not_parsed)
362 {
363     // This function is usually called if there in no .debug_aranges section
364     // in order to produce a compile unit level set of address ranges that
365     // is accurate. If the DIEs weren't parsed, then we don't want all dies for
366     // all compile units to stay loaded when they weren't needed. So we can end
367     // up parsing the DWARF and then throwing them all away to keep memory usage
368     // down.
369     const bool clear_dies = ExtractDIEsIfNeeded (false) > 1;
370     
371     const DWARFDebugInfoEntry* die = DIE();
372     if (die)
373         die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges);
374     
375     if (debug_aranges->IsEmpty())
376     {
377         // We got nothing from the functions, maybe we have a line tables only
378         // situation. Check the line tables and build the arange table from this.
379         SymbolContext sc;
380         sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
381         if (sc.comp_unit)
382         {
383             SymbolFileDWARFDebugMap *debug_map_sym_file = m_dwarf2Data->GetDebugMapSymfile();
384             if (debug_map_sym_file == NULL)
385             {
386                 LineTable *line_table = sc.comp_unit->GetLineTable();
387
388                 if (line_table)
389                 {
390                     LineTable::FileAddressRanges file_ranges;
391                     const bool append = true;
392                     const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append);
393                     for (uint32_t idx=0; idx<num_ranges; ++idx)
394                     {
395                         const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx);
396                         debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(), range.GetRangeEnd());
397                         printf ("0x%8.8x: [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", GetOffset(), range.GetRangeBase(), range.GetRangeEnd());
398                     }
399                 }
400             }
401             else
402                 debug_map_sym_file->AddOSOARanges(dwarf2Data,debug_aranges);
403         }
404     }
405     
406     // Keep memory down by clearing DIEs if this generate function
407     // caused them to be parsed
408     if (clear_dies)
409         ClearDIEs (true);
410
411 }
412
413
414 const DWARFDebugAranges &
415 DWARFCompileUnit::GetFunctionAranges ()
416 {
417     if (m_func_aranges_ap.get() == NULL)
418     {
419         m_func_aranges_ap.reset (new DWARFDebugAranges());
420         Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
421
422         if (log)
423         {
424             m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log,
425                                                                     "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]",
426                                                                     GetOffset());
427         }
428         const DWARFDebugInfoEntry* die = DIE();
429         if (die)
430             die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get());
431         const bool minimize = false;
432         m_func_aranges_ap->Sort(minimize);
433     }
434     return *m_func_aranges_ap.get();
435 }
436
437 bool
438 DWARFCompileUnit::LookupAddress
439 (
440     const dw_addr_t address,
441     DWARFDebugInfoEntry** function_die_handle,
442     DWARFDebugInfoEntry** block_die_handle
443 )
444 {
445     bool success = false;
446
447     if (function_die_handle != NULL && DIE())
448     {
449
450         const DWARFDebugAranges &func_aranges = GetFunctionAranges ();
451
452         // Re-check the aranges auto pointer contents in case it was created above
453         if (!func_aranges.IsEmpty())
454         {
455             *function_die_handle = GetDIEPtr(func_aranges.FindAddress(address));
456             if (*function_die_handle != NULL)
457             {
458                 success = true;
459                 if (block_die_handle != NULL)
460                 {
461                     DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild();
462                     while (child)
463                     {
464                         if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle))
465                             break;
466                         child = child->GetSibling();
467                     }
468                 }
469             }
470         }
471     }
472     return success;
473 }
474
475 //----------------------------------------------------------------------
476 // Compare function DWARFDebugAranges::Range structures
477 //----------------------------------------------------------------------
478 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
479 {
480     return die1.GetOffset() < die2.GetOffset();
481 }
482
483 //----------------------------------------------------------------------
484 // GetDIEPtr()
485 //
486 // Get the DIE (Debug Information Entry) with the specified offset.
487 //----------------------------------------------------------------------
488 DWARFDebugInfoEntry*
489 DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset)
490 {
491     if (die_offset != DW_INVALID_OFFSET)
492     {
493         ExtractDIEsIfNeeded (false);
494         DWARFDebugInfoEntry compare_die;
495         compare_die.SetOffset(die_offset);
496         DWARFDebugInfoEntry::iterator end = m_die_array.end();
497         DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
498         if (pos != end)
499         {
500             if (die_offset == (*pos).GetOffset())
501                 return &(*pos);
502         }
503     }
504     return NULL;    // Not found in any compile units
505 }
506
507 //----------------------------------------------------------------------
508 // GetDIEPtrContainingOffset()
509 //
510 // Get the DIE (Debug Information Entry) that contains the specified
511 // .debug_info offset.
512 //----------------------------------------------------------------------
513 const DWARFDebugInfoEntry*
514 DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset)
515 {
516     if (die_offset != DW_INVALID_OFFSET)
517     {
518         ExtractDIEsIfNeeded (false);
519         DWARFDebugInfoEntry compare_die;
520         compare_die.SetOffset(die_offset);
521         DWARFDebugInfoEntry::iterator end = m_die_array.end();
522         DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
523         if (pos != end)
524         {
525             if (die_offset >= (*pos).GetOffset())
526             {
527                 DWARFDebugInfoEntry::iterator next = pos + 1;
528                 if (next != end)
529                 {
530                     if (die_offset < (*next).GetOffset())
531                         return &(*pos);
532                 }
533             }
534         }
535     }
536     return NULL;    // Not found in any compile units
537 }
538
539
540
541 size_t
542 DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const
543 {
544     size_t old_size = dies.Size();
545     DWARFDebugInfoEntry::const_iterator pos;
546     DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
547     for (pos = m_die_array.begin(); pos != end; ++pos)
548     {
549         if (pos->Tag() == tag)
550             dies.Append (&(*pos));
551     }
552
553     // Return the number of DIEs added to the collection
554     return dies.Size() - old_size;
555 }
556
557 //void
558 //DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
559 //{
560 //    m_global_die_indexes.push_back (die_idx);
561 //}
562 //
563 //
564 //void
565 //DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
566 //{
567 //    // Indexes to all file level global and static variables
568 //    m_global_die_indexes;
569 //    
570 //    if (m_die_array.empty())
571 //        return;
572 //    
573 //    const DWARFDebugInfoEntry* first_die = &m_die_array[0];
574 //    const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
575 //    if (first_die <= die && die < end)
576 //        m_global_die_indexes.push_back (die - first_die);
577 //}
578
579
580 void
581 DWARFCompileUnit::Index (const uint32_t cu_idx,
582                          NameToDIE& func_basenames,
583                          NameToDIE& func_fullnames,
584                          NameToDIE& func_methods,
585                          NameToDIE& func_selectors,
586                          NameToDIE& objc_class_selectors,
587                          NameToDIE& globals,
588                          NameToDIE& types,
589                          NameToDIE& namespaces)
590 {
591     const DWARFDataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data();
592
593     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize());
594
595     Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS));
596     
597     if (log)
598     {
599         m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log, 
600                                                                 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]",
601                                                                 GetOffset());
602     }
603
604     DWARFDebugInfoEntry::const_iterator pos;
605     DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin();
606     DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
607     for (pos = begin; pos != end; ++pos)
608     {
609         const DWARFDebugInfoEntry &die = *pos;
610         
611         const dw_tag_t tag = die.Tag();
612     
613         switch (tag)
614         {
615         case DW_TAG_subprogram:
616         case DW_TAG_inlined_subroutine:
617         case DW_TAG_base_type:
618         case DW_TAG_class_type:
619         case DW_TAG_constant:
620         case DW_TAG_enumeration_type:
621         case DW_TAG_string_type:
622         case DW_TAG_subroutine_type:
623         case DW_TAG_structure_type:
624         case DW_TAG_union_type:
625         case DW_TAG_typedef:
626         case DW_TAG_namespace:
627         case DW_TAG_variable:
628         case DW_TAG_unspecified_type:
629             break;
630             
631         default:
632             continue;
633         }
634
635         DWARFDebugInfoEntry::Attributes attributes;
636         const char *name = NULL;
637         const char *mangled_cstr = NULL;
638         bool is_declaration = false;
639         //bool is_artificial = false;
640         bool has_address = false;
641         bool has_location = false;
642         bool is_global_or_static_variable = false;
643         
644         dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
645         const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, fixed_form_sizes, attributes);
646         if (num_attributes > 0)
647         {
648             for (uint32_t i=0; i<num_attributes; ++i)
649             {
650                 dw_attr_t attr = attributes.AttributeAtIndex(i);
651                 DWARFFormValue form_value;
652                 switch (attr)
653                 {
654                 case DW_AT_name:
655                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
656                         name = form_value.AsCString(debug_str);
657                     break;
658
659                 case DW_AT_declaration:
660                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
661                         is_declaration = form_value.Unsigned() != 0;
662                     break;
663
664 //                case DW_AT_artificial:
665 //                    if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
666 //                        is_artificial = form_value.Unsigned() != 0;
667 //                    break;
668
669                 case DW_AT_MIPS_linkage_name:
670                 case DW_AT_linkage_name:
671                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
672                         mangled_cstr = form_value.AsCString(debug_str);                        
673                     break;
674
675                 case DW_AT_low_pc:
676                 case DW_AT_high_pc:
677                 case DW_AT_ranges:
678                     has_address = true;
679                     break;
680
681                 case DW_AT_entry_pc:
682                     has_address = true;
683                     break;
684
685                 case DW_AT_location:
686                     has_location = true;
687                     if (tag == DW_TAG_variable)
688                     {
689                         const DWARFDebugInfoEntry* parent_die = die.GetParent();
690                         while ( parent_die != NULL )
691                         {
692                             switch (parent_die->Tag())
693                             {
694                             case DW_TAG_subprogram:
695                             case DW_TAG_lexical_block:
696                             case DW_TAG_inlined_subroutine:
697                                 // Even if this is a function level static, we don't add it. We could theoretically
698                                 // add these if we wanted to by introspecting into the DW_AT_location and seeing
699                                 // if the location describes a hard coded address, but we dont want the performance
700                                 // penalty of that right now.
701                                 is_global_or_static_variable = false;
702 //                              if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
703 //                              {
704 //                                  // If we have valid block data, then we have location expression bytes
705 //                                  // that are fixed (not a location list).
706 //                                  const uint8_t *block_data = form_value.BlockData();
707 //                                  if (block_data)
708 //                                  {
709 //                                      uint32_t block_length = form_value.Unsigned();
710 //                                      if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
711 //                                      {
712 //                                          if (block_data[0] == DW_OP_addr)
713 //                                              add_die = true;
714 //                                      }
715 //                                  }
716 //                              }
717                                 parent_die = NULL;  // Terminate the while loop.
718                                 break;
719
720                             case DW_TAG_compile_unit:
721                                 is_global_or_static_variable = true;
722                                 parent_die = NULL;  // Terminate the while loop.
723                                 break;
724
725                             default:
726                                 parent_die = parent_die->GetParent();   // Keep going in the while loop.
727                                 break;
728                             }
729                         }
730                     }
731                     break;
732                     
733                 case DW_AT_specification:
734                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
735                         specification_die_offset = form_value.Reference(this);
736                     break;
737                 }
738             }
739         }
740
741         switch (tag)
742         {
743         case DW_TAG_subprogram:
744             if (has_address)
745             {
746                 if (name)
747                 {
748                     // Note, this check is also done in ParseMethodName, but since this is a hot loop, we do the
749                     // simple inlined check outside the call.
750                     ObjCLanguageRuntime::MethodName objc_method(name, true);
751                     if (objc_method.IsValid(true))
752                     {
753                         ConstString objc_class_name_with_category (objc_method.GetClassNameWithCategory());
754                         ConstString objc_selector_name (objc_method.GetSelector());
755                         ConstString objc_fullname_no_category_name (objc_method.GetFullNameWithoutCategory(true));
756                         ConstString objc_class_name_no_category (objc_method.GetClassName());
757                         func_fullnames.Insert (ConstString(name), die.GetOffset());
758                         if (objc_class_name_with_category)
759                             objc_class_selectors.Insert(objc_class_name_with_category, die.GetOffset());
760                         if (objc_class_name_no_category && objc_class_name_no_category != objc_class_name_with_category)
761                             objc_class_selectors.Insert(objc_class_name_no_category, die.GetOffset());
762                         if (objc_selector_name)
763                             func_selectors.Insert (objc_selector_name, die.GetOffset());
764                         if (objc_fullname_no_category_name)
765                             func_fullnames.Insert (objc_fullname_no_category_name, die.GetOffset());
766                     }
767                     // If we have a mangled name, then the DW_AT_name attribute
768                     // is usually the method name without the class or any parameters
769                     const DWARFDebugInfoEntry *parent = die.GetParent();
770                     bool is_method = false;
771                     if (parent)
772                     {
773                         dw_tag_t parent_tag = parent->Tag();
774                         if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
775                         {
776                             is_method = true;
777                         }
778                         else
779                         {
780                             if (specification_die_offset != DW_INVALID_OFFSET)
781                             {
782                                 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL);
783                                 if (specification_die)
784                                 {
785                                     parent = specification_die->GetParent();
786                                     if (parent)
787                                     {
788                                         parent_tag = parent->Tag();
789                                     
790                                         if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
791                                             is_method = true;
792                                     }
793                                 }
794                             }
795                         }
796                     }
797
798
799                     if (is_method)
800                         func_methods.Insert (ConstString(name), die.GetOffset());
801                     else
802                         func_basenames.Insert (ConstString(name), die.GetOffset());
803
804                     if (!is_method && !mangled_cstr && !objc_method.IsValid(true))
805                         func_fullnames.Insert (ConstString(name), die.GetOffset());
806                 }
807                 if (mangled_cstr)
808                 {
809                     // Make sure our mangled name isn't the same string table entry
810                     // as our name. If it starts with '_', then it is ok, else compare
811                     // the string to make sure it isn't the same and we don't end up
812                     // with duplicate entries
813                     if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (name && ::strcmp(name, mangled_cstr) != 0)))
814                     {
815                         Mangled mangled (ConstString(mangled_cstr), true);
816                         func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset());
817                         if (mangled.GetDemangledName())
818                             func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset());
819                     }
820                 }
821             }
822             break;
823
824         case DW_TAG_inlined_subroutine:
825             if (has_address)
826             {
827                 if (name)
828                     func_basenames.Insert (ConstString(name), die.GetOffset());
829                 if (mangled_cstr)
830                 {
831                     // Make sure our mangled name isn't the same string table entry
832                     // as our name. If it starts with '_', then it is ok, else compare
833                     // the string to make sure it isn't the same and we don't end up
834                     // with duplicate entries
835                     if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
836                     {
837                         Mangled mangled (ConstString(mangled_cstr), true);
838                         func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset());
839                         if (mangled.GetDemangledName())
840                             func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset());
841                     }
842                 }
843                 else
844                     func_fullnames.Insert (ConstString(name), die.GetOffset());
845             }
846             break;
847         
848         case DW_TAG_base_type:
849         case DW_TAG_class_type:
850         case DW_TAG_constant:
851         case DW_TAG_enumeration_type:
852         case DW_TAG_string_type:
853         case DW_TAG_subroutine_type:
854         case DW_TAG_structure_type:
855         case DW_TAG_union_type:
856         case DW_TAG_typedef:
857         case DW_TAG_unspecified_type:
858             if (name && is_declaration == false)
859             {
860                 types.Insert (ConstString(name), die.GetOffset());
861             }
862             break;
863
864         case DW_TAG_namespace:
865             if (name)
866                 namespaces.Insert (ConstString(name), die.GetOffset());
867             break;
868
869         case DW_TAG_variable:
870             if (name && has_location && is_global_or_static_variable)
871             {
872                 globals.Insert (ConstString(name), die.GetOffset());
873                 // Be sure to include variables by their mangled and demangled
874                 // names if they have any since a variable can have a basename
875                 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled 
876                 // mangled name "(anonymous namespace)::i"...
877                 
878                 // Make sure our mangled name isn't the same string table entry
879                 // as our name. If it starts with '_', then it is ok, else compare
880                 // the string to make sure it isn't the same and we don't end up
881                 // with duplicate entries
882                 if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
883                 {
884                     Mangled mangled (ConstString(mangled_cstr), true);
885                     globals.Insert (mangled.GetMangledName(), die.GetOffset());
886                     if (mangled.GetDemangledName())
887                         globals.Insert (mangled.GetDemangledName(), die.GetOffset());
888                 }
889             }
890             break;
891             
892         default:
893             continue;
894         }
895     }
896 }
897
898 bool
899 DWARFCompileUnit::Supports_unnamed_objc_bitfields ()
900 {
901     if (GetProducer() == eProducerClang)
902     {
903         const uint32_t major_version = GetProducerVersionMajor();
904         if (major_version > 425 || (major_version == 425 && GetProducerVersionUpdate() >= 13))
905             return true;
906         else
907             return false;
908     }
909     return true; // Assume all other compilers didn't have incorrect ObjC bitfield info
910 }
911
912 bool
913 DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type ()
914 {
915     if (GetProducer() == eProducerLLVMGCC)
916         return false;
917     return true;
918 }
919
920 bool
921 DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid()
922 {
923     // llvm-gcc makes completely invalid decl file attributes and won't ever
924     // be fixed, so we need to know to ignore these.
925     return GetProducer() == eProducerLLVMGCC;
926 }
927
928 void
929 DWARFCompileUnit::ParseProducerInfo ()
930 {
931     m_producer_version_major = UINT32_MAX;
932     m_producer_version_minor = UINT32_MAX;
933     m_producer_version_update = UINT32_MAX;
934
935     const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly();
936     if (die)
937     {
938
939         const char *producer_cstr = die->GetAttributeValueAsString(m_dwarf2Data, this, DW_AT_producer, NULL);
940         if (producer_cstr)
941         {
942             RegularExpression llvm_gcc_regex("^4\\.[012]\\.[01] \\(Based on Apple Inc\\. build [0-9]+\\) \\(LLVM build [\\.0-9]+\\)$");
943             if (llvm_gcc_regex.Execute (producer_cstr))
944             {
945                 m_producer = eProducerLLVMGCC;
946             }
947             else if (strstr(producer_cstr, "clang"))
948             {
949                 static RegularExpression g_clang_version_regex("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)");
950                 RegularExpression::Match regex_match(3);
951                 if (g_clang_version_regex.Execute (producer_cstr, &regex_match))
952                 {
953                     std::string str;
954                     if (regex_match.GetMatchAtIndex (producer_cstr, 1, str))
955                         m_producer_version_major = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
956                     if (regex_match.GetMatchAtIndex (producer_cstr, 2, str))
957                         m_producer_version_minor = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
958                     if (regex_match.GetMatchAtIndex (producer_cstr, 3, str))
959                         m_producer_version_update = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
960                 }
961                 m_producer = eProducerClang;
962             }
963             else if (strstr(producer_cstr, "GNU"))
964                 m_producer = eProducerGCC;
965         }
966     }
967     if (m_producer == eProducerInvalid)
968         m_producer = eProcucerOther;
969 }
970
971 DWARFCompileUnit::Producer
972 DWARFCompileUnit::GetProducer ()
973 {
974     if (m_producer == eProducerInvalid)
975         ParseProducerInfo ();
976     return m_producer;
977 }
978
979
980 uint32_t
981 DWARFCompileUnit::GetProducerVersionMajor()
982 {
983     if (m_producer_version_major == 0)
984         ParseProducerInfo ();
985     return m_producer_version_major;
986 }
987
988 uint32_t
989 DWARFCompileUnit::GetProducerVersionMinor()
990 {
991     if (m_producer_version_minor == 0)
992         ParseProducerInfo ();
993     return m_producer_version_minor;
994 }
995
996 uint32_t
997 DWARFCompileUnit::GetProducerVersionUpdate()
998 {
999     if (m_producer_version_update == 0)
1000         ParseProducerInfo ();
1001     return m_producer_version_update;
1002 }
1003