blob: 0d3673d3ccfbb16441cb0a9cf6a8368408c1f427 [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 Srbecky252fa902016-03-11 14:25:00 +0000117 // Compensate for compiler's off-by-one-instruction error.
118 //
119 // The compiler generates stackmap with PC *after* the branch instruction
120 // (because this is the PC which is easier to obtain when unwinding).
121 //
122 // However, the debugger is more clever and it will ask us for line-number
123 // mapping at the location of the branch instruction (since the following
124 // instruction could belong to other line, this is the correct thing to do).
125 //
126 // So we really want to just decrement the PC by one instruction so that the
127 // branch instruction is covered as well. However, we do not know the size
128 // of the previous instruction, and we can not subtract just a fixed amount
129 // (the debugger would trust us that the PC is valid; it might try to set
130 // breakpoint there at some point, and setting breakpoint in mid-instruction
131 // would make the process crash in spectacular way).
132 //
133 // Therefore, we say that the PC which the compiler gave us for the stackmap
134 // is the end of its associated address range, and we use the PC from the
135 // previous stack map as the start of the range. This ensures that the PC is
136 // valid and that the branch instruction is covered.
137 //
138 // This ensures we have correct line number mapping at call sites (which is
139 // important for backtraces), but there is nothing we can do for non-call
140 // sites (so stepping through optimized code in debugger is not possible).
141 //
142 // We do not adjust the stackmaps if the code was compiled as debuggable.
143 // In that case, the stackmaps should accurately cover all instructions.
144 if (!mi->is_native_debuggable) {
145 for (size_t i = pc2dex_map.size() - 1; i > 0; --i) {
146 pc2dex_map[i].from_ = pc2dex_map[i - 1].from_;
147 }
148 pc2dex_map[0].from_ = 0;
149 }
150
David Srbecky197160d2016-03-07 17:33:57 +0000151 Elf_Addr method_address = base_address + mi->code_address;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000152
David Srbecky99b87eb2016-02-09 18:16:35 +0000153 PositionInfos dex2line_map;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000154 const DexFile* dex = mi->dex_file;
David Srbecky99b87eb2016-02-09 18:16:35 +0000155 if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000156 continue;
157 }
158
David Srbecky99b87eb2016-02-09 18:16:35 +0000159 if (dex2line_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000160 continue;
161 }
162
163 opcodes.SetAddress(method_address);
164 if (dwarf_isa != -1) {
165 opcodes.SetISA(dwarf_isa);
166 }
167
168 // Get and deduplicate directory and filename.
169 int file_index = 0; // 0 - primary source file of the compilation.
170 auto& dex_class_def = dex->GetClassDef(mi->class_def_index);
171 const char* source_file = dex->GetSourceFile(dex_class_def);
172 if (source_file != nullptr) {
173 std::string file_name(source_file);
174 size_t file_name_slash = file_name.find_last_of('/');
175 std::string class_name(dex->GetClassDescriptor(dex_class_def));
176 size_t class_name_slash = class_name.find_last_of('/');
177 std::string full_path(file_name);
178
179 // Guess directory from package name.
180 int directory_index = 0; // 0 - current directory of the compilation.
181 if (file_name_slash == std::string::npos && // Just filename.
182 class_name.front() == 'L' && // Type descriptor for a class.
183 class_name_slash != std::string::npos) { // Has package name.
184 std::string package_name = class_name.substr(1, class_name_slash - 1);
185 auto it = directories_map.find(package_name);
186 if (it == directories_map.end()) {
187 directory_index = 1 + directories.size();
188 directories_map.emplace(package_name, directory_index);
189 directories.push_back(package_name);
190 } else {
191 directory_index = it->second;
192 }
193 full_path = package_name + "/" + file_name;
194 }
195
196 // Add file entry.
197 auto it2 = files_map.find(full_path);
198 if (it2 == files_map.end()) {
199 file_index = 1 + files.size();
200 files_map.emplace(full_path, file_index);
201 files.push_back(dwarf::FileEntry {
202 file_name,
203 directory_index,
204 0, // Modification time - NA.
205 0, // File size - NA.
206 });
207 } else {
208 file_index = it2->second;
209 }
210 }
211 opcodes.SetFile(file_index);
212
213 // Generate mapping opcodes from PC to Java lines.
214 if (file_index != 0) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000215 // If the method was not compiled as native-debuggable, we still generate all available
216 // lines, but we try to prevent the debugger from stepping and setting breakpoints since
217 // the information is too inaccurate for that (breakpoints would be set after the calls).
218 const bool default_is_stmt = mi->is_native_debuggable;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000219 bool first = true;
David Srbecky99b87eb2016-02-09 18:16:35 +0000220 for (SrcMapElem pc2dex : pc2dex_map) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000221 uint32_t pc = pc2dex.from_;
222 int dex_pc = pc2dex.to_;
223 // Find mapping with address with is greater than our dex pc; then go back one step.
David Srbecky99b87eb2016-02-09 18:16:35 +0000224 auto dex2line = std::upper_bound(
225 dex2line_map.begin(),
226 dex2line_map.end(),
227 dex_pc,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000228 [](uint32_t address, const DexFile::PositionInfo& entry) {
229 return address < entry.address_;
230 });
David Srbecky99b87eb2016-02-09 18:16:35 +0000231 // Look for first valid mapping after the prologue.
232 if (dex2line != dex2line_map.begin() && pc >= prologue_end) {
233 int line = (--dex2line)->line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000234 if (first) {
235 first = false;
236 if (pc > 0) {
237 // Assume that any preceding code is prologue.
David Srbecky99b87eb2016-02-09 18:16:35 +0000238 int first_line = dex2line_map.front().line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000239 // Prologue is not a sensible place for a breakpoint.
David Srbecky91cc06c2016-03-07 16:13:58 +0000240 opcodes.SetIsStmt(false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000241 opcodes.AddRow(method_address, first_line);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000242 opcodes.SetPrologueEnd();
243 }
David Srbecky91cc06c2016-03-07 16:13:58 +0000244 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000245 opcodes.AddRow(method_address + pc, line);
246 } else if (line != opcodes.CurrentLine()) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000247 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000248 opcodes.AddRow(method_address + pc, line);
249 }
250 }
251 }
252 } else {
253 // line 0 - instruction cannot be attributed to any source line.
254 opcodes.AddRow(method_address, 0);
255 }
256
David Srbecky197160d2016-03-07 17:33:57 +0000257 opcodes.AdvancePC(method_address + mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000258 opcodes.EndSequence();
259 }
260 std::vector<uint8_t> buffer;
261 buffer.reserve(opcodes.data()->size() + KB);
262 size_t offset = builder_->GetDebugLine()->GetSize();
263 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_);
264 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
265 return buffer.size();
266 }
267
268 void End(bool write_oat_patches) {
269 builder_->GetDebugLine()->End();
270 if (write_oat_patches) {
271 builder_->WritePatches(".debug_line.oat_patches",
272 ArrayRef<const uintptr_t>(debug_line_patches_));
273 }
274 }
275
276 private:
277 ElfBuilder<ElfTypes>* builder_;
278 std::vector<uintptr_t> debug_line_patches_;
279};
280
281} // namespace debug
282} // namespace art
283
284#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
285