Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 1 | /* |
| 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 Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 19 | #include <iomanip> |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 22 | #include "art_method.h" |
David Sehr | 9c4a015 | 2018-04-05 12:23:54 -0700 | [diff] [blame] | 23 | #include "base/indenter.h" |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 24 | #include "base/stats.h" |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 26 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 27 | namespace art { |
| 28 | |
David Srbecky | 078d7ba | 2018-06-21 15:36:48 +0100 | [diff] [blame] | 29 | void CodeInfo::Decode(const uint8_t* data) { |
| 30 | size_t non_header_size = DecodeUnsignedLeb128(&data); |
| 31 | size_ = UnsignedLeb128Size(non_header_size) + non_header_size; |
| 32 | MemoryRegion region(const_cast<uint8_t*>(data), non_header_size); |
| 33 | BitMemoryReader reader(BitMemoryRegion(region), /* bit_offset */ 0); |
| 34 | stack_maps_.Decode(reader); |
| 35 | register_masks_.Decode(reader); |
| 36 | stack_masks_.Decode(reader); |
| 37 | invoke_infos_.Decode(reader); |
| 38 | inline_infos_.Decode(reader); |
| 39 | dex_register_masks_.Decode(reader); |
| 40 | dex_register_maps_.Decode(reader); |
| 41 | dex_register_catalog_.Decode(reader); |
| 42 | number_of_dex_registers_ = DecodeVarintBits(reader); |
| 43 | CHECK_EQ(non_header_size, BitsToBytesRoundUp(reader.GetBitOffset())) << "Invalid CodeInfo"; |
| 44 | } |
| 45 | |
David Srbecky | 0b4e5a3 | 2018-06-11 16:25:29 +0100 | [diff] [blame] | 46 | BitTable<StackMap>::const_iterator CodeInfo::BinarySearchNativePc(uint32_t packed_pc) const { |
| 47 | return std::partition_point( |
| 48 | stack_maps_.begin(), |
| 49 | stack_maps_.end(), |
| 50 | [packed_pc](const StackMap& sm) { |
| 51 | return sm.GetPackedNativePc() < packed_pc && sm.GetKind() != StackMap::Kind::Catch; |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | StackMap CodeInfo::GetStackMapForNativePcOffset(uint32_t pc, InstructionSet isa) const { |
| 56 | auto it = BinarySearchNativePc(StackMap::PackNativePc(pc, isa)); |
| 57 | // Start at the lower bound and iterate over all stack maps with the given native pc. |
| 58 | for (; it != stack_maps_.end() && (*it).GetNativePcOffset(isa) == pc; ++it) { |
| 59 | StackMap::Kind kind = static_cast<StackMap::Kind>((*it).GetKind()); |
| 60 | if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) { |
| 61 | return *it; |
| 62 | } |
| 63 | } |
David Srbecky | a45a85c | 2018-06-21 16:03:12 +0100 | [diff] [blame] | 64 | return stack_maps_.GetInvalidRow(); |
David Srbecky | 0b4e5a3 | 2018-06-11 16:25:29 +0100 | [diff] [blame] | 65 | } |
| 66 | |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame] | 67 | // Scan backward to determine dex register locations at given stack map. |
| 68 | // All registers for a stack map are combined - inlined registers are just appended, |
| 69 | // therefore 'first_dex_register' allows us to select a sub-range to decode. |
| 70 | void CodeInfo::DecodeDexRegisterMap(uint32_t stack_map_index, |
| 71 | uint32_t first_dex_register, |
| 72 | /*out*/ DexRegisterMap* map) const { |
| 73 | // Count remaining work so we know when we have finished. |
| 74 | uint32_t remaining_registers = map->size(); |
| 75 | |
| 76 | // Keep scanning backwards and collect the most recent location of each register. |
| 77 | for (int32_t s = stack_map_index; s >= 0 && remaining_registers != 0; s--) { |
| 78 | StackMap stack_map = GetStackMapAt(s); |
| 79 | DCHECK_LE(stack_map_index - s, kMaxDexRegisterMapSearchDistance) << "Unbounded search"; |
| 80 | |
| 81 | // The mask specifies which registers where modified in this stack map. |
| 82 | // NB: the mask can be shorter than expected if trailing zero bits were removed. |
| 83 | uint32_t mask_index = stack_map.GetDexRegisterMaskIndex(); |
| 84 | if (mask_index == StackMap::kNoValue) { |
| 85 | continue; // Nothing changed at this stack map. |
| 86 | } |
| 87 | BitMemoryRegion mask = dex_register_masks_.GetBitMemoryRegion(mask_index); |
| 88 | if (mask.size_in_bits() <= first_dex_register) { |
| 89 | continue; // Nothing changed after the first register we are interested in. |
| 90 | } |
| 91 | |
| 92 | // The map stores one catalogue index per each modified register location. |
| 93 | uint32_t map_index = stack_map.GetDexRegisterMapIndex(); |
| 94 | DCHECK_NE(map_index, StackMap::kNoValue); |
| 95 | |
| 96 | // Skip initial registers which we are not interested in (to get to inlined registers). |
| 97 | map_index += mask.PopCount(0, first_dex_register); |
| 98 | mask = mask.Subregion(first_dex_register, mask.size_in_bits() - first_dex_register); |
| 99 | |
| 100 | // Update registers that we see for first time (i.e. most recent value). |
| 101 | DexRegisterLocation* regs = map->data(); |
| 102 | const uint32_t end = std::min<uint32_t>(map->size(), mask.size_in_bits()); |
| 103 | const size_t kNumBits = BitSizeOf<uint32_t>(); |
| 104 | for (uint32_t reg = 0; reg < end; reg += kNumBits) { |
| 105 | // Process the mask in chunks of kNumBits for performance. |
| 106 | uint32_t bits = mask.LoadBits(reg, std::min<uint32_t>(end - reg, kNumBits)); |
| 107 | while (bits != 0) { |
| 108 | uint32_t bit = CTZ(bits); |
| 109 | if (regs[reg + bit].GetKind() == DexRegisterLocation::Kind::kInvalid) { |
| 110 | regs[reg + bit] = GetDexRegisterCatalogEntry(dex_register_maps_.Get(map_index)); |
| 111 | remaining_registers--; |
| 112 | } |
| 113 | map_index++; |
| 114 | bits ^= 1u << bit; // Clear the bit. |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Set any remaining registers to None (which is the default state at first stack map). |
| 120 | if (remaining_registers != 0) { |
| 121 | DexRegisterLocation* regs = map->data(); |
| 122 | for (uint32_t r = 0; r < map->size(); r++) { |
| 123 | if (regs[r].GetKind() == DexRegisterLocation::Kind::kInvalid) { |
| 124 | regs[r] = DexRegisterLocation::None(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 130 | template<typename Accessor> |
| 131 | static void AddTableSizeStats(const char* table_name, |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 132 | const BitTable<Accessor>& table, |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 133 | /*out*/ Stats* parent) { |
| 134 | Stats* table_stats = parent->Child(table_name); |
| 135 | table_stats->AddBits(table.BitSize()); |
| 136 | table_stats->Child("Header")->AddBits(table.HeaderBitSize()); |
| 137 | const char* const* column_names = GetBitTableColumnNames<Accessor>(); |
| 138 | for (size_t c = 0; c < table.NumColumns(); c++) { |
| 139 | if (table.NumColumnBits(c) > 0) { |
| 140 | Stats* column_stats = table_stats->Child(column_names[c]); |
| 141 | column_stats->AddBits(table.NumRows() * table.NumColumnBits(c), table.NumRows()); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void CodeInfo::AddSizeStats(/*out*/ Stats* parent) const { |
| 147 | Stats* stats = parent->Child("CodeInfo"); |
| 148 | stats->AddBytes(size_); |
| 149 | stats->Child("Header")->AddBytes(UnsignedLeb128Size(size_)); |
| 150 | AddTableSizeStats<StackMap>("StackMaps", stack_maps_, stats); |
| 151 | AddTableSizeStats<RegisterMask>("RegisterMasks", register_masks_, stats); |
| 152 | AddTableSizeStats<MaskInfo>("StackMasks", stack_masks_, stats); |
| 153 | AddTableSizeStats<InvokeInfo>("InvokeInfos", invoke_infos_, stats); |
| 154 | AddTableSizeStats<InlineInfo>("InlineInfos", inline_infos_, stats); |
| 155 | AddTableSizeStats<MaskInfo>("DexRegisterMasks", dex_register_masks_, stats); |
| 156 | AddTableSizeStats<DexRegisterMapInfo>("DexRegisterMaps", dex_register_maps_, stats); |
| 157 | AddTableSizeStats<DexRegisterInfo>("DexRegisterCatalog", dex_register_catalog_, stats); |
| 158 | } |
| 159 | |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 160 | void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const { |
| 161 | if (HasAnyLiveDexRegisters()) { |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 162 | ScopedIndentation indent1(vios); |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 163 | for (size_t i = 0; i < size(); ++i) { |
| 164 | DexRegisterLocation reg = (*this)[i]; |
| 165 | if (reg.IsLive()) { |
| 166 | vios->Stream() << "v" << i << ":" << reg << " "; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | vios->Stream() << "\n"; |
| 170 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 171 | } |
| 172 | |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 173 | template<typename Accessor> |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 174 | static void DumpTable(VariableIndentationOutputStream* vios, |
| 175 | const char* table_name, |
David Srbecky | cf7833e | 2018-06-14 16:45:22 +0100 | [diff] [blame] | 176 | const BitTable<Accessor>& table, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 177 | bool verbose, |
| 178 | bool is_mask = false) { |
| 179 | if (table.NumRows() != 0) { |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 180 | vios->Stream() << table_name << " BitSize=" << table.BitSize(); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 181 | vios->Stream() << " Rows=" << table.NumRows() << " Bits={"; |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 182 | const char* const* column_names = GetBitTableColumnNames<Accessor>(); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 183 | for (size_t c = 0; c < table.NumColumns(); c++) { |
| 184 | vios->Stream() << (c != 0 ? " " : ""); |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 185 | vios->Stream() << column_names[c] << "=" << table.NumColumnBits(c); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 186 | } |
| 187 | vios->Stream() << "}\n"; |
| 188 | if (verbose) { |
| 189 | ScopedIndentation indent1(vios); |
| 190 | for (size_t r = 0; r < table.NumRows(); r++) { |
| 191 | vios->Stream() << "[" << std::right << std::setw(3) << r << "]={"; |
| 192 | for (size_t c = 0; c < table.NumColumns(); c++) { |
| 193 | vios->Stream() << (c != 0 ? " " : ""); |
| 194 | if (is_mask) { |
| 195 | BitMemoryRegion bits = table.GetBitMemoryRegion(r, c); |
| 196 | for (size_t b = 0, e = bits.size_in_bits(); b < e; b++) { |
| 197 | vios->Stream() << bits.LoadBit(e - b - 1); |
| 198 | } |
| 199 | } else { |
| 200 | vios->Stream() << std::right << std::setw(8) << static_cast<int32_t>(table.Get(r, c)); |
| 201 | } |
| 202 | } |
| 203 | vios->Stream() << "}\n"; |
| 204 | } |
| 205 | } |
| 206 | } |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 209 | void CodeInfo::Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 210 | uint32_t code_offset, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 211 | bool verbose, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 212 | InstructionSet instruction_set, |
| 213 | const MethodInfo& method_info) const { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 214 | vios->Stream() |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 215 | << "CodeInfo" |
| 216 | << " BitSize=" << size_ * kBitsPerByte |
| 217 | << "\n"; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 218 | ScopedIndentation indent1(vios); |
David Srbecky | 86decb6 | 2018-06-05 06:41:10 +0100 | [diff] [blame] | 219 | DumpTable<StackMap>(vios, "StackMaps", stack_maps_, verbose); |
| 220 | DumpTable<RegisterMask>(vios, "RegisterMasks", register_masks_, verbose); |
| 221 | DumpTable<MaskInfo>(vios, "StackMasks", stack_masks_, verbose, true /* is_mask */); |
| 222 | DumpTable<InvokeInfo>(vios, "InvokeInfos", invoke_infos_, verbose); |
| 223 | DumpTable<InlineInfo>(vios, "InlineInfos", inline_infos_, verbose); |
| 224 | DumpTable<MaskInfo>(vios, "DexRegisterMasks", dex_register_masks_, verbose, true /* is_mask */); |
| 225 | DumpTable<DexRegisterMapInfo>(vios, "DexRegisterMaps", dex_register_maps_, verbose); |
| 226 | DumpTable<DexRegisterInfo>(vios, "DexRegisterCatalog", dex_register_catalog_, verbose); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 227 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 228 | // Display stack maps along with (live) Dex register maps. |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 229 | if (verbose) { |
| 230 | for (size_t i = 0; i < GetNumberOfStackMaps(); ++i) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 231 | StackMap stack_map = GetStackMapAt(i); |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 232 | stack_map.Dump(vios, *this, method_info, code_offset, instruction_set); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 237 | void StackMap::Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 238 | const CodeInfo& code_info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 239 | const MethodInfo& method_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 240 | uint32_t code_offset, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 241 | InstructionSet instruction_set) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 242 | const uint32_t pc_offset = GetNativePcOffset(instruction_set); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 243 | vios->Stream() |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 244 | << "StackMap[" << Row() << "]" |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 245 | << std::hex |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 246 | << " (native_pc=0x" << code_offset + pc_offset |
| 247 | << ", dex_pc=0x" << GetDexPc() |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 248 | << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this) |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 249 | << std::dec |
| 250 | << ", stack_mask=0b"; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 251 | BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this); |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 252 | for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) { |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 253 | vios->Stream() << stack_mask.LoadBit(e - i - 1); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 254 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 255 | vios->Stream() << ")\n"; |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 256 | code_info.GetDexRegisterMapOf(*this).Dump(vios); |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 257 | uint32_t depth = code_info.GetInlineDepthOf(*this); |
| 258 | for (size_t d = 0; d < depth; d++) { |
| 259 | InlineInfo inline_info = code_info.GetInlineInfoAtDepth(*this, d); |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 260 | inline_info.Dump(vios, code_info, *this, method_info); |
Nicolas Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 261 | } |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 264 | void InlineInfo::Dump(VariableIndentationOutputStream* vios, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 265 | const CodeInfo& code_info, |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 266 | const StackMap& stack_map, |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 267 | const MethodInfo& method_info) const { |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 268 | uint32_t depth = Row() - stack_map.GetInlineInfoIndex(); |
| 269 | vios->Stream() |
| 270 | << "InlineInfo[" << Row() << "]" |
| 271 | << " (depth=" << depth |
| 272 | << std::hex |
| 273 | << ", dex_pc=0x" << GetDexPc(); |
| 274 | if (EncodesArtMethod()) { |
| 275 | ScopedObjectAccess soa(Thread::Current()); |
| 276 | vios->Stream() << ", method=" << GetArtMethod()->PrettyMethod(); |
| 277 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 278 | vios->Stream() |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 279 | << std::dec |
| 280 | << ", method_index=" << GetMethodIndex(method_info); |
| 281 | } |
| 282 | vios->Stream() << ")\n"; |
David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 283 | code_info.GetDexRegisterMapAtDepth(depth, stack_map).Dump(vios); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | } // namespace art |