blob: 59a89e12b84c63e7a14d3b67c01973e985c677b9 [file] [log] [blame]
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001/*
2 * Copyright (C) 2015 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#include "stack_map.h"
18
David Srbecky71ec1cc2018-05-18 15:57:25 +010019#include <iomanip>
Nicolas Geoffray896f8f72015-03-30 15:44:25 +010020#include <stdint.h>
21
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000022#include "art_method.h"
David Sehr9c4a0152018-04-05 12:23:54 -070023#include "base/indenter.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000024#include "scoped_thread_state_change-inl.h"
Roland Levillain0396ed72015-05-27 15:12:19 +010025
Nicolas Geoffray004c2302015-03-20 10:06:38 +000026namespace art {
27
David Srbecky6de88332018-06-03 12:00:11 +010028// Scan backward to determine dex register locations at given stack map.
29// All registers for a stack map are combined - inlined registers are just appended,
30// therefore 'first_dex_register' allows us to select a sub-range to decode.
31void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
32 uint32_t first_dex_register,
33 /*out*/ DexRegisterMap* map) const {
34 // Count remaining work so we know when we have finished.
35 uint32_t remaining_registers = map->size();
36
37 // Keep scanning backwards and collect the most recent location of each register.
38 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
39 StackMap stack_map = GetStackMapAt(s);
40 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
41
42 // The mask specifies which registers where modified in this stack map.
43 // NB: the mask can be shorter than expected if trailing zero bits were removed.
44 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
45 if (mask_index == StackMap::kNoValue) {
46 continue; // Nothing changed at this stack map.
47 }
48 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
49 if (mask.size_in_bits() <= first_dex_register) {
50 continue; // Nothing changed after the first register we are interested in.
51 }
52
53 // The map stores one catalogue index per each modified register location.
54 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
55 DCHECK_NE(map_index, StackMap::kNoValue);
56
57 // Skip initial registers which we are not interested in (to get to inlined registers).
58 map_index += mask.PopCount(0, first_dex_register);
59 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
60
61 // Update registers that we see for first time (i.e. most recent value).
62 DexRegisterLocation* regs = map->data();
63 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
64 const size_t kNumBits = BitSizeOf<uint32_t>();
65 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
66 // Process the mask in chunks of kNumBits for performance.
67 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
68 while (bits != 0) {
69 uint32_t bit = CTZ(bits);
70 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
71 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
72 remaining_registers--;
73 }
74 map_index++;
75 bits ^= 1u << bit; // Clear the bit.
76 }
77 }
78 }
79
80 // Set any remaining registers to None (which is the default state at first stack map).
81 if (remaining_registers != 0) {
82 DexRegisterLocation* regs = map->data();
83 for (uint32_t r = 0; r < map->size(); r++) {
84 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
85 regs[r] = DexRegisterLocation::None();
86 }
87 }
88 }
89}
90
David Srbecky71ec1cc2018-05-18 15:57:25 +010091std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg) {
David Srbecky7dc11782016-02-25 13:23:56 +000092 using Kind = DexRegisterLocation::Kind;
David Srbecky71ec1cc2018-05-18 15:57:25 +010093 switch (reg.GetKind()) {
David Srbecky7dc11782016-02-25 13:23:56 +000094 case Kind::kNone:
David Srbecky71ec1cc2018-05-18 15:57:25 +010095 return stream << "None";
David Srbecky7dc11782016-02-25 13:23:56 +000096 case Kind::kInStack:
David Srbecky71ec1cc2018-05-18 15:57:25 +010097 return stream << "sp+" << reg.GetValue();
David Srbecky7dc11782016-02-25 13:23:56 +000098 case Kind::kInRegister:
David Srbecky71ec1cc2018-05-18 15:57:25 +010099 return stream << "r" << reg.GetValue();
David Srbecky7dc11782016-02-25 13:23:56 +0000100 case Kind::kInRegisterHigh:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100101 return stream << "r" << reg.GetValue() << "/hi";
David Srbecky7dc11782016-02-25 13:23:56 +0000102 case Kind::kInFpuRegister:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100103 return stream << "f" << reg.GetValue();
David Srbecky7dc11782016-02-25 13:23:56 +0000104 case Kind::kInFpuRegisterHigh:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100105 return stream << "f" << reg.GetValue() << "/hi";
David Srbecky7dc11782016-02-25 13:23:56 +0000106 case Kind::kConstant:
David Srbecky71ec1cc2018-05-18 15:57:25 +0100107 return stream << "#" << reg.GetValue();
David Srbecky6de88332018-06-03 12:00:11 +0100108 case Kind::kInvalid:
109 return stream << "Invalid";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100110 default:
111 return stream << "DexRegisterLocation(" << static_cast<uint32_t>(reg.GetKind())
112 << "," << reg.GetValue() << ")";
David Srbecky7dc11782016-02-25 13:23:56 +0000113 }
David Srbecky7dc11782016-02-25 13:23:56 +0000114}
115
David Srbecky71ec1cc2018-05-18 15:57:25 +0100116static void DumpDexRegisterMap(VariableIndentationOutputStream* vios,
117 const DexRegisterMap& map) {
118 if (map.IsValid()) {
119 ScopedIndentation indent1(vios);
120 for (size_t i = 0; i < map.size(); ++i) {
121 if (map.IsDexRegisterLive(i)) {
122 vios->Stream() << "v" << i << ":" << map.Get(i) << " ";
123 }
124 }
125 vios->Stream() << "\n";
126 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000127}
128
David Srbecky71ec1cc2018-05-18 15:57:25 +0100129template<uint32_t kNumColumns>
130static void DumpTable(VariableIndentationOutputStream* vios,
131 const char* table_name,
132 const BitTable<kNumColumns>& table,
133 bool verbose,
134 bool is_mask = false) {
135 if (table.NumRows() != 0) {
136 vios->Stream() << table_name << " BitSize=" << table.NumRows() * table.NumRowBits();
137 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
138 for (size_t c = 0; c < table.NumColumns(); c++) {
139 vios->Stream() << (c != 0 ? " " : "");
140 vios->Stream() << table.NumColumnBits(c);
141 }
142 vios->Stream() << "}\n";
143 if (verbose) {
144 ScopedIndentation indent1(vios);
145 for (size_t r = 0; r < table.NumRows(); r++) {
146 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
147 for (size_t c = 0; c < table.NumColumns(); c++) {
148 vios->Stream() << (c != 0 ? " " : "");
149 if (is_mask) {
150 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
151 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
152 vios->Stream() << bits.LoadBit(e - b - 1);
153 }
154 } else {
155 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
156 }
157 }
158 vios->Stream() << "}\n";
159 }
160 }
161 }
David Srbecky61b28a12016-02-25 21:55:03 +0000162}
163
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100164void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100165 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100166 uint16_t num_dex_registers,
167 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700168 InstructionSet instruction_set,
169 const MethodInfo& method_info) const {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100170 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100171 << "CodeInfo"
172 << " BitSize=" << size_ * kBitsPerByte
173 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100174 ScopedIndentation indent1(vios);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100175 DumpTable(vios, "StackMaps", stack_maps_, verbose);
176 DumpTable(vios, "RegisterMasks", register_masks_, verbose);
177 DumpTable(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */);
178 DumpTable(vios, "InvokeInfos", invoke_infos_, verbose);
179 DumpTable(vios, "InlineInfos", inline_infos_, verbose);
180 DumpTable(vios, "DexRegisterMasks", dex_register_masks_, verbose, true /* is_mask */);
181 DumpTable(vios, "DexRegisterMaps", dex_register_maps_, verbose);
182 DumpTable(vios, "DexRegisterCatalog", dex_register_catalog_, verbose);
183
Roland Levillaina552e1c2015-03-26 15:01:03 +0000184 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100185 if (verbose) {
186 for (size_t i = 0; i < GetNumberOfStackMaps(); ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100187 StackMap stack_map = GetStackMapAt(i);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100188 stack_map.Dump(vios, *this, method_info, code_offset, num_dex_registers, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100189 }
190 }
191}
192
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100193void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100194 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700195 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100196 uint32_t code_offset,
197 uint16_t number_of_dex_registers,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100198 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100199 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100200 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100201 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100202 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100203 << " (native_pc=0x" << code_offset + pc_offset
204 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100205 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100206 << std::dec
207 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100208 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000209 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000210 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100211 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100212 vios->Stream() << ")\n";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100213 DumpDexRegisterMap(vios, code_info.GetDexRegisterMapOf(*this, number_of_dex_registers));
David Srbecky6e69e522018-06-03 12:00:14 +0100214 uint32_t depth = code_info.GetInlineDepthOf(*this);
215 for (size_t d = 0; d < depth; d++) {
216 InlineInfo inline_info = code_info.GetInlineInfoAtDepth(*this, d);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100217 // We do not know the length of the dex register maps of inlined frames
218 // at this level, so we just pass null to `InlineInfo::Dump` to tell
219 // it not to look at these maps.
David Srbecky6e69e522018-06-03 12:00:14 +0100220 inline_info.Dump(vios, code_info, *this, method_info, 0);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100221 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100222}
223
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100224void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100225 const CodeInfo& code_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100226 const StackMap& stack_map,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700227 const MethodInfo& method_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100228 uint16_t number_of_dex_registers) const {
229 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
230 vios->Stream()
231 << "InlineInfo[" << Row() << "]"
232 << " (depth=" << depth
233 << std::hex
234 << ", dex_pc=0x" << GetDexPc();
235 if (EncodesArtMethod()) {
236 ScopedObjectAccess soa(Thread::Current());
237 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
238 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100239 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100240 << std::dec
241 << ", method_index=" << GetMethodIndex(method_info);
242 }
243 vios->Stream() << ")\n";
244 if (number_of_dex_registers != 0) {
245 uint16_t vregs = number_of_dex_registers;
246 DumpDexRegisterMap(vios, code_info.GetDexRegisterMapAtDepth(depth, stack_map, vregs));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100247 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000248}
249
250} // namespace art