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