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