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