blob: 6fe05a483d26af57e47098e04db47add14e23afa [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();
191 uint32_t cunit_low_pc = static_cast<uint32_t>(-1);
192 uint32_t cunit_high_pc = 0;
193 for (auto method_info : method_infos) {
194 cunit_low_pc = std::min(cunit_low_pc, method_info.low_pc_);
195 cunit_high_pc = std::max(cunit_high_pc, method_info.high_pc_);
196 }
197
David Srbecky626a1662015-04-12 13:12:26 +0100198 // Find all addresses (low_pc) which contain deduped methods.
199 // The first instance of method is not marked deduped_, but the rest is.
200 std::unordered_set<uint32_t> deduped_addresses;
201 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
202 if (it->deduped_) {
203 deduped_addresses.insert(it->low_pc_);
204 }
205 }
206
David Srbecky3b9d57a2015-04-10 00:22:14 +0100207 // Write .debug_info section.
208 size_t debug_abbrev_offset = debug_abbrev->size();
209 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
210 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
211 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
212 info.WriteData1(DW_AT_language, DW_LANG_Java);
213 info.WriteAddr(DW_AT_low_pc, cunit_low_pc + text_section_offset);
214 info.WriteAddr(DW_AT_high_pc, cunit_high_pc + text_section_offset);
215 info.WriteData4(DW_AT_stmt_list, debug_line->size());
216 for (auto method_info : method_infos) {
217 std::string method_name = PrettyMethod(method_info.dex_method_index_,
218 *method_info.dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100219 if (deduped_addresses.find(method_info.low_pc_) != deduped_addresses.end()) {
220 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100221 }
222 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
223 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
224 info.WriteAddr(DW_AT_low_pc, method_info.low_pc_ + text_section_offset);
225 info.WriteAddr(DW_AT_high_pc, method_info.high_pc_ + text_section_offset);
226 info.EndTag(); // DW_TAG_subprogram
227 }
228 info.EndTag(); // DW_TAG_compile_unit
David Srbecky2f6cdb02015-04-11 00:17:53 +0100229 auto* debug_info_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_info");
230 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100231
232 // TODO: in gdb info functions <regexp> - reports Java functions, but
233 // source file is <unknown> because .debug_line is formed as one
234 // compilation unit. To fix this it is possible to generate
235 // a separate compilation unit for every distinct Java source.
236 // Each of the these compilation units can have several non-adjacent
237 // method ranges.
238
239 // Write .debug_line section.
240 std::vector<FileEntry> files;
241 std::unordered_map<std::string, size_t> files_map;
242 std::vector<std::string> directories;
243 std::unordered_map<std::string, size_t> directories_map;
244 int code_factor_bits_ = 0;
245 int dwarf_isa = -1;
246 switch (isa) {
247 case kArm: // arm actually means thumb2.
248 case kThumb2:
249 code_factor_bits_ = 1; // 16-bit instuctions
250 dwarf_isa = 1; // DW_ISA_ARM_thumb.
251 break;
252 case kArm64:
253 case kMips:
254 case kMips64:
255 code_factor_bits_ = 2; // 32-bit instructions
256 break;
257 case kNone:
258 case kX86:
259 case kX86_64:
260 break;
261 }
262 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
263 opcodes.SetAddress(text_section_offset + cunit_low_pc);
264 if (dwarf_isa != -1) {
265 opcodes.SetISA(dwarf_isa);
266 }
267 for (const OatWriter::DebugInfo& mi : method_infos) {
268 // Addresses in the line table should be unique and increasing.
269 if (mi.deduped_) {
270 continue;
271 }
272
273 struct DebugInfoCallbacks {
274 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
275 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
276 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
277 return false;
278 }
279 DefaultSrcMap dex2line_;
280 } debug_info_callbacks;
281
282 const DexFile* dex = mi.dex_file_;
283 if (mi.code_item_ != nullptr) {
284 dex->DecodeDebugInfo(mi.code_item_,
285 (mi.access_flags_ & kAccStatic) != 0,
286 mi.dex_method_index_,
287 DebugInfoCallbacks::NewPosition,
288 nullptr,
289 &debug_info_callbacks);
290 }
291
292 // Get and deduplicate directory and filename.
293 int file_index = 0; // 0 - primary source file of the compilation.
294 auto& dex_class_def = dex->GetClassDef(mi.class_def_index_);
295 const char* source_file = dex->GetSourceFile(dex_class_def);
296 if (source_file != nullptr) {
297 std::string file_name(source_file);
298 size_t file_name_slash = file_name.find_last_of('/');
299 std::string class_name(dex->GetClassDescriptor(dex_class_def));
300 size_t class_name_slash = class_name.find_last_of('/');
301 std::string full_path(file_name);
302
303 // Guess directory from package name.
304 int directory_index = 0; // 0 - current directory of the compilation.
305 if (file_name_slash == std::string::npos && // Just filename.
306 class_name.front() == 'L' && // Type descriptor for a class.
307 class_name_slash != std::string::npos) { // Has package name.
308 std::string package_name = class_name.substr(1, class_name_slash - 1);
309 auto it = directories_map.find(package_name);
310 if (it == directories_map.end()) {
311 directory_index = 1 + directories.size();
312 directories_map.emplace(package_name, directory_index);
313 directories.push_back(package_name);
314 } else {
315 directory_index = it->second;
316 }
317 full_path = package_name + "/" + file_name;
318 }
319
320 // Add file entry.
321 auto it2 = files_map.find(full_path);
322 if (it2 == files_map.end()) {
323 file_index = 1 + files.size();
324 files_map.emplace(full_path, file_index);
325 files.push_back(FileEntry {
326 file_name,
327 directory_index,
328 0, // Modification time - NA.
329 0, // File size - NA.
330 });
331 } else {
332 file_index = it2->second;
333 }
334 }
335 opcodes.SetFile(file_index);
336
337 // Generate mapping opcodes from PC to Java lines.
338 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
339 uint32_t low_pc = text_section_offset + mi.low_pc_;
340 if (file_index != 0 && !dex2line_map.empty()) {
341 bool first = true;
342 for (SrcMapElem pc2dex : mi.compiled_method_->GetSrcMappingTable()) {
343 uint32_t pc = pc2dex.from_;
344 int dex_pc = pc2dex.to_;
345 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
346 if (dex2line.first) {
347 int line = dex2line.second;
348 if (first) {
349 first = false;
350 if (pc > 0) {
351 // Assume that any preceding code is prologue.
352 int first_line = dex2line_map.front().to_;
353 // Prologue is not a sensible place for a breakpoint.
354 opcodes.NegateStmt();
355 opcodes.AddRow(low_pc, first_line);
356 opcodes.NegateStmt();
357 opcodes.SetPrologueEnd();
358 }
359 opcodes.AddRow(low_pc + pc, line);
360 } else if (line != opcodes.CurrentLine()) {
361 opcodes.AddRow(low_pc + pc, line);
362 }
363 }
364 }
365 } else {
366 // line 0 - instruction cannot be attributed to any source line.
367 opcodes.AddRow(low_pc, 0);
368 }
369 }
370 opcodes.AdvancePC(text_section_offset + cunit_high_pc);
371 opcodes.EndSequence();
David Srbecky2f6cdb02015-04-11 00:17:53 +0100372 auto* debug_line_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_line");
373 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100374}
375
376} // namespace dwarf
377} // namespace art