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" |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" |
Roland Levillain | 0396ed7 | 2015-05-27 15:12:19 +0100 | [diff] [blame] | 25 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 26 | namespace art { |
| 27 | |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame^] | 28 | // 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. |
| 31 | void 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 Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 91 | std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation& reg) { |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 92 | using Kind = DexRegisterLocation::Kind; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 93 | switch (reg.GetKind()) { |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 94 | case Kind::kNone: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 95 | return stream << "None"; |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 96 | case Kind::kInStack: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 97 | return stream << "sp+" << reg.GetValue(); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 98 | case Kind::kInRegister: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 99 | return stream << "r" << reg.GetValue(); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 100 | case Kind::kInRegisterHigh: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 101 | return stream << "r" << reg.GetValue() << "/hi"; |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 102 | case Kind::kInFpuRegister: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 103 | return stream << "f" << reg.GetValue(); |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 104 | case Kind::kInFpuRegisterHigh: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 105 | return stream << "f" << reg.GetValue() << "/hi"; |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 106 | case Kind::kConstant: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 107 | return stream << "#" << reg.GetValue(); |
David Srbecky | 6de8833 | 2018-06-03 12:00:11 +0100 | [diff] [blame^] | 108 | case Kind::kInvalid: |
| 109 | return stream << "Invalid"; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 110 | default: |
| 111 | return stream << "DexRegisterLocation(" << static_cast<uint32_t>(reg.GetKind()) |
| 112 | << "," << reg.GetValue() << ")"; |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 113 | } |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 114 | } |
| 115 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 116 | static 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 127 | } |
| 128 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 129 | template<uint32_t kNumColumns> |
| 130 | static 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 Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 164 | void CodeInfo::Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 165 | uint32_t code_offset, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 166 | uint16_t num_dex_registers, |
| 167 | bool verbose, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 168 | InstructionSet instruction_set, |
| 169 | const MethodInfo& method_info) const { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 170 | vios->Stream() |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 171 | << "CodeInfo" |
| 172 | << " BitSize=" << size_ * kBitsPerByte |
| 173 | << "\n"; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 174 | ScopedIndentation indent1(vios); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 175 | 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 184 | // Display stack maps along with (live) Dex register maps. |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 185 | if (verbose) { |
| 186 | for (size_t i = 0; i < GetNumberOfStackMaps(); ++i) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 187 | StackMap stack_map = GetStackMapAt(i); |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 188 | stack_map.Dump(vios, *this, method_info, code_offset, num_dex_registers, instruction_set); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 193 | void StackMap::Dump(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 194 | const CodeInfo& code_info, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 195 | const MethodInfo& method_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 196 | uint32_t code_offset, |
| 197 | uint16_t number_of_dex_registers, |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 198 | InstructionSet instruction_set) const { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 199 | const uint32_t pc_offset = GetNativePcOffset(instruction_set); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 200 | vios->Stream() |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 201 | << "StackMap[" << Row() << "]" |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 202 | << std::hex |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 203 | << " (native_pc=0x" << code_offset + pc_offset |
| 204 | << ", dex_pc=0x" << GetDexPc() |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 205 | << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this) |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 206 | << std::dec |
| 207 | << ", stack_mask=0b"; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 208 | BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this); |
David Srbecky | 4b59d10 | 2018-05-29 21:46:10 +0000 | [diff] [blame] | 209 | 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] | 210 | vios->Stream() << stack_mask.LoadBit(e - i - 1); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 211 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 212 | vios->Stream() << ")\n"; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 213 | DumpDexRegisterMap(vios, code_info.GetDexRegisterMapOf(*this, number_of_dex_registers)); |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 214 | 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 Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 217 | // 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 Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 220 | inline_info.Dump(vios, code_info, *this, method_info, 0); |
Nicolas Geoffray | 12bdb72 | 2015-06-17 09:44:43 +0100 | [diff] [blame] | 221 | } |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 224 | void InlineInfo::Dump(VariableIndentationOutputStream* vios, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 225 | const CodeInfo& code_info, |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 226 | const StackMap& stack_map, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 227 | const MethodInfo& method_info, |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 228 | 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 Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 239 | vios->Stream() |
David Srbecky | 6e69e52 | 2018-06-03 12:00:14 +0100 | [diff] [blame] | 240 | << 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 Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 247 | } |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | } // namespace art |