blob: e1ab340b2802b758463eef4f731fe5fcdfbdbf49 [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 Srbecky626a1662015-04-12 13:12:26 +010019#include <unordered_set>
20
Andreas Gampee3d623e2015-05-01 16:11:04 -070021#include "base/casts.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010022#include "compiled_method.h"
23#include "driver/compiler_driver.h"
24#include "dex_file-inl.h"
25#include "dwarf/headers.h"
26#include "dwarf/register.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000027#include "elf_builder.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010028#include "oat_writer.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010030
31namespace art {
32namespace dwarf {
33
David Srbecky6d8c8f02015-10-26 10:57:09 +000034static void WriteCIE(InstructionSet isa,
35 CFIFormat format,
36 std::vector<uint8_t>* buffer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +010037 // Scratch registers should be marked as undefined. This tells the
38 // debugger that its value in the previous frame is not recoverable.
39 bool is64bit = Is64BitInstructionSet(isa);
40 switch (isa) {
41 case kArm:
42 case kThumb2: {
43 DebugFrameOpCodeWriter<> opcodes;
44 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
45 // core registers.
46 for (int reg = 0; reg < 13; reg++) {
47 if (reg < 4 || reg == 12) {
48 opcodes.Undefined(Reg::ArmCore(reg));
49 } else {
50 opcodes.SameValue(Reg::ArmCore(reg));
51 }
52 }
53 // fp registers.
54 for (int reg = 0; reg < 32; reg++) {
55 if (reg < 16) {
56 opcodes.Undefined(Reg::ArmFp(reg));
57 } else {
58 opcodes.SameValue(Reg::ArmFp(reg));
59 }
60 }
David Srbecky527c9c72015-04-17 21:14:10 +010061 auto return_reg = Reg::ArmCore(14); // R14(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +000062 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +010063 return;
64 }
65 case kArm64: {
66 DebugFrameOpCodeWriter<> opcodes;
67 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
68 // core registers.
69 for (int reg = 0; reg < 30; reg++) {
70 if (reg < 8 || reg == 16 || reg == 17) {
71 opcodes.Undefined(Reg::Arm64Core(reg));
72 } else {
73 opcodes.SameValue(Reg::Arm64Core(reg));
74 }
75 }
76 // fp registers.
77 for (int reg = 0; reg < 32; reg++) {
78 if (reg < 8 || reg >= 16) {
79 opcodes.Undefined(Reg::Arm64Fp(reg));
80 } else {
81 opcodes.SameValue(Reg::Arm64Fp(reg));
82 }
83 }
David Srbecky527c9c72015-04-17 21:14:10 +010084 auto return_reg = Reg::Arm64Core(30); // R30(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +000085 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +010086 return;
87 }
88 case kMips:
89 case kMips64: {
90 DebugFrameOpCodeWriter<> opcodes;
91 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
92 // core registers.
93 for (int reg = 1; reg < 26; reg++) {
94 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
95 opcodes.Undefined(Reg::MipsCore(reg));
96 } else {
97 opcodes.SameValue(Reg::MipsCore(reg));
98 }
99 }
David Srbecky527c9c72015-04-17 21:14:10 +0100100 auto return_reg = Reg::MipsCore(31); // R31(RA).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000101 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100102 return;
103 }
104 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100105 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
106 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100107 DebugFrameOpCodeWriter<> opcodes;
108 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
109 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
110 // core registers.
111 for (int reg = 0; reg < 8; reg++) {
112 if (reg <= 3) {
113 opcodes.Undefined(Reg::X86Core(reg));
114 } else if (reg == 4) {
115 // Stack pointer.
116 } else {
117 opcodes.SameValue(Reg::X86Core(reg));
118 }
119 }
120 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100121 if (generate_opcodes_for_x86_fp) {
122 for (int reg = 0; reg < 8; reg++) {
123 opcodes.Undefined(Reg::X86Fp(reg));
124 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100125 }
David Srbecky527c9c72015-04-17 21:14:10 +0100126 auto return_reg = Reg::X86Core(8); // R8(EIP).
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 kX86_64: {
131 DebugFrameOpCodeWriter<> opcodes;
132 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
133 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
134 // core registers.
135 for (int reg = 0; reg < 16; reg++) {
136 if (reg == 4) {
137 // Stack pointer.
138 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
139 opcodes.Undefined(Reg::X86_64Core(reg));
140 } else {
141 opcodes.SameValue(Reg::X86_64Core(reg));
142 }
143 }
144 // fp registers.
145 for (int reg = 0; reg < 16; reg++) {
146 if (reg < 12) {
147 opcodes.Undefined(Reg::X86_64Fp(reg));
148 } else {
149 opcodes.SameValue(Reg::X86_64Fp(reg));
150 }
151 }
David Srbecky527c9c72015-04-17 21:14:10 +0100152 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100154 return;
155 }
156 case kNone:
157 break;
158 }
159 LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
160 UNREACHABLE();
161}
162
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163template<typename ElfTypes>
164void WriteCFISection(ElfBuilder<ElfTypes>* builder,
165 const std::vector<OatWriter::DebugInfo>& method_infos,
166 CFIFormat format) {
167 CHECK(format == dwarf::DW_DEBUG_FRAME_FORMAT ||
168 format == dwarf::DW_EH_FRAME_FORMAT);
169 typedef typename ElfTypes::Addr Elf_Addr;
170
171 std::vector<uint32_t> binary_search_table;
172 std::vector<uintptr_t> patch_locations;
173 if (format == DW_EH_FRAME_FORMAT) {
174 binary_search_table.reserve(2 * method_infos.size());
175 } else {
176 patch_locations.reserve(method_infos.size());
177 }
David Srbecky527c9c72015-04-17 21:14:10 +0100178
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100179 // Write .eh_frame/.debug_frame section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000180 auto* cfi_section = (format == dwarf::DW_DEBUG_FRAME_FORMAT
181 ? builder->GetDebugFrame()
182 : builder->GetEhFrame());
183 {
184 cfi_section->Start();
185 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
186 const Elf_Addr text_address = builder->GetText()->GetAddress();
187 const Elf_Addr cfi_address = cfi_section->GetAddress();
188 const Elf_Addr cie_address = cfi_address;
189 Elf_Addr buffer_address = cfi_address;
190 std::vector<uint8_t> buffer; // Small temporary buffer.
191 WriteCIE(builder->GetIsa(), format, &buffer);
192 cfi_section->WriteFully(buffer.data(), buffer.size());
193 buffer_address += buffer.size();
194 buffer.clear();
195 for (const OatWriter::DebugInfo& mi : method_infos) {
196 if (!mi.deduped_) { // Only one FDE per unique address.
197 ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo();
198 if (!opcodes.empty()) {
199 const Elf_Addr code_address = text_address + mi.low_pc_;
200 if (format == DW_EH_FRAME_FORMAT) {
201 binary_search_table.push_back(
202 dchecked_integral_cast<uint32_t>(code_address));
203 binary_search_table.push_back(
204 dchecked_integral_cast<uint32_t>(buffer_address));
205 }
206 WriteFDE(is64bit, cfi_address, cie_address,
207 code_address, mi.high_pc_ - mi.low_pc_,
208 opcodes, format, buffer_address, &buffer,
209 &patch_locations);
210 cfi_section->WriteFully(buffer.data(), buffer.size());
211 buffer_address += buffer.size();
212 buffer.clear();
213 }
David Srbecky6d73c9d2015-05-01 15:00:40 +0100214 }
David Srbecky8dc73242015-04-12 11:40:39 +0100215 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000216 cfi_section->End();
David Srbecky8dc73242015-04-12 11:40:39 +0100217 }
David Srbecky527c9c72015-04-17 21:14:10 +0100218
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100219 if (format == DW_EH_FRAME_FORMAT) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000220 auto* header_section = builder->GetEhFrameHdr();
221 header_section->Start();
222 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100223 // Write .eh_frame_hdr section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000224 std::vector<uint8_t> buffer;
225 Writer<> header(&buffer);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100226 header.PushUint8(1); // Version.
227 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
228 // so we have to use pcrel which means relative to the pointer's location.
229 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
230 // Encoding of binary search table size.
231 header.PushUint8(DW_EH_PE_udata4);
232 // Encoding of binary search table addresses - libunwind supports only this
233 // specific combination, which means relative to the start of .eh_frame_hdr.
234 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000235 // .eh_frame pointer
236 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100237 // Binary search table size (number of entries).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000238 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
239 header_section->WriteFully(buffer.data(), buffer.size());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100240 // Binary search table.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000241 for (size_t i = 0; i < binary_search_table.size(); i++) {
242 // Make addresses section-relative since we know the header address now.
243 binary_search_table[i] -= header_address;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100244 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000245 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
246 header_section->End();
247 } else {
248 builder->WritePatches(".debug_frame.oat_patches", &patch_locations);
David Srbecky033d7452015-04-30 19:57:35 +0100249 }
David Srbecky8dc73242015-04-12 11:40:39 +0100250}
251
David Srbecky6d8c8f02015-10-26 10:57:09 +0000252template<typename ElfTypes>
David Srbeckyb851b492015-11-11 20:19:38 +0000253class DebugInfoWriter {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000254 typedef typename ElfTypes::Addr Elf_Addr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100255
David Srbeckyb851b492015-11-11 20:19:38 +0000256 public:
257 explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbecky626a1662015-04-12 13:12:26 +0100258 }
259
David Srbeckyb851b492015-11-11 20:19:38 +0000260 void Start() {
261 builder_->GetDebugInfo()->Start();
David Srbecky799b8c42015-04-14 01:57:43 +0100262 }
263
David Srbeckyb851b492015-11-11 20:19:38 +0000264 void Write(const std::vector<const OatWriter::DebugInfo*>& method_infos,
265 size_t debug_line_offset) {
266 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
267 const Elf_Addr text_address = builder_->GetText()->GetAddress();
David Srbecky799b8c42015-04-14 01:57:43 +0100268 uint32_t cunit_low_pc = 0xFFFFFFFFU;
269 uint32_t cunit_high_pc = 0;
David Srbeckyb851b492015-11-11 20:19:38 +0000270 for (auto method_info : method_infos) {
David Srbecky799b8c42015-04-14 01:57:43 +0100271 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
272 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100273 }
274
David Srbeckyb851b492015-11-11 20:19:38 +0000275 size_t debug_abbrev_offset = debug_abbrev_.size();
276 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev_);
David Srbecky799b8c42015-04-14 01:57:43 +0100277 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
David Srbeckyb851b492015-11-11 20:19:38 +0000278 info.WriteStrp(DW_AT_producer, "Android dex2oat", &debug_str_);
David Srbecky799b8c42015-04-14 01:57:43 +0100279 info.WriteData1(DW_AT_language, DW_LANG_Java);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000280 info.WriteAddr(DW_AT_low_pc, text_address + cunit_low_pc);
281 info.WriteAddr(DW_AT_high_pc, text_address + cunit_high_pc);
David Srbeckyb851b492015-11-11 20:19:38 +0000282 info.WriteData4(DW_AT_stmt_list, debug_line_offset);
283 for (auto method_info : method_infos) {
David Srbecky799b8c42015-04-14 01:57:43 +0100284 std::string method_name = PrettyMethod(method_info->dex_method_index_,
285 *method_info->dex_file_, true);
David Srbecky799b8c42015-04-14 01:57:43 +0100286 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
David Srbeckyb851b492015-11-11 20:19:38 +0000287 info.WriteStrp(DW_AT_name, method_name.data(), &debug_str_);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000288 info.WriteAddr(DW_AT_low_pc, text_address + method_info->low_pc_);
289 info.WriteAddr(DW_AT_high_pc, text_address + method_info->high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100290 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100291 }
David Srbecky799b8c42015-04-14 01:57:43 +0100292 info.EndTag(); // DW_TAG_compile_unit
David Srbeckyb851b492015-11-11 20:19:38 +0000293 std::vector<uint8_t> buffer;
294 buffer.reserve(info.data()->size() + KB);
295 size_t offset = builder_->GetDebugInfo()->GetSize();
296 WriteDebugInfoCU(debug_abbrev_offset, info, offset, &buffer, &debug_info_patches_);
297 builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
298 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100299
David Srbeckyb851b492015-11-11 20:19:38 +0000300 void End() {
301 builder_->GetDebugInfo()->End();
302 builder_->WritePatches(".debug_info.oat_patches", &debug_info_patches_);
303 builder_->WriteSection(".debug_abbrev", &debug_abbrev_);
304 builder_->WriteSection(".debug_str", &debug_str_);
305 }
306
307 private:
308 ElfBuilder<ElfTypes>* builder_;
309 std::vector<uintptr_t> debug_info_patches_;
310 std::vector<uint8_t> debug_abbrev_;
311 std::vector<uint8_t> debug_str_;
312};
313
314template<typename ElfTypes>
315class DebugLineWriter {
316 typedef typename ElfTypes::Addr Elf_Addr;
317
318 public:
319 explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
320 }
321
322 void Start() {
323 builder_->GetDebugLine()->Start();
324 }
325
326 // Write line table for given set of methods.
327 // Returns the number of bytes written.
328 size_t Write(const std::vector<const OatWriter::DebugInfo*>& method_infos) {
329 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
330 const Elf_Addr text_address = builder_->GetText()->GetAddress();
331 uint32_t cunit_low_pc = 0xFFFFFFFFU;
332 uint32_t cunit_high_pc = 0;
333 for (auto method_info : method_infos) {
334 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
335 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
336 }
337
David Srbecky799b8c42015-04-14 01:57:43 +0100338 std::vector<FileEntry> files;
339 std::unordered_map<std::string, size_t> files_map;
340 std::vector<std::string> directories;
341 std::unordered_map<std::string, size_t> directories_map;
342 int code_factor_bits_ = 0;
343 int dwarf_isa = -1;
David Srbeckyb851b492015-11-11 20:19:38 +0000344 switch (builder_->GetIsa()) {
David Srbecky799b8c42015-04-14 01:57:43 +0100345 case kArm: // arm actually means thumb2.
346 case kThumb2:
347 code_factor_bits_ = 1; // 16-bit instuctions
348 dwarf_isa = 1; // DW_ISA_ARM_thumb.
349 break;
350 case kArm64:
351 case kMips:
352 case kMips64:
353 code_factor_bits_ = 2; // 32-bit instructions
354 break;
355 case kNone:
356 case kX86:
357 case kX86_64:
358 break;
359 }
David Srbecky297ed222015-04-15 01:18:12 +0100360 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000361 opcodes.SetAddress(text_address + cunit_low_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100362 if (dwarf_isa != -1) {
363 opcodes.SetISA(dwarf_isa);
364 }
David Srbeckyb851b492015-11-11 20:19:38 +0000365 for (const OatWriter::DebugInfo* mi : method_infos) {
David Srbecky799b8c42015-04-14 01:57:43 +0100366 struct DebugInfoCallbacks {
367 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
368 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
369 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
370 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100371 }
David Srbecky799b8c42015-04-14 01:57:43 +0100372 DefaultSrcMap dex2line_;
373 } debug_info_callbacks;
374
David Srbecky6d8c8f02015-10-26 10:57:09 +0000375 Elf_Addr method_address = text_address + mi->low_pc_;
376
David Srbecky799b8c42015-04-14 01:57:43 +0100377 const DexFile* dex = mi->dex_file_;
378 if (mi->code_item_ != nullptr) {
379 dex->DecodeDebugInfo(mi->code_item_,
380 (mi->access_flags_ & kAccStatic) != 0,
381 mi->dex_method_index_,
382 DebugInfoCallbacks::NewPosition,
383 nullptr,
384 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100385 }
386
David Srbecky799b8c42015-04-14 01:57:43 +0100387 // Get and deduplicate directory and filename.
388 int file_index = 0; // 0 - primary source file of the compilation.
389 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
390 const char* source_file = dex->GetSourceFile(dex_class_def);
391 if (source_file != nullptr) {
392 std::string file_name(source_file);
393 size_t file_name_slash = file_name.find_last_of('/');
394 std::string class_name(dex->GetClassDescriptor(dex_class_def));
395 size_t class_name_slash = class_name.find_last_of('/');
396 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100397
David Srbecky799b8c42015-04-14 01:57:43 +0100398 // Guess directory from package name.
399 int directory_index = 0; // 0 - current directory of the compilation.
400 if (file_name_slash == std::string::npos && // Just filename.
401 class_name.front() == 'L' && // Type descriptor for a class.
402 class_name_slash != std::string::npos) { // Has package name.
403 std::string package_name = class_name.substr(1, class_name_slash - 1);
404 auto it = directories_map.find(package_name);
405 if (it == directories_map.end()) {
406 directory_index = 1 + directories.size();
407 directories_map.emplace(package_name, directory_index);
408 directories.push_back(package_name);
409 } else {
410 directory_index = it->second;
411 }
412 full_path = package_name + "/" + file_name;
413 }
414
415 // Add file entry.
416 auto it2 = files_map.find(full_path);
417 if (it2 == files_map.end()) {
418 file_index = 1 + files.size();
419 files_map.emplace(full_path, file_index);
420 files.push_back(FileEntry {
421 file_name,
422 directory_index,
423 0, // Modification time - NA.
424 0, // File size - NA.
425 });
426 } else {
427 file_index = it2->second;
428 }
429 }
430 opcodes.SetFile(file_index);
431
432 // Generate mapping opcodes from PC to Java lines.
433 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100434 if (file_index != 0 && !dex2line_map.empty()) {
435 bool first = true;
436 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
437 uint32_t pc = pc2dex.from_;
438 int dex_pc = pc2dex.to_;
439 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
440 if (dex2line.first) {
441 int line = dex2line.second;
442 if (first) {
443 first = false;
444 if (pc > 0) {
445 // Assume that any preceding code is prologue.
446 int first_line = dex2line_map.front().to_;
447 // Prologue is not a sensible place for a breakpoint.
448 opcodes.NegateStmt();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000449 opcodes.AddRow(method_address, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100450 opcodes.NegateStmt();
451 opcodes.SetPrologueEnd();
452 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000453 opcodes.AddRow(method_address + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100454 } else if (line != opcodes.CurrentLine()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000455 opcodes.AddRow(method_address + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100456 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100457 }
458 }
David Srbecky799b8c42015-04-14 01:57:43 +0100459 } else {
460 // line 0 - instruction cannot be attributed to any source line.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000461 opcodes.AddRow(method_address, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100462 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100463 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000464 opcodes.AdvancePC(text_address + cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100465 opcodes.EndSequence();
David Srbeckyb851b492015-11-11 20:19:38 +0000466 std::vector<uint8_t> buffer;
467 buffer.reserve(opcodes.data()->size() + KB);
468 size_t offset = builder_->GetDebugLine()->GetSize();
469 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
470 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
471 return buffer.size();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100472 }
David Srbeckyb851b492015-11-11 20:19:38 +0000473
474 void End() {
475 builder_->GetDebugLine()->End();
476 builder_->WritePatches(".debug_line.oat_patches", &debug_line_patches);
477 }
478
479 private:
480 ElfBuilder<ElfTypes>* builder_;
481 std::vector<uintptr_t> debug_line_patches;
482};
483
484template<typename ElfTypes>
485void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
486 const std::vector<OatWriter::DebugInfo>& method_infos) {
487 struct CompilationUnit {
488 std::vector<const OatWriter::DebugInfo*> methods_;
489 size_t debug_line_offset_ = 0;
490 };
491
492 // Group the methods into compilation units based on source file.
493 std::vector<CompilationUnit> compilation_units;
494 const char* last_source_file = nullptr;
495 for (const OatWriter::DebugInfo& mi : method_infos) {
496 // Attribute given instruction range only to single method.
497 // Otherwise the debugger might get really confused.
498 if (!mi.deduped_) {
499 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
500 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
501 if (compilation_units.empty() || source_file != last_source_file) {
502 compilation_units.push_back(CompilationUnit());
503 }
504 compilation_units.back().methods_.push_back(&mi);
505 last_source_file = source_file;
506 }
507 }
508
509 // Write .debug_line section.
510 {
511 DebugLineWriter<ElfTypes> line_writer(builder);
512 line_writer.Start();
513 size_t offset = 0;
514 for (auto& compilation_unit : compilation_units) {
515 compilation_unit.debug_line_offset_ = offset;
516 offset += line_writer.Write(compilation_unit.methods_);
517 }
518 line_writer.End();
519 }
520
521 // Write .debug_info section.
522 {
523 DebugInfoWriter<ElfTypes> info_writer(builder);
524 info_writer.Start();
525 for (const auto& compilation_unit : compilation_units) {
526 info_writer.Write(compilation_unit.methods_, compilation_unit.debug_line_offset_);
527 }
528 info_writer.End();
529 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100530}
531
David Srbecky6d8c8f02015-10-26 10:57:09 +0000532// Explicit instantiations
533template void WriteCFISection<ElfTypes32>(
534 ElfBuilder<ElfTypes32>* builder,
535 const std::vector<OatWriter::DebugInfo>& method_infos,
536 CFIFormat format);
537template void WriteCFISection<ElfTypes64>(
538 ElfBuilder<ElfTypes64>* builder,
539 const std::vector<OatWriter::DebugInfo>& method_infos,
540 CFIFormat format);
541template void WriteDebugSections<ElfTypes32>(
542 ElfBuilder<ElfTypes32>* builder,
543 const std::vector<OatWriter::DebugInfo>& method_infos);
544template void WriteDebugSections<ElfTypes64>(
545 ElfBuilder<ElfTypes64>* builder,
546 const std::vector<OatWriter::DebugInfo>& method_infos);
547
David Srbecky3b9d57a2015-04-10 00:22:14 +0100548} // namespace dwarf
549} // namespace art