blob: 6df5ea9eef999163c58c6afdc60e6b084c90b3a4 [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
152/*
153 * @brief Generate the DWARF sections.
154 * @param oat_writer The Oat file Writer.
155 * @param eh_frame Call Frame Information.
156 * @param debug_info Compilation unit information.
157 * @param debug_abbrev Abbreviations used to generate dbg_info.
158 * @param debug_str Debug strings.
159 * @param debug_line Line number table.
160 */
161void WriteDebugSections(const CompilerDriver* compiler,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100162 OatWriter* oat_writer,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100163 uint32_t text_section_offset,
164 std::vector<uint8_t>* eh_frame,
165 std::vector<uint8_t>* debug_info,
166 std::vector<uint8_t>* debug_abbrev,
167 std::vector<uint8_t>* debug_str,
168 std::vector<uint8_t>* debug_line) {
169 const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetMethodDebugInfo();
170 const InstructionSet isa = compiler->GetInstructionSet();
171 uint32_t cunit_low_pc = static_cast<uint32_t>(-1);
172 uint32_t cunit_high_pc = 0;
173 for (auto method_info : method_infos) {
174 cunit_low_pc = std::min(cunit_low_pc, method_info.low_pc_);
175 cunit_high_pc = std::max(cunit_high_pc, method_info.high_pc_);
176 }
177
178 // Write .eh_frame section.
David Srbecky2f6cdb02015-04-11 00:17:53 +0100179 auto* eh_frame_patches = oat_writer->GetAbsolutePatchLocationsFor(".eh_frame");
David Srbecky3b9d57a2015-04-10 00:22:14 +0100180 size_t cie_offset = eh_frame->size();
181 WriteEhFrameCIE(isa, eh_frame);
182 for (const OatWriter::DebugInfo& mi : method_infos) {
183 const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
184 if (opcodes != nullptr) {
185 WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
186 text_section_offset + mi.low_pc_, mi.high_pc_ - mi.low_pc_,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100187 opcodes, eh_frame, eh_frame_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100188 }
189 }
190
191 // Write .debug_info section.
192 size_t debug_abbrev_offset = debug_abbrev->size();
193 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
194 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
195 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
196 info.WriteData1(DW_AT_language, DW_LANG_Java);
197 info.WriteAddr(DW_AT_low_pc, cunit_low_pc + text_section_offset);
198 info.WriteAddr(DW_AT_high_pc, cunit_high_pc + text_section_offset);
199 info.WriteData4(DW_AT_stmt_list, debug_line->size());
200 for (auto method_info : method_infos) {
201 std::string method_name = PrettyMethod(method_info.dex_method_index_,
202 *method_info.dex_file_, true);
203 if (method_info.deduped_) {
204 // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol
205 // so that it will show up in a debuggerd crash report.
206 method_name += " [ DEDUPED ]";
207 }
208 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
209 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
210 info.WriteAddr(DW_AT_low_pc, method_info.low_pc_ + text_section_offset);
211 info.WriteAddr(DW_AT_high_pc, method_info.high_pc_ + text_section_offset);
212 info.EndTag(); // DW_TAG_subprogram
213 }
214 info.EndTag(); // DW_TAG_compile_unit
David Srbecky2f6cdb02015-04-11 00:17:53 +0100215 auto* debug_info_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_info");
216 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100217
218 // TODO: in gdb info functions <regexp> - reports Java functions, but
219 // source file is <unknown> because .debug_line is formed as one
220 // compilation unit. To fix this it is possible to generate
221 // a separate compilation unit for every distinct Java source.
222 // Each of the these compilation units can have several non-adjacent
223 // method ranges.
224
225 // Write .debug_line section.
226 std::vector<FileEntry> files;
227 std::unordered_map<std::string, size_t> files_map;
228 std::vector<std::string> directories;
229 std::unordered_map<std::string, size_t> directories_map;
230 int code_factor_bits_ = 0;
231 int dwarf_isa = -1;
232 switch (isa) {
233 case kArm: // arm actually means thumb2.
234 case kThumb2:
235 code_factor_bits_ = 1; // 16-bit instuctions
236 dwarf_isa = 1; // DW_ISA_ARM_thumb.
237 break;
238 case kArm64:
239 case kMips:
240 case kMips64:
241 code_factor_bits_ = 2; // 32-bit instructions
242 break;
243 case kNone:
244 case kX86:
245 case kX86_64:
246 break;
247 }
248 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
249 opcodes.SetAddress(text_section_offset + cunit_low_pc);
250 if (dwarf_isa != -1) {
251 opcodes.SetISA(dwarf_isa);
252 }
253 for (const OatWriter::DebugInfo& mi : method_infos) {
254 // Addresses in the line table should be unique and increasing.
255 if (mi.deduped_) {
256 continue;
257 }
258
259 struct DebugInfoCallbacks {
260 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
261 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
262 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
263 return false;
264 }
265 DefaultSrcMap dex2line_;
266 } debug_info_callbacks;
267
268 const DexFile* dex = mi.dex_file_;
269 if (mi.code_item_ != nullptr) {
270 dex->DecodeDebugInfo(mi.code_item_,
271 (mi.access_flags_ & kAccStatic) != 0,
272 mi.dex_method_index_,
273 DebugInfoCallbacks::NewPosition,
274 nullptr,
275 &debug_info_callbacks);
276 }
277
278 // Get and deduplicate directory and filename.
279 int file_index = 0; // 0 - primary source file of the compilation.
280 auto& dex_class_def = dex->GetClassDef(mi.class_def_index_);
281 const char* source_file = dex->GetSourceFile(dex_class_def);
282 if (source_file != nullptr) {
283 std::string file_name(source_file);
284 size_t file_name_slash = file_name.find_last_of('/');
285 std::string class_name(dex->GetClassDescriptor(dex_class_def));
286 size_t class_name_slash = class_name.find_last_of('/');
287 std::string full_path(file_name);
288
289 // Guess directory from package name.
290 int directory_index = 0; // 0 - current directory of the compilation.
291 if (file_name_slash == std::string::npos && // Just filename.
292 class_name.front() == 'L' && // Type descriptor for a class.
293 class_name_slash != std::string::npos) { // Has package name.
294 std::string package_name = class_name.substr(1, class_name_slash - 1);
295 auto it = directories_map.find(package_name);
296 if (it == directories_map.end()) {
297 directory_index = 1 + directories.size();
298 directories_map.emplace(package_name, directory_index);
299 directories.push_back(package_name);
300 } else {
301 directory_index = it->second;
302 }
303 full_path = package_name + "/" + file_name;
304 }
305
306 // Add file entry.
307 auto it2 = files_map.find(full_path);
308 if (it2 == files_map.end()) {
309 file_index = 1 + files.size();
310 files_map.emplace(full_path, file_index);
311 files.push_back(FileEntry {
312 file_name,
313 directory_index,
314 0, // Modification time - NA.
315 0, // File size - NA.
316 });
317 } else {
318 file_index = it2->second;
319 }
320 }
321 opcodes.SetFile(file_index);
322
323 // Generate mapping opcodes from PC to Java lines.
324 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
325 uint32_t low_pc = text_section_offset + mi.low_pc_;
326 if (file_index != 0 && !dex2line_map.empty()) {
327 bool first = true;
328 for (SrcMapElem pc2dex : mi.compiled_method_->GetSrcMappingTable()) {
329 uint32_t pc = pc2dex.from_;
330 int dex_pc = pc2dex.to_;
331 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
332 if (dex2line.first) {
333 int line = dex2line.second;
334 if (first) {
335 first = false;
336 if (pc > 0) {
337 // Assume that any preceding code is prologue.
338 int first_line = dex2line_map.front().to_;
339 // Prologue is not a sensible place for a breakpoint.
340 opcodes.NegateStmt();
341 opcodes.AddRow(low_pc, first_line);
342 opcodes.NegateStmt();
343 opcodes.SetPrologueEnd();
344 }
345 opcodes.AddRow(low_pc + pc, line);
346 } else if (line != opcodes.CurrentLine()) {
347 opcodes.AddRow(low_pc + pc, line);
348 }
349 }
350 }
351 } else {
352 // line 0 - instruction cannot be attributed to any source line.
353 opcodes.AddRow(low_pc, 0);
354 }
355 }
356 opcodes.AdvancePC(text_section_offset + cunit_high_pc);
357 opcodes.EndSequence();
David Srbecky2f6cdb02015-04-11 00:17:53 +0100358 auto* debug_line_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_line");
359 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100360}
361
362} // namespace dwarf
363} // namespace art