David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 1 | /* |
| 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 Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 20 | #include <cstdint> |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 21 | #include <unordered_map> |
| 22 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 23 | #include "base/casts.h" |
David Srbecky | 7c869b3 | 2015-04-12 08:47:47 +0100 | [diff] [blame] | 24 | #include "dwarf/dwarf_constants.h" |
| 25 | #include "dwarf/writer.h" |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 26 | #include "leb128.h" |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace 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 Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 33 | template <typename Vector> |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 34 | struct FNVHash { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 35 | 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 Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 38 | 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 Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 51 | * StartTag(DW_TAG_compile_unit); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 52 | * WriteStrp(DW_AT_producer, "Compiler name", debug_str); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 53 | * StartTag(DW_TAG_subprogram); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 54 | * WriteStrp(DW_AT_name, "Foo", debug_str); |
| 55 | * EndTag(); |
| 56 | * EndTag(); |
| 57 | */ |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 58 | template <typename Vector = std::vector<uint8_t>> |
| 59 | class DebugInfoEntryWriter FINAL : private Writer<Vector> { |
| 60 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 61 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 62 | public: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 63 | static constexpr size_t kCompilationUnitHeaderSize = 11; |
| 64 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 65 | // Start debugging information entry. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 66 | // Returns offset of the entry in compilation unit. |
| 67 | size_t StartTag(Tag tag) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 68 | if (inside_entry_) { |
| 69 | // Write abbrev code for the previous entry. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 70 | // Parent entry is finalized before any children are written. |
| 71 | this->UpdateUleb128(abbrev_code_offset_, EndAbbrev(DW_CHILDREN_yes)); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 72 | inside_entry_ = false; |
| 73 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 74 | StartAbbrev(tag); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 75 | // Abbrev code placeholder of sufficient size. |
| 76 | abbrev_code_offset_ = this->data()->size(); |
| 77 | this->PushUleb128(NextAbbrevCode()); |
| 78 | depth_++; |
| 79 | inside_entry_ = true; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 80 | return abbrev_code_offset_ + kCompilationUnitHeaderSize; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // End debugging information entry. |
| 84 | void EndTag() { |
| 85 | DCHECK_GT(depth_, 0); |
| 86 | if (inside_entry_) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 87 | // Write abbrev code for this entry. |
| 88 | this->UpdateUleb128(abbrev_code_offset_, EndAbbrev(DW_CHILDREN_no)); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 89 | inside_entry_ = false; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 90 | // 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 Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 95 | } |
| 96 | depth_--; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void WriteAddr(Attribute attrib, uint64_t value) { |
| 100 | AddAbbrevAttribute(attrib, DW_FORM_addr); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 101 | patch_locations_.push_back(this->data()->size()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 102 | if (is64bit_) { |
| 103 | this->PushUint64(value); |
| 104 | } else { |
| 105 | this->PushUint32(value); |
| 106 | } |
| 107 | } |
| 108 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 109 | void WriteBlock(Attribute attrib, const void* ptr, size_t num_bytes) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 110 | AddAbbrevAttribute(attrib, DW_FORM_block); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 111 | this->PushUleb128(num_bytes); |
| 112 | this->PushData(ptr, num_bytes); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 113 | } |
| 114 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame^] | 115 | 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 Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 121 | 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 Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame^] | 141 | void WriteSecOffset(Attribute attrib, uint32_t offset) { |
| 142 | AddAbbrevAttribute(attrib, DW_FORM_sec_offset); |
| 143 | this->PushUint32(offset); |
| 144 | } |
| 145 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 146 | 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 Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 166 | void WriteRef4(Attribute attrib, uint32_t cu_offset) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 167 | AddAbbrevAttribute(attrib, DW_FORM_ref4); |
| 168 | this->PushUint32(cu_offset); |
| 169 | } |
| 170 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 171 | void WriteRef(Attribute attrib, uint32_t cu_offset) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 172 | 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 Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 181 | void WriteStrp(Attribute attrib, size_t debug_str_offset) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 182 | AddAbbrevAttribute(attrib, DW_FORM_strp); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 183 | this->PushUint32(dchecked_integral_cast<uint32_t>(debug_str_offset)); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 184 | } |
| 185 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 186 | void WriteStrp(Attribute attrib, const char* str, size_t len, |
| 187 | std::vector<uint8_t>* debug_str) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 188 | AddAbbrevAttribute(attrib, DW_FORM_strp); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 189 | 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 Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 196 | } |
| 197 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 198 | bool Is64bit() const { return is64bit_; } |
| 199 | |
| 200 | const std::vector<uintptr_t>& GetPatchLocations() const { |
| 201 | return patch_locations_; |
| 202 | } |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 203 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 204 | int Depth() const { return depth_; } |
| 205 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 206 | using Writer<Vector>::data; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 207 | using Writer<Vector>::size; |
| 208 | using Writer<Vector>::UpdateUint32; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 209 | |
| 210 | DebugInfoEntryWriter(bool is64bitArch, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 211 | Vector* debug_abbrev, |
| 212 | const typename Vector::allocator_type& alloc = |
| 213 | typename Vector::allocator_type()) |
| 214 | : Writer<Vector>(&entries_), |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 215 | 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 Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 224 | DCHECK(!inside_entry_); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 225 | DCHECK_EQ(depth_, 0); |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | // Start abbreviation declaration. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 230 | void StartAbbrev(Tag tag) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 231 | current_abbrev_.clear(); |
| 232 | EncodeUnsignedLeb128(¤t_abbrev_, tag); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 233 | has_children_offset_ = current_abbrev_.size(); |
| 234 | current_abbrev_.push_back(0); // Place-holder for DW_CHILDREN. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Add attribute specification. |
| 238 | void AddAbbrevAttribute(Attribute name, Form type) { |
| 239 | DCHECK(inside_entry_) << "Call StartTag before adding attributes."; |
| 240 | EncodeUnsignedLeb128(¤t_abbrev_, name); |
| 241 | EncodeUnsignedLeb128(¤t_abbrev_, type); |
| 242 | } |
| 243 | |
| 244 | int NextAbbrevCode() { |
| 245 | return 1 + abbrev_codes_.size(); |
| 246 | } |
| 247 | |
| 248 | // End abbreviation declaration and return its code. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 249 | int EndAbbrev(Children has_children) { |
| 250 | DCHECK(!current_abbrev_.empty()); |
| 251 | current_abbrev_[has_children_offset_] = has_children; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 252 | 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 Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 256 | const Vector& abbrev = it.first->first; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 257 | 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 Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 269 | Writer<Vector> debug_abbrev_; |
| 270 | Vector current_abbrev_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 271 | size_t has_children_offset_ = 0; |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 272 | std::unordered_map<Vector, int, |
| 273 | FNVHash<Vector> > abbrev_codes_; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 274 | |
| 275 | // Fields for writing of debugging information entries. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 276 | Vector entries_; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 277 | 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 Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 281 | std::vector<uintptr_t> patch_locations_; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | } // namespace dwarf |
| 285 | } // namespace art |
| 286 | |
| 287 | #endif // ART_COMPILER_DWARF_DEBUG_INFO_ENTRY_WRITER_H_ |