blob: 43609e80bd1b25b2b321985dc2e8571da976ecca [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"
David Srbecky86decb62018-06-05 06:41:10 +010024#include "base/stats.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000025#include "scoped_thread_state_change-inl.h"
Roland Levillain0396ed72015-05-27 15:12:19 +010026
Nicolas Geoffray004c2302015-03-20 10:06:38 +000027namespace art {
28
David Srbecky6de88332018-06-03 12:00:11 +010029// Scan backward to determine dex register locations at given stack map.
30// All registers for a stack map are combined - inlined registers are just appended,
31// therefore 'first_dex_register' allows us to select a sub-range to decode.
32void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index,
33 uint32_t first_dex_register,
34 /*out*/ DexRegisterMap* map) const {
35 // Count remaining work so we know when we have finished.
36 uint32_t remaining_registers = map->size();
37
38 // Keep scanning backwards and collect the most recent location of each register.
39 for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) {
40 StackMap stack_map = GetStackMapAt(s);
41 DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search";
42
43 // The mask specifies which registers where modified in this stack map.
44 // NB: the mask can be shorter than expected if trailing zero bits were removed.
45 uint32_t mask_index = stack_map.GetDexRegisterMaskIndex();
46 if (mask_index == StackMap::kNoValue) {
47 continue; // Nothing changed at this stack map.
48 }
49 BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index);
50 if (mask.size_in_bits() <= first_dex_register) {
51 continue; // Nothing changed after the first register we are interested in.
52 }
53
54 // The map stores one catalogue index per each modified register location.
55 uint32_t map_index = stack_map.GetDexRegisterMapIndex();
56 DCHECK_NE(map_index, StackMap::kNoValue);
57
58 // Skip initial registers which we are not interested in (to get to inlined registers).
59 map_index += mask.PopCount(0, first_dex_register);
60 mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register);
61
62 // Update registers that we see for first time (i.e. most recent value).
63 DexRegisterLocation* regs = map->data();
64 const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits());
65 const size_t kNumBits = BitSizeOf<uint32_t>();
66 for (uint32_t reg = 0; reg < end; reg += kNumBits) {
67 // Process the mask in chunks of kNumBits for performance.
68 uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits));
69 while (bits != 0) {
70 uint32_t bit = CTZ(bits);
71 if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) {
72 regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index));
73 remaining_registers--;
74 }
75 map_index++;
76 bits ^= 1u << bit; // Clear the bit.
77 }
78 }
79 }
80
81 // Set any remaining registers to None (which is the default state at first stack map).
82 if (remaining_registers != 0) {
83 DexRegisterLocation* regs = map->data();
84 for (uint32_t r = 0; r < map->size(); r++) {
85 if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) {
86 regs[r] = DexRegisterLocation::None();
87 }
88 }
89 }
90}
91
David Srbecky86decb62018-06-05 06:41:10 +010092template<typename Accessor>
93static void AddTableSizeStats(const char* table_name,
94 const BitTable<Accessor::kCount>& table,
95 /*out*/ Stats* parent) {
96 Stats* table_stats = parent->Child(table_name);
97 table_stats->AddBits(table.BitSize());
98 table_stats->Child("Header")->AddBits(table.HeaderBitSize());
99 const char* const* column_names = GetBitTableColumnNames<Accessor>();
100 for (size_t c = 0; c < table.NumColumns(); c++) {
101 if (table.NumColumnBits(c) > 0) {
102 Stats* column_stats = table_stats->Child(column_names[c]);
103 column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows());
104 }
105 }
106}
107
108void CodeInfo::AddSizeStats(/*out*/ Stats* parent) const {
109 Stats* stats = parent->Child("CodeInfo");
110 stats->AddBytes(size_);
111 stats->Child("Header")->AddBytes(UnsignedLeb128Size(size_));
112 AddTableSizeStats<StackMap>("StackMaps", stack_maps_, stats);
113 AddTableSizeStats<RegisterMask>("RegisterMasks", register_masks_, stats);
114 AddTableSizeStats<MaskInfo>("StackMasks", stack_masks_, stats);
115 AddTableSizeStats<InvokeInfo>("InvokeInfos", invoke_infos_, stats);
116 AddTableSizeStats<InlineInfo>("InlineInfos", inline_infos_, stats);
117 AddTableSizeStats<MaskInfo>("DexRegisterMasks", dex_register_masks_, stats);
118 AddTableSizeStats<DexRegisterMapInfo>("DexRegisterMaps", dex_register_maps_, stats);
119 AddTableSizeStats<DexRegisterInfo>("DexRegisterCatalog", dex_register_catalog_, stats);
120}
121
David Srbeckye1402122018-06-13 18:20:45 +0100122void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
123 if (HasAnyLiveDexRegisters()) {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100124 ScopedIndentation indent1(vios);
David Srbeckye1402122018-06-13 18:20:45 +0100125 for (size_t i = 0; i < size(); ++i) {
126 DexRegisterLocation reg = (*this)[i];
127 if (reg.IsLive()) {
128 vios->Stream() << "v" << i << ":" << reg << " ";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100129 }
130 }
131 vios->Stream() << "\n";
132 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000133}
134
David Srbecky86decb62018-06-05 06:41:10 +0100135template<typename Accessor>
David Srbecky71ec1cc2018-05-18 15:57:25 +0100136static void DumpTable(VariableIndentationOutputStream* vios,
137 const char* table_name,
David Srbecky86decb62018-06-05 06:41:10 +0100138 const BitTable<Accessor::kCount>& table,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100139 bool verbose,
140 bool is_mask = false) {
141 if (table.NumRows() != 0) {
David Srbecky86decb62018-06-05 06:41:10 +0100142 vios->Stream() << table_name << " BitSize=" << table.BitSize();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100143 vios->Stream() << " Rows=" << table.NumRows() << " Bits={";
David Srbecky86decb62018-06-05 06:41:10 +0100144 const char* const* column_names = GetBitTableColumnNames<Accessor>();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100145 for (size_t c = 0; c < table.NumColumns(); c++) {
146 vios->Stream() << (c != 0 ? " " : "");
David Srbecky86decb62018-06-05 06:41:10 +0100147 vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100148 }
149 vios->Stream() << "}\n";
150 if (verbose) {
151 ScopedIndentation indent1(vios);
152 for (size_t r = 0; r < table.NumRows(); r++) {
153 vios->Stream() << "[" << std::right << std::setw(3) << r << "]={";
154 for (size_t c = 0; c < table.NumColumns(); c++) {
155 vios->Stream() << (c != 0 ? " " : "");
156 if (is_mask) {
157 BitMemoryRegion bits = table.GetBitMemoryRegion(r, c);
158 for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) {
159 vios->Stream() << bits.LoadBit(e - b - 1);
160 }
161 } else {
162 vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c));
163 }
164 }
165 vios->Stream() << "}\n";
166 }
167 }
168 }
David Srbecky61b28a12016-02-25 21:55:03 +0000169}
170
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100171void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100172 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100173 bool verbose,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700174 InstructionSet instruction_set,
175 const MethodInfo& method_info) const {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100176 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100177 << "CodeInfo"
178 << " BitSize=" << size_ * kBitsPerByte
179 << "\n";
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100180 ScopedIndentation indent1(vios);
David Srbecky86decb62018-06-05 06:41:10 +0100181 DumpTable<StackMap>(vios, "StackMaps", stack_maps_, verbose);
182 DumpTable<RegisterMask>(vios, "RegisterMasks", register_masks_, verbose);
183 DumpTable<MaskInfo>(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */);
184 DumpTable<InvokeInfo>(vios, "InvokeInfos", invoke_infos_, verbose);
185 DumpTable<InlineInfo>(vios, "InlineInfos", inline_infos_, verbose);
186 DumpTable<MaskInfo>(vios, "DexRegisterMasks", dex_register_masks_, verbose, true /* is_mask */);
187 DumpTable<DexRegisterMapInfo>(vios, "DexRegisterMaps", dex_register_maps_, verbose);
188 DumpTable<DexRegisterInfo>(vios, "DexRegisterCatalog", dex_register_catalog_, verbose);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100189
Roland Levillaina552e1c2015-03-26 15:01:03 +0000190 // Display stack maps along with (live) Dex register maps.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100191 if (verbose) {
192 for (size_t i = 0; i < GetNumberOfStackMaps(); ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100193 StackMap stack_map = GetStackMapAt(i);
David Srbeckyfd89b072018-06-03 12:00:22 +0100194 stack_map.Dump(vios, *this, method_info, code_offset, instruction_set);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100195 }
196 }
197}
198
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100199void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100200 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700201 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100202 uint32_t code_offset,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100203 InstructionSet instruction_set) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100204 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100205 vios->Stream()
David Srbecky71ec1cc2018-05-18 15:57:25 +0100206 << "StackMap[" << Row() << "]"
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100207 << std::hex
David Srbecky71ec1cc2018-05-18 15:57:25 +0100208 << " (native_pc=0x" << code_offset + pc_offset
209 << ", dex_pc=0x" << GetDexPc()
David Srbecky052f8ca2018-04-26 15:42:54 +0100210 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100211 << std::dec
212 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100213 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000214 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000215 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100216 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100217 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100218 code_info.GetDexRegisterMapOf(*this).Dump(vios);
David Srbecky6e69e522018-06-03 12:00:14 +0100219 uint32_t depth = code_info.GetInlineDepthOf(*this);
220 for (size_t d = 0; d < depth; d++) {
221 InlineInfo inline_info = code_info.GetInlineInfoAtDepth(*this, d);
David Srbeckyfd89b072018-06-03 12:00:22 +0100222 inline_info.Dump(vios, code_info, *this, method_info);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100223 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100224}
225
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100226void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100227 const CodeInfo& code_info,
David Srbecky6e69e522018-06-03 12:00:14 +0100228 const StackMap& stack_map,
David Srbeckyfd89b072018-06-03 12:00:22 +0100229 const MethodInfo& method_info) const {
David Srbecky6e69e522018-06-03 12:00:14 +0100230 uint32_t depth = Row() - stack_map.GetInlineInfoIndex();
231 vios->Stream()
232 << "InlineInfo[" << Row() << "]"
233 << " (depth=" << depth
234 << std::hex
235 << ", dex_pc=0x" << GetDexPc();
236 if (EncodesArtMethod()) {
237 ScopedObjectAccess soa(Thread::Current());
238 vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod();
239 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100240 vios->Stream()
David Srbecky6e69e522018-06-03 12:00:14 +0100241 << std::dec
242 << ", method_index=" << GetMethodIndex(method_info);
243 }
244 vios->Stream() << ")\n";
David Srbeckye1402122018-06-13 18:20:45 +0100245 code_info.GetDexRegisterMapAtDepth(depth, stack_map).Dump(vios);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000246}
247
248} // namespace art