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