• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOC_WRITER_H_
18 #define ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
19 
20 #include <cstring>
21 #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 
30 namespace art {
31 namespace debug {
32 using Reg = dwarf::Reg;
33 
GetDwarfCoreReg(InstructionSet isa,int machine_reg)34 static 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 
GetDwarfFpReg(InstructionSet isa,int machine_reg)55 static 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 
76 struct VariableLocation {
77   uint32_t low_pc;  // Relative to compilation unit.
78   uint32_t high_pc;  // Relative to compilation unit.
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.
86 // PCs corresponding to stackmap with dex register map are accurate,
87 // all other PCs are best-effort only.
GetVariableLocations(const MethodDebugInfo * method_info,const std::vector<DexRegisterMap> & dex_register_maps,uint16_t vreg,bool is64bitValue,uint64_t compilation_unit_code_address,uint32_t dex_pc_low,uint32_t dex_pc_high,InstructionSet isa)88 std::vector<VariableLocation> GetVariableLocations(
89     const MethodDebugInfo* method_info,
90     const std::vector<DexRegisterMap>& dex_register_maps,
91     uint16_t vreg,
92     bool is64bitValue,
93     uint64_t compilation_unit_code_address,
94     uint32_t dex_pc_low,
95     uint32_t dex_pc_high,
96     InstructionSet isa) {
97   std::vector<VariableLocation> variable_locations;
98 
99   // Get stack maps sorted by pc (they might not be sorted internally).
100   // TODO(dsrbecky) Remove this once stackmaps get sorted by pc.
101   const CodeInfo code_info(method_info->code_info);
102   const CodeInfoEncoding encoding = code_info.ExtractEncoding();
103   std::map<uint32_t, uint32_t> stack_maps;  // low_pc -> stack_map_index.
104   for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(encoding); s++) {
105     StackMap stack_map = code_info.GetStackMapAt(s, encoding);
106     DCHECK(stack_map.IsValid());
107     if (!stack_map.HasDexRegisterMap(encoding.stack_map.encoding)) {
108       // The compiler creates stackmaps without register maps at the start of
109       // basic blocks in order to keep instruction-accurate line number mapping.
110       // However, we never stop at those (breakpoint locations always have map).
111       // Therefore, for the purpose of local variables, we ignore them.
112       // The main reason for this is to save space by avoiding undefined gaps.
113       continue;
114     }
115     const uint32_t pc_offset = stack_map.GetNativePcOffset(encoding.stack_map.encoding, isa);
116     DCHECK_LE(pc_offset, method_info->code_size);
117     DCHECK_LE(compilation_unit_code_address, method_info->code_address);
118     const uint32_t low_pc = dchecked_integral_cast<uint32_t>(
119         method_info->code_address + pc_offset - compilation_unit_code_address);
120     stack_maps.emplace(low_pc, s);
121   }
122 
123   // Create entries for the requested register based on stack map data.
124   for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
125     const uint32_t low_pc = it->first;
126     const uint32_t stack_map_index = it->second;
127     const StackMap& stack_map = code_info.GetStackMapAt(stack_map_index, encoding);
128     auto next_it = it;
129     next_it++;
130     const uint32_t high_pc = next_it != stack_maps.end()
131       ? next_it->first
132       : method_info->code_address + method_info->code_size - compilation_unit_code_address;
133     DCHECK_LE(low_pc, high_pc);
134     if (low_pc == high_pc) {
135       continue;  // Ignore if the address range is empty.
136     }
137 
138     // Check that the stack map is in the requested range.
139     uint32_t dex_pc = stack_map.GetDexPc(encoding.stack_map.encoding);
140     if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
141       // The variable is not in scope at this PC. Therefore omit the entry.
142       // Note that this is different to None() entry which means in scope, but unknown location.
143       continue;
144     }
145 
146     // Find the location of the dex register.
147     DexRegisterLocation reg_lo = DexRegisterLocation::None();
148     DexRegisterLocation reg_hi = DexRegisterLocation::None();
149     DCHECK_LT(stack_map_index, dex_register_maps.size());
150     DexRegisterMap dex_register_map = dex_register_maps[stack_map_index];
151     DCHECK(dex_register_map.IsValid());
152     reg_lo = dex_register_map.GetDexRegisterLocation(
153         vreg, method_info->code_item->registers_size_, code_info, encoding);
154     if (is64bitValue) {
155       reg_hi = dex_register_map.GetDexRegisterLocation(
156           vreg + 1, method_info->code_item->registers_size_, code_info, encoding);
157     }
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;
166     } 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.
WriteDebugLocEntry(const MethodDebugInfo * method_info,const std::vector<DexRegisterMap> & dex_register_maps,uint16_t vreg,bool is64bitValue,uint64_t compilation_unit_code_address,uint32_t dex_pc_low,uint32_t dex_pc_high,InstructionSet isa,dwarf::DebugInfoEntryWriter<> * debug_info,std::vector<uint8_t> * debug_loc_buffer,std::vector<uint8_t> * debug_ranges_buffer)177 static void WriteDebugLocEntry(const MethodDebugInfo* method_info,
178                                const std::vector<DexRegisterMap>& dex_register_maps,
179                                uint16_t vreg,
180                                bool is64bitValue,
181                                uint64_t compilation_unit_code_address,
182                                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;
189   if (method_info->code_info == nullptr || dex_register_maps.empty()) {
190     return;
191   }
192 
193   std::vector<VariableLocation> variable_locations = GetVariableLocations(
194       method_info,
195       dex_register_maps,
196       vreg,
197       is64bitValue,
198       compilation_unit_code_address,
199       dex_pc_low,
200       dex_pc_high,
201       isa);
202 
203   // Write .debug_loc entries.
204   dwarf::Writer<> debug_loc(debug_loc_buffer);
205   const size_t debug_loc_offset = debug_loc.size();
206   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) {
220         // The stack offset is relative to SP. Make it relative to CFA.
221         expr.WriteOpFbreg(value - method_info->frame_size_in_bytes);
222         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) {
233         if ((isa == kArm || isa == kThumb2) &&
234             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.
254         LOG(ERROR) << "Unexpected register location kind: " << kind;
255         break;
256       }
257       if (is64bitValue) {
258         // Write the marker which is needed by split 64-bit values.
259         // This code is skipped by the special cases.
260         expr.WriteOpPiece(4);
261       }
262     }
263 
264     if (expr.size() > 0) {
265       if (is64bit) {
266         debug_loc.PushUint64(variable_location.low_pc);
267         debug_loc.PushUint64(variable_location.high_pc);
268       } else {
269         debug_loc.PushUint32(variable_location.low_pc);
270         debug_loc.PushUint32(variable_location.high_pc);
271       }
272       // Write the expression.
273       debug_loc.PushUint16(expr.size());
274       debug_loc.PushData(expr.data());
275     } else {
276       // Do not generate .debug_loc if the location is not known.
277     }
278   }
279   // Write end-of-list entry.
280   if (is64bit) {
281     debug_loc.PushUint64(0);
282     debug_loc.PushUint64(0);
283   } else {
284     debug_loc.PushUint32(0);
285     debug_loc.PushUint32(0);
286   }
287 
288   // Write .debug_ranges entries.
289   // This includes ranges where the variable is in scope but the location is not known.
290   dwarf::Writer<> debug_ranges(debug_ranges_buffer);
291   size_t debug_ranges_offset = debug_ranges.size();
292   for (size_t i = 0; i < variable_locations.size(); i++) {
293     uint32_t low_pc = variable_locations[i].low_pc;
294     uint32_t high_pc = variable_locations[i].high_pc;
295     while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
296       // Merge address range with the next entry.
297       high_pc = variable_locations[++i].high_pc;
298     }
299     if (is64bit) {
300       debug_ranges.PushUint64(low_pc);
301       debug_ranges.PushUint64(high_pc);
302     } else {
303       debug_ranges.PushUint32(low_pc);
304       debug_ranges.PushUint32(high_pc);
305     }
306   }
307   // Write end-of-list entry.
308   if (is64bit) {
309     debug_ranges.PushUint64(0);
310     debug_ranges.PushUint64(0);
311   } else {
312     debug_ranges.PushUint32(0);
313     debug_ranges.PushUint32(0);
314   }
315 
316   // Simple de-duplication - check whether this entry is same as the last one (or tail of it).
317   size_t debug_ranges_entry_size = debug_ranges.size() - debug_ranges_offset;
318   if (debug_ranges_offset >= debug_ranges_entry_size) {
319     size_t previous_offset = debug_ranges_offset - debug_ranges_entry_size;
320     if (memcmp(debug_ranges_buffer->data() + previous_offset,
321                debug_ranges_buffer->data() + debug_ranges_offset,
322                debug_ranges_entry_size) == 0) {
323       // Remove what we have just written and use the last entry instead.
324       debug_ranges_buffer->resize(debug_ranges_offset);
325       debug_ranges_offset = previous_offset;
326     }
327   }
328 
329   // Write attributes to .debug_info.
330   debug_info->WriteSecOffset(dwarf::DW_AT_location, debug_loc_offset);
331   debug_info->WriteSecOffset(dwarf::DW_AT_start_scope, debug_ranges_offset);
332 }
333 
334 }  // namespace debug
335 }  // namespace art
336 
337 #endif  // ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
338 
339