blob: 4896bc1e9bb0ba8a822805f19482454daf67d863 [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
David Srbecky6a6b38f2016-03-11 14:35:45 +000020#include <unordered_set>
David Srbeckyc5bfa972016-02-05 15:49:10 +000021#include <vector>
22
Vladimir Marko79c4f832022-11-15 17:12:25 +000023#include "base/macros.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000024#include "debug/elf_compilation_unit.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010025#include "debug/src_map_elem.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file-inl.h"
David Srbecky2faab002019-02-12 16:35:48 +000027#include "dwarf/debug_line_opcode_writer.h"
28#include "dwarf/headers.h"
29#include "elf/elf_builder.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000030#include "oat_file.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000031#include "stack_map.h"
32
Vladimir Marko79c4f832022-11-15 17:12:25 +000033namespace art HIDDEN {
David Srbeckyc5bfa972016-02-05 15:49:10 +000034namespace debug {
35
Vladimir Marko4f990712021-07-14 12:45:13 +010036using PositionInfos = std::vector<DexFile::PositionInfo>;
David Srbeckyc5bfa972016-02-05 15:49:10 +000037
David Srbeckyc5bfa972016-02-05 15:49:10 +000038template<typename ElfTypes>
39class ElfDebugLineWriter {
40 using Elf_Addr = typename ElfTypes::Addr;
41
42 public:
David Srbecky2faab002019-02-12 16:35:48 +000043 explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000044 }
45
46 void Start() {
47 builder_->GetDebugLine()->Start();
48 }
49
50 // Write line table for given set of methods.
51 // Returns the number of bytes written.
52 size_t WriteCompilationUnit(ElfCompilationUnit& compilation_unit) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080053 const InstructionSet isa = builder_->GetIsa();
54 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky197160d2016-03-07 17:33:57 +000055 const Elf_Addr base_address = compilation_unit.is_code_address_text_relative
David Srbeckyc5bfa972016-02-05 15:49:10 +000056 ? builder_->GetText()->GetAddress()
57 : 0;
58
David Srbeckye155f4b2017-12-06 15:18:38 +000059 compilation_unit.debug_line_offset = builder_->GetDebugLine()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +000060
61 std::vector<dwarf::FileEntry> files;
62 std::unordered_map<std::string, size_t> files_map;
63 std::vector<std::string> directories;
64 std::unordered_map<std::string, size_t> directories_map;
65 int code_factor_bits_ = 0;
66 int dwarf_isa = -1;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080067 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +000068 case InstructionSet::kArm: // arm actually means thumb2.
69 case InstructionSet::kThumb2:
David Srbeckyc5bfa972016-02-05 15:49:10 +000070 code_factor_bits_ = 1; // 16-bit instuctions
71 dwarf_isa = 1; // DW_ISA_ARM_thumb.
72 break;
Vladimir Marko33bff252017-11-01 14:35:42 +000073 case InstructionSet::kArm64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000074 code_factor_bits_ = 2; // 32-bit instructions
75 break;
Vladimir Marko33bff252017-11-01 14:35:42 +000076 case InstructionSet::kNone:
Colin Crossebc548c2022-07-14 18:09:41 -070077 case InstructionSet::kRiscv64:
Vladimir Marko33bff252017-11-01 14:35:42 +000078 case InstructionSet::kX86:
79 case InstructionSet::kX86_64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000080 break;
81 }
David Srbecky6a6b38f2016-03-11 14:35:45 +000082 std::unordered_set<uint64_t> seen_addresses(compilation_unit.methods.size());
David Srbeckyc5bfa972016-02-05 15:49:10 +000083 dwarf::DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
84 for (const MethodDebugInfo* mi : compilation_unit.methods) {
85 // Ignore function if we have already generated line table for the same address.
86 // It would confuse the debugger and the DWARF specification forbids it.
David Srbecky6a6b38f2016-03-11 14:35:45 +000087 // We allow the line table for method to be replicated in different compilation unit.
88 // This ensures that each compilation unit contains line table for all its methods.
89 if (!seen_addresses.insert(mi->code_address).second) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000090 continue;
91 }
92
David Srbecky99b87eb2016-02-09 18:16:35 +000093 uint32_t prologue_end = std::numeric_limits<uint32_t>::max();
David Srbecky197160d2016-03-07 17:33:57 +000094 std::vector<SrcMapElem> pc2dex_map;
95 if (mi->code_info != nullptr) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000096 // Use stack maps to create mapping table from pc to dex.
David Srbecky197160d2016-03-07 17:33:57 +000097 const CodeInfo code_info(mi->code_info);
David Srbecky052f8ca2018-04-26 15:42:54 +010098 pc2dex_map.reserve(code_info.GetNumberOfStackMaps());
David Srbecky93bd3612018-07-02 19:30:18 +010099 for (StackMap stack_map : code_info.GetStackMaps()) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100100 const uint32_t pc = stack_map.GetNativePcOffset(isa);
101 const int32_t dex = stack_map.GetDexPc();
David Srbecky197160d2016-03-07 17:33:57 +0000102 pc2dex_map.push_back({pc, dex});
David Srbecky052f8ca2018-04-26 15:42:54 +0100103 if (stack_map.HasDexRegisterMap()) {
David Srbecky99b87eb2016-02-09 18:16:35 +0000104 // Guess that the first map with local variables is the end of prologue.
105 prologue_end = std::min(prologue_end, pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000106 }
107 }
David Srbecky197160d2016-03-07 17:33:57 +0000108 std::sort(pc2dex_map.begin(), pc2dex_map.end());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000109 }
110
David Srbecky99b87eb2016-02-09 18:16:35 +0000111 if (pc2dex_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000112 continue;
113 }
114
David Srbecky252fa902016-03-11 14:25:00 +0000115 // Compensate for compiler's off-by-one-instruction error.
116 //
117 // The compiler generates stackmap with PC *after* the branch instruction
118 // (because this is the PC which is easier to obtain when unwinding).
119 //
120 // However, the debugger is more clever and it will ask us for line-number
121 // mapping at the location of the branch instruction (since the following
122 // instruction could belong to other line, this is the correct thing to do).
123 //
124 // So we really want to just decrement the PC by one instruction so that the
125 // branch instruction is covered as well. However, we do not know the size
126 // of the previous instruction, and we can not subtract just a fixed amount
127 // (the debugger would trust us that the PC is valid; it might try to set
128 // breakpoint there at some point, and setting breakpoint in mid-instruction
129 // would make the process crash in spectacular way).
130 //
131 // Therefore, we say that the PC which the compiler gave us for the stackmap
132 // is the end of its associated address range, and we use the PC from the
133 // previous stack map as the start of the range. This ensures that the PC is
134 // valid and that the branch instruction is covered.
135 //
136 // This ensures we have correct line number mapping at call sites (which is
137 // important for backtraces), but there is nothing we can do for non-call
138 // sites (so stepping through optimized code in debugger is not possible).
139 //
140 // We do not adjust the stackmaps if the code was compiled as debuggable.
141 // In that case, the stackmaps should accurately cover all instructions.
142 if (!mi->is_native_debuggable) {
143 for (size_t i = pc2dex_map.size() - 1; i > 0; --i) {
144 pc2dex_map[i].from_ = pc2dex_map[i - 1].from_;
145 }
146 pc2dex_map[0].from_ = 0;
147 }
148
David Srbecky197160d2016-03-07 17:33:57 +0000149 Elf_Addr method_address = base_address + mi->code_address;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000150
David Srbecky99b87eb2016-02-09 18:16:35 +0000151 PositionInfos dex2line_map;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000152 const DexFile* dex = mi->dex_file;
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700153 DCHECK(dex != nullptr);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800154 CodeItemDebugInfoAccessor accessor(*dex, mi->code_item, mi->dex_method_index);
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700155 if (!accessor.DecodeDebugPositionInfo(
156 [&](const DexFile::PositionInfo& entry) {
157 dex2line_map.push_back(entry);
158 return false;
159 })) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000160 continue;
161 }
162
David Srbecky99b87eb2016-02-09 18:16:35 +0000163 if (dex2line_map.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000164 continue;
165 }
166
167 opcodes.SetAddress(method_address);
168 if (dwarf_isa != -1) {
169 opcodes.SetISA(dwarf_isa);
170 }
171
172 // Get and deduplicate directory and filename.
173 int file_index = 0; // 0 - primary source file of the compilation.
174 auto& dex_class_def = dex->GetClassDef(mi->class_def_index);
175 const char* source_file = dex->GetSourceFile(dex_class_def);
176 if (source_file != nullptr) {
177 std::string file_name(source_file);
178 size_t file_name_slash = file_name.find_last_of('/');
179 std::string class_name(dex->GetClassDescriptor(dex_class_def));
180 size_t class_name_slash = class_name.find_last_of('/');
181 std::string full_path(file_name);
182
183 // Guess directory from package name.
184 int directory_index = 0; // 0 - current directory of the compilation.
185 if (file_name_slash == std::string::npos && // Just filename.
186 class_name.front() == 'L' && // Type descriptor for a class.
187 class_name_slash != std::string::npos) { // Has package name.
188 std::string package_name = class_name.substr(1, class_name_slash - 1);
189 auto it = directories_map.find(package_name);
190 if (it == directories_map.end()) {
191 directory_index = 1 + directories.size();
192 directories_map.emplace(package_name, directory_index);
193 directories.push_back(package_name);
194 } else {
195 directory_index = it->second;
196 }
197 full_path = package_name + "/" + file_name;
198 }
199
200 // Add file entry.
201 auto it2 = files_map.find(full_path);
202 if (it2 == files_map.end()) {
203 file_index = 1 + files.size();
204 files_map.emplace(full_path, file_index);
205 files.push_back(dwarf::FileEntry {
206 file_name,
207 directory_index,
208 0, // Modification time - NA.
209 0, // File size - NA.
210 });
211 } else {
212 file_index = it2->second;
213 }
214 }
215 opcodes.SetFile(file_index);
216
217 // Generate mapping opcodes from PC to Java lines.
218 if (file_index != 0) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000219 // If the method was not compiled as native-debuggable, we still generate all available
220 // lines, but we try to prevent the debugger from stepping and setting breakpoints since
221 // the information is too inaccurate for that (breakpoints would be set after the calls).
222 const bool default_is_stmt = mi->is_native_debuggable;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000223 bool first = true;
David Srbecky99b87eb2016-02-09 18:16:35 +0000224 for (SrcMapElem pc2dex : pc2dex_map) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000225 uint32_t pc = pc2dex.from_;
226 int dex_pc = pc2dex.to_;
227 // Find mapping with address with is greater than our dex pc; then go back one step.
David Srbecky99b87eb2016-02-09 18:16:35 +0000228 auto dex2line = std::upper_bound(
229 dex2line_map.begin(),
230 dex2line_map.end(),
231 dex_pc,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000232 [](uint32_t address, const DexFile::PositionInfo& entry) {
233 return address < entry.address_;
234 });
David Srbecky99b87eb2016-02-09 18:16:35 +0000235 // Look for first valid mapping after the prologue.
236 if (dex2line != dex2line_map.begin() && pc >= prologue_end) {
237 int line = (--dex2line)->line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000238 if (first) {
239 first = false;
240 if (pc > 0) {
241 // Assume that any preceding code is prologue.
David Srbecky99b87eb2016-02-09 18:16:35 +0000242 int first_line = dex2line_map.front().line_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000243 // Prologue is not a sensible place for a breakpoint.
David Srbecky91cc06c2016-03-07 16:13:58 +0000244 opcodes.SetIsStmt(false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000245 opcodes.AddRow(method_address, first_line);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000246 opcodes.SetPrologueEnd();
247 }
David Srbecky91cc06c2016-03-07 16:13:58 +0000248 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000249 opcodes.AddRow(method_address + pc, line);
250 } else if (line != opcodes.CurrentLine()) {
David Srbecky91cc06c2016-03-07 16:13:58 +0000251 opcodes.SetIsStmt(default_is_stmt);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000252 opcodes.AddRow(method_address + pc, line);
253 }
254 }
255 }
256 } else {
257 // line 0 - instruction cannot be attributed to any source line.
258 opcodes.AddRow(method_address, 0);
259 }
260
David Srbecky197160d2016-03-07 17:33:57 +0000261 opcodes.AdvancePC(method_address + mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000262 opcodes.EndSequence();
263 }
264 std::vector<uint8_t> buffer;
265 buffer.reserve(opcodes.data()->size() + KB);
David Srbecky7370d922019-02-12 14:00:30 +0000266 WriteDebugLineTable(directories, files, opcodes, &buffer);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000267 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
268 return buffer.size();
269 }
270
David Srbecky7370d922019-02-12 14:00:30 +0000271 void End() {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000272 builder_->GetDebugLine()->End();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000273 }
274
275 private:
David Srbecky2faab002019-02-12 16:35:48 +0000276 ElfBuilder<ElfTypes>* builder_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000277};
278
279} // namespace debug
280} // namespace art
281
282#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_
283