blob: 2e98b69c47e960527430bc451d0be8a38fc164ce [file] [log] [blame]
David Srbecky3b9d57a2015-04-10 00:22:14 +01001/*
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 Srbecky5e974a62016-01-22 14:25:03 +000019#include <algorithm>
David Srbecky626a1662015-04-12 13:12:26 +010020#include <unordered_set>
Vladimir Marko10c13562015-11-25 14:33:36 +000021#include <vector>
David Srbecky9b3607d2016-01-14 18:15:54 +000022#include <cstdio>
David Srbecky626a1662015-04-12 13:12:26 +010023
Andreas Gampee3d623e2015-05-01 16:11:04 -070024#include "base/casts.h"
David Srbecky04b05262015-11-09 18:05:48 +000025#include "base/stl_util.h"
David Srbecky9b3607d2016-01-14 18:15:54 +000026#include "linear_alloc.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010027#include "compiled_method.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010028#include "dex_file-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000029#include "driver/compiler_driver.h"
David Srbecky04b05262015-11-09 18:05:48 +000030#include "dwarf/dedup_vector.h"
David Srbecky91cb54e2016-01-15 13:47:59 +000031#include "dwarf/expression.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010032#include "dwarf/headers.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000033#include "dwarf/method_debug_info.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010034#include "dwarf/register.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000035#include "elf_builder.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000036#include "linker/vector_output_stream.h"
Tamas Berghammer86e42782016-01-05 14:29:02 +000037#include "mirror/array.h"
38#include "mirror/class-inl.h"
39#include "mirror/class.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010040#include "oat_writer.h"
David Srbecky0fd295f2015-11-16 16:39:10 +000041#include "stack_map.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000042#include "utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010043
David Srbecky5b1c2ca2016-01-25 17:32:41 +000044// liblzma.
45#include "XzEnc.h"
46#include "7zCrc.h"
47#include "XzCrc64.h"
48
David Srbecky3b9d57a2015-04-10 00:22:14 +010049namespace art {
50namespace dwarf {
51
David Srbeckye0febdf2015-12-17 20:53:07 +000052// The ARM specification defines three special mapping symbols
53// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
54// These symbols can be used by tools, for example, to pretty
55// print instructions correctly. Objdump will use them if they
56// exist, but it will still work well without them.
57// However, these extra symbols take space, so let's just generate
58// one symbol which marks the whole .text section as code.
59constexpr bool kGenerateSingleArmMappingSymbol = true;
60
David Srbecky0fd295f2015-11-16 16:39:10 +000061static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
62 switch (isa) {
63 case kArm:
64 case kThumb2:
65 return Reg::ArmCore(machine_reg);
66 case kArm64:
67 return Reg::Arm64Core(machine_reg);
68 case kX86:
69 return Reg::X86Core(machine_reg);
70 case kX86_64:
71 return Reg::X86_64Core(machine_reg);
72 case kMips:
73 return Reg::MipsCore(machine_reg);
74 case kMips64:
75 return Reg::Mips64Core(machine_reg);
76 default:
77 LOG(FATAL) << "Unknown instruction set: " << isa;
78 UNREACHABLE();
79 }
80}
81
82static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
83 switch (isa) {
84 case kArm:
85 case kThumb2:
86 return Reg::ArmFp(machine_reg);
87 case kArm64:
88 return Reg::Arm64Fp(machine_reg);
89 case kX86:
90 return Reg::X86Fp(machine_reg);
91 case kX86_64:
92 return Reg::X86_64Fp(machine_reg);
93 default:
94 LOG(FATAL) << "Unknown instruction set: " << isa;
95 UNREACHABLE();
96 }
97}
98
David Srbecky6d8c8f02015-10-26 10:57:09 +000099static void WriteCIE(InstructionSet isa,
100 CFIFormat format,
101 std::vector<uint8_t>* buffer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100102 // Scratch registers should be marked as undefined. This tells the
103 // debugger that its value in the previous frame is not recoverable.
104 bool is64bit = Is64BitInstructionSet(isa);
105 switch (isa) {
106 case kArm:
107 case kThumb2: {
108 DebugFrameOpCodeWriter<> opcodes;
109 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
110 // core registers.
111 for (int reg = 0; reg < 13; reg++) {
112 if (reg < 4 || reg == 12) {
113 opcodes.Undefined(Reg::ArmCore(reg));
114 } else {
115 opcodes.SameValue(Reg::ArmCore(reg));
116 }
117 }
118 // fp registers.
119 for (int reg = 0; reg < 32; reg++) {
120 if (reg < 16) {
121 opcodes.Undefined(Reg::ArmFp(reg));
122 } else {
123 opcodes.SameValue(Reg::ArmFp(reg));
124 }
125 }
David Srbecky527c9c72015-04-17 21:14:10 +0100126 auto return_reg = Reg::ArmCore(14); // R14(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100128 return;
129 }
130 case kArm64: {
131 DebugFrameOpCodeWriter<> opcodes;
132 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
133 // core registers.
134 for (int reg = 0; reg < 30; reg++) {
135 if (reg < 8 || reg == 16 || reg == 17) {
136 opcodes.Undefined(Reg::Arm64Core(reg));
137 } else {
138 opcodes.SameValue(Reg::Arm64Core(reg));
139 }
140 }
141 // fp registers.
142 for (int reg = 0; reg < 32; reg++) {
143 if (reg < 8 || reg >= 16) {
144 opcodes.Undefined(Reg::Arm64Fp(reg));
145 } else {
146 opcodes.SameValue(Reg::Arm64Fp(reg));
147 }
148 }
David Srbecky527c9c72015-04-17 21:14:10 +0100149 auto return_reg = Reg::Arm64Core(30); // R30(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000150 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100151 return;
152 }
153 case kMips:
154 case kMips64: {
155 DebugFrameOpCodeWriter<> opcodes;
156 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
157 // core registers.
158 for (int reg = 1; reg < 26; reg++) {
159 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
160 opcodes.Undefined(Reg::MipsCore(reg));
161 } else {
162 opcodes.SameValue(Reg::MipsCore(reg));
163 }
164 }
David Srbecky527c9c72015-04-17 21:14:10 +0100165 auto return_reg = Reg::MipsCore(31); // R31(RA).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100167 return;
168 }
169 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100170 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
171 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100172 DebugFrameOpCodeWriter<> opcodes;
173 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
174 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
175 // core registers.
176 for (int reg = 0; reg < 8; reg++) {
177 if (reg <= 3) {
178 opcodes.Undefined(Reg::X86Core(reg));
179 } else if (reg == 4) {
180 // Stack pointer.
181 } else {
182 opcodes.SameValue(Reg::X86Core(reg));
183 }
184 }
185 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100186 if (generate_opcodes_for_x86_fp) {
187 for (int reg = 0; reg < 8; reg++) {
188 opcodes.Undefined(Reg::X86Fp(reg));
189 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100190 }
David Srbecky527c9c72015-04-17 21:14:10 +0100191 auto return_reg = Reg::X86Core(8); // R8(EIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000192 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100193 return;
194 }
195 case kX86_64: {
196 DebugFrameOpCodeWriter<> opcodes;
197 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
198 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
199 // core registers.
200 for (int reg = 0; reg < 16; reg++) {
201 if (reg == 4) {
202 // Stack pointer.
203 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
204 opcodes.Undefined(Reg::X86_64Core(reg));
205 } else {
206 opcodes.SameValue(Reg::X86_64Core(reg));
207 }
208 }
209 // fp registers.
210 for (int reg = 0; reg < 16; reg++) {
211 if (reg < 12) {
212 opcodes.Undefined(Reg::X86_64Fp(reg));
213 } else {
214 opcodes.SameValue(Reg::X86_64Fp(reg));
215 }
216 }
David Srbecky527c9c72015-04-17 21:14:10 +0100217 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000218 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100219 return;
220 }
221 case kNone:
222 break;
223 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000224 LOG(FATAL) << "Cannot write CIE frame for ISA " << isa;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100225 UNREACHABLE();
226}
227
David Srbecky6d8c8f02015-10-26 10:57:09 +0000228template<typename ElfTypes>
229void WriteCFISection(ElfBuilder<ElfTypes>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000230 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky5e974a62016-01-22 14:25:03 +0000231 CFIFormat format,
232 bool write_oat_patches) {
David Srbeckye0febdf2015-12-17 20:53:07 +0000233 CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000234 typedef typename ElfTypes::Addr Elf_Addr;
235
Tamas Berghammer86e42782016-01-05 14:29:02 +0000236 if (method_infos.empty()) {
237 return;
238 }
239
David Srbecky6d8c8f02015-10-26 10:57:09 +0000240 std::vector<uint32_t> binary_search_table;
241 std::vector<uintptr_t> patch_locations;
242 if (format == DW_EH_FRAME_FORMAT) {
243 binary_search_table.reserve(2 * method_infos.size());
244 } else {
245 patch_locations.reserve(method_infos.size());
246 }
David Srbecky527c9c72015-04-17 21:14:10 +0100247
David Srbecky5e974a62016-01-22 14:25:03 +0000248 // The methods can be written any order.
249 // Let's therefore sort them in the lexicographical order of the opcodes.
250 // This has no effect on its own. However, if the final .debug_frame section is
251 // compressed it reduces the size since similar opcodes sequences are grouped.
252 std::vector<const MethodDebugInfo*> sorted_method_infos;
253 sorted_method_infos.reserve(method_infos.size());
254 for (size_t i = 0; i < method_infos.size(); i++) {
255 sorted_method_infos.push_back(&method_infos[i]);
256 }
257 std::sort(
258 sorted_method_infos.begin(),
259 sorted_method_infos.end(),
260 [](const MethodDebugInfo* lhs, const MethodDebugInfo* rhs) {
261 ArrayRef<const uint8_t> l = lhs->compiled_method_->GetCFIInfo();
262 ArrayRef<const uint8_t> r = rhs->compiled_method_->GetCFIInfo();
263 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
264 });
265
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100266 // Write .eh_frame/.debug_frame section.
David Srbeckye0febdf2015-12-17 20:53:07 +0000267 auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT
David Srbecky6d8c8f02015-10-26 10:57:09 +0000268 ? builder->GetDebugFrame()
269 : builder->GetEhFrame());
270 {
271 cfi_section->Start();
272 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +0000273 const Elf_Addr text_address = builder->GetText()->Exists()
274 ? builder->GetText()->GetAddress()
275 : 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000276 const Elf_Addr cfi_address = cfi_section->GetAddress();
277 const Elf_Addr cie_address = cfi_address;
278 Elf_Addr buffer_address = cfi_address;
279 std::vector<uint8_t> buffer; // Small temporary buffer.
280 WriteCIE(builder->GetIsa(), format, &buffer);
281 cfi_section->WriteFully(buffer.data(), buffer.size());
282 buffer_address += buffer.size();
283 buffer.clear();
David Srbecky5e974a62016-01-22 14:25:03 +0000284 for (const MethodDebugInfo* mi : sorted_method_infos) {
285 if (!mi->deduped_) { // Only one FDE per unique address.
286 ArrayRef<const uint8_t> opcodes = mi->compiled_method_->GetCFIInfo();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000287 if (!opcodes.empty()) {
David Srbecky5e974a62016-01-22 14:25:03 +0000288 const Elf_Addr code_address = text_address + mi->low_pc_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000289 if (format == DW_EH_FRAME_FORMAT) {
290 binary_search_table.push_back(
291 dchecked_integral_cast<uint32_t>(code_address));
292 binary_search_table.push_back(
293 dchecked_integral_cast<uint32_t>(buffer_address));
294 }
295 WriteFDE(is64bit, cfi_address, cie_address,
David Srbecky5e974a62016-01-22 14:25:03 +0000296 code_address, mi->high_pc_ - mi->low_pc_,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000297 opcodes, format, buffer_address, &buffer,
298 &patch_locations);
299 cfi_section->WriteFully(buffer.data(), buffer.size());
300 buffer_address += buffer.size();
301 buffer.clear();
302 }
David Srbecky6d73c9d2015-05-01 15:00:40 +0100303 }
David Srbecky8dc73242015-04-12 11:40:39 +0100304 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000305 cfi_section->End();
David Srbecky8dc73242015-04-12 11:40:39 +0100306 }
David Srbecky527c9c72015-04-17 21:14:10 +0100307
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100308 if (format == DW_EH_FRAME_FORMAT) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000309 auto* header_section = builder->GetEhFrameHdr();
310 header_section->Start();
311 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100312 // Write .eh_frame_hdr section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000313 std::vector<uint8_t> buffer;
314 Writer<> header(&buffer);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100315 header.PushUint8(1); // Version.
316 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
317 // so we have to use pcrel which means relative to the pointer's location.
318 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
319 // Encoding of binary search table size.
320 header.PushUint8(DW_EH_PE_udata4);
321 // Encoding of binary search table addresses - libunwind supports only this
322 // specific combination, which means relative to the start of .eh_frame_hdr.
323 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000324 // .eh_frame pointer
325 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100326 // Binary search table size (number of entries).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000327 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
328 header_section->WriteFully(buffer.data(), buffer.size());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100329 // Binary search table.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000330 for (size_t i = 0; i < binary_search_table.size(); i++) {
331 // Make addresses section-relative since we know the header address now.
332 binary_search_table[i] -= header_address;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100333 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000334 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
335 header_section->End();
336 } else {
David Srbecky5e974a62016-01-22 14:25:03 +0000337 if (write_oat_patches) {
338 builder->WritePatches(".debug_frame.oat_patches",
339 ArrayRef<const uintptr_t>(patch_locations));
340 }
David Srbecky033d7452015-04-30 19:57:35 +0100341 }
David Srbecky8dc73242015-04-12 11:40:39 +0100342}
343
David Srbecky996ed0b2015-11-27 10:27:11 +0000344namespace {
345 struct CompilationUnit {
346 std::vector<const MethodDebugInfo*> methods_;
347 size_t debug_line_offset_ = 0;
David Srbecky5cc349f2015-12-18 15:04:48 +0000348 uintptr_t low_pc_ = std::numeric_limits<uintptr_t>::max();
349 uintptr_t high_pc_ = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000350 };
351
David Srbeckyb06e28e2015-12-10 13:15:00 +0000352 typedef std::vector<DexFile::LocalInfo> LocalInfos;
David Srbecky996ed0b2015-11-27 10:27:11 +0000353
David Srbeckyb06e28e2015-12-10 13:15:00 +0000354 void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) {
355 static_cast<LocalInfos*>(ctx)->push_back(entry);
356 }
357
358 typedef std::vector<DexFile::PositionInfo> PositionInfos;
359
360 bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
361 static_cast<PositionInfos*>(ctx)->push_back(entry);
362 return false;
363 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000364
365 std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
366 std::vector<const char*> names;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000367 if (mi->code_item_ != nullptr) {
368 const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_);
369 if (stream != nullptr) {
370 DecodeUnsignedLeb128(&stream); // line.
371 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
372 for (uint32_t i = 0; i < parameters_size; ++i) {
373 uint32_t id = DecodeUnsignedLeb128P1(&stream);
374 names.push_back(mi->dex_file_->StringDataByIdx(id));
375 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000376 }
377 }
378 return names;
379 }
380
381 struct VariableLocation {
382 uint32_t low_pc;
383 uint32_t high_pc;
384 DexRegisterLocation reg_lo; // May be None if the location is unknown.
385 DexRegisterLocation reg_hi; // Most significant bits of 64-bit value.
386 };
387
388 // Get the location of given dex register (e.g. stack or machine register).
389 // Note that the location might be different based on the current pc.
390 // The result will cover all ranges where the variable is in scope.
391 std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info,
392 uint16_t vreg,
393 bool is64bitValue,
394 uint32_t dex_pc_low,
395 uint32_t dex_pc_high) {
396 std::vector<VariableLocation> variable_locations;
397
398 // Get stack maps sorted by pc (they might not be sorted internally).
399 const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data());
400 const StackMapEncoding encoding = code_info.ExtractEncoding();
401 std::map<uint32_t, StackMap> stack_maps;
402 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
403 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
404 DCHECK(stack_map.IsValid());
405 const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding);
406 DCHECK_LE(low_pc, method_info->high_pc_);
407 stack_maps.emplace(low_pc, stack_map);
408 }
409
410 // Create entries for the requested register based on stack map data.
411 for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
412 const StackMap& stack_map = it->second;
413 const uint32_t low_pc = it->first;
414 auto next_it = it;
415 next_it++;
416 const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
417 : method_info->high_pc_;
418 DCHECK_LE(low_pc, high_pc);
419 if (low_pc == high_pc) {
420 continue; // Ignore if the address range is empty.
421 }
422
423 // Check that the stack map is in the requested range.
424 uint32_t dex_pc = stack_map.GetDexPc(encoding);
425 if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
426 continue;
427 }
428
429 // Find the location of the dex register.
430 DexRegisterLocation reg_lo = DexRegisterLocation::None();
431 DexRegisterLocation reg_hi = DexRegisterLocation::None();
432 if (stack_map.HasDexRegisterMap(encoding)) {
433 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
434 stack_map, encoding, method_info->code_item_->registers_size_);
435 reg_lo = dex_register_map.GetDexRegisterLocation(
436 vreg, method_info->code_item_->registers_size_, code_info, encoding);
437 if (is64bitValue) {
438 reg_hi = dex_register_map.GetDexRegisterLocation(
439 vreg + 1, method_info->code_item_->registers_size_, code_info, encoding);
440 }
441 }
442
443 // Add location entry for this address range.
444 if (!variable_locations.empty() &&
445 variable_locations.back().reg_lo == reg_lo &&
446 variable_locations.back().reg_hi == reg_hi &&
447 variable_locations.back().high_pc == low_pc) {
448 // Merge with the previous entry (extend its range).
449 variable_locations.back().high_pc = high_pc;
450 } else {
451 variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
452 }
453 }
454
455 return variable_locations;
456 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000457
458 bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) {
459 return method_info->compiled_method_->GetQuickCode().size() > 0 &&
460 method_info->compiled_method_->GetVmapTable().size() > 0 &&
461 method_info->compiled_method_->GetGcMap().size() == 0 &&
462 method_info->code_item_ != nullptr;
463 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000464} // namespace
David Srbecky04b05262015-11-09 18:05:48 +0000465
466// Helper class to write .debug_info and its supporting sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000467template<typename ElfTypes>
David Srbeckyb851b492015-11-11 20:19:38 +0000468class DebugInfoWriter {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000469 typedef typename ElfTypes::Addr Elf_Addr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100470
David Srbecky04b05262015-11-09 18:05:48 +0000471 // Helper class to write one compilation unit.
472 // It holds helper methods and temporary state.
473 class CompilationUnitWriter {
474 public:
475 explicit CompilationUnitWriter(DebugInfoWriter* owner)
476 : owner_(owner),
477 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) {
478 }
479
480 void Write(const CompilationUnit& compilation_unit) {
481 CHECK(!compilation_unit.methods_.empty());
David Srbecky5cc349f2015-12-18 15:04:48 +0000482 const Elf_Addr text_address = owner_->builder_->GetText()->Exists()
483 ? owner_->builder_->GetText()->GetAddress()
484 : 0;
485 const uintptr_t cu_size = compilation_unit.high_pc_ - compilation_unit.low_pc_;
David Srbecky04b05262015-11-09 18:05:48 +0000486
487 info_.StartTag(DW_TAG_compile_unit);
488 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
489 info_.WriteData1(DW_AT_language, DW_LANG_Java);
Tamas Berghammer8c557122015-12-10 15:06:25 +0000490 info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT"));
David Srbecky04b05262015-11-09 18:05:48 +0000491 info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000492 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size));
David Srbecky0fd295f2015-11-16 16:39:10 +0000493 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_);
David Srbecky04b05262015-11-09 18:05:48 +0000494
495 const char* last_dex_class_desc = nullptr;
496 for (auto mi : compilation_unit.methods_) {
497 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000498 const DexFile::CodeItem* dex_code = mi->code_item_;
David Srbecky04b05262015-11-09 18:05:48 +0000499 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_);
500 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
501 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
502 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
David Srbecky996ed0b2015-11-27 10:27:11 +0000503 const bool is_static = (mi->access_flags_ & kAccStatic) != 0;
David Srbecky04b05262015-11-09 18:05:48 +0000504
505 // Enclose the method in correct class definition.
506 if (last_dex_class_desc != dex_class_desc) {
507 if (last_dex_class_desc != nullptr) {
508 EndClassTag(last_dex_class_desc);
509 }
Tamas Berghammer86e42782016-01-05 14:29:02 +0000510 // Write reference tag for the class we are about to declare.
511 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
512 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
513 size_t type_attrib_offset = info_.size();
514 info_.WriteRef4(DW_AT_type, 0);
515 info_.EndTag();
516 // Declare the class that owns this method.
517 size_t class_offset = StartClassTag(dex_class_desc);
518 info_.UpdateUint32(type_attrib_offset, class_offset);
519 info_.WriteFlag(DW_AT_declaration, true);
David Srbecky04b05262015-11-09 18:05:48 +0000520 // Check that each class is defined only once.
521 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
522 CHECK(unique) << "Redefinition of " << dex_class_desc;
523 last_dex_class_desc = dex_class_desc;
524 }
525
David Srbecky04b05262015-11-09 18:05:48 +0000526 int start_depth = info_.Depth();
527 info_.StartTag(DW_TAG_subprogram);
528 WriteName(dex->GetMethodName(dex_method));
529 info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000530 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_));
David Srbecky91cb54e2016-01-15 13:47:59 +0000531 std::vector<uint8_t> expr_buffer;
532 Expression expr(&expr_buffer);
533 expr.WriteOpCallFrameCfa();
534 info_.WriteExprLoc(DW_AT_frame_base, expr);
David Srbecky04b05262015-11-09 18:05:48 +0000535 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
David Srbeckyb06e28e2015-12-10 13:15:00 +0000536
537 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
538 // guarantee order or uniqueness so it is safer to iterate over them manually.
539 // DecodeDebugLocalInfo might not also be available if there is no debug info.
540 std::vector<const char*> param_names = GetParamNames(mi);
541 uint32_t arg_reg = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000542 if (!is_static) {
543 info_.StartTag(DW_TAG_formal_parameter);
544 WriteName("this");
545 info_.WriteFlag(DW_AT_artificial, true);
546 WriteLazyType(dex_class_desc);
David Srbeckyb06e28e2015-12-10 13:15:00 +0000547 if (dex_code != nullptr) {
548 // Write the stack location of the parameter.
549 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
550 const bool is64bitValue = false;
551 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
552 }
553 arg_reg++;
David Srbecky996ed0b2015-11-27 10:27:11 +0000554 info_.EndTag();
555 }
David Srbecky04b05262015-11-09 18:05:48 +0000556 if (dex_params != nullptr) {
557 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
558 info_.StartTag(DW_TAG_formal_parameter);
559 // Parameter names may not be always available.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000560 if (i < param_names.size()) {
David Srbecky04b05262015-11-09 18:05:48 +0000561 WriteName(param_names[i]);
562 }
David Srbecky0fd295f2015-11-16 16:39:10 +0000563 // Write the type.
564 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
565 WriteLazyType(type_desc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000566 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
David Srbeckyb06e28e2015-12-10 13:15:00 +0000567 if (dex_code != nullptr) {
568 // Write the stack location of the parameter.
569 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
570 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
571 }
572 arg_reg += is64bitValue ? 2 : 1;
David Srbecky04b05262015-11-09 18:05:48 +0000573 info_.EndTag();
574 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000575 if (dex_code != nullptr) {
576 DCHECK_EQ(arg_reg, dex_code->ins_size_);
David Srbecky0fd295f2015-11-16 16:39:10 +0000577 }
David Srbecky04b05262015-11-09 18:05:48 +0000578 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000579
580 // Write local variables.
581 LocalInfos local_infos;
582 if (dex->DecodeDebugLocalInfo(dex_code,
583 is_static,
584 mi->dex_method_index_,
585 LocalInfoCallback,
586 &local_infos)) {
587 for (const DexFile::LocalInfo& var : local_infos) {
588 if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) {
589 info_.StartTag(DW_TAG_variable);
590 WriteName(var.name_);
591 WriteLazyType(var.descriptor_);
592 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
593 WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_,
594 var.start_address_, var.end_address_);
595 info_.EndTag();
596 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000597 }
598 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000599
David Srbecky04b05262015-11-09 18:05:48 +0000600 info_.EndTag();
601 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
602 }
603 if (last_dex_class_desc != nullptr) {
604 EndClassTag(last_dex_class_desc);
605 }
606 CHECK_EQ(info_.Depth(), 1);
607 FinishLazyTypes();
608 info_.EndTag(); // DW_TAG_compile_unit
609 std::vector<uint8_t> buffer;
610 buffer.reserve(info_.data()->size() + KB);
611 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
612 const size_t debug_abbrev_offset =
613 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
614 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
615 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
616 }
617
Tamas Berghammer86e42782016-01-05 14:29:02 +0000618 void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
619 info_.StartTag(DW_TAG_compile_unit);
620 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
621 info_.WriteData1(DW_AT_language, DW_LANG_Java);
622
David Srbecky9b3607d2016-01-14 18:15:54 +0000623 std::vector<uint8_t> expr_buffer;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000624 for (mirror::Class* type : types) {
625 if (type->IsPrimitive()) {
626 // For primitive types the definition and the declaration is the same.
627 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
628 WriteTypeDeclaration(type->GetDescriptor(nullptr));
629 }
630 } else if (type->IsArrayClass()) {
631 mirror::Class* element_type = type->GetComponentType();
632 uint32_t component_size = type->GetComponentSize();
633 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
634 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
635
636 info_.StartTag(DW_TAG_array_type);
637 std::string descriptor_string;
638 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
David Srbecky9b3607d2016-01-14 18:15:54 +0000639 WriteLinkageName(type);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000640 info_.WriteUdata(DW_AT_data_member_location, data_offset);
641 info_.StartTag(DW_TAG_subrange_type);
David Srbecky9b3607d2016-01-14 18:15:54 +0000642 Expression count_expr(&expr_buffer);
David Srbecky91cb54e2016-01-15 13:47:59 +0000643 count_expr.WriteOpPushObjectAddress();
644 count_expr.WriteOpPlusUconst(length_offset);
645 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
646 info_.WriteExprLoc(DW_AT_count, count_expr);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000647 info_.EndTag(); // DW_TAG_subrange_type.
648 info_.EndTag(); // DW_TAG_array_type.
David Srbecky9b3607d2016-01-14 18:15:54 +0000649 } else if (type->IsInterface()) {
650 // Skip. Variables cannot have an interface as a dynamic type.
651 // We do not expose the interface information to the debugger in any way.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000652 } else {
David Srbecky9b3607d2016-01-14 18:15:54 +0000653 // Declare base class. We can not use the standard WriteLazyType
654 // since we want to avoid the DW_TAG_reference_tag wrapping.
655 mirror::Class* base_class = type->GetSuperClass();
656 size_t base_class_declaration_offset = 0;
657 if (base_class != nullptr) {
658 std::string tmp_storage;
659 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
660 base_class_declaration_offset = StartClassTag(base_class_desc);
661 info_.WriteFlag(DW_AT_declaration, true);
662 WriteLinkageName(base_class);
663 EndClassTag(base_class_desc);
664 }
665
Tamas Berghammer86e42782016-01-05 14:29:02 +0000666 std::string descriptor_string;
667 const char* desc = type->GetDescriptor(&descriptor_string);
668 StartClassTag(desc);
669
670 if (!type->IsVariableSize()) {
671 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
672 }
673
David Srbecky9b3607d2016-01-14 18:15:54 +0000674 WriteLinkageName(type);
675
676 if (type->IsObjectClass()) {
677 // Generate artificial member which is used to get the dynamic type of variable.
678 // The run-time value of this field will correspond to linkage name of some type.
679 // We need to do it only once in j.l.Object since all other types inherit it.
680 info_.StartTag(DW_TAG_member);
681 WriteName(".dynamic_type");
682 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
683 info_.WriteFlag(DW_AT_artificial, true);
684 // Create DWARF expression to get the value of the methods_ field.
685 Expression expr(&expr_buffer);
686 // The address of the object has been implicitly pushed on the stack.
687 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
688 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
689 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
690 expr.WriteOpDerefSize(4);
691 if (kPoisonHeapReferences) {
692 expr.WriteOpNeg();
693 // DWARF stack is pointer sized. Ensure that the high bits are clear.
694 expr.WriteOpConstu(0xFFFFFFFF);
695 expr.WriteOpAnd();
696 }
697 // Add offset to the methods_ field.
698 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
699 // Top of stack holds the location of the field now.
700 info_.WriteExprLoc(DW_AT_data_member_location, expr);
701 info_.EndTag(); // DW_TAG_member.
702 }
703
Tamas Berghammer86e42782016-01-05 14:29:02 +0000704 // Base class.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000705 if (base_class != nullptr) {
706 info_.StartTag(DW_TAG_inheritance);
David Srbecky9b3607d2016-01-14 18:15:54 +0000707 info_.WriteRef4(DW_AT_type, base_class_declaration_offset);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000708 info_.WriteUdata(DW_AT_data_member_location, 0);
709 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
710 info_.EndTag(); // DW_TAG_inheritance.
711 }
712
713 // Member variables.
714 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
715 ArtField* field = type->GetInstanceField(i);
716 info_.StartTag(DW_TAG_member);
717 WriteName(field->GetName());
718 WriteLazyType(field->GetTypeDescriptor());
719 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
720 uint32_t access_flags = field->GetAccessFlags();
721 if (access_flags & kAccPublic) {
722 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
723 } else if (access_flags & kAccProtected) {
724 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
725 } else if (access_flags & kAccPrivate) {
726 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
727 }
728 info_.EndTag(); // DW_TAG_member.
729 }
730
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000731 if (type->IsStringClass()) {
732 // Emit debug info about an artifical class member for java.lang.String which represents
733 // the first element of the data stored in a string instance. Consumers of the debug
734 // info will be able to read the content of java.lang.String based on the count (real
735 // field) and based on the location of this data member.
736 info_.StartTag(DW_TAG_member);
737 WriteName("value");
738 // We don't support fields with C like array types so we just say its type is java char.
739 WriteLazyType("C"); // char.
740 info_.WriteUdata(DW_AT_data_member_location,
741 mirror::String::ValueOffset().Uint32Value());
742 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
743 info_.EndTag(); // DW_TAG_member.
744 }
745
Tamas Berghammer86e42782016-01-05 14:29:02 +0000746 EndClassTag(desc);
747 }
748 }
749
750 CHECK_EQ(info_.Depth(), 1);
751 FinishLazyTypes();
752 info_.EndTag(); // DW_TAG_compile_unit.
753 std::vector<uint8_t> buffer;
754 buffer.reserve(info_.data()->size() + KB);
755 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
756 const size_t debug_abbrev_offset =
757 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
758 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
759 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
760 }
761
David Srbecky9b3607d2016-01-14 18:15:54 +0000762 // Linkage name uniquely identifies type.
763 // It is used to determine the dynamic type of objects.
764 // We use the methods_ field of class since it is unique and it is not moved by the GC.
765 void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) {
766 auto* methods_ptr = type->GetMethodsPtr();
767 if (methods_ptr == nullptr) {
768 // Some types might have no methods. Allocate empty array instead.
769 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
770 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
771 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
772 type->SetMethodsPtr(methods_ptr, 0, 0);
773 DCHECK(type->GetMethodsPtr() != nullptr);
774 }
775 char name[32];
776 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
777 info_.WriteString(DW_AT_linkage_name, name);
778 }
779
David Srbecky0fd295f2015-11-16 16:39:10 +0000780 // Write table into .debug_loc which describes location of dex register.
781 // The dex register might be valid only at some points and it might
782 // move between machine registers and stack.
David Srbecky996ed0b2015-11-27 10:27:11 +0000783 void WriteRegLocation(const MethodDebugInfo* method_info,
784 uint16_t vreg,
785 bool is64bitValue,
786 uint32_t compilation_unit_low_pc,
787 uint32_t dex_pc_low = 0,
788 uint32_t dex_pc_high = 0xFFFFFFFF) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000789 using Kind = DexRegisterLocation::Kind;
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000790 if (!IsFromOptimizingCompiler(method_info)) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000791 return;
792 }
793
David Srbecky996ed0b2015-11-27 10:27:11 +0000794 Writer<> debug_loc(&owner_->debug_loc_);
795 Writer<> debug_ranges(&owner_->debug_ranges_);
796 info_.WriteSecOffset(DW_AT_location, debug_loc.size());
797 info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size());
David Srbecky0fd295f2015-11-16 16:39:10 +0000798
David Srbecky996ed0b2015-11-27 10:27:11 +0000799 std::vector<VariableLocation> variable_locations = GetVariableLocations(
800 method_info,
801 vreg,
802 is64bitValue,
803 dex_pc_low,
804 dex_pc_high);
805
806 // Write .debug_loc entries.
David Srbecky0fd295f2015-11-16 16:39:10 +0000807 const InstructionSet isa = owner_->builder_->GetIsa();
808 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky91cb54e2016-01-15 13:47:59 +0000809 std::vector<uint8_t> expr_buffer;
David Srbecky996ed0b2015-11-27 10:27:11 +0000810 for (const VariableLocation& variable_location : variable_locations) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000811 // Translate dex register location to DWARF expression.
812 // Note that 64-bit value might be split to two distinct locations.
813 // (for example, two 32-bit machine registers, or even stack and register)
David Srbecky91cb54e2016-01-15 13:47:59 +0000814 Expression expr(&expr_buffer);
David Srbecky996ed0b2015-11-27 10:27:11 +0000815 DexRegisterLocation reg_lo = variable_location.reg_lo;
816 DexRegisterLocation reg_hi = variable_location.reg_hi;
David Srbecky0fd295f2015-11-16 16:39:10 +0000817 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
818 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
819 const Kind kind = reg_loc.GetKind();
820 const int32_t value = reg_loc.GetValue();
821 if (kind == Kind::kInStack) {
822 const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
David Srbecky0fd295f2015-11-16 16:39:10 +0000823 // The stack offset is relative to SP. Make it relative to CFA.
David Srbecky91cb54e2016-01-15 13:47:59 +0000824 expr.WriteOpFbreg(value - frame_size);
David Srbecky0fd295f2015-11-16 16:39:10 +0000825 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
826 reg_hi.GetValue() == value + 4) {
827 break; // the high word is correctly implied by the low word.
828 }
829 } else if (kind == Kind::kInRegister) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000830 expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000831 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
832 reg_hi.GetValue() == value) {
833 break; // the high word is correctly implied by the low word.
834 }
835 } else if (kind == Kind::kInFpuRegister) {
836 if ((isa == kArm || isa == kThumb2) &&
837 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
838 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
839 // Translate S register pair to D register (e.g. S4+S5 to D2).
David Srbecky91cb54e2016-01-15 13:47:59 +0000840 expr.WriteOpReg(Reg::ArmDp(value / 2).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000841 break;
842 }
David Srbecky3dd7e5a2015-11-27 13:31:16 +0000843 if (isa == kMips || isa == kMips64) {
844 // TODO: Find what the DWARF floating point register numbers are on MIPS.
845 break;
846 }
David Srbecky91cb54e2016-01-15 13:47:59 +0000847 expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000848 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
849 reg_hi.GetValue() == reg_lo.GetValue()) {
850 break; // the high word is correctly implied by the low word.
851 }
852 } else if (kind == Kind::kConstant) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000853 expr.WriteOpConsts(value);
854 expr.WriteOpStackValue();
David Srbecky0fd295f2015-11-16 16:39:10 +0000855 } else if (kind == Kind::kNone) {
856 break;
857 } else {
858 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
859 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
860 // the special cases above and they should not occur alone.
861 LOG(ERROR) << "Unexpected register location kind: "
862 << DexRegisterLocation::PrettyDescriptor(kind);
863 break;
864 }
865 if (is64bitValue) {
866 // Write the marker which is needed by split 64-bit values.
867 // This code is skipped by the special cases.
David Srbecky91cb54e2016-01-15 13:47:59 +0000868 expr.WriteOpPiece(4);
David Srbecky0fd295f2015-11-16 16:39:10 +0000869 }
870 }
871
David Srbecky91cb54e2016-01-15 13:47:59 +0000872 if (expr.size() > 0) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000873 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000874 debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
875 debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000876 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000877 debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
878 debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000879 }
880 // Write the expression.
David Srbecky91cb54e2016-01-15 13:47:59 +0000881 debug_loc.PushUint16(expr.size());
882 debug_loc.PushData(expr.data());
David Srbecky0fd295f2015-11-16 16:39:10 +0000883 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000884 // Do not generate .debug_loc if the location is not known.
David Srbecky0fd295f2015-11-16 16:39:10 +0000885 }
886 }
887 // Write end-of-list entry.
888 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000889 debug_loc.PushUint64(0);
890 debug_loc.PushUint64(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000891 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000892 debug_loc.PushUint32(0);
893 debug_loc.PushUint32(0);
894 }
895
896 // Write .debug_ranges entries.
897 // This includes ranges where the variable is in scope but the location is not known.
898 for (size_t i = 0; i < variable_locations.size(); i++) {
899 uint32_t low_pc = variable_locations[i].low_pc;
900 uint32_t high_pc = variable_locations[i].high_pc;
901 while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
902 // Merge address range with the next entry.
903 high_pc = variable_locations[++i].high_pc;
904 }
905 if (is64bit) {
906 debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
907 debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
908 } else {
909 debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
910 debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
911 }
912 }
913 // Write end-of-list entry.
914 if (is64bit) {
915 debug_ranges.PushUint64(0);
916 debug_ranges.PushUint64(0);
917 } else {
918 debug_ranges.PushUint32(0);
919 debug_ranges.PushUint32(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000920 }
921 }
922
David Srbecky04b05262015-11-09 18:05:48 +0000923 // Some types are difficult to define as we go since they need
924 // to be enclosed in the right set of namespaces. Therefore we
925 // just define all types lazily at the end of compilation unit.
926 void WriteLazyType(const char* type_descriptor) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000927 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000928 lazy_types_.emplace(std::string(type_descriptor), info_.size());
David Srbecky04b05262015-11-09 18:05:48 +0000929 info_.WriteRef4(DW_AT_type, 0);
930 }
931 }
932
933 void FinishLazyTypes() {
934 for (const auto& lazy_type : lazy_types_) {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000935 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
David Srbecky04b05262015-11-09 18:05:48 +0000936 }
937 lazy_types_.clear();
938 }
939
940 private:
941 void WriteName(const char* name) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000942 if (name != nullptr) {
943 info_.WriteStrp(DW_AT_name, owner_->WriteString(name));
944 }
David Srbecky04b05262015-11-09 18:05:48 +0000945 }
946
947 // Convert dex type descriptor to DWARF.
948 // Returns offset in the compilation unit.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000949 size_t WriteTypeDeclaration(const std::string& desc) {
950 DCHECK(!desc.empty());
David Srbecky04b05262015-11-09 18:05:48 +0000951 const auto& it = type_cache_.find(desc);
952 if (it != type_cache_.end()) {
953 return it->second;
954 }
955
956 size_t offset;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000957 if (desc[0] == 'L') {
David Srbecky04b05262015-11-09 18:05:48 +0000958 // Class type. For example: Lpackage/name;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000959 size_t class_offset = StartClassTag(desc.c_str());
David Srbecky04b05262015-11-09 18:05:48 +0000960 info_.WriteFlag(DW_AT_declaration, true);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000961 EndClassTag(desc.c_str());
962 // Reference to the class type.
963 offset = info_.StartTag(DW_TAG_reference_type);
964 info_.WriteRef(DW_AT_type, class_offset);
965 info_.EndTag();
966 } else if (desc[0] == '[') {
David Srbecky04b05262015-11-09 18:05:48 +0000967 // Array type.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000968 size_t element_type = WriteTypeDeclaration(desc.substr(1));
969 size_t array_type = info_.StartTag(DW_TAG_array_type);
970 info_.WriteFlag(DW_AT_declaration, true);
David Srbecky04b05262015-11-09 18:05:48 +0000971 info_.WriteRef(DW_AT_type, element_type);
972 info_.EndTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000973 offset = info_.StartTag(DW_TAG_reference_type);
974 info_.WriteRef4(DW_AT_type, array_type);
975 info_.EndTag();
David Srbecky04b05262015-11-09 18:05:48 +0000976 } else {
977 // Primitive types.
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000978 DCHECK_EQ(desc.size(), 1u);
979
David Srbecky04b05262015-11-09 18:05:48 +0000980 const char* name;
David Srbecky0fd295f2015-11-16 16:39:10 +0000981 uint32_t encoding;
982 uint32_t byte_size;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000983 switch (desc[0]) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000984 case 'B':
985 name = "byte";
986 encoding = DW_ATE_signed;
987 byte_size = 1;
988 break;
989 case 'C':
990 name = "char";
991 encoding = DW_ATE_UTF;
992 byte_size = 2;
993 break;
994 case 'D':
995 name = "double";
996 encoding = DW_ATE_float;
997 byte_size = 8;
998 break;
999 case 'F':
1000 name = "float";
1001 encoding = DW_ATE_float;
1002 byte_size = 4;
1003 break;
1004 case 'I':
1005 name = "int";
1006 encoding = DW_ATE_signed;
1007 byte_size = 4;
1008 break;
1009 case 'J':
1010 name = "long";
1011 encoding = DW_ATE_signed;
1012 byte_size = 8;
1013 break;
1014 case 'S':
1015 name = "short";
1016 encoding = DW_ATE_signed;
1017 byte_size = 2;
1018 break;
1019 case 'Z':
1020 name = "boolean";
1021 encoding = DW_ATE_boolean;
1022 byte_size = 1;
1023 break;
1024 case 'V':
1025 LOG(FATAL) << "Void type should not be encoded";
1026 UNREACHABLE();
David Srbecky04b05262015-11-09 18:05:48 +00001027 default:
Tamas Berghammer86e42782016-01-05 14:29:02 +00001028 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
David Srbecky04b05262015-11-09 18:05:48 +00001029 UNREACHABLE();
1030 }
1031 offset = info_.StartTag(DW_TAG_base_type);
1032 WriteName(name);
David Srbecky0fd295f2015-11-16 16:39:10 +00001033 info_.WriteData1(DW_AT_encoding, encoding);
1034 info_.WriteData1(DW_AT_byte_size, byte_size);
David Srbecky04b05262015-11-09 18:05:48 +00001035 info_.EndTag();
1036 }
1037
1038 type_cache_.emplace(desc, offset);
1039 return offset;
1040 }
1041
1042 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
1043 // Returns offset of the class tag in the compilation unit.
1044 size_t StartClassTag(const char* desc) {
1045 DCHECK(desc != nullptr && desc[0] == 'L');
1046 // Enclose the type in namespace tags.
1047 const char* end;
1048 for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
1049 info_.StartTag(DW_TAG_namespace);
1050 WriteName(std::string(desc, end - desc).c_str());
1051 }
1052 // Start the class tag.
1053 size_t offset = info_.StartTag(DW_TAG_class_type);
1054 end = strchr(desc, ';');
1055 CHECK(end != nullptr);
1056 WriteName(std::string(desc, end - desc).c_str());
1057 return offset;
1058 }
1059
1060 void EndClassTag(const char* desc) {
1061 DCHECK(desc != nullptr && desc[0] == 'L');
1062 // End the class tag.
1063 info_.EndTag();
1064 // Close namespace tags.
1065 const char* end;
1066 for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
1067 info_.EndTag();
1068 }
1069 }
1070
1071 // For access to the ELF sections.
1072 DebugInfoWriter<ElfTypes>* owner_;
1073 // Debug abbrevs for this compilation unit only.
1074 std::vector<uint8_t> debug_abbrev_;
1075 // Temporary buffer to create and store the entries.
1076 DebugInfoEntryWriter<> info_;
1077 // Cache of already translated type descriptors.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001078 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
David Srbecky04b05262015-11-09 18:05:48 +00001079 // 32-bit references which need to be resolved to a type later.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001080 // Given type may be used multiple times. Therefore we need a multimap.
1081 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
David Srbecky04b05262015-11-09 18:05:48 +00001082 };
1083
David Srbeckyb851b492015-11-11 20:19:38 +00001084 public:
1085 explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbecky626a1662015-04-12 13:12:26 +01001086 }
1087
David Srbeckyb851b492015-11-11 20:19:38 +00001088 void Start() {
1089 builder_->GetDebugInfo()->Start();
David Srbecky799b8c42015-04-14 01:57:43 +01001090 }
1091
David Srbecky04b05262015-11-09 18:05:48 +00001092 void WriteCompilationUnit(const CompilationUnit& compilation_unit) {
1093 CompilationUnitWriter writer(this);
1094 writer.Write(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001095 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001096
Tamas Berghammer86e42782016-01-05 14:29:02 +00001097 void WriteTypes(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
1098 CompilationUnitWriter writer(this);
1099 writer.Write(types);
1100 }
1101
David Srbeckyb851b492015-11-11 20:19:38 +00001102 void End() {
1103 builder_->GetDebugInfo()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001104 builder_->WritePatches(".debug_info.oat_patches",
1105 ArrayRef<const uintptr_t>(debug_info_patches_));
David Srbecky04b05262015-11-09 18:05:48 +00001106 builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data());
1107 builder_->WriteSection(".debug_str", &debug_str_.Data());
David Srbecky0fd295f2015-11-16 16:39:10 +00001108 builder_->WriteSection(".debug_loc", &debug_loc_);
David Srbecky996ed0b2015-11-27 10:27:11 +00001109 builder_->WriteSection(".debug_ranges", &debug_ranges_);
David Srbeckyb851b492015-11-11 20:19:38 +00001110 }
1111
1112 private:
David Srbecky04b05262015-11-09 18:05:48 +00001113 size_t WriteString(const char* str) {
1114 return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1);
1115 }
1116
David Srbeckyb851b492015-11-11 20:19:38 +00001117 ElfBuilder<ElfTypes>* builder_;
1118 std::vector<uintptr_t> debug_info_patches_;
David Srbecky04b05262015-11-09 18:05:48 +00001119 DedupVector debug_abbrev_;
1120 DedupVector debug_str_;
David Srbecky0fd295f2015-11-16 16:39:10 +00001121 std::vector<uint8_t> debug_loc_;
David Srbecky996ed0b2015-11-27 10:27:11 +00001122 std::vector<uint8_t> debug_ranges_;
David Srbecky04b05262015-11-09 18:05:48 +00001123
1124 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
David Srbeckyb851b492015-11-11 20:19:38 +00001125};
1126
1127template<typename ElfTypes>
1128class DebugLineWriter {
1129 typedef typename ElfTypes::Addr Elf_Addr;
1130
1131 public:
1132 explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
1133 }
1134
1135 void Start() {
1136 builder_->GetDebugLine()->Start();
1137 }
1138
1139 // Write line table for given set of methods.
1140 // Returns the number of bytes written.
David Srbecky04b05262015-11-09 18:05:48 +00001141 size_t WriteCompilationUnit(CompilationUnit& compilation_unit) {
David Srbeckyb851b492015-11-11 20:19:38 +00001142 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +00001143 const Elf_Addr text_address = builder_->GetText()->Exists()
1144 ? builder_->GetText()->GetAddress()
1145 : 0;
David Srbecky04b05262015-11-09 18:05:48 +00001146
1147 compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize();
David Srbeckyb851b492015-11-11 20:19:38 +00001148
David Srbecky799b8c42015-04-14 01:57:43 +01001149 std::vector<FileEntry> files;
1150 std::unordered_map<std::string, size_t> files_map;
1151 std::vector<std::string> directories;
1152 std::unordered_map<std::string, size_t> directories_map;
1153 int code_factor_bits_ = 0;
1154 int dwarf_isa = -1;
David Srbeckyb851b492015-11-11 20:19:38 +00001155 switch (builder_->GetIsa()) {
David Srbecky799b8c42015-04-14 01:57:43 +01001156 case kArm: // arm actually means thumb2.
1157 case kThumb2:
1158 code_factor_bits_ = 1; // 16-bit instuctions
1159 dwarf_isa = 1; // DW_ISA_ARM_thumb.
1160 break;
1161 case kArm64:
1162 case kMips:
1163 case kMips64:
1164 code_factor_bits_ = 2; // 32-bit instructions
1165 break;
1166 case kNone:
1167 case kX86:
1168 case kX86_64:
1169 break;
1170 }
David Srbecky297ed222015-04-15 01:18:12 +01001171 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
Vladimir Marko10c13562015-11-25 14:33:36 +00001172 for (const MethodDebugInfo* mi : compilation_unit.methods_) {
David Srbecky04b05262015-11-09 18:05:48 +00001173 // Ignore function if we have already generated line table for the same address.
1174 // It would confuse the debugger and the DWARF specification forbids it.
1175 if (mi->deduped_) {
1176 continue;
1177 }
1178
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001179 ArrayRef<const SrcMapElem> src_mapping_table;
1180 std::vector<SrcMapElem> src_mapping_table_from_stack_maps;
1181 if (IsFromOptimizingCompiler(mi)) {
1182 // Use stack maps to create mapping table from pc to dex.
1183 const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data());
1184 const StackMapEncoding encoding = code_info.ExtractEncoding();
1185 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
1186 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
1187 DCHECK(stack_map.IsValid());
David Srbecky4f50ee22016-01-25 17:00:24 +00001188 // Emit only locations where we have local-variable information.
1189 // In particular, skip mappings inside the prologue.
1190 if (stack_map.HasDexRegisterMap(encoding)) {
1191 const uint32_t pc = stack_map.GetNativePcOffset(encoding);
1192 const int32_t dex = stack_map.GetDexPc(encoding);
1193 src_mapping_table_from_stack_maps.push_back({pc, dex});
1194 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001195 }
1196 std::sort(src_mapping_table_from_stack_maps.begin(),
1197 src_mapping_table_from_stack_maps.end());
1198 src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps);
1199 } else {
1200 // Use the mapping table provided by the quick compiler.
1201 src_mapping_table = mi->compiled_method_->GetSrcMappingTable();
1202 }
1203
1204 if (src_mapping_table.empty()) {
1205 continue;
1206 }
1207
David Srbecky6d8c8f02015-10-26 10:57:09 +00001208 Elf_Addr method_address = text_address + mi->low_pc_;
1209
David Srbeckyb06e28e2015-12-10 13:15:00 +00001210 PositionInfos position_infos;
David Srbecky799b8c42015-04-14 01:57:43 +01001211 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001212 if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) {
1213 continue;
David Srbecky3b9d57a2015-04-10 00:22:14 +01001214 }
1215
David Srbeckyb06e28e2015-12-10 13:15:00 +00001216 if (position_infos.empty()) {
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001217 continue;
1218 }
1219
1220 opcodes.SetAddress(method_address);
1221 if (dwarf_isa != -1) {
1222 opcodes.SetISA(dwarf_isa);
1223 }
1224
David Srbecky799b8c42015-04-14 01:57:43 +01001225 // Get and deduplicate directory and filename.
1226 int file_index = 0; // 0 - primary source file of the compilation.
1227 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
1228 const char* source_file = dex->GetSourceFile(dex_class_def);
1229 if (source_file != nullptr) {
1230 std::string file_name(source_file);
1231 size_t file_name_slash = file_name.find_last_of('/');
1232 std::string class_name(dex->GetClassDescriptor(dex_class_def));
1233 size_t class_name_slash = class_name.find_last_of('/');
1234 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001235
David Srbecky799b8c42015-04-14 01:57:43 +01001236 // Guess directory from package name.
1237 int directory_index = 0; // 0 - current directory of the compilation.
1238 if (file_name_slash == std::string::npos && // Just filename.
1239 class_name.front() == 'L' && // Type descriptor for a class.
1240 class_name_slash != std::string::npos) { // Has package name.
1241 std::string package_name = class_name.substr(1, class_name_slash - 1);
1242 auto it = directories_map.find(package_name);
1243 if (it == directories_map.end()) {
1244 directory_index = 1 + directories.size();
1245 directories_map.emplace(package_name, directory_index);
1246 directories.push_back(package_name);
1247 } else {
1248 directory_index = it->second;
1249 }
1250 full_path = package_name + "/" + file_name;
1251 }
1252
1253 // Add file entry.
1254 auto it2 = files_map.find(full_path);
1255 if (it2 == files_map.end()) {
1256 file_index = 1 + files.size();
1257 files_map.emplace(full_path, file_index);
1258 files.push_back(FileEntry {
1259 file_name,
1260 directory_index,
1261 0, // Modification time - NA.
1262 0, // File size - NA.
1263 });
1264 } else {
1265 file_index = it2->second;
1266 }
1267 }
1268 opcodes.SetFile(file_index);
1269
1270 // Generate mapping opcodes from PC to Java lines.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001271 if (file_index != 0) {
David Srbecky799b8c42015-04-14 01:57:43 +01001272 bool first = true;
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001273 for (SrcMapElem pc2dex : src_mapping_table) {
David Srbecky799b8c42015-04-14 01:57:43 +01001274 uint32_t pc = pc2dex.from_;
1275 int dex_pc = pc2dex.to_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001276 // Find mapping with address with is greater than our dex pc; then go back one step.
1277 auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc,
1278 [](uint32_t address, const DexFile::PositionInfo& entry) {
1279 return address < entry.address_;
1280 });
1281 if (ub != position_infos.begin()) {
1282 int line = (--ub)->line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001283 if (first) {
1284 first = false;
1285 if (pc > 0) {
1286 // Assume that any preceding code is prologue.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001287 int first_line = position_infos.front().line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001288 // Prologue is not a sensible place for a breakpoint.
1289 opcodes.NegateStmt();
David Srbecky6d8c8f02015-10-26 10:57:09 +00001290 opcodes.AddRow(method_address, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +01001291 opcodes.NegateStmt();
1292 opcodes.SetPrologueEnd();
1293 }
David Srbecky6d8c8f02015-10-26 10:57:09 +00001294 opcodes.AddRow(method_address + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +01001295 } else if (line != opcodes.CurrentLine()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +00001296 opcodes.AddRow(method_address + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001297 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001298 }
1299 }
David Srbecky799b8c42015-04-14 01:57:43 +01001300 } else {
1301 // line 0 - instruction cannot be attributed to any source line.
David Srbecky6d8c8f02015-10-26 10:57:09 +00001302 opcodes.AddRow(method_address, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001303 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001304
1305 opcodes.AdvancePC(text_address + mi->high_pc_);
1306 opcodes.EndSequence();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001307 }
David Srbeckyb851b492015-11-11 20:19:38 +00001308 std::vector<uint8_t> buffer;
1309 buffer.reserve(opcodes.data()->size() + KB);
1310 size_t offset = builder_->GetDebugLine()->GetSize();
1311 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
1312 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
1313 return buffer.size();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001314 }
David Srbeckyb851b492015-11-11 20:19:38 +00001315
1316 void End() {
1317 builder_->GetDebugLine()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001318 builder_->WritePatches(".debug_line.oat_patches",
1319 ArrayRef<const uintptr_t>(debug_line_patches));
David Srbeckyb851b492015-11-11 20:19:38 +00001320 }
1321
1322 private:
1323 ElfBuilder<ElfTypes>* builder_;
1324 std::vector<uintptr_t> debug_line_patches;
1325};
1326
1327template<typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001328static void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
Tamas Berghammer86e42782016-01-05 14:29:02 +00001329 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyb851b492015-11-11 20:19:38 +00001330 // Group the methods into compilation units based on source file.
1331 std::vector<CompilationUnit> compilation_units;
1332 const char* last_source_file = nullptr;
Vladimir Marko10c13562015-11-25 14:33:36 +00001333 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky04b05262015-11-09 18:05:48 +00001334 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
1335 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
1336 if (compilation_units.empty() || source_file != last_source_file) {
1337 compilation_units.push_back(CompilationUnit());
David Srbeckyb851b492015-11-11 20:19:38 +00001338 }
David Srbecky04b05262015-11-09 18:05:48 +00001339 CompilationUnit& cu = compilation_units.back();
1340 cu.methods_.push_back(&mi);
1341 cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_);
1342 cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_);
1343 last_source_file = source_file;
David Srbeckyb851b492015-11-11 20:19:38 +00001344 }
1345
1346 // Write .debug_line section.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001347 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001348 DebugLineWriter<ElfTypes> line_writer(builder);
1349 line_writer.Start();
David Srbeckyb851b492015-11-11 20:19:38 +00001350 for (auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001351 line_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001352 }
1353 line_writer.End();
1354 }
1355
1356 // Write .debug_info section.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001357 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001358 DebugInfoWriter<ElfTypes> info_writer(builder);
1359 info_writer.Start();
1360 for (const auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001361 info_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001362 }
1363 info_writer.End();
1364 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001365}
1366
David Srbeckye0febdf2015-12-17 20:53:07 +00001367template <typename ElfTypes>
David Srbecky52886112016-01-22 13:56:47 +00001368static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
1369 const ArrayRef<const MethodDebugInfo>& method_infos,
1370 bool with_signature) {
David Srbeckye0febdf2015-12-17 20:53:07 +00001371 bool generated_mapping_symbol = false;
1372 auto* strtab = builder->GetStrTab();
1373 auto* symtab = builder->GetSymTab();
1374
1375 if (method_infos.empty()) {
1376 return;
1377 }
1378
1379 // Find all addresses (low_pc) which contain deduped methods.
1380 // The first instance of method is not marked deduped_, but the rest is.
1381 std::unordered_set<uint32_t> deduped_addresses;
1382 for (const MethodDebugInfo& info : method_infos) {
1383 if (info.deduped_) {
1384 deduped_addresses.insert(info.low_pc_);
1385 }
1386 }
1387
1388 strtab->Start();
1389 strtab->Write(""); // strtab should start with empty string.
David Srbecky52886112016-01-22 13:56:47 +00001390 std::string last_name;
1391 size_t last_name_offset = 0;
David Srbeckye0febdf2015-12-17 20:53:07 +00001392 for (const MethodDebugInfo& info : method_infos) {
1393 if (info.deduped_) {
1394 continue; // Add symbol only for the first instance.
1395 }
David Srbecky52886112016-01-22 13:56:47 +00001396 std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, with_signature);
David Srbeckye0febdf2015-12-17 20:53:07 +00001397 if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) {
1398 name += " [DEDUPED]";
1399 }
David Srbecky52886112016-01-22 13:56:47 +00001400 // If we write method names without signature, we might see the same name multiple times.
1401 size_t name_offset = (name == last_name ? last_name_offset : strtab->Write(name));
David Srbeckye0febdf2015-12-17 20:53:07 +00001402
David Srbecky5cc349f2015-12-18 15:04:48 +00001403 const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr;
1404 const bool is_relative = (text != nullptr);
David Srbeckye0febdf2015-12-17 20:53:07 +00001405 uint32_t low_pc = info.low_pc_;
1406 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
1407 low_pc += info.compiled_method_->CodeDelta();
David Srbecky52886112016-01-22 13:56:47 +00001408 symtab->Add(name_offset,
1409 text,
1410 low_pc,
1411 is_relative,
1412 info.high_pc_ - info.low_pc_,
1413 STB_GLOBAL,
1414 STT_FUNC);
David Srbeckye0febdf2015-12-17 20:53:07 +00001415
1416 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
1417 // instructions, so that disassembler tools can correctly disassemble.
1418 // Note that even if we generate just a single mapping symbol, ARM's Streamline
1419 // requires it to match function symbol. Just address 0 does not work.
1420 if (info.compiled_method_->GetInstructionSet() == kThumb2) {
1421 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001422 symtab->Add(strtab->Write("$t"), text, info.low_pc_ & ~1,
1423 is_relative, 0, STB_LOCAL, STT_NOTYPE);
David Srbeckye0febdf2015-12-17 20:53:07 +00001424 generated_mapping_symbol = true;
1425 }
1426 }
David Srbecky52886112016-01-22 13:56:47 +00001427
1428 last_name = std::move(name);
1429 last_name_offset = name_offset;
David Srbeckye0febdf2015-12-17 20:53:07 +00001430 }
1431 strtab->End();
1432
1433 // Symbols are buffered and written after names (because they are smaller).
1434 // We could also do two passes in this function to avoid the buffering.
1435 symtab->Start();
1436 symtab->Write();
1437 symtab->End();
1438}
1439
1440template <typename ElfTypes>
1441void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
1442 const ArrayRef<const MethodDebugInfo>& method_infos,
1443 CFIFormat cfi_format) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001444 // Add methods to .symtab.
David Srbecky52886112016-01-22 13:56:47 +00001445 WriteDebugSymbols(builder, method_infos, true /* with_signature */);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001446 // Generate CFI (stack unwinding information).
David Srbecky5e974a62016-01-22 14:25:03 +00001447 WriteCFISection(builder, method_infos, cfi_format, true /* write_oat_patches */);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001448 // Write DWARF .debug_* sections.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001449 WriteDebugSections(builder, method_infos);
David Srbeckye0febdf2015-12-17 20:53:07 +00001450}
1451
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001452static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) {
1453 // Configure the compression library.
1454 CrcGenerateTable();
1455 Crc64GenerateTable();
1456 CLzma2EncProps lzma2Props;
1457 Lzma2EncProps_Init(&lzma2Props);
1458 lzma2Props.lzmaProps.level = 1; // Fast compression.
1459 Lzma2EncProps_Normalize(&lzma2Props);
1460 CXzProps props;
1461 XzProps_Init(&props);
1462 props.lzma2Props = &lzma2Props;
1463 // Implement the required interface for communication (written in C so no virtual methods).
1464 struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress {
1465 static SRes ReadImpl(void* p, void* buf, size_t* size) {
1466 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqInStream*>(p));
1467 *size = std::min(*size, ctx->src_->size() - ctx->src_pos_);
1468 memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size);
1469 ctx->src_pos_ += *size;
1470 return SZ_OK;
1471 }
1472 static size_t WriteImpl(void* p, const void* buf, size_t size) {
1473 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqOutStream*>(p));
1474 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
1475 ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size);
1476 return size;
1477 }
1478 static SRes ProgressImpl(void* , UInt64, UInt64) {
1479 return SZ_OK;
1480 }
1481 size_t src_pos_;
1482 const std::vector<uint8_t>* src_;
1483 std::vector<uint8_t>* dst_;
1484 };
1485 XzCallbacks callbacks;
1486 callbacks.Read = XzCallbacks::ReadImpl;
1487 callbacks.Write = XzCallbacks::WriteImpl;
1488 callbacks.Progress = XzCallbacks::ProgressImpl;
1489 callbacks.src_pos_ = 0;
1490 callbacks.src_ = src;
1491 callbacks.dst_ = dst;
1492 // Compress.
1493 SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks);
1494 CHECK_EQ(res, SZ_OK);
1495}
1496
1497template <typename ElfTypes>
1498void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder,
1499 const ArrayRef<const MethodDebugInfo>& method_infos) {
1500 const InstructionSet isa = parent_builder->GetIsa();
1501 std::vector<uint8_t> buffer;
1502 buffer.reserve(KB);
1503 VectorOutputStream out("Mini-debug-info ELF file", &buffer);
1504 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1505 builder->Start();
1506 // Write .rodata and .text as NOBITS sections.
1507 // This allows tools to detect virtual address relocation of the parent ELF file.
1508 builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress());
1509 builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize());
1510 builder->SetVirtualAddress(parent_builder->GetText()->GetAddress());
1511 builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize());
David Srbecky52886112016-01-22 13:56:47 +00001512 WriteDebugSymbols(builder.get(), method_infos, false /* with_signature */);
David Srbecky5e974a62016-01-22 14:25:03 +00001513 WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT, false /* write_oat_paches */);
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001514 builder->End();
1515 CHECK(builder->Good());
1516 std::vector<uint8_t> compressed_buffer;
1517 compressed_buffer.reserve(buffer.size() / 4);
1518 XzCompress(&buffer, &compressed_buffer);
1519 parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer);
1520}
1521
David Srbecky5cc349f2015-12-18 15:04:48 +00001522template <typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001523static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal(
David Srbecky5cc349f2015-12-18 15:04:48 +00001524 const dwarf::MethodDebugInfo& method_info) {
1525 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1526 std::vector<uint8_t> buffer;
1527 buffer.reserve(KB);
1528 VectorOutputStream out("Debug ELF file", &buffer);
1529 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1530 builder->Start();
1531 WriteDebugInfo(builder.get(),
1532 ArrayRef<const MethodDebugInfo>(&method_info, 1),
1533 DW_DEBUG_FRAME_FORMAT);
1534 builder->End();
1535 CHECK(builder->Good());
1536 // Make a copy of the buffer. We want to shrink it anyway.
1537 uint8_t* result = new uint8_t[buffer.size()];
1538 CHECK(result != nullptr);
1539 memcpy(result, buffer.data(), buffer.size());
1540 return ArrayRef<const uint8_t>(result, buffer.size());
1541}
1542
Tamas Berghammer86e42782016-01-05 14:29:02 +00001543ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001544 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1545 if (Is64BitInstructionSet(isa)) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001546 return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info);
David Srbecky5cc349f2015-12-18 15:04:48 +00001547 } else {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001548 return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info);
1549 }
1550}
1551
1552template <typename ElfTypes>
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001553static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal(
1554 const InstructionSet isa, const ArrayRef<mirror::Class*>& types)
Tamas Berghammer86e42782016-01-05 14:29:02 +00001555 SHARED_REQUIRES(Locks::mutator_lock_) {
1556 std::vector<uint8_t> buffer;
1557 buffer.reserve(KB);
1558 VectorOutputStream out("Debug ELF file", &buffer);
1559 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1560 builder->Start();
1561
1562 DebugInfoWriter<ElfTypes> info_writer(builder.get());
1563 info_writer.Start();
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001564 info_writer.WriteTypes(types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001565 info_writer.End();
1566
1567 builder->End();
1568 CHECK(builder->Good());
1569 // Make a copy of the buffer. We want to shrink it anyway.
1570 uint8_t* result = new uint8_t[buffer.size()];
1571 CHECK(result != nullptr);
1572 memcpy(result, buffer.data(), buffer.size());
1573 return ArrayRef<const uint8_t>(result, buffer.size());
1574}
1575
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001576ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa,
1577 const ArrayRef<mirror::Class*>& types) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001578 if (Is64BitInstructionSet(isa)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001579 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001580 } else {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001581 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types);
David Srbecky5cc349f2015-12-18 15:04:48 +00001582 }
1583}
1584
David Srbecky6d8c8f02015-10-26 10:57:09 +00001585// Explicit instantiations
David Srbeckye0febdf2015-12-17 20:53:07 +00001586template void WriteDebugInfo<ElfTypes32>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001587 ElfBuilder<ElfTypes32>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001588 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001589 CFIFormat cfi_format);
1590template void WriteDebugInfo<ElfTypes64>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001591 ElfBuilder<ElfTypes64>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001592 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001593 CFIFormat cfi_format);
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001594template void WriteMiniDebugInfo<ElfTypes32>(
1595 ElfBuilder<ElfTypes32>* builder,
1596 const ArrayRef<const MethodDebugInfo>& method_infos);
1597template void WriteMiniDebugInfo<ElfTypes64>(
1598 ElfBuilder<ElfTypes64>* builder,
1599 const ArrayRef<const MethodDebugInfo>& method_infos);
David Srbecky6d8c8f02015-10-26 10:57:09 +00001600
David Srbecky3b9d57a2015-04-10 00:22:14 +01001601} // namespace dwarf
1602} // namespace art