blob: 09bb6bcae72e54b92a3c6c2f6240e95c2c748b9b [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 Srbecky297ed222015-04-15 01:18:12 +0100211 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100212
David Srbecky626a1662015-04-12 13:12:26 +0100213 // Find all addresses (low_pc) which contain deduped methods.
214 // The first instance of method is not marked deduped_, but the rest is.
215 std::unordered_set<uint32_t> deduped_addresses;
216 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
217 if (it->deduped_) {
218 deduped_addresses.insert(it->low_pc_);
219 }
220 }
221
David Srbecky799b8c42015-04-14 01:57:43 +0100222 // Group the methods into compilation units based on source file.
223 std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units;
224 const char* last_source_file = nullptr;
225 for (const auto& mi : method_infos) {
226 // Attribute given instruction range only to single method.
227 // Otherwise the debugger might get really confused.
228 if (!mi.deduped_) {
229 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
230 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
231 if (compilation_units.empty() || source_file != last_source_file) {
232 compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>());
233 }
234 compilation_units.back().push_back(&mi);
235 last_source_file = source_file;
236 }
237 }
238
David Srbecky3b9d57a2015-04-10 00:22:14 +0100239 // Write .debug_info section.
David Srbecky799b8c42015-04-14 01:57:43 +0100240 for (const auto& compilation_unit : compilation_units) {
241 uint32_t cunit_low_pc = 0xFFFFFFFFU;
242 uint32_t cunit_high_pc = 0;
243 for (auto method_info : compilation_unit) {
244 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
245 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100246 }
247
David Srbecky799b8c42015-04-14 01:57:43 +0100248 size_t debug_abbrev_offset = debug_abbrev->size();
David Srbecky297ed222015-04-15 01:18:12 +0100249 DebugInfoEntryWriter<> info(is64bit, debug_abbrev);
David Srbecky799b8c42015-04-14 01:57:43 +0100250 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
251 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
252 info.WriteData1(DW_AT_language, DW_LANG_Java);
David Srbecky527c9c72015-04-17 21:14:10 +0100253 info.WriteAddr(DW_AT_low_pc, cunit_low_pc);
254 info.WriteAddr(DW_AT_high_pc, cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100255 info.WriteData4(DW_AT_stmt_list, debug_line->size());
256 for (auto method_info : compilation_unit) {
257 std::string method_name = PrettyMethod(method_info->dex_method_index_,
258 *method_info->dex_file_, true);
259 if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) {
260 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100261 }
David Srbecky799b8c42015-04-14 01:57:43 +0100262 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
263 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
David Srbecky527c9c72015-04-17 21:14:10 +0100264 info.WriteAddr(DW_AT_low_pc, method_info->low_pc_);
265 info.WriteAddr(DW_AT_high_pc, method_info->high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100266 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100267 }
David Srbecky799b8c42015-04-14 01:57:43 +0100268 info.EndTag(); // DW_TAG_compile_unit
David Srbecky799b8c42015-04-14 01:57:43 +0100269 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100270
David Srbecky799b8c42015-04-14 01:57:43 +0100271 // Write .debug_line section.
272 std::vector<FileEntry> files;
273 std::unordered_map<std::string, size_t> files_map;
274 std::vector<std::string> directories;
275 std::unordered_map<std::string, size_t> directories_map;
276 int code_factor_bits_ = 0;
277 int dwarf_isa = -1;
278 switch (isa) {
279 case kArm: // arm actually means thumb2.
280 case kThumb2:
281 code_factor_bits_ = 1; // 16-bit instuctions
282 dwarf_isa = 1; // DW_ISA_ARM_thumb.
283 break;
284 case kArm64:
285 case kMips:
286 case kMips64:
287 code_factor_bits_ = 2; // 32-bit instructions
288 break;
289 case kNone:
290 case kX86:
291 case kX86_64:
292 break;
293 }
David Srbecky297ed222015-04-15 01:18:12 +0100294 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
David Srbecky527c9c72015-04-17 21:14:10 +0100295 opcodes.SetAddress(cunit_low_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100296 if (dwarf_isa != -1) {
297 opcodes.SetISA(dwarf_isa);
298 }
299 for (const OatWriter::DebugInfo* mi : compilation_unit) {
300 struct DebugInfoCallbacks {
301 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
302 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
303 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
304 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100305 }
David Srbecky799b8c42015-04-14 01:57:43 +0100306 DefaultSrcMap dex2line_;
307 } debug_info_callbacks;
308
309 const DexFile* dex = mi->dex_file_;
310 if (mi->code_item_ != nullptr) {
311 dex->DecodeDebugInfo(mi->code_item_,
312 (mi->access_flags_ & kAccStatic) != 0,
313 mi->dex_method_index_,
314 DebugInfoCallbacks::NewPosition,
315 nullptr,
316 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100317 }
318
David Srbecky799b8c42015-04-14 01:57:43 +0100319 // Get and deduplicate directory and filename.
320 int file_index = 0; // 0 - primary source file of the compilation.
321 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
322 const char* source_file = dex->GetSourceFile(dex_class_def);
323 if (source_file != nullptr) {
324 std::string file_name(source_file);
325 size_t file_name_slash = file_name.find_last_of('/');
326 std::string class_name(dex->GetClassDescriptor(dex_class_def));
327 size_t class_name_slash = class_name.find_last_of('/');
328 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100329
David Srbecky799b8c42015-04-14 01:57:43 +0100330 // Guess directory from package name.
331 int directory_index = 0; // 0 - current directory of the compilation.
332 if (file_name_slash == std::string::npos && // Just filename.
333 class_name.front() == 'L' && // Type descriptor for a class.
334 class_name_slash != std::string::npos) { // Has package name.
335 std::string package_name = class_name.substr(1, class_name_slash - 1);
336 auto it = directories_map.find(package_name);
337 if (it == directories_map.end()) {
338 directory_index = 1 + directories.size();
339 directories_map.emplace(package_name, directory_index);
340 directories.push_back(package_name);
341 } else {
342 directory_index = it->second;
343 }
344 full_path = package_name + "/" + file_name;
345 }
346
347 // Add file entry.
348 auto it2 = files_map.find(full_path);
349 if (it2 == files_map.end()) {
350 file_index = 1 + files.size();
351 files_map.emplace(full_path, file_index);
352 files.push_back(FileEntry {
353 file_name,
354 directory_index,
355 0, // Modification time - NA.
356 0, // File size - NA.
357 });
358 } else {
359 file_index = it2->second;
360 }
361 }
362 opcodes.SetFile(file_index);
363
364 // Generate mapping opcodes from PC to Java lines.
365 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100366 if (file_index != 0 && !dex2line_map.empty()) {
367 bool first = true;
368 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
369 uint32_t pc = pc2dex.from_;
370 int dex_pc = pc2dex.to_;
371 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
372 if (dex2line.first) {
373 int line = dex2line.second;
374 if (first) {
375 first = false;
376 if (pc > 0) {
377 // Assume that any preceding code is prologue.
378 int first_line = dex2line_map.front().to_;
379 // Prologue is not a sensible place for a breakpoint.
380 opcodes.NegateStmt();
David Srbecky527c9c72015-04-17 21:14:10 +0100381 opcodes.AddRow(mi->low_pc_, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100382 opcodes.NegateStmt();
383 opcodes.SetPrologueEnd();
384 }
David Srbecky527c9c72015-04-17 21:14:10 +0100385 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100386 } else if (line != opcodes.CurrentLine()) {
David Srbecky527c9c72015-04-17 21:14:10 +0100387 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100388 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100389 }
390 }
David Srbecky799b8c42015-04-14 01:57:43 +0100391 } else {
392 // line 0 - instruction cannot be attributed to any source line.
David Srbecky527c9c72015-04-17 21:14:10 +0100393 opcodes.AddRow(mi->low_pc_, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100394 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100395 }
David Srbecky527c9c72015-04-17 21:14:10 +0100396 opcodes.AdvancePC(cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100397 opcodes.EndSequence();
David Srbecky799b8c42015-04-14 01:57:43 +0100398 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100399 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100400}
401
402} // namespace dwarf
403} // namespace art