blob: 9875c71080d31c6a2bafc730983d5a41b710b84f [file] [log] [blame]
David Srbeckyc5bfa972016-02-05 15:49:10 +00001/*
2 * Copyright (C) 2016 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#ifndef ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
18#define ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
19
20#include <vector>
21
22#include "compiled_method.h"
23#include "debug/dwarf/debug_line_opcode_writer.h"
24#include "debug/dwarf/headers.h"
25#include "debug/elf_compilation_unit.h"
26#include "dex_file-inl.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000027#include "elf_builder.h"
28#include "stack_map.h"
29
30namespace art {
31namespace debug {
32
33typedef std::vector<DexFile::PositionInfo> PositionInfos;
34
35static bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
36 static_cast<PositionInfos*>(ctx)->push_back(entry);
37 return false;
38}
39
40template<typename ElfTypes>
41class ElfDebugLineWriter {
42 using Elf_Addr = typename ElfTypes::Addr;
43
44 public:
45 explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
46 }
47
48 void Start() {
49 builder_->GetDebugLine()->Start();
50 }
51
52 // Write line table for given set of methods.
53 // Returns the number of bytes written.
54 size_t WriteCompilationUnit(ElfCompilationUnit& compilation_unit) {
55 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
David Srbecky197160d2016-03-07 17:33:57 +000056 const Elf_Addr base_address = compilation_unit.is_code_address_text_relative
David Srbeckyc5bfa972016-02-05 15:49:10 +000057 ? builder_->GetText()->GetAddress()
58 : 0;
59
60 compilation_unit.debug_line_offset = builder_->GetDebugLine()->GetSize();
61
62 std::vector<dwarf::FileEntry> files;
63 std::unordered_map<std::string, size_t> files_map;
64 std::vector<std::string> directories;
65 std::unordered_map<std::string, size_t> directories_map;
66 int code_factor_bits_ = 0;
67 int dwarf_isa = -1;
68 switch (builder_->GetIsa()) {
69 case kArm: // arm actually means thumb2.
70 case kThumb2:
71 code_factor_bits_ = 1; // 16-bit instuctions
72 dwarf_isa = 1; // DW_ISA_ARM_thumb.
73 break;
74 case kArm64:
75 case kMips:
76 case kMips64:
77 code_factor_bits_ = 2; // 32-bit instructions
78 break;
79 case kNone:
80 case kX86:
81 case kX86_64:
82 break;
83 }
84 dwarf::DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
85 for (const MethodDebugInfo* mi : compilation_unit.methods) {
86 // Ignore function if we have already generated line table for the same address.
87 // It would confuse the debugger and the DWARF specification forbids it.
David Srbecky8862fac2016-03-11 14:34:47 +000088 if (mi->deduped) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000089 continue;
90 }
91
David Srbecky99b87eb2016-02-09 18:16:35 +000092 uint32_t prologue_end = std::numeric_limits<uint32_t>::max();
David Srbecky197160d2016-03-07 17:33:57 +000093 std::vector<SrcMapElem> pc2dex_map;
94 if (mi->code_info != nullptr) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000095 // Use stack maps to create mapping table from pc to dex.
David Srbecky197160d2016-03-07 17:33:57 +000096 const CodeInfo code_info(mi->code_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +000097 const StackMapEncoding encoding = code_info.ExtractEncoding();
David Srbecky197160d2016-03-07 17:33:57 +000098 pc2dex_map.reserve(code_info.GetNumberOfStackMaps());
David Srbeckyc5bfa972016-02-05 15:49:10 +000099 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
100 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
101 DCHECK(stack_map.IsValid());
David Srbecky99b87eb2016-02-09 18:16:35 +0000102 const uint32_t pc = stack_map.GetNativePcOffset(encoding);
103 const int32_t dex = stack_map.GetDexPc(encoding);
David Srbecky197160d2016-03-07 17:33:57 +0000104 pc2dex_map.push_back({pc, dex});
David Srbeckyc5bfa972016-02-05 15:49:10 +0000105 if (stack_map.HasDexRegisterMap(encoding)) {
David Srbecky99b87eb2016-02-09 18:16:35 +0000106 // Guess that the first map with local variables is the end of prologue.
107 prologue_end = std::min(prologue_end, pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000108 }
109 }
David Srbecky197160d2016-03-07 17:33:57 +0000110 std::sort(pc2dex_map.begin(), pc2dex_map.end());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000111 }
112
David Srbecky99b87eb2016-02-09 18:16:35 +0000113 if (pc2dex_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000114 continue;
115 }
116
David Srbecky197160d2016-03-07 17:33:57 +0000117 Elf_Addr method_address = base_address + mi->code_address;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000118
David Srbecky99b87eb2016-02-09 18:16:35 +0000119 PositionInfos dex2line_map;
David Srbecky09c2a6b2016-03-11 17:11:44 +0000120 DCHECK(mi->dex_file != nullptr);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000121 const DexFile* dex = mi->dex_file;
David Srbecky99b87eb2016-02-09 18:16:35 +0000122 if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000123 continue;
124 }
125
David Srbecky99b87eb2016-02-09 18:16:35 +0000126 if (dex2line_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000127 continue;
128 }
129
130 opcodes.SetAddress(method_address);
131 if (dwarf_isa != -1) {
132 opcodes.SetISA(dwarf_isa);
133 }
134
135 // Get and deduplicate directory and filename.
136 int file_index = 0; // 0 - primary source file of the compilation.
137 auto& dex_class_def = dex->GetClassDef(mi->class_def_index);
138 const char* source_file = dex->GetSourceFile(dex_class_def);
139 if (source_file != nullptr) {
140 std::string file_name(source_file);
141 size_t file_name_slash = file_name.find_last_of('/');
142 std::string class_name(dex->GetClassDescriptor(dex_class_def));
143 size_t class_name_slash = class_name.find_last_of('/');
144 std::string full_path(file_name);
145
146 // Guess directory from package name.
147 int directory_index = 0; // 0 - current directory of the compilation.
148 if (file_name_slash == std::string::npos && // Just filename.
149 class_name.front() == 'L' && // Type descriptor for a class.
150 class_name_slash != std::string::npos) { // Has package name.
151 std::string package_name = class_name.substr(1, class_name_slash - 1);
152 auto it = directories_map.find(package_name);
153 if (it == directories_map.end()) {
154 directory_index = 1 + directories.size();
155 directories_map.emplace(package_name, directory_index);
156 directories.push_back(package_name);
157 } else {
158 directory_index = it->second;
159 }
160 full_path = package_name + "/" + file_name;
161 }
162
163 // Add file entry.
164 auto it2 = files_map.find(full_path);
165 if (it2 == files_map.end()) {
166 file_index = 1 + files.size();
167 files_map.emplace(full_path, file_index);
168 files.push_back(dwarf::FileEntry {
169 file_name,
170 directory_index,
171 0, // Modification time - NA.
172 0, // File size - NA.
173 });
174 } else {
175 file_index = it2->second;
176 }
177 }
178 opcodes.SetFile(file_index);
179
180 // Generate mapping opcodes from PC to Java lines.
181 if (file_index != 0) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000182 // If the method was not compiled as native-debuggable, we still generate all available
183 // lines, but we try to prevent the debugger from stepping and setting breakpoints since
184 // the information is too inaccurate for that (breakpoints would be set after the calls).
185 const bool default_is_stmt = mi->is_native_debuggable;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000186 bool first = true;
David Srbecky99b87eb2016-02-09 18:16:35 +0000187 for (SrcMapElem pc2dex : pc2dex_map) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000188 uint32_t pc = pc2dex.from_;
189 int dex_pc = pc2dex.to_;
190 // Find mapping with address with is greater than our dex pc; then go back one step.
David Srbecky99b87eb2016-02-09 18:16:35 +0000191 auto dex2line = std::upper_bound(
192 dex2line_map.begin(),
193 dex2line_map.end(),
194 dex_pc,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000195 [](uint32_t address, const DexFile::PositionInfo& entry) {
196 return address < entry.address_;
197 });
David Srbecky99b87eb2016-02-09 18:16:35 +0000198 // Look for first valid mapping after the prologue.
199 if (dex2line != dex2line_map.begin() && pc >= prologue_end) {
200 int line = (--dex2line)->line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000201 if (first) {
202 first = false;
203 if (pc > 0) {
204 // Assume that any preceding code is prologue.
David Srbecky99b87eb2016-02-09 18:16:35 +0000205 int first_line = dex2line_map.front().line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000206 // Prologue is not a sensible place for a breakpoint.
David Srbecky91cc06c2016-03-07 16:13:58 +0000207 opcodes.SetIsStmt(false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000208 opcodes.AddRow(method_address, first_line);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000209 opcodes.SetPrologueEnd();
210 }
David Srbecky91cc06c2016-03-07 16:13:58 +0000211 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000212 opcodes.AddRow(method_address + pc, line);
213 } else if (line != opcodes.CurrentLine()) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000214 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000215 opcodes.AddRow(method_address + pc, line);
216 }
217 }
218 }
219 } else {
220 // line 0 - instruction cannot be attributed to any source line.
221 opcodes.AddRow(method_address, 0);
222 }
223
David Srbecky197160d2016-03-07 17:33:57 +0000224 opcodes.AdvancePC(method_address + mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000225 opcodes.EndSequence();
226 }
227 std::vector<uint8_t> buffer;
228 buffer.reserve(opcodes.data()->size() + KB);
229 size_t offset = builder_->GetDebugLine()->GetSize();
230 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_);
231 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
232 return buffer.size();
233 }
234
235 void End(bool write_oat_patches) {
236 builder_->GetDebugLine()->End();
237 if (write_oat_patches) {
238 builder_->WritePatches(".debug_line.oat_patches",
239 ArrayRef<const uintptr_t>(debug_line_patches_));
240 }
241 }
242
243 private:
244 ElfBuilder<ElfTypes>* builder_;
245 std::vector<uintptr_t> debug_line_patches_;
246};
247
248} // namespace debug
249} // namespace art
250
251#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
252