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