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