LLDB mainline
DWARFDebugAbbrev.cpp
Go to the documentation of this file.
1//===-- DWARFDebugAbbrev.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DWARFDebugAbbrev.h"
10#include "DWARFDataExtractor.h"
11#include "DWARFFormValue.h"
12#include "lldb/Utility/Stream.h"
13
14using namespace lldb;
15using namespace lldb_private;
16
17// DWARFAbbreviationDeclarationSet::Clear()
19 m_idx_offset = 0;
20 m_decls.clear();
21}
22
23// DWARFAbbreviationDeclarationSet::Extract()
24llvm::Error
26 lldb::offset_t *offset_ptr) {
27 llvm::DataExtractor llvm_data = data.GetAsLLVM();
28 const lldb::offset_t begin_offset = *offset_ptr;
29 m_offset = begin_offset;
30 Clear();
31 DWARFAbbreviationDeclaration abbrevDeclaration;
32 uint32_t prev_abbr_code = 0;
33 while (true) {
34 llvm::Expected<llvm::DWARFAbbreviationDeclaration::ExtractState> es =
35 abbrevDeclaration.extract(llvm_data, offset_ptr);
36 if (!es)
37 return es.takeError();
38 if (*es == llvm::DWARFAbbreviationDeclaration::ExtractState::Complete)
39 break;
40 if (m_idx_offset == 0)
41 m_idx_offset = abbrevDeclaration.getCode();
42 else if (prev_abbr_code + 1 != abbrevDeclaration.getCode())
44
45 prev_abbr_code = abbrevDeclaration.getCode();
46 m_decls.push_back(abbrevDeclaration);
47 }
48 return llvm::ErrorSuccess();
49}
50
51// DWARFAbbreviationDeclarationSet::GetAbbreviationDeclaration()
54 uint32_t abbrCode) const {
55 if (m_idx_offset == UINT32_MAX) {
56 for (const auto &decl : m_decls) {
57 if (decl.getCode() == abbrCode)
58 return &decl;
59 }
60 return nullptr;
61 }
62 if (abbrCode < m_idx_offset || abbrCode >= m_idx_offset + m_decls.size())
63 return nullptr;
64 return &m_decls[abbrCode - m_idx_offset];
65}
66
67// DWARFAbbreviationDeclarationSet::GetUnsupportedForms()
69 std::set<dw_form_t> &invalid_forms) const {
70 for (const auto &decl : m_decls) {
71 for (const auto &attr : decl.attributes()) {
72 if (!DWARFFormValue::FormIsSupported(attr.Form))
73 invalid_forms.insert(attr.Form);
74 }
75 }
76}
77
78// Encode
79//
80// Encode the abbreviation table onto the end of the buffer provided into a
81// byte representation as would be found in a ".debug_abbrev" debug information
82// section.
83// void
84// DWARFAbbreviationDeclarationSet::Encode(BinaryStreamBuf& debug_abbrev_buf)
85// const
86//{
87// DWARFAbbreviationDeclarationCollConstIter pos;
88// DWARFAbbreviationDeclarationCollConstIter end = m_decls.end();
89// for (pos = m_decls.begin(); pos != end; ++pos)
90// pos->Append(debug_abbrev_buf);
91// debug_abbrev_buf.Append8(0);
92//}
93
94// DWARFDebugAbbrev constructor
96 : m_abbrevCollMap(), m_prev_abbr_offset_pos(m_abbrevCollMap.end()) {}
97
98// DWARFDebugAbbrev::Parse()
100 lldb::offset_t offset = 0;
101
102 while (data.ValidOffset(offset)) {
103 uint32_t initial_cu_offset = offset;
105
106 llvm::Error error = abbrevDeclSet.extract(data, &offset);
107 if (error)
108 return error;
109
110 m_abbrevCollMap[initial_cu_offset] = abbrevDeclSet;
111 }
113 return llvm::ErrorSuccess();
114}
115
116// DWARFDebugAbbrev::GetAbbreviationDeclarationSet()
119 dw_offset_t cu_abbr_offset) const {
122 if (m_prev_abbr_offset_pos != end &&
123 m_prev_abbr_offset_pos->first == cu_abbr_offset)
124 return &(m_prev_abbr_offset_pos->second);
125 else {
126 pos = m_abbrevCollMap.find(cu_abbr_offset);
128 }
129
130 if (pos != m_abbrevCollMap.end())
131 return &(pos->second);
132 return nullptr;
133}
134
135// DWARFDebugAbbrev::GetUnsupportedForms()
137 std::set<dw_form_t> &invalid_forms) const {
138 for (const auto &pair : m_abbrevCollMap)
139 pair.second.GetUnsupportedForms(invalid_forms);
140}
static llvm::raw_ostream & error(Stream &strm)
DWARFAbbreviationDeclarationCollMap::const_iterator DWARFAbbreviationDeclarationCollMapConstIter
llvm::DWARFAbbreviationDeclaration DWARFAbbreviationDeclaration
std::vector< DWARFAbbreviationDeclaration > m_decls
llvm::Error extract(const lldb_private::DWARFDataExtractor &data, lldb::offset_t *offset_ptr)
Extract all abbrev decls in a set.
void GetUnsupportedForms(std::set< dw_form_t > &invalid_forms) const
const DWARFAbbreviationDeclaration * GetAbbreviationDeclaration(uint32_t abbrCode) const
void GetUnsupportedForms(std::set< dw_form_t > &invalid_forms) const
const DWARFAbbreviationDeclarationSet * GetAbbreviationDeclarationSet(dw_offset_t cu_abbr_offset) const
DWARFAbbreviationDeclarationCollMapConstIter m_prev_abbr_offset_pos
DWARFAbbreviationDeclarationCollMap m_abbrevCollMap
llvm::Error parse(const lldb_private::DWARFDataExtractor &data)
Extract all abbreviations for a particular compile unit.
static bool FormIsSupported(dw_form_t form)
llvm::DWARFDataExtractor GetAsLLVM() const
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
uint64_t dw_offset_t
Definition: dwarf.h:31
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:83