]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp
MFV r277981:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / SymbolFile / DWARF / DWARFDebugInfoEntry.cpp
1 //===-- DWARFDebugInfoEntry.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 "DWARFDebugInfoEntry.h"
11
12 #include <assert.h>
13
14 #include <algorithm>
15
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Expression/DWARFExpression.h"
19 #include "lldb/Symbol/ObjectFile.h"
20
21 #include "DWARFCompileUnit.h"
22 #include "SymbolFileDWARF.h"
23 #include "DWARFDebugAbbrev.h"
24 #include "DWARFDebugAranges.h"
25 #include "DWARFDebugInfo.h"
26 #include "DWARFDeclContext.h"
27 #include "DWARFDIECollection.h"
28 #include "DWARFFormValue.h"
29 #include "DWARFLocationDescription.h"
30 #include "DWARFLocationList.h"
31 #include "DWARFDebugRanges.h"
32
33 using namespace lldb_private;
34 using namespace std;
35 extern int g_verbose;
36
37
38
39 DWARFDebugInfoEntry::Attributes::Attributes() :
40     m_infos()
41 {
42 }
43
44 DWARFDebugInfoEntry::Attributes::~Attributes()
45 {
46 }
47
48
49 uint32_t
50 DWARFDebugInfoEntry::Attributes::FindAttributeIndex(dw_attr_t attr) const
51 {
52     collection::const_iterator end = m_infos.end();
53     collection::const_iterator beg = m_infos.begin();
54     collection::const_iterator pos;
55     for (pos = beg; pos != end; ++pos)
56     {
57         if (pos->attr == attr)
58             return std::distance(beg, pos);
59     }
60     return UINT32_MAX;
61 }
62
63 void
64 DWARFDebugInfoEntry::Attributes::Append(const DWARFCompileUnit *cu, dw_offset_t attr_die_offset, dw_attr_t attr, dw_form_t form)
65 {
66     Info info = { cu, attr_die_offset, attr, form };
67     m_infos.push_back(info);
68 }
69
70 bool
71 DWARFDebugInfoEntry::Attributes::ContainsAttribute(dw_attr_t attr) const
72 {
73     return FindAttributeIndex(attr) != UINT32_MAX;
74 }
75
76 bool
77 DWARFDebugInfoEntry::Attributes::RemoveAttribute(dw_attr_t attr)
78 {
79     uint32_t attr_index = FindAttributeIndex(attr);
80     if (attr_index != UINT32_MAX)
81     {
82         m_infos.erase(m_infos.begin() + attr_index);
83         return true;
84     }
85     return false;
86 }
87
88 bool
89 DWARFDebugInfoEntry::Attributes::ExtractFormValueAtIndex (SymbolFileDWARF* dwarf2Data, uint32_t i, DWARFFormValue &form_value) const
90 {
91     form_value.SetForm(FormAtIndex(i));
92     lldb::offset_t offset = DIEOffsetAtIndex(i);
93     return form_value.ExtractValue(dwarf2Data->get_debug_info_data(), &offset, CompileUnitAtIndex(i));
94 }
95
96 uint64_t
97 DWARFDebugInfoEntry::Attributes::FormValueAsUnsigned (SymbolFileDWARF* dwarf2Data, dw_attr_t attr, uint64_t fail_value) const
98 {
99     const uint32_t attr_idx = FindAttributeIndex (attr);
100     if (attr_idx != UINT32_MAX)
101         return FormValueAsUnsignedAtIndex (dwarf2Data, attr_idx, fail_value);
102     return fail_value;
103 }
104
105 uint64_t
106 DWARFDebugInfoEntry::Attributes::FormValueAsUnsignedAtIndex(SymbolFileDWARF* dwarf2Data, uint32_t i, uint64_t fail_value) const
107 {
108     DWARFFormValue form_value;
109     if (ExtractFormValueAtIndex(dwarf2Data, i, form_value))
110         return form_value.Reference(CompileUnitAtIndex(i));
111     return fail_value;
112 }
113
114
115
116 bool
117 DWARFDebugInfoEntry::FastExtract
118 (
119     const DWARFDataExtractor& debug_info_data,
120     const DWARFCompileUnit* cu,
121     const uint8_t *fixed_form_sizes,
122     lldb::offset_t *offset_ptr
123 )
124 {
125     m_offset = *offset_ptr;
126     m_parent_idx = 0;
127     m_sibling_idx = 0;
128     m_empty_children = false;
129     const uint64_t abbr_idx = debug_info_data.GetULEB128 (offset_ptr);
130     assert (abbr_idx < (1 << DIE_ABBR_IDX_BITSIZE));
131     m_abbr_idx = abbr_idx;
132     
133     //assert (fixed_form_sizes);  // For best performance this should be specified!
134     
135     if (m_abbr_idx)
136     {
137         lldb::offset_t offset = *offset_ptr;
138
139         const DWARFAbbreviationDeclaration *abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration(m_abbr_idx);
140         
141         if (abbrevDecl == NULL)
142         {
143             cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid abbreviation code %u, please file a bug and attach the file at the start of this error message", 
144                                                                                  m_offset, 
145                                                                                  (unsigned)abbr_idx);
146             // WE can't parse anymore if the DWARF is borked...
147             *offset_ptr = UINT32_MAX;
148             return false;
149         }
150         m_tag = abbrevDecl->Tag();
151         m_has_children = abbrevDecl->HasChildren();
152         // Skip all data in the .debug_info for the attributes
153         const uint32_t numAttributes = abbrevDecl->NumAttributes();
154         uint32_t i;
155         dw_form_t form;
156         for (i=0; i<numAttributes; ++i)
157         {
158             form = abbrevDecl->GetFormByIndexUnchecked(i);
159
160             const uint8_t fixed_skip_size = fixed_form_sizes [form];
161             if (fixed_skip_size)
162                 offset += fixed_skip_size;
163             else
164             {
165                 bool form_is_indirect = false;
166                 do
167                 {
168                     form_is_indirect = false;
169                     uint32_t form_size = 0;
170                     switch (form)
171                     {
172                     // Blocks if inlined data that have a length field and the data bytes
173                     // inlined in the .debug_info
174                     case DW_FORM_exprloc     :
175                     case DW_FORM_block       : form_size = debug_info_data.GetULEB128 (&offset);      break;
176                     case DW_FORM_block1      : form_size = debug_info_data.GetU8_unchecked (&offset); break;
177                     case DW_FORM_block2      : form_size = debug_info_data.GetU16_unchecked (&offset);break;
178                     case DW_FORM_block4      : form_size = debug_info_data.GetU32_unchecked (&offset);break;
179
180                     // Inlined NULL terminated C-strings
181                     case DW_FORM_string      :
182                         debug_info_data.GetCStr (&offset);
183                         break;
184
185                     // Compile unit address sized values
186                     case DW_FORM_addr        :
187                         form_size = cu->GetAddressByteSize();
188                         break;
189                     case DW_FORM_ref_addr    :
190                         if (cu->GetVersion() <= 2)
191                             form_size = cu->GetAddressByteSize();
192                         else
193                             form_size = 4; // 4 bytes for DWARF 32, 8 bytes for DWARF 64, but we don't support DWARF64 yet
194                         break;
195
196                     // 0 sized form
197                     case DW_FORM_flag_present:
198                         form_size = 0;
199                         break;
200
201                     // 1 byte values
202                     case DW_FORM_data1       :
203                     case DW_FORM_flag        :
204                     case DW_FORM_ref1        :
205                         form_size = 1;
206                         break;
207
208                     // 2 byte values
209                     case DW_FORM_data2       :
210                     case DW_FORM_ref2        :
211                         form_size = 2;
212                         break;
213
214                     // 4 byte values
215                     case DW_FORM_strp        :
216                     case DW_FORM_data4       :
217                     case DW_FORM_ref4        :
218                         form_size = 4;
219                         break;
220
221                     // 8 byte values
222                     case DW_FORM_data8       :
223                     case DW_FORM_ref8        :
224                     case DW_FORM_ref_sig8    :
225                         form_size = 8;
226                         break;
227
228                     // signed or unsigned LEB 128 values
229                     case DW_FORM_sdata       :
230                     case DW_FORM_udata       :
231                     case DW_FORM_ref_udata   :
232                         debug_info_data.Skip_LEB128 (&offset);
233                         break;
234
235                     case DW_FORM_indirect    :
236                         form_is_indirect = true;
237                         form = debug_info_data.GetULEB128 (&offset);
238                         break;
239
240                     case DW_FORM_sec_offset  :
241                         if (cu->GetAddressByteSize () == 4)
242                             debug_info_data.GetU32 (offset_ptr);
243                         else
244                             debug_info_data.GetU64 (offset_ptr);
245                         break;
246
247                     default:
248                         *offset_ptr = m_offset;
249                         return false;
250                     }
251                     offset += form_size;
252
253                 } while (form_is_indirect);
254             }
255         }
256         *offset_ptr = offset;
257         return true;
258     }
259     else
260     {
261         m_tag = 0;
262         m_has_children = false;
263         return true;    // NULL debug tag entry
264     }
265
266     return false;
267 }
268
269 //----------------------------------------------------------------------
270 // Extract
271 //
272 // Extract a debug info entry for a given compile unit from the
273 // .debug_info and .debug_abbrev data within the SymbolFileDWARF class
274 // starting at the given offset
275 //----------------------------------------------------------------------
276 bool
277 DWARFDebugInfoEntry::Extract
278 (
279     SymbolFileDWARF* dwarf2Data,
280     const DWARFCompileUnit* cu,
281     lldb::offset_t *offset_ptr
282 )
283 {
284     const DWARFDataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
285 //    const DWARFDataExtractor& debug_str_data = dwarf2Data->get_debug_str_data();
286     const uint32_t cu_end_offset = cu->GetNextCompileUnitOffset();
287     const uint8_t cu_addr_size = cu->GetAddressByteSize();
288     lldb::offset_t offset = *offset_ptr;
289 //  if (offset >= cu_end_offset)
290 //      Log::Error("DIE at offset 0x%8.8x is beyond the end of the current compile unit (0x%8.8x)", m_offset, cu_end_offset);
291     if ((offset < cu_end_offset) && debug_info_data.ValidOffset(offset))
292     {
293         m_offset = offset;
294
295         const uint64_t abbr_idx = debug_info_data.GetULEB128(&offset);
296         assert (abbr_idx < (1 << DIE_ABBR_IDX_BITSIZE));
297         m_abbr_idx = abbr_idx;
298         if (abbr_idx)
299         {
300             const DWARFAbbreviationDeclaration *abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration(abbr_idx);
301
302             if (abbrevDecl)
303             {
304                 m_tag = abbrevDecl->Tag();
305                 m_has_children = abbrevDecl->HasChildren();
306
307                 bool isCompileUnitTag = m_tag == DW_TAG_compile_unit;
308                 if (cu && isCompileUnitTag)
309                     ((DWARFCompileUnit*)cu)->SetBaseAddress(0);
310
311                 // Skip all data in the .debug_info for the attributes
312                 const uint32_t numAttributes = abbrevDecl->NumAttributes();
313                 uint32_t i;
314                 dw_attr_t attr;
315                 dw_form_t form;
316                 for (i=0; i<numAttributes; ++i)
317                 {
318                     abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form);
319
320                     if (isCompileUnitTag && ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc)))
321                     {
322                         DWARFFormValue form_value(form);
323                         if (form_value.ExtractValue(debug_info_data, &offset, cu))
324                         {
325                             if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
326                                 ((DWARFCompileUnit*)cu)->SetBaseAddress(form_value.Unsigned());
327                         }
328                     }
329                     else
330                     {
331                         bool form_is_indirect = false;
332                         do
333                         {
334                             form_is_indirect = false;
335                             uint32_t form_size = 0;
336                             switch (form)
337                             {
338                             // Blocks if inlined data that have a length field and the data bytes
339                             // inlined in the .debug_info
340                             case DW_FORM_exprloc     :
341                             case DW_FORM_block       : form_size = debug_info_data.GetULEB128(&offset);  break;
342                             case DW_FORM_block1      : form_size = debug_info_data.GetU8(&offset);       break;
343                             case DW_FORM_block2      : form_size = debug_info_data.GetU16(&offset);      break;
344                             case DW_FORM_block4      : form_size = debug_info_data.GetU32(&offset);      break;
345
346                             // Inlined NULL terminated C-strings
347                             case DW_FORM_string      : debug_info_data.GetCStr(&offset);                 break;
348
349                             // Compile unit address sized values
350                             case DW_FORM_addr        :
351                                 form_size = cu_addr_size;
352                                 break;
353                             case DW_FORM_ref_addr    :
354                                 if (cu->GetVersion() <= 2)
355                                     form_size = cu_addr_size;
356                                 else
357                                     form_size = 4; // 4 bytes for DWARF 32, 8 bytes for DWARF 64, but we don't support DWARF64 yet
358                                 break;
359
360                             // 0 sized form
361                             case DW_FORM_flag_present:
362                                 form_size = 0;
363                                 break;
364
365                             // 1 byte values
366                             case DW_FORM_data1       :
367                             case DW_FORM_flag        :
368                             case DW_FORM_ref1        :
369                                 form_size = 1;
370                                 break;
371
372                             // 2 byte values
373                             case DW_FORM_data2       :
374                             case DW_FORM_ref2        :
375                                 form_size = 2;
376                                 break;
377
378                             // 4 byte values
379                             case DW_FORM_strp        :
380                                 form_size = 4;
381                                 break;
382
383                             case DW_FORM_data4       :
384                             case DW_FORM_ref4        :
385                                 form_size = 4;
386                                 break;
387
388                             // 8 byte values
389                             case DW_FORM_data8       :
390                             case DW_FORM_ref8        :
391                             case DW_FORM_ref_sig8    :
392                                 form_size = 8;
393                                 break;
394
395                             // signed or unsigned LEB 128 values
396                             case DW_FORM_sdata       :
397                             case DW_FORM_udata       :
398                             case DW_FORM_ref_udata   :
399                                 debug_info_data.Skip_LEB128(&offset);
400                                 break;
401
402                             case DW_FORM_indirect    :
403                                 form = debug_info_data.GetULEB128(&offset);
404                                 form_is_indirect = true;
405                                 break;
406
407                             case DW_FORM_sec_offset  :
408                                 if (cu->GetAddressByteSize () == 4)
409                                     debug_info_data.GetU32 (offset_ptr);
410                                 else
411                                     debug_info_data.GetU64 (offset_ptr);
412                                 break;
413
414                             default:
415                                 *offset_ptr = offset;
416                                 return false;
417                             }
418
419                             offset += form_size;
420                         } while (form_is_indirect);
421                     }
422                 }
423                 *offset_ptr = offset;
424                 return true;
425             }
426         }
427         else
428         {
429             m_tag = 0;
430             m_has_children = false;
431             *offset_ptr = offset;
432             return true;    // NULL debug tag entry
433         }
434     }
435
436     return false;
437 }
438
439 //----------------------------------------------------------------------
440 // DumpAncestry
441 //
442 // Dumps all of a debug information entries parents up until oldest and
443 // all of it's attributes to the specified stream.
444 //----------------------------------------------------------------------
445 void
446 DWARFDebugInfoEntry::DumpAncestry
447 (
448     SymbolFileDWARF* dwarf2Data,
449     const DWARFCompileUnit* cu,
450     const DWARFDebugInfoEntry* oldest,
451     Stream &s,
452     uint32_t recurse_depth
453 ) const
454 {
455     const DWARFDebugInfoEntry* parent = GetParent();
456     if (parent && parent != oldest)
457         parent->DumpAncestry(dwarf2Data, cu, oldest, s, 0);
458     Dump(dwarf2Data, cu, s, recurse_depth);
459 }
460
461 //----------------------------------------------------------------------
462 // Compare two DIE by comparing all their attributes values, and
463 // following all DW_FORM_ref attributes and comparing their contents as
464 // well (except for DW_AT_sibling attributes.
465 //
466 //  DWARFDebugInfoEntry::CompareState compare_state;
467 //  int result = DWARFDebugInfoEntry::Compare(this, 0x00017ccb, 0x0001eb2b, compare_state, false, true);
468 //----------------------------------------------------------------------
469 //int
470 //DWARFDebugInfoEntry::Compare
471 //(
472 //    SymbolFileDWARF* dwarf2Data,
473 //    dw_offset_t a_die_offset,
474 //    dw_offset_t b_die_offset,
475 //    CompareState &compare_state,
476 //    bool compare_siblings,
477 //    bool compare_children
478 //)
479 //{
480 //    if (a_die_offset == b_die_offset)
481 //        return 0;
482 //
483 //    DWARFCompileUnitSP a_cu_sp;
484 //    DWARFCompileUnitSP b_cu_sp;
485 //    const DWARFDebugInfoEntry* a_die = dwarf2Data->DebugInfo()->GetDIEPtr(a_die_offset, &a_cu_sp);
486 //    const DWARFDebugInfoEntry* b_die = dwarf2Data->DebugInfo()->GetDIEPtr(b_die_offset, &b_cu_sp);
487 //
488 //    return Compare(dwarf2Data, a_cu_sp.get(), a_die, b_cu_sp.get(), b_die, compare_state, compare_siblings, compare_children);
489 //}
490 //
491 //int
492 //DWARFDebugInfoEntry::Compare
493 //(
494 //    SymbolFileDWARF* dwarf2Data,
495 //    DWARFCompileUnit* a_cu, const DWARFDebugInfoEntry* a_die,
496 //    DWARFCompileUnit* b_cu, const DWARFDebugInfoEntry* b_die,
497 //    CompareState &compare_state,
498 //    bool compare_siblings,
499 //    bool compare_children
500 //)
501 //{
502 //    if (a_die == b_die)
503 //        return 0;
504 //
505 //    if (!compare_state.AddTypePair(a_die->GetOffset(), b_die->GetOffset()))
506 //    {
507 //        // We are already comparing both of these types, so let
508 //        // compares complete for the real result
509 //        return 0;
510 //    }
511 //
512 //    //printf("DWARFDebugInfoEntry::Compare(0x%8.8x, 0x%8.8x)\n", a_die->GetOffset(), b_die->GetOffset());
513 //
514 //    // Do we have two valid DIEs?
515 //    if (a_die && b_die)
516 //    {
517 //        // Both DIE are valid
518 //        int result = 0;
519 //
520 //        const dw_tag_t a_tag = a_die->Tag();
521 //        const dw_tag_t b_tag = b_die->Tag();
522 //        if (a_tag == 0 && b_tag == 0)
523 //            return 0;
524 //
525 //        //printf("    comparing tags: %s and %s\n", DW_TAG_value_to_name(a_tag), DW_TAG_value_to_name(b_tag));
526 //
527 //        if (a_tag < b_tag)
528 //            return -1;
529 //        else if (a_tag > b_tag)
530 //            return 1;
531 //
532 //        DWARFDebugInfoEntry::Attributes a_attrs;
533 //        DWARFDebugInfoEntry::Attributes b_attrs;
534 //        size_t a_attr_count = a_die->GetAttributes(dwarf2Data, a_cu, a_attrs);
535 //        size_t b_attr_count = b_die->GetAttributes(dwarf2Data, b_cu, b_attrs);
536 //        if (a_attr_count != b_attr_count)
537 //        {
538 //            a_attrs.RemoveAttribute(DW_AT_sibling);
539 //            b_attrs.RemoveAttribute(DW_AT_sibling);
540 //        }
541 //
542 //        a_attr_count = a_attrs.Size();
543 //        b_attr_count = b_attrs.Size();
544 //
545 //        DWARFFormValue a_form_value;
546 //        DWARFFormValue b_form_value;
547 //
548 //        if (a_attr_count != b_attr_count)
549 //        {
550 //            uint32_t is_decl_index = a_attrs.FindAttributeIndex(DW_AT_declaration);
551 //            uint32_t a_name_index = UINT32_MAX;
552 //            uint32_t b_name_index = UINT32_MAX;
553 //            if (is_decl_index != UINT32_MAX)
554 //            {
555 //                if (a_attr_count == 2)
556 //                {
557 //                    a_name_index = a_attrs.FindAttributeIndex(DW_AT_name);
558 //                    b_name_index = b_attrs.FindAttributeIndex(DW_AT_name);
559 //                }
560 //            }
561 //            else
562 //            {
563 //                is_decl_index = b_attrs.FindAttributeIndex(DW_AT_declaration);
564 //                if (is_decl_index != UINT32_MAX && a_attr_count == 2)
565 //                {
566 //                    a_name_index = a_attrs.FindAttributeIndex(DW_AT_name);
567 //                    b_name_index = b_attrs.FindAttributeIndex(DW_AT_name);
568 //                }
569 //            }
570 //            if (a_name_index != UINT32_MAX && b_name_index != UINT32_MAX)
571 //            {
572 //                if (a_attrs.ExtractFormValueAtIndex(dwarf2Data, a_name_index, a_form_value) &&
573 //                    b_attrs.ExtractFormValueAtIndex(dwarf2Data, b_name_index, b_form_value))
574 //                {
575 //                    result = DWARFFormValue::Compare (a_form_value, b_form_value, a_cu, b_cu, &dwarf2Data->get_debug_str_data());
576 //                    if (result == 0)
577 //                    {
578 //                        a_attr_count = b_attr_count = 0;
579 //                        compare_children = false;
580 //                    }
581 //                }
582 //            }
583 //        }
584 //
585 //        if (a_attr_count < b_attr_count)
586 //            return -1;
587 //        if (a_attr_count > b_attr_count)
588 //            return 1;
589 //
590 //
591 //        // The number of attributes are the same...
592 //        if (a_attr_count > 0)
593 //        {
594 //            const DWARFDataExtractor* debug_str_data_ptr = &dwarf2Data->get_debug_str_data();
595 //
596 //            uint32_t i;
597 //            for (i=0; i<a_attr_count; ++i)
598 //            {
599 //                const dw_attr_t a_attr = a_attrs.AttributeAtIndex(i);
600 //                const dw_attr_t b_attr = b_attrs.AttributeAtIndex(i);
601 //                //printf("    comparing attributes\n\t\t0x%8.8x: %s %s\t\t0x%8.8x: %s %s\n",
602 //                //                a_attrs.DIEOffsetAtIndex(i), DW_FORM_value_to_name(a_attrs.FormAtIndex(i)), DW_AT_value_to_name(a_attr),
603 //                //                b_attrs.DIEOffsetAtIndex(i), DW_FORM_value_to_name(b_attrs.FormAtIndex(i)), DW_AT_value_to_name(b_attr));
604 //
605 //                if (a_attr < b_attr)
606 //                    return -1;
607 //                else if (a_attr > b_attr)
608 //                    return 1;
609 //
610 //                switch (a_attr)
611 //                {
612 //                // Since we call a form of GetAttributes which inlines the
613 //                // attributes from DW_AT_abstract_origin and DW_AT_specification
614 //                // we don't care if their values mismatch...
615 //                case DW_AT_abstract_origin:
616 //                case DW_AT_specification:
617 //                case DW_AT_sibling:
618 //                case DW_AT_containing_type:
619 //                    //printf("        action = IGNORE\n");
620 //                    result = 0;
621 //                    break;  // ignore
622 //
623 //                default:
624 //                    if (a_attrs.ExtractFormValueAtIndex(dwarf2Data, i, a_form_value) &&
625 //                        b_attrs.ExtractFormValueAtIndex(dwarf2Data, i, b_form_value))
626 //                        result = DWARFFormValue::Compare (a_form_value, b_form_value, a_cu, b_cu, debug_str_data_ptr);
627 //                    break;
628 //                }
629 //
630 //                //printf("\t  result = %i\n", result);
631 //
632 //                if (result != 0)
633 //                {
634 //                    // Attributes weren't equal, lets see if we care?
635 //                    switch (a_attr)
636 //                    {
637 //                    case DW_AT_decl_file:
638 //                        // TODO: add the ability to compare files in two different compile units
639 //                        if (a_cu == b_cu)
640 //                        {
641 //                            //printf("        action = RETURN RESULT\n");
642 //                            return result;  // Only return the compare results when the compile units are the same and the decl_file attributes can be compared
643 //                        }
644 //                        else
645 //                        {
646 //                            result = 0;
647 //                            //printf("        action = IGNORE\n");
648 //                        }
649 //                        break;
650 //
651 //                    default:
652 //                        switch (a_attrs.FormAtIndex(i))
653 //                        {
654 //                        case DW_FORM_ref1:
655 //                        case DW_FORM_ref2:
656 //                        case DW_FORM_ref4:
657 //                        case DW_FORM_ref8:
658 //                        case DW_FORM_ref_udata:
659 //                        case DW_FORM_ref_addr:
660 //                            //printf("    action = COMPARE DIEs 0x%8.8x 0x%8.8x\n", (dw_offset_t)a_form_value.Reference(a_cu), (dw_offset_t)b_form_value.Reference(b_cu));
661 //                            // These attribute values refer to other DIEs, so lets compare those instead of their DIE offsets...
662 //                            result = Compare(dwarf2Data, a_form_value.Reference(a_cu), b_form_value.Reference(b_cu), compare_state, false, true);
663 //                            if (result != 0)
664 //                                return result;
665 //                            break;
666 //
667 //                        default:
668 //                            // We do care that they were different, return this result...
669 //                            //printf("        action = RETURN RESULT\n");
670 //                            return result;
671 //                        }
672 //                    }
673 //                }
674 //            }
675 //        }
676 //        //printf("    SUCCESS\n\t\t0x%8.8x: %s\n\t\t0x%8.8x: %s\n", a_die->GetOffset(), DW_TAG_value_to_name(a_tag), b_die->GetOffset(), DW_TAG_value_to_name(b_tag));
677 //
678 //        if (compare_children)
679 //        {
680 //            bool a_has_children = a_die->HasChildren();
681 //            bool b_has_children = b_die->HasChildren();
682 //            if (a_has_children == b_has_children)
683 //            {
684 //                // Both either have kids or don't
685 //                if (a_has_children)
686 //                    result = Compare(   dwarf2Data,
687 //                                        a_cu, a_die->GetFirstChild(),
688 //                                        b_cu, b_die->GetFirstChild(),
689 //                                        compare_state, true, compare_children);
690 //                else
691 //                    result = 0;
692 //            }
693 //            else if (!a_has_children)
694 //                result = -1;    // A doesn't have kids, but B does
695 //            else
696 //                result = 1; // A has kids, but B doesn't
697 //        }
698 //
699 //        if (compare_siblings)
700 //        {
701 //            result = Compare(   dwarf2Data,
702 //                                a_cu, a_die->GetSibling(),
703 //                                b_cu, b_die->GetSibling(),
704 //                                compare_state, true, compare_children);
705 //        }
706 //
707 //        return result;
708 //    }
709 //
710 //    if (a_die == NULL)
711 //        return -1;  // a_die is NULL, yet b_die is non-NULL
712 //    else
713 //        return 1;   // a_die is non-NULL, yet b_die is NULL
714 //
715 //}
716 //
717 //
718 //int
719 //DWARFDebugInfoEntry::Compare
720 //(
721 //  SymbolFileDWARF* dwarf2Data,
722 //  const DWARFCompileUnit* cu_a,
723 //  const DWARFDebugInfoEntry* die_a,
724 //  const DWARFCompileUnit* cu_a,
725 //  const DWARFDebugInfoEntry* die_b,
726 //  CompareState &compare_state
727 //)
728 //{
729 //}
730
731 //----------------------------------------------------------------------
732 // GetDIENamesAndRanges
733 //
734 // Gets the valid address ranges for a given DIE by looking for a
735 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges
736 // attributes.
737 //----------------------------------------------------------------------
738 bool
739 DWARFDebugInfoEntry::GetDIENamesAndRanges
740 (
741     SymbolFileDWARF* dwarf2Data,
742     const DWARFCompileUnit* cu,
743     const char * &name,
744     const char * &mangled,
745     DWARFDebugRanges::RangeList& ranges,
746     int& decl_file,
747     int& decl_line,
748     int& decl_column,
749     int& call_file,
750     int& call_line,
751     int& call_column,
752     DWARFExpression *frame_base
753 ) const
754 {
755     if (dwarf2Data == NULL)
756         return false;
757
758     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
759     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
760     std::vector<dw_offset_t> die_offsets;
761     bool set_frame_base_loclist_addr = false;
762     
763     lldb::offset_t offset;
764     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
765
766     lldb::ModuleSP module = dwarf2Data->GetObjectFile()->GetModule();
767
768     if (abbrevDecl)
769     {
770         const DWARFDataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
771
772         if (!debug_info_data.ValidOffset(offset))
773             return false;
774
775         const uint32_t numAttributes = abbrevDecl->NumAttributes();
776         uint32_t i;
777         dw_attr_t attr;
778         dw_form_t form;
779         bool do_offset = false;
780
781         for (i=0; i<numAttributes; ++i)
782         {
783             abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form);
784             DWARFFormValue form_value(form);
785             if (form_value.ExtractValue(debug_info_data, &offset, cu))
786             {
787                 switch (attr)
788                 {
789                 case DW_AT_low_pc:
790                     lo_pc = form_value.Unsigned();
791
792                     if (do_offset)
793                         hi_pc += lo_pc;
794                     do_offset = false;
795                     break;
796
797                 case DW_AT_entry_pc:
798                     lo_pc = form_value.Unsigned();
799                     break;
800
801                 case DW_AT_high_pc:
802                     hi_pc = form_value.Unsigned();
803                     if (form_value.Form() != DW_FORM_addr)
804                     {
805                         if (lo_pc == LLDB_INVALID_ADDRESS)
806                             do_offset = hi_pc != LLDB_INVALID_ADDRESS;
807                         else
808                             hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save on relocations
809                     }
810                     break;
811
812                 case DW_AT_ranges:
813                     {
814                         const DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges();
815                         debug_ranges->FindRanges(form_value.Unsigned(), ranges);
816                         // All DW_AT_ranges are relative to the base address of the
817                         // compile unit. We add the compile unit base address to make
818                         // sure all the addresses are properly fixed up.
819                         ranges.Slide(cu->GetBaseAddress());
820                     }
821                     break;
822
823                 case DW_AT_name:
824                     if (name == NULL)
825                         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
826                     break;
827
828                 case DW_AT_MIPS_linkage_name:
829                 case DW_AT_linkage_name:
830                     if (mangled == NULL)
831                         mangled = form_value.AsCString(&dwarf2Data->get_debug_str_data());
832                     break;
833
834                 case DW_AT_abstract_origin:
835                     die_offsets.push_back(form_value.Reference(cu));
836                     break;
837
838                 case DW_AT_specification:
839                     die_offsets.push_back(form_value.Reference(cu));
840                     break;
841
842                 case DW_AT_decl_file:
843                     if (decl_file == 0)
844                         decl_file = form_value.Unsigned();
845                     break;
846
847                 case DW_AT_decl_line:
848                     if (decl_line == 0)
849                         decl_line = form_value.Unsigned();
850                     break;
851
852                 case DW_AT_decl_column:
853                     if (decl_column == 0)
854                         decl_column = form_value.Unsigned();
855                     break;
856
857                 case DW_AT_call_file:
858                     if (call_file == 0)
859                         call_file = form_value.Unsigned();
860                     break;
861
862                 case DW_AT_call_line:
863                     if (call_line == 0)
864                         call_line = form_value.Unsigned();
865                     break;
866
867                 case DW_AT_call_column:
868                     if (call_column == 0)
869                         call_column = form_value.Unsigned();
870                     break;
871
872                 case DW_AT_frame_base:
873                     if (frame_base)
874                     {
875                         if (form_value.BlockData())
876                         {
877                             uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
878                             uint32_t block_length = form_value.Unsigned();
879                             frame_base->SetOpcodeData(module, debug_info_data, block_offset, block_length);
880                         }
881                         else
882                         {
883                             const DWARFDataExtractor &debug_loc_data = dwarf2Data->get_debug_loc_data();
884                             const dw_offset_t debug_loc_offset = form_value.Unsigned();
885
886                             size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
887                             if (loc_list_length > 0)
888                             {
889                                 frame_base->SetOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
890                                 if (lo_pc != LLDB_INVALID_ADDRESS)
891                                 {
892                                     assert (lo_pc >= cu->GetBaseAddress());
893                                     frame_base->SetLocationListSlide(lo_pc - cu->GetBaseAddress());
894                                 }
895                                 else
896                                 {
897                                     set_frame_base_loclist_addr = true;
898                                 }
899                             }
900                         }
901                     }
902                     break;
903
904                 default:
905                     break;
906                 }
907             }
908         }
909     }
910
911     if (ranges.IsEmpty())
912     {
913         if (lo_pc != LLDB_INVALID_ADDRESS)
914         {
915             if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
916                 ranges.Append(DWARFDebugRanges::Range (lo_pc, hi_pc - lo_pc));
917             else
918                 ranges.Append(DWARFDebugRanges::Range (lo_pc, 0));
919         }
920     }
921     
922     if (set_frame_base_loclist_addr)
923     {
924         dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
925         assert (lowest_range_pc >= cu->GetBaseAddress());
926         frame_base->SetLocationListSlide (lowest_range_pc - cu->GetBaseAddress());
927     }
928
929     if (ranges.IsEmpty() || name == NULL || mangled == NULL)
930     {
931         std::vector<dw_offset_t>::const_iterator pos;
932         std::vector<dw_offset_t>::const_iterator end = die_offsets.end();
933         for (pos = die_offsets.begin(); pos != end; ++pos)
934         {
935             DWARFCompileUnitSP cu_sp_ptr;
936             const DWARFDebugInfoEntry* die = NULL;
937             dw_offset_t die_offset = *pos;
938             if (die_offset != DW_INVALID_OFFSET)
939             {
940                 die = dwarf2Data->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr);
941                 if (die)
942                     die->GetDIENamesAndRanges(dwarf2Data, cu_sp_ptr.get(), name, mangled, ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column);
943             }
944         }
945     }
946     return !ranges.IsEmpty();
947 }
948
949 //----------------------------------------------------------------------
950 // Dump
951 //
952 // Dumps a debug information entry and all of it's attributes to the
953 // specified stream.
954 //----------------------------------------------------------------------
955 void
956 DWARFDebugInfoEntry::Dump
957 (
958     SymbolFileDWARF* dwarf2Data,
959     const DWARFCompileUnit* cu,
960     Stream &s,
961     uint32_t recurse_depth
962 ) const
963 {
964     const DWARFDataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
965     lldb::offset_t offset = m_offset;
966
967     if (debug_info_data.ValidOffset(offset))
968     {
969         dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset);
970
971         s.Printf("\n0x%8.8x: ", m_offset);
972         s.Indent();
973         if (abbrCode != m_abbr_idx)
974         {
975             s.Printf( "error: DWARF has been modified\n");
976         }
977         else if (abbrCode)
978         {
979             const DWARFAbbreviationDeclaration* abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration (abbrCode);
980
981             if (abbrevDecl)
982             {
983                 s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag()));
984                 s.Printf( " [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*':' ');
985
986                 // Dump all data in the .debug_info for the attributes
987                 const uint32_t numAttributes = abbrevDecl->NumAttributes();
988                 uint32_t i;
989                 dw_attr_t attr;
990                 dw_form_t form;
991                 for (i=0; i<numAttributes; ++i)
992                 {
993                     abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form);
994
995                     DumpAttribute(dwarf2Data, cu, debug_info_data, &offset, s, attr, form);
996                 }
997
998                 const DWARFDebugInfoEntry* child = GetFirstChild();
999                 if (recurse_depth > 0 && child)
1000                 {
1001                     s.IndentMore();
1002
1003                     while (child)
1004                     {
1005                         child->Dump(dwarf2Data, cu, s, recurse_depth-1);
1006                         child = child->GetSibling();
1007                     }
1008                     s.IndentLess();
1009                 }
1010             }
1011             else
1012                 s.Printf( "Abbreviation code note found in 'debug_abbrev' class for code: %u\n", abbrCode);
1013         }
1014         else
1015         {
1016             s.Printf( "NULL\n");
1017         }
1018     }
1019 }
1020
1021 void
1022 DWARFDebugInfoEntry::DumpLocation
1023 (
1024     SymbolFileDWARF* dwarf2Data,
1025     DWARFCompileUnit* cu,
1026     Stream &s
1027 ) const
1028 {
1029     const DWARFDebugInfoEntry *cu_die = cu->GetCompileUnitDIEOnly();
1030     const char *cu_name = NULL;
1031     if (cu_die != NULL)
1032         cu_name = cu_die->GetName (dwarf2Data, cu);
1033     const char *obj_file_name = NULL;
1034     ObjectFile *obj_file = dwarf2Data->GetObjectFile();
1035     if (obj_file)
1036         obj_file_name = obj_file->GetFileSpec().GetFilename().AsCString();
1037     const char *die_name = GetName (dwarf2Data, cu);
1038     s.Printf ("0x%8.8x/0x%8.8x: %-30s (from %s in %s)", 
1039               cu->GetOffset(),
1040               GetOffset(),
1041               die_name ? die_name : "", 
1042               cu_name ? cu_name : "<NULL>",
1043               obj_file_name ? obj_file_name : "<NULL>");
1044 }
1045
1046 //----------------------------------------------------------------------
1047 // DumpAttribute
1048 //
1049 // Dumps a debug information entry attribute along with it's form. Any
1050 // special display of attributes is done (disassemble location lists,
1051 // show enumeration values for attributes, etc).
1052 //----------------------------------------------------------------------
1053 void
1054 DWARFDebugInfoEntry::DumpAttribute
1055 (
1056     SymbolFileDWARF* dwarf2Data,
1057     const DWARFCompileUnit* cu,
1058     const DWARFDataExtractor& debug_info_data,
1059     lldb::offset_t *offset_ptr,
1060     Stream &s,
1061     dw_attr_t attr,
1062     dw_form_t form
1063 )
1064 {
1065     bool verbose    = s.GetVerbose();
1066     bool show_form  = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm);
1067     
1068     const DWARFDataExtractor* debug_str_data = dwarf2Data ? &dwarf2Data->get_debug_str_data() : NULL;
1069     if (verbose)
1070         s.Offset (*offset_ptr);
1071     else
1072         s.Printf ("            ");
1073     s.Indent(DW_AT_value_to_name(attr));
1074
1075     if (show_form)
1076     {
1077         s.Printf( "[%s", DW_FORM_value_to_name(form));
1078     }
1079
1080     DWARFFormValue form_value(form);
1081
1082     if (!form_value.ExtractValue(debug_info_data, offset_ptr, cu))
1083         return;
1084
1085     if (show_form)
1086     {
1087         if (form == DW_FORM_indirect)
1088         {
1089             s.Printf( " [%s]", DW_FORM_value_to_name(form_value.Form()));
1090         }
1091
1092         s.PutCString("] ");
1093     }
1094
1095     s.PutCString("( ");
1096
1097     // Always dump form value if verbose is enabled
1098     if (verbose)
1099     {
1100         form_value.Dump(s, debug_str_data, cu);
1101     }
1102
1103
1104     // Check to see if we have any special attribute formatters
1105     switch (attr)
1106     {
1107     case DW_AT_stmt_list:
1108         if ( verbose ) s.PutCString(" ( ");
1109         s.Printf( "0x%8.8" PRIx64, form_value.Unsigned());
1110         if ( verbose ) s.PutCString(" )");
1111         break;
1112
1113     case DW_AT_language:
1114         if ( verbose ) s.PutCString(" ( ");
1115         s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
1116         if ( verbose ) s.PutCString(" )");
1117         break;
1118
1119     case DW_AT_encoding:
1120         if ( verbose ) s.PutCString(" ( ");
1121         s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
1122         if ( verbose ) s.PutCString(" )");
1123         break;
1124
1125     case DW_AT_frame_base:
1126     case DW_AT_location:
1127     case DW_AT_data_member_location:
1128         {
1129             const uint8_t* blockData = form_value.BlockData();
1130             if (blockData)
1131             {
1132                 if (!verbose)
1133                     form_value.Dump(s, debug_str_data, cu);
1134
1135                 // Location description is inlined in data in the form value
1136                 DWARFDataExtractor locationData(debug_info_data, (*offset_ptr) - form_value.Unsigned(), form_value.Unsigned());
1137                 if ( verbose ) s.PutCString(" ( ");
1138                 print_dwarf_expression (s, locationData, DWARFCompileUnit::GetAddressByteSize(cu), 4, false);
1139                 if ( verbose ) s.PutCString(" )");
1140             }
1141             else
1142             {
1143                 // We have a location list offset as the value that is
1144                 // the offset into the .debug_loc section that describes
1145                 // the value over it's lifetime
1146                 uint64_t debug_loc_offset = form_value.Unsigned();
1147                 if (dwarf2Data)
1148                 {
1149                     if ( !verbose )
1150                         form_value.Dump(s, debug_str_data, cu);
1151                     DWARFLocationList::Dump(s, cu, dwarf2Data->get_debug_loc_data(), debug_loc_offset);
1152                 }
1153                 else
1154                 {
1155                     if ( !verbose )
1156                         form_value.Dump(s, NULL, cu);
1157                 }
1158             }
1159         }
1160         break;
1161
1162     case DW_AT_abstract_origin:
1163     case DW_AT_specification:
1164         {
1165             uint64_t abstract_die_offset = form_value.Reference(cu);
1166             form_value.Dump(s, debug_str_data, cu);
1167         //  *ostrm_ptr << HEX32 << abstract_die_offset << " ( ";
1168             if ( verbose ) s.PutCString(" ( ");
1169             GetName(dwarf2Data, cu, abstract_die_offset, s);
1170             if ( verbose ) s.PutCString(" )");
1171         }
1172         break;
1173
1174     case DW_AT_type:
1175         {
1176             uint64_t type_die_offset = form_value.Reference(cu);
1177             if (!verbose)
1178                 form_value.Dump(s, debug_str_data, cu);
1179             s.PutCString(" ( ");
1180             AppendTypeName(dwarf2Data, cu, type_die_offset, s);
1181             s.PutCString(" )");
1182         }
1183         break;
1184
1185     case DW_AT_ranges:
1186         {
1187             if ( !verbose )
1188                 form_value.Dump(s, debug_str_data, cu);
1189             lldb::offset_t ranges_offset = form_value.Unsigned();
1190             dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
1191             if (dwarf2Data)
1192                 DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(), &ranges_offset, base_addr);
1193         }
1194         break;
1195
1196     default:
1197         if ( !verbose )
1198             form_value.Dump(s, debug_str_data, cu);
1199         break;
1200     }
1201
1202     s.PutCString(" )\n");
1203 }
1204
1205 //----------------------------------------------------------------------
1206 // Get all attribute values for a given DIE, including following any
1207 // specification or abstract origin attributes and including those in
1208 // the results. Any duplicate attributes will have the first instance
1209 // take precedence (this can happen for declaration attributes).
1210 //----------------------------------------------------------------------
1211 size_t
1212 DWARFDebugInfoEntry::GetAttributes
1213 (
1214     SymbolFileDWARF* dwarf2Data,
1215     const DWARFCompileUnit* cu,
1216     const uint8_t *fixed_form_sizes,
1217     DWARFDebugInfoEntry::Attributes& attributes,
1218     uint32_t curr_depth
1219 ) const
1220 {
1221     lldb::offset_t offset;
1222     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1223
1224     if (abbrevDecl)
1225     {
1226         const DWARFDataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
1227
1228         if (fixed_form_sizes == NULL)
1229             fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(cu->GetAddressByteSize());
1230
1231         const uint32_t num_attributes = abbrevDecl->NumAttributes();
1232         uint32_t i;
1233         dw_attr_t attr;
1234         dw_form_t form;
1235         DWARFFormValue form_value;
1236         for (i=0; i<num_attributes; ++i)
1237         {
1238             abbrevDecl->GetAttrAndFormByIndexUnchecked (i, attr, form);
1239             
1240             // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
1241             // attributes, the depth will be non-zero. We need to omit certain
1242             // attributes that don't make sense.
1243             switch (attr)
1244             {
1245             case DW_AT_sibling:
1246             case DW_AT_declaration:
1247                 if (curr_depth > 0)
1248                 {
1249                     // This attribute doesn't make sense when combined with
1250                     // the DIE that references this DIE. We know a DIE is 
1251                     // referencing this DIE because curr_depth is not zero
1252                     break;  
1253                 }
1254                 // Fall through...
1255             default:
1256                 attributes.Append(cu, offset, attr, form);
1257                 break;
1258             }
1259
1260             if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin))
1261             {
1262                 form_value.SetForm(form);
1263                 if (form_value.ExtractValue(debug_info_data, &offset, cu))
1264                 {
1265                     const DWARFDebugInfoEntry* die = NULL;
1266                     dw_offset_t die_offset = form_value.Reference(cu);
1267                     if (cu->ContainsDIEOffset(die_offset))
1268                     {
1269                         die = const_cast<DWARFCompileUnit*>(cu)->GetDIEPtr(die_offset);
1270                         if (die)
1271                             die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes, curr_depth + 1);
1272                     }
1273                     else
1274                     {
1275                         DWARFCompileUnitSP cu_sp_ptr;
1276                         die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr);
1277                         if (die)
1278                             die->GetAttributes(dwarf2Data, cu_sp_ptr.get(), fixed_form_sizes, attributes, curr_depth + 1);
1279                     }
1280                 }
1281             }
1282             else
1283             {
1284                 const uint8_t fixed_skip_size = fixed_form_sizes [form];
1285                 if (fixed_skip_size)
1286                     offset += fixed_skip_size;
1287                 else
1288                     DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu);
1289             }
1290         }
1291     }
1292     else
1293     {
1294         attributes.Clear();
1295     }
1296     return attributes.Size();
1297
1298 }
1299
1300 //----------------------------------------------------------------------
1301 // GetAttributeValue
1302 //
1303 // Get the value of an attribute and return the .debug_info offset of the
1304 // attribute if it was properly extracted into form_value, or zero
1305 // if we fail since an offset of zero is invalid for an attribute (it
1306 // would be a compile unit header).
1307 //----------------------------------------------------------------------
1308 dw_offset_t
1309 DWARFDebugInfoEntry::GetAttributeValue
1310 (
1311     SymbolFileDWARF* dwarf2Data,
1312     const DWARFCompileUnit* cu,
1313     const dw_attr_t attr,
1314     DWARFFormValue& form_value,
1315     dw_offset_t* end_attr_offset_ptr
1316 ) const
1317 {
1318     lldb::offset_t offset;
1319     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1320
1321     if (abbrevDecl)
1322     {
1323         uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
1324
1325         if (attr_idx != DW_INVALID_INDEX)
1326         {
1327             const DWARFDataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
1328
1329             uint32_t idx=0;
1330             while (idx<attr_idx)
1331                 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), debug_info_data, &offset, cu);
1332
1333             const dw_offset_t attr_offset = offset;
1334             form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
1335             if (form_value.ExtractValue(debug_info_data, &offset, cu))
1336             {
1337                 if (end_attr_offset_ptr)
1338                     *end_attr_offset_ptr = offset;
1339                 return attr_offset;
1340             }
1341         }
1342     }
1343
1344     return 0;
1345 }
1346
1347 //----------------------------------------------------------------------
1348 // GetAttributeValueAsString
1349 //
1350 // Get the value of an attribute as a string return it. The resulting
1351 // pointer to the string data exists within the supplied SymbolFileDWARF
1352 // and will only be available as long as the SymbolFileDWARF is still around
1353 // and it's content doesn't change.
1354 //----------------------------------------------------------------------
1355 const char*
1356 DWARFDebugInfoEntry::GetAttributeValueAsString
1357 (
1358     SymbolFileDWARF* dwarf2Data,
1359     const DWARFCompileUnit* cu,
1360     const dw_attr_t attr,
1361     const char* fail_value) const
1362 {
1363     DWARFFormValue form_value;
1364     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1365         return form_value.AsCString(&dwarf2Data->get_debug_str_data());
1366     return fail_value;
1367 }
1368
1369 //----------------------------------------------------------------------
1370 // GetAttributeValueAsUnsigned
1371 //
1372 // Get the value of an attribute as unsigned and return it.
1373 //----------------------------------------------------------------------
1374 uint64_t
1375 DWARFDebugInfoEntry::GetAttributeValueAsUnsigned
1376 (
1377     SymbolFileDWARF* dwarf2Data,
1378     const DWARFCompileUnit* cu,
1379     const dw_attr_t attr,
1380     uint64_t fail_value
1381 ) const
1382 {
1383     DWARFFormValue form_value;
1384     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1385         return form_value.Unsigned();
1386     return fail_value;
1387 }
1388
1389 //----------------------------------------------------------------------
1390 // GetAttributeValueAsSigned
1391 //
1392 // Get the value of an attribute a signed value and return it.
1393 //----------------------------------------------------------------------
1394 int64_t
1395 DWARFDebugInfoEntry::GetAttributeValueAsSigned
1396 (
1397     SymbolFileDWARF* dwarf2Data,
1398     const DWARFCompileUnit* cu,
1399     const dw_attr_t attr,
1400     int64_t fail_value
1401 ) const
1402 {
1403     DWARFFormValue form_value;
1404     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1405         return form_value.Signed();
1406     return fail_value;
1407 }
1408
1409 //----------------------------------------------------------------------
1410 // GetAttributeValueAsReference
1411 //
1412 // Get the value of an attribute as reference and fix up and compile
1413 // unit relative offsets as needed.
1414 //----------------------------------------------------------------------
1415 uint64_t
1416 DWARFDebugInfoEntry::GetAttributeValueAsReference
1417 (
1418     SymbolFileDWARF* dwarf2Data,
1419     const DWARFCompileUnit* cu,
1420     const dw_attr_t attr,
1421     uint64_t fail_value
1422 ) const
1423 {
1424     DWARFFormValue form_value;
1425     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1426         return form_value.Reference(cu);
1427     return fail_value;
1428 }
1429
1430 //----------------------------------------------------------------------
1431 // GetAttributeHighPC
1432 //
1433 // Get the hi_pc, adding hi_pc to lo_pc when specified
1434 // as an <offset-from-low-pc>.
1435 //
1436 // Returns the hi_pc or fail_value.
1437 //----------------------------------------------------------------------
1438 dw_addr_t
1439 DWARFDebugInfoEntry::GetAttributeHighPC
1440 (
1441     SymbolFileDWARF* dwarf2Data,
1442     const DWARFCompileUnit* cu,
1443     dw_addr_t lo_pc,
1444     uint64_t fail_value
1445 ) const
1446 {
1447     DWARFFormValue form_value;
1448
1449     if (GetAttributeValue(dwarf2Data, cu, DW_AT_high_pc, form_value))
1450     {
1451         dw_addr_t hi_pc = form_value.Unsigned();
1452         if (form_value.Form() != DW_FORM_addr)
1453             hi_pc += lo_pc; // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
1454         return hi_pc; 
1455     }
1456     return fail_value;
1457 }
1458
1459 //----------------------------------------------------------------------
1460 // GetAttributeAddressRange
1461 //
1462 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified
1463 // as an <offset-from-low-pc>.
1464 //
1465 // Returns true or sets lo_pc and hi_pc to fail_value.
1466 //----------------------------------------------------------------------
1467 bool
1468 DWARFDebugInfoEntry::GetAttributeAddressRange
1469 (
1470     SymbolFileDWARF* dwarf2Data,
1471     const DWARFCompileUnit* cu,
1472     dw_addr_t& lo_pc,
1473     dw_addr_t& hi_pc,
1474     uint64_t fail_value
1475 ) const
1476 {
1477     lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, fail_value);
1478     if (lo_pc != fail_value)
1479     {
1480         hi_pc = GetAttributeHighPC(dwarf2Data, cu, lo_pc, fail_value);
1481         if (hi_pc != fail_value)
1482           return true;
1483     }
1484     lo_pc = fail_value;
1485     hi_pc = fail_value;
1486     return false;
1487 }
1488
1489 size_t
1490 DWARFDebugInfoEntry::GetAttributeAddressRanges(SymbolFileDWARF* dwarf2Data,
1491                                                const DWARFCompileUnit* cu,
1492                                                DWARFDebugRanges::RangeList &ranges,
1493                                                bool check_hi_lo_pc) const
1494 {
1495     ranges.Clear();
1496     
1497     dw_offset_t ranges_offset = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET);
1498     if (ranges_offset != DW_INVALID_OFFSET)
1499     {
1500         dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET);
1501         if (debug_ranges_offset != DW_INVALID_OFFSET)
1502         {
1503             DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges();
1504             
1505             debug_ranges->FindRanges(debug_ranges_offset, ranges);
1506             ranges.Slide (cu->GetBaseAddress());
1507         }
1508     }
1509     else if (check_hi_lo_pc)
1510     {
1511         dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1512         dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1513         if (GetAttributeAddressRange (dwarf2Data, cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS))
1514         {
1515             if (lo_pc < hi_pc)
1516                 ranges.Append(DWARFDebugRanges::RangeList::Entry(lo_pc, hi_pc - lo_pc));
1517         }
1518     }
1519     return ranges.GetSize();
1520 }
1521
1522 //----------------------------------------------------------------------
1523 // GetAttributeValueAsLocation
1524 //
1525 // Get the value of an attribute as reference and fix up and compile
1526 // unit relative offsets as needed.
1527 //----------------------------------------------------------------------
1528 dw_offset_t
1529 DWARFDebugInfoEntry::GetAttributeValueAsLocation
1530 (
1531     SymbolFileDWARF* dwarf2Data,
1532     const DWARFCompileUnit* cu,
1533     const dw_attr_t attr,
1534     DWARFDataExtractor& location_data,
1535     uint32_t &block_size
1536 ) const
1537 {
1538     block_size = 0;
1539     DWARFFormValue form_value;
1540
1541     // Empty out data in case we don't find anything
1542     location_data.Clear();
1543     dw_offset_t end_addr_offset = DW_INVALID_OFFSET;
1544     const dw_offset_t attr_offset = GetAttributeValue(dwarf2Data, cu, attr, form_value, &end_addr_offset);
1545     if (attr_offset)
1546     {
1547         const uint8_t* blockData = form_value.BlockData();
1548         if (blockData)
1549         {
1550             // We have an inlined location list in the .debug_info section
1551             const DWARFDataExtractor& debug_info = dwarf2Data->get_debug_info_data();
1552             dw_offset_t block_offset = blockData - debug_info.GetDataStart();
1553             block_size = (end_addr_offset - attr_offset) - form_value.Unsigned();
1554             location_data.SetData(debug_info, block_offset, block_size);
1555         }
1556         else
1557         {
1558             // We have a location list offset as the value that is
1559             // the offset into the .debug_loc section that describes
1560             // the value over it's lifetime
1561             lldb::offset_t debug_loc_offset = form_value.Unsigned();
1562             if (dwarf2Data)
1563             {
1564                 assert(dwarf2Data->get_debug_loc_data().GetAddressByteSize() == cu->GetAddressByteSize());
1565                 return DWARFLocationList::Extract(dwarf2Data->get_debug_loc_data(), &debug_loc_offset, location_data);
1566             }
1567         }
1568     }
1569     return attr_offset;
1570 }
1571
1572 //----------------------------------------------------------------------
1573 // GetName
1574 //
1575 // Get value of the DW_AT_name attribute and return it if one exists,
1576 // else return NULL.
1577 //----------------------------------------------------------------------
1578 const char*
1579 DWARFDebugInfoEntry::GetName
1580 (
1581     SymbolFileDWARF* dwarf2Data,
1582     const DWARFCompileUnit* cu
1583 ) const
1584 {
1585     DWARFFormValue form_value;
1586     if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1587         return form_value.AsCString(&dwarf2Data->get_debug_str_data());
1588     else
1589     {
1590         if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value))
1591         {
1592             DWARFCompileUnitSP cu_sp_ptr;
1593             const DWARFDebugInfoEntry* die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(form_value.Reference(cu), &cu_sp_ptr);
1594             if (die)
1595                 return die->GetName(dwarf2Data, cu_sp_ptr.get());
1596         }
1597     }
1598     return NULL;
1599 }
1600
1601
1602 //----------------------------------------------------------------------
1603 // GetMangledName
1604 //
1605 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if
1606 // one exists, else return the value of the DW_AT_name attribute
1607 //----------------------------------------------------------------------
1608 const char*
1609 DWARFDebugInfoEntry::GetMangledName
1610 (
1611     SymbolFileDWARF* dwarf2Data,
1612     const DWARFCompileUnit* cu,
1613     bool substitute_name_allowed
1614 ) const
1615 {
1616     const char* name = NULL;
1617     DWARFFormValue form_value;
1618
1619     if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value))
1620         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1621
1622     if (GetAttributeValue(dwarf2Data, cu, DW_AT_linkage_name, form_value))
1623         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1624
1625     if (substitute_name_allowed && name == NULL)
1626     {
1627         if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1628             name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1629     }
1630     return name;
1631 }
1632
1633
1634 //----------------------------------------------------------------------
1635 // GetPubname
1636 //
1637 // Get value the name for a DIE as it should appear for a
1638 // .debug_pubnames or .debug_pubtypes section.
1639 //----------------------------------------------------------------------
1640 const char*
1641 DWARFDebugInfoEntry::GetPubname
1642 (
1643     SymbolFileDWARF* dwarf2Data,
1644     const DWARFCompileUnit* cu
1645 ) const
1646 {
1647     const char* name = NULL;
1648     if (!dwarf2Data)
1649         return name;
1650     
1651     DWARFFormValue form_value;
1652
1653     if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value))
1654         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1655     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_linkage_name, form_value))
1656         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1657     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1658         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1659     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value))
1660     {
1661         // The specification DIE may be in another compile unit so we need
1662         // to get a die and its compile unit.
1663         DWARFCompileUnitSP cu_sp_ptr;
1664         const DWARFDebugInfoEntry* die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(form_value.Reference(cu), &cu_sp_ptr);
1665         if (die)
1666             return die->GetPubname(dwarf2Data, cu_sp_ptr.get());
1667     }
1668     return name;
1669 }
1670
1671
1672 //----------------------------------------------------------------------
1673 // GetName
1674 //
1675 // Get value of the DW_AT_name attribute for a debug information entry
1676 // that exists at offset "die_offset" and place that value into the
1677 // supplied stream object. If the DIE is a NULL object "NULL" is placed
1678 // into the stream, and if no DW_AT_name attribute exists for the DIE
1679 // then nothing is printed.
1680 //----------------------------------------------------------------------
1681 bool
1682 DWARFDebugInfoEntry::GetName
1683 (
1684     SymbolFileDWARF* dwarf2Data,
1685     const DWARFCompileUnit* cu,
1686     const dw_offset_t die_offset,
1687     Stream &s
1688 )
1689 {
1690     if (dwarf2Data == NULL)
1691     {
1692         s.PutCString("NULL");
1693         return false;
1694     }
1695     
1696     DWARFDebugInfoEntry die;
1697     lldb::offset_t offset = die_offset;
1698     if (die.Extract(dwarf2Data, cu, &offset))
1699     {
1700         if (die.IsNULL())
1701         {
1702             s.PutCString("NULL");
1703             return true;
1704         }
1705         else
1706         {
1707             DWARFFormValue form_value;
1708             if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1709             {
1710                 const char* name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1711                 if (name)
1712                 {
1713                     s.PutCString(name);
1714                     return true;
1715                 }
1716             }
1717         }
1718     }
1719     return false;
1720 }
1721
1722 //----------------------------------------------------------------------
1723 // AppendTypeName
1724 //
1725 // Follows the type name definition down through all needed tags to
1726 // end up with a fully qualified type name and dump the results to
1727 // the supplied stream. This is used to show the name of types given
1728 // a type identifier.
1729 //----------------------------------------------------------------------
1730 bool
1731 DWARFDebugInfoEntry::AppendTypeName
1732 (
1733     SymbolFileDWARF* dwarf2Data,
1734     const DWARFCompileUnit* cu,
1735     const dw_offset_t die_offset,
1736     Stream &s
1737 )
1738 {
1739     if (dwarf2Data == NULL)
1740     {
1741         s.PutCString("NULL");
1742         return false;
1743     }
1744     
1745     DWARFDebugInfoEntry die;
1746     lldb::offset_t offset = die_offset;
1747     if (die.Extract(dwarf2Data, cu, &offset))
1748     {
1749         if (die.IsNULL())
1750         {
1751             s.PutCString("NULL");
1752             return true;
1753         }
1754         else
1755         {
1756             const char* name = die.GetPubname(dwarf2Data, cu);
1757         //  if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1758         //      name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1759             if (name)
1760                 s.PutCString(name);
1761             else
1762             {
1763                 bool result = true;
1764                 const DWARFAbbreviationDeclaration* abbrevDecl = die.GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1765                 
1766                 if (abbrevDecl == NULL)
1767                     return false;
1768
1769                 switch (abbrevDecl->Tag())
1770                 {
1771                 case DW_TAG_array_type:         break;  // print out a "[]" after printing the full type of the element below
1772                 case DW_TAG_base_type:          s.PutCString("base ");         break;
1773                 case DW_TAG_class_type:         s.PutCString("class ");            break;
1774                 case DW_TAG_const_type:         s.PutCString("const ");            break;
1775                 case DW_TAG_enumeration_type:   s.PutCString("enum ");         break;
1776                 case DW_TAG_file_type:          s.PutCString("file ");         break;
1777                 case DW_TAG_interface_type:     s.PutCString("interface ");        break;
1778                 case DW_TAG_packed_type:        s.PutCString("packed ");       break;
1779                 case DW_TAG_pointer_type:       break;  // print out a '*' after printing the full type below
1780                 case DW_TAG_ptr_to_member_type: break;  // print out a '*' after printing the full type below
1781                 case DW_TAG_reference_type:     break;  // print out a '&' after printing the full type below
1782                 case DW_TAG_restrict_type:      s.PutCString("restrict ");     break;
1783                 case DW_TAG_set_type:           s.PutCString("set ");          break;
1784                 case DW_TAG_shared_type:        s.PutCString("shared ");       break;
1785                 case DW_TAG_string_type:        s.PutCString("string ");       break;
1786                 case DW_TAG_structure_type:     s.PutCString("struct ");       break;
1787                 case DW_TAG_subrange_type:      s.PutCString("subrange ");     break;
1788                 case DW_TAG_subroutine_type:    s.PutCString("function ");     break;
1789                 case DW_TAG_thrown_type:        s.PutCString("thrown ");       break;
1790                 case DW_TAG_union_type:         s.PutCString("union ");            break;
1791                 case DW_TAG_unspecified_type:   s.PutCString("unspecified ");  break;
1792                 case DW_TAG_volatile_type:      s.PutCString("volatile ");     break;
1793                 default:
1794                     return false;
1795                 }
1796
1797                 // Follow the DW_AT_type if possible
1798                 DWARFFormValue form_value;
1799                 if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_type, form_value))
1800                 {
1801                     uint64_t next_die_offset = form_value.Reference(cu);
1802                     result = AppendTypeName(dwarf2Data, cu, next_die_offset, s);
1803                 }
1804
1805                 switch (abbrevDecl->Tag())
1806                 {
1807                 case DW_TAG_array_type:         s.PutCString("[]");    break;
1808                 case DW_TAG_pointer_type:       s.PutChar('*');    break;
1809                 case DW_TAG_ptr_to_member_type: s.PutChar('*');    break;
1810                 case DW_TAG_reference_type:     s.PutChar('&');    break;
1811                 default:
1812                     break;
1813                 }
1814                 return result;
1815             }
1816         }
1817     }
1818     return false;
1819 }
1820
1821 bool
1822 DWARFDebugInfoEntry::Contains (const DWARFDebugInfoEntry *die) const
1823 {
1824     if (die)
1825     {
1826         const dw_offset_t die_offset = die->GetOffset();
1827         if (die_offset > GetOffset())
1828         {
1829             const DWARFDebugInfoEntry *sibling = GetSibling();
1830             assert (sibling); // TODO: take this out
1831             if (sibling)
1832                 return die_offset < sibling->GetOffset();
1833         }
1834     }
1835     return false;
1836 }
1837
1838 //----------------------------------------------------------------------
1839 // BuildAddressRangeTable
1840 //----------------------------------------------------------------------
1841 void
1842 DWARFDebugInfoEntry::BuildAddressRangeTable
1843 (
1844     SymbolFileDWARF* dwarf2Data,
1845     const DWARFCompileUnit* cu,
1846     DWARFDebugAranges* debug_aranges
1847 ) const
1848 {
1849     if (m_tag)
1850     {
1851         if (m_tag == DW_TAG_subprogram)
1852         {
1853             dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1854             dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1855             if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS))
1856             {
1857                 /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x - 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc);
1858                 debug_aranges->AppendRange (cu->GetOffset(), lo_pc, hi_pc);
1859             }
1860         }
1861
1862
1863         const DWARFDebugInfoEntry* child = GetFirstChild();
1864         while (child)
1865         {
1866             child->BuildAddressRangeTable(dwarf2Data, cu, debug_aranges);
1867             child = child->GetSibling();
1868         }
1869     }
1870 }
1871
1872 //----------------------------------------------------------------------
1873 // BuildFunctionAddressRangeTable
1874 //
1875 // This function is very similar to the BuildAddressRangeTable function
1876 // except that the actual DIE offset for the function is placed in the
1877 // table instead of the compile unit offset (which is the way the
1878 // standard .debug_aranges section does it).
1879 //----------------------------------------------------------------------
1880 void
1881 DWARFDebugInfoEntry::BuildFunctionAddressRangeTable
1882 (
1883     SymbolFileDWARF* dwarf2Data,
1884     const DWARFCompileUnit* cu,
1885     DWARFDebugAranges* debug_aranges
1886 ) const
1887 {
1888     if (m_tag)
1889     {
1890         if (m_tag == DW_TAG_subprogram)
1891         {
1892             dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1893             dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1894             if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS))
1895             {
1896             //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
1897                 debug_aranges->AppendRange (GetOffset(), lo_pc, hi_pc);
1898             }
1899         }
1900
1901         const DWARFDebugInfoEntry* child = GetFirstChild();
1902         while (child)
1903         {
1904             child->BuildFunctionAddressRangeTable(dwarf2Data, cu, debug_aranges);
1905             child = child->GetSibling();
1906         }
1907     }
1908 }
1909
1910 void
1911 DWARFDebugInfoEntry::GetDeclContextDIEs (SymbolFileDWARF* dwarf2Data, 
1912                                          DWARFCompileUnit* cu,
1913                                          DWARFDIECollection &decl_context_dies) const
1914 {
1915     const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1916     if (parent_decl_ctx_die && parent_decl_ctx_die != this)
1917     {
1918         decl_context_dies.Append(parent_decl_ctx_die);
1919         parent_decl_ctx_die->GetDeclContextDIEs (dwarf2Data, cu, decl_context_dies);
1920     }
1921 }
1922
1923 void
1924 DWARFDebugInfoEntry::GetDWARFDeclContext (SymbolFileDWARF* dwarf2Data,
1925                                           DWARFCompileUnit* cu,
1926                                           DWARFDeclContext &dwarf_decl_ctx) const
1927 {
1928     const dw_tag_t tag = Tag();
1929     if (tag != DW_TAG_compile_unit)
1930     {
1931         dwarf_decl_ctx.AppendDeclContext(tag, GetName(dwarf2Data, cu));
1932         const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1933         if (parent_decl_ctx_die && parent_decl_ctx_die != this)
1934         {
1935             if (parent_decl_ctx_die->Tag() != DW_TAG_compile_unit)
1936                 parent_decl_ctx_die->GetDWARFDeclContext (dwarf2Data, cu, dwarf_decl_ctx);
1937         }
1938     }
1939 }
1940
1941
1942 bool
1943 DWARFDebugInfoEntry::MatchesDWARFDeclContext (SymbolFileDWARF* dwarf2Data,
1944                                               DWARFCompileUnit* cu,
1945                                               const DWARFDeclContext &dwarf_decl_ctx) const
1946 {
1947     
1948     DWARFDeclContext this_dwarf_decl_ctx;
1949     GetDWARFDeclContext (dwarf2Data, cu, this_dwarf_decl_ctx);
1950     return this_dwarf_decl_ctx == dwarf_decl_ctx;
1951 }
1952
1953 const DWARFDebugInfoEntry *
1954 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data, 
1955                                                                                           DWARFCompileUnit* cu) const
1956 {
1957         DWARFDebugInfoEntry::Attributes attributes;
1958         GetAttributes(dwarf2Data, cu, NULL, attributes);
1959         return GetParentDeclContextDIE (dwarf2Data, cu, attributes);
1960 }
1961
1962 const DWARFDebugInfoEntry *
1963 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data, 
1964                                                                                           DWARFCompileUnit* cu,
1965                                                                                           const DWARFDebugInfoEntry::Attributes& attributes) const
1966 {
1967         const DWARFDebugInfoEntry * die = this;
1968         
1969         while (die != NULL)
1970         {
1971                 // If this is the original DIE that we are searching for a declaration 
1972                 // for, then don't look in the cache as we don't want our own decl 
1973                 // context to be our decl context...
1974                 if (die != this)
1975                 {            
1976                         switch (die->Tag())
1977                         {
1978                                 case DW_TAG_compile_unit:
1979                                 case DW_TAG_namespace:
1980                                 case DW_TAG_structure_type:
1981                                 case DW_TAG_union_type:
1982                                 case DW_TAG_class_type:
1983                                         return die;
1984                                         
1985                                 default:
1986                                         break;
1987                         }
1988                 }
1989                 
1990                 dw_offset_t die_offset;
1991         
1992                 die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_specification, DW_INVALID_OFFSET);
1993                 if (die_offset != DW_INVALID_OFFSET)
1994                 {
1995                         const DWARFDebugInfoEntry *spec_die = cu->GetDIEPtr (die_offset);
1996                         if (spec_die)
1997                         {
1998                                 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = spec_die->GetParentDeclContextDIE (dwarf2Data, cu);
1999                                 if (spec_die_decl_ctx_die)
2000                                         return spec_die_decl_ctx_die;
2001                         }
2002                 }
2003                 
2004         die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_abstract_origin, DW_INVALID_OFFSET);
2005                 if (die_offset != DW_INVALID_OFFSET)
2006                 {
2007                         const DWARFDebugInfoEntry *abs_die = cu->GetDIEPtr (die_offset);
2008                         if (abs_die)
2009                         {
2010                                 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = abs_die->GetParentDeclContextDIE (dwarf2Data, cu);
2011                                 if (abs_die_decl_ctx_die)
2012                                         return abs_die_decl_ctx_die;
2013                         }
2014                 }
2015                 
2016                 die = die->GetParent();
2017         }
2018     return NULL;
2019 }
2020
2021
2022 const char *
2023 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data, 
2024                                                                            DWARFCompileUnit* cu,
2025                                                                            std::string &storage) const
2026 {
2027         DWARFDebugInfoEntry::Attributes attributes;
2028         GetAttributes(dwarf2Data, cu, NULL, attributes);
2029         return GetQualifiedName (dwarf2Data, cu, attributes, storage);
2030 }
2031
2032 const char*
2033 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data, 
2034                                                                            DWARFCompileUnit* cu,
2035                                                                            const DWARFDebugInfoEntry::Attributes& attributes,
2036                                                                            std::string &storage) const
2037 {
2038         
2039         const char *name = GetName (dwarf2Data, cu);
2040         
2041         if (name)
2042         {
2043                 const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
2044                 storage.clear();
2045                 // TODO: change this to get the correct decl context parent....
2046                 while (parent_decl_ctx_die)
2047                 {
2048                         const dw_tag_t parent_tag = parent_decl_ctx_die->Tag();
2049                         switch (parent_tag)
2050                         {
2051                 case DW_TAG_namespace:
2052                                 {
2053                                         const char *namespace_name = parent_decl_ctx_die->GetName (dwarf2Data, cu);
2054                                         if (namespace_name)
2055                                         {
2056                                                 storage.insert (0, "::");
2057                                                 storage.insert (0, namespace_name);
2058                                         }
2059                                         else
2060                                         {
2061                                                 storage.insert (0, "(anonymous namespace)::");
2062                                         }
2063                                         parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu);
2064                                 }
2065                     break;
2066                                         
2067                 case DW_TAG_class_type:
2068                 case DW_TAG_structure_type:
2069                 case DW_TAG_union_type:
2070                                 {
2071                                         const char *class_union_struct_name = parent_decl_ctx_die->GetName (dwarf2Data, cu);
2072                     
2073                                         if (class_union_struct_name)
2074                                         {
2075                                                 storage.insert (0, "::");
2076                                                 storage.insert (0, class_union_struct_name);
2077                                         }
2078                                         parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu);
2079                                 }
2080                     break;
2081                     
2082                 default:
2083                     parent_decl_ctx_die = NULL;
2084                     break;
2085                         }
2086                 }
2087                 
2088                 if (storage.empty())
2089                         storage.append ("::");
2090         
2091                 storage.append (name);
2092         }
2093         if (storage.empty())
2094                 return NULL;
2095         return storage.c_str();
2096 }
2097
2098
2099 //----------------------------------------------------------------------
2100 // LookupAddress
2101 //----------------------------------------------------------------------
2102 bool
2103 DWARFDebugInfoEntry::LookupAddress
2104 (
2105     const dw_addr_t address,
2106     SymbolFileDWARF* dwarf2Data,
2107     const DWARFCompileUnit* cu,
2108     DWARFDebugInfoEntry** function_die,
2109     DWARFDebugInfoEntry** block_die
2110 )
2111 {
2112     bool found_address = false;
2113     if (m_tag)
2114     {
2115         bool check_children = false;
2116         bool match_addr_range = false;
2117     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset, DW_TAG_value_to_name(tag), address);
2118         switch (m_tag)
2119         {
2120         case DW_TAG_array_type                 : break;
2121         case DW_TAG_class_type                 : check_children = true; break;
2122         case DW_TAG_entry_point                : break;
2123         case DW_TAG_enumeration_type           : break;
2124         case DW_TAG_formal_parameter           : break;
2125         case DW_TAG_imported_declaration       : break;
2126         case DW_TAG_label                      : break;
2127         case DW_TAG_lexical_block              : check_children = true; match_addr_range = true; break;
2128         case DW_TAG_member                     : break;
2129         case DW_TAG_pointer_type               : break;
2130         case DW_TAG_reference_type             : break;
2131         case DW_TAG_compile_unit               : match_addr_range = true; break;
2132         case DW_TAG_string_type                : break;
2133         case DW_TAG_structure_type             : check_children = true; break;
2134         case DW_TAG_subroutine_type            : break;
2135         case DW_TAG_typedef                    : break;
2136         case DW_TAG_union_type                 : break;
2137         case DW_TAG_unspecified_parameters     : break;
2138         case DW_TAG_variant                    : break;
2139         case DW_TAG_common_block               : check_children = true; break;
2140         case DW_TAG_common_inclusion           : break;
2141         case DW_TAG_inheritance                : break;
2142         case DW_TAG_inlined_subroutine         : check_children = true; match_addr_range = true; break;
2143         case DW_TAG_module                     : match_addr_range = true; break;
2144         case DW_TAG_ptr_to_member_type         : break;
2145         case DW_TAG_set_type                   : break;
2146         case DW_TAG_subrange_type              : break;
2147         case DW_TAG_with_stmt                  : break;
2148         case DW_TAG_access_declaration         : break;
2149         case DW_TAG_base_type                  : break;
2150         case DW_TAG_catch_block                : match_addr_range = true; break;
2151         case DW_TAG_const_type                 : break;
2152         case DW_TAG_constant                   : break;
2153         case DW_TAG_enumerator                 : break;
2154         case DW_TAG_file_type                  : break;
2155         case DW_TAG_friend                     : break;
2156         case DW_TAG_namelist                   : break;
2157         case DW_TAG_namelist_item              : break;
2158         case DW_TAG_packed_type                : break;
2159         case DW_TAG_subprogram                 : match_addr_range = true; break;
2160         case DW_TAG_template_type_parameter    : break;
2161         case DW_TAG_template_value_parameter   : break;
2162         case DW_TAG_thrown_type                : break;
2163         case DW_TAG_try_block                  : match_addr_range = true; break;
2164         case DW_TAG_variant_part               : break;
2165         case DW_TAG_variable                   : break;
2166         case DW_TAG_volatile_type              : break;
2167         case DW_TAG_dwarf_procedure            : break;
2168         case DW_TAG_restrict_type              : break;
2169         case DW_TAG_interface_type             : break;
2170         case DW_TAG_namespace                  : check_children = true; break;
2171         case DW_TAG_imported_module            : break;
2172         case DW_TAG_unspecified_type           : break;
2173         case DW_TAG_partial_unit               : break;
2174         case DW_TAG_imported_unit              : break;
2175         case DW_TAG_shared_type                : break;
2176         default: break;
2177         }
2178
2179         if (match_addr_range)
2180         {
2181             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
2182             if (lo_pc != LLDB_INVALID_ADDRESS)
2183             {
2184                 dw_addr_t hi_pc = GetAttributeHighPC(dwarf2Data, cu, lo_pc, LLDB_INVALID_ADDRESS);
2185                 if (hi_pc != LLDB_INVALID_ADDRESS)
2186                 {
2187                     //  printf("\n0x%8.8x: %30s: address = 0x%8.8x  [0x%8.8x - 0x%8.8x) ", m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc);
2188                     if ((lo_pc <= address) && (address < hi_pc))
2189                     {
2190                         found_address = true;
2191                     //  puts("***MATCH***");
2192                         switch (m_tag)
2193                         {
2194                         case DW_TAG_compile_unit:       // File
2195                             check_children = ((function_die != NULL) || (block_die != NULL));
2196                             break;
2197
2198                         case DW_TAG_subprogram:         // Function
2199                             if (function_die)
2200                                 *function_die = this;
2201                             check_children = (block_die != NULL);
2202                             break;
2203
2204                         case DW_TAG_inlined_subroutine: // Inlined Function
2205                         case DW_TAG_lexical_block:      // Block { } in code
2206                             if (block_die)
2207                             {
2208                                 *block_die = this;
2209                                 check_children = true;
2210                             }
2211                             break;
2212
2213                         default:
2214                             check_children = true;
2215                             break;
2216                         }
2217                     }
2218                 }
2219                 else
2220                 {   // compile units may not have a valid high/low pc when there
2221                     // are address gaps in subroutines so we must always search
2222                     // if there is no valid high and low PC
2223                     check_children = (m_tag == DW_TAG_compile_unit) && ((function_die != NULL) || (block_die != NULL));
2224                 }
2225             }
2226             else
2227             {
2228                 dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET);
2229                 if (debug_ranges_offset != DW_INVALID_OFFSET)
2230                 {
2231                     DWARFDebugRanges::RangeList ranges;
2232                     DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges();
2233                     debug_ranges->FindRanges(debug_ranges_offset, ranges);
2234                     // All DW_AT_ranges are relative to the base address of the
2235                     // compile unit. We add the compile unit base address to make
2236                     // sure all the addresses are properly fixed up.
2237                     ranges.Slide (cu->GetBaseAddress());
2238                     if (ranges.FindEntryThatContains(address))
2239                     {
2240                         found_address = true;
2241                     //  puts("***MATCH***");
2242                         switch (m_tag)
2243                         {
2244                         case DW_TAG_compile_unit:       // File
2245                             check_children = ((function_die != NULL) || (block_die != NULL));
2246                             break;
2247
2248                         case DW_TAG_subprogram:         // Function
2249                             if (function_die)
2250                                 *function_die = this;
2251                             check_children = (block_die != NULL);
2252                             break;
2253
2254                         case DW_TAG_inlined_subroutine: // Inlined Function
2255                         case DW_TAG_lexical_block:      // Block { } in code
2256                             if (block_die)
2257                             {
2258                                 *block_die = this;
2259                                 check_children = true;
2260                             }
2261                             break;
2262
2263                         default:
2264                             check_children = true;
2265                             break;
2266                         }
2267                     }
2268                     else
2269                     {
2270                         check_children = false;
2271                     }
2272                 }
2273             }
2274         }
2275
2276
2277         if (check_children)
2278         {
2279         //  printf("checking children\n");
2280             DWARFDebugInfoEntry* child = GetFirstChild();
2281             while (child)
2282             {
2283                 if (child->LookupAddress(address, dwarf2Data, cu, function_die, block_die))
2284                     return true;
2285                 child = child->GetSibling();
2286             }
2287         }
2288     }
2289     return found_address;
2290 }
2291
2292 const DWARFAbbreviationDeclaration* 
2293 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr (SymbolFileDWARF* dwarf2Data,
2294                                                     const DWARFCompileUnit *cu,
2295                                                     lldb::offset_t &offset) const
2296 {
2297     if (dwarf2Data)
2298     {
2299         offset = GetOffset();
2300         
2301         const DWARFAbbreviationDeclaration* abbrev_decl = cu->GetAbbreviations()->GetAbbreviationDeclaration (m_abbr_idx);
2302         if (abbrev_decl)
2303         {
2304             // Make sure the abbreviation code still matches. If it doesn't and
2305             // the DWARF data was mmap'ed, the backing file might have been modified
2306             // which is bad news.
2307             const uint64_t abbrev_code = dwarf2Data->get_debug_info_data().GetULEB128 (&offset);
2308         
2309             if (abbrev_decl->Code() == abbrev_code)
2310                 return abbrev_decl;
2311             
2312             dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("0x%8.8x: the DWARF debug information has been modified (abbrev code was %u, and is now %u)", 
2313                                                                                    GetOffset(),
2314                                                                                    (uint32_t)abbrev_decl->Code(),
2315                                                                                    (uint32_t)abbrev_code);
2316         }
2317     }
2318     offset = DW_INVALID_OFFSET;
2319     return NULL;
2320 }
2321
2322
2323 bool
2324 DWARFDebugInfoEntry::OffsetLessThan (const DWARFDebugInfoEntry& a, const DWARFDebugInfoEntry& b)
2325 {
2326     return a.GetOffset() < b.GetOffset();
2327 }
2328
2329 void
2330 DWARFDebugInfoEntry::DumpDIECollection (Stream &strm, DWARFDebugInfoEntry::collection &die_collection)
2331 {
2332     DWARFDebugInfoEntry::const_iterator pos;
2333     DWARFDebugInfoEntry::const_iterator end = die_collection.end();
2334     strm.PutCString("\noffset    parent   sibling  child\n");
2335     strm.PutCString("--------  -------- -------- --------\n");
2336     for (pos = die_collection.begin(); pos != end; ++pos)
2337     {
2338         const DWARFDebugInfoEntry& die_ref = *pos;
2339         const DWARFDebugInfoEntry* p = die_ref.GetParent();
2340         const DWARFDebugInfoEntry* s = die_ref.GetSibling();
2341         const DWARFDebugInfoEntry* c = die_ref.GetFirstChild();
2342         strm.Printf("%.8x: %.8x %.8x %.8x 0x%4.4x %s%s\n", 
2343                     die_ref.GetOffset(),
2344                     p ? p->GetOffset() : 0,
2345                     s ? s->GetOffset() : 0,
2346                     c ? c->GetOffset() : 0,
2347                     die_ref.Tag(), 
2348                     DW_TAG_value_to_name(die_ref.Tag()),
2349                     die_ref.HasChildren() ? " *" : "");
2350     }
2351 }
2352
2353