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 | |
| 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 Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 27 | #include "elf_builder.h" |
| 28 | #include "stack_map.h" |
| 29 | |
| 30 | namespace art { |
| 31 | namespace debug { |
| 32 | |
| 33 | typedef std::vector<DexFile::PositionInfo> PositionInfos; |
| 34 | |
| 35 | static bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { |
| 36 | static_cast<PositionInfos*>(ctx)->push_back(entry); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | template<typename ElfTypes> |
| 41 | class 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()); |
| 56 | const Elf_Addr text_address = builder_->GetText()->Exists() |
| 57 | ? 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 Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 92 | uint32_t prologue_end = std::numeric_limits<uint32_t>::max(); |
| 93 | ArrayRef<const SrcMapElem> pc2dex_map; |
| 94 | std::vector<SrcMapElem> pc2dex_map_from_stack_maps; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 95 | if (mi->IsFromOptimizingCompiler()) { |
| 96 | // Use stack maps to create mapping table from pc to dex. |
| 97 | const CodeInfo code_info(mi->compiled_method->GetVmapTable().data()); |
| 98 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 99 | 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 Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 102 | const uint32_t pc = stack_map.GetNativePcOffset(encoding); |
| 103 | const int32_t dex = stack_map.GetDexPc(encoding); |
| 104 | pc2dex_map_from_stack_maps.push_back({pc, dex}); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 105 | if (stack_map.HasDexRegisterMap(encoding)) { |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 106 | // Guess that the first map with local variables is the end of prologue. |
| 107 | prologue_end = std::min(prologue_end, pc); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 110 | std::sort(pc2dex_map_from_stack_maps.begin(), |
| 111 | pc2dex_map_from_stack_maps.end()); |
| 112 | pc2dex_map = ArrayRef<const SrcMapElem>(pc2dex_map_from_stack_maps); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 113 | } else { |
| 114 | // Use the mapping table provided by the quick compiler. |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 115 | pc2dex_map = mi->compiled_method->GetSrcMappingTable(); |
| 116 | prologue_end = 0; |
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 | |
| 123 | Elf_Addr method_address = text_address + mi->low_pc; |
| 124 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 125 | PositionInfos dex2line_map; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 126 | const DexFile* dex = mi->dex_file; |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 127 | if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 131 | if (dex2line_map.empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 132 | continue; |
| 133 | } |
| 134 | |
| 135 | opcodes.SetAddress(method_address); |
| 136 | if (dwarf_isa != -1) { |
| 137 | opcodes.SetISA(dwarf_isa); |
| 138 | } |
| 139 | |
| 140 | // Get and deduplicate directory and filename. |
| 141 | int file_index = 0; // 0 - primary source file of the compilation. |
| 142 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index); |
| 143 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 144 | if (source_file != nullptr) { |
| 145 | std::string file_name(source_file); |
| 146 | size_t file_name_slash = file_name.find_last_of('/'); |
| 147 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 148 | size_t class_name_slash = class_name.find_last_of('/'); |
| 149 | std::string full_path(file_name); |
| 150 | |
| 151 | // Guess directory from package name. |
| 152 | int directory_index = 0; // 0 - current directory of the compilation. |
| 153 | if (file_name_slash == std::string::npos && // Just filename. |
| 154 | class_name.front() == 'L' && // Type descriptor for a class. |
| 155 | class_name_slash != std::string::npos) { // Has package name. |
| 156 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 157 | auto it = directories_map.find(package_name); |
| 158 | if (it == directories_map.end()) { |
| 159 | directory_index = 1 + directories.size(); |
| 160 | directories_map.emplace(package_name, directory_index); |
| 161 | directories.push_back(package_name); |
| 162 | } else { |
| 163 | directory_index = it->second; |
| 164 | } |
| 165 | full_path = package_name + "/" + file_name; |
| 166 | } |
| 167 | |
| 168 | // Add file entry. |
| 169 | auto it2 = files_map.find(full_path); |
| 170 | if (it2 == files_map.end()) { |
| 171 | file_index = 1 + files.size(); |
| 172 | files_map.emplace(full_path, file_index); |
| 173 | files.push_back(dwarf::FileEntry { |
| 174 | file_name, |
| 175 | directory_index, |
| 176 | 0, // Modification time - NA. |
| 177 | 0, // File size - NA. |
| 178 | }); |
| 179 | } else { |
| 180 | file_index = it2->second; |
| 181 | } |
| 182 | } |
| 183 | opcodes.SetFile(file_index); |
| 184 | |
| 185 | // Generate mapping opcodes from PC to Java lines. |
| 186 | if (file_index != 0) { |
| 187 | bool first = true; |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 188 | for (SrcMapElem pc2dex : pc2dex_map) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 189 | uint32_t pc = pc2dex.from_; |
| 190 | int dex_pc = pc2dex.to_; |
| 191 | // 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] | 192 | auto dex2line = std::upper_bound( |
| 193 | dex2line_map.begin(), |
| 194 | dex2line_map.end(), |
| 195 | dex_pc, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 196 | [](uint32_t address, const DexFile::PositionInfo& entry) { |
| 197 | return address < entry.address_; |
| 198 | }); |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 199 | // Look for first valid mapping after the prologue. |
| 200 | if (dex2line != dex2line_map.begin() && pc >= prologue_end) { |
| 201 | int line = (--dex2line)->line_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 202 | if (first) { |
| 203 | first = false; |
| 204 | if (pc > 0) { |
| 205 | // Assume that any preceding code is prologue. |
David Srbecky | 99b87eb | 2016-02-09 18:16:35 +0000 | [diff] [blame] | 206 | int first_line = dex2line_map.front().line_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 207 | // Prologue is not a sensible place for a breakpoint. |
| 208 | opcodes.NegateStmt(); |
| 209 | opcodes.AddRow(method_address, first_line); |
| 210 | opcodes.NegateStmt(); |
| 211 | opcodes.SetPrologueEnd(); |
| 212 | } |
| 213 | opcodes.AddRow(method_address + pc, line); |
| 214 | } else if (line != opcodes.CurrentLine()) { |
| 215 | opcodes.AddRow(method_address + pc, line); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } else { |
| 220 | // line 0 - instruction cannot be attributed to any source line. |
| 221 | opcodes.AddRow(method_address, 0); |
| 222 | } |
| 223 | |
| 224 | opcodes.AdvancePC(text_address + mi->high_pc); |
| 225 | opcodes.EndSequence(); |
| 226 | } |
| 227 | std::vector<uint8_t> buffer; |
| 228 | buffer.reserve(opcodes.data()->size() + KB); |
| 229 | size_t offset = builder_->GetDebugLine()->GetSize(); |
| 230 | WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_); |
| 231 | builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); |
| 232 | return buffer.size(); |
| 233 | } |
| 234 | |
| 235 | void End(bool write_oat_patches) { |
| 236 | builder_->GetDebugLine()->End(); |
| 237 | if (write_oat_patches) { |
| 238 | builder_->WritePatches(".debug_line.oat_patches", |
| 239 | ArrayRef<const uintptr_t>(debug_line_patches_)); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | ElfBuilder<ElfTypes>* builder_; |
| 245 | std::vector<uintptr_t> debug_line_patches_; |
| 246 | }; |
| 247 | |
| 248 | } // namespace debug |
| 249 | } // namespace art |
| 250 | |
| 251 | #endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_ |
| 252 | |