blob: c1bf9152129fc888122d4b9f2903fb5e894b9e64 [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) {
Vladimir Marko33bff252017-11-01 14:35:42 +000036 case InstructionSet::kArm:
37 case InstructionSet::kThumb2:
David Srbeckyc5bfa972016-02-05 15:49:10 +000038 return Reg::ArmCore(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000039 case InstructionSet::kArm64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000040 return Reg::Arm64Core(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000041 case InstructionSet::kX86:
David Srbeckyc5bfa972016-02-05 15:49:10 +000042 return Reg::X86Core(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000043 case InstructionSet::kX86_64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000044 return Reg::X86_64Core(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000045 case InstructionSet::kMips:
David Srbeckyc5bfa972016-02-05 15:49:10 +000046 return Reg::MipsCore(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000047 case InstructionSet::kMips64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000048 return Reg::Mips64Core(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000049 case InstructionSet::kNone:
David Srbeckyc5bfa972016-02-05 15:49:10 +000050 LOG(FATAL) << "No instruction set";
51 }
52 UNREACHABLE();
53}
54
55static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
56 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +000057 case InstructionSet::kArm:
58 case InstructionSet::kThumb2:
David Srbeckyc5bfa972016-02-05 15:49:10 +000059 return Reg::ArmFp(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000060 case InstructionSet::kArm64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000061 return Reg::Arm64Fp(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000062 case InstructionSet::kX86:
David Srbeckyc5bfa972016-02-05 15:49:10 +000063 return Reg::X86Fp(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000064 case InstructionSet::kX86_64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000065 return Reg::X86_64Fp(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000066 case InstructionSet::kMips:
David Srbeckyc5bfa972016-02-05 15:49:10 +000067 return Reg::MipsFp(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000068 case InstructionSet::kMips64:
David Srbeckyc5bfa972016-02-05 15:49:10 +000069 return Reg::Mips64Fp(machine_reg);
Vladimir Marko33bff252017-11-01 14:35:42 +000070 case InstructionSet::kNone:
David Srbeckyc5bfa972016-02-05 15:49:10 +000071 LOG(FATAL) << "No instruction set";
72 }
73 UNREACHABLE();
74}
75
76struct VariableLocation {
David Srbecky197160d2016-03-07 17:33:57 +000077 uint32_t low_pc; // Relative to compilation unit.
78 uint32_t high_pc; // Relative to compilation unit.
David Srbeckyc5bfa972016-02-05 15:49:10 +000079 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.
Andreas Gampe8ea4eec2017-05-30 13:53:03 -070088static std::vector<VariableLocation> GetVariableLocations(
David Srbecky2ed15b62016-03-04 11:34:46 +000089 const MethodDebugInfo* method_info,
90 const std::vector<DexRegisterMap>& dex_register_maps,
91 uint16_t vreg,
92 bool is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +000093 uint64_t compilation_unit_code_address,
David Srbecky2ed15b62016-03-04 11:34:46 +000094 uint32_t dex_pc_low,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080095 uint32_t dex_pc_high,
96 InstructionSet isa) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000097 std::vector<VariableLocation> variable_locations;
98
99 // Get stack maps sorted by pc (they might not be sorted internally).
David Srbeckyd89f6052016-03-12 21:10:04 +0000100 // TODO(dsrbecky) Remove this once stackmaps get sorted by pc.
David Srbecky197160d2016-03-07 17:33:57 +0000101 const CodeInfo code_info(method_info->code_info);
David Srbecky2ed15b62016-03-04 11:34:46 +0000102 std::map<uint32_t, uint32_t> stack_maps; // low_pc -> stack_map_index.
David Srbecky052f8ca2018-04-26 15:42:54 +0100103 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
104 StackMap stack_map = code_info.GetStackMapAt(s);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000105 DCHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +0100106 if (!stack_map.HasDexRegisterMap()) {
David Srbeckyd89f6052016-03-12 21:10:04 +0000107 // The compiler creates stackmaps without register maps at the start of
108 // basic blocks in order to keep instruction-accurate line number mapping.
109 // However, we never stop at those (breakpoint locations always have map).
110 // Therefore, for the purpose of local variables, we ignore them.
111 // The main reason for this is to save space by avoiding undefined gaps.
112 continue;
113 }
David Srbecky052f8ca2018-04-26 15:42:54 +0100114 const uint32_t pc_offset = stack_map.GetNativePcOffset(isa);
David Srbecky197160d2016-03-07 17:33:57 +0000115 DCHECK_LE(pc_offset, method_info->code_size);
116 DCHECK_LE(compilation_unit_code_address, method_info->code_address);
117 const uint32_t low_pc = dchecked_integral_cast<uint32_t>(
118 method_info->code_address + pc_offset - compilation_unit_code_address);
David Srbecky2ed15b62016-03-04 11:34:46 +0000119 stack_maps.emplace(low_pc, s);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000120 }
121
122 // Create entries for the requested register based on stack map data.
123 for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000124 const uint32_t low_pc = it->first;
David Srbecky2ed15b62016-03-04 11:34:46 +0000125 const uint32_t stack_map_index = it->second;
David Srbecky052f8ca2018-04-26 15:42:54 +0100126 const StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000127 auto next_it = it;
128 next_it++;
David Srbecky197160d2016-03-07 17:33:57 +0000129 const uint32_t high_pc = next_it != stack_maps.end()
130 ? next_it->first
131 : method_info->code_address + method_info->code_size - compilation_unit_code_address;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000132 DCHECK_LE(low_pc, high_pc);
133 if (low_pc == high_pc) {
134 continue; // Ignore if the address range is empty.
135 }
136
137 // Check that the stack map is in the requested range.
David Srbecky052f8ca2018-04-26 15:42:54 +0100138 uint32_t dex_pc = stack_map.GetDexPc();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000139 if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
David Srbeckyd89f6052016-03-12 21:10:04 +0000140 // The variable is not in scope at this PC. Therefore omit the entry.
141 // Note that this is different to None() entry which means in scope, but unknown location.
David Srbeckyc5bfa972016-02-05 15:49:10 +0000142 continue;
143 }
144
145 // Find the location of the dex register.
146 DexRegisterLocation reg_lo = DexRegisterLocation::None();
147 DexRegisterLocation reg_hi = DexRegisterLocation::None();
David Srbecky2ed15b62016-03-04 11:34:46 +0000148 DCHECK_LT(stack_map_index, dex_register_maps.size());
149 DexRegisterMap dex_register_map = dex_register_maps[stack_map_index];
David Srbeckyd89f6052016-03-12 21:10:04 +0000150 DCHECK(dex_register_map.IsValid());
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800151 CodeItemDataAccessor accessor(*method_info->dex_file, method_info->code_item);
David Srbeckyd89f6052016-03-12 21:10:04 +0000152 reg_lo = dex_register_map.GetDexRegisterLocation(
David Srbecky052f8ca2018-04-26 15:42:54 +0100153 vreg, accessor.RegistersSize(), code_info);
David Srbeckyd89f6052016-03-12 21:10:04 +0000154 if (is64bitValue) {
155 reg_hi = dex_register_map.GetDexRegisterLocation(
David Srbecky052f8ca2018-04-26 15:42:54 +0100156 vreg + 1, accessor.RegistersSize(), code_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000157 }
158
159 // Add location entry for this address range.
160 if (!variable_locations.empty() &&
161 variable_locations.back().reg_lo == reg_lo &&
162 variable_locations.back().reg_hi == reg_hi &&
163 variable_locations.back().high_pc == low_pc) {
164 // Merge with the previous entry (extend its range).
165 variable_locations.back().high_pc = high_pc;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000166 } else {
167 variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
168 }
169 }
170
171 return variable_locations;
172}
173
174// Write table into .debug_loc which describes location of dex register.
175// The dex register might be valid only at some points and it might
176// move between machine registers and stack.
177static void WriteDebugLocEntry(const MethodDebugInfo* method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000178 const std::vector<DexRegisterMap>& dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000179 uint16_t vreg,
180 bool is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000181 uint64_t compilation_unit_code_address,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000182 uint32_t dex_pc_low,
183 uint32_t dex_pc_high,
184 InstructionSet isa,
185 dwarf::DebugInfoEntryWriter<>* debug_info,
186 std::vector<uint8_t>* debug_loc_buffer,
187 std::vector<uint8_t>* debug_ranges_buffer) {
188 using Kind = DexRegisterLocation::Kind;
David Srbecky197160d2016-03-07 17:33:57 +0000189 if (method_info->code_info == nullptr || dex_register_maps.empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000190 return;
191 }
192
David Srbeckyc5bfa972016-02-05 15:49:10 +0000193 std::vector<VariableLocation> variable_locations = GetVariableLocations(
194 method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000195 dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000196 vreg,
197 is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000198 compilation_unit_code_address,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000199 dex_pc_low,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800200 dex_pc_high,
201 isa);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000202
203 // Write .debug_loc entries.
David Srbeckyb396c732016-02-10 14:35:34 +0000204 dwarf::Writer<> debug_loc(debug_loc_buffer);
205 const size_t debug_loc_offset = debug_loc.size();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000206 const bool is64bit = Is64BitInstructionSet(isa);
207 std::vector<uint8_t> expr_buffer;
208 for (const VariableLocation& variable_location : variable_locations) {
209 // Translate dex register location to DWARF expression.
210 // Note that 64-bit value might be split to two distinct locations.
211 // (for example, two 32-bit machine registers, or even stack and register)
212 dwarf::Expression expr(&expr_buffer);
213 DexRegisterLocation reg_lo = variable_location.reg_lo;
214 DexRegisterLocation reg_hi = variable_location.reg_hi;
215 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
216 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
217 const Kind kind = reg_loc.GetKind();
218 const int32_t value = reg_loc.GetValue();
219 if (kind == Kind::kInStack) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000220 // The stack offset is relative to SP. Make it relative to CFA.
David Srbecky197160d2016-03-07 17:33:57 +0000221 expr.WriteOpFbreg(value - method_info->frame_size_in_bytes);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000222 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
223 reg_hi.GetValue() == value + 4) {
224 break; // the high word is correctly implied by the low word.
225 }
226 } else if (kind == Kind::kInRegister) {
227 expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
228 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
229 reg_hi.GetValue() == value) {
230 break; // the high word is correctly implied by the low word.
231 }
232 } else if (kind == Kind::kInFpuRegister) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000233 if ((isa == InstructionSet::kArm || isa == InstructionSet::kThumb2) &&
David Srbeckyc5bfa972016-02-05 15:49:10 +0000234 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
235 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
236 // Translate S register pair to D register (e.g. S4+S5 to D2).
237 expr.WriteOpReg(Reg::ArmDp(value / 2).num());
238 break;
239 }
240 expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
241 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
242 reg_hi.GetValue() == reg_lo.GetValue()) {
243 break; // the high word is correctly implied by the low word.
244 }
245 } else if (kind == Kind::kConstant) {
246 expr.WriteOpConsts(value);
247 expr.WriteOpStackValue();
248 } else if (kind == Kind::kNone) {
249 break;
250 } else {
251 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
252 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
253 // the special cases above and they should not occur alone.
David Srbecky566865d2017-11-29 14:32:43 +0000254 LOG(WARNING) << "Unexpected register location: " << kind
255 << " (This can indicate either a bug in the dexer when generating"
256 << " local variable information, or a bug in ART compiler."
257 << " Please file a bug at go/art-bug)";
David Srbeckyc5bfa972016-02-05 15:49:10 +0000258 break;
259 }
260 if (is64bitValue) {
261 // Write the marker which is needed by split 64-bit values.
262 // This code is skipped by the special cases.
263 expr.WriteOpPiece(4);
264 }
265 }
266
267 if (expr.size() > 0) {
268 if (is64bit) {
David Srbecky197160d2016-03-07 17:33:57 +0000269 debug_loc.PushUint64(variable_location.low_pc);
270 debug_loc.PushUint64(variable_location.high_pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000271 } else {
David Srbecky197160d2016-03-07 17:33:57 +0000272 debug_loc.PushUint32(variable_location.low_pc);
273 debug_loc.PushUint32(variable_location.high_pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000274 }
275 // Write the expression.
276 debug_loc.PushUint16(expr.size());
277 debug_loc.PushData(expr.data());
278 } else {
279 // Do not generate .debug_loc if the location is not known.
280 }
281 }
282 // Write end-of-list entry.
283 if (is64bit) {
284 debug_loc.PushUint64(0);
285 debug_loc.PushUint64(0);
286 } else {
287 debug_loc.PushUint32(0);
288 debug_loc.PushUint32(0);
289 }
290
291 // Write .debug_ranges entries.
292 // This includes ranges where the variable is in scope but the location is not known.
David Srbeckyb396c732016-02-10 14:35:34 +0000293 dwarf::Writer<> debug_ranges(debug_ranges_buffer);
294 size_t debug_ranges_offset = debug_ranges.size();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000295 for (size_t i = 0; i < variable_locations.size(); i++) {
296 uint32_t low_pc = variable_locations[i].low_pc;
297 uint32_t high_pc = variable_locations[i].high_pc;
298 while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
299 // Merge address range with the next entry.
300 high_pc = variable_locations[++i].high_pc;
301 }
302 if (is64bit) {
David Srbecky197160d2016-03-07 17:33:57 +0000303 debug_ranges.PushUint64(low_pc);
304 debug_ranges.PushUint64(high_pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000305 } else {
David Srbecky197160d2016-03-07 17:33:57 +0000306 debug_ranges.PushUint32(low_pc);
307 debug_ranges.PushUint32(high_pc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000308 }
309 }
310 // Write end-of-list entry.
311 if (is64bit) {
312 debug_ranges.PushUint64(0);
313 debug_ranges.PushUint64(0);
314 } else {
315 debug_ranges.PushUint32(0);
316 debug_ranges.PushUint32(0);
317 }
David Srbeckyb396c732016-02-10 14:35:34 +0000318
319 // Simple de-duplication - check whether this entry is same as the last one (or tail of it).
320 size_t debug_ranges_entry_size = debug_ranges.size() - debug_ranges_offset;
321 if (debug_ranges_offset >= debug_ranges_entry_size) {
322 size_t previous_offset = debug_ranges_offset - debug_ranges_entry_size;
323 if (memcmp(debug_ranges_buffer->data() + previous_offset,
324 debug_ranges_buffer->data() + debug_ranges_offset,
325 debug_ranges_entry_size) == 0) {
326 // Remove what we have just written and use the last entry instead.
327 debug_ranges_buffer->resize(debug_ranges_offset);
328 debug_ranges_offset = previous_offset;
329 }
330 }
331
332 // Write attributes to .debug_info.
333 debug_info->WriteSecOffset(dwarf::DW_AT_location, debug_loc_offset);
334 debug_info->WriteSecOffset(dwarf::DW_AT_start_scope, debug_ranges_offset);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000335}
336
337} // namespace debug
338} // namespace art
339
340#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
341