blob: 3c92b86208178faccfa53d62305fcb7df961a69a [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"
Roland Levillain0396ed72015-05-27 15:12:19 +010022#include "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 +000028constexpr uint32_t StackMap::kNoDexRegisterMap;
29constexpr uint32_t StackMap::kNoInlineInfo;
30
David Srbecky7dc11782016-02-25 13:23:56 +000031std::ostream& operator<<(std::ostream& stream, const DexRegisterLocation::Kind& kind) {
32 using Kind = DexRegisterLocation::Kind;
33 switch (kind) {
34 case Kind::kNone:
35 return stream << "none";
36 case Kind::kInStack:
37 return stream << "in stack";
38 case Kind::kInRegister:
39 return stream << "in register";
40 case Kind::kInRegisterHigh:
41 return stream << "in register high";
42 case Kind::kInFpuRegister:
43 return stream << "in fpu register";
44 case Kind::kInFpuRegisterHigh:
45 return stream << "in fpu register high";
46 case Kind::kConstant:
47 return stream << "as constant";
48 case Kind::kInStackLargeOffset:
49 return stream << "in stack (large offset)";
50 case Kind::kConstantLargeValue:
51 return stream << "as constant (large value)";
52 }
53 return stream << "Kind<" << static_cast<uint32_t>(kind) << ">";
54}
55
Roland Levillain1c1da432015-07-16 11:54:44 +010056DexRegisterLocation::Kind DexRegisterMap::GetLocationInternalKind(
57 uint16_t dex_register_number,
58 uint16_t number_of_dex_registers,
59 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +000060 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +000061 DexRegisterLocationCatalog dex_register_location_catalog =
David Brazdilf677ebf2015-05-29 16:29:43 +010062 code_info.GetDexRegisterLocationCatalog(enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +000063 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
64 dex_register_number,
65 number_of_dex_registers,
David Srbecky09ed0982016-02-12 21:58:43 +000066 code_info.GetNumberOfLocationCatalogEntries(enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +000067 return dex_register_location_catalog.GetLocationInternalKind(location_catalog_entry_index);
68}
69
70DexRegisterLocation DexRegisterMap::GetDexRegisterLocation(uint16_t dex_register_number,
71 uint16_t number_of_dex_registers,
David Brazdilf677ebf2015-05-29 16:29:43 +010072 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +000073 const CodeInfoEncoding& enc) const {
Roland Levillaina552e1c2015-03-26 15:01:03 +000074 DexRegisterLocationCatalog dex_register_location_catalog =
David Brazdilf677ebf2015-05-29 16:29:43 +010075 code_info.GetDexRegisterLocationCatalog(enc);
Roland Levillaina552e1c2015-03-26 15:01:03 +000076 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
77 dex_register_number,
78 number_of_dex_registers,
David Srbecky09ed0982016-02-12 21:58:43 +000079 code_info.GetNumberOfLocationCatalogEntries(enc));
Roland Levillaina552e1c2015-03-26 15:01:03 +000080 return dex_register_location_catalog.GetDexRegisterLocation(location_catalog_entry_index);
81}
82
Roland Levillaina552e1c2015-03-26 15:01:03 +000083static void DumpRegisterMapping(std::ostream& os,
84 size_t dex_register_num,
85 DexRegisterLocation location,
86 const std::string& prefix = "v",
87 const std::string& suffix = "") {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010088 os << prefix << dex_register_num << ": "
David Srbecky7dc11782016-02-25 13:23:56 +000089 << location.GetInternalKind()
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010090 << " (" << location.GetValue() << ")" << suffix << '\n';
Roland Levillain0396ed72015-05-27 15:12:19 +010091}
92
David Srbecky09ed0982016-02-12 21:58:43 +000093void StackMapEncoding::Dump(VariableIndentationOutputStream* vios) const {
94 vios->Stream()
95 << "StackMapEncoding"
96 << " (native_pc_bit_offset=" << static_cast<uint32_t>(kNativePcBitOffset)
97 << ", dex_pc_bit_offset=" << static_cast<uint32_t>(dex_pc_bit_offset_)
98 << ", dex_register_map_bit_offset=" << static_cast<uint32_t>(dex_register_map_bit_offset_)
99 << ", inline_info_bit_offset=" << static_cast<uint32_t>(inline_info_bit_offset_)
100 << ", register_mask_bit_offset=" << static_cast<uint32_t>(register_mask_bit_offset_)
101 << ", stack_mask_bit_offset=" << static_cast<uint32_t>(stack_mask_bit_offset_)
102 << ")\n";
103}
104
David Srbecky61b28a12016-02-25 21:55:03 +0000105void InlineInfoEncoding::Dump(VariableIndentationOutputStream* vios) const {
106 vios->Stream()
107 << "InlineInfoEncoding"
108 << " (method_index_bit_offset=" << static_cast<uint32_t>(kMethodIndexBitOffset)
109 << ", dex_pc_bit_offset=" << static_cast<uint32_t>(dex_pc_bit_offset_)
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000110 << ", extra_data_bit_offset=" << static_cast<uint32_t>(extra_data_bit_offset_)
David Srbecky61b28a12016-02-25 21:55:03 +0000111 << ", dex_register_map_bit_offset=" << static_cast<uint32_t>(dex_register_map_bit_offset_)
112 << ", total_bit_size=" << static_cast<uint32_t>(total_bit_size_)
113 << ")\n";
114}
115
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100116void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100117 uint32_t code_offset,
Roland Levillain0396ed72015-05-27 15:12:19 +0100118 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800119 bool dump_stack_maps,
120 InstructionSet instruction_set) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000121 CodeInfoEncoding encoding = ExtractEncoding();
122 size_t number_of_stack_maps = GetNumberOfStackMaps(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100123 vios->Stream()
David Srbecky09ed0982016-02-12 21:58:43 +0000124 << "Optimized CodeInfo (number_of_dex_registers=" << number_of_dex_registers
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100125 << ", number_of_stack_maps=" << number_of_stack_maps
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100126 << ")\n";
127 ScopedIndentation indent1(vios);
David Srbecky09ed0982016-02-12 21:58:43 +0000128 encoding.stack_map_encoding.Dump(vios);
David Srbecky61b28a12016-02-25 21:55:03 +0000129 if (HasInlineInfo(encoding)) {
130 encoding.inline_info_encoding.Dump(vios);
131 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000132 // Display the Dex register location catalog.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100133 GetDexRegisterLocationCatalog(encoding).Dump(vios, *this);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000134 // Display stack maps along with (live) Dex register maps.
Roland Levillain0396ed72015-05-27 15:12:19 +0100135 if (dump_stack_maps) {
136 for (size_t i = 0; i < number_of_stack_maps; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100137 StackMap stack_map = GetStackMapAt(i, encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100138 stack_map.Dump(vios,
David Brazdilf677ebf2015-05-29 16:29:43 +0100139 *this,
140 encoding,
141 code_offset,
142 number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800143 instruction_set,
David Brazdilf677ebf2015-05-29 16:29:43 +0100144 " " + std::to_string(i));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000145 }
146 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100147 // TODO: Dump the stack map's inline information? We need to know more from the caller:
148 // we need to know the number of dex registers for each inlined method.
149}
150
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100151void DexRegisterLocationCatalog::Dump(VariableIndentationOutputStream* vios,
152 const CodeInfo& code_info) {
David Srbecky09ed0982016-02-12 21:58:43 +0000153 CodeInfoEncoding encoding = code_info.ExtractEncoding();
154 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
David Brazdilf677ebf2015-05-29 16:29:43 +0100155 size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100156 vios->Stream()
Roland Levillain0396ed72015-05-27 15:12:19 +0100157 << "DexRegisterLocationCatalog (number_of_entries=" << number_of_location_catalog_entries
158 << ", size_in_bytes=" << location_catalog_size_in_bytes << ")\n";
159 for (size_t i = 0; i < number_of_location_catalog_entries; ++i) {
160 DexRegisterLocation location = GetDexRegisterLocation(i);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100161 ScopedIndentation indent1(vios);
162 DumpRegisterMapping(vios->Stream(), i, location, "entry ");
Roland Levillain0396ed72015-05-27 15:12:19 +0100163 }
164}
165
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100166void DexRegisterMap::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100167 const CodeInfo& code_info,
168 uint16_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000169 CodeInfoEncoding encoding = code_info.ExtractEncoding();
170 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100171 // TODO: Display the bit mask of live Dex registers.
172 for (size_t j = 0; j < number_of_dex_registers; ++j) {
173 if (IsDexRegisterLive(j)) {
174 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
175 j, number_of_dex_registers, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +0100176 DexRegisterLocation location = GetDexRegisterLocation(j,
177 number_of_dex_registers,
178 code_info,
179 encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100180 ScopedIndentation indent1(vios);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100181 DumpRegisterMapping(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100182 vios->Stream(), j, location, "v",
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100183 "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]");
184 }
185 }
186}
187
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100188void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100189 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000190 const CodeInfoEncoding& encoding,
Roland Levillainf2650d12015-05-28 14:53:28 +0100191 uint32_t code_offset,
192 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800193 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100194 const std::string& header_suffix) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000195 StackMapEncoding stack_map_encoding = encoding.stack_map_encoding;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800196 const uint32_t pc_offset = GetNativePcOffset(stack_map_encoding, instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100197 vios->Stream()
198 << "StackMap" << header_suffix
199 << std::hex
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800200 << " [native_pc=0x" << code_offset + pc_offset << "]"
David Srbecky09ed0982016-02-12 21:58:43 +0000201 << " (dex_pc=0x" << GetDexPc(stack_map_encoding)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800202 << ", native_pc_offset=0x" << pc_offset
David Srbecky09ed0982016-02-12 21:58:43 +0000203 << ", dex_register_map_offset=0x" << GetDexRegisterMapOffset(stack_map_encoding)
204 << ", inline_info_offset=0x" << GetInlineDescriptorOffset(stack_map_encoding)
205 << ", register_mask=0x" << GetRegisterMask(stack_map_encoding)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100206 << std::dec
207 << ", stack_mask=0b";
David Srbecky09ed0982016-02-12 21:58:43 +0000208 for (size_t i = 0, e = GetNumberOfStackMaskBits(stack_map_encoding); i < e; ++i) {
209 vios->Stream() << GetStackMaskBit(stack_map_encoding, e - i - 1);
Roland Levillainf2650d12015-05-28 14:53:28 +0100210 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100211 vios->Stream() << ")\n";
David Srbecky09ed0982016-02-12 21:58:43 +0000212 if (HasDexRegisterMap(stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100213 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
214 *this, encoding, number_of_dex_registers);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100215 dex_register_map.Dump(vios, code_info, number_of_dex_registers);
Roland Levillainf2650d12015-05-28 14:53:28 +0100216 }
David Srbecky09ed0982016-02-12 21:58:43 +0000217 if (HasInlineInfo(stack_map_encoding)) {
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100218 InlineInfo inline_info = code_info.GetInlineInfoOf(*this, encoding);
219 // We do not know the length of the dex register maps of inlined frames
220 // at this level, so we just pass null to `InlineInfo::Dump` to tell
221 // it not to look at these maps.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100222 inline_info.Dump(vios, code_info, nullptr);
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,
228 uint16_t number_of_dex_registers[]) const {
David Srbecky61b28a12016-02-25 21:55:03 +0000229 InlineInfoEncoding inline_info_encoding = code_info.ExtractEncoding().inline_info_encoding;
230 vios->Stream() << "InlineInfo with depth "
231 << static_cast<uint32_t>(GetDepth(inline_info_encoding))
232 << "\n";
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100233
David Srbecky61b28a12016-02-25 21:55:03 +0000234 for (size_t i = 0; i < GetDepth(inline_info_encoding); ++i) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100235 vios->Stream()
236 << " At depth " << i
237 << std::hex
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000238 << " (dex_pc=0x" << GetDexPcAtDepth(inline_info_encoding, i);
239 if (EncodesArtMethodAtDepth(inline_info_encoding, i)) {
240 ScopedObjectAccess soa(Thread::Current());
241 vios->Stream() << ", method=" << GetArtMethodAtDepth(inline_info_encoding, i)->PrettyMethod();
242 } else {
243 vios->Stream()
244 << std::dec
245 << ", method_index=" << GetMethodIndexAtDepth(inline_info_encoding, i);
246 }
247 vios->Stream() << ")\n";
David Srbecky61b28a12016-02-25 21:55:03 +0000248 if (HasDexRegisterMapAtDepth(inline_info_encoding, i) && (number_of_dex_registers != nullptr)) {
David Srbecky09ed0982016-02-12 21:58:43 +0000249 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100250 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100251 code_info.GetDexRegisterMapAtDepth(i, *this, encoding, number_of_dex_registers[i]);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100252 ScopedIndentation indent1(vios);
253 dex_register_map.Dump(vios, code_info, number_of_dex_registers[i]);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100254 }
255 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000256}
257
258} // namespace art