David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "elf_writer_debug.h" |
| 18 | |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 19 | #include <algorithm> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 20 | #include <unordered_set> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 21 | #include <vector> |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 22 | #include <cstdio> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 23 | |
Andreas Gampe | e3d623e | 2015-05-01 16:11:04 -0700 | [diff] [blame] | 24 | #include "base/casts.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 25 | #include "base/stl_util.h" |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 26 | #include "linear_alloc.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 27 | #include "compiled_method.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 28 | #include "dex_file-inl.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 29 | #include "driver/compiler_driver.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 30 | #include "dwarf/dedup_vector.h" |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 31 | #include "dwarf/expression.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 32 | #include "dwarf/headers.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 33 | #include "dwarf/method_debug_info.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 34 | #include "dwarf/register.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 35 | #include "elf_builder.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 36 | #include "linker/vector_output_stream.h" |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 37 | #include "mirror/array.h" |
| 38 | #include "mirror/class-inl.h" |
| 39 | #include "mirror/class.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 40 | #include "oat_writer.h" |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 41 | #include "stack_map.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 42 | #include "utils.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 43 | |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 44 | // liblzma. |
| 45 | #include "XzEnc.h" |
| 46 | #include "7zCrc.h" |
| 47 | #include "XzCrc64.h" |
| 48 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 49 | namespace art { |
| 50 | namespace dwarf { |
| 51 | |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 52 | // The ARM specification defines three special mapping symbols |
| 53 | // $a, $t and $d which mark ARM, Thumb and data ranges respectively. |
| 54 | // These symbols can be used by tools, for example, to pretty |
| 55 | // print instructions correctly. Objdump will use them if they |
| 56 | // exist, but it will still work well without them. |
| 57 | // However, these extra symbols take space, so let's just generate |
| 58 | // one symbol which marks the whole .text section as code. |
| 59 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 60 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 61 | static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) { |
| 62 | switch (isa) { |
| 63 | case kArm: |
| 64 | case kThumb2: |
| 65 | return Reg::ArmCore(machine_reg); |
| 66 | case kArm64: |
| 67 | return Reg::Arm64Core(machine_reg); |
| 68 | case kX86: |
| 69 | return Reg::X86Core(machine_reg); |
| 70 | case kX86_64: |
| 71 | return Reg::X86_64Core(machine_reg); |
| 72 | case kMips: |
| 73 | return Reg::MipsCore(machine_reg); |
| 74 | case kMips64: |
| 75 | return Reg::Mips64Core(machine_reg); |
| 76 | default: |
| 77 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 78 | UNREACHABLE(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) { |
| 83 | switch (isa) { |
| 84 | case kArm: |
| 85 | case kThumb2: |
| 86 | return Reg::ArmFp(machine_reg); |
| 87 | case kArm64: |
| 88 | return Reg::Arm64Fp(machine_reg); |
| 89 | case kX86: |
| 90 | return Reg::X86Fp(machine_reg); |
| 91 | case kX86_64: |
| 92 | return Reg::X86_64Fp(machine_reg); |
| 93 | default: |
| 94 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 95 | UNREACHABLE(); |
| 96 | } |
| 97 | } |
| 98 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 99 | static void WriteCIE(InstructionSet isa, |
| 100 | CFIFormat format, |
| 101 | std::vector<uint8_t>* buffer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 102 | // Scratch registers should be marked as undefined. This tells the |
| 103 | // debugger that its value in the previous frame is not recoverable. |
| 104 | bool is64bit = Is64BitInstructionSet(isa); |
| 105 | switch (isa) { |
| 106 | case kArm: |
| 107 | case kThumb2: { |
| 108 | DebugFrameOpCodeWriter<> opcodes; |
| 109 | opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP). |
| 110 | // core registers. |
| 111 | for (int reg = 0; reg < 13; reg++) { |
| 112 | if (reg < 4 || reg == 12) { |
| 113 | opcodes.Undefined(Reg::ArmCore(reg)); |
| 114 | } else { |
| 115 | opcodes.SameValue(Reg::ArmCore(reg)); |
| 116 | } |
| 117 | } |
| 118 | // fp registers. |
| 119 | for (int reg = 0; reg < 32; reg++) { |
| 120 | if (reg < 16) { |
| 121 | opcodes.Undefined(Reg::ArmFp(reg)); |
| 122 | } else { |
| 123 | opcodes.SameValue(Reg::ArmFp(reg)); |
| 124 | } |
| 125 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 126 | auto return_reg = Reg::ArmCore(14); // R14(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 127 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | case kArm64: { |
| 131 | DebugFrameOpCodeWriter<> opcodes; |
| 132 | opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP). |
| 133 | // core registers. |
| 134 | for (int reg = 0; reg < 30; reg++) { |
| 135 | if (reg < 8 || reg == 16 || reg == 17) { |
| 136 | opcodes.Undefined(Reg::Arm64Core(reg)); |
| 137 | } else { |
| 138 | opcodes.SameValue(Reg::Arm64Core(reg)); |
| 139 | } |
| 140 | } |
| 141 | // fp registers. |
| 142 | for (int reg = 0; reg < 32; reg++) { |
| 143 | if (reg < 8 || reg >= 16) { |
| 144 | opcodes.Undefined(Reg::Arm64Fp(reg)); |
| 145 | } else { |
| 146 | opcodes.SameValue(Reg::Arm64Fp(reg)); |
| 147 | } |
| 148 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 149 | auto return_reg = Reg::Arm64Core(30); // R30(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 150 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | case kMips: |
| 154 | case kMips64: { |
| 155 | DebugFrameOpCodeWriter<> opcodes; |
| 156 | opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP). |
| 157 | // core registers. |
| 158 | for (int reg = 1; reg < 26; reg++) { |
| 159 | if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*. |
| 160 | opcodes.Undefined(Reg::MipsCore(reg)); |
| 161 | } else { |
| 162 | opcodes.SameValue(Reg::MipsCore(reg)); |
| 163 | } |
| 164 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 165 | auto return_reg = Reg::MipsCore(31); // R31(RA). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 166 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | case kX86: { |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 170 | // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296 |
| 171 | constexpr bool generate_opcodes_for_x86_fp = false; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 172 | DebugFrameOpCodeWriter<> opcodes; |
| 173 | opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP). |
| 174 | opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP). |
| 175 | // core registers. |
| 176 | for (int reg = 0; reg < 8; reg++) { |
| 177 | if (reg <= 3) { |
| 178 | opcodes.Undefined(Reg::X86Core(reg)); |
| 179 | } else if (reg == 4) { |
| 180 | // Stack pointer. |
| 181 | } else { |
| 182 | opcodes.SameValue(Reg::X86Core(reg)); |
| 183 | } |
| 184 | } |
| 185 | // fp registers. |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 186 | if (generate_opcodes_for_x86_fp) { |
| 187 | for (int reg = 0; reg < 8; reg++) { |
| 188 | opcodes.Undefined(Reg::X86Fp(reg)); |
| 189 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 190 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 191 | auto return_reg = Reg::X86Core(8); // R8(EIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 192 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | case kX86_64: { |
| 196 | DebugFrameOpCodeWriter<> opcodes; |
| 197 | opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP). |
| 198 | opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP). |
| 199 | // core registers. |
| 200 | for (int reg = 0; reg < 16; reg++) { |
| 201 | if (reg == 4) { |
| 202 | // Stack pointer. |
| 203 | } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP. |
| 204 | opcodes.Undefined(Reg::X86_64Core(reg)); |
| 205 | } else { |
| 206 | opcodes.SameValue(Reg::X86_64Core(reg)); |
| 207 | } |
| 208 | } |
| 209 | // fp registers. |
| 210 | for (int reg = 0; reg < 16; reg++) { |
| 211 | if (reg < 12) { |
| 212 | opcodes.Undefined(Reg::X86_64Fp(reg)); |
| 213 | } else { |
| 214 | opcodes.SameValue(Reg::X86_64Fp(reg)); |
| 215 | } |
| 216 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 217 | auto return_reg = Reg::X86_64Core(16); // R16(RIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 218 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | case kNone: |
| 222 | break; |
| 223 | } |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 224 | LOG(FATAL) << "Cannot write CIE frame for ISA " << isa; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 225 | UNREACHABLE(); |
| 226 | } |
| 227 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 228 | template<typename ElfTypes> |
| 229 | void WriteCFISection(ElfBuilder<ElfTypes>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 230 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 231 | CFIFormat format, |
| 232 | bool write_oat_patches) { |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 233 | CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 234 | typedef typename ElfTypes::Addr Elf_Addr; |
| 235 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 236 | if (method_infos.empty()) { |
| 237 | return; |
| 238 | } |
| 239 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 240 | std::vector<uint32_t> binary_search_table; |
| 241 | std::vector<uintptr_t> patch_locations; |
| 242 | if (format == DW_EH_FRAME_FORMAT) { |
| 243 | binary_search_table.reserve(2 * method_infos.size()); |
| 244 | } else { |
| 245 | patch_locations.reserve(method_infos.size()); |
| 246 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 247 | |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 248 | // The methods can be written any order. |
| 249 | // Let's therefore sort them in the lexicographical order of the opcodes. |
| 250 | // This has no effect on its own. However, if the final .debug_frame section is |
| 251 | // compressed it reduces the size since similar opcodes sequences are grouped. |
| 252 | std::vector<const MethodDebugInfo*> sorted_method_infos; |
| 253 | sorted_method_infos.reserve(method_infos.size()); |
| 254 | for (size_t i = 0; i < method_infos.size(); i++) { |
| 255 | sorted_method_infos.push_back(&method_infos[i]); |
| 256 | } |
| 257 | std::sort( |
| 258 | sorted_method_infos.begin(), |
| 259 | sorted_method_infos.end(), |
| 260 | [](const MethodDebugInfo* lhs, const MethodDebugInfo* rhs) { |
| 261 | ArrayRef<const uint8_t> l = lhs->compiled_method_->GetCFIInfo(); |
| 262 | ArrayRef<const uint8_t> r = rhs->compiled_method_->GetCFIInfo(); |
| 263 | return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); |
| 264 | }); |
| 265 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 266 | // Write .eh_frame/.debug_frame section. |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 267 | auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 268 | ? builder->GetDebugFrame() |
| 269 | : builder->GetEhFrame()); |
| 270 | { |
| 271 | cfi_section->Start(); |
| 272 | const bool is64bit = Is64BitInstructionSet(builder->GetIsa()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 273 | const Elf_Addr text_address = builder->GetText()->Exists() |
| 274 | ? builder->GetText()->GetAddress() |
| 275 | : 0; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 276 | const Elf_Addr cfi_address = cfi_section->GetAddress(); |
| 277 | const Elf_Addr cie_address = cfi_address; |
| 278 | Elf_Addr buffer_address = cfi_address; |
| 279 | std::vector<uint8_t> buffer; // Small temporary buffer. |
| 280 | WriteCIE(builder->GetIsa(), format, &buffer); |
| 281 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 282 | buffer_address += buffer.size(); |
| 283 | buffer.clear(); |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 284 | for (const MethodDebugInfo* mi : sorted_method_infos) { |
| 285 | if (!mi->deduped_) { // Only one FDE per unique address. |
| 286 | ArrayRef<const uint8_t> opcodes = mi->compiled_method_->GetCFIInfo(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 287 | if (!opcodes.empty()) { |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 288 | const Elf_Addr code_address = text_address + mi->low_pc_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 289 | if (format == DW_EH_FRAME_FORMAT) { |
| 290 | binary_search_table.push_back( |
| 291 | dchecked_integral_cast<uint32_t>(code_address)); |
| 292 | binary_search_table.push_back( |
| 293 | dchecked_integral_cast<uint32_t>(buffer_address)); |
| 294 | } |
| 295 | WriteFDE(is64bit, cfi_address, cie_address, |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 296 | code_address, mi->high_pc_ - mi->low_pc_, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 297 | opcodes, format, buffer_address, &buffer, |
| 298 | &patch_locations); |
| 299 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 300 | buffer_address += buffer.size(); |
| 301 | buffer.clear(); |
| 302 | } |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 303 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 304 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 305 | cfi_section->End(); |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 306 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 307 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 308 | if (format == DW_EH_FRAME_FORMAT) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 309 | auto* header_section = builder->GetEhFrameHdr(); |
| 310 | header_section->Start(); |
| 311 | uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 312 | // Write .eh_frame_hdr section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 313 | std::vector<uint8_t> buffer; |
| 314 | Writer<> header(&buffer); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 315 | header.PushUint8(1); // Version. |
| 316 | // Encoding of .eh_frame pointer - libunwind does not honor datarel here, |
| 317 | // so we have to use pcrel which means relative to the pointer's location. |
| 318 | header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); |
| 319 | // Encoding of binary search table size. |
| 320 | header.PushUint8(DW_EH_PE_udata4); |
| 321 | // Encoding of binary search table addresses - libunwind supports only this |
| 322 | // specific combination, which means relative to the start of .eh_frame_hdr. |
| 323 | header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 324 | // .eh_frame pointer |
| 325 | header.PushInt32(cfi_section->GetAddress() - (header_address + 4u)); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 326 | // Binary search table size (number of entries). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 327 | header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2)); |
| 328 | header_section->WriteFully(buffer.data(), buffer.size()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 329 | // Binary search table. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 330 | for (size_t i = 0; i < binary_search_table.size(); i++) { |
| 331 | // Make addresses section-relative since we know the header address now. |
| 332 | binary_search_table[i] -= header_address; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 333 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 334 | header_section->WriteFully(binary_search_table.data(), binary_search_table.size()); |
| 335 | header_section->End(); |
| 336 | } else { |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 337 | if (write_oat_patches) { |
| 338 | builder->WritePatches(".debug_frame.oat_patches", |
| 339 | ArrayRef<const uintptr_t>(patch_locations)); |
| 340 | } |
David Srbecky | 033d745 | 2015-04-30 19:57:35 +0100 | [diff] [blame] | 341 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 342 | } |
| 343 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 344 | namespace { |
| 345 | struct CompilationUnit { |
| 346 | std::vector<const MethodDebugInfo*> methods_; |
| 347 | size_t debug_line_offset_ = 0; |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 348 | uintptr_t low_pc_ = std::numeric_limits<uintptr_t>::max(); |
| 349 | uintptr_t high_pc_ = 0; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 352 | typedef std::vector<DexFile::LocalInfo> LocalInfos; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 353 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 354 | void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) { |
| 355 | static_cast<LocalInfos*>(ctx)->push_back(entry); |
| 356 | } |
| 357 | |
| 358 | typedef std::vector<DexFile::PositionInfo> PositionInfos; |
| 359 | |
| 360 | bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { |
| 361 | static_cast<PositionInfos*>(ctx)->push_back(entry); |
| 362 | return false; |
| 363 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 364 | |
| 365 | std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) { |
| 366 | std::vector<const char*> names; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 367 | if (mi->code_item_ != nullptr) { |
| 368 | const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_); |
| 369 | if (stream != nullptr) { |
| 370 | DecodeUnsignedLeb128(&stream); // line. |
| 371 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 372 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 373 | uint32_t id = DecodeUnsignedLeb128P1(&stream); |
| 374 | names.push_back(mi->dex_file_->StringDataByIdx(id)); |
| 375 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | return names; |
| 379 | } |
| 380 | |
| 381 | struct VariableLocation { |
| 382 | uint32_t low_pc; |
| 383 | uint32_t high_pc; |
| 384 | DexRegisterLocation reg_lo; // May be None if the location is unknown. |
| 385 | DexRegisterLocation reg_hi; // Most significant bits of 64-bit value. |
| 386 | }; |
| 387 | |
| 388 | // Get the location of given dex register (e.g. stack or machine register). |
| 389 | // Note that the location might be different based on the current pc. |
| 390 | // The result will cover all ranges where the variable is in scope. |
| 391 | std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info, |
| 392 | uint16_t vreg, |
| 393 | bool is64bitValue, |
| 394 | uint32_t dex_pc_low, |
| 395 | uint32_t dex_pc_high) { |
| 396 | std::vector<VariableLocation> variable_locations; |
| 397 | |
| 398 | // Get stack maps sorted by pc (they might not be sorted internally). |
| 399 | const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data()); |
| 400 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 401 | std::map<uint32_t, StackMap> stack_maps; |
| 402 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 403 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 404 | DCHECK(stack_map.IsValid()); |
| 405 | const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding); |
| 406 | DCHECK_LE(low_pc, method_info->high_pc_); |
| 407 | stack_maps.emplace(low_pc, stack_map); |
| 408 | } |
| 409 | |
| 410 | // Create entries for the requested register based on stack map data. |
| 411 | for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) { |
| 412 | const StackMap& stack_map = it->second; |
| 413 | const uint32_t low_pc = it->first; |
| 414 | auto next_it = it; |
| 415 | next_it++; |
| 416 | const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first |
| 417 | : method_info->high_pc_; |
| 418 | DCHECK_LE(low_pc, high_pc); |
| 419 | if (low_pc == high_pc) { |
| 420 | continue; // Ignore if the address range is empty. |
| 421 | } |
| 422 | |
| 423 | // Check that the stack map is in the requested range. |
| 424 | uint32_t dex_pc = stack_map.GetDexPc(encoding); |
| 425 | if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) { |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | // Find the location of the dex register. |
| 430 | DexRegisterLocation reg_lo = DexRegisterLocation::None(); |
| 431 | DexRegisterLocation reg_hi = DexRegisterLocation::None(); |
| 432 | if (stack_map.HasDexRegisterMap(encoding)) { |
| 433 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf( |
| 434 | stack_map, encoding, method_info->code_item_->registers_size_); |
| 435 | reg_lo = dex_register_map.GetDexRegisterLocation( |
| 436 | vreg, method_info->code_item_->registers_size_, code_info, encoding); |
| 437 | if (is64bitValue) { |
| 438 | reg_hi = dex_register_map.GetDexRegisterLocation( |
| 439 | vreg + 1, method_info->code_item_->registers_size_, code_info, encoding); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Add location entry for this address range. |
| 444 | if (!variable_locations.empty() && |
| 445 | variable_locations.back().reg_lo == reg_lo && |
| 446 | variable_locations.back().reg_hi == reg_hi && |
| 447 | variable_locations.back().high_pc == low_pc) { |
| 448 | // Merge with the previous entry (extend its range). |
| 449 | variable_locations.back().high_pc = high_pc; |
| 450 | } else { |
| 451 | variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi}); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return variable_locations; |
| 456 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 457 | |
| 458 | bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) { |
| 459 | return method_info->compiled_method_->GetQuickCode().size() > 0 && |
| 460 | method_info->compiled_method_->GetVmapTable().size() > 0 && |
| 461 | method_info->compiled_method_->GetGcMap().size() == 0 && |
| 462 | method_info->code_item_ != nullptr; |
| 463 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 464 | } // namespace |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 465 | |
| 466 | // Helper class to write .debug_info and its supporting sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 467 | template<typename ElfTypes> |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 468 | class DebugInfoWriter { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 469 | typedef typename ElfTypes::Addr Elf_Addr; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 470 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 471 | // Helper class to write one compilation unit. |
| 472 | // It holds helper methods and temporary state. |
| 473 | class CompilationUnitWriter { |
| 474 | public: |
| 475 | explicit CompilationUnitWriter(DebugInfoWriter* owner) |
| 476 | : owner_(owner), |
| 477 | info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) { |
| 478 | } |
| 479 | |
| 480 | void Write(const CompilationUnit& compilation_unit) { |
| 481 | CHECK(!compilation_unit.methods_.empty()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 482 | const Elf_Addr text_address = owner_->builder_->GetText()->Exists() |
| 483 | ? owner_->builder_->GetText()->GetAddress() |
| 484 | : 0; |
| 485 | const uintptr_t cu_size = compilation_unit.high_pc_ - compilation_unit.low_pc_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 486 | |
| 487 | info_.StartTag(DW_TAG_compile_unit); |
| 488 | info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat")); |
| 489 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
Tamas Berghammer | 8c55712 | 2015-12-10 15:06:25 +0000 | [diff] [blame] | 490 | info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT")); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 491 | info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 492 | info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size)); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 493 | info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 494 | |
| 495 | const char* last_dex_class_desc = nullptr; |
| 496 | for (auto mi : compilation_unit.methods_) { |
| 497 | const DexFile* dex = mi->dex_file_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 498 | const DexFile::CodeItem* dex_code = mi->code_item_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 499 | const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_); |
| 500 | const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method); |
| 501 | const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto); |
| 502 | const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 503 | const bool is_static = (mi->access_flags_ & kAccStatic) != 0; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 504 | |
| 505 | // Enclose the method in correct class definition. |
| 506 | if (last_dex_class_desc != dex_class_desc) { |
| 507 | if (last_dex_class_desc != nullptr) { |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 508 | EndClassTag(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 509 | } |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 510 | // Write reference tag for the class we are about to declare. |
| 511 | size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type); |
| 512 | type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset); |
| 513 | size_t type_attrib_offset = info_.size(); |
| 514 | info_.WriteRef4(DW_AT_type, 0); |
| 515 | info_.EndTag(); |
| 516 | // Declare the class that owns this method. |
| 517 | size_t class_offset = StartClassTag(dex_class_desc); |
| 518 | info_.UpdateUint32(type_attrib_offset, class_offset); |
| 519 | info_.WriteFlag(DW_AT_declaration, true); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 520 | // Check that each class is defined only once. |
| 521 | bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second; |
| 522 | CHECK(unique) << "Redefinition of " << dex_class_desc; |
| 523 | last_dex_class_desc = dex_class_desc; |
| 524 | } |
| 525 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 526 | int start_depth = info_.Depth(); |
| 527 | info_.StartTag(DW_TAG_subprogram); |
| 528 | WriteName(dex->GetMethodName(dex_method)); |
| 529 | info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 530 | info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_)); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 531 | std::vector<uint8_t> expr_buffer; |
| 532 | Expression expr(&expr_buffer); |
| 533 | expr.WriteOpCallFrameCfa(); |
| 534 | info_.WriteExprLoc(DW_AT_frame_base, expr); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 535 | WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto)); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 536 | |
| 537 | // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not |
| 538 | // guarantee order or uniqueness so it is safer to iterate over them manually. |
| 539 | // DecodeDebugLocalInfo might not also be available if there is no debug info. |
| 540 | std::vector<const char*> param_names = GetParamNames(mi); |
| 541 | uint32_t arg_reg = 0; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 542 | if (!is_static) { |
| 543 | info_.StartTag(DW_TAG_formal_parameter); |
| 544 | WriteName("this"); |
| 545 | info_.WriteFlag(DW_AT_artificial, true); |
| 546 | WriteLazyType(dex_class_desc); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 547 | if (dex_code != nullptr) { |
| 548 | // Write the stack location of the parameter. |
| 549 | const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg; |
| 550 | const bool is64bitValue = false; |
| 551 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 552 | } |
| 553 | arg_reg++; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 554 | info_.EndTag(); |
| 555 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 556 | if (dex_params != nullptr) { |
| 557 | for (uint32_t i = 0; i < dex_params->Size(); ++i) { |
| 558 | info_.StartTag(DW_TAG_formal_parameter); |
| 559 | // Parameter names may not be always available. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 560 | if (i < param_names.size()) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 561 | WriteName(param_names[i]); |
| 562 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 563 | // Write the type. |
| 564 | const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_); |
| 565 | WriteLazyType(type_desc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 566 | const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J'; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 567 | if (dex_code != nullptr) { |
| 568 | // Write the stack location of the parameter. |
| 569 | const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg; |
| 570 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 571 | } |
| 572 | arg_reg += is64bitValue ? 2 : 1; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 573 | info_.EndTag(); |
| 574 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 575 | if (dex_code != nullptr) { |
| 576 | DCHECK_EQ(arg_reg, dex_code->ins_size_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 577 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 578 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 579 | |
| 580 | // Write local variables. |
| 581 | LocalInfos local_infos; |
| 582 | if (dex->DecodeDebugLocalInfo(dex_code, |
| 583 | is_static, |
| 584 | mi->dex_method_index_, |
| 585 | LocalInfoCallback, |
| 586 | &local_infos)) { |
| 587 | for (const DexFile::LocalInfo& var : local_infos) { |
| 588 | if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) { |
| 589 | info_.StartTag(DW_TAG_variable); |
| 590 | WriteName(var.name_); |
| 591 | WriteLazyType(var.descriptor_); |
| 592 | bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J'; |
| 593 | WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_, |
| 594 | var.start_address_, var.end_address_); |
| 595 | info_.EndTag(); |
| 596 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 599 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 600 | info_.EndTag(); |
| 601 | CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end. |
| 602 | } |
| 603 | if (last_dex_class_desc != nullptr) { |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 604 | EndClassTag(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 605 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 606 | FinishLazyTypes(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 607 | CloseNamespacesAboveDepth(0); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 608 | info_.EndTag(); // DW_TAG_compile_unit |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 609 | CHECK_EQ(info_.Depth(), 0); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 610 | std::vector<uint8_t> buffer; |
| 611 | buffer.reserve(info_.data()->size() + KB); |
| 612 | const size_t offset = owner_->builder_->GetDebugInfo()->GetSize(); |
| 613 | const size_t debug_abbrev_offset = |
| 614 | owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size()); |
| 615 | WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_); |
| 616 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 617 | } |
| 618 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 619 | void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 620 | info_.StartTag(DW_TAG_compile_unit); |
| 621 | info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat")); |
| 622 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
| 623 | |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 624 | std::vector<uint8_t> expr_buffer; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 625 | for (mirror::Class* type : types) { |
| 626 | if (type->IsPrimitive()) { |
| 627 | // For primitive types the definition and the declaration is the same. |
| 628 | if (type->GetPrimitiveType() != Primitive::kPrimVoid) { |
| 629 | WriteTypeDeclaration(type->GetDescriptor(nullptr)); |
| 630 | } |
| 631 | } else if (type->IsArrayClass()) { |
| 632 | mirror::Class* element_type = type->GetComponentType(); |
| 633 | uint32_t component_size = type->GetComponentSize(); |
| 634 | uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
| 635 | uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 636 | |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 637 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 638 | info_.StartTag(DW_TAG_array_type); |
| 639 | std::string descriptor_string; |
| 640 | WriteLazyType(element_type->GetDescriptor(&descriptor_string)); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 641 | WriteLinkageName(type); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 642 | info_.WriteUdata(DW_AT_data_member_location, data_offset); |
| 643 | info_.StartTag(DW_TAG_subrange_type); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 644 | Expression count_expr(&expr_buffer); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 645 | count_expr.WriteOpPushObjectAddress(); |
| 646 | count_expr.WriteOpPlusUconst(length_offset); |
| 647 | count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide. |
| 648 | info_.WriteExprLoc(DW_AT_count, count_expr); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 649 | info_.EndTag(); // DW_TAG_subrange_type. |
| 650 | info_.EndTag(); // DW_TAG_array_type. |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 651 | } else if (type->IsInterface()) { |
| 652 | // Skip. Variables cannot have an interface as a dynamic type. |
| 653 | // We do not expose the interface information to the debugger in any way. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 654 | } else { |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 655 | // Declare base class. We can not use the standard WriteLazyType |
| 656 | // since we want to avoid the DW_TAG_reference_tag wrapping. |
| 657 | mirror::Class* base_class = type->GetSuperClass(); |
| 658 | size_t base_class_declaration_offset = 0; |
| 659 | if (base_class != nullptr) { |
| 660 | std::string tmp_storage; |
| 661 | const char* base_class_desc = base_class->GetDescriptor(&tmp_storage); |
| 662 | base_class_declaration_offset = StartClassTag(base_class_desc); |
| 663 | info_.WriteFlag(DW_AT_declaration, true); |
| 664 | WriteLinkageName(base_class); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 665 | EndClassTag(); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 668 | std::string descriptor_string; |
| 669 | const char* desc = type->GetDescriptor(&descriptor_string); |
| 670 | StartClassTag(desc); |
| 671 | |
| 672 | if (!type->IsVariableSize()) { |
| 673 | info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize()); |
| 674 | } |
| 675 | |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 676 | WriteLinkageName(type); |
| 677 | |
| 678 | if (type->IsObjectClass()) { |
| 679 | // Generate artificial member which is used to get the dynamic type of variable. |
| 680 | // The run-time value of this field will correspond to linkage name of some type. |
| 681 | // We need to do it only once in j.l.Object since all other types inherit it. |
| 682 | info_.StartTag(DW_TAG_member); |
| 683 | WriteName(".dynamic_type"); |
| 684 | WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I"); |
| 685 | info_.WriteFlag(DW_AT_artificial, true); |
| 686 | // Create DWARF expression to get the value of the methods_ field. |
| 687 | Expression expr(&expr_buffer); |
| 688 | // The address of the object has been implicitly pushed on the stack. |
| 689 | // Dereference the klass_ field of Object (32-bit; possibly poisoned). |
| 690 | DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u); |
| 691 | DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u); |
| 692 | expr.WriteOpDerefSize(4); |
| 693 | if (kPoisonHeapReferences) { |
| 694 | expr.WriteOpNeg(); |
| 695 | // DWARF stack is pointer sized. Ensure that the high bits are clear. |
| 696 | expr.WriteOpConstu(0xFFFFFFFF); |
| 697 | expr.WriteOpAnd(); |
| 698 | } |
| 699 | // Add offset to the methods_ field. |
| 700 | expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value()); |
| 701 | // Top of stack holds the location of the field now. |
| 702 | info_.WriteExprLoc(DW_AT_data_member_location, expr); |
| 703 | info_.EndTag(); // DW_TAG_member. |
| 704 | } |
| 705 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 706 | // Base class. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 707 | if (base_class != nullptr) { |
| 708 | info_.StartTag(DW_TAG_inheritance); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 709 | info_.WriteRef4(DW_AT_type, base_class_declaration_offset); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 710 | info_.WriteUdata(DW_AT_data_member_location, 0); |
| 711 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public); |
| 712 | info_.EndTag(); // DW_TAG_inheritance. |
| 713 | } |
| 714 | |
| 715 | // Member variables. |
| 716 | for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) { |
| 717 | ArtField* field = type->GetInstanceField(i); |
| 718 | info_.StartTag(DW_TAG_member); |
| 719 | WriteName(field->GetName()); |
| 720 | WriteLazyType(field->GetTypeDescriptor()); |
| 721 | info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value()); |
| 722 | uint32_t access_flags = field->GetAccessFlags(); |
| 723 | if (access_flags & kAccPublic) { |
| 724 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public); |
| 725 | } else if (access_flags & kAccProtected) { |
| 726 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected); |
| 727 | } else if (access_flags & kAccPrivate) { |
| 728 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private); |
| 729 | } |
| 730 | info_.EndTag(); // DW_TAG_member. |
| 731 | } |
| 732 | |
Tamas Berghammer | 03c941f | 2016-01-15 13:39:57 +0000 | [diff] [blame] | 733 | if (type->IsStringClass()) { |
| 734 | // Emit debug info about an artifical class member for java.lang.String which represents |
| 735 | // the first element of the data stored in a string instance. Consumers of the debug |
| 736 | // info will be able to read the content of java.lang.String based on the count (real |
| 737 | // field) and based on the location of this data member. |
| 738 | info_.StartTag(DW_TAG_member); |
| 739 | WriteName("value"); |
| 740 | // We don't support fields with C like array types so we just say its type is java char. |
| 741 | WriteLazyType("C"); // char. |
| 742 | info_.WriteUdata(DW_AT_data_member_location, |
| 743 | mirror::String::ValueOffset().Uint32Value()); |
| 744 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private); |
| 745 | info_.EndTag(); // DW_TAG_member. |
| 746 | } |
| 747 | |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 748 | EndClassTag(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 752 | FinishLazyTypes(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 753 | CloseNamespacesAboveDepth(0); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 754 | info_.EndTag(); // DW_TAG_compile_unit. |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 755 | CHECK_EQ(info_.Depth(), 0); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 756 | std::vector<uint8_t> buffer; |
| 757 | buffer.reserve(info_.data()->size() + KB); |
| 758 | const size_t offset = owner_->builder_->GetDebugInfo()->GetSize(); |
| 759 | const size_t debug_abbrev_offset = |
| 760 | owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size()); |
| 761 | WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_); |
| 762 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 763 | } |
| 764 | |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 765 | // Linkage name uniquely identifies type. |
| 766 | // It is used to determine the dynamic type of objects. |
| 767 | // We use the methods_ field of class since it is unique and it is not moved by the GC. |
| 768 | void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 769 | auto* methods_ptr = type->GetMethodsPtr(); |
| 770 | if (methods_ptr == nullptr) { |
| 771 | // Some types might have no methods. Allocate empty array instead. |
| 772 | LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc(); |
| 773 | void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>)); |
| 774 | methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0); |
| 775 | type->SetMethodsPtr(methods_ptr, 0, 0); |
| 776 | DCHECK(type->GetMethodsPtr() != nullptr); |
| 777 | } |
| 778 | char name[32]; |
| 779 | snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr)); |
| 780 | info_.WriteString(DW_AT_linkage_name, name); |
| 781 | } |
| 782 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 783 | // Write table into .debug_loc which describes location of dex register. |
| 784 | // The dex register might be valid only at some points and it might |
| 785 | // move between machine registers and stack. |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 786 | void WriteRegLocation(const MethodDebugInfo* method_info, |
| 787 | uint16_t vreg, |
| 788 | bool is64bitValue, |
| 789 | uint32_t compilation_unit_low_pc, |
| 790 | uint32_t dex_pc_low = 0, |
| 791 | uint32_t dex_pc_high = 0xFFFFFFFF) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 792 | using Kind = DexRegisterLocation::Kind; |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 793 | if (!IsFromOptimizingCompiler(method_info)) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 797 | Writer<> debug_loc(&owner_->debug_loc_); |
| 798 | Writer<> debug_ranges(&owner_->debug_ranges_); |
| 799 | info_.WriteSecOffset(DW_AT_location, debug_loc.size()); |
| 800 | info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 801 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 802 | std::vector<VariableLocation> variable_locations = GetVariableLocations( |
| 803 | method_info, |
| 804 | vreg, |
| 805 | is64bitValue, |
| 806 | dex_pc_low, |
| 807 | dex_pc_high); |
| 808 | |
| 809 | // Write .debug_loc entries. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 810 | const InstructionSet isa = owner_->builder_->GetIsa(); |
| 811 | const bool is64bit = Is64BitInstructionSet(isa); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 812 | std::vector<uint8_t> expr_buffer; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 813 | for (const VariableLocation& variable_location : variable_locations) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 814 | // Translate dex register location to DWARF expression. |
| 815 | // Note that 64-bit value might be split to two distinct locations. |
| 816 | // (for example, two 32-bit machine registers, or even stack and register) |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 817 | Expression expr(&expr_buffer); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 818 | DexRegisterLocation reg_lo = variable_location.reg_lo; |
| 819 | DexRegisterLocation reg_hi = variable_location.reg_hi; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 820 | for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) { |
| 821 | DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi); |
| 822 | const Kind kind = reg_loc.GetKind(); |
| 823 | const int32_t value = reg_loc.GetValue(); |
| 824 | if (kind == Kind::kInStack) { |
| 825 | const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes(); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 826 | // The stack offset is relative to SP. Make it relative to CFA. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 827 | expr.WriteOpFbreg(value - frame_size); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 828 | if (piece == 0 && reg_hi.GetKind() == Kind::kInStack && |
| 829 | reg_hi.GetValue() == value + 4) { |
| 830 | break; // the high word is correctly implied by the low word. |
| 831 | } |
| 832 | } else if (kind == Kind::kInRegister) { |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 833 | expr.WriteOpReg(GetDwarfCoreReg(isa, value).num()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 834 | if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh && |
| 835 | reg_hi.GetValue() == value) { |
| 836 | break; // the high word is correctly implied by the low word. |
| 837 | } |
| 838 | } else if (kind == Kind::kInFpuRegister) { |
| 839 | if ((isa == kArm || isa == kThumb2) && |
| 840 | piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister && |
| 841 | reg_hi.GetValue() == value + 1 && value % 2 == 0) { |
| 842 | // Translate S register pair to D register (e.g. S4+S5 to D2). |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 843 | expr.WriteOpReg(Reg::ArmDp(value / 2).num()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 844 | break; |
| 845 | } |
David Srbecky | 3dd7e5a | 2015-11-27 13:31:16 +0000 | [diff] [blame] | 846 | if (isa == kMips || isa == kMips64) { |
| 847 | // TODO: Find what the DWARF floating point register numbers are on MIPS. |
| 848 | break; |
| 849 | } |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 850 | expr.WriteOpReg(GetDwarfFpReg(isa, value).num()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 851 | if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh && |
| 852 | reg_hi.GetValue() == reg_lo.GetValue()) { |
| 853 | break; // the high word is correctly implied by the low word. |
| 854 | } |
| 855 | } else if (kind == Kind::kConstant) { |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 856 | expr.WriteOpConsts(value); |
| 857 | expr.WriteOpStackValue(); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 858 | } else if (kind == Kind::kNone) { |
| 859 | break; |
| 860 | } else { |
| 861 | // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind(). |
| 862 | // kInRegisterHigh and kInFpuRegisterHigh should be handled by |
| 863 | // the special cases above and they should not occur alone. |
| 864 | LOG(ERROR) << "Unexpected register location kind: " |
| 865 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 866 | break; |
| 867 | } |
| 868 | if (is64bitValue) { |
| 869 | // Write the marker which is needed by split 64-bit values. |
| 870 | // This code is skipped by the special cases. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 871 | expr.WriteOpPiece(4); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 875 | if (expr.size() > 0) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 876 | if (is64bit) { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 877 | debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc); |
| 878 | debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 879 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 880 | debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc); |
| 881 | debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 882 | } |
| 883 | // Write the expression. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 884 | debug_loc.PushUint16(expr.size()); |
| 885 | debug_loc.PushData(expr.data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 886 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 887 | // Do not generate .debug_loc if the location is not known. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | // Write end-of-list entry. |
| 891 | if (is64bit) { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 892 | debug_loc.PushUint64(0); |
| 893 | debug_loc.PushUint64(0); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 894 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 895 | debug_loc.PushUint32(0); |
| 896 | debug_loc.PushUint32(0); |
| 897 | } |
| 898 | |
| 899 | // Write .debug_ranges entries. |
| 900 | // This includes ranges where the variable is in scope but the location is not known. |
| 901 | for (size_t i = 0; i < variable_locations.size(); i++) { |
| 902 | uint32_t low_pc = variable_locations[i].low_pc; |
| 903 | uint32_t high_pc = variable_locations[i].high_pc; |
| 904 | while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) { |
| 905 | // Merge address range with the next entry. |
| 906 | high_pc = variable_locations[++i].high_pc; |
| 907 | } |
| 908 | if (is64bit) { |
| 909 | debug_ranges.PushUint64(low_pc - compilation_unit_low_pc); |
| 910 | debug_ranges.PushUint64(high_pc - compilation_unit_low_pc); |
| 911 | } else { |
| 912 | debug_ranges.PushUint32(low_pc - compilation_unit_low_pc); |
| 913 | debug_ranges.PushUint32(high_pc - compilation_unit_low_pc); |
| 914 | } |
| 915 | } |
| 916 | // Write end-of-list entry. |
| 917 | if (is64bit) { |
| 918 | debug_ranges.PushUint64(0); |
| 919 | debug_ranges.PushUint64(0); |
| 920 | } else { |
| 921 | debug_ranges.PushUint32(0); |
| 922 | debug_ranges.PushUint32(0); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 926 | // Some types are difficult to define as we go since they need |
| 927 | // to be enclosed in the right set of namespaces. Therefore we |
| 928 | // just define all types lazily at the end of compilation unit. |
| 929 | void WriteLazyType(const char* type_descriptor) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 930 | if (type_descriptor != nullptr && type_descriptor[0] != 'V') { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 931 | lazy_types_.emplace(std::string(type_descriptor), info_.size()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 932 | info_.WriteRef4(DW_AT_type, 0); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | void FinishLazyTypes() { |
| 937 | for (const auto& lazy_type : lazy_types_) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 938 | info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first)); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 939 | } |
| 940 | lazy_types_.clear(); |
| 941 | } |
| 942 | |
| 943 | private: |
| 944 | void WriteName(const char* name) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 945 | if (name != nullptr) { |
| 946 | info_.WriteStrp(DW_AT_name, owner_->WriteString(name)); |
| 947 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // Convert dex type descriptor to DWARF. |
| 951 | // Returns offset in the compilation unit. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 952 | size_t WriteTypeDeclaration(const std::string& desc) { |
| 953 | DCHECK(!desc.empty()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 954 | const auto& it = type_cache_.find(desc); |
| 955 | if (it != type_cache_.end()) { |
| 956 | return it->second; |
| 957 | } |
| 958 | |
| 959 | size_t offset; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 960 | if (desc[0] == 'L') { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 961 | // Class type. For example: Lpackage/name; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 962 | size_t class_offset = StartClassTag(desc.c_str()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 963 | info_.WriteFlag(DW_AT_declaration, true); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 964 | EndClassTag(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 965 | // Reference to the class type. |
| 966 | offset = info_.StartTag(DW_TAG_reference_type); |
| 967 | info_.WriteRef(DW_AT_type, class_offset); |
| 968 | info_.EndTag(); |
| 969 | } else if (desc[0] == '[') { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 970 | // Array type. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 971 | size_t element_type = WriteTypeDeclaration(desc.substr(1)); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 972 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 973 | size_t array_type = info_.StartTag(DW_TAG_array_type); |
| 974 | info_.WriteFlag(DW_AT_declaration, true); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 975 | info_.WriteRef(DW_AT_type, element_type); |
| 976 | info_.EndTag(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 977 | offset = info_.StartTag(DW_TAG_reference_type); |
| 978 | info_.WriteRef4(DW_AT_type, array_type); |
| 979 | info_.EndTag(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 980 | } else { |
| 981 | // Primitive types. |
Tamas Berghammer | 03c941f | 2016-01-15 13:39:57 +0000 | [diff] [blame] | 982 | DCHECK_EQ(desc.size(), 1u); |
| 983 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 984 | const char* name; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 985 | uint32_t encoding; |
| 986 | uint32_t byte_size; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 987 | switch (desc[0]) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 988 | case 'B': |
| 989 | name = "byte"; |
| 990 | encoding = DW_ATE_signed; |
| 991 | byte_size = 1; |
| 992 | break; |
| 993 | case 'C': |
| 994 | name = "char"; |
| 995 | encoding = DW_ATE_UTF; |
| 996 | byte_size = 2; |
| 997 | break; |
| 998 | case 'D': |
| 999 | name = "double"; |
| 1000 | encoding = DW_ATE_float; |
| 1001 | byte_size = 8; |
| 1002 | break; |
| 1003 | case 'F': |
| 1004 | name = "float"; |
| 1005 | encoding = DW_ATE_float; |
| 1006 | byte_size = 4; |
| 1007 | break; |
| 1008 | case 'I': |
| 1009 | name = "int"; |
| 1010 | encoding = DW_ATE_signed; |
| 1011 | byte_size = 4; |
| 1012 | break; |
| 1013 | case 'J': |
| 1014 | name = "long"; |
| 1015 | encoding = DW_ATE_signed; |
| 1016 | byte_size = 8; |
| 1017 | break; |
| 1018 | case 'S': |
| 1019 | name = "short"; |
| 1020 | encoding = DW_ATE_signed; |
| 1021 | byte_size = 2; |
| 1022 | break; |
| 1023 | case 'Z': |
| 1024 | name = "boolean"; |
| 1025 | encoding = DW_ATE_boolean; |
| 1026 | byte_size = 1; |
| 1027 | break; |
| 1028 | case 'V': |
| 1029 | LOG(FATAL) << "Void type should not be encoded"; |
| 1030 | UNREACHABLE(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1031 | default: |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1032 | LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\""; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1033 | UNREACHABLE(); |
| 1034 | } |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1035 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1036 | offset = info_.StartTag(DW_TAG_base_type); |
| 1037 | WriteName(name); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1038 | info_.WriteData1(DW_AT_encoding, encoding); |
| 1039 | info_.WriteData1(DW_AT_byte_size, byte_size); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1040 | info_.EndTag(); |
| 1041 | } |
| 1042 | |
| 1043 | type_cache_.emplace(desc, offset); |
| 1044 | return offset; |
| 1045 | } |
| 1046 | |
| 1047 | // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags. |
| 1048 | // Returns offset of the class tag in the compilation unit. |
| 1049 | size_t StartClassTag(const char* desc) { |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1050 | std::string name = SetNamespaceForClass(desc); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1051 | size_t offset = info_.StartTag(DW_TAG_class_type); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1052 | WriteName(name.c_str()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1053 | return offset; |
| 1054 | } |
| 1055 | |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1056 | void EndClassTag() { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1057 | info_.EndTag(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1058 | } |
| 1059 | |
| 1060 | // Set the current namespace nesting to one required by the given class. |
| 1061 | // Returns the class name with namespaces, 'L', and ';' stripped. |
| 1062 | std::string SetNamespaceForClass(const char* desc) { |
| 1063 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 1064 | desc++; // Skip the initial 'L'. |
| 1065 | size_t depth = 0; |
| 1066 | for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) { |
| 1067 | // Check whether the name at this depth is already what we need. |
| 1068 | if (depth < current_namespace_.size()) { |
| 1069 | const std::string& name = current_namespace_[depth]; |
| 1070 | if (name.compare(0, name.size(), desc, end - desc) == 0) { |
| 1071 | continue; |
| 1072 | } |
| 1073 | } |
| 1074 | // Otherwise we need to open a new namespace tag at this depth. |
| 1075 | CloseNamespacesAboveDepth(depth); |
| 1076 | info_.StartTag(DW_TAG_namespace); |
| 1077 | std::string name(desc, end - desc); |
| 1078 | WriteName(name.c_str()); |
| 1079 | current_namespace_.push_back(std::move(name)); |
| 1080 | } |
| 1081 | CloseNamespacesAboveDepth(depth); |
| 1082 | return std::string(desc, strchr(desc, ';') - desc); |
| 1083 | } |
| 1084 | |
| 1085 | // Close namespace tags to reach the given nesting depth. |
| 1086 | void CloseNamespacesAboveDepth(size_t depth) { |
| 1087 | DCHECK_LE(depth, current_namespace_.size()); |
| 1088 | while (current_namespace_.size() > depth) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1089 | info_.EndTag(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1090 | current_namespace_.pop_back(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | // For access to the ELF sections. |
| 1095 | DebugInfoWriter<ElfTypes>* owner_; |
| 1096 | // Debug abbrevs for this compilation unit only. |
| 1097 | std::vector<uint8_t> debug_abbrev_; |
| 1098 | // Temporary buffer to create and store the entries. |
| 1099 | DebugInfoEntryWriter<> info_; |
| 1100 | // Cache of already translated type descriptors. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1101 | std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1102 | // 32-bit references which need to be resolved to a type later. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1103 | // Given type may be used multiple times. Therefore we need a multimap. |
| 1104 | std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset. |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame^] | 1105 | // The current set of open namespace tags which are active and not closed yet. |
| 1106 | std::vector<std::string> current_namespace_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1107 | }; |
| 1108 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1109 | public: |
| 1110 | explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1113 | void Start() { |
| 1114 | builder_->GetDebugInfo()->Start(); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1117 | void WriteCompilationUnit(const CompilationUnit& compilation_unit) { |
| 1118 | CompilationUnitWriter writer(this); |
| 1119 | writer.Write(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1120 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1121 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1122 | void WriteTypes(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1123 | CompilationUnitWriter writer(this); |
| 1124 | writer.Write(types); |
| 1125 | } |
| 1126 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1127 | void End() { |
| 1128 | builder_->GetDebugInfo()->End(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1129 | builder_->WritePatches(".debug_info.oat_patches", |
| 1130 | ArrayRef<const uintptr_t>(debug_info_patches_)); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1131 | builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data()); |
| 1132 | builder_->WriteSection(".debug_str", &debug_str_.Data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1133 | builder_->WriteSection(".debug_loc", &debug_loc_); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 1134 | builder_->WriteSection(".debug_ranges", &debug_ranges_); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | private: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1138 | size_t WriteString(const char* str) { |
| 1139 | return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1); |
| 1140 | } |
| 1141 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1142 | ElfBuilder<ElfTypes>* builder_; |
| 1143 | std::vector<uintptr_t> debug_info_patches_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1144 | DedupVector debug_abbrev_; |
| 1145 | DedupVector debug_str_; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1146 | std::vector<uint8_t> debug_loc_; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 1147 | std::vector<uint8_t> debug_ranges_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1148 | |
| 1149 | std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only. |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1150 | }; |
| 1151 | |
| 1152 | template<typename ElfTypes> |
| 1153 | class DebugLineWriter { |
| 1154 | typedef typename ElfTypes::Addr Elf_Addr; |
| 1155 | |
| 1156 | public: |
| 1157 | explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
| 1158 | } |
| 1159 | |
| 1160 | void Start() { |
| 1161 | builder_->GetDebugLine()->Start(); |
| 1162 | } |
| 1163 | |
| 1164 | // Write line table for given set of methods. |
| 1165 | // Returns the number of bytes written. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1166 | size_t WriteCompilationUnit(CompilationUnit& compilation_unit) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1167 | const bool is64bit = Is64BitInstructionSet(builder_->GetIsa()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1168 | const Elf_Addr text_address = builder_->GetText()->Exists() |
| 1169 | ? builder_->GetText()->GetAddress() |
| 1170 | : 0; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1171 | |
| 1172 | compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1173 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1174 | std::vector<FileEntry> files; |
| 1175 | std::unordered_map<std::string, size_t> files_map; |
| 1176 | std::vector<std::string> directories; |
| 1177 | std::unordered_map<std::string, size_t> directories_map; |
| 1178 | int code_factor_bits_ = 0; |
| 1179 | int dwarf_isa = -1; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1180 | switch (builder_->GetIsa()) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1181 | case kArm: // arm actually means thumb2. |
| 1182 | case kThumb2: |
| 1183 | code_factor_bits_ = 1; // 16-bit instuctions |
| 1184 | dwarf_isa = 1; // DW_ISA_ARM_thumb. |
| 1185 | break; |
| 1186 | case kArm64: |
| 1187 | case kMips: |
| 1188 | case kMips64: |
| 1189 | code_factor_bits_ = 2; // 32-bit instructions |
| 1190 | break; |
| 1191 | case kNone: |
| 1192 | case kX86: |
| 1193 | case kX86_64: |
| 1194 | break; |
| 1195 | } |
David Srbecky | 297ed22 | 2015-04-15 01:18:12 +0100 | [diff] [blame] | 1196 | DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1197 | for (const MethodDebugInfo* mi : compilation_unit.methods_) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1198 | // Ignore function if we have already generated line table for the same address. |
| 1199 | // It would confuse the debugger and the DWARF specification forbids it. |
| 1200 | if (mi->deduped_) { |
| 1201 | continue; |
| 1202 | } |
| 1203 | |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1204 | ArrayRef<const SrcMapElem> src_mapping_table; |
| 1205 | std::vector<SrcMapElem> src_mapping_table_from_stack_maps; |
| 1206 | if (IsFromOptimizingCompiler(mi)) { |
| 1207 | // Use stack maps to create mapping table from pc to dex. |
| 1208 | const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data()); |
| 1209 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 1210 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 1211 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 1212 | DCHECK(stack_map.IsValid()); |
David Srbecky | 4f50ee2 | 2016-01-25 17:00:24 +0000 | [diff] [blame] | 1213 | // Emit only locations where we have local-variable information. |
| 1214 | // In particular, skip mappings inside the prologue. |
| 1215 | if (stack_map.HasDexRegisterMap(encoding)) { |
| 1216 | const uint32_t pc = stack_map.GetNativePcOffset(encoding); |
| 1217 | const int32_t dex = stack_map.GetDexPc(encoding); |
| 1218 | src_mapping_table_from_stack_maps.push_back({pc, dex}); |
| 1219 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1220 | } |
| 1221 | std::sort(src_mapping_table_from_stack_maps.begin(), |
| 1222 | src_mapping_table_from_stack_maps.end()); |
| 1223 | src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps); |
| 1224 | } else { |
| 1225 | // Use the mapping table provided by the quick compiler. |
| 1226 | src_mapping_table = mi->compiled_method_->GetSrcMappingTable(); |
| 1227 | } |
| 1228 | |
| 1229 | if (src_mapping_table.empty()) { |
| 1230 | continue; |
| 1231 | } |
| 1232 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1233 | Elf_Addr method_address = text_address + mi->low_pc_; |
| 1234 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1235 | PositionInfos position_infos; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1236 | const DexFile* dex = mi->dex_file_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1237 | if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) { |
| 1238 | continue; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1241 | if (position_infos.empty()) { |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1242 | continue; |
| 1243 | } |
| 1244 | |
| 1245 | opcodes.SetAddress(method_address); |
| 1246 | if (dwarf_isa != -1) { |
| 1247 | opcodes.SetISA(dwarf_isa); |
| 1248 | } |
| 1249 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1250 | // Get and deduplicate directory and filename. |
| 1251 | int file_index = 0; // 0 - primary source file of the compilation. |
| 1252 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index_); |
| 1253 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 1254 | if (source_file != nullptr) { |
| 1255 | std::string file_name(source_file); |
| 1256 | size_t file_name_slash = file_name.find_last_of('/'); |
| 1257 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 1258 | size_t class_name_slash = class_name.find_last_of('/'); |
| 1259 | std::string full_path(file_name); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1260 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1261 | // Guess directory from package name. |
| 1262 | int directory_index = 0; // 0 - current directory of the compilation. |
| 1263 | if (file_name_slash == std::string::npos && // Just filename. |
| 1264 | class_name.front() == 'L' && // Type descriptor for a class. |
| 1265 | class_name_slash != std::string::npos) { // Has package name. |
| 1266 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 1267 | auto it = directories_map.find(package_name); |
| 1268 | if (it == directories_map.end()) { |
| 1269 | directory_index = 1 + directories.size(); |
| 1270 | directories_map.emplace(package_name, directory_index); |
| 1271 | directories.push_back(package_name); |
| 1272 | } else { |
| 1273 | directory_index = it->second; |
| 1274 | } |
| 1275 | full_path = package_name + "/" + file_name; |
| 1276 | } |
| 1277 | |
| 1278 | // Add file entry. |
| 1279 | auto it2 = files_map.find(full_path); |
| 1280 | if (it2 == files_map.end()) { |
| 1281 | file_index = 1 + files.size(); |
| 1282 | files_map.emplace(full_path, file_index); |
| 1283 | files.push_back(FileEntry { |
| 1284 | file_name, |
| 1285 | directory_index, |
| 1286 | 0, // Modification time - NA. |
| 1287 | 0, // File size - NA. |
| 1288 | }); |
| 1289 | } else { |
| 1290 | file_index = it2->second; |
| 1291 | } |
| 1292 | } |
| 1293 | opcodes.SetFile(file_index); |
| 1294 | |
| 1295 | // Generate mapping opcodes from PC to Java lines. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1296 | if (file_index != 0) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1297 | bool first = true; |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1298 | for (SrcMapElem pc2dex : src_mapping_table) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1299 | uint32_t pc = pc2dex.from_; |
| 1300 | int dex_pc = pc2dex.to_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1301 | // Find mapping with address with is greater than our dex pc; then go back one step. |
| 1302 | auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc, |
| 1303 | [](uint32_t address, const DexFile::PositionInfo& entry) { |
| 1304 | return address < entry.address_; |
| 1305 | }); |
| 1306 | if (ub != position_infos.begin()) { |
| 1307 | int line = (--ub)->line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1308 | if (first) { |
| 1309 | first = false; |
| 1310 | if (pc > 0) { |
| 1311 | // Assume that any preceding code is prologue. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1312 | int first_line = position_infos.front().line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1313 | // Prologue is not a sensible place for a breakpoint. |
| 1314 | opcodes.NegateStmt(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1315 | opcodes.AddRow(method_address, first_line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1316 | opcodes.NegateStmt(); |
| 1317 | opcodes.SetPrologueEnd(); |
| 1318 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1319 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1320 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1321 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1322 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1323 | } |
| 1324 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1325 | } else { |
| 1326 | // line 0 - instruction cannot be attributed to any source line. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1327 | opcodes.AddRow(method_address, 0); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1328 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1329 | |
| 1330 | opcodes.AdvancePC(text_address + mi->high_pc_); |
| 1331 | opcodes.EndSequence(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1332 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1333 | std::vector<uint8_t> buffer; |
| 1334 | buffer.reserve(opcodes.data()->size() + KB); |
| 1335 | size_t offset = builder_->GetDebugLine()->GetSize(); |
| 1336 | WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches); |
| 1337 | builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); |
| 1338 | return buffer.size(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1339 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1340 | |
| 1341 | void End() { |
| 1342 | builder_->GetDebugLine()->End(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1343 | builder_->WritePatches(".debug_line.oat_patches", |
| 1344 | ArrayRef<const uintptr_t>(debug_line_patches)); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | private: |
| 1348 | ElfBuilder<ElfTypes>* builder_; |
| 1349 | std::vector<uintptr_t> debug_line_patches; |
| 1350 | }; |
| 1351 | |
| 1352 | template<typename ElfTypes> |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1353 | static void WriteDebugSections(ElfBuilder<ElfTypes>* builder, |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1354 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1355 | // Group the methods into compilation units based on source file. |
| 1356 | std::vector<CompilationUnit> compilation_units; |
| 1357 | const char* last_source_file = nullptr; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1358 | for (const MethodDebugInfo& mi : method_infos) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1359 | auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_); |
| 1360 | const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def); |
| 1361 | if (compilation_units.empty() || source_file != last_source_file) { |
| 1362 | compilation_units.push_back(CompilationUnit()); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1363 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1364 | CompilationUnit& cu = compilation_units.back(); |
| 1365 | cu.methods_.push_back(&mi); |
| 1366 | cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_); |
| 1367 | cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_); |
| 1368 | last_source_file = source_file; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | // Write .debug_line section. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1372 | if (!compilation_units.empty()) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1373 | DebugLineWriter<ElfTypes> line_writer(builder); |
| 1374 | line_writer.Start(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1375 | for (auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1376 | line_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1377 | } |
| 1378 | line_writer.End(); |
| 1379 | } |
| 1380 | |
| 1381 | // Write .debug_info section. |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1382 | if (!compilation_units.empty()) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1383 | DebugInfoWriter<ElfTypes> info_writer(builder); |
| 1384 | info_writer.Start(); |
| 1385 | for (const auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1386 | info_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1387 | } |
| 1388 | info_writer.End(); |
| 1389 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1392 | template <typename ElfTypes> |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1393 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
| 1394 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 1395 | bool with_signature) { |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1396 | bool generated_mapping_symbol = false; |
| 1397 | auto* strtab = builder->GetStrTab(); |
| 1398 | auto* symtab = builder->GetSymTab(); |
| 1399 | |
| 1400 | if (method_infos.empty()) { |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | // Find all addresses (low_pc) which contain deduped methods. |
| 1405 | // The first instance of method is not marked deduped_, but the rest is. |
| 1406 | std::unordered_set<uint32_t> deduped_addresses; |
| 1407 | for (const MethodDebugInfo& info : method_infos) { |
| 1408 | if (info.deduped_) { |
| 1409 | deduped_addresses.insert(info.low_pc_); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | strtab->Start(); |
| 1414 | strtab->Write(""); // strtab should start with empty string. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1415 | std::string last_name; |
| 1416 | size_t last_name_offset = 0; |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1417 | for (const MethodDebugInfo& info : method_infos) { |
| 1418 | if (info.deduped_) { |
| 1419 | continue; // Add symbol only for the first instance. |
| 1420 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1421 | std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, with_signature); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1422 | if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) { |
| 1423 | name += " [DEDUPED]"; |
| 1424 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1425 | // If we write method names without signature, we might see the same name multiple times. |
| 1426 | size_t name_offset = (name == last_name ? last_name_offset : strtab->Write(name)); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1427 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1428 | const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr; |
| 1429 | const bool is_relative = (text != nullptr); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1430 | uint32_t low_pc = info.low_pc_; |
| 1431 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 1432 | low_pc += info.compiled_method_->CodeDelta(); |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1433 | symtab->Add(name_offset, |
| 1434 | text, |
| 1435 | low_pc, |
| 1436 | is_relative, |
| 1437 | info.high_pc_ - info.low_pc_, |
| 1438 | STB_GLOBAL, |
| 1439 | STT_FUNC); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1440 | |
| 1441 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 1442 | // instructions, so that disassembler tools can correctly disassemble. |
| 1443 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 1444 | // requires it to match function symbol. Just address 0 does not work. |
| 1445 | if (info.compiled_method_->GetInstructionSet() == kThumb2) { |
| 1446 | if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1447 | symtab->Add(strtab->Write("$t"), text, info.low_pc_ & ~1, |
| 1448 | is_relative, 0, STB_LOCAL, STT_NOTYPE); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1449 | generated_mapping_symbol = true; |
| 1450 | } |
| 1451 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1452 | |
| 1453 | last_name = std::move(name); |
| 1454 | last_name_offset = name_offset; |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1455 | } |
| 1456 | strtab->End(); |
| 1457 | |
| 1458 | // Symbols are buffered and written after names (because they are smaller). |
| 1459 | // We could also do two passes in this function to avoid the buffering. |
| 1460 | symtab->Start(); |
| 1461 | symtab->Write(); |
| 1462 | symtab->End(); |
| 1463 | } |
| 1464 | |
| 1465 | template <typename ElfTypes> |
| 1466 | void WriteDebugInfo(ElfBuilder<ElfTypes>* builder, |
| 1467 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 1468 | CFIFormat cfi_format) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1469 | // Add methods to .symtab. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1470 | WriteDebugSymbols(builder, method_infos, true /* with_signature */); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1471 | // Generate CFI (stack unwinding information). |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 1472 | WriteCFISection(builder, method_infos, cfi_format, true /* write_oat_patches */); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1473 | // Write DWARF .debug_* sections. |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1474 | WriteDebugSections(builder, method_infos); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 1477 | static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) { |
| 1478 | // Configure the compression library. |
| 1479 | CrcGenerateTable(); |
| 1480 | Crc64GenerateTable(); |
| 1481 | CLzma2EncProps lzma2Props; |
| 1482 | Lzma2EncProps_Init(&lzma2Props); |
| 1483 | lzma2Props.lzmaProps.level = 1; // Fast compression. |
| 1484 | Lzma2EncProps_Normalize(&lzma2Props); |
| 1485 | CXzProps props; |
| 1486 | XzProps_Init(&props); |
| 1487 | props.lzma2Props = &lzma2Props; |
| 1488 | // Implement the required interface for communication (written in C so no virtual methods). |
| 1489 | struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress { |
| 1490 | static SRes ReadImpl(void* p, void* buf, size_t* size) { |
| 1491 | auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqInStream*>(p)); |
| 1492 | *size = std::min(*size, ctx->src_->size() - ctx->src_pos_); |
| 1493 | memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size); |
| 1494 | ctx->src_pos_ += *size; |
| 1495 | return SZ_OK; |
| 1496 | } |
| 1497 | static size_t WriteImpl(void* p, const void* buf, size_t size) { |
| 1498 | auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqOutStream*>(p)); |
| 1499 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf); |
| 1500 | ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size); |
| 1501 | return size; |
| 1502 | } |
| 1503 | static SRes ProgressImpl(void* , UInt64, UInt64) { |
| 1504 | return SZ_OK; |
| 1505 | } |
| 1506 | size_t src_pos_; |
| 1507 | const std::vector<uint8_t>* src_; |
| 1508 | std::vector<uint8_t>* dst_; |
| 1509 | }; |
| 1510 | XzCallbacks callbacks; |
| 1511 | callbacks.Read = XzCallbacks::ReadImpl; |
| 1512 | callbacks.Write = XzCallbacks::WriteImpl; |
| 1513 | callbacks.Progress = XzCallbacks::ProgressImpl; |
| 1514 | callbacks.src_pos_ = 0; |
| 1515 | callbacks.src_ = src; |
| 1516 | callbacks.dst_ = dst; |
| 1517 | // Compress. |
| 1518 | SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks); |
| 1519 | CHECK_EQ(res, SZ_OK); |
| 1520 | } |
| 1521 | |
| 1522 | template <typename ElfTypes> |
| 1523 | void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder, |
| 1524 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
| 1525 | const InstructionSet isa = parent_builder->GetIsa(); |
| 1526 | std::vector<uint8_t> buffer; |
| 1527 | buffer.reserve(KB); |
| 1528 | VectorOutputStream out("Mini-debug-info ELF file", &buffer); |
| 1529 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 1530 | builder->Start(); |
| 1531 | // Write .rodata and .text as NOBITS sections. |
| 1532 | // This allows tools to detect virtual address relocation of the parent ELF file. |
| 1533 | builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress()); |
| 1534 | builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize()); |
| 1535 | builder->SetVirtualAddress(parent_builder->GetText()->GetAddress()); |
| 1536 | builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize()); |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1537 | WriteDebugSymbols(builder.get(), method_infos, false /* with_signature */); |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 1538 | WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT, false /* write_oat_paches */); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 1539 | builder->End(); |
| 1540 | CHECK(builder->Good()); |
| 1541 | std::vector<uint8_t> compressed_buffer; |
| 1542 | compressed_buffer.reserve(buffer.size() / 4); |
| 1543 | XzCompress(&buffer, &compressed_buffer); |
| 1544 | parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer); |
| 1545 | } |
| 1546 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1547 | template <typename ElfTypes> |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1548 | static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal( |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1549 | const dwarf::MethodDebugInfo& method_info) { |
| 1550 | const InstructionSet isa = method_info.compiled_method_->GetInstructionSet(); |
| 1551 | std::vector<uint8_t> buffer; |
| 1552 | buffer.reserve(KB); |
| 1553 | VectorOutputStream out("Debug ELF file", &buffer); |
| 1554 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 1555 | builder->Start(); |
| 1556 | WriteDebugInfo(builder.get(), |
| 1557 | ArrayRef<const MethodDebugInfo>(&method_info, 1), |
| 1558 | DW_DEBUG_FRAME_FORMAT); |
| 1559 | builder->End(); |
| 1560 | CHECK(builder->Good()); |
| 1561 | // Make a copy of the buffer. We want to shrink it anyway. |
| 1562 | uint8_t* result = new uint8_t[buffer.size()]; |
| 1563 | CHECK(result != nullptr); |
| 1564 | memcpy(result, buffer.data(), buffer.size()); |
| 1565 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 1566 | } |
| 1567 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1568 | ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info) { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1569 | const InstructionSet isa = method_info.compiled_method_->GetInstructionSet(); |
| 1570 | if (Is64BitInstructionSet(isa)) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1571 | return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1572 | } else { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1573 | return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | template <typename ElfTypes> |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1578 | static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal( |
| 1579 | const InstructionSet isa, const ArrayRef<mirror::Class*>& types) |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1580 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1581 | std::vector<uint8_t> buffer; |
| 1582 | buffer.reserve(KB); |
| 1583 | VectorOutputStream out("Debug ELF file", &buffer); |
| 1584 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 1585 | builder->Start(); |
| 1586 | |
| 1587 | DebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 1588 | info_writer.Start(); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1589 | info_writer.WriteTypes(types); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1590 | info_writer.End(); |
| 1591 | |
| 1592 | builder->End(); |
| 1593 | CHECK(builder->Good()); |
| 1594 | // Make a copy of the buffer. We want to shrink it anyway. |
| 1595 | uint8_t* result = new uint8_t[buffer.size()]; |
| 1596 | CHECK(result != nullptr); |
| 1597 | memcpy(result, buffer.data(), buffer.size()); |
| 1598 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 1599 | } |
| 1600 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1601 | ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa, |
| 1602 | const ArrayRef<mirror::Class*>& types) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1603 | if (Is64BitInstructionSet(isa)) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1604 | return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1605 | } else { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1606 | return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1610 | // Explicit instantiations |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1611 | template void WriteDebugInfo<ElfTypes32>( |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1612 | ElfBuilder<ElfTypes32>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1613 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1614 | CFIFormat cfi_format); |
| 1615 | template void WriteDebugInfo<ElfTypes64>( |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1616 | ElfBuilder<ElfTypes64>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1617 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1618 | CFIFormat cfi_format); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 1619 | template void WriteMiniDebugInfo<ElfTypes32>( |
| 1620 | ElfBuilder<ElfTypes32>* builder, |
| 1621 | const ArrayRef<const MethodDebugInfo>& method_infos); |
| 1622 | template void WriteMiniDebugInfo<ElfTypes64>( |
| 1623 | ElfBuilder<ElfTypes64>* builder, |
| 1624 | const ArrayRef<const MethodDebugInfo>& method_infos); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1625 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1626 | } // namespace dwarf |
| 1627 | } // namespace art |