blob: ac0f4ca21dadbd336902235c99f6c63f591811ea [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
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"
27#include "dex_file.h"
28#include "elf_builder.h"
29#include "stack_map.h"
30
31namespace art {
32namespace debug {
33
34typedef std::vector<DexFile::PositionInfo> PositionInfos;
35
36static bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
37 static_cast<PositionInfos*>(ctx)->push_back(entry);
38 return false;
39}
40
41template<typename ElfTypes>
42class 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());
57 const Elf_Addr text_address = builder_->GetText()->Exists()
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 }
85 dwarf::DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
86 for (const MethodDebugInfo* mi : compilation_unit.methods) {
87 // Ignore function if we have already generated line table for the same address.
88 // It would confuse the debugger and the DWARF specification forbids it.
89 if (mi->deduped) {
90 continue;
91 }
92
93 ArrayRef<const SrcMapElem> src_mapping_table;
94 std::vector<SrcMapElem> src_mapping_table_from_stack_maps;
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());
102 // Emit only locations where we have local-variable information.
103 // In particular, skip mappings inside the prologue.
104 if (stack_map.HasDexRegisterMap(encoding)) {
105 const uint32_t pc = stack_map.GetNativePcOffset(encoding);
106 const int32_t dex = stack_map.GetDexPc(encoding);
107 src_mapping_table_from_stack_maps.push_back({pc, dex});
108 }
109 }
110 std::sort(src_mapping_table_from_stack_maps.begin(),
111 src_mapping_table_from_stack_maps.end());
112 src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps);
113 } else {
114 // Use the mapping table provided by the quick compiler.
115 src_mapping_table = mi->compiled_method->GetSrcMappingTable();
116 }
117
118 if (src_mapping_table.empty()) {
119 continue;
120 }
121
122 Elf_Addr method_address = text_address + mi->low_pc;
123
124 PositionInfos position_infos;
125 const DexFile* dex = mi->dex_file;
126 if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &position_infos)) {
127 continue;
128 }
129
130 if (position_infos.empty()) {
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) {
186 bool first = true;
187 for (SrcMapElem pc2dex : src_mapping_table) {
188 uint32_t pc = pc2dex.from_;
189 int dex_pc = pc2dex.to_;
190 // Find mapping with address with is greater than our dex pc; then go back one step.
191 auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc,
192 [](uint32_t address, const DexFile::PositionInfo& entry) {
193 return address < entry.address_;
194 });
195 if (ub != position_infos.begin()) {
196 int line = (--ub)->line_;
197 if (first) {
198 first = false;
199 if (pc > 0) {
200 // Assume that any preceding code is prologue.
201 int first_line = position_infos.front().line_;
202 // Prologue is not a sensible place for a breakpoint.
203 opcodes.NegateStmt();
204 opcodes.AddRow(method_address, first_line);
205 opcodes.NegateStmt();
206 opcodes.SetPrologueEnd();
207 }
208 opcodes.AddRow(method_address + pc, line);
209 } else if (line != opcodes.CurrentLine()) {
210 opcodes.AddRow(method_address + pc, line);
211 }
212 }
213 }
214 } else {
215 // line 0 - instruction cannot be attributed to any source line.
216 opcodes.AddRow(method_address, 0);
217 }
218
219 opcodes.AdvancePC(text_address + mi->high_pc);
220 opcodes.EndSequence();
221 }
222 std::vector<uint8_t> buffer;
223 buffer.reserve(opcodes.data()->size() + KB);
224 size_t offset = builder_->GetDebugLine()->GetSize();
225 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_);
226 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
227 return buffer.size();
228 }
229
230 void End(bool write_oat_patches) {
231 builder_->GetDebugLine()->End();
232 if (write_oat_patches) {
233 builder_->WritePatches(".debug_line.oat_patches",
234 ArrayRef<const uintptr_t>(debug_line_patches_));
235 }
236 }
237
238 private:
239 ElfBuilder<ElfTypes>* builder_;
240 std::vector<uintptr_t> debug_line_patches_;
241};
242
243} // namespace debug
244} // namespace art
245
246#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
247