David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 1 | /* |
| 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 Srbecky | 6a6b38f | 2016-03-11 14:35:45 +0000 | [diff] [blame] | 20 | #include <unordered_set> |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 23 | #include "debug/dwarf/debug_line_opcode_writer.h" |
| 24 | #include "debug/dwarf/headers.h" |
| 25 | #include "debug/elf_compilation_unit.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 26 | #include "debug/src_map_elem.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 27 | #include "dex/dex_file-inl.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 28 | #include "linker/elf_builder.h" |
Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 29 | #include "oat_file.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 30 | #include "stack_map.h" |
| 31 | |
| 32 | namespace art { |
| 33 | namespace debug { |
| 34 | |
| 35 | typedef std::vector<DexFile::PositionInfo> PositionInfos; |
| 36 | |
| 37 | static bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { |
| 38 | static_cast<PositionInfos*>(ctx)->push_back(entry); |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | template<typename ElfTypes> |
| 43 | class ElfDebugLineWriter { |
| 44 | using Elf_Addr = typename ElfTypes::Addr; |
| 45 | |
| 46 | public: |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 47 | explicit ElfDebugLineWriter(linker::ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 48 | } |
| 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 Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 57 | const InstructionSet isa = builder_->GetIsa(); |
| 58 | const bool is64bit = Is64BitInstructionSet(isa); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 59 | const Elf_Addr base_address = compilation_unit.is_code_address_text_relative |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 60 | ? builder_->GetText()->GetAddress() |
| 61 | : 0; |
| 62 | |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 63 | compilation_unit.debug_line_offset = builder_->GetDebugLine()->GetPosition(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 64 | |
| 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 Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 71 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 72 | case InstructionSet::kArm: // arm actually means thumb2. |
| 73 | case InstructionSet::kThumb2: |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 74 | code_factor_bits_ = 1; // 16-bit instuctions |
| 75 | dwarf_isa = 1; // DW_ISA_ARM_thumb. |
| 76 | break; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 77 | case InstructionSet::kArm64: |
| 78 | case InstructionSet::kMips: |
| 79 | case InstructionSet::kMips64: |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 80 | code_factor_bits_ = 2; // 32-bit instructions |
| 81 | break; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 82 | case InstructionSet::kNone: |
| 83 | case InstructionSet::kX86: |
| 84 | case InstructionSet::kX86_64: |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 85 | break; |
| 86 | } |
David Srbecky | 6a6b38f | 2016-03-11 14:35:45 +0000 | [diff] [blame] | 87 | std::unordered_set<uint64_t> seen_addresses(compilation_unit.methods.size()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 88 | 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 Srbecky | 6a6b38f | 2016-03-11 14:35:45 +0000 | [diff] [blame] | 92 | // 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 Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 95 | continue; |
| 96 | } |
| 97 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 98 | uint32_t prologue_end = std::numeric_limits<uint32_t>::max(); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 99 | std::vector<SrcMapElem> pc2dex_map; |
| 100 | if (mi->code_info != nullptr) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 101 | // Use stack maps to create mapping table from pc to dex. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 102 | const CodeInfo code_info(mi->code_info); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 103 | const CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
| 104 | pc2dex_map.reserve(code_info.GetNumberOfStackMaps(encoding)); |
| 105 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(encoding); s++) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 106 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 107 | DCHECK(stack_map.IsValid()); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 108 | const uint32_t pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, isa); |
| 109 | const int32_t dex = stack_map.GetDexPc(encoding.stack_map.encoding); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 110 | pc2dex_map.push_back({pc, dex}); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 111 | if (stack_map.HasDexRegisterMap(encoding.stack_map.encoding)) { |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 112 | // Guess that the first map with local variables is the end of prologue. |
| 113 | prologue_end = std::min(prologue_end, pc); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 116 | std::sort(pc2dex_map.begin(), pc2dex_map.end()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 117 | } |
| 118 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 119 | if (pc2dex_map.empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | |
David Srbecky | 252fa90 | 2016-03-11 14:25:00 +0000 | [diff] [blame] | 123 | // Compensate for compiler's off-by-one-instruction error. |
| 124 | // |
| 125 | // The compiler generates stackmap with PC *after* the branch instruction |
| 126 | // (because this is the PC which is easier to obtain when unwinding). |
| 127 | // |
| 128 | // However, the debugger is more clever and it will ask us for line-number |
| 129 | // mapping at the location of the branch instruction (since the following |
| 130 | // instruction could belong to other line, this is the correct thing to do). |
| 131 | // |
| 132 | // So we really want to just decrement the PC by one instruction so that the |
| 133 | // branch instruction is covered as well. However, we do not know the size |
| 134 | // of the previous instruction, and we can not subtract just a fixed amount |
| 135 | // (the debugger would trust us that the PC is valid; it might try to set |
| 136 | // breakpoint there at some point, and setting breakpoint in mid-instruction |
| 137 | // would make the process crash in spectacular way). |
| 138 | // |
| 139 | // Therefore, we say that the PC which the compiler gave us for the stackmap |
| 140 | // is the end of its associated address range, and we use the PC from the |
| 141 | // previous stack map as the start of the range. This ensures that the PC is |
| 142 | // valid and that the branch instruction is covered. |
| 143 | // |
| 144 | // This ensures we have correct line number mapping at call sites (which is |
| 145 | // important for backtraces), but there is nothing we can do for non-call |
| 146 | // sites (so stepping through optimized code in debugger is not possible). |
| 147 | // |
| 148 | // We do not adjust the stackmaps if the code was compiled as debuggable. |
| 149 | // In that case, the stackmaps should accurately cover all instructions. |
| 150 | if (!mi->is_native_debuggable) { |
| 151 | for (size_t i = pc2dex_map.size() - 1; i > 0; --i) { |
| 152 | pc2dex_map[i].from_ = pc2dex_map[i - 1].from_; |
| 153 | } |
| 154 | pc2dex_map[0].from_ = 0; |
| 155 | } |
| 156 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 157 | Elf_Addr method_address = base_address + mi->code_address; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 158 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 159 | PositionInfos dex2line_map; |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 160 | DCHECK(mi->dex_file != nullptr); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 161 | const DexFile* dex = mi->dex_file; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 162 | CodeItemDebugInfoAccessor accessor(*dex, mi->code_item, mi->dex_method_index); |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 163 | const uint32_t debug_info_offset = accessor.DebugInfoOffset(); |
| 164 | if (!dex->DecodeDebugPositionInfo(debug_info_offset, PositionInfoCallback, &dex2line_map)) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 165 | continue; |
| 166 | } |
| 167 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 168 | if (dex2line_map.empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 169 | continue; |
| 170 | } |
| 171 | |
| 172 | opcodes.SetAddress(method_address); |
| 173 | if (dwarf_isa != -1) { |
| 174 | opcodes.SetISA(dwarf_isa); |
| 175 | } |
| 176 | |
| 177 | // Get and deduplicate directory and filename. |
| 178 | int file_index = 0; // 0 - primary source file of the compilation. |
| 179 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index); |
| 180 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 181 | if (source_file != nullptr) { |
| 182 | std::string file_name(source_file); |
| 183 | size_t file_name_slash = file_name.find_last_of('/'); |
| 184 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 185 | size_t class_name_slash = class_name.find_last_of('/'); |
| 186 | std::string full_path(file_name); |
| 187 | |
| 188 | // Guess directory from package name. |
| 189 | int directory_index = 0; // 0 - current directory of the compilation. |
| 190 | if (file_name_slash == std::string::npos && // Just filename. |
| 191 | class_name.front() == 'L' && // Type descriptor for a class. |
| 192 | class_name_slash != std::string::npos) { // Has package name. |
| 193 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 194 | auto it = directories_map.find(package_name); |
| 195 | if (it == directories_map.end()) { |
| 196 | directory_index = 1 + directories.size(); |
| 197 | directories_map.emplace(package_name, directory_index); |
| 198 | directories.push_back(package_name); |
| 199 | } else { |
| 200 | directory_index = it->second; |
| 201 | } |
| 202 | full_path = package_name + "/" + file_name; |
| 203 | } |
| 204 | |
| 205 | // Add file entry. |
| 206 | auto it2 = files_map.find(full_path); |
| 207 | if (it2 == files_map.end()) { |
| 208 | file_index = 1 + files.size(); |
| 209 | files_map.emplace(full_path, file_index); |
| 210 | files.push_back(dwarf::FileEntry { |
| 211 | file_name, |
| 212 | directory_index, |
| 213 | 0, // Modification time - NA. |
| 214 | 0, // File size - NA. |
| 215 | }); |
| 216 | } else { |
| 217 | file_index = it2->second; |
| 218 | } |
| 219 | } |
| 220 | opcodes.SetFile(file_index); |
| 221 | |
| 222 | // Generate mapping opcodes from PC to Java lines. |
| 223 | if (file_index != 0) { |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 224 | // If the method was not compiled as native-debuggable, we still generate all available |
| 225 | // lines, but we try to prevent the debugger from stepping and setting breakpoints since |
| 226 | // the information is too inaccurate for that (breakpoints would be set after the calls). |
| 227 | const bool default_is_stmt = mi->is_native_debuggable; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 228 | bool first = true; |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 229 | for (SrcMapElem pc2dex : pc2dex_map) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 230 | uint32_t pc = pc2dex.from_; |
| 231 | int dex_pc = pc2dex.to_; |
| 232 | // Find mapping with address with is greater than our dex pc; then go back one step. |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 233 | auto dex2line = std::upper_bound( |
| 234 | dex2line_map.begin(), |
| 235 | dex2line_map.end(), |
| 236 | dex_pc, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 237 | [](uint32_t address, const DexFile::PositionInfo& entry) { |
| 238 | return address < entry.address_; |
| 239 | }); |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 240 | // Look for first valid mapping after the prologue. |
| 241 | if (dex2line != dex2line_map.begin() && pc >= prologue_end) { |
| 242 | int line = (--dex2line)->line_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 243 | if (first) { |
| 244 | first = false; |
| 245 | if (pc > 0) { |
| 246 | // Assume that any preceding code is prologue. |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 247 | int first_line = dex2line_map.front().line_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 248 | // Prologue is not a sensible place for a breakpoint. |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 249 | opcodes.SetIsStmt(false); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 250 | opcodes.AddRow(method_address, first_line); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 251 | opcodes.SetPrologueEnd(); |
| 252 | } |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 253 | opcodes.SetIsStmt(default_is_stmt); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 254 | opcodes.AddRow(method_address + pc, line); |
| 255 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 256 | opcodes.SetIsStmt(default_is_stmt); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 257 | opcodes.AddRow(method_address + pc, line); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } else { |
| 262 | // line 0 - instruction cannot be attributed to any source line. |
| 263 | opcodes.AddRow(method_address, 0); |
| 264 | } |
| 265 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 266 | opcodes.AdvancePC(method_address + mi->code_size); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 267 | opcodes.EndSequence(); |
| 268 | } |
| 269 | std::vector<uint8_t> buffer; |
| 270 | buffer.reserve(opcodes.data()->size() + KB); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 271 | size_t offset = builder_->GetDebugLine()->GetPosition(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 272 | WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_); |
| 273 | builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); |
| 274 | return buffer.size(); |
| 275 | } |
| 276 | |
| 277 | void End(bool write_oat_patches) { |
| 278 | builder_->GetDebugLine()->End(); |
| 279 | if (write_oat_patches) { |
| 280 | builder_->WritePatches(".debug_line.oat_patches", |
| 281 | ArrayRef<const uintptr_t>(debug_line_patches_)); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | private: |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 286 | linker::ElfBuilder<ElfTypes>* builder_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 287 | std::vector<uintptr_t> debug_line_patches_; |
| 288 | }; |
| 289 | |
| 290 | } // namespace debug |
| 291 | } // namespace art |
| 292 | |
| 293 | #endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_ |
| 294 | |