blob: ed26d966031a8aa65321680fd2b477b513b6f08b [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.
88 if (mi->deduped) {
89 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 Srbeckyc5bfa972016-02-05 15:49:10 +0000120 const DexFile* dex = mi->dex_file;
David Srbecky99b87eb2016-02-09 18:16:35 +0000121 if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000122 continue;
123 }
124
David Srbecky99b87eb2016-02-09 18:16:35 +0000125 if (dex2line_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000126 continue;
127 }
128
129 opcodes.SetAddress(method_address);
130 if (dwarf_isa != -1) {
131 opcodes.SetISA(dwarf_isa);
132 }
133
134 // Get and deduplicate directory and filename.
135 int file_index = 0; // 0 - primary source file of the compilation.
136 auto& dex_class_def = dex->GetClassDef(mi->class_def_index);
137 const char* source_file = dex->GetSourceFile(dex_class_def);
138 if (source_file != nullptr) {
139 std::string file_name(source_file);
140 size_t file_name_slash = file_name.find_last_of('/');
141 std::string class_name(dex->GetClassDescriptor(dex_class_def));
142 size_t class_name_slash = class_name.find_last_of('/');
143 std::string full_path(file_name);
144
145 // Guess directory from package name.
146 int directory_index = 0; // 0 - current directory of the compilation.
147 if (file_name_slash == std::string::npos && // Just filename.
148 class_name.front() == 'L' && // Type descriptor for a class.
149 class_name_slash != std::string::npos) { // Has package name.
150 std::string package_name = class_name.substr(1, class_name_slash - 1);
151 auto it = directories_map.find(package_name);
152 if (it == directories_map.end()) {
153 directory_index = 1 + directories.size();
154 directories_map.emplace(package_name, directory_index);
155 directories.push_back(package_name);
156 } else {
157 directory_index = it->second;
158 }
159 full_path = package_name + "/" + file_name;
160 }
161
162 // Add file entry.
163 auto it2 = files_map.find(full_path);
164 if (it2 == files_map.end()) {
165 file_index = 1 + files.size();
166 files_map.emplace(full_path, file_index);
167 files.push_back(dwarf::FileEntry {
168 file_name,
169 directory_index,
170 0, // Modification time - NA.
171 0, // File size - NA.
172 });
173 } else {
174 file_index = it2->second;
175 }
176 }
177 opcodes.SetFile(file_index);
178
179 // Generate mapping opcodes from PC to Java lines.
180 if (file_index != 0) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000181 // If the method was not compiled as native-debuggable, we still generate all available
182 // lines, but we try to prevent the debugger from stepping and setting breakpoints since
183 // the information is too inaccurate for that (breakpoints would be set after the calls).
184 const bool default_is_stmt = mi->is_native_debuggable;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000185 bool first = true;
David Srbecky99b87eb2016-02-09 18:16:35 +0000186 for (SrcMapElem pc2dex : pc2dex_map) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000187 uint32_t pc = pc2dex.from_;
188 int dex_pc = pc2dex.to_;
189 // Find mapping with address with is greater than our dex pc; then go back one step.
David Srbecky99b87eb2016-02-09 18:16:35 +0000190 auto dex2line = std::upper_bound(
191 dex2line_map.begin(),
192 dex2line_map.end(),
193 dex_pc,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000194 [](uint32_t address, const DexFile::PositionInfo& entry) {
195 return address < entry.address_;
196 });
David Srbecky99b87eb2016-02-09 18:16:35 +0000197 // Look for first valid mapping after the prologue.
198 if (dex2line != dex2line_map.begin() && pc >= prologue_end) {
199 int line = (--dex2line)->line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000200 if (first) {
201 first = false;
202 if (pc > 0) {
203 // Assume that any preceding code is prologue.
David Srbecky99b87eb2016-02-09 18:16:35 +0000204 int first_line = dex2line_map.front().line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000205 // Prologue is not a sensible place for a breakpoint.
David Srbecky91cc06c2016-03-07 16:13:58 +0000206 opcodes.SetIsStmt(false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000207 opcodes.AddRow(method_address, first_line);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000208 opcodes.SetPrologueEnd();
209 }
David Srbecky91cc06c2016-03-07 16:13:58 +0000210 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000211 opcodes.AddRow(method_address + pc, line);
212 } else if (line != opcodes.CurrentLine()) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000213 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000214 opcodes.AddRow(method_address + pc, line);
215 }
216 }
217 }
218 } else {
219 // line 0 - instruction cannot be attributed to any source line.
220 opcodes.AddRow(method_address, 0);
221 }
222
David Srbecky197160d2016-03-07 17:33:57 +0000223 opcodes.AdvancePC(method_address + mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000224 opcodes.EndSequence();
225 }
226 std::vector<uint8_t> buffer;
227 buffer.reserve(opcodes.data()->size() + KB);
228 size_t offset = builder_->GetDebugLine()->GetSize();
229 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_);
230 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
231 return buffer.size();
232 }
233
234 void End(bool write_oat_patches) {
235 builder_->GetDebugLine()->End();
236 if (write_oat_patches) {
237 builder_->WritePatches(".debug_line.oat_patches",
238 ArrayRef<const uintptr_t>(debug_line_patches_));
239 }
240 }
241
242 private:
243 ElfBuilder<ElfTypes>* builder_;
244 std::vector<uintptr_t> debug_line_patches_;
245};
246
247} // namespace debug
248} // namespace art
249
250#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
251