blob: a551e4b4951e5020390d7892674d121a9634eeca [file] [log] [blame]
David Srbeckyb5362472015-04-08 19:37:39 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
18#define ART_COMPILER_DWARF_DEBUG_INFO_ENTRY_WRITER_H_
19
David Srbecky2f6cdb02015-04-11 00:17:53 +010020#include <cstdint>
David Srbeckyb5362472015-04-08 19:37:39 +010021#include <unordered_map>
22
David Srbecky04b05262015-11-09 18:05:48 +000023#include "base/casts.h"
David Srbecky7c869b32015-04-12 08:47:47 +010024#include "dwarf/dwarf_constants.h"
25#include "dwarf/writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010026#include "leb128.h"
David Srbeckyb5362472015-04-08 19:37:39 +010027
28namespace art {
29namespace dwarf {
30
31// 32-bit FNV-1a hash function which we use to find duplicate abbreviations.
32// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
Vladimir Markoec7802a2015-10-01 20:57:57 +010033template <typename Vector>
David Srbeckyb5362472015-04-08 19:37:39 +010034struct FNVHash {
Vladimir Markoec7802a2015-10-01 20:57:57 +010035 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
36
37 size_t operator()(const Vector& v) const {
David Srbeckyb5362472015-04-08 19:37:39 +010038 uint32_t hash = 2166136261u;
39 for (size_t i = 0; i < v.size(); i++) {
40 hash = (hash ^ v[i]) * 16777619u;
41 }
42 return hash;
43 }
44};
45
46/*
47 * Writer for debug information entries (DIE).
48 * It also handles generation of abbreviations.
49 *
50 * Usage:
David Srbecky04b05262015-11-09 18:05:48 +000051 * StartTag(DW_TAG_compile_unit);
David Srbeckyb5362472015-04-08 19:37:39 +010052 * WriteStrp(DW_AT_producer, "Compiler name", debug_str);
David Srbecky04b05262015-11-09 18:05:48 +000053 * StartTag(DW_TAG_subprogram);
David Srbeckyb5362472015-04-08 19:37:39 +010054 * WriteStrp(DW_AT_name, "Foo", debug_str);
55 * EndTag();
56 * EndTag();
57 */
Vladimir Markoec7802a2015-10-01 20:57:57 +010058template <typename Vector = std::vector<uint8_t>>
59class DebugInfoEntryWriter FINAL : private Writer<Vector> {
60 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
61
David Srbeckyb5362472015-04-08 19:37:39 +010062 public:
David Srbecky04b05262015-11-09 18:05:48 +000063 static constexpr size_t kCompilationUnitHeaderSize = 11;
64
David Srbeckyb5362472015-04-08 19:37:39 +010065 // Start debugging information entry.
David Srbecky04b05262015-11-09 18:05:48 +000066 // Returns offset of the entry in compilation unit.
67 size_t StartTag(Tag tag) {
David Srbeckyb5362472015-04-08 19:37:39 +010068 if (inside_entry_) {
69 // Write abbrev code for the previous entry.
David Srbecky04b05262015-11-09 18:05:48 +000070 // Parent entry is finalized before any children are written.
71 this->UpdateUleb128(abbrev_code_offset_, EndAbbrev(DW_CHILDREN_yes));
David Srbeckyb5362472015-04-08 19:37:39 +010072 inside_entry_ = false;
73 }
David Srbecky04b05262015-11-09 18:05:48 +000074 StartAbbrev(tag);
David Srbeckyb5362472015-04-08 19:37:39 +010075 // Abbrev code placeholder of sufficient size.
76 abbrev_code_offset_ = this->data()->size();
77 this->PushUleb128(NextAbbrevCode());
78 depth_++;
79 inside_entry_ = true;
David Srbecky04b05262015-11-09 18:05:48 +000080 return abbrev_code_offset_ + kCompilationUnitHeaderSize;
David Srbeckyb5362472015-04-08 19:37:39 +010081 }
82
83 // End debugging information entry.
84 void EndTag() {
85 DCHECK_GT(depth_, 0);
86 if (inside_entry_) {
David Srbecky04b05262015-11-09 18:05:48 +000087 // Write abbrev code for this entry.
88 this->UpdateUleb128(abbrev_code_offset_, EndAbbrev(DW_CHILDREN_no));
David Srbeckyb5362472015-04-08 19:37:39 +010089 inside_entry_ = false;
David Srbecky04b05262015-11-09 18:05:48 +000090 // This entry has no children and so there is no terminator.
91 } else {
92 // The entry has been already finalized so it must be parent entry
93 // and we need to write the terminator required by DW_CHILDREN_yes.
94 this->PushUint8(0);
David Srbeckyb5362472015-04-08 19:37:39 +010095 }
96 depth_--;
David Srbeckyb5362472015-04-08 19:37:39 +010097 }
98
99 void WriteAddr(Attribute attrib, uint64_t value) {
100 AddAbbrevAttribute(attrib, DW_FORM_addr);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100101 patch_locations_.push_back(this->data()->size());
David Srbeckyb5362472015-04-08 19:37:39 +0100102 if (is64bit_) {
103 this->PushUint64(value);
104 } else {
105 this->PushUint32(value);
106 }
107 }
108
David Srbecky04b05262015-11-09 18:05:48 +0000109 void WriteBlock(Attribute attrib, const void* ptr, size_t num_bytes) {
David Srbeckyb5362472015-04-08 19:37:39 +0100110 AddAbbrevAttribute(attrib, DW_FORM_block);
David Srbecky04b05262015-11-09 18:05:48 +0000111 this->PushUleb128(num_bytes);
112 this->PushData(ptr, num_bytes);
David Srbeckyb5362472015-04-08 19:37:39 +0100113 }
114
David Srbecky0fd295f2015-11-16 16:39:10 +0000115 void WriteExprLoc(Attribute attrib, const void* ptr, size_t num_bytes) {
116 AddAbbrevAttribute(attrib, DW_FORM_exprloc);
117 this->PushUleb128(dchecked_integral_cast<uint32_t>(num_bytes));
118 this->PushData(ptr, num_bytes);
119 }
120
David Srbeckyb5362472015-04-08 19:37:39 +0100121 void WriteData1(Attribute attrib, uint8_t value) {
122 AddAbbrevAttribute(attrib, DW_FORM_data1);
123 this->PushUint8(value);
124 }
125
126 void WriteData2(Attribute attrib, uint16_t value) {
127 AddAbbrevAttribute(attrib, DW_FORM_data2);
128 this->PushUint16(value);
129 }
130
131 void WriteData4(Attribute attrib, uint32_t value) {
132 AddAbbrevAttribute(attrib, DW_FORM_data4);
133 this->PushUint32(value);
134 }
135
136 void WriteData8(Attribute attrib, uint64_t value) {
137 AddAbbrevAttribute(attrib, DW_FORM_data8);
138 this->PushUint64(value);
139 }
140
David Srbecky0fd295f2015-11-16 16:39:10 +0000141 void WriteSecOffset(Attribute attrib, uint32_t offset) {
142 AddAbbrevAttribute(attrib, DW_FORM_sec_offset);
143 this->PushUint32(offset);
144 }
145
David Srbeckyb5362472015-04-08 19:37:39 +0100146 void WriteSdata(Attribute attrib, int value) {
147 AddAbbrevAttribute(attrib, DW_FORM_sdata);
148 this->PushSleb128(value);
149 }
150
151 void WriteUdata(Attribute attrib, int value) {
152 AddAbbrevAttribute(attrib, DW_FORM_udata);
153 this->PushUleb128(value);
154 }
155
156 void WriteUdata(Attribute attrib, uint32_t value) {
157 AddAbbrevAttribute(attrib, DW_FORM_udata);
158 this->PushUleb128(value);
159 }
160
161 void WriteFlag(Attribute attrib, bool value) {
162 AddAbbrevAttribute(attrib, DW_FORM_flag);
163 this->PushUint8(value ? 1 : 0);
164 }
165
David Srbecky04b05262015-11-09 18:05:48 +0000166 void WriteRef4(Attribute attrib, uint32_t cu_offset) {
David Srbeckyb5362472015-04-08 19:37:39 +0100167 AddAbbrevAttribute(attrib, DW_FORM_ref4);
168 this->PushUint32(cu_offset);
169 }
170
David Srbecky04b05262015-11-09 18:05:48 +0000171 void WriteRef(Attribute attrib, uint32_t cu_offset) {
David Srbeckyb5362472015-04-08 19:37:39 +0100172 AddAbbrevAttribute(attrib, DW_FORM_ref_udata);
173 this->PushUleb128(cu_offset);
174 }
175
176 void WriteString(Attribute attrib, const char* value) {
177 AddAbbrevAttribute(attrib, DW_FORM_string);
178 this->PushString(value);
179 }
180
David Srbecky04b05262015-11-09 18:05:48 +0000181 void WriteStrp(Attribute attrib, size_t debug_str_offset) {
David Srbeckyb5362472015-04-08 19:37:39 +0100182 AddAbbrevAttribute(attrib, DW_FORM_strp);
David Srbecky04b05262015-11-09 18:05:48 +0000183 this->PushUint32(dchecked_integral_cast<uint32_t>(debug_str_offset));
David Srbeckyb5362472015-04-08 19:37:39 +0100184 }
185
David Srbecky04b05262015-11-09 18:05:48 +0000186 void WriteStrp(Attribute attrib, const char* str, size_t len,
187 std::vector<uint8_t>* debug_str) {
David Srbeckyb5362472015-04-08 19:37:39 +0100188 AddAbbrevAttribute(attrib, DW_FORM_strp);
David Srbecky04b05262015-11-09 18:05:48 +0000189 this->PushUint32(debug_str->size());
190 debug_str->insert(debug_str->end(), str, str + len);
191 debug_str->push_back(0);
192 }
193
194 void WriteStrp(Attribute attrib, const char* str, std::vector<uint8_t>* debug_str) {
195 WriteStrp(attrib, str, strlen(str), debug_str);
David Srbeckyb5362472015-04-08 19:37:39 +0100196 }
197
David Srbecky2f6cdb02015-04-11 00:17:53 +0100198 bool Is64bit() const { return is64bit_; }
199
200 const std::vector<uintptr_t>& GetPatchLocations() const {
201 return patch_locations_;
202 }
David Srbeckyb5362472015-04-08 19:37:39 +0100203
David Srbecky04b05262015-11-09 18:05:48 +0000204 int Depth() const { return depth_; }
205
Vladimir Markoec7802a2015-10-01 20:57:57 +0100206 using Writer<Vector>::data;
David Srbecky04b05262015-11-09 18:05:48 +0000207 using Writer<Vector>::size;
208 using Writer<Vector>::UpdateUint32;
David Srbeckyb5362472015-04-08 19:37:39 +0100209
210 DebugInfoEntryWriter(bool is64bitArch,
Vladimir Markoec7802a2015-10-01 20:57:57 +0100211 Vector* debug_abbrev,
212 const typename Vector::allocator_type& alloc =
213 typename Vector::allocator_type())
214 : Writer<Vector>(&entries_),
David Srbeckyb5362472015-04-08 19:37:39 +0100215 debug_abbrev_(debug_abbrev),
216 current_abbrev_(alloc),
217 abbrev_codes_(alloc),
218 entries_(alloc),
219 is64bit_(is64bitArch) {
220 debug_abbrev_.PushUint8(0); // Add abbrev table terminator.
221 }
222
223 ~DebugInfoEntryWriter() {
David Srbecky04b05262015-11-09 18:05:48 +0000224 DCHECK(!inside_entry_);
David Srbeckyb5362472015-04-08 19:37:39 +0100225 DCHECK_EQ(depth_, 0);
226 }
227
228 private:
229 // Start abbreviation declaration.
David Srbecky04b05262015-11-09 18:05:48 +0000230 void StartAbbrev(Tag tag) {
David Srbeckyb5362472015-04-08 19:37:39 +0100231 current_abbrev_.clear();
232 EncodeUnsignedLeb128(&current_abbrev_, tag);
David Srbecky04b05262015-11-09 18:05:48 +0000233 has_children_offset_ = current_abbrev_.size();
234 current_abbrev_.push_back(0); // Place-holder for DW_CHILDREN.
David Srbeckyb5362472015-04-08 19:37:39 +0100235 }
236
237 // Add attribute specification.
238 void AddAbbrevAttribute(Attribute name, Form type) {
239 DCHECK(inside_entry_) << "Call StartTag before adding attributes.";
240 EncodeUnsignedLeb128(&current_abbrev_, name);
241 EncodeUnsignedLeb128(&current_abbrev_, type);
242 }
243
244 int NextAbbrevCode() {
245 return 1 + abbrev_codes_.size();
246 }
247
248 // End abbreviation declaration and return its code.
David Srbecky04b05262015-11-09 18:05:48 +0000249 int EndAbbrev(Children has_children) {
250 DCHECK(!current_abbrev_.empty());
251 current_abbrev_[has_children_offset_] = has_children;
David Srbeckyb5362472015-04-08 19:37:39 +0100252 auto it = abbrev_codes_.insert(std::make_pair(std::move(current_abbrev_),
253 NextAbbrevCode()));
254 int abbrev_code = it.first->second;
255 if (UNLIKELY(it.second)) { // Inserted new entry.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100256 const Vector& abbrev = it.first->first;
David Srbeckyb5362472015-04-08 19:37:39 +0100257 debug_abbrev_.Pop(); // Remove abbrev table terminator.
258 debug_abbrev_.PushUleb128(abbrev_code);
259 debug_abbrev_.PushData(abbrev.data(), abbrev.size());
260 debug_abbrev_.PushUint8(0); // Attribute list end.
261 debug_abbrev_.PushUint8(0); // Attribute list end.
262 debug_abbrev_.PushUint8(0); // Add abbrev table terminator.
263 }
264 return abbrev_code;
265 }
266
267 private:
268 // Fields for writing and deduplication of abbrevs.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100269 Writer<Vector> debug_abbrev_;
270 Vector current_abbrev_;
David Srbecky04b05262015-11-09 18:05:48 +0000271 size_t has_children_offset_ = 0;
Vladimir Markoec7802a2015-10-01 20:57:57 +0100272 std::unordered_map<Vector, int,
273 FNVHash<Vector> > abbrev_codes_;
David Srbeckyb5362472015-04-08 19:37:39 +0100274
275 // Fields for writing of debugging information entries.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100276 Vector entries_;
David Srbeckyb5362472015-04-08 19:37:39 +0100277 bool is64bit_;
278 int depth_ = 0;
279 size_t abbrev_code_offset_ = 0; // Location to patch once we know the code.
280 bool inside_entry_ = false; // Entry ends at first child (if any).
David Srbecky2f6cdb02015-04-11 00:17:53 +0100281 std::vector<uintptr_t> patch_locations_;
David Srbeckyb5362472015-04-08 19:37:39 +0100282};
283
284} // namespace dwarf
285} // namespace art
286
287#endif // ART_COMPILER_DWARF_DEBUG_INFO_ENTRY_WRITER_H_