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