blob: c321b4bc4ff585482c8b4aff6b9ee725e124bd44 [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_LOC_WRITER_H_
18#define ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
19
David Srbeckyb396c732016-02-10 14:35:34 +000020#include <cstring>
David Srbeckyc5bfa972016-02-05 15:49:10 +000021#include <map>
22
23#include "arch/instruction_set.h"
24#include "compiled_method.h"
25#include "debug/dwarf/debug_info_entry_writer.h"
26#include "debug/dwarf/register.h"
27#include "debug/method_debug_info.h"
28#include "stack_map.h"
29
30namespace art {
31namespace debug {
32using Reg = dwarf::Reg;
33
34static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
35 switch (isa) {
36 case kArm:
37 case kThumb2:
38 return Reg::ArmCore(machine_reg);
39 case kArm64:
40 return Reg::Arm64Core(machine_reg);
41 case kX86:
42 return Reg::X86Core(machine_reg);
43 case kX86_64:
44 return Reg::X86_64Core(machine_reg);
45 case kMips:
46 return Reg::MipsCore(machine_reg);
47 case kMips64:
48 return Reg::Mips64Core(machine_reg);
49 case kNone:
50 LOG(FATAL) << "No instruction set";
51 }
52 UNREACHABLE();
53}
54
55static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
56 switch (isa) {
57 case kArm:
58 case kThumb2:
59 return Reg::ArmFp(machine_reg);
60 case kArm64:
61 return Reg::Arm64Fp(machine_reg);
62 case kX86:
63 return Reg::X86Fp(machine_reg);
64 case kX86_64:
65 return Reg::X86_64Fp(machine_reg);
66 case kMips:
67 return Reg::MipsFp(machine_reg);
68 case kMips64:
69 return Reg::Mips64Fp(machine_reg);
70 case kNone:
71 LOG(FATAL) << "No instruction set";
72 }
73 UNREACHABLE();
74}
75
76struct VariableLocation {
77 uint32_t low_pc;
78 uint32_t high_pc;
79 DexRegisterLocation reg_lo; // May be None if the location is unknown.
80 DexRegisterLocation reg_hi; // Most significant bits of 64-bit value.
81};
82
83// Get the location of given dex register (e.g. stack or machine register).
84// Note that the location might be different based on the current pc.
85// The result will cover all ranges where the variable is in scope.
David Srbeckybfd26cd2016-02-10 13:57:09 +000086// PCs corresponding to stackmap with dex register map are accurate,
87// all other PCs are best-effort only.
David Srbecky2ed15b62016-03-04 11:34:46 +000088std::vector<VariableLocation> GetVariableLocations(
89 const MethodDebugInfo* method_info,
90 const std::vector<DexRegisterMap>& dex_register_maps,
91 uint16_t vreg,
92 bool is64bitValue,
93 uint32_t dex_pc_low,
94 uint32_t dex_pc_high) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000095 std::vector<VariableLocation> variable_locations;
96
97 // Get stack maps sorted by pc (they might not be sorted internally).
98 const CodeInfo code_info(method_info->compiled_method->GetVmapTable().data());
99 const StackMapEncoding encoding = code_info.ExtractEncoding();
David Srbecky2ed15b62016-03-04 11:34:46 +0000100 std::map<uint32_t, uint32_t> stack_maps; // low_pc -> stack_map_index.
David Srbeckyc5bfa972016-02-05 15:49:10 +0000101 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
102 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
103 DCHECK(stack_map.IsValid());
104 const uint32_t low_pc = method_info->low_pc + stack_map.GetNativePcOffset(encoding);
105 DCHECK_LE(low_pc, method_info->high_pc);
David Srbecky2ed15b62016-03-04 11:34:46 +0000106 stack_maps.emplace(low_pc, s);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000107 }
108
109 // Create entries for the requested register based on stack map data.
110 for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000111 const uint32_t low_pc = it->first;
David Srbecky2ed15b62016-03-04 11:34:46 +0000112 const uint32_t stack_map_index = it->second;
113 const StackMap& stack_map = code_info.GetStackMapAt(stack_map_index, encoding);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000114 auto next_it = it;
115 next_it++;
116 const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
117 : method_info->high_pc;
118 DCHECK_LE(low_pc, high_pc);
119 if (low_pc == high_pc) {
120 continue; // Ignore if the address range is empty.
121 }
122
123 // Check that the stack map is in the requested range.
124 uint32_t dex_pc = stack_map.GetDexPc(encoding);
125 if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
126 continue;
127 }
128
129 // Find the location of the dex register.
130 DexRegisterLocation reg_lo = DexRegisterLocation::None();
131 DexRegisterLocation reg_hi = DexRegisterLocation::None();
David Srbecky2ed15b62016-03-04 11:34:46 +0000132 DCHECK_LT(stack_map_index, dex_register_maps.size());
133 DexRegisterMap dex_register_map = dex_register_maps[stack_map_index];
134 if (dex_register_map.IsValid()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000135 reg_lo = dex_register_map.GetDexRegisterLocation(
136 vreg, method_info->code_item->registers_size_, code_info, encoding);
137 if (is64bitValue) {
138 reg_hi = dex_register_map.GetDexRegisterLocation(
139 vreg + 1, method_info->code_item->registers_size_, code_info, encoding);
140 }
141 }
142
143 // Add location entry for this address range.
144 if (!variable_locations.empty() &&
145 variable_locations.back().reg_lo == reg_lo &&
146 variable_locations.back().reg_hi == reg_hi &&
147 variable_locations.back().high_pc == low_pc) {
148 // Merge with the previous entry (extend its range).
149 variable_locations.back().high_pc = high_pc;
David Srbeckybfd26cd2016-02-10 13:57:09 +0000150 } else if (!variable_locations.empty() && reg_lo == DexRegisterLocation::None()) {
151 // Unknown location - use the last known location as best-effort guess.
152 variable_locations.back().high_pc = high_pc;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000153 } else {
154 variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
155 }
156 }
157
158 return variable_locations;
159}
160
161// Write table into .debug_loc which describes location of dex register.
162// The dex register might be valid only at some points and it might
163// move between machine registers and stack.
164static void WriteDebugLocEntry(const MethodDebugInfo* method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000165 const std::vector<DexRegisterMap>& dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000166 uint16_t vreg,
167 bool is64bitValue,
168 uint32_t compilation_unit_low_pc,
169 uint32_t dex_pc_low,
170 uint32_t dex_pc_high,
171 InstructionSet isa,
172 dwarf::DebugInfoEntryWriter<>* debug_info,
173 std::vector<uint8_t>* debug_loc_buffer,
174 std::vector<uint8_t>* debug_ranges_buffer) {
175 using Kind = DexRegisterLocation::Kind;
176 if (!method_info->IsFromOptimizingCompiler()) {
177 return;
178 }
179
David Srbeckyc5bfa972016-02-05 15:49:10 +0000180 std::vector<VariableLocation> variable_locations = GetVariableLocations(
181 method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000182 dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000183 vreg,
184 is64bitValue,
185 dex_pc_low,
186 dex_pc_high);
187
188 // Write .debug_loc entries.
David Srbeckyb396c732016-02-10 14:35:34 +0000189 dwarf::Writer<> debug_loc(debug_loc_buffer);
190 const size_t debug_loc_offset = debug_loc.size();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000191 const bool is64bit = Is64BitInstructionSet(isa);
192 std::vector<uint8_t> expr_buffer;
193 for (const VariableLocation& variable_location : variable_locations) {
194 // Translate dex register location to DWARF expression.
195 // Note that 64-bit value might be split to two distinct locations.
196 // (for example, two 32-bit machine registers, or even stack and register)
197 dwarf::Expression expr(&expr_buffer);
198 DexRegisterLocation reg_lo = variable_location.reg_lo;
199 DexRegisterLocation reg_hi = variable_location.reg_hi;
200 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
201 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
202 const Kind kind = reg_loc.GetKind();
203 const int32_t value = reg_loc.GetValue();
204 if (kind == Kind::kInStack) {
205 const size_t frame_size = method_info->compiled_method->GetFrameSizeInBytes();
206 // The stack offset is relative to SP. Make it relative to CFA.
207 expr.WriteOpFbreg(value - frame_size);
208 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
209 reg_hi.GetValue() == value + 4) {
210 break; // the high word is correctly implied by the low word.
211 }
212 } else if (kind == Kind::kInRegister) {
213 expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
214 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
215 reg_hi.GetValue() == value) {
216 break; // the high word is correctly implied by the low word.
217 }
218 } else if (kind == Kind::kInFpuRegister) {
219 if ((isa == kArm || isa == kThumb2) &&
220 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
221 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
222 // Translate S register pair to D register (e.g. S4+S5 to D2).
223 expr.WriteOpReg(Reg::ArmDp(value / 2).num());
224 break;
225 }
226 expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
227 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
228 reg_hi.GetValue() == reg_lo.GetValue()) {
229 break; // the high word is correctly implied by the low word.
230 }
231 } else if (kind == Kind::kConstant) {
232 expr.WriteOpConsts(value);
233 expr.WriteOpStackValue();
234 } else if (kind == Kind::kNone) {
235 break;
236 } else {
237 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
238 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
239 // the special cases above and they should not occur alone.
David Srbecky7dc11782016-02-25 13:23:56 +0000240 LOG(ERROR) << "Unexpected register location kind: " << kind;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000241 break;
242 }
243 if (is64bitValue) {
244 // Write the marker which is needed by split 64-bit values.
245 // This code is skipped by the special cases.
246 expr.WriteOpPiece(4);
247 }
248 }
249
250 if (expr.size() > 0) {
251 if (is64bit) {
252 debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
253 debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
254 } else {
255 debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
256 debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
257 }
258 // Write the expression.
259 debug_loc.PushUint16(expr.size());
260 debug_loc.PushData(expr.data());
261 } else {
262 // Do not generate .debug_loc if the location is not known.
263 }
264 }
265 // Write end-of-list entry.
266 if (is64bit) {
267 debug_loc.PushUint64(0);
268 debug_loc.PushUint64(0);
269 } else {
270 debug_loc.PushUint32(0);
271 debug_loc.PushUint32(0);
272 }
273
274 // Write .debug_ranges entries.
275 // This includes ranges where the variable is in scope but the location is not known.
David Srbeckyb396c732016-02-10 14:35:34 +0000276 dwarf::Writer<> debug_ranges(debug_ranges_buffer);
277 size_t debug_ranges_offset = debug_ranges.size();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000278 for (size_t i = 0; i < variable_locations.size(); i++) {
279 uint32_t low_pc = variable_locations[i].low_pc;
280 uint32_t high_pc = variable_locations[i].high_pc;
281 while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
282 // Merge address range with the next entry.
283 high_pc = variable_locations[++i].high_pc;
284 }
285 if (is64bit) {
286 debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
287 debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
288 } else {
289 debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
290 debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
291 }
292 }
293 // Write end-of-list entry.
294 if (is64bit) {
295 debug_ranges.PushUint64(0);
296 debug_ranges.PushUint64(0);
297 } else {
298 debug_ranges.PushUint32(0);
299 debug_ranges.PushUint32(0);
300 }
David Srbeckyb396c732016-02-10 14:35:34 +0000301
302 // Simple de-duplication - check whether this entry is same as the last one (or tail of it).
303 size_t debug_ranges_entry_size = debug_ranges.size() - debug_ranges_offset;
304 if (debug_ranges_offset >= debug_ranges_entry_size) {
305 size_t previous_offset = debug_ranges_offset - debug_ranges_entry_size;
306 if (memcmp(debug_ranges_buffer->data() + previous_offset,
307 debug_ranges_buffer->data() + debug_ranges_offset,
308 debug_ranges_entry_size) == 0) {
309 // Remove what we have just written and use the last entry instead.
310 debug_ranges_buffer->resize(debug_ranges_offset);
311 debug_ranges_offset = previous_offset;
312 }
313 }
314
315 // Write attributes to .debug_info.
316 debug_info->WriteSecOffset(dwarf::DW_AT_location, debug_loc_offset);
317 debug_info->WriteSecOffset(dwarf::DW_AT_start_scope, debug_ranges_offset);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000318}
319
320} // namespace debug
321} // namespace art
322
323#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
324