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