blob: 28e69994721a1e576c9d940a6ab97a280237e4bb [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: {
David Srbecky8a813f72015-04-20 16:43:52 +0100102 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
103 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100104 DebugFrameOpCodeWriter<> opcodes;
105 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
106 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
107 // core registers.
108 for (int reg = 0; reg < 8; reg++) {
109 if (reg <= 3) {
110 opcodes.Undefined(Reg::X86Core(reg));
111 } else if (reg == 4) {
112 // Stack pointer.
113 } else {
114 opcodes.SameValue(Reg::X86Core(reg));
115 }
116 }
117 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100118 if (generate_opcodes_for_x86_fp) {
119 for (int reg = 0; reg < 8; reg++) {
120 opcodes.Undefined(Reg::X86Fp(reg));
121 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100122 }
David Srbecky527c9c72015-04-17 21:14:10 +0100123 auto return_reg = Reg::X86Core(8); // R8(EIP).
124 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100125 return;
126 }
127 case kX86_64: {
128 DebugFrameOpCodeWriter<> opcodes;
129 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
130 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
131 // core registers.
132 for (int reg = 0; reg < 16; reg++) {
133 if (reg == 4) {
134 // Stack pointer.
135 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
136 opcodes.Undefined(Reg::X86_64Core(reg));
137 } else {
138 opcodes.SameValue(Reg::X86_64Core(reg));
139 }
140 }
141 // fp registers.
142 for (int reg = 0; reg < 16; reg++) {
143 if (reg < 12) {
144 opcodes.Undefined(Reg::X86_64Fp(reg));
145 } else {
146 opcodes.SameValue(Reg::X86_64Fp(reg));
147 }
148 }
David Srbecky527c9c72015-04-17 21:14:10 +0100149 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
150 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100151 return;
152 }
153 case kNone:
154 break;
155 }
156 LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
157 UNREACHABLE();
158}
159
David Srbecky8dc73242015-04-12 11:40:39 +0100160void WriteEhFrame(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100161 const OatWriter* oat_writer,
162 ExceptionHeaderValueApplication address_type,
163 std::vector<uint8_t>* eh_frame,
164 std::vector<uintptr_t>* eh_frame_patches,
165 std::vector<uint8_t>* eh_frame_hdr) {
David Srbecky8dc73242015-04-12 11:40:39 +0100166 const auto& method_infos = oat_writer->GetMethodDebugInfo();
167 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky527c9c72015-04-17 21:14:10 +0100168
169 // Write .eh_frame section.
David Srbecky8dc73242015-04-12 11:40:39 +0100170 size_t cie_offset = eh_frame->size();
David Srbecky527c9c72015-04-17 21:14:10 +0100171 WriteEhFrameCIE(isa, address_type, eh_frame);
David Srbecky8dc73242015-04-12 11:40:39 +0100172 for (const OatWriter::DebugInfo& mi : method_infos) {
173 const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
174 if (opcodes != nullptr) {
175 WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
David Srbecky527c9c72015-04-17 21:14:10 +0100176 mi.low_pc_, mi.high_pc_ - mi.low_pc_,
David Srbecky8dc73242015-04-12 11:40:39 +0100177 opcodes, eh_frame, eh_frame_patches);
178 }
179 }
David Srbecky527c9c72015-04-17 21:14:10 +0100180
181 // Write .eh_frame_hdr section.
182 Writer<> header(eh_frame_hdr);
183 header.PushUint8(1); // Version.
184 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); // Encoding of .eh_frame pointer.
185 header.PushUint8(DW_EH_PE_omit); // Encoding of binary search table size.
186 header.PushUint8(DW_EH_PE_omit); // Encoding of binary search table addresses.
187 // .eh_frame pointer - .eh_frame_hdr section is after .eh_frame section, and need to encode
188 // relative to this location as libunwind doesn't honor datarel for eh_frame_hdr correctly.
189 header.PushInt32(-static_cast<int32_t>(eh_frame->size() + 4U));
190 // Omit binary search table size (number of entries).
191 // Omit binary search table.
David Srbecky8dc73242015-04-12 11:40:39 +0100192}
193
David Srbecky3b9d57a2015-04-10 00:22:14 +0100194/*
195 * @brief Generate the DWARF sections.
196 * @param oat_writer The Oat file Writer.
197 * @param eh_frame Call Frame Information.
198 * @param debug_info Compilation unit information.
David Srbecky527c9c72015-04-17 21:14:10 +0100199 * @param debug_info_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100200 * @param debug_abbrev Abbreviations used to generate dbg_info.
201 * @param debug_str Debug strings.
202 * @param debug_line Line number table.
David Srbecky527c9c72015-04-17 21:14:10 +0100203 * @param debug_line_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100204 */
205void WriteDebugSections(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100206 const OatWriter* oat_writer,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100207 std::vector<uint8_t>* debug_info,
David Srbecky527c9c72015-04-17 21:14:10 +0100208 std::vector<uintptr_t>* debug_info_patches,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100209 std::vector<uint8_t>* debug_abbrev,
210 std::vector<uint8_t>* debug_str,
David Srbecky527c9c72015-04-17 21:14:10 +0100211 std::vector<uint8_t>* debug_line,
212 std::vector<uintptr_t>* debug_line_patches) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100213 const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetMethodDebugInfo();
214 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100215
David Srbecky626a1662015-04-12 13:12:26 +0100216 // Find all addresses (low_pc) which contain deduped methods.
217 // The first instance of method is not marked deduped_, but the rest is.
218 std::unordered_set<uint32_t> deduped_addresses;
219 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
220 if (it->deduped_) {
221 deduped_addresses.insert(it->low_pc_);
222 }
223 }
224
David Srbecky799b8c42015-04-14 01:57:43 +0100225 // Group the methods into compilation units based on source file.
226 std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units;
227 const char* last_source_file = nullptr;
228 for (const auto& mi : method_infos) {
229 // Attribute given instruction range only to single method.
230 // Otherwise the debugger might get really confused.
231 if (!mi.deduped_) {
232 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
233 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
234 if (compilation_units.empty() || source_file != last_source_file) {
235 compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>());
236 }
237 compilation_units.back().push_back(&mi);
238 last_source_file = source_file;
239 }
240 }
241
David Srbecky3b9d57a2015-04-10 00:22:14 +0100242 // Write .debug_info section.
David Srbecky799b8c42015-04-14 01:57:43 +0100243 for (const auto& compilation_unit : compilation_units) {
244 uint32_t cunit_low_pc = 0xFFFFFFFFU;
245 uint32_t cunit_high_pc = 0;
246 for (auto method_info : compilation_unit) {
247 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
248 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100249 }
250
David Srbecky799b8c42015-04-14 01:57:43 +0100251 size_t debug_abbrev_offset = debug_abbrev->size();
252 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
253 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
254 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
255 info.WriteData1(DW_AT_language, DW_LANG_Java);
David Srbecky527c9c72015-04-17 21:14:10 +0100256 info.WriteAddr(DW_AT_low_pc, cunit_low_pc);
257 info.WriteAddr(DW_AT_high_pc, cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100258 info.WriteData4(DW_AT_stmt_list, debug_line->size());
259 for (auto method_info : compilation_unit) {
260 std::string method_name = PrettyMethod(method_info->dex_method_index_,
261 *method_info->dex_file_, true);
262 if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) {
263 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100264 }
David Srbecky799b8c42015-04-14 01:57:43 +0100265 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
266 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
David Srbecky527c9c72015-04-17 21:14:10 +0100267 info.WriteAddr(DW_AT_low_pc, method_info->low_pc_);
268 info.WriteAddr(DW_AT_high_pc, method_info->high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100269 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100270 }
David Srbecky799b8c42015-04-14 01:57:43 +0100271 info.EndTag(); // DW_TAG_compile_unit
David Srbecky799b8c42015-04-14 01:57:43 +0100272 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100273
David Srbecky799b8c42015-04-14 01:57:43 +0100274 // Write .debug_line section.
275 std::vector<FileEntry> files;
276 std::unordered_map<std::string, size_t> files_map;
277 std::vector<std::string> directories;
278 std::unordered_map<std::string, size_t> directories_map;
279 int code_factor_bits_ = 0;
280 int dwarf_isa = -1;
281 switch (isa) {
282 case kArm: // arm actually means thumb2.
283 case kThumb2:
284 code_factor_bits_ = 1; // 16-bit instuctions
285 dwarf_isa = 1; // DW_ISA_ARM_thumb.
286 break;
287 case kArm64:
288 case kMips:
289 case kMips64:
290 code_factor_bits_ = 2; // 32-bit instructions
291 break;
292 case kNone:
293 case kX86:
294 case kX86_64:
295 break;
296 }
297 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
David Srbecky527c9c72015-04-17 21:14:10 +0100298 opcodes.SetAddress(cunit_low_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100299 if (dwarf_isa != -1) {
300 opcodes.SetISA(dwarf_isa);
301 }
302 for (const OatWriter::DebugInfo* mi : compilation_unit) {
303 struct DebugInfoCallbacks {
304 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
305 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
306 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
307 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100308 }
David Srbecky799b8c42015-04-14 01:57:43 +0100309 DefaultSrcMap dex2line_;
310 } debug_info_callbacks;
311
312 const DexFile* dex = mi->dex_file_;
313 if (mi->code_item_ != nullptr) {
314 dex->DecodeDebugInfo(mi->code_item_,
315 (mi->access_flags_ & kAccStatic) != 0,
316 mi->dex_method_index_,
317 DebugInfoCallbacks::NewPosition,
318 nullptr,
319 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100320 }
321
David Srbecky799b8c42015-04-14 01:57:43 +0100322 // Get and deduplicate directory and filename.
323 int file_index = 0; // 0 - primary source file of the compilation.
324 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
325 const char* source_file = dex->GetSourceFile(dex_class_def);
326 if (source_file != nullptr) {
327 std::string file_name(source_file);
328 size_t file_name_slash = file_name.find_last_of('/');
329 std::string class_name(dex->GetClassDescriptor(dex_class_def));
330 size_t class_name_slash = class_name.find_last_of('/');
331 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100332
David Srbecky799b8c42015-04-14 01:57:43 +0100333 // Guess directory from package name.
334 int directory_index = 0; // 0 - current directory of the compilation.
335 if (file_name_slash == std::string::npos && // Just filename.
336 class_name.front() == 'L' && // Type descriptor for a class.
337 class_name_slash != std::string::npos) { // Has package name.
338 std::string package_name = class_name.substr(1, class_name_slash - 1);
339 auto it = directories_map.find(package_name);
340 if (it == directories_map.end()) {
341 directory_index = 1 + directories.size();
342 directories_map.emplace(package_name, directory_index);
343 directories.push_back(package_name);
344 } else {
345 directory_index = it->second;
346 }
347 full_path = package_name + "/" + file_name;
348 }
349
350 // Add file entry.
351 auto it2 = files_map.find(full_path);
352 if (it2 == files_map.end()) {
353 file_index = 1 + files.size();
354 files_map.emplace(full_path, file_index);
355 files.push_back(FileEntry {
356 file_name,
357 directory_index,
358 0, // Modification time - NA.
359 0, // File size - NA.
360 });
361 } else {
362 file_index = it2->second;
363 }
364 }
365 opcodes.SetFile(file_index);
366
367 // Generate mapping opcodes from PC to Java lines.
368 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100369 if (file_index != 0 && !dex2line_map.empty()) {
370 bool first = true;
371 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
372 uint32_t pc = pc2dex.from_;
373 int dex_pc = pc2dex.to_;
374 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
375 if (dex2line.first) {
376 int line = dex2line.second;
377 if (first) {
378 first = false;
379 if (pc > 0) {
380 // Assume that any preceding code is prologue.
381 int first_line = dex2line_map.front().to_;
382 // Prologue is not a sensible place for a breakpoint.
383 opcodes.NegateStmt();
David Srbecky527c9c72015-04-17 21:14:10 +0100384 opcodes.AddRow(mi->low_pc_, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100385 opcodes.NegateStmt();
386 opcodes.SetPrologueEnd();
387 }
David Srbecky527c9c72015-04-17 21:14:10 +0100388 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100389 } else if (line != opcodes.CurrentLine()) {
David Srbecky527c9c72015-04-17 21:14:10 +0100390 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100391 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100392 }
393 }
David Srbecky799b8c42015-04-14 01:57:43 +0100394 } else {
395 // line 0 - instruction cannot be attributed to any source line.
David Srbecky527c9c72015-04-17 21:14:10 +0100396 opcodes.AddRow(mi->low_pc_, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100397 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100398 }
David Srbecky527c9c72015-04-17 21:14:10 +0100399 opcodes.AdvancePC(cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100400 opcodes.EndSequence();
David Srbecky799b8c42015-04-14 01:57:43 +0100401 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100402 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100403}
404
405} // namespace dwarf
406} // namespace art