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 <unordered_set>
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"
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 
PositionInfoCallback(void * ctx,const DexFile::PositionInfo & entry)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:
ElfDebugLineWriter(ElfBuilder<ElfTypes> * builder)46   explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
47   }
48 
Start()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.
WriteCompilationUnit(ElfCompilationUnit & compilation_unit)55   size_t WriteCompilationUnit(ElfCompilationUnit& compilation_unit) {
56     const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
57     const Elf_Addr base_address = compilation_unit.is_code_address_text_relative
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     std::unordered_set<uint64_t> seen_addresses(compilation_unit.methods.size());
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.
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) {
93         continue;
94       }
95 
96       uint32_t prologue_end = std::numeric_limits<uint32_t>::max();
97       std::vector<SrcMapElem> pc2dex_map;
98       if (mi->code_info != nullptr) {
99         // Use stack maps to create mapping table from pc to dex.
100         const CodeInfo code_info(mi->code_info);
101         const CodeInfoEncoding encoding = code_info.ExtractEncoding();
102         pc2dex_map.reserve(code_info.GetNumberOfStackMaps(encoding));
103         for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(encoding); s++) {
104           StackMap stack_map = code_info.GetStackMapAt(s, encoding);
105           DCHECK(stack_map.IsValid());
106           const uint32_t pc = stack_map.GetNativePcOffset(encoding.stack_map_encoding);
107           const int32_t dex = stack_map.GetDexPc(encoding.stack_map_encoding);
108           pc2dex_map.push_back({pc, dex});
109           if (stack_map.HasDexRegisterMap(encoding.stack_map_encoding)) {
110             // Guess that the first map with local variables is the end of prologue.
111             prologue_end = std::min(prologue_end, pc);
112           }
113         }
114         std::sort(pc2dex_map.begin(), pc2dex_map.end());
115       }
116 
117       if (pc2dex_map.empty()) {
118         continue;
119       }
120 
121       // Compensate for compiler's off-by-one-instruction error.
122       //
123       // The compiler generates stackmap with PC *after* the branch instruction
124       // (because this is the PC which is easier to obtain when unwinding).
125       //
126       // However, the debugger is more clever and it will ask us for line-number
127       // mapping at the location of the branch instruction (since the following
128       // instruction could belong to other line, this is the correct thing to do).
129       //
130       // So we really want to just decrement the PC by one instruction so that the
131       // branch instruction is covered as well. However, we do not know the size
132       // of the previous instruction, and we can not subtract just a fixed amount
133       // (the debugger would trust us that the PC is valid; it might try to set
134       // breakpoint there at some point, and setting breakpoint in mid-instruction
135       // would make the process crash in spectacular way).
136       //
137       // Therefore, we say that the PC which the compiler gave us for the stackmap
138       // is the end of its associated address range, and we use the PC from the
139       // previous stack map as the start of the range. This ensures that the PC is
140       // valid and that the branch instruction is covered.
141       //
142       // This ensures we have correct line number mapping at call sites (which is
143       // important for backtraces), but there is nothing we can do for non-call
144       // sites (so stepping through optimized code in debugger is not possible).
145       //
146       // We do not adjust the stackmaps if the code was compiled as debuggable.
147       // In that case, the stackmaps should accurately cover all instructions.
148       if (!mi->is_native_debuggable) {
149         for (size_t i = pc2dex_map.size() - 1; i > 0; --i) {
150           pc2dex_map[i].from_ = pc2dex_map[i - 1].from_;
151         }
152         pc2dex_map[0].from_ = 0;
153       }
154 
155       Elf_Addr method_address = base_address + mi->code_address;
156 
157       PositionInfos dex2line_map;
158       DCHECK(mi->dex_file != nullptr);
159       const DexFile* dex = mi->dex_file;
160       if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) {
161         continue;
162       }
163 
164       if (dex2line_map.empty()) {
165         continue;
166       }
167 
168       opcodes.SetAddress(method_address);
169       if (dwarf_isa != -1) {
170         opcodes.SetISA(dwarf_isa);
171       }
172 
173       // Get and deduplicate directory and filename.
174       int file_index = 0;  // 0 - primary source file of the compilation.
175       auto& dex_class_def = dex->GetClassDef(mi->class_def_index);
176       const char* source_file = dex->GetSourceFile(dex_class_def);
177       if (source_file != nullptr) {
178         std::string file_name(source_file);
179         size_t file_name_slash = file_name.find_last_of('/');
180         std::string class_name(dex->GetClassDescriptor(dex_class_def));
181         size_t class_name_slash = class_name.find_last_of('/');
182         std::string full_path(file_name);
183 
184         // Guess directory from package name.
185         int directory_index = 0;  // 0 - current directory of the compilation.
186         if (file_name_slash == std::string::npos &&  // Just filename.
187             class_name.front() == 'L' &&  // Type descriptor for a class.
188             class_name_slash != std::string::npos) {  // Has package name.
189           std::string package_name = class_name.substr(1, class_name_slash - 1);
190           auto it = directories_map.find(package_name);
191           if (it == directories_map.end()) {
192             directory_index = 1 + directories.size();
193             directories_map.emplace(package_name, directory_index);
194             directories.push_back(package_name);
195           } else {
196             directory_index = it->second;
197           }
198           full_path = package_name + "/" + file_name;
199         }
200 
201         // Add file entry.
202         auto it2 = files_map.find(full_path);
203         if (it2 == files_map.end()) {
204           file_index = 1 + files.size();
205           files_map.emplace(full_path, file_index);
206           files.push_back(dwarf::FileEntry {
207             file_name,
208             directory_index,
209             0,  // Modification time - NA.
210             0,  // File size - NA.
211           });
212         } else {
213           file_index = it2->second;
214         }
215       }
216       opcodes.SetFile(file_index);
217 
218       // Generate mapping opcodes from PC to Java lines.
219       if (file_index != 0) {
220         // If the method was not compiled as native-debuggable, we still generate all available
221         // lines, but we try to prevent the debugger from stepping and setting breakpoints since
222         // the information is too inaccurate for that (breakpoints would be set after the calls).
223         const bool default_is_stmt = mi->is_native_debuggable;
224         bool first = true;
225         for (SrcMapElem pc2dex : pc2dex_map) {
226           uint32_t pc = pc2dex.from_;
227           int dex_pc = pc2dex.to_;
228           // Find mapping with address with is greater than our dex pc; then go back one step.
229           auto dex2line = std::upper_bound(
230               dex2line_map.begin(),
231               dex2line_map.end(),
232               dex_pc,
233               [](uint32_t address, const DexFile::PositionInfo& entry) {
234                   return address < entry.address_;
235               });
236           // Look for first valid mapping after the prologue.
237           if (dex2line != dex2line_map.begin() && pc >= prologue_end) {
238             int line = (--dex2line)->line_;
239             if (first) {
240               first = false;
241               if (pc > 0) {
242                 // Assume that any preceding code is prologue.
243                 int first_line = dex2line_map.front().line_;
244                 // Prologue is not a sensible place for a breakpoint.
245                 opcodes.SetIsStmt(false);
246                 opcodes.AddRow(method_address, first_line);
247                 opcodes.SetPrologueEnd();
248               }
249               opcodes.SetIsStmt(default_is_stmt);
250               opcodes.AddRow(method_address + pc, line);
251             } else if (line != opcodes.CurrentLine()) {
252               opcodes.SetIsStmt(default_is_stmt);
253               opcodes.AddRow(method_address + pc, line);
254             }
255           }
256         }
257       } else {
258         // line 0 - instruction cannot be attributed to any source line.
259         opcodes.AddRow(method_address, 0);
260       }
261 
262       opcodes.AdvancePC(method_address + mi->code_size);
263       opcodes.EndSequence();
264     }
265     std::vector<uint8_t> buffer;
266     buffer.reserve(opcodes.data()->size() + KB);
267     size_t offset = builder_->GetDebugLine()->GetSize();
268     WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_);
269     builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
270     return buffer.size();
271   }
272 
End(bool write_oat_patches)273   void End(bool write_oat_patches) {
274     builder_->GetDebugLine()->End();
275     if (write_oat_patches) {
276       builder_->WritePatches(".debug_line.oat_patches",
277                              ArrayRef<const uintptr_t>(debug_line_patches_));
278     }
279   }
280 
281  private:
282   ElfBuilder<ElfTypes>* builder_;
283   std::vector<uintptr_t> debug_line_patches_;
284 };
285 
286 }  // namespace debug
287 }  // namespace art
288 
289 #endif  // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
290 
291