blob: a7adab5506d9b550ef3d22fa7c8257f89e53d7c1 [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
David Srbecky6a6b38f2016-03-11 14:35:45 +000020#include <unordered_set>
David Srbeckyc5bfa972016-02-05 15:49:10 +000021#include <vector>
22
David Srbeckyc5bfa972016-02-05 15:49:10 +000023#include "debug/dwarf/debug_line_opcode_writer.h"
24#include "debug/dwarf/headers.h"
25#include "debug/elf_compilation_unit.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010026#include "debug/src_map_elem.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file-inl.h"
Vladimir Marko74527972016-11-29 15:57:32 +000028#include "linker/elf_builder.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000029#include "oat_file.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000030#include "stack_map.h"
31
32namespace art {
33namespace debug {
34
35typedef std::vector<DexFile::PositionInfo> PositionInfos;
36
37static bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
38 static_cast<PositionInfos*>(ctx)->push_back(entry);
39 return false;
40}
41
42template<typename ElfTypes>
43class ElfDebugLineWriter {
44 using Elf_Addr = typename ElfTypes::Addr;
45
46 public:
Vladimir Marko74527972016-11-29 15:57:32 +000047 explicit ElfDebugLineWriter(linker::ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000048 }
49
50 void Start() {
51 builder_->GetDebugLine()->Start();
52 }
53
54 // Write line table for given set of methods.
55 // Returns the number of bytes written.
56 size_t WriteCompilationUnit(ElfCompilationUnit& compilation_unit) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080057 const InstructionSet isa = builder_->GetIsa();
58 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky197160d2016-03-07 17:33:57 +000059 const Elf_Addr base_address = compilation_unit.is_code_address_text_relative
David Srbeckyc5bfa972016-02-05 15:49:10 +000060 ? builder_->GetText()->GetAddress()
61 : 0;
62
David Srbeckye155f4b2017-12-06 15:18:38 +000063 compilation_unit.debug_line_offset = builder_->GetDebugLine()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +000064
65 std::vector<dwarf::FileEntry> files;
66 std::unordered_map<std::string, size_t> files_map;
67 std::vector<std::string> directories;
68 std::unordered_map<std::string, size_t> directories_map;
69 int code_factor_bits_ = 0;
70 int dwarf_isa = -1;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080071 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +000072 case InstructionSet::kArm: // arm actually means thumb2.
73 case InstructionSet::kThumb2:
David Srbeckyc5bfa972016-02-05 15:49:10 +000074 code_factor_bits_ = 1; // 16-bit instuctions
75 dwarf_isa = 1; // DW_ISA_ARM_thumb.
76 break;
Vladimir Marko33bff252017-11-01 14:35:42 +000077 case InstructionSet::kArm64:
78 case InstructionSet::kMips:
79 case InstructionSet::kMips64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000080 code_factor_bits_ = 2; // 32-bit instructions
81 break;
Vladimir Marko33bff252017-11-01 14:35:42 +000082 case InstructionSet::kNone:
83 case InstructionSet::kX86:
84 case InstructionSet::kX86_64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000085 break;
86 }
David Srbecky6a6b38f2016-03-11 14:35:45 +000087 std::unordered_set<uint64_t> seen_addresses(compilation_unit.methods.size());
David Srbeckyc5bfa972016-02-05 15:49:10 +000088 dwarf::DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
89 for (const MethodDebugInfo* mi : compilation_unit.methods) {
90 // Ignore function if we have already generated line table for the same address.
91 // It would confuse the debugger and the DWARF specification forbids it.
David Srbecky6a6b38f2016-03-11 14:35:45 +000092 // We allow the line table for method to be replicated in different compilation unit.
93 // This ensures that each compilation unit contains line table for all its methods.
94 if (!seen_addresses.insert(mi->code_address).second) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000095 continue;
96 }
97
David Srbecky99b87eb2016-02-09 18:16:35 +000098 uint32_t prologue_end = std::numeric_limits<uint32_t>::max();
David Srbecky197160d2016-03-07 17:33:57 +000099 std::vector<SrcMapElem> pc2dex_map;
100 if (mi->code_info != nullptr) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000101 // Use stack maps to create mapping table from pc to dex.
David Srbecky197160d2016-03-07 17:33:57 +0000102 const CodeInfo code_info(mi->code_info);
David Srbecky052f8ca2018-04-26 15:42:54 +0100103 pc2dex_map.reserve(code_info.GetNumberOfStackMaps());
104 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
105 StackMap stack_map = code_info.GetStackMapAt(s);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000106 DCHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +0100107 const uint32_t pc = stack_map.GetNativePcOffset(isa);
108 const int32_t dex = stack_map.GetDexPc();
David Srbecky197160d2016-03-07 17:33:57 +0000109 pc2dex_map.push_back({pc, dex});
David Srbecky052f8ca2018-04-26 15:42:54 +0100110 if (stack_map.HasDexRegisterMap()) {
David Srbecky99b87eb2016-02-09 18:16:35 +0000111 // Guess that the first map with local variables is the end of prologue.
112 prologue_end = std::min(prologue_end, pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000113 }
114 }
David Srbecky197160d2016-03-07 17:33:57 +0000115 std::sort(pc2dex_map.begin(), pc2dex_map.end());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000116 }
117
David Srbecky99b87eb2016-02-09 18:16:35 +0000118 if (pc2dex_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000119 continue;
120 }
121
David Srbecky252fa902016-03-11 14:25:00 +0000122 // Compensate for compiler's off-by-one-instruction error.
123 //
124 // The compiler generates stackmap with PC *after* the branch instruction
125 // (because this is the PC which is easier to obtain when unwinding).
126 //
127 // However, the debugger is more clever and it will ask us for line-number
128 // mapping at the location of the branch instruction (since the following
129 // instruction could belong to other line, this is the correct thing to do).
130 //
131 // So we really want to just decrement the PC by one instruction so that the
132 // branch instruction is covered as well. However, we do not know the size
133 // of the previous instruction, and we can not subtract just a fixed amount
134 // (the debugger would trust us that the PC is valid; it might try to set
135 // breakpoint there at some point, and setting breakpoint in mid-instruction
136 // would make the process crash in spectacular way).
137 //
138 // Therefore, we say that the PC which the compiler gave us for the stackmap
139 // is the end of its associated address range, and we use the PC from the
140 // previous stack map as the start of the range. This ensures that the PC is
141 // valid and that the branch instruction is covered.
142 //
143 // This ensures we have correct line number mapping at call sites (which is
144 // important for backtraces), but there is nothing we can do for non-call
145 // sites (so stepping through optimized code in debugger is not possible).
146 //
147 // We do not adjust the stackmaps if the code was compiled as debuggable.
148 // In that case, the stackmaps should accurately cover all instructions.
149 if (!mi->is_native_debuggable) {
150 for (size_t i = pc2dex_map.size() - 1; i > 0; --i) {
151 pc2dex_map[i].from_ = pc2dex_map[i - 1].from_;
152 }
153 pc2dex_map[0].from_ = 0;
154 }
155
David Srbecky197160d2016-03-07 17:33:57 +0000156 Elf_Addr method_address = base_address + mi->code_address;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000157
David Srbecky99b87eb2016-02-09 18:16:35 +0000158 PositionInfos dex2line_map;
David Srbecky09c2a6b2016-03-11 17:11:44 +0000159 DCHECK(mi->dex_file != nullptr);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000160 const DexFile* dex = mi->dex_file;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800161 CodeItemDebugInfoAccessor accessor(*dex, mi->code_item, mi->dex_method_index);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800162 const uint32_t debug_info_offset = accessor.DebugInfoOffset();
163 if (!dex->DecodeDebugPositionInfo(debug_info_offset, PositionInfoCallback, &dex2line_map)) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000164 continue;
165 }
166
David Srbecky99b87eb2016-02-09 18:16:35 +0000167 if (dex2line_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000168 continue;
169 }
170
171 opcodes.SetAddress(method_address);
172 if (dwarf_isa != -1) {
173 opcodes.SetISA(dwarf_isa);
174 }
175
176 // Get and deduplicate directory and filename.
177 int file_index = 0; // 0 - primary source file of the compilation.
178 auto& dex_class_def = dex->GetClassDef(mi->class_def_index);
179 const char* source_file = dex->GetSourceFile(dex_class_def);
180 if (source_file != nullptr) {
181 std::string file_name(source_file);
182 size_t file_name_slash = file_name.find_last_of('/');
183 std::string class_name(dex->GetClassDescriptor(dex_class_def));
184 size_t class_name_slash = class_name.find_last_of('/');
185 std::string full_path(file_name);
186
187 // Guess directory from package name.
188 int directory_index = 0; // 0 - current directory of the compilation.
189 if (file_name_slash == std::string::npos && // Just filename.
190 class_name.front() == 'L' && // Type descriptor for a class.
191 class_name_slash != std::string::npos) { // Has package name.
192 std::string package_name = class_name.substr(1, class_name_slash - 1);
193 auto it = directories_map.find(package_name);
194 if (it == directories_map.end()) {
195 directory_index = 1 + directories.size();
196 directories_map.emplace(package_name, directory_index);
197 directories.push_back(package_name);
198 } else {
199 directory_index = it->second;
200 }
201 full_path = package_name + "/" + file_name;
202 }
203
204 // Add file entry.
205 auto it2 = files_map.find(full_path);
206 if (it2 == files_map.end()) {
207 file_index = 1 + files.size();
208 files_map.emplace(full_path, file_index);
209 files.push_back(dwarf::FileEntry {
210 file_name,
211 directory_index,
212 0, // Modification time - NA.
213 0, // File size - NA.
214 });
215 } else {
216 file_index = it2->second;
217 }
218 }
219 opcodes.SetFile(file_index);
220
221 // Generate mapping opcodes from PC to Java lines.
222 if (file_index != 0) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000223 // If the method was not compiled as native-debuggable, we still generate all available
224 // lines, but we try to prevent the debugger from stepping and setting breakpoints since
225 // the information is too inaccurate for that (breakpoints would be set after the calls).
226 const bool default_is_stmt = mi->is_native_debuggable;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000227 bool first = true;
David Srbecky99b87eb2016-02-09 18:16:35 +0000228 for (SrcMapElem pc2dex : pc2dex_map) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000229 uint32_t pc = pc2dex.from_;
230 int dex_pc = pc2dex.to_;
231 // Find mapping with address with is greater than our dex pc; then go back one step.
David Srbecky99b87eb2016-02-09 18:16:35 +0000232 auto dex2line = std::upper_bound(
233 dex2line_map.begin(),
234 dex2line_map.end(),
235 dex_pc,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000236 [](uint32_t address, const DexFile::PositionInfo& entry) {
237 return address < entry.address_;
238 });
David Srbecky99b87eb2016-02-09 18:16:35 +0000239 // Look for first valid mapping after the prologue.
240 if (dex2line != dex2line_map.begin() && pc >= prologue_end) {
241 int line = (--dex2line)->line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000242 if (first) {
243 first = false;
244 if (pc > 0) {
245 // Assume that any preceding code is prologue.
David Srbecky99b87eb2016-02-09 18:16:35 +0000246 int first_line = dex2line_map.front().line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000247 // Prologue is not a sensible place for a breakpoint.
David Srbecky91cc06c2016-03-07 16:13:58 +0000248 opcodes.SetIsStmt(false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000249 opcodes.AddRow(method_address, first_line);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000250 opcodes.SetPrologueEnd();
251 }
David Srbecky91cc06c2016-03-07 16:13:58 +0000252 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000253 opcodes.AddRow(method_address + pc, line);
254 } else if (line != opcodes.CurrentLine()) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000255 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000256 opcodes.AddRow(method_address + pc, line);
257 }
258 }
259 }
260 } else {
261 // line 0 - instruction cannot be attributed to any source line.
262 opcodes.AddRow(method_address, 0);
263 }
264
David Srbecky197160d2016-03-07 17:33:57 +0000265 opcodes.AdvancePC(method_address + mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000266 opcodes.EndSequence();
267 }
268 std::vector<uint8_t> buffer;
269 buffer.reserve(opcodes.data()->size() + KB);
David Srbeckye155f4b2017-12-06 15:18:38 +0000270 size_t offset = builder_->GetDebugLine()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000271 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_);
272 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
273 return buffer.size();
274 }
275
276 void End(bool write_oat_patches) {
277 builder_->GetDebugLine()->End();
278 if (write_oat_patches) {
279 builder_->WritePatches(".debug_line.oat_patches",
280 ArrayRef<const uintptr_t>(debug_line_patches_));
281 }
282 }
283
284 private:
Vladimir Marko74527972016-11-29 15:57:32 +0000285 linker::ElfBuilder<ElfTypes>* builder_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000286 std::vector<uintptr_t> debug_line_patches_;
287};
288
289} // namespace debug
290} // namespace art
291
292#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
293