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 | |
| 23 | #include "compiled_method.h" |
| 24 | #include "debug/dwarf/debug_line_opcode_writer.h" |
| 25 | #include "debug/dwarf/headers.h" |
| 26 | #include "debug/elf_compilation_unit.h" |
| 27 | #include "dex_file-inl.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 28 | #include "elf_builder.h" |
| 29 | #include "stack_map.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace debug { |
| 33 | |
| 34 | typedef std::vector<DexFile::PositionInfo> PositionInfos; |
| 35 | |
| 36 | static bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { |
| 37 | static_cast<PositionInfos*>(ctx)->push_back(entry); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | template<typename ElfTypes> |
| 42 | class ElfDebugLineWriter { |
| 43 | using Elf_Addr = typename ElfTypes::Addr; |
| 44 | |
| 45 | public: |
| 46 | explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
| 47 | } |
| 48 | |
| 49 | void Start() { |
| 50 | builder_->GetDebugLine()->Start(); |
| 51 | } |
| 52 | |
| 53 | // Write line table for given set of methods. |
| 54 | // Returns the number of bytes written. |
| 55 | size_t WriteCompilationUnit(ElfCompilationUnit& compilation_unit) { |
| 56 | const bool is64bit = Is64BitInstructionSet(builder_->GetIsa()); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 57 | const Elf_Addr base_address = compilation_unit.is_code_address_text_relative |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 58 | ? builder_->GetText()->GetAddress() |
| 59 | : 0; |
| 60 | |
| 61 | compilation_unit.debug_line_offset = builder_->GetDebugLine()->GetSize(); |
| 62 | |
| 63 | std::vector<dwarf::FileEntry> files; |
| 64 | std::unordered_map<std::string, size_t> files_map; |
| 65 | std::vector<std::string> directories; |
| 66 | std::unordered_map<std::string, size_t> directories_map; |
| 67 | int code_factor_bits_ = 0; |
| 68 | int dwarf_isa = -1; |
| 69 | switch (builder_->GetIsa()) { |
| 70 | case kArm: // arm actually means thumb2. |
| 71 | case kThumb2: |
| 72 | code_factor_bits_ = 1; // 16-bit instuctions |
| 73 | dwarf_isa = 1; // DW_ISA_ARM_thumb. |
| 74 | break; |
| 75 | case kArm64: |
| 76 | case kMips: |
| 77 | case kMips64: |
| 78 | code_factor_bits_ = 2; // 32-bit instructions |
| 79 | break; |
| 80 | case kNone: |
| 81 | case kX86: |
| 82 | case kX86_64: |
| 83 | break; |
| 84 | } |
David Srbecky | 6a6b38f | 2016-03-11 14:35:45 +0000 | [diff] [blame^] | 85 | std::unordered_set<uint64_t> seen_addresses(compilation_unit.methods.size()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 86 | dwarf::DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); |
| 87 | for (const MethodDebugInfo* mi : compilation_unit.methods) { |
| 88 | // Ignore function if we have already generated line table for the same address. |
| 89 | // It would confuse the debugger and the DWARF specification forbids it. |
David Srbecky | 6a6b38f | 2016-03-11 14:35:45 +0000 | [diff] [blame^] | 90 | // We allow the line table for method to be replicated in different compilation unit. |
| 91 | // This ensures that each compilation unit contains line table for all its methods. |
| 92 | if (!seen_addresses.insert(mi->code_address).second) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 93 | continue; |
| 94 | } |
| 95 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 96 | uint32_t prologue_end = std::numeric_limits<uint32_t>::max(); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 97 | std::vector<SrcMapElem> pc2dex_map; |
| 98 | if (mi->code_info != nullptr) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 99 | // Use stack maps to create mapping table from pc to dex. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 100 | const CodeInfo code_info(mi->code_info); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 101 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 102 | pc2dex_map.reserve(code_info.GetNumberOfStackMaps()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 103 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 104 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 105 | DCHECK(stack_map.IsValid()); |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 106 | const uint32_t pc = stack_map.GetNativePcOffset(encoding); |
| 107 | const int32_t dex = stack_map.GetDexPc(encoding); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 108 | pc2dex_map.push_back({pc, dex}); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 109 | if (stack_map.HasDexRegisterMap(encoding)) { |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 110 | // Guess that the first map with local variables is the end of prologue. |
| 111 | prologue_end = std::min(prologue_end, pc); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 114 | std::sort(pc2dex_map.begin(), pc2dex_map.end()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 115 | } |
| 116 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 117 | if (pc2dex_map.empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 118 | continue; |
| 119 | } |
| 120 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 121 | Elf_Addr method_address = base_address + mi->code_address; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 122 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 123 | PositionInfos dex2line_map; |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 124 | DCHECK(mi->dex_file != nullptr); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 125 | const DexFile* dex = mi->dex_file; |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 126 | if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 127 | continue; |
| 128 | } |
| 129 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 130 | if (dex2line_map.empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | |
| 134 | opcodes.SetAddress(method_address); |
| 135 | if (dwarf_isa != -1) { |
| 136 | opcodes.SetISA(dwarf_isa); |
| 137 | } |
| 138 | |
| 139 | // Get and deduplicate directory and filename. |
| 140 | int file_index = 0; // 0 - primary source file of the compilation. |
| 141 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index); |
| 142 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 143 | if (source_file != nullptr) { |
| 144 | std::string file_name(source_file); |
| 145 | size_t file_name_slash = file_name.find_last_of('/'); |
| 146 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 147 | size_t class_name_slash = class_name.find_last_of('/'); |
| 148 | std::string full_path(file_name); |
| 149 | |
| 150 | // Guess directory from package name. |
| 151 | int directory_index = 0; // 0 - current directory of the compilation. |
| 152 | if (file_name_slash == std::string::npos && // Just filename. |
| 153 | class_name.front() == 'L' && // Type descriptor for a class. |
| 154 | class_name_slash != std::string::npos) { // Has package name. |
| 155 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 156 | auto it = directories_map.find(package_name); |
| 157 | if (it == directories_map.end()) { |
| 158 | directory_index = 1 + directories.size(); |
| 159 | directories_map.emplace(package_name, directory_index); |
| 160 | directories.push_back(package_name); |
| 161 | } else { |
| 162 | directory_index = it->second; |
| 163 | } |
| 164 | full_path = package_name + "/" + file_name; |
| 165 | } |
| 166 | |
| 167 | // Add file entry. |
| 168 | auto it2 = files_map.find(full_path); |
| 169 | if (it2 == files_map.end()) { |
| 170 | file_index = 1 + files.size(); |
| 171 | files_map.emplace(full_path, file_index); |
| 172 | files.push_back(dwarf::FileEntry { |
| 173 | file_name, |
| 174 | directory_index, |
| 175 | 0, // Modification time - NA. |
| 176 | 0, // File size - NA. |
| 177 | }); |
| 178 | } else { |
| 179 | file_index = it2->second; |
| 180 | } |
| 181 | } |
| 182 | opcodes.SetFile(file_index); |
| 183 | |
| 184 | // Generate mapping opcodes from PC to Java lines. |
| 185 | if (file_index != 0) { |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 186 | // If the method was not compiled as native-debuggable, we still generate all available |
| 187 | // lines, but we try to prevent the debugger from stepping and setting breakpoints since |
| 188 | // the information is too inaccurate for that (breakpoints would be set after the calls). |
| 189 | const bool default_is_stmt = mi->is_native_debuggable; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 190 | bool first = true; |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 191 | for (SrcMapElem pc2dex : pc2dex_map) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 192 | uint32_t pc = pc2dex.from_; |
| 193 | int dex_pc = pc2dex.to_; |
| 194 | // 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] | 195 | auto dex2line = std::upper_bound( |
| 196 | dex2line_map.begin(), |
| 197 | dex2line_map.end(), |
| 198 | dex_pc, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 199 | [](uint32_t address, const DexFile::PositionInfo& entry) { |
| 200 | return address < entry.address_; |
| 201 | }); |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 202 | // Look for first valid mapping after the prologue. |
| 203 | if (dex2line != dex2line_map.begin() && pc >= prologue_end) { |
| 204 | int line = (--dex2line)->line_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 205 | if (first) { |
| 206 | first = false; |
| 207 | if (pc > 0) { |
| 208 | // Assume that any preceding code is prologue. |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 209 | int first_line = dex2line_map.front().line_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 210 | // Prologue is not a sensible place for a breakpoint. |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 211 | opcodes.SetIsStmt(false); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 212 | opcodes.AddRow(method_address, first_line); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 213 | opcodes.SetPrologueEnd(); |
| 214 | } |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 215 | opcodes.SetIsStmt(default_is_stmt); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 216 | opcodes.AddRow(method_address + pc, line); |
| 217 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 218 | opcodes.SetIsStmt(default_is_stmt); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 219 | opcodes.AddRow(method_address + pc, line); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } else { |
| 224 | // line 0 - instruction cannot be attributed to any source line. |
| 225 | opcodes.AddRow(method_address, 0); |
| 226 | } |
| 227 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 228 | opcodes.AdvancePC(method_address + mi->code_size); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 229 | opcodes.EndSequence(); |
| 230 | } |
| 231 | std::vector<uint8_t> buffer; |
| 232 | buffer.reserve(opcodes.data()->size() + KB); |
| 233 | size_t offset = builder_->GetDebugLine()->GetSize(); |
| 234 | WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_); |
| 235 | builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); |
| 236 | return buffer.size(); |
| 237 | } |
| 238 | |
| 239 | void End(bool write_oat_patches) { |
| 240 | builder_->GetDebugLine()->End(); |
| 241 | if (write_oat_patches) { |
| 242 | builder_->WritePatches(".debug_line.oat_patches", |
| 243 | ArrayRef<const uintptr_t>(debug_line_patches_)); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private: |
| 248 | ElfBuilder<ElfTypes>* builder_; |
| 249 | std::vector<uintptr_t> debug_line_patches_; |
| 250 | }; |
| 251 | |
| 252 | } // namespace debug |
| 253 | } // namespace art |
| 254 | |
| 255 | #endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_ |
| 256 | |