blob: cf0adae525c48d5ac3609e647d66c4465a814fd9 [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
David Srbecky3b9d57a2015-04-10 00:22:14 +010021#include "compiled_method.h"
22#include "driver/compiler_driver.h"
23#include "dex_file-inl.h"
24#include "dwarf/headers.h"
25#include "dwarf/register.h"
26#include "oat_writer.h"
27
28namespace art {
29namespace dwarf {
30
David Srbecky527c9c72015-04-17 21:14:10 +010031static void WriteEhFrameCIE(InstructionSet isa,
32 ExceptionHeaderValueApplication addr_type,
33 std::vector<uint8_t>* eh_frame) {
David Srbecky3b9d57a2015-04-10 00:22:14 +010034 // Scratch registers should be marked as undefined. This tells the
35 // debugger that its value in the previous frame is not recoverable.
36 bool is64bit = Is64BitInstructionSet(isa);
37 switch (isa) {
38 case kArm:
39 case kThumb2: {
40 DebugFrameOpCodeWriter<> opcodes;
41 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
42 // core registers.
43 for (int reg = 0; reg < 13; reg++) {
44 if (reg < 4 || reg == 12) {
45 opcodes.Undefined(Reg::ArmCore(reg));
46 } else {
47 opcodes.SameValue(Reg::ArmCore(reg));
48 }
49 }
50 // fp registers.
51 for (int reg = 0; reg < 32; reg++) {
52 if (reg < 16) {
53 opcodes.Undefined(Reg::ArmFp(reg));
54 } else {
55 opcodes.SameValue(Reg::ArmFp(reg));
56 }
57 }
David Srbecky527c9c72015-04-17 21:14:10 +010058 auto return_reg = Reg::ArmCore(14); // R14(LR).
59 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +010060 return;
61 }
62 case kArm64: {
63 DebugFrameOpCodeWriter<> opcodes;
64 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
65 // core registers.
66 for (int reg = 0; reg < 30; reg++) {
67 if (reg < 8 || reg == 16 || reg == 17) {
68 opcodes.Undefined(Reg::Arm64Core(reg));
69 } else {
70 opcodes.SameValue(Reg::Arm64Core(reg));
71 }
72 }
73 // fp registers.
74 for (int reg = 0; reg < 32; reg++) {
75 if (reg < 8 || reg >= 16) {
76 opcodes.Undefined(Reg::Arm64Fp(reg));
77 } else {
78 opcodes.SameValue(Reg::Arm64Fp(reg));
79 }
80 }
David Srbecky527c9c72015-04-17 21:14:10 +010081 auto return_reg = Reg::Arm64Core(30); // R30(LR).
82 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +010083 return;
84 }
85 case kMips:
86 case kMips64: {
87 DebugFrameOpCodeWriter<> opcodes;
88 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
89 // core registers.
90 for (int reg = 1; reg < 26; reg++) {
91 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
92 opcodes.Undefined(Reg::MipsCore(reg));
93 } else {
94 opcodes.SameValue(Reg::MipsCore(reg));
95 }
96 }
David Srbecky527c9c72015-04-17 21:14:10 +010097 auto return_reg = Reg::MipsCore(31); // R31(RA).
98 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +010099 return;
100 }
101 case kX86: {
102 DebugFrameOpCodeWriter<> opcodes;
103 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
104 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
105 // core registers.
106 for (int reg = 0; reg < 8; reg++) {
107 if (reg <= 3) {
108 opcodes.Undefined(Reg::X86Core(reg));
109 } else if (reg == 4) {
110 // Stack pointer.
111 } else {
112 opcodes.SameValue(Reg::X86Core(reg));
113 }
114 }
115 // fp registers.
116 for (int reg = 0; reg < 8; reg++) {
117 opcodes.Undefined(Reg::X86Fp(reg));
118 }
David Srbecky527c9c72015-04-17 21:14:10 +0100119 auto return_reg = Reg::X86Core(8); // R8(EIP).
120 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100121 return;
122 }
123 case kX86_64: {
124 DebugFrameOpCodeWriter<> opcodes;
125 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
126 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
127 // core registers.
128 for (int reg = 0; reg < 16; reg++) {
129 if (reg == 4) {
130 // Stack pointer.
131 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
132 opcodes.Undefined(Reg::X86_64Core(reg));
133 } else {
134 opcodes.SameValue(Reg::X86_64Core(reg));
135 }
136 }
137 // fp registers.
138 for (int reg = 0; reg < 16; reg++) {
139 if (reg < 12) {
140 opcodes.Undefined(Reg::X86_64Fp(reg));
141 } else {
142 opcodes.SameValue(Reg::X86_64Fp(reg));
143 }
144 }
David Srbecky527c9c72015-04-17 21:14:10 +0100145 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
146 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100147 return;
148 }
149 case kNone:
150 break;
151 }
152 LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
153 UNREACHABLE();
154}
155
David Srbecky8dc73242015-04-12 11:40:39 +0100156void WriteEhFrame(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100157 const OatWriter* oat_writer,
158 ExceptionHeaderValueApplication address_type,
159 std::vector<uint8_t>* eh_frame,
160 std::vector<uintptr_t>* eh_frame_patches,
161 std::vector<uint8_t>* eh_frame_hdr) {
David Srbecky8dc73242015-04-12 11:40:39 +0100162 const auto& method_infos = oat_writer->GetMethodDebugInfo();
163 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky527c9c72015-04-17 21:14:10 +0100164
165 // Write .eh_frame section.
David Srbecky8dc73242015-04-12 11:40:39 +0100166 size_t cie_offset = eh_frame->size();
David Srbecky527c9c72015-04-17 21:14:10 +0100167 WriteEhFrameCIE(isa, address_type, eh_frame);
David Srbecky8dc73242015-04-12 11:40:39 +0100168 for (const OatWriter::DebugInfo& mi : method_infos) {
169 const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
170 if (opcodes != nullptr) {
171 WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
David Srbecky527c9c72015-04-17 21:14:10 +0100172 mi.low_pc_, mi.high_pc_ - mi.low_pc_,
David Srbecky8dc73242015-04-12 11:40:39 +0100173 opcodes, eh_frame, eh_frame_patches);
174 }
175 }
David Srbecky527c9c72015-04-17 21:14:10 +0100176
177 // Write .eh_frame_hdr section.
178 Writer<> header(eh_frame_hdr);
179 header.PushUint8(1); // Version.
180 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); // Encoding of .eh_frame pointer.
181 header.PushUint8(DW_EH_PE_omit); // Encoding of binary search table size.
182 header.PushUint8(DW_EH_PE_omit); // Encoding of binary search table addresses.
183 // .eh_frame pointer - .eh_frame_hdr section is after .eh_frame section, and need to encode
184 // relative to this location as libunwind doesn't honor datarel for eh_frame_hdr correctly.
185 header.PushInt32(-static_cast<int32_t>(eh_frame->size() + 4U));
186 // Omit binary search table size (number of entries).
187 // Omit binary search table.
David Srbecky8dc73242015-04-12 11:40:39 +0100188}
189
David Srbecky3b9d57a2015-04-10 00:22:14 +0100190/*
191 * @brief Generate the DWARF sections.
192 * @param oat_writer The Oat file Writer.
193 * @param eh_frame Call Frame Information.
194 * @param debug_info Compilation unit information.
David Srbecky527c9c72015-04-17 21:14:10 +0100195 * @param debug_info_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100196 * @param debug_abbrev Abbreviations used to generate dbg_info.
197 * @param debug_str Debug strings.
198 * @param debug_line Line number table.
David Srbecky527c9c72015-04-17 21:14:10 +0100199 * @param debug_line_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100200 */
201void WriteDebugSections(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100202 const OatWriter* oat_writer,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100203 std::vector<uint8_t>* debug_info,
David Srbecky527c9c72015-04-17 21:14:10 +0100204 std::vector<uintptr_t>* debug_info_patches,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100205 std::vector<uint8_t>* debug_abbrev,
206 std::vector<uint8_t>* debug_str,
David Srbecky527c9c72015-04-17 21:14:10 +0100207 std::vector<uint8_t>* debug_line,
208 std::vector<uintptr_t>* debug_line_patches) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100209 const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetMethodDebugInfo();
210 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100211
David Srbecky626a1662015-04-12 13:12:26 +0100212 // Find all addresses (low_pc) which contain deduped methods.
213 // The first instance of method is not marked deduped_, but the rest is.
214 std::unordered_set<uint32_t> deduped_addresses;
215 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
216 if (it->deduped_) {
217 deduped_addresses.insert(it->low_pc_);
218 }
219 }
220
David Srbecky799b8c42015-04-14 01:57:43 +0100221 // Group the methods into compilation units based on source file.
222 std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units;
223 const char* last_source_file = nullptr;
224 for (const auto& mi : method_infos) {
225 // Attribute given instruction range only to single method.
226 // Otherwise the debugger might get really confused.
227 if (!mi.deduped_) {
228 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
229 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
230 if (compilation_units.empty() || source_file != last_source_file) {
231 compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>());
232 }
233 compilation_units.back().push_back(&mi);
234 last_source_file = source_file;
235 }
236 }
237
David Srbecky3b9d57a2015-04-10 00:22:14 +0100238 // Write .debug_info section.
David Srbecky799b8c42015-04-14 01:57:43 +0100239 for (const auto& compilation_unit : compilation_units) {
240 uint32_t cunit_low_pc = 0xFFFFFFFFU;
241 uint32_t cunit_high_pc = 0;
242 for (auto method_info : compilation_unit) {
243 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
244 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100245 }
246
David Srbecky799b8c42015-04-14 01:57:43 +0100247 size_t debug_abbrev_offset = debug_abbrev->size();
248 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
249 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
250 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
251 info.WriteData1(DW_AT_language, DW_LANG_Java);
David Srbecky527c9c72015-04-17 21:14:10 +0100252 info.WriteAddr(DW_AT_low_pc, cunit_low_pc);
253 info.WriteAddr(DW_AT_high_pc, cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100254 info.WriteData4(DW_AT_stmt_list, debug_line->size());
255 for (auto method_info : compilation_unit) {
256 std::string method_name = PrettyMethod(method_info->dex_method_index_,
257 *method_info->dex_file_, true);
258 if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) {
259 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100260 }
David Srbecky799b8c42015-04-14 01:57:43 +0100261 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
262 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
David Srbecky527c9c72015-04-17 21:14:10 +0100263 info.WriteAddr(DW_AT_low_pc, method_info->low_pc_);
264 info.WriteAddr(DW_AT_high_pc, method_info->high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100265 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100266 }
David Srbecky799b8c42015-04-14 01:57:43 +0100267 info.EndTag(); // DW_TAG_compile_unit
David Srbecky799b8c42015-04-14 01:57:43 +0100268 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100269
David Srbecky799b8c42015-04-14 01:57:43 +0100270 // Write .debug_line section.
271 std::vector<FileEntry> files;
272 std::unordered_map<std::string, size_t> files_map;
273 std::vector<std::string> directories;
274 std::unordered_map<std::string, size_t> directories_map;
275 int code_factor_bits_ = 0;
276 int dwarf_isa = -1;
277 switch (isa) {
278 case kArm: // arm actually means thumb2.
279 case kThumb2:
280 code_factor_bits_ = 1; // 16-bit instuctions
281 dwarf_isa = 1; // DW_ISA_ARM_thumb.
282 break;
283 case kArm64:
284 case kMips:
285 case kMips64:
286 code_factor_bits_ = 2; // 32-bit instructions
287 break;
288 case kNone:
289 case kX86:
290 case kX86_64:
291 break;
292 }
293 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
David Srbecky527c9c72015-04-17 21:14:10 +0100294 opcodes.SetAddress(cunit_low_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100295 if (dwarf_isa != -1) {
296 opcodes.SetISA(dwarf_isa);
297 }
298 for (const OatWriter::DebugInfo* mi : compilation_unit) {
299 struct DebugInfoCallbacks {
300 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
301 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
302 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
303 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100304 }
David Srbecky799b8c42015-04-14 01:57:43 +0100305 DefaultSrcMap dex2line_;
306 } debug_info_callbacks;
307
308 const DexFile* dex = mi->dex_file_;
309 if (mi->code_item_ != nullptr) {
310 dex->DecodeDebugInfo(mi->code_item_,
311 (mi->access_flags_ & kAccStatic) != 0,
312 mi->dex_method_index_,
313 DebugInfoCallbacks::NewPosition,
314 nullptr,
315 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100316 }
317
David Srbecky799b8c42015-04-14 01:57:43 +0100318 // Get and deduplicate directory and filename.
319 int file_index = 0; // 0 - primary source file of the compilation.
320 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
321 const char* source_file = dex->GetSourceFile(dex_class_def);
322 if (source_file != nullptr) {
323 std::string file_name(source_file);
324 size_t file_name_slash = file_name.find_last_of('/');
325 std::string class_name(dex->GetClassDescriptor(dex_class_def));
326 size_t class_name_slash = class_name.find_last_of('/');
327 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100328
David Srbecky799b8c42015-04-14 01:57:43 +0100329 // Guess directory from package name.
330 int directory_index = 0; // 0 - current directory of the compilation.
331 if (file_name_slash == std::string::npos && // Just filename.
332 class_name.front() == 'L' && // Type descriptor for a class.
333 class_name_slash != std::string::npos) { // Has package name.
334 std::string package_name = class_name.substr(1, class_name_slash - 1);
335 auto it = directories_map.find(package_name);
336 if (it == directories_map.end()) {
337 directory_index = 1 + directories.size();
338 directories_map.emplace(package_name, directory_index);
339 directories.push_back(package_name);
340 } else {
341 directory_index = it->second;
342 }
343 full_path = package_name + "/" + file_name;
344 }
345
346 // Add file entry.
347 auto it2 = files_map.find(full_path);
348 if (it2 == files_map.end()) {
349 file_index = 1 + files.size();
350 files_map.emplace(full_path, file_index);
351 files.push_back(FileEntry {
352 file_name,
353 directory_index,
354 0, // Modification time - NA.
355 0, // File size - NA.
356 });
357 } else {
358 file_index = it2->second;
359 }
360 }
361 opcodes.SetFile(file_index);
362
363 // Generate mapping opcodes from PC to Java lines.
364 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100365 if (file_index != 0 && !dex2line_map.empty()) {
366 bool first = true;
367 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
368 uint32_t pc = pc2dex.from_;
369 int dex_pc = pc2dex.to_;
370 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
371 if (dex2line.first) {
372 int line = dex2line.second;
373 if (first) {
374 first = false;
375 if (pc > 0) {
376 // Assume that any preceding code is prologue.
377 int first_line = dex2line_map.front().to_;
378 // Prologue is not a sensible place for a breakpoint.
379 opcodes.NegateStmt();
David Srbecky527c9c72015-04-17 21:14:10 +0100380 opcodes.AddRow(mi->low_pc_, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100381 opcodes.NegateStmt();
382 opcodes.SetPrologueEnd();
383 }
David Srbecky527c9c72015-04-17 21:14:10 +0100384 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100385 } else if (line != opcodes.CurrentLine()) {
David Srbecky527c9c72015-04-17 21:14:10 +0100386 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100387 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100388 }
389 }
David Srbecky799b8c42015-04-14 01:57:43 +0100390 } else {
391 // line 0 - instruction cannot be attributed to any source line.
David Srbecky527c9c72015-04-17 21:14:10 +0100392 opcodes.AddRow(mi->low_pc_, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100393 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100394 }
David Srbecky527c9c72015-04-17 21:14:10 +0100395 opcodes.AdvancePC(cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100396 opcodes.EndSequence();
David Srbecky799b8c42015-04-14 01:57:43 +0100397 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100398 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100399}
400
401} // namespace dwarf
402} // namespace art