blob: 61fe2e79656397d5e5aa9d4d0c744ffaedd8b23a [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
Nicolas Geoffray896f8f72015-03-30 15:44:25 +010019#include <stdint.h>
20
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000021#include "art_method.h"
David Sehr9c4a0152018-04-05 12:23:54 -070022#include "base/indenter.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000023#include "scoped_thread_state_change-inl.h"
Roland Levillain0396ed72015-05-27 15:12:19 +010024
Nicolas Geoffray004c2302015-03-20 10:06:38 +000025namespace art {
26
Roland Levillaina552e1c2015-03-26 15:01:03 +000027constexpr size_t DexRegisterLocationCatalog::kNoLocationEntryIndex;
Nicolas Geoffray004c2302015-03-20 10:06:38 +000028
David Srbecky7dc11782016-02-25 13:23:56 +000029std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind) {
30 using Kind = DexRegisterLocation::Kind;
31 switch (kind) {
32 case Kind::kNone:
33 return stream << "none";
34 case Kind::kInStack:
35 return stream << "in stack";
36 case Kind::kInRegister:
37 return stream << "in register";
38 case Kind::kInRegisterHigh:
39 return stream << "in register high";
40 case Kind::kInFpuRegister:
41 return stream << "in fpu register";
42 case Kind::kInFpuRegisterHigh:
43 return stream << "in fpu register high";
44 case Kind::kConstant:
45 return stream << "as constant";
46 case Kind::kInStackLargeOffset:
47 return stream << "in stack (large offset)";
48 case Kind::kConstantLargeValue:
49 return stream << "as constant (large value)";
50 }
51 return stream << "Kind<" << static_cast<uint32_t>(kind) << ">";
52}
53
Roland Levillain1c1da432015-07-16 11:54:44 +010054DexRegisterLocation::Kind DexRegisterMap::GetLocationInternalKind(
David Srbecky21d45b42018-05-30 06:35:05 +010055 uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +000056 DexRegisterLocationCatalog dex_register_location_catalog =
David Srbecky21d45b42018-05-30 06:35:05 +010057 code_info_.GetDexRegisterLocationCatalog();
Roland Levillaina552e1c2015-03-26 15:01:03 +000058 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
59 dex_register_number,
David Srbecky21d45b42018-05-30 06:35:05 +010060 code_info_.GetNumberOfLocationCatalogEntries());
Roland Levillaina552e1c2015-03-26 15:01:03 +000061 return dex_register_location_catalog.GetLocationInternalKind(location_catalog_entry_index);
62}
63
David Srbecky21d45b42018-05-30 06:35:05 +010064DexRegisterLocation DexRegisterMap::GetDexRegisterLocation(uint16_t dex_register_number) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +000065 DexRegisterLocationCatalog dex_register_location_catalog =
David Srbecky21d45b42018-05-30 06:35:05 +010066 code_info_.GetDexRegisterLocationCatalog();
Roland Levillaina552e1c2015-03-26 15:01:03 +000067 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
68 dex_register_number,
David Srbecky21d45b42018-05-30 06:35:05 +010069 code_info_.GetNumberOfLocationCatalogEntries());
Roland Levillaina552e1c2015-03-26 15:01:03 +000070 return dex_register_location_catalog.GetDexRegisterLocation(location_catalog_entry_index);
71}
72
Roland Levillaina552e1c2015-03-26 15:01:03 +000073static void DumpRegisterMapping(std::ostream& os,
74 size_t dex_register_num,
75 DexRegisterLocation location,
76 const std::string& prefix = "v",
77 const std::string& suffix = "") {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010078 os << prefix << dex_register_num << ": "
David Srbecky7dc11782016-02-25 13:23:56 +000079 << location.GetInternalKind()
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010080 << " (" << location.GetValue() << ")" << suffix << '\n';
Roland Levillain0396ed72015-05-27 15:12:19 +010081}
82
David Srbecky052f8ca2018-04-26 15:42:54 +010083void StackMap::DumpEncoding(const BitTable<6>& table,
84 VariableIndentationOutputStream* vios) {
David Srbecky09ed0982016-02-12 21:58:43 +000085 vios->Stream()
86 << "StackMapEncoding"
David Srbeckyd02b23f2018-05-29 23:27:22 +010087 << " (PackedNativePcBits=" << table.NumColumnBits(kPackedNativePc)
David Srbecky052f8ca2018-04-26 15:42:54 +010088 << ", DexPcBits=" << table.NumColumnBits(kDexPc)
89 << ", DexRegisterMapOffsetBits=" << table.NumColumnBits(kDexRegisterMapOffset)
90 << ", InlineInfoIndexBits=" << table.NumColumnBits(kInlineInfoIndex)
91 << ", RegisterMaskIndexBits=" << table.NumColumnBits(kRegisterMaskIndex)
92 << ", StackMaskIndexBits=" << table.NumColumnBits(kStackMaskIndex)
David Srbecky09ed0982016-02-12 21:58:43 +000093 << ")\n";
94}
95
David Srbecky052f8ca2018-04-26 15:42:54 +010096void InlineInfo::DumpEncoding(const BitTable<5>& table,
97 VariableIndentationOutputStream* vios) {
David Srbecky61b28a12016-02-25 21:55:03 +000098 vios->Stream()
99 << "InlineInfoEncoding"
David Srbecky052f8ca2018-04-26 15:42:54 +0100100 << " (IsLastBits=" << table.NumColumnBits(kIsLast)
101 << ", MethodIndexIdxBits=" << table.NumColumnBits(kMethodIndexIdx)
102 << ", DexPcBits=" << table.NumColumnBits(kDexPc)
103 << ", ExtraDataBits=" << table.NumColumnBits(kExtraData)
104 << ", DexRegisterMapOffsetBits=" << table.NumColumnBits(kDexRegisterMapOffset)
David Srbecky61b28a12016-02-25 21:55:03 +0000105 << ")\n";
106}
107
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100108void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100109 uint32_t code_offset,
Roland Levillain0396ed72015-05-27 15:12:19 +0100110 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800111 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700112 InstructionSet instruction_set,
113 const MethodInfo& method_info) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100114 size_t number_of_stack_maps = GetNumberOfStackMaps();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100115 vios->Stream()
David Srbecky09ed0982016-02-12 21:58:43 +0000116 << "Optimized CodeInfo (number_of_dex_registers=" << number_of_dex_registers
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100117 << ", number_of_stack_maps=" << number_of_stack_maps
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100118 << ")\n";
119 ScopedIndentation indent1(vios);
David Srbecky052f8ca2018-04-26 15:42:54 +0100120 StackMap::DumpEncoding(stack_maps_, vios);
121 if (HasInlineInfo()) {
122 InlineInfo::DumpEncoding(inline_infos_, vios);
David Srbecky61b28a12016-02-25 21:55:03 +0000123 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000124 // Display the Dex register location catalog.
David Srbecky052f8ca2018-04-26 15:42:54 +0100125 GetDexRegisterLocationCatalog().Dump(vios, *this);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000126 // Display stack maps along with (live) Dex register maps.
Roland Levillain0396ed72015-05-27 15:12:19 +0100127 if (dump_stack_maps) {
128 for (size_t i = 0; i < number_of_stack_maps; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100129 StackMap stack_map = GetStackMapAt(i);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100130 stack_map.Dump(vios,
David Brazdilf677ebf2015-05-29 16:29:43 +0100131 *this,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700132 method_info,
David Brazdilf677ebf2015-05-29 16:29:43 +0100133 code_offset,
134 number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800135 instruction_set,
David Brazdilf677ebf2015-05-29 16:29:43 +0100136 " " + std::to_string(i));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000137 }
138 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100139 // TODO: Dump the stack map's inline information? We need to know more from the caller:
140 // we need to know the number of dex registers for each inlined method.
141}
142
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100143void DexRegisterLocationCatalog::Dump(VariableIndentationOutputStream* vios,
144 const CodeInfo& code_info) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100145 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
146 size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100147 vios->Stream()
Roland Levillain0396ed72015-05-27 15:12:19 +0100148 << "DexRegisterLocationCatalog (number_of_entries=" << number_of_location_catalog_entries
149 << ", size_in_bytes=" << location_catalog_size_in_bytes << ")\n";
150 for (size_t i = 0; i < number_of_location_catalog_entries; ++i) {
151 DexRegisterLocation location = GetDexRegisterLocation(i);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100152 ScopedIndentation indent1(vios);
153 DumpRegisterMapping(vios->Stream(), i, location, "entry ");
Roland Levillain0396ed72015-05-27 15:12:19 +0100154 }
155}
156
David Srbecky21d45b42018-05-30 06:35:05 +0100157void DexRegisterMap::Dump(VariableIndentationOutputStream* vios) const {
158 size_t number_of_location_catalog_entries = code_info_.GetNumberOfLocationCatalogEntries();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100159 // TODO: Display the bit mask of live Dex registers.
David Srbecky21d45b42018-05-30 06:35:05 +0100160 for (size_t j = 0; j < number_of_dex_registers_; ++j) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100161 if (IsDexRegisterLive(j)) {
162 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
David Srbecky21d45b42018-05-30 06:35:05 +0100163 j,
164 number_of_location_catalog_entries);
165 DexRegisterLocation location = GetDexRegisterLocation(j);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100166 ScopedIndentation indent1(vios);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100167 DumpRegisterMapping(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100168 vios->Stream(), j, location, "v",
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100169 "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]");
170 }
171 }
172}
173
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100174void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100175 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700176 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100177 uint32_t code_offset,
178 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800179 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100180 const std::string& header_suffix) const {
David Srbecky052f8ca2018-04-26 15:42:54 +0100181 const uint32_t pc_offset = GetNativePcOffset(instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100182 vios->Stream()
183 << "StackMap" << header_suffix
184 << std::hex
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800185 << " [native_pc=0x" << code_offset + pc_offset << "]"
David Srbecky052f8ca2018-04-26 15:42:54 +0100186 << " (dex_pc=0x" << GetDexPc()
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800187 << ", native_pc_offset=0x" << pc_offset
David Srbecky052f8ca2018-04-26 15:42:54 +0100188 << ", dex_register_map_offset=0x" << GetDexRegisterMapOffset()
189 << ", inline_info_offset=0x" << GetInlineInfoIndex()
190 << ", register_mask=0x" << code_info.GetRegisterMaskOf(*this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100191 << std::dec
192 << ", stack_mask=0b";
David Srbecky052f8ca2018-04-26 15:42:54 +0100193 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(*this);
David Srbecky4b59d102018-05-29 21:46:10 +0000194 for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +0000195 vios->Stream() << stack_mask.LoadBit(e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100196 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100197 vios->Stream() << ")\n";
David Srbecky052f8ca2018-04-26 15:42:54 +0100198 if (HasDexRegisterMap()) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100199 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
David Srbecky052f8ca2018-04-26 15:42:54 +0100200 *this, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100201 dex_register_map.Dump(vios);
Roland Levillainf2650d12015-05-28 14:53:28 +0100202 }
David Srbecky052f8ca2018-04-26 15:42:54 +0100203 if (HasInlineInfo()) {
204 InlineInfo inline_info = code_info.GetInlineInfoOf(*this);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100205 // We do not know the length of the dex register maps of inlined frames
206 // at this level, so we just pass null to `InlineInfo::Dump` to tell
207 // it not to look at these maps.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700208 inline_info.Dump(vios, code_info, method_info, nullptr);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100209 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100210}
211
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100212void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100213 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700214 const MethodInfo& method_info,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100215 uint16_t number_of_dex_registers[]) const {
David Srbecky61b28a12016-02-25 21:55:03 +0000216 vios->Stream() << "InlineInfo with depth "
David Srbecky052f8ca2018-04-26 15:42:54 +0100217 << static_cast<uint32_t>(GetDepth())
David Srbecky61b28a12016-02-25 21:55:03 +0000218 << "\n";
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100219
David Srbecky052f8ca2018-04-26 15:42:54 +0100220 for (size_t i = 0; i < GetDepth(); ++i) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100221 vios->Stream()
222 << " At depth " << i
223 << std::hex
David Srbecky052f8ca2018-04-26 15:42:54 +0100224 << " (dex_pc=0x" << GetDexPcAtDepth(i);
225 if (EncodesArtMethodAtDepth(i)) {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000226 ScopedObjectAccess soa(Thread::Current());
David Srbecky052f8ca2018-04-26 15:42:54 +0100227 vios->Stream() << ", method=" << GetArtMethodAtDepth(i)->PrettyMethod();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000228 } else {
229 vios->Stream()
230 << std::dec
David Srbecky052f8ca2018-04-26 15:42:54 +0100231 << ", method_index=" << GetMethodIndexAtDepth(method_info, i);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000232 }
233 vios->Stream() << ")\n";
David Srbecky052f8ca2018-04-26 15:42:54 +0100234 if (HasDexRegisterMapAtDepth(i) && (number_of_dex_registers != nullptr)) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100235 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100236 code_info.GetDexRegisterMapAtDepth(i, *this, number_of_dex_registers[i]);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100237 ScopedIndentation indent1(vios);
David Srbecky21d45b42018-05-30 06:35:05 +0100238 dex_register_map.Dump(vios);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100239 }
240 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000241}
242
243} // namespace art