1 //===-- DWARFAbbreviationDeclaration.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 "DWARFAbbreviationDeclaration.h"
11 
12 #include "lldb/Core/dwarf.h"
13 #include "lldb/Utility/Stream.h"
14 
15 #include "DWARFFormValue.h"
16 
17 using namespace lldb_private;
18 
19 DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration()
20     : m_code(InvalidCode), m_tag(0), m_has_children(0), m_attributes() {}
21 
22 DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration(dw_tag_t tag,
23                                                            uint8_t has_children)
24     : m_code(InvalidCode), m_tag(tag), m_has_children(has_children),
25       m_attributes() {}
26 
27 bool DWARFAbbreviationDeclaration::Extract(const DWARFDataExtractor &data,
28                                            lldb::offset_t *offset_ptr) {
29   return Extract(data, offset_ptr, data.GetULEB128(offset_ptr));
30 }
31 
32 bool DWARFAbbreviationDeclaration::Extract(const DWARFDataExtractor &data,
33                                            lldb::offset_t *offset_ptr,
34                                            dw_uleb128_t code) {
35   m_code = code;
36   m_attributes.clear();
37   if (m_code) {
38     m_tag = data.GetULEB128(offset_ptr);
39     m_has_children = data.GetU8(offset_ptr);
40 
41     while (data.ValidOffset(*offset_ptr)) {
42       dw_attr_t attr = data.GetULEB128(offset_ptr);
43       dw_form_t form = data.GetULEB128(offset_ptr);
44       DWARFFormValue::ValueType val;
45 
46       if (form == DW_FORM_implicit_const)
47         val.value.sval = data.GetULEB128(offset_ptr);
48 
49       if (attr && form)
50         m_attributes.push_back(DWARFAttribute(attr, form, val));
51       else
52         break;
53     }
54 
55     return m_tag != 0;
56   } else {
57     m_tag = 0;
58     m_has_children = 0;
59   }
60 
61   return false;
62 }
63 
64 void DWARFAbbreviationDeclaration::Dump(Stream *s) const {
65   s->Printf("Debug Abbreviation Declaration: code = 0x%4.4x, tag = %s, "
66             "has_children = %s\n",
67             m_code, DW_TAG_value_to_name(m_tag),
68             DW_CHILDREN_value_to_name(m_has_children));
69 
70   DWARFAttribute::const_iterator pos;
71 
72   for (pos = m_attributes.begin(); pos != m_attributes.end(); ++pos)
73     s->Printf("        attr = %s, form = %s\n",
74               DW_AT_value_to_name(pos->get_attr()),
75               DW_FORM_value_to_name(pos->get_form()));
76 
77   s->Printf("\n");
78 }
79 
80 bool DWARFAbbreviationDeclaration::IsValid() {
81   return m_code != 0 && m_tag != 0;
82 }
83 
84 uint32_t
85 DWARFAbbreviationDeclaration::FindAttributeIndex(dw_attr_t attr) const {
86   uint32_t i;
87   const uint32_t kNumAttributes = m_attributes.size();
88   for (i = 0; i < kNumAttributes; ++i) {
89     if (m_attributes[i].get_attr() == attr)
90       return i;
91   }
92   return DW_INVALID_INDEX;
93 }
94 
95 bool DWARFAbbreviationDeclaration::
96 operator==(const DWARFAbbreviationDeclaration &rhs) const {
97   return Tag() == rhs.Tag() && HasChildren() == rhs.HasChildren() &&
98          Attributes() == rhs.Attributes();
99 }
100