blob: 250ff2af1a13acd2ebfb0bf877bf5607a9916306 [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_)
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800100 << ", register_mask_bit_offset=" << static_cast<uint32_t>(register_mask_index_bit_offset_)
David Srbecky45aa5982016-03-18 02:15:09 +0000101 << ", stack_mask_index_bit_offset=" << static_cast<uint32_t>(stack_mask_index_bit_offset_)
102 << ", total_bit_size=" << static_cast<uint32_t>(total_bit_size_)
David Srbecky09ed0982016-02-12 21:58:43 +0000103 << ")\n";
104}
105
David Srbecky61b28a12016-02-25 21:55:03 +0000106void InlineInfoEncoding::Dump(VariableIndentationOutputStream* vios) const {
107 vios->Stream()
108 << "InlineInfoEncoding"
109 << " (method_index_bit_offset=" << static_cast<uint32_t>(kMethodIndexBitOffset)
110 << ", dex_pc_bit_offset=" << static_cast<uint32_t>(dex_pc_bit_offset_)
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000111 << ", extra_data_bit_offset=" << static_cast<uint32_t>(extra_data_bit_offset_)
David Srbecky61b28a12016-02-25 21:55:03 +0000112 << ", dex_register_map_bit_offset=" << static_cast<uint32_t>(dex_register_map_bit_offset_)
113 << ", total_bit_size=" << static_cast<uint32_t>(total_bit_size_)
114 << ")\n";
115}
116
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100117void CodeInfo::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100118 uint32_t code_offset,
Roland Levillain0396ed72015-05-27 15:12:19 +0100119 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800120 bool dump_stack_maps,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700121 InstructionSet instruction_set,
122 const MethodInfo& method_info) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000123 CodeInfoEncoding encoding = ExtractEncoding();
124 size_t number_of_stack_maps = GetNumberOfStackMaps(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100125 vios->Stream()
David Srbecky09ed0982016-02-12 21:58:43 +0000126 << "Optimized CodeInfo (number_of_dex_registers=" << number_of_dex_registers
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100127 << ", number_of_stack_maps=" << number_of_stack_maps
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100128 << ")\n";
129 ScopedIndentation indent1(vios);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800130 encoding.stack_map.encoding.Dump(vios);
David Srbecky61b28a12016-02-25 21:55:03 +0000131 if (HasInlineInfo(encoding)) {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800132 encoding.inline_info.encoding.Dump(vios);
David Srbecky61b28a12016-02-25 21:55:03 +0000133 }
Roland Levillaina552e1c2015-03-26 15:01:03 +0000134 // Display the Dex register location catalog.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100135 GetDexRegisterLocationCatalog(encoding).Dump(vios, *this);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000136 // Display stack maps along with (live) Dex register maps.
Roland Levillain0396ed72015-05-27 15:12:19 +0100137 if (dump_stack_maps) {
138 for (size_t i = 0; i < number_of_stack_maps; ++i) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100139 StackMap stack_map = GetStackMapAt(i, encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100140 stack_map.Dump(vios,
David Brazdilf677ebf2015-05-29 16:29:43 +0100141 *this,
142 encoding,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700143 method_info,
David Brazdilf677ebf2015-05-29 16:29:43 +0100144 code_offset,
145 number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800146 instruction_set,
David Brazdilf677ebf2015-05-29 16:29:43 +0100147 " " + std::to_string(i));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000148 }
149 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100150 // TODO: Dump the stack map's inline information? We need to know more from the caller:
151 // we need to know the number of dex registers for each inlined method.
152}
153
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100154void DexRegisterLocationCatalog::Dump(VariableIndentationOutputStream* vios,
155 const CodeInfo& code_info) {
David Srbecky09ed0982016-02-12 21:58:43 +0000156 CodeInfoEncoding encoding = code_info.ExtractEncoding();
157 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
David Brazdilf677ebf2015-05-29 16:29:43 +0100158 size_t location_catalog_size_in_bytes = code_info.GetDexRegisterLocationCatalogSize(encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100159 vios->Stream()
Roland Levillain0396ed72015-05-27 15:12:19 +0100160 << "DexRegisterLocationCatalog (number_of_entries=" << number_of_location_catalog_entries
161 << ", size_in_bytes=" << location_catalog_size_in_bytes << ")\n";
162 for (size_t i = 0; i < number_of_location_catalog_entries; ++i) {
163 DexRegisterLocation location = GetDexRegisterLocation(i);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100164 ScopedIndentation indent1(vios);
165 DumpRegisterMapping(vios->Stream(), i, location, "entry ");
Roland Levillain0396ed72015-05-27 15:12:19 +0100166 }
167}
168
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100169void DexRegisterMap::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100170 const CodeInfo& code_info,
171 uint16_t number_of_dex_registers) const {
David Srbecky09ed0982016-02-12 21:58:43 +0000172 CodeInfoEncoding encoding = code_info.ExtractEncoding();
173 size_t number_of_location_catalog_entries = code_info.GetNumberOfLocationCatalogEntries(encoding);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100174 // TODO: Display the bit mask of live Dex registers.
175 for (size_t j = 0; j < number_of_dex_registers; ++j) {
176 if (IsDexRegisterLive(j)) {
177 size_t location_catalog_entry_index = GetLocationCatalogEntryIndex(
178 j, number_of_dex_registers, number_of_location_catalog_entries);
David Brazdilf677ebf2015-05-29 16:29:43 +0100179 DexRegisterLocation location = GetDexRegisterLocation(j,
180 number_of_dex_registers,
181 code_info,
182 encoding);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100183 ScopedIndentation indent1(vios);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100184 DumpRegisterMapping(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100185 vios->Stream(), j, location, "v",
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100186 "\t[entry " + std::to_string(static_cast<int>(location_catalog_entry_index)) + "]");
187 }
188 }
189}
190
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100191void StackMap::Dump(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +0100192 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +0000193 const CodeInfoEncoding& encoding,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700194 const MethodInfo& method_info,
Roland Levillainf2650d12015-05-28 14:53:28 +0100195 uint32_t code_offset,
196 uint16_t number_of_dex_registers,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800197 InstructionSet instruction_set,
Roland Levillainf2650d12015-05-28 14:53:28 +0100198 const std::string& header_suffix) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800199 StackMapEncoding stack_map_encoding = encoding.stack_map.encoding;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800200 const uint32_t pc_offset = GetNativePcOffset(stack_map_encoding, instruction_set);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100201 vios->Stream()
202 << "StackMap" << header_suffix
203 << std::hex
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800204 << " [native_pc=0x" << code_offset + pc_offset << "]"
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800205 << " [entry_size=0x" << encoding.stack_map.encoding.BitSize() << " bits]"
David Srbecky09ed0982016-02-12 21:58:43 +0000206 << " (dex_pc=0x" << GetDexPc(stack_map_encoding)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800207 << ", native_pc_offset=0x" << pc_offset
David Srbecky09ed0982016-02-12 21:58:43 +0000208 << ", dex_register_map_offset=0x" << GetDexRegisterMapOffset(stack_map_encoding)
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800209 << ", inline_info_offset=0x" << GetInlineInfoIndex(stack_map_encoding)
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800210 << ", register_mask=0x" << code_info.GetRegisterMaskOf(encoding, *this)
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100211 << std::dec
212 << ", stack_mask=0b";
David Srbecky45aa5982016-03-18 02:15:09 +0000213 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, *this);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800214 for (size_t i = 0, e = encoding.stack_mask.encoding.BitSize(); 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 Srbecky09ed0982016-02-12 21:58:43 +0000218 if (HasDexRegisterMap(stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100219 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
220 *this, encoding, number_of_dex_registers);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100221 dex_register_map.Dump(vios, code_info, number_of_dex_registers);
Roland Levillainf2650d12015-05-28 14:53:28 +0100222 }
David Srbecky09ed0982016-02-12 21:58:43 +0000223 if (HasInlineInfo(stack_map_encoding)) {
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100224 InlineInfo inline_info = code_info.GetInlineInfoOf(*this, encoding);
225 // We do not know the length of the dex register maps of inlined frames
226 // at this level, so we just pass null to `InlineInfo::Dump` to tell
227 // it not to look at these maps.
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700228 inline_info.Dump(vios, code_info, method_info, nullptr);
Nicolas Geoffray12bdb722015-06-17 09:44:43 +0100229 }
Roland Levillainf2650d12015-05-28 14:53:28 +0100230}
231
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100232void InlineInfo::Dump(VariableIndentationOutputStream* vios,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100233 const CodeInfo& code_info,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700234 const MethodInfo& method_info,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100235 uint16_t number_of_dex_registers[]) const {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800236 InlineInfoEncoding inline_info_encoding = code_info.ExtractEncoding().inline_info.encoding;
David Srbecky61b28a12016-02-25 21:55:03 +0000237 vios->Stream() << "InlineInfo with depth "
238 << static_cast<uint32_t>(GetDepth(inline_info_encoding))
239 << "\n";
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100240
David Srbecky61b28a12016-02-25 21:55:03 +0000241 for (size_t i = 0; i < GetDepth(inline_info_encoding); ++i) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100242 vios->Stream()
243 << " At depth " << i
244 << std::hex
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000245 << " (dex_pc=0x" << GetDexPcAtDepth(inline_info_encoding, i);
246 if (EncodesArtMethodAtDepth(inline_info_encoding, i)) {
247 ScopedObjectAccess soa(Thread::Current());
248 vios->Stream() << ", method=" << GetArtMethodAtDepth(inline_info_encoding, i)->PrettyMethod();
249 } else {
250 vios->Stream()
251 << std::dec
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700252 << ", method_index=" << GetMethodIndexAtDepth(inline_info_encoding, method_info, i);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000253 }
254 vios->Stream() << ")\n";
David Srbecky61b28a12016-02-25 21:55:03 +0000255 if (HasDexRegisterMapAtDepth(inline_info_encoding, i) && (number_of_dex_registers != nullptr)) {
David Srbecky09ed0982016-02-12 21:58:43 +0000256 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100257 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +0100258 code_info.GetDexRegisterMapAtDepth(i, *this, encoding, number_of_dex_registers[i]);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100259 ScopedIndentation indent1(vios);
260 dex_register_map.Dump(vios, code_info, number_of_dex_registers[i]);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100261 }
262 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000263}
264
265} // namespace art