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