blob: 39233ce94a4dea7827298a3223f92c2f5b58e99f [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
31static void WriteEhFrameCIE(InstructionSet isa, std::vector<uint8_t>* eh_frame) {
32 // Scratch registers should be marked as undefined. This tells the
33 // debugger that its value in the previous frame is not recoverable.
34 bool is64bit = Is64BitInstructionSet(isa);
35 switch (isa) {
36 case kArm:
37 case kThumb2: {
38 DebugFrameOpCodeWriter<> opcodes;
39 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
40 // core registers.
41 for (int reg = 0; reg < 13; reg++) {
42 if (reg < 4 || reg == 12) {
43 opcodes.Undefined(Reg::ArmCore(reg));
44 } else {
45 opcodes.SameValue(Reg::ArmCore(reg));
46 }
47 }
48 // fp registers.
49 for (int reg = 0; reg < 32; reg++) {
50 if (reg < 16) {
51 opcodes.Undefined(Reg::ArmFp(reg));
52 } else {
53 opcodes.SameValue(Reg::ArmFp(reg));
54 }
55 }
56 auto return_address_reg = Reg::ArmCore(14); // R14(LR).
57 WriteEhFrameCIE(is64bit, return_address_reg, opcodes, eh_frame);
58 return;
59 }
60 case kArm64: {
61 DebugFrameOpCodeWriter<> opcodes;
62 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
63 // core registers.
64 for (int reg = 0; reg < 30; reg++) {
65 if (reg < 8 || reg == 16 || reg == 17) {
66 opcodes.Undefined(Reg::Arm64Core(reg));
67 } else {
68 opcodes.SameValue(Reg::Arm64Core(reg));
69 }
70 }
71 // fp registers.
72 for (int reg = 0; reg < 32; reg++) {
73 if (reg < 8 || reg >= 16) {
74 opcodes.Undefined(Reg::Arm64Fp(reg));
75 } else {
76 opcodes.SameValue(Reg::Arm64Fp(reg));
77 }
78 }
79 auto return_address_reg = Reg::Arm64Core(30); // R30(LR).
80 WriteEhFrameCIE(is64bit, return_address_reg, opcodes, eh_frame);
81 return;
82 }
83 case kMips:
84 case kMips64: {
85 DebugFrameOpCodeWriter<> opcodes;
86 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
87 // core registers.
88 for (int reg = 1; reg < 26; reg++) {
89 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
90 opcodes.Undefined(Reg::MipsCore(reg));
91 } else {
92 opcodes.SameValue(Reg::MipsCore(reg));
93 }
94 }
95 auto return_address_reg = Reg::MipsCore(31); // R31(RA).
96 WriteEhFrameCIE(is64bit, return_address_reg, opcodes, eh_frame);
97 return;
98 }
99 case kX86: {
100 DebugFrameOpCodeWriter<> opcodes;
101 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
102 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
103 // core registers.
104 for (int reg = 0; reg < 8; reg++) {
105 if (reg <= 3) {
106 opcodes.Undefined(Reg::X86Core(reg));
107 } else if (reg == 4) {
108 // Stack pointer.
109 } else {
110 opcodes.SameValue(Reg::X86Core(reg));
111 }
112 }
113 // fp registers.
114 for (int reg = 0; reg < 8; reg++) {
115 opcodes.Undefined(Reg::X86Fp(reg));
116 }
117 auto return_address_reg = Reg::X86Core(8); // R8(EIP).
118 WriteEhFrameCIE(is64bit, return_address_reg, opcodes, eh_frame);
119 return;
120 }
121 case kX86_64: {
122 DebugFrameOpCodeWriter<> opcodes;
123 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
124 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
125 // core registers.
126 for (int reg = 0; reg < 16; reg++) {
127 if (reg == 4) {
128 // Stack pointer.
129 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
130 opcodes.Undefined(Reg::X86_64Core(reg));
131 } else {
132 opcodes.SameValue(Reg::X86_64Core(reg));
133 }
134 }
135 // fp registers.
136 for (int reg = 0; reg < 16; reg++) {
137 if (reg < 12) {
138 opcodes.Undefined(Reg::X86_64Fp(reg));
139 } else {
140 opcodes.SameValue(Reg::X86_64Fp(reg));
141 }
142 }
143 auto return_address_reg = Reg::X86_64Core(16); // R16(RIP).
144 WriteEhFrameCIE(is64bit, return_address_reg, opcodes, eh_frame);
145 return;
146 }
147 case kNone:
148 break;
149 }
150 LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
151 UNREACHABLE();
152}
153
David Srbecky8dc73242015-04-12 11:40:39 +0100154void WriteEhFrame(const CompilerDriver* compiler,
155 OatWriter* oat_writer,
156 uint32_t text_section_offset,
157 std::vector<uint8_t>* eh_frame) {
158 const auto& method_infos = oat_writer->GetMethodDebugInfo();
159 const InstructionSet isa = compiler->GetInstructionSet();
160 size_t cie_offset = eh_frame->size();
161 auto* eh_frame_patches = oat_writer->GetAbsolutePatchLocationsFor(".eh_frame");
162 WriteEhFrameCIE(isa, eh_frame);
163 for (const OatWriter::DebugInfo& mi : method_infos) {
164 const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
165 if (opcodes != nullptr) {
166 WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
167 text_section_offset + mi.low_pc_, mi.high_pc_ - mi.low_pc_,
168 opcodes, eh_frame, eh_frame_patches);
169 }
170 }
171}
172
David Srbecky3b9d57a2015-04-10 00:22:14 +0100173/*
174 * @brief Generate the DWARF sections.
175 * @param oat_writer The Oat file Writer.
176 * @param eh_frame Call Frame Information.
177 * @param debug_info Compilation unit information.
178 * @param debug_abbrev Abbreviations used to generate dbg_info.
179 * @param debug_str Debug strings.
180 * @param debug_line Line number table.
181 */
182void WriteDebugSections(const CompilerDriver* compiler,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100183 OatWriter* oat_writer,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100184 uint32_t text_section_offset,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100185 std::vector<uint8_t>* debug_info,
186 std::vector<uint8_t>* debug_abbrev,
187 std::vector<uint8_t>* debug_str,
188 std::vector<uint8_t>* debug_line) {
189 const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetMethodDebugInfo();
190 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100191
David Srbecky626a1662015-04-12 13:12:26 +0100192 // Find all addresses (low_pc) which contain deduped methods.
193 // The first instance of method is not marked deduped_, but the rest is.
194 std::unordered_set<uint32_t> deduped_addresses;
195 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
196 if (it->deduped_) {
197 deduped_addresses.insert(it->low_pc_);
198 }
199 }
200
David Srbecky799b8c42015-04-14 01:57:43 +0100201 // Group the methods into compilation units based on source file.
202 std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units;
203 const char* last_source_file = nullptr;
204 for (const auto& mi : method_infos) {
205 // Attribute given instruction range only to single method.
206 // Otherwise the debugger might get really confused.
207 if (!mi.deduped_) {
208 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
209 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
210 if (compilation_units.empty() || source_file != last_source_file) {
211 compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>());
212 }
213 compilation_units.back().push_back(&mi);
214 last_source_file = source_file;
215 }
216 }
217
David Srbecky3b9d57a2015-04-10 00:22:14 +0100218 // Write .debug_info section.
David Srbecky799b8c42015-04-14 01:57:43 +0100219 for (const auto& compilation_unit : compilation_units) {
220 uint32_t cunit_low_pc = 0xFFFFFFFFU;
221 uint32_t cunit_high_pc = 0;
222 for (auto method_info : compilation_unit) {
223 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
224 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100225 }
226
David Srbecky799b8c42015-04-14 01:57:43 +0100227 size_t debug_abbrev_offset = debug_abbrev->size();
228 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
229 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
230 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
231 info.WriteData1(DW_AT_language, DW_LANG_Java);
232 info.WriteAddr(DW_AT_low_pc, cunit_low_pc + text_section_offset);
233 info.WriteAddr(DW_AT_high_pc, cunit_high_pc + text_section_offset);
234 info.WriteData4(DW_AT_stmt_list, debug_line->size());
235 for (auto method_info : compilation_unit) {
236 std::string method_name = PrettyMethod(method_info->dex_method_index_,
237 *method_info->dex_file_, true);
238 if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) {
239 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100240 }
David Srbecky799b8c42015-04-14 01:57:43 +0100241 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
242 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
243 info.WriteAddr(DW_AT_low_pc, method_info->low_pc_ + text_section_offset);
244 info.WriteAddr(DW_AT_high_pc, method_info->high_pc_ + text_section_offset);
245 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100246 }
David Srbecky799b8c42015-04-14 01:57:43 +0100247 info.EndTag(); // DW_TAG_compile_unit
248 auto* debug_info_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_info");
249 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100250
David Srbecky799b8c42015-04-14 01:57:43 +0100251 // Write .debug_line section.
252 std::vector<FileEntry> files;
253 std::unordered_map<std::string, size_t> files_map;
254 std::vector<std::string> directories;
255 std::unordered_map<std::string, size_t> directories_map;
256 int code_factor_bits_ = 0;
257 int dwarf_isa = -1;
258 switch (isa) {
259 case kArm: // arm actually means thumb2.
260 case kThumb2:
261 code_factor_bits_ = 1; // 16-bit instuctions
262 dwarf_isa = 1; // DW_ISA_ARM_thumb.
263 break;
264 case kArm64:
265 case kMips:
266 case kMips64:
267 code_factor_bits_ = 2; // 32-bit instructions
268 break;
269 case kNone:
270 case kX86:
271 case kX86_64:
272 break;
273 }
274 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
275 opcodes.SetAddress(text_section_offset + cunit_low_pc);
276 if (dwarf_isa != -1) {
277 opcodes.SetISA(dwarf_isa);
278 }
279 for (const OatWriter::DebugInfo* mi : compilation_unit) {
280 struct DebugInfoCallbacks {
281 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
282 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
283 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
284 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100285 }
David Srbecky799b8c42015-04-14 01:57:43 +0100286 DefaultSrcMap dex2line_;
287 } debug_info_callbacks;
288
289 const DexFile* dex = mi->dex_file_;
290 if (mi->code_item_ != nullptr) {
291 dex->DecodeDebugInfo(mi->code_item_,
292 (mi->access_flags_ & kAccStatic) != 0,
293 mi->dex_method_index_,
294 DebugInfoCallbacks::NewPosition,
295 nullptr,
296 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100297 }
298
David Srbecky799b8c42015-04-14 01:57:43 +0100299 // Get and deduplicate directory and filename.
300 int file_index = 0; // 0 - primary source file of the compilation.
301 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
302 const char* source_file = dex->GetSourceFile(dex_class_def);
303 if (source_file != nullptr) {
304 std::string file_name(source_file);
305 size_t file_name_slash = file_name.find_last_of('/');
306 std::string class_name(dex->GetClassDescriptor(dex_class_def));
307 size_t class_name_slash = class_name.find_last_of('/');
308 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100309
David Srbecky799b8c42015-04-14 01:57:43 +0100310 // Guess directory from package name.
311 int directory_index = 0; // 0 - current directory of the compilation.
312 if (file_name_slash == std::string::npos && // Just filename.
313 class_name.front() == 'L' && // Type descriptor for a class.
314 class_name_slash != std::string::npos) { // Has package name.
315 std::string package_name = class_name.substr(1, class_name_slash - 1);
316 auto it = directories_map.find(package_name);
317 if (it == directories_map.end()) {
318 directory_index = 1 + directories.size();
319 directories_map.emplace(package_name, directory_index);
320 directories.push_back(package_name);
321 } else {
322 directory_index = it->second;
323 }
324 full_path = package_name + "/" + file_name;
325 }
326
327 // Add file entry.
328 auto it2 = files_map.find(full_path);
329 if (it2 == files_map.end()) {
330 file_index = 1 + files.size();
331 files_map.emplace(full_path, file_index);
332 files.push_back(FileEntry {
333 file_name,
334 directory_index,
335 0, // Modification time - NA.
336 0, // File size - NA.
337 });
338 } else {
339 file_index = it2->second;
340 }
341 }
342 opcodes.SetFile(file_index);
343
344 // Generate mapping opcodes from PC to Java lines.
345 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
346 uint32_t low_pc = text_section_offset + mi->low_pc_;
347 if (file_index != 0 && !dex2line_map.empty()) {
348 bool first = true;
349 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
350 uint32_t pc = pc2dex.from_;
351 int dex_pc = pc2dex.to_;
352 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
353 if (dex2line.first) {
354 int line = dex2line.second;
355 if (first) {
356 first = false;
357 if (pc > 0) {
358 // Assume that any preceding code is prologue.
359 int first_line = dex2line_map.front().to_;
360 // Prologue is not a sensible place for a breakpoint.
361 opcodes.NegateStmt();
362 opcodes.AddRow(low_pc, first_line);
363 opcodes.NegateStmt();
364 opcodes.SetPrologueEnd();
365 }
366 opcodes.AddRow(low_pc + pc, line);
367 } else if (line != opcodes.CurrentLine()) {
368 opcodes.AddRow(low_pc + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100369 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100370 }
371 }
David Srbecky799b8c42015-04-14 01:57:43 +0100372 } else {
373 // line 0 - instruction cannot be attributed to any source line.
374 opcodes.AddRow(low_pc, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100375 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100376 }
David Srbecky799b8c42015-04-14 01:57:43 +0100377 opcodes.AdvancePC(text_section_offset + cunit_high_pc);
378 opcodes.EndSequence();
379 auto* debug_line_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_line");
380 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100381 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100382}
383
384} // namespace dwarf
385} // namespace art