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