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