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 | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 24 | #include "dwarf/debug_abbrev_writer.h" |
David Srbecky | 7c869b3 | 2015-04-12 08:47:47 +0100 | [diff] [blame] | 25 | #include "dwarf/dwarf_constants.h" |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 26 | #include "dwarf/expression.h" |
David Srbecky | 7c869b3 | 2015-04-12 08:47:47 +0100 | [diff] [blame] | 27 | #include "dwarf/writer.h" |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 28 | #include "leb128.h" |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace dwarf { |
| 32 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Writer for debug information entries (DIE). |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 35 | * |
| 36 | * Usage: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 37 | * StartTag(DW_TAG_compile_unit); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 38 | * WriteStrp(DW_AT_producer, "Compiler name", debug_str); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 39 | * StartTag(DW_TAG_subprogram); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 40 | * WriteStrp(DW_AT_name, "Foo", debug_str); |
| 41 | * EndTag(); |
| 42 | * EndTag(); |
| 43 | */ |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 44 | template <typename Vector = std::vector<uint8_t>> |
| 45 | class DebugInfoEntryWriter FINAL : private Writer<Vector> { |
| 46 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 47 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 48 | public: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 49 | static constexpr size_t kCompilationUnitHeaderSize = 11; |
| 50 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 51 | // Start debugging information entry. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 52 | // Returns offset of the entry in compilation unit. |
| 53 | size_t StartTag(Tag tag) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 54 | if (inside_entry_) { |
| 55 | // Write abbrev code for the previous entry. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 56 | // Parent entry is finalized before any children are written. |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 57 | this->UpdateUleb128(abbrev_code_offset_, debug_abbrev_->EndAbbrev(DW_CHILDREN_yes)); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 58 | inside_entry_ = false; |
| 59 | } |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 60 | debug_abbrev_->StartAbbrev(tag); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 61 | // Abbrev code placeholder of sufficient size. |
| 62 | abbrev_code_offset_ = this->data()->size(); |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 63 | this->PushUleb128(debug_abbrev_->NextAbbrevCode()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 64 | depth_++; |
| 65 | inside_entry_ = true; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 66 | return abbrev_code_offset_ + kCompilationUnitHeaderSize; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // End debugging information entry. |
| 70 | void EndTag() { |
| 71 | DCHECK_GT(depth_, 0); |
| 72 | if (inside_entry_) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 73 | // Write abbrev code for this entry. |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 74 | this->UpdateUleb128(abbrev_code_offset_, debug_abbrev_->EndAbbrev(DW_CHILDREN_no)); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 75 | inside_entry_ = false; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 76 | // This entry has no children and so there is no terminator. |
| 77 | } else { |
| 78 | // The entry has been already finalized so it must be parent entry |
| 79 | // and we need to write the terminator required by DW_CHILDREN_yes. |
| 80 | this->PushUint8(0); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 81 | } |
| 82 | depth_--; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void WriteAddr(Attribute attrib, uint64_t value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 86 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_addr); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 87 | patch_locations_.push_back(this->data()->size()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 88 | if (is64bit_) { |
| 89 | this->PushUint64(value); |
| 90 | } else { |
| 91 | this->PushUint32(value); |
| 92 | } |
| 93 | } |
| 94 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 95 | void WriteBlock(Attribute attrib, const uint8_t* ptr, size_t num_bytes) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 96 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_block); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 97 | this->PushUleb128(num_bytes); |
| 98 | this->PushData(ptr, num_bytes); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 99 | } |
| 100 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 101 | void WriteExprLoc(Attribute attrib, const Expression& expr) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 102 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_exprloc); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 103 | this->PushUleb128(dchecked_integral_cast<uint32_t>(expr.size())); |
| 104 | this->PushData(expr.data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 105 | } |
| 106 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 107 | void WriteData1(Attribute attrib, uint8_t value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 108 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_data1); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 109 | this->PushUint8(value); |
| 110 | } |
| 111 | |
| 112 | void WriteData2(Attribute attrib, uint16_t value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 113 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_data2); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 114 | this->PushUint16(value); |
| 115 | } |
| 116 | |
| 117 | void WriteData4(Attribute attrib, uint32_t value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 118 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_data4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 119 | this->PushUint32(value); |
| 120 | } |
| 121 | |
| 122 | void WriteData8(Attribute attrib, uint64_t value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 123 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_data8); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 124 | this->PushUint64(value); |
| 125 | } |
| 126 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 127 | void WriteSecOffset(Attribute attrib, uint32_t offset) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 128 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_sec_offset); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 129 | this->PushUint32(offset); |
| 130 | } |
| 131 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 132 | void WriteSdata(Attribute attrib, int value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 133 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_sdata); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 134 | this->PushSleb128(value); |
| 135 | } |
| 136 | |
| 137 | void WriteUdata(Attribute attrib, int value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 138 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_udata); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 139 | this->PushUleb128(value); |
| 140 | } |
| 141 | |
| 142 | void WriteUdata(Attribute attrib, uint32_t value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 143 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_udata); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 144 | this->PushUleb128(value); |
| 145 | } |
| 146 | |
| 147 | void WriteFlag(Attribute attrib, bool value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 148 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_flag); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 149 | this->PushUint8(value ? 1 : 0); |
| 150 | } |
| 151 | |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 152 | void WriteFlagPresent(Attribute attrib) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 153 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_flag_present); |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 154 | } |
| 155 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 156 | void WriteRef4(Attribute attrib, uint32_t cu_offset) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 157 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_ref4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 158 | this->PushUint32(cu_offset); |
| 159 | } |
| 160 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 161 | void WriteRef(Attribute attrib, uint32_t cu_offset) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 162 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_ref_udata); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 163 | this->PushUleb128(cu_offset); |
| 164 | } |
| 165 | |
| 166 | void WriteString(Attribute attrib, const char* value) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 167 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_string); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 168 | this->PushString(value); |
| 169 | } |
| 170 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 171 | void WriteStrp(Attribute attrib, size_t debug_str_offset) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 172 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_strp); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 173 | this->PushUint32(dchecked_integral_cast<uint32_t>(debug_str_offset)); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 174 | } |
| 175 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 176 | void WriteStrp(Attribute attrib, const char* str, size_t len, |
| 177 | std::vector<uint8_t>* debug_str) { |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 178 | debug_abbrev_->AddAbbrevAttribute(attrib, DW_FORM_strp); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 179 | this->PushUint32(debug_str->size()); |
| 180 | debug_str->insert(debug_str->end(), str, str + len); |
| 181 | debug_str->push_back(0); |
| 182 | } |
| 183 | |
| 184 | void WriteStrp(Attribute attrib, const char* str, std::vector<uint8_t>* debug_str) { |
| 185 | WriteStrp(attrib, str, strlen(str), debug_str); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 186 | } |
| 187 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 188 | bool Is64bit() const { return is64bit_; } |
| 189 | |
| 190 | const std::vector<uintptr_t>& GetPatchLocations() const { |
| 191 | return patch_locations_; |
| 192 | } |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 193 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 194 | int Depth() const { return depth_; } |
| 195 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 196 | using Writer<Vector>::data; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 197 | using Writer<Vector>::size; |
| 198 | using Writer<Vector>::UpdateUint32; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 199 | |
| 200 | DebugInfoEntryWriter(bool is64bitArch, |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 201 | DebugAbbrevWriter<Vector>* debug_abbrev, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 202 | const typename Vector::allocator_type& alloc = |
| 203 | typename Vector::allocator_type()) |
| 204 | : Writer<Vector>(&entries_), |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 205 | debug_abbrev_(debug_abbrev), |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 206 | entries_(alloc), |
| 207 | is64bit_(is64bitArch) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | ~DebugInfoEntryWriter() { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 211 | DCHECK(!inside_entry_); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 212 | DCHECK_EQ(depth_, 0); |
| 213 | } |
| 214 | |
| 215 | private: |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame] | 216 | DebugAbbrevWriter<Vector>* debug_abbrev_; |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 217 | Vector entries_; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 218 | bool is64bit_; |
| 219 | int depth_ = 0; |
| 220 | size_t abbrev_code_offset_ = 0; // Location to patch once we know the code. |
| 221 | bool inside_entry_ = false; // Entry ends at first child (if any). |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 222 | std::vector<uintptr_t> patch_locations_; |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | } // namespace dwarf |
| 226 | } // namespace art |
| 227 | |
| 228 | #endif // ART_COMPILER_DWARF_DEBUG_INFO_ENTRY_WRITER_H_ |