blob: 3acea4216d207cf8e84e0dc247637d8835d59877 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
David Srbecky86decb62018-06-05 06:41:10 +010021#include <iomanip>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070022#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080023#include <map>
24#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070025#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070026#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070027#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070028#include <vector>
29
Andreas Gampe221d9812018-01-22 17:48:56 -080030#include "android-base/logging.h"
Andreas Gampef9411702018-09-06 17:16:57 -070031#include "android-base/parseint.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080032#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080033#include "android-base/strings.h"
34
Ian Rogersd582fa42014-11-05 23:46:43 -080035#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070036#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "art_method-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000038#include "base/bit_utils_iterator.h"
David Sehr9c4a0152018-04-05 12:23:54 -070039#include "base/indenter.h"
David Sehrc431b9d2018-03-02 12:01:51 -080040#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080041#include "base/safe_map.h"
David Srbecky86decb62018-06-05 06:41:10 +010042#include "base/stats.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000043#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080044#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070046#include "class_linker.h"
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010047#include "class_root.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010048#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000049#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000050#include "debug/elf_debug_writer.h"
51#include "debug/method_debug_info.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030052#include "dex/art_dex_file_loader.h"
Mathieu Chartier3d092992018-05-24 13:36:23 -070053#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080054#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080055#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080056#include "dex/dex_file-inl.h"
57#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070058#include "dex/string_reference.h"
David Sehr9c4a0152018-04-05 12:23:54 -070059#include "dex/type_lookup_table.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030060#include "dexlayout.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080061#include "disassembler.h"
David Srbecky2faab002019-02-12 16:35:48 +000062#include "elf/elf_builder.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070063#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070064#include "gc/space/image_space.h"
65#include "gc/space/large_object_space.h"
66#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080067#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070068#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000069#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070070#include "interpreter/unstarted_runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071#include "mirror/array-inl.h"
72#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010073#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074#include "mirror/object-inl.h"
75#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010077#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070079#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070080#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070081#include "stack_map.h"
David Srbecky2faab002019-02-12 16:35:48 +000082#include "stream/buffered_output_stream.h"
83#include "stream/file_output_stream.h"
84#include "subtype_check.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080085#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010086#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000088#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070089#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070090
Igor Murashkin37743352014-11-13 14:38:00 -080091#include <sys/stat.h>
92#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070093
Igor Murashkin37743352014-11-13 14:38:00 -080094namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070095
Andreas Gampe46ee31b2016-12-14 10:11:49 -080096using android::base::StringPrintf;
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080099 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700100 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700101 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100102 "kSaveAllCalleeSavesMethod",
103 "kSaveRefsOnlyMethod",
104 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +0100105 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -0700106 "kSaveEverythingMethodForClinit",
107 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108};
109
110const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700111 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700112 "kClassRoots",
Alex Light885f0c12019-01-09 13:48:55 -0800113 "kSpecialRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700114};
115
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700116// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700117static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700118
Andreas Gampeb40d3612018-06-26 15:49:42 -0700119const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120 DCHECK(oat_dex_file != nullptr);
121 auto it = opened_dex_files.find(oat_dex_file);
122 if (it != opened_dex_files.end()) {
123 return it->second.get();
124 }
125 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
126 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
127 return ret;
128}
129
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800130template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000133 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
134 oat_file_(oat_file),
135 builder_(nullptr),
136 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
137 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700138 }
139
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700140 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800142 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000143 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
Andreas Gampe08c277c2017-04-26 22:22:15 -0700145 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
146 if (elf_file == nullptr) {
147 return false;
148 }
David Srbecky2faab002019-02-12 16:35:48 +0000149 std::unique_ptr<BufferedOutputStream> output_stream =
150 std::make_unique<BufferedOutputStream>(
151 std::make_unique<FileOutputStream>(elf_file.get()));
152 builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153
154 builder_->Start();
155
156 auto* rodata = builder_->GetRoData();
157 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 const uint8_t* rodata_begin = oat_file_->Begin();
160 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000161 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000162 rodata->Start();
163 rodata->WriteFully(rodata_begin, rodata_size);
164 rodata->End();
165 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
168 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000169 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000170 text->Start();
171 text->WriteFully(text_begin, text_size);
172 text->End();
173 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 builder_->PrepareDynamicSection(elf_file->GetPath(),
176 rodata_size,
177 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000178 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100180 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000181 oat_file_->BssRootsOffset(),
182 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000183 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184
Vladimir Marko1b404a82017-09-01 13:35:26 +0100185 const OatHeader& oat_header = oat_file_->GetOatHeader();
186 #define DO_TRAMPOLINE(fn_name) \
187 if (oat_header.Get ## fn_name ## Offset() != 0) { \
188 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000189 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100190 info.isa = oat_header.GetInstructionSet(); \
191 info.is_code_address_text_relative = true; \
192 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
193 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
194 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
195 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
196 method_debug_infos_.push_back(std::move(info)); \
197 }
Vladimir Marko7dac8642019-11-06 17:09:30 +0000198 DO_TRAMPOLINE(JniDlsymLookupTrampoline);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000199 DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);
Vladimir Marko1b404a82017-09-01 13:35:26 +0100200 DO_TRAMPOLINE(QuickGenericJniTrampoline);
201 DO_TRAMPOLINE(QuickImtConflictTrampoline);
202 DO_TRAMPOLINE(QuickResolutionTrampoline);
203 DO_TRAMPOLINE(QuickToInterpreterBridge);
204 #undef DO_TRAMPOLINE
205
David Srbecky5d950762016-03-07 20:47:29 +0000206 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100207
208 // TODO: Try to symbolize link-time thunks?
209 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700210
David Srbecky32210b92017-12-04 14:39:21 +0000211 // TODO: Add symbols for dex bytecode in the .dex section.
212
213 debug::DebugInfo debug_info{};
214 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
215
David Srbecky7370d922019-02-12 14:00:30 +0000216 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217
David Srbecky6d8c8f02015-10-26 10:57:09 +0000218 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700219
Andreas Gampe08c277c2017-04-26 22:22:15 -0700220 bool ret_value = builder_->Good();
221
222 builder_.reset();
223 output_stream.reset();
224
225 if (elf_file->FlushCloseOrErase() != 0) {
226 return false;
227 }
228 elf_file.reset();
229
230 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 }
232
David Srbecky5d950762016-03-07 20:47:29 +0000233 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700234 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700235 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700236 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000238 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 }
240 }
241
Andreas Gampeb40d3612018-06-26 15:49:42 -0700242 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700244 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
245 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 return;
247 }
248 for (size_t class_def_index = 0;
249 class_def_index < dex_file->NumClassDefs();
250 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
252 OatClassType type = oat_class.GetType();
253 switch (type) {
254 case kOatClassAllCompiled:
255 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000256 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 break;
258
259 case kOatClassNoneCompiled:
260 case kOatClassMax:
261 // Ignore.
262 break;
263 }
264 }
265 }
266
David Srbecky5d950762016-03-07 20:47:29 +0000267 void WalkOatClass(const OatFile::OatClass& oat_class,
268 const DexFile& dex_file,
269 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700270 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 // Note: even if this is an interface or a native class, we still have to walk it, as there
272 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700274 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000275 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
276 dex_file,
277 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700278 method.GetIndex(),
279 method.GetCodeItem(),
280 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 }
283
David Srbecky5d950762016-03-07 20:47:29 +0000284 void WalkOatMethod(const OatFile::OatMethod& oat_method,
285 const DexFile& dex_file,
286 uint32_t class_def_index,
287 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800288 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000289 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700290 if ((method_access_flags & kAccAbstract) != 0) {
291 // Abstract method, no code.
292 return;
293 }
David Srbecky5d950762016-03-07 20:47:29 +0000294 const OatHeader& oat_header = oat_file_->GetOatHeader();
295 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
296 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700297 // No code.
298 return;
299 }
300
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100301 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
302 // Clear Thumb2 bit.
303 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
304
Vladimir Marko1b404a82017-09-01 13:35:26 +0100305 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000306 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000307 info.dex_file = &dex_file;
308 info.class_def_index = class_def_index;
309 info.dex_method_index = dex_method_index;
310 info.access_flags = method_access_flags;
311 info.code_item = code_item;
312 info.isa = oat_header.GetInstructionSet();
313 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
314 info.is_native_debuggable = oat_header.IsNativeDebuggable();
315 info.is_optimized = method_header->IsOptimized();
316 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100317 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000318 info.code_size = method_header->GetCodeSize();
319 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
320 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
321 info.cfi = ArrayRef<uint8_t>();
322 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700323 }
324
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700325 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 const OatFile* oat_file_;
David Srbecky2faab002019-02-12 16:35:48 +0000327 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000328 std::vector<debug::MethodDebugInfo> method_debug_infos_;
329 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700330 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000331 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700332};
333
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334class OatDumperOptions {
335 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100336 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100337 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700338 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700339 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 const char* class_filter,
341 const char* method_filter,
342 bool list_classes,
343 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000344 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800346 const char* app_image,
347 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100349 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100350 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700351 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700352 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000354 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 list_classes_(list_classes),
356 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000357 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800359 app_image_(app_image),
360 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800361 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800362 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700364 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100365 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 const bool disassemble_code_;
367 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800368 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000369 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 const bool list_classes_;
371 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000372 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800374 const char* const app_image_;
375 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700377 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700378};
379
Elliott Hughese3c845c2012-02-28 17:23:01 -0800380class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700381 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100382 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000383 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800384 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700385 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800386 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800387 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
388 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700389 new DisassemblerOptions(
390 options_.absolute_addresses_,
391 oat_file.Begin(),
392 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700393 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700394 Is64BitInstructionSet(instruction_set_)
395 ? &Thread::DumpThreadOffset<PointerSize::k64>
396 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800397 CHECK(options_.class_loader_ != nullptr);
398 CHECK(options_.class_filter_ != nullptr);
399 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800400 AddAllOffsets();
401 }
402
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700403 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 delete disassembler_;
405 }
406
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800407 InstructionSet GetInstructionSet() {
408 return instruction_set_;
409 }
410
Andreas Gampec55bb392018-09-21 00:02:02 +0000411 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300412
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700413 bool Dump(std::ostream& os) {
414 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
417 os << "MAGIC:\n";
418 os << oat_header.GetMagic() << "\n\n";
419
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800420 os << "LOCATION:\n";
421 os << oat_file_.GetLocation() << "\n\n";
422
Brian Carlstromaded5f72011-10-07 17:15:04 -0700423 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800424 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425
Elliott Hughesa72ec822012-03-05 17:12:22 -0800426 os << "INSTRUCTION SET:\n";
427 os << oat_header.GetInstructionSet() << "\n\n";
428
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700429 {
430 std::unique_ptr<const InstructionSetFeatures> features(
431 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
432 oat_header.GetInstructionSetFeaturesBitmap()));
433 os << "INSTRUCTION SET FEATURES:\n";
434 os << features->GetFeatureString() << "\n\n";
435 }
Dave Allison70202782013-10-22 17:52:19 -0700436
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437 os << "DEX FILE COUNT:\n";
438 os << oat_header.GetDexFileCount() << "\n\n";
439
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800440#define DUMP_OAT_HEADER_OFFSET(label, offset) \
441 os << label " OFFSET:\n"; \
442 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800444 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
445 } \
446 os << StringPrintf("\n\n");
447
448 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
Vladimir Marko7dac8642019-11-06 17:09:30 +0000449 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
450 GetJniDlsymLookupTrampolineOffset);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000451 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP CRITICAL TRAMPOLINE",
452 GetJniDlsymLookupCriticalTrampolineOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800453 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
454 GetQuickGenericJniTrampolineOffset);
455 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
456 GetQuickImtConflictTrampolineOffset);
457 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
458 GetQuickResolutionTrampolineOffset);
459 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
460 GetQuickToInterpreterBridgeOffset);
461#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700462
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700463 // Print the key-value store.
464 {
465 os << "KEY VALUE STORE:\n";
466 size_t index = 0;
467 const char* key;
468 const char* value;
469 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
470 os << key << " = " << value << "\n";
471 index++;
472 }
473 os << "\n";
474 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700475
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800476 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700477 os << "BEGIN:\n";
478 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700479
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700480 os << "END:\n";
481 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
482 }
483
484 os << "SIZE:\n";
485 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486
487 os << std::flush;
488
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800489 // If set, adjust relative address to be searched
490 if (options_.addr2instr_ != 0) {
491 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
492 os << "SEARCH ADDRESS (executable offset + input):\n";
493 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
494 }
495
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000496 // Dump .data.bimg.rel.ro entries.
497 DumpDataBimgRelRoEntries(os);
498
499 // Dump .bss summary, individual entries are dumped per dex file.
500 os << ".bss: ";
501 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
502 os << "empty.\n\n";
503 } else {
504 os << oat_file_.GetBssMethods().size() << " methods, ";
505 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
506 }
507
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700508 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700509 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700510 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700511 CHECK(oat_dex_file != nullptr);
512 std::string error_msg;
513 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
514 if (dex_file == nullptr) {
515 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
516 << error_msg;
517 continue;
518 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700519
Mathieu Chartier120aa282017-08-05 16:03:03 -0700520 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
521 if (layout_sections != nullptr) {
522 os << "Layout data\n";
523 os << *layout_sections;
524 os << "\n";
525 }
526
Vladimir Markobacb8e42018-06-13 14:45:30 +0100527 if (!options_.dump_header_only_) {
528 // Dump .bss entries.
529 DumpBssEntries(
530 os,
531 "ArtMethod",
532 oat_dex_file->GetMethodBssMapping(),
533 dex_file->NumMethodIds(),
534 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
535 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
536 DumpBssEntries(
537 os,
538 "Class",
539 oat_dex_file->GetTypeBssMapping(),
540 dex_file->NumTypeIds(),
541 sizeof(GcRoot<mirror::Class>),
542 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
543 DumpBssEntries(
544 os,
545 "String",
546 oat_dex_file->GetStringBssMapping(),
547 dex_file->NumStringIds(),
548 sizeof(GcRoot<mirror::Class>),
549 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
550 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700551 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700552
David Brazdilc03d7b62016-03-02 12:18:03 +0000553 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000554 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000555 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000556 if (vdex_header.IsValid()) {
557 std::string error_msg;
558 std::vector<const DexFile*> dex_files;
559 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
560 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
561 if (dex_file == nullptr) {
562 os << "Error opening dex file: " << error_msg << std::endl;
563 return false;
564 }
565 dex_files.push_back(dex_file);
566 }
Vladimir Markoc3908792020-04-06 14:52:04 +0100567 verifier::VerifierDeps deps(dex_files, /*output_only=*/ false);
568 if (!deps.ParseStoredData(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData())) {
569 os << "Error parsing verifier dependencies." << std::endl;
570 return false;
571 }
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000572 deps.Dump(&vios);
573 } else {
574 os << "UNRECOGNIZED vdex file, magic "
575 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000576 << ", verifier deps version "
577 << vdex_header.GetVerifierDepsVersion()
578 << ", dex section version "
579 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000580 << "\n";
581 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000582 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700583 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000584 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300585 if (!DumpOatDexFile(os, *oat_dex_file)) {
586 success = false;
587 }
588 }
589 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800590
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300591 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000592 std::string error_msg;
593 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
594 if (!OS::FileExists(vdex_filename.c_str())) {
595 os << "File " << vdex_filename.c_str() << " does not exist\n";
596 return false;
597 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300598
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000599 DexFileUniqV vdex_dex_files;
600 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
601 &vdex_dex_files,
602 &error_msg);
603 if (vdex_file.get() == nullptr) {
604 os << "Failed to open vdex file: " << error_msg << "\n";
605 return false;
606 }
607 if (oat_dex_files_.size() != vdex_dex_files.size()) {
608 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
609 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
610 return false;
611 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300612
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000613 size_t i = 0;
614 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700615 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000616 CHECK(oat_dex_file != nullptr);
617 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300618
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300619 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
620 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
621 // the original input Dex file, the `update_checksum_` option is used to recompute the
622 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
623 // false), ExportDexFile() enforces a reproducible checksum verification.
624 if (vdex_dex_file->IsCompactDexFile()) {
625 Options options;
626 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
627 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700628 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300629 std::unique_ptr<art::DexContainer> dex_container;
630 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
631 vdex_dex_file.get(),
632 i,
633 &dex_container,
634 &error_msg);
635 if (!result) {
636 os << "DexLayout failed to process Dex file: " + error_msg;
637 success = false;
638 break;
639 }
640 DexContainer::Section* main_section = dex_container->GetMainSection();
641 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
642
643 const ArtDexFileLoader dex_file_loader;
644 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
645 main_section->Begin(),
646 main_section->Size(),
647 vdex_dex_file->GetLocation(),
648 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700649 /*oat_dex_file=*/ nullptr,
650 /*verify=*/ false,
651 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300652 &error_msg));
653 if (dex == nullptr) {
654 os << "Failed to load DexFile from layout container: " + error_msg;
655 success = false;
656 break;
657 }
658 if (dex->IsCompactDexFile()) {
659 os <<"CompactDex conversion to StandardDex failed";
660 success = false;
661 break;
662 }
663
Andreas Gampe9b031f72018-10-04 11:03:34 -0700664 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300665 success = false;
666 break;
667 }
668 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700669 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300670 success = false;
671 break;
672 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300673 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000674 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700675 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700676 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000677
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800678 {
679 os << "OAT FILE STATS:\n";
680 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100681 stats_.AddBytes(oat_file_.Size());
682 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800683 }
684
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700685 os << std::flush;
686 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700687 }
688
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800689 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700690 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
691 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800692 return 0; // Address not in oat file
693 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800694 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
695 reinterpret_cast<uintptr_t>(oat_file_.Begin());
696 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800697 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800698 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800699 return end_offset - begin_offset;
700 }
701
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800702 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700703 return oat_file_.GetOatHeader().GetInstructionSet();
704 }
705
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700706 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800707 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700708 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700709 CHECK(oat_dex_file != nullptr);
710 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700711 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
712 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700713 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
714 << "': " << error_msg;
715 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800716 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800717 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700718 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700719 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700720 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100721 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Vladimir Markoe00e2012019-05-14 14:42:02 +0100722 uint32_t oat_method_index;
723 if (m->IsStatic() || m->IsDirect()) {
724 // Simple case where the oat method index was stashed at load time.
725 oat_method_index = m->GetMethodIndex();
726 } else {
727 // Compute the oat_method_index by search for its position in the class def.
728 ClassAccessor accessor(*dex_file, *class_def);
729 oat_method_index = accessor.NumDirectMethods();
730 bool found_virtual = false;
731 for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
732 // Check method index instead of identity in case of duplicate method definitions.
733 if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
734 found_virtual = true;
735 break;
736 }
737 ++oat_method_index;
738 }
739 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
740 << dex_file->PrettyMethod(m->GetDexMethodIndex());
741 }
742 return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 }
744 }
745 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700746 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800747 }
748
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300749 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
750 // files are fully unquickened and stored in dex_files
751 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
752 /* out */ DexFileUniqV* dex_files,
753 /* out */ std::string* error_msg) {
754 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
755 if (file == nullptr) {
756 *error_msg = "Could not open file " + vdex_filename + " for reading.";
757 return nullptr;
758 }
759
760 int64_t vdex_length = file->GetLength();
761 if (vdex_length == -1) {
762 *error_msg = "Could not read the length of file " + vdex_filename;
763 return nullptr;
764 }
765
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100766 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300767 file->GetLength(),
768 PROT_READ | PROT_WRITE,
769 MAP_PRIVATE,
770 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700771 /* start offset= */ 0,
772 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300773 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100774 error_msg);
775 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300776 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
777 return nullptr;
778 }
779
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100780 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300781 if (!vdex_file->IsValid()) {
782 *error_msg = "Vdex file is not valid";
783 return nullptr;
784 }
785
786 DexFileUniqV tmp_dex_files;
787 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
788 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
789 return nullptr;
790 }
791
792 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700793 /* decompile_return_instruction= */ true);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300794
795 *dex_files = std::move(tmp_dex_files);
796 return vdex_file;
797 }
798
David Srbecky86decb62018-06-05 06:41:10 +0100799 bool AddStatsObject(const void* address) {
800 return seen_stats_objects_.insert(address).second; // Inserted new entry.
801 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800802
David Srbecky86decb62018-06-05 06:41:10 +0100803 void DumpStats(VariableIndentationOutputStream& os,
804 const std::string& name,
805 const Stats& stats,
806 double total) {
807 if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
808 double percent = 100.0 * stats.Value() / total;
809 os.Stream()
810 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
811 << std::setw(8) << stats.Count() << " "
812 << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
813 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
814
815 // Sort all children by largest value first, than by name.
816 std::map<std::pair<double, std::string>, const Stats&> sorted_children;
817 for (const auto& it : stats.Children()) {
818 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800819 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800820
David Srbecky86decb62018-06-05 06:41:10 +0100821 // Add "other" row to represent any amount not account for by the children.
822 Stats other;
823 other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
824 if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
825 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800826 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800827
David Srbecky86decb62018-06-05 06:41:10 +0100828 // Print the data.
829 ScopedIndentation indent1(&os);
830 for (const auto& it : sorted_children) {
831 DumpStats(os, it.first.second, it.second, total);
832 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800833 }
David Srbecky86decb62018-06-05 06:41:10 +0100834 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800835
Brian Carlstromaded5f72011-10-07 17:15:04 -0700836 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800837 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800838 // We don't know the length of the code for each method, but we need to know where to stop
839 // when disassembling. What we do know is that a region of code will be followed by some other
840 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
841 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800842 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700843 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700844 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700845 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700846 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
847 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700848 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
849 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800850 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800851 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800852 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700853 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700854 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700855 for (uint32_t class_method_index = 0;
856 class_method_index < accessor.NumMethods();
857 ++class_method_index) {
858 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800859 }
860 }
861 }
862
863 // If the last thing in the file is code for a method, there won't be an offset for the "next"
864 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
865 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800866 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800867 }
868
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700869 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
870 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
871 }
872
Elliott Hughese3c845c2012-02-28 17:23:01 -0800873 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800874 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000875 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800876 code_offset &= ~0x1;
877 }
878 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800879 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800880 }
881
Andreas Gampeb40d3612018-06-26 15:49:42 -0700882 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700883 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800884 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700885 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800886 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800887 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700888
Andreas Gampe2ba88952016-04-29 17:52:07 -0700889 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000890 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
891 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100892
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000893 // Print data range of the dex file embedded inside the corresponding vdex file.
894 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
895 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
896 os << StringPrintf(
897 "dex-file: 0x%08x..0x%08x\n",
898 dex_offset,
899 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
900 } else {
901 os << StringPrintf("dex-file not in VDEX file\n");
902 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700903
Andreas Gampe2ba88952016-04-29 17:52:07 -0700904 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700905 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700906 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
907 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700908 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 os << std::flush;
910 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700911 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912
Andreas Gampe2ba88952016-04-29 17:52:07 -0700913 // Print lookup table, if it exists.
914 if (oat_dex_file.GetLookupTableData() != nullptr) {
915 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
916 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700917 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700918 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
919 table_offset,
920 table_offset + table_size - 1);
921 }
922
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100923 VariableIndentationOutputStream vios(&os);
924 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700925 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800926 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700927 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800928 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
929 continue;
930 }
931
Mathieu Chartier98dad402018-06-04 18:00:12 -0700932 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100934 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700936 static_cast<ssize_t>(class_def_index),
937 descriptor,
938 oat_class_offset,
939 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100940 << " (" << oat_class.GetStatus() << ")"
941 << " (" << oat_class.GetType() << ")\n";
942 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700943 if (options_.list_classes_) {
944 continue;
945 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700946 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700947 success = false;
948 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800949 if (stop_analysis) {
950 os << std::flush;
951 return success;
952 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700953 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700954 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700955 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700956 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700957 }
958
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300959 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
960 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
961 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
962 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300963 bool ExportDexFile(std::ostream& os,
964 const OatDexFile& oat_dex_file,
965 const DexFile* dex_file,
966 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800967 std::string error_msg;
968 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300969
970 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
971 // same as the one extracted from the Oat container (pre-oreo)
972 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800973
974 // Some quick checks just in case
975 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
976 os << "Invalid dex file\n";
977 return false;
978 }
979
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300980 if (dex_file == nullptr) {
981 // Exported bytecode is quickened (dex-to-dex transformations present)
982 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
983 if (dex_file == nullptr) {
984 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
985 return false;
986 }
987
988 // Recompute checksum
989 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
990 dex_file->CalculateChecksum();
991 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300992 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
993 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
994 if (!used_dexlayout) {
995 // Vdex unquicken output should match original input bytecode
996 uint32_t orig_checksum =
997 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
998 if (orig_checksum != dex_file->CalculateChecksum()) {
999 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1000 return false;
1001 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001002 }
1003 }
1004
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001005 // Verify output directory exists
1006 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1007 // TODO: Extend OS::DirectoryExists if symlink support is required
1008 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1009 return false;
1010 }
1011
1012 // Beautify path names
1013 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1014 return false;
1015 }
1016
1017 std::string dex_orig_name;
1018 size_t dex_orig_pos = dex_file_location.rfind('/');
1019 if (dex_orig_pos == std::string::npos)
1020 dex_orig_name = dex_file_location;
1021 else
1022 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1023
1024 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001025 if (dex_orig_name.size() == 8 &&
1026 dex_orig_name.compare("base.apk") == 0 &&
1027 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001028 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1029 size_t apk_orig_pos = dex_file_location.rfind('/');
1030 if (apk_orig_pos != std::string::npos) {
1031 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1032 }
1033 }
1034
1035 std::string out_dex_path(options_.export_dex_location_);
1036 if (out_dex_path.back() != '/') {
1037 out_dex_path.append("/");
1038 }
1039 out_dex_path.append(dex_orig_name);
1040 out_dex_path.append("_export.dex");
1041 if (out_dex_path.length() > PATH_MAX) {
1042 return false;
1043 }
1044
1045 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1046 if (file.get() == nullptr) {
1047 os << "Failed to open output dex file " << out_dex_path;
1048 return false;
1049 }
1050
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001051 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001052 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001053 os << "Failed to write dex file";
1054 file->Erase();
1055 return false;
1056 }
1057
1058 if (file->FlushCloseOrErase() != 0) {
1059 os << "Flush and close failed";
1060 return false;
1061 }
1062
1063 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1064 os << std::flush;
1065
1066 return true;
1067 }
1068
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001069 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001070 const OatFile::OatClass& oat_class,
1071 const DexFile& dex_file,
1072 const ClassAccessor& class_accessor,
1073 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001075 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001076 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001077 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1078 if (!DumpOatMethod(vios,
1079 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1080 class_method_index,
1081 oat_class,
1082 dex_file,
1083 method.GetIndex(),
1084 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001085 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001086 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001087 success = false;
1088 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001089 if (addr_found) {
1090 *stop_analysis = true;
1091 return success;
1092 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001093 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001094 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001095 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001097 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001098
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001099 static constexpr uint32_t kPrologueBytes = 16;
1100
1101 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1102 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1103
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001104 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001105 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001106 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001107 const OatFile::OatClass& oat_class,
1108 const DexFile& dex_file,
1109 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001110 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001111 uint32_t method_access_flags,
1112 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001114
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001115 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001116
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001117 // TODO: Support regex
1118 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1119 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001120 return success;
1121 }
1122
David Sehr709b0702016-10-13 09:12:37 -07001123 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001124 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1125 class_method_index, pretty_method.c_str(),
1126 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001127 if (options_.list_methods_) {
1128 return success;
1129 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001130
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001131 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1132 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1133 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1134 uint32_t code_offset = oat_method.GetCodeOffset();
1135 uint32_t code_size = oat_method.GetQuickCodeSize();
1136 if (resolved_addr2instr_ != 0) {
1137 if (resolved_addr2instr_ > code_offset + code_size) {
1138 return success;
1139 } else {
1140 *addr_found = true; // stop analyzing file at next iteration
1141 }
1142 }
1143
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001144 // Everything below is indented at least once.
1145 ScopedIndentation indent1(vios);
1146
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001147 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001148 vios->Stream() << "DEX CODE:\n";
1149 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001150 if (code_item_accessor.HasCodeItem()) {
1151 for (const DexInstructionPcPair& inst : code_item_accessor) {
1152 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1153 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1154 }
1155 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001156 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001157
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001158 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001159 std::unique_ptr<verifier::MethodVerifier> verifier;
1160 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001161 // We need to have the handle scope stay live until after the verifier since the verifier has
1162 // a handle to the dex cache from hs.
1163 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1165 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001166 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001167 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001168 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001169 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001170 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001171 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001172 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001173 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001174 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001175 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001176 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001177 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001178 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1179 oat_method_offsets_offset, oat_file_.Size());
1180 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001181 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001182 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001183 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001184
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001185 ScopedIndentation indent2(vios);
1186 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001187 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1188 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001189 vios->Stream() << StringPrintf("WARNING: "
1190 "code offset 0x%08x is past end of file 0x%08zx.\n",
1191 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001192 success = false;
1193 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001194 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 }
1196 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001197 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1199 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001200 if (AddStatsObject(method_header)) {
1201 stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1202 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001203 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001205 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001207 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001209 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1210 method_header_offset, oat_file_.Size());
1211 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001212 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001213 return false;
1214 }
1215
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001218 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001220 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001221 uint32_t vmap_table_offset = method_header ==
1222 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001223 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001224
1225 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001226 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001227 ? oat_file_.GetVdexFile()->Size()
1228 : method_header->GetCode() - oat_file_.Begin();
1229 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001230 vios->Stream() << StringPrintf("WARNING: "
1231 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1232 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001233 vmap_table_offset,
1234 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001236 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001237 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001238 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001239 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001240 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001243
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 ScopedIndentation indent2(vios);
1245 vios->Stream()
1246 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1247 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1248 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1249 vios->Stream() << "\n";
1250 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1251 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1252 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253 }
1254 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 // Based on spill masks from QuickMethodFrameInfo so placed
1256 // after it is dumped, but useful for understanding quick
1257 // code, so dumped here.
1258 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001259 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001260 }
1261 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 vios->Stream() << "CODE: ";
Nicolas Geoffray57083762019-03-05 09:24:45 +00001263 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1264 if (code_size_offset > oat_file_.Size()) {
1265 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001266 vios->Stream() << StringPrintf("WARNING: "
Nicolas Geoffray57083762019-03-05 09:24:45 +00001267 "code size offset 0x%08x is past end of file 0x%08zx.",
1268 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 success = false;
Nicolas Geoffray57083762019-03-05 09:24:45 +00001270 } else {
1271 const void* code = oat_method.GetQuickCode();
1272 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1273 uint64_t aligned_code_end = aligned_code_begin + code_size;
1274 if (AddStatsObject(code)) {
1275 stats_.Child("Code")->AddBytes(code_size);
1276 }
1277
1278 if (options_.absolute_addresses_) {
1279 vios->Stream() << StringPrintf("%p ", code);
1280 }
1281 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1282 code_offset,
1283 code_size_offset,
1284 code_size,
1285 code != nullptr ? "..." : "");
1286
1287 ScopedIndentation indent2(vios);
1288 if (aligned_code_begin > oat_file_.Size()) {
1289 vios->Stream() << StringPrintf("WARNING: "
1290 "start of code at 0x%08x is past end of file 0x%08zx.",
1291 aligned_code_begin, oat_file_.Size());
1292 success = false;
1293 } else if (aligned_code_end > oat_file_.Size()) {
1294 vios->Stream() << StringPrintf(
1295 "WARNING: "
1296 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1297 "code size is 0x%08x loaded from offset 0x%08x.\n",
1298 aligned_code_end, oat_file_.Size(),
1299 code_size, code_size_offset);
1300 success = false;
1301 if (options_.disassemble_code_) {
1302 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1303 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1304 }
1305 }
1306 } else if (code_size > kMaxCodeSize) {
1307 vios->Stream() << StringPrintf(
1308 "WARNING: "
1309 "code size %d is bigger than max expected threshold of %d. "
1310 "code size is 0x%08x loaded from offset 0x%08x.\n",
1311 code_size, kMaxCodeSize,
1312 code_size, code_size_offset);
1313 success = false;
1314 if (options_.disassemble_code_) {
1315 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1316 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1317 }
1318 }
1319 } else if (options_.disassemble_code_) {
1320 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1321 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001322 }
1323 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001324 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001325 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001326 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001327
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001328 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1329 if (spill_mask == 0) {
1330 return;
1331 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001333 for (size_t i = 0; i < 32; i++) {
1334 if ((spill_mask & (1 << i)) != 0) {
1335 if (is_float) {
1336 os << "fr" << i;
1337 } else {
1338 os << "r" << i;
1339 }
1340 spill_mask ^= 1 << i; // clear bit
1341 if (spill_mask != 0) {
1342 os << ", ";
1343 } else {
1344 break;
1345 }
1346 }
1347 }
1348 os << ")";
1349 }
1350
Roland Levillain442b46a2015-02-18 16:54:21 +00001351 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001352 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001353 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001354 const CodeItemDataAccessor& code_item_accessor) {
1355 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001356 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001357 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001358 if (raw_code_info != nullptr) {
1359 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001360 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001361 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001362 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001363 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001364 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001365 // We don't encode the size in the table, so just emit that we have quickened
1366 // information.
1367 ScopedIndentation indent(vios);
1368 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001369 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001370 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001371 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001372 }
1373
1374 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001376 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001377 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001378 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001379 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001380 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001381 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001382 }
1383
Andreas Gampe36a296f2017-06-13 14:11:11 -07001384 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1385 // According to stack model, the first out is above the Method referernce.
1386 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1387 }
1388
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001389 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001390 uint32_t core_spills,
1391 uint32_t fp_spills,
1392 size_t frame_size,
1393 int reg,
1394 InstructionSet isa) {
1395 PointerSize pointer_size = InstructionSetPointerSize(isa);
1396 if (kIsDebugBuild) {
1397 auto* runtime = Runtime::Current();
1398 if (runtime != nullptr) {
1399 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1400 }
1401 }
1402 DCHECK_ALIGNED(frame_size, kStackAlignment);
1403 DCHECK_NE(reg, -1);
1404 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1405 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1406 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001407 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1408 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001409 const int max_num_special_temps = 1;
1410 if (reg == temp_threshold) {
1411 // The current method pointer corresponds to special location on stack.
1412 return 0;
1413 } else if (reg >= temp_threshold + max_num_special_temps) {
1414 /*
1415 * Special temporaries may have custom locations and the logic above deals with that.
1416 * However, non-special temporaries are placed relative to the outs.
1417 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001418 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001419 + static_cast<size_t>(pointer_size) /* art method */;
1420 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1421 return temps_start + relative_offset;
1422 } else if (reg < num_regs) {
1423 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1424 return locals_start + (reg * sizeof(uint32_t));
1425 } else {
1426 // Handle ins.
1427 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1428 + static_cast<size_t>(pointer_size) /* art method */;
1429 }
1430 }
1431
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001432 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001433 const CodeItemDataAccessor& code_item_accessor) {
1434 if (code_item_accessor.HasCodeItem()) {
1435 size_t num_locals_ins = code_item_accessor.RegistersSize();
1436 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001437 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001438 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001439
1440 os << "vr_stack_locations:";
1441 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1442 // For readability, delimit the different kinds of VRs.
1443 if (reg == num_locals_ins) {
1444 os << "\n\tmethod*:";
1445 } else if (reg == num_locals && num_ins > 0) {
1446 os << "\n\tins:";
1447 } else if (reg == 0 && num_locals > 0) {
1448 os << "\n\tlocals:";
1449 }
1450
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001451 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001452 oat_method.GetCoreSpillMask(),
1453 oat_method.GetFpSpillMask(),
1454 oat_method.GetFrameSizeInBytes(),
1455 reg,
1456 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001457 os << " v" << reg << "[sp + #" << offset << "]";
1458 }
1459
1460 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1461 if (out_reg == 0) {
1462 os << "\n\touts:";
1463 }
1464
Andreas Gampe36a296f2017-06-13 14:11:11 -07001465 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001466 os << " v" << out_reg << "[sp + #" << offset << "]";
1467 }
1468
1469 os << "\n";
1470 }
1471 }
1472
Roland Levillainf2650d12015-05-28 14:53:28 +01001473 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1474 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001475 static bool IsMethodGeneratedByOptimizingCompiler(
1476 const OatFile::OatMethod& oat_method,
1477 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001478 // If the native GC map is null and the Dex `code_item` is not
1479 // null, then this method has been compiled with the optimizing
1480 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001481 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001482 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001483 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001484 }
1485
1486 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1487 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001488 static bool IsMethodGeneratedByDexToDexCompiler(
1489 const OatFile::OatMethod& oat_method,
1490 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001491 // If the quick code is null, the Dex `code_item` is not
1492 // null, and the vmap table is not null, then this method has been compiled
1493 // with the dextodex compiler.
1494 return oat_method.GetQuickCode() == nullptr &&
1495 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001496 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001497 }
1498
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001500 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001501 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001502 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001503 const dex::ClassDef& class_def,
1504 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001505 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 if ((method_access_flags & kAccNative) == 0) {
1507 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001508 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001509 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001510 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1511 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1512 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001513 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1514 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001515 if (method == nullptr) {
1516 soa.Self()->ClearException();
1517 return nullptr;
1518 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001519 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001520 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001521 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001523
1524 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001525 }
1526
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001527 // The StackMapsHelper provides the stack maps in the native PC order.
1528 // For identical native PCs, the order from the CodeInfo is preserved.
1529 class StackMapsHelper {
1530 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001531 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001532 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001533 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001534 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001535 offset_(static_cast<uint32_t>(-1)),
1536 stack_map_index_(0u),
1537 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001538 if (number_of_stack_maps_ != 0u) {
1539 // Check if native PCs are ordered.
1540 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001541 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001542 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001543 StackMap current = code_info_.GetStackMapAt(i);
1544 if (last.GetNativePcOffset(instruction_set) >
1545 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001546 ordered = false;
1547 break;
1548 }
1549 last = current;
1550 }
1551 if (!ordered) {
1552 // Create indirection indexes for access in native PC order. We do not optimize
1553 // for the fact that there can currently be only two separately ordered ranges,
1554 // namely normal stack maps and catch-point stack maps.
1555 indexes_.resize(number_of_stack_maps_);
1556 std::iota(indexes_.begin(), indexes_.end(), 0u);
1557 std::sort(indexes_.begin(),
1558 indexes_.end(),
1559 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001560 StackMap left = code_info_.GetStackMapAt(lhs);
1561 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1562 StackMap right = code_info_.GetStackMapAt(rhs);
1563 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001564 // If the PCs are the same, compare indexes to preserve the original order.
1565 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1566 });
1567 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001568 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001569 }
1570 }
1571
1572 const CodeInfo& GetCodeInfo() const {
1573 return code_info_;
1574 }
1575
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001576 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001577 return offset_;
1578 }
1579
1580 StackMap GetStackMap() const {
1581 return GetStackMapAt(stack_map_index_);
1582 }
1583
1584 void Next() {
1585 ++stack_map_index_;
1586 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001587 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001588 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001589 }
1590
1591 private:
1592 StackMap GetStackMapAt(size_t i) const {
1593 if (!indexes_.empty()) {
1594 i = indexes_[i];
1595 }
1596 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001597 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001598 }
1599
1600 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001601 const size_t number_of_stack_maps_;
1602 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001603 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001604 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001605 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001606 };
1607
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001608 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001609 const OatFile::OatMethod& oat_method,
1610 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001611 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001612 const void* quick_code = oat_method.GetQuickCode();
1613
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001614 if (code_size == 0) {
1615 code_size = oat_method.GetQuickCodeSize();
1616 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001617 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001618 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001619 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001620 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1621 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001622 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001623 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
David Srbecky86decb62018-06-05 06:41:10 +01001624 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky42deda82018-08-10 11:23:27 +01001625 helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
David Srbecky86decb62018-06-05 06:41:10 +01001626 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001627 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1628 size_t offset = 0;
1629 while (offset < code_size) {
1630 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1631 if (offset == helper.GetOffset()) {
1632 ScopedIndentation indent1(vios);
1633 StackMap stack_map = helper.GetStackMap();
1634 DCHECK(stack_map.IsValid());
1635 stack_map.Dump(vios,
1636 helper.GetCodeInfo(),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001637 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001638 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001639 do {
1640 helper.Next();
1641 // There may be multiple stack maps at a given PC. We display only the first one.
1642 } while (offset == helper.GetOffset());
1643 }
1644 DCHECK_LT(offset, helper.GetOffset());
1645 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001646 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001647 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1648 size_t offset = 0;
1649 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001650 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001652 }
1653 }
1654
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001655 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1656 REQUIRES_SHARED(Locks::mutator_lock_) {
1657 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1658 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1659 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1660 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1661 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1662 DCHECK(boot_image_live_objects != nullptr);
1663 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1664 const uint8_t* boot_image_live_objects_address =
1665 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1666 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1667 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1668 boot_image_live_objects->GetLength()).Uint32Value();
1669 return std::make_pair(boot_image_live_objects_address + begin_offset,
1670 boot_image_live_objects_address + end_offset);
1671 }
1672
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001673 void DumpDataBimgRelRoEntries(std::ostream& os) {
1674 os << ".data.bimg.rel.ro: ";
1675 if (oat_file_.GetBootImageRelocations().empty()) {
1676 os << "empty.\n\n";
1677 return;
1678 }
1679
1680 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1681 Runtime* runtime = Runtime::Current();
1682 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1683 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1684 runtime->GetHeap()->GetBootImageSpaces();
1685 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001686 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1687 const uint8_t* live_objects_begin = live_objects.first;
1688 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001689 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1690 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1691 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1692 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001693 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001694 bool found = false;
1695 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001696 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001697 if (local_offset < space->GetImageHeader().GetImageSize()) {
1698 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001699 if (address >= live_objects_begin && address < live_objects_end) {
1700 size_t index =
1701 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1702 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001703 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001704 index);
1705 } else {
1706 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1707 if (o->IsString()) {
1708 os << " String: " << o->AsString()->ToModifiedUtf8();
1709 } else if (o->IsClass()) {
1710 os << " Class: " << o->AsClass()->PrettyDescriptor();
1711 } else {
1712 os << StringPrintf(" 0x%08x %s",
1713 object_offset,
1714 o->GetClass()->PrettyDescriptor().c_str());
1715 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001716 }
1717 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001718 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001719 os << " ArtMethod: " << m->PrettyMethod();
1720 } else {
1721 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1722 object_offset,
1723 space->GetImageFilename().c_str());
1724 }
1725 found = true;
1726 break;
1727 }
1728 }
1729 if (!found) {
1730 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1731 }
1732 os << "\n";
1733 }
1734 } else {
1735 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1736 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1737 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1738 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1739 }
1740 }
1741 os << "\n";
1742 }
1743
Vladimir Markof3c52b42017-11-17 17:32:12 +00001744 template <typename NameGetter>
1745 void DumpBssEntries(std::ostream& os,
1746 const char* slot_type,
1747 const IndexBssMapping* mapping,
1748 uint32_t number_of_indexes,
1749 size_t slot_size,
1750 NameGetter name) {
1751 os << ".bss mapping for " << slot_type << ": ";
1752 if (mapping == nullptr) {
1753 os << "empty.\n";
1754 return;
1755 }
1756 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1757 size_t num_valid_indexes = 0u;
1758 for (const IndexBssMappingEntry& entry : *mapping) {
1759 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1760 }
1761 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1762 os << std::hex;
1763 for (const IndexBssMappingEntry& entry : *mapping) {
1764 uint32_t index = entry.GetIndex(index_bits);
1765 uint32_t mask = entry.GetMask(index_bits);
1766 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1767 for (uint32_t n : LowToHighBits(mask)) {
1768 size_t current_index = index - (32u - index_bits) + n;
1769 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1770 bss_offset += slot_size;
1771 }
1772 DCHECK_EQ(bss_offset, entry.bss_offset);
1773 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1774 }
1775 os << std::dec;
1776 }
1777
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001778 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001779 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001780 const OatDumperOptions& options_;
1781 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001782 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001783 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001784 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001785 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001786 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001787};
1788
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001789class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001790 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001791 ImageDumper(std::ostream* os,
1792 gc::space::ImageSpace& image_space,
1793 const ImageHeader& image_header,
1794 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001795 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001796 vios_(os),
1797 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001798 image_space_(image_space),
1799 image_header_(image_header),
1800 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001801
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001802 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001804 std::ostream& indent_os = vios_.Stream();
1805
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001806 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001807
Jeff Haodcdc85b2015-12-04 14:06:18 -08001808 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1809
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001810 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1811 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1812 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001813
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001814 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1815 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1816 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1817 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1818 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1819
1820 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1821 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001822 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001823
1824 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1825 auto section = static_cast<ImageHeader::ImageSections>(i);
1826 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1827 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001828
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001829 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001830 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001831 static_assert(arraysize(image_roots_descriptions_) ==
1832 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001833 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1834 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001835 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1836 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001837 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1838 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001839 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001840 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001841 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001842 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001843 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001844 ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001845 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001846 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1847 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001848 run++;
1849 } else {
1850 break;
1851 }
1852 }
1853 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001855 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001856 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001857 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001858 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001859 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001860 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001861 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001862 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001863 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001864 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001865 }
1866 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001867 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001868
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001869 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001870 os << "METHOD ROOTS\n";
1871 static_assert(arraysize(image_methods_descriptions_) ==
1872 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1873 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1874 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1875 const char* description = image_methods_descriptions_[i];
1876 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001877 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001878 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001879 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001880 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001881
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001882 Runtime* const runtime = Runtime::Current();
1883 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001884 std::string image_filename = image_space_.GetImageFilename();
1885 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001886 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001887 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001888 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001889 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001890 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001891 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1892 }
1893 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001894 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001895 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001896 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001897 /*executable=*/ false,
1898 /*low_4gb=*/ false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001899 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001900 }
1901 if (oat_file == nullptr) {
1902 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1903 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001904 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001905 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001906
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001907 stats_.oat_file_bytes = oat_file->Size();
1908
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001909 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001910
Andreas Gampeb40d3612018-06-26 15:49:42 -07001911 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001912 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001913 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1914 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001915 }
1916
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001917 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001918
Jeff Haodcdc85b2015-12-04 14:06:18 -08001919 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001920 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001921 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001922 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001923 {
1924 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1925 heap->FlushAllocStack();
1926 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001927 // Since FlushAllocStack() above resets the (active) allocation
1928 // stack. Need to revoke the thread-local allocation stacks that
1929 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001930 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001931 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001932 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001933 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001934 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001935 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001936 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001937 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001938 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001939 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001940 ObjPtr<mirror::DexCache> dex_cache =
1941 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001942 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001943 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001944 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001945 }
1946 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001947 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1948 DumpObject(obj);
1949 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001951 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001952 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001953 indent_os << "\n";
1954 // TODO: Dump fields.
1955 // Dump methods after.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -07001956 image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1957 REQUIRES_SHARED(Locks::mutator_lock_) {
1958 std::ostream& indent_os = vios_.Stream();
1959 indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1960 DumpMethod(&method, indent_os);
1961 indent_os << "\n";
1962 }, image_space_.Begin(), image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001963 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001964 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001965 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001966 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001967 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001968 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001969 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1970 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001971 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001972 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001973 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001974 // If the image is compressed, adjust to decompressed size.
1975 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartiera5c01d72019-07-26 14:39:28 -07001976 if (!image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001977 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1978 }
1979 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001980 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001981 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001982 const auto& object_section = image_header_.GetObjectsSection();
1983 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001984 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001985 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1986 const auto& intern_section = image_header_.GetInternedStringsSection();
1987 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07001988 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1989 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001990 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001991
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001992 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001993
1994 // Objects are kObjectAlignment-aligned.
1995 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1996 if (object_section.Offset() > header_bytes) {
1997 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1998 }
1999
2000 // Field section is 4-byte aligned.
2001 constexpr size_t kFieldSectionAlignment = 4U;
2002 uint32_t end_objects = object_section.Offset() + object_section.Size();
2003 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2004 stats_.alignment_bytes += field_section.Offset() - end_objects;
2005
2006 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2007 uint32_t end_fields = field_section.Offset() + field_section.Size();
2008 CHECK_ALIGNED(method_section.Offset(), 4);
2009 stats_.alignment_bytes += method_section.Offset() - end_fields;
2010
2011 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2012 uint32_t end_methods = method_section.Offset() + method_section.Size();
2013 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2014 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2015
2016 // Intern table is 8-byte aligned.
2017 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002018 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002019 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2020
2021 // Add space between intern table and class table.
2022 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2023 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2024
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002025 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2026 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002027 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002028 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002029
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 stats_.bitmap_bytes += bitmap_section.Size();
2031 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002032 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002033 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002034 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002035 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002036 stats_.sro_offset_bytes += sro_section.Size();
2037 stats_.metadata_bytes += metadata_section.Size();
2038
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002039 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002040 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002041
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002042 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002043
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002044 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002045 }
2046
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002047 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -07002048 static void PrettyObjectValue(std::ostream& os,
2049 ObjPtr<mirror::Class> type,
2050 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002051 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002052 CHECK(type != nullptr);
2053 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002054 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002055 } else if (type->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002056 ObjPtr<mirror::String> string = value->AsString();
2057 os << StringPrintf("%p String: %s\n",
2058 string.Ptr(),
Ian Rogers68b56852014-08-29 20:19:11 -07002059 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002060 } else if (type->IsClassClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002061 ObjPtr<mirror::Class> klass = value->AsClass();
2062 os << StringPrintf("%p Class: %s\n",
2063 klass.Ptr(),
2064 mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002065 } else {
David Sehr709b0702016-10-13 09:12:37 -07002066 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002067 }
2068 }
2069
Mathieu Chartier3398c782016-09-30 10:27:43 -07002070 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002071 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002072 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002073 switch (field->GetTypeAsPrimitiveType()) {
2074 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002075 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002076 break;
2077 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002078 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002079 break;
2080 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002082 break;
2083 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002085 break;
2086 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002087 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002088 break;
2089 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002090 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 break;
2092 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002093 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002094 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002095 break;
2096 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002097 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002098 break;
2099 case Primitive::kPrimNot: {
2100 // Get the value, don't compute the type unless it is non-null as we don't want
2101 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002102 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002103 if (value == nullptr) {
2104 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002105 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002106 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002107 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002108 if (field_type != nullptr) {
2109 PrettyObjectValue(os, field_type, value);
2110 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002111 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002112 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002113 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2114 }
Ian Rogers50239c72013-06-17 14:53:22 -07002115 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002116 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002117 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002118 default:
2119 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2120 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002121 }
2122 }
2123
Andreas Gampe98104992018-10-16 12:49:47 -07002124 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002125 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002126 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002127 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002128 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002129 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002130 for (ArtField& field : klass->GetIFields()) {
2131 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002132 }
2133 }
2134
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002135 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002137 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002138
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002139 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002140 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002141 image_header_.GetPointerSize());
David Srbeckya54e0cc2019-04-15 15:03:26 +01002142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2143 if (class_linker->IsQuickResolutionStub(quick_code) ||
2144 class_linker->IsQuickToInterpreterBridge(quick_code) ||
2145 class_linker->IsQuickGenericJniStub(quick_code) ||
Vladimir Markofa458ac2020-02-12 14:08:07 +00002146 class_linker->IsJniDlsymLookupStub(quick_code) ||
2147 class_linker->IsJniDlsymLookupCriticalStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002148 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002150 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002151 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002152 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002153 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002154 }
2155
Mathieu Chartiere401d142015-04-22 13:56:20 -07002156 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002157 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002158 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2159 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002160 return 0;
2161 }
David Srbeckya54e0cc2019-04-15 15:03:26 +01002162 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2163 reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2164 return method_header->GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002165 }
2166
Mathieu Chartiere401d142015-04-22 13:56:20 -07002167 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002168 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002169 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002170 if (oat_code_begin == nullptr) {
2171 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002172 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002173 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002174 }
2175
Andreas Gampe0c183382017-07-13 22:26:24 -07002176 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002177 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002178 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002179 return;
2180 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002181
2182 size_t object_bytes = obj->SizeOf();
2183 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002184 stats_.object_bytes += object_bytes;
2185 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002186
Andreas Gampe0c183382017-07-13 22:26:24 -07002187 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002188
Vladimir Marko4617d582019-03-28 13:48:31 +00002189 ObjPtr<mirror::Class> obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002190 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002191 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002193 } else if (obj->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002194 ObjPtr<mirror::Class> klass = obj->AsClass();
2195 os << StringPrintf("%p: java.lang.Class \"%s\" (",
2196 obj,
David Sehr709b0702016-10-13 09:12:37 -07002197 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002198 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002199 } else if (obj_class->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002200 os << StringPrintf("%p: java.lang.String %s\n",
2201 obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002202 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002203 } else {
David Sehr709b0702016-10-13 09:12:37 -07002204 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002205 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002206 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002207 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002208 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002209 if (obj->IsObjectArray()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002210 ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002211 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00002212 ObjPtr<mirror::Object> value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002213 size_t run = 0;
2214 for (int32_t j = i + 1; j < length; j++) {
2215 if (value == obj_array->Get(j)) {
2216 run++;
2217 } else {
2218 break;
2219 }
2220 }
2221 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002222 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002223 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002224 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002225 i = i + run;
2226 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002227 ObjPtr<mirror::Class> value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002228 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002229 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002230 }
2231 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002232 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002233
Vladimir Marko305c38b2018-02-14 11:50:07 +00002234 if (kBitstringSubtypeCheckEnabled) {
2235 os << "SUBTYPE_CHECK_BITS: ";
2236 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2237 os << "\n";
2238 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002239
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002240 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002241 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002242 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002243 for (ArtField& field : klass->GetSFields()) {
2244 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002245 }
2246 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002247 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002248 auto it = dex_caches_.find(obj);
2249 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002250 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002251 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002252 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002253 size_t num_methods = dex_cache->NumResolvedMethods();
2254 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002255 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002256 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002257 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002258 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002259 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2260 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002261 size_t run = 0;
2262 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002263 j != length &&
2264 elem == mirror::DexCache::GetNativePairPtrSize(
2265 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002266 ++j) {
2267 ++run;
2268 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002269 if (run == 0) {
2270 os << StringPrintf("%zd: ", i);
2271 } else {
2272 os << StringPrintf("%zd to %zd: ", i, i + run);
2273 i = i + run;
2274 }
2275 std::string msg;
2276 if (elem == nullptr) {
2277 msg = "null";
2278 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002279 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002280 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002281 } else {
2282 msg = "<not in method section>";
2283 }
2284 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002285 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002286 }
2287 size_t num_fields = dex_cache->NumResolvedFields();
2288 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002289 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002290 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002291 auto* resolved_fields = dex_cache->GetResolvedFields();
2292 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002293 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002294 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002295 size_t run = 0;
2296 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002297 j != length &&
2298 elem == mirror::DexCache::GetNativePairPtrSize(
2299 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002300 ++j) {
2301 ++run;
2302 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002303 if (run == 0) {
2304 os << StringPrintf("%zd: ", i);
2305 } else {
2306 os << StringPrintf("%zd to %zd: ", i, i + run);
2307 i = i + run;
2308 }
2309 std::string msg;
2310 if (elem == nullptr) {
2311 msg = "null";
2312 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002313 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002314 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002315 } else {
2316 msg = "<not in field section>";
2317 }
2318 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002320 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002321 size_t num_types = dex_cache->NumResolvedTypes();
2322 if (num_types != 0u) {
2323 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002324 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002325 auto* resolved_types = dex_cache->GetResolvedTypes();
2326 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002327 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002328 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002329 for (size_t j = i + 1; j != num_types; ++j) {
2330 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2331 if (pair.index != other_pair.index ||
2332 pair.object.Read() != other_pair.object.Read()) {
2333 break;
2334 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002335 ++run;
2336 }
2337 if (run == 0) {
2338 os << StringPrintf("%zd: ", i);
2339 } else {
2340 os << StringPrintf("%zd to %zd: ", i, i + run);
2341 i = i + run;
2342 }
2343 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002344 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002345 if (elem == nullptr) {
2346 msg = "null";
2347 } else {
David Sehr709b0702016-10-13 09:12:37 -07002348 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002349 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002350 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002351 }
2352 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002353 }
2354 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002355 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002356 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002357 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002358
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002359 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002360 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361 DCHECK(method != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002362 const PointerSize pointer_size = image_header_.GetPointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002363 if (method->IsNative()) {
David Srbeckya54e0cc2019-04-15 15:03:26 +01002364 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002365 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002366 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2367 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002368 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002369 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002370 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002371 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2372 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002373 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002374 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002375 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002376 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002377 } else if (method->IsRuntimeMethod()) {
2378 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2379 if (table != nullptr) {
2380 indent_os << "IMT conflict table " << table << " method: ";
2381 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002382 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2383 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002384 }
2385 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002386 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002387 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002388 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002389 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390
David Srbeckya54e0cc2019-04-15 15:03:26 +01002391 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2392 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2393
Mathieu Chartiere401d142015-04-22 13:56:20 -07002394 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002395 size_t vmap_table_bytes = 0u;
David Srbeckya54e0cc2019-04-15 15:03:26 +01002396 if (quick_oat_code_begin != nullptr) {
2397 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2398 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2399 vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2400 &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002401 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002402 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002403 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002404 }
2405
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002406 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2407 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002408 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002409 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410 if (method->IsConstructor()) {
2411 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002412 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002413 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002414 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002415 }
2416 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002418 }
2419 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002420 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002421
Igor Murashkin7617abd2015-07-10 18:27:47 -07002422 uint32_t method_access_flags = method->GetAccessFlags();
2423
Mathieu Chartiere401d142015-04-22 13:56:20 -07002424 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002425 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2426 dex_instruction_bytes,
2427 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002428 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002429
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002430 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002431 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002432
2433 double expansion =
2434 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002435 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002436 }
2437 }
2438
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002439 std::set<const void*> already_seen_;
2440 // Compute the size of the given data within the oat file and whether this is the first time
2441 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002442 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002443 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002444 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002445 already_seen_.insert(oat_data);
2446 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002447 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002448 }
2449 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002450 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002451
2452 public:
2453 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002454 size_t oat_file_bytes = 0u;
2455 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002456
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002457 size_t header_bytes = 0u;
2458 size_t object_bytes = 0u;
2459 size_t art_field_bytes = 0u;
2460 size_t art_method_bytes = 0u;
2461 size_t dex_cache_arrays_bytes = 0u;
2462 size_t interned_strings_bytes = 0u;
2463 size_t class_table_bytes = 0u;
2464 size_t sro_offset_bytes = 0u;
2465 size_t metadata_bytes = 0u;
2466 size_t bitmap_bytes = 0u;
2467 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002468
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002469 size_t managed_code_bytes = 0u;
2470 size_t managed_code_bytes_ignoring_deduplication = 0u;
2471 size_t native_to_managed_code_bytes = 0u;
2472 size_t class_initializer_code_bytes = 0u;
2473 size_t large_initializer_code_bytes = 0u;
2474 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002476 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002477
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002478 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002479
Mathieu Chartiere401d142015-04-22 13:56:20 -07002480 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002481 std::vector<size_t> method_outlier_size;
2482 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002483 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002484
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002485 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002486
Elliott Hughesa0e18062012-04-13 15:59:59 -07002487 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002488 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002489 size_t bytes;
2490 size_t count;
2491 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002492 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002493 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002494
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002495 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002496 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2497 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002498 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002499 it->second.count += 1;
2500 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002501 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002502 }
2503 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002504
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002505 double PercentOfOatBytes(size_t size) {
2506 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2507 }
2508
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002509 double PercentOfFileBytes(size_t size) {
2510 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2511 }
2512
2513 double PercentOfObjectBytes(size_t size) {
2514 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2515 }
2516
Mathieu Chartiere401d142015-04-22 13:56:20 -07002517 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002518 method_outlier_size.push_back(total_size);
2519 method_outlier_expansion.push_back(expansion);
2520 method_outlier.push_back(method);
2521 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002522
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002523 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002524 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002525 size_t sum_of_sizes = 0;
2526 size_t sum_of_sizes_squared = 0;
2527 size_t sum_of_expansion = 0;
2528 size_t sum_of_expansion_squared = 0;
2529 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002530 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002531 return;
2532 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002533 for (size_t i = 0; i < n; i++) {
2534 size_t cur_size = method_outlier_size[i];
2535 sum_of_sizes += cur_size;
2536 sum_of_sizes_squared += cur_size * cur_size;
2537 double cur_expansion = method_outlier_expansion[i];
2538 sum_of_expansion += cur_expansion;
2539 sum_of_expansion_squared += cur_expansion * cur_expansion;
2540 }
2541 size_t size_mean = sum_of_sizes / n;
2542 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2543 double expansion_mean = sum_of_expansion / n;
2544 double expansion_variance =
2545 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2546
2547 // Dump methods whose size is a certain number of standard deviations from the mean
2548 size_t dumped_values = 0;
2549 size_t skipped_values = 0;
2550 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2551 size_t cur_size_variance = i * i * size_variance;
2552 bool first = true;
2553 for (size_t j = 0; j < n; j++) {
2554 size_t cur_size = method_outlier_size[j];
2555 if (cur_size > size_mean) {
2556 size_t cur_var = cur_size - size_mean;
2557 cur_var = cur_var * cur_var;
2558 if (cur_var > cur_size_variance) {
2559 if (dumped_values > 20) {
2560 if (i == 1) {
2561 skipped_values++;
2562 } else {
2563 i = 2; // jump to counting for 1 standard deviation
2564 break;
2565 }
2566 } else {
2567 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002568 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002569 first = false;
2570 }
David Sehr709b0702016-10-13 09:12:37 -07002571 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002572 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002573 method_outlier_size[j] = 0; // don't consider this method again
2574 dumped_values++;
2575 }
2576 }
2577 }
2578 }
2579 }
2580 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002581 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002582 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002583 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002584 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002585
2586 // Dump methods whose expansion is a certain number of standard deviations from the mean
2587 dumped_values = 0;
2588 skipped_values = 0;
2589 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2590 double cur_expansion_variance = i * i * expansion_variance;
2591 bool first = true;
2592 for (size_t j = 0; j < n; j++) {
2593 double cur_expansion = method_outlier_expansion[j];
2594 if (cur_expansion > expansion_mean) {
2595 size_t cur_var = cur_expansion - expansion_mean;
2596 cur_var = cur_var * cur_var;
2597 if (cur_var > cur_expansion_variance) {
2598 if (dumped_values > 20) {
2599 if (i == 1) {
2600 skipped_values++;
2601 } else {
2602 i = 2; // jump to counting for 1 standard deviation
2603 break;
2604 }
2605 } else {
2606 if (first) {
2607 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002608 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002609 first = false;
2610 }
David Sehr709b0702016-10-13 09:12:37 -07002611 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002612 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002613 method_outlier_expansion[j] = 0.0; // don't consider this method again
2614 dumped_values++;
2615 }
2616 }
2617 }
2618 }
2619 }
2620 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002621 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002622 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002623 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002624 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002625 }
2626
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002627 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002628 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002629 {
2630 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2631 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002632 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2633 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2634 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2635 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2636 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2637 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002638 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002639 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2640 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002641 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2642 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002643 header_bytes, PercentOfFileBytes(header_bytes),
2644 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002645 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2646 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002647 dex_cache_arrays_bytes,
2648 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002649 interned_strings_bytes,
2650 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002651 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002652 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2653 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002654 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2656 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002657 CHECK_EQ(file_bytes,
2658 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2659 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002660 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002661 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002662
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002663 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002664 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002665 for (const auto& sizes_and_count : sizes_and_counts) {
2666 const std::string& descriptor(sizes_and_count.first);
2667 double average = static_cast<double>(sizes_and_count.second.bytes) /
2668 static_cast<double>(sizes_and_count.second.count);
2669 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002671 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002672 descriptor.c_str(), sizes_and_count.second.bytes,
2673 sizes_and_count.second.count, average, percent);
2674 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002675 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002676 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002677 CHECK_EQ(object_bytes, object_bytes_total);
2678
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002679 os << StringPrintf("oat_file_bytes = %8zd\n"
2680 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002681 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2682 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2683 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2684 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002685 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002686 managed_code_bytes,
2687 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002688 native_to_managed_code_bytes,
2689 PercentOfOatBytes(native_to_managed_code_bytes),
2690 class_initializer_code_bytes,
2691 PercentOfOatBytes(class_initializer_code_bytes),
2692 large_initializer_code_bytes,
2693 PercentOfOatBytes(large_initializer_code_bytes),
2694 large_method_code_bytes,
2695 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002697 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002699 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2700 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002701 }
2702
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002703 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002704 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002705 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002706
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002707 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2708 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002709 static_cast<double>(managed_code_bytes) /
2710 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002711 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002712 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002713 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002714
2715 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002716 }
2717 } stats_;
2718
2719 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002720 enum {
2721 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2722 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2723 kLargeConstructorDexBytes = 4000,
2724 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2725 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2726 kLargeMethodDexBytes = 16000
2727 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002728
2729 // For performance, use the *os_ directly for anything that doesn't need indentation
2730 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002731 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002732 VariableIndentationOutputStream vios_;
2733 ScopedIndentation indent1_;
2734
Ian Rogers1d54e732013-05-02 21:10:01 -07002735 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002736 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002737 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002738 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002739 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002740
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002741 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002742};
2743
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002744static int DumpImage(gc::space::ImageSpace* image_space,
2745 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002746 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002747 const ImageHeader& image_header = image_space->GetImageHeader();
2748 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002749 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002750 return EXIT_FAILURE;
2751 }
2752 ImageDumper image_dumper(os, *image_space, image_header, options);
2753 if (!image_dumper.Dump()) {
2754 return EXIT_FAILURE;
2755 }
2756 return EXIT_SUCCESS;
2757}
2758
2759static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002760 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002761 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002762 options->class_loader_ = &null_class_loader;
2763
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002764 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002765 if (options->app_image_ != nullptr) {
2766 if (options->app_oat_ == nullptr) {
2767 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002768 return EXIT_FAILURE;
2769 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002770 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2771 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2772 // pointers into 32 bit pointer sized ArtMethods.
2773 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002774 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002775 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002776 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002777 /*executable=*/ false,
2778 /*low_4gb=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002779 &error_msg));
2780 if (oat_file == nullptr) {
2781 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002782 return EXIT_FAILURE;
2783 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002784 std::unique_ptr<gc::space::ImageSpace> space(
2785 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2786 if (space == nullptr) {
2787 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2788 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002789 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002790 }
2791 // Open dex files for the image.
2792 std::vector<std::unique_ptr<const DexFile>> dex_files;
2793 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2794 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2795 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002796 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002797 }
2798 // Dump the actual image.
2799 int result = DumpImage(space.get(), options, os);
2800 if (result != EXIT_SUCCESS) {
2801 return result;
2802 }
2803 // Fall through to dump the boot images.
2804 }
2805
2806 gc::Heap* heap = runtime->GetHeap();
Vladimir Markodd09f712019-12-06 12:37:10 +00002807 if (!heap->HasBootImageSpace()) {
2808 LOG(ERROR) << "No image spaces";
2809 return EXIT_FAILURE;
2810 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002811 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2812 int result = DumpImage(image_space, options, os);
2813 if (result != EXIT_SUCCESS) {
2814 return result;
2815 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002816 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002817 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002818}
2819
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002820static jobject InstallOatFile(Runtime* runtime,
2821 std::unique_ptr<OatFile> oat_file,
2822 std::vector<const DexFile*>* class_path)
2823 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002824 Thread* self = Thread::Current();
2825 CHECK(self != nullptr);
2826 // Need well-known-classes.
2827 WellKnownClasses::Init(self->GetJniEnv());
2828
Vladimir Marko421087b2018-02-27 11:00:17 +00002829 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002830 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002831 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002832 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002833 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002834 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002835 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002836 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002837 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002838 }
2839
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002840 // Need a class loader. Fake that we're a compiler.
2841 // Note: this will run initializers through the unstarted runtime, so make sure it's
2842 // initialized.
2843 interpreter::UnstartedRuntime::Initialize();
2844
2845 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2846
Vladimir Marko421087b2018-02-27 11:00:17 +00002847 // Need to register dex files to get a working dex cache.
2848 for (const DexFile* dex_file : *class_path) {
2849 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2850 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2851 CHECK(dex_cache != nullptr);
2852 }
2853
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002854 return class_loader;
2855}
2856
2857static int DumpOatWithRuntime(Runtime* runtime,
2858 std::unique_ptr<OatFile> oat_file,
2859 OatDumperOptions* options,
2860 std::ostream* os) {
2861 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2862 ScopedObjectAccess soa(Thread::Current());
2863
2864 OatFile* oat_file_ptr = oat_file.get();
2865 std::vector<const DexFile*> class_path;
2866 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002867
2868 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002869 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002870 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002871 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002872 options->class_loader_ = &loader_handle;
2873
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002874 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002875 bool success = oat_dumper.Dump(*os);
2876 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2877}
2878
2879static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002880 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002881 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002882 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002883 options->class_loader_ = &null_class_loader;
2884
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002885 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002886 bool success = oat_dumper.Dump(*os);
2887 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2888}
2889
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002890static int DumpOat(Runtime* runtime,
2891 const char* oat_filename,
2892 const char* dex_filename,
2893 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002894 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002895 if (dex_filename == nullptr) {
2896 LOG(WARNING) << "No dex filename provided, "
2897 << "oatdump might fail if the oat file does not contain the dex code.";
2898 }
Vladimir Markob7bf8432019-12-03 13:18:50 +00002899 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2900 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2901 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002902 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002903 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002904 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002905 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002906 /*executable=*/ false,
2907 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002908 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002909 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002910 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002911 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002912 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002913 return EXIT_FAILURE;
2914 }
2915
2916 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002917 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002918 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002919 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002920 }
2921}
2922
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002923static int SymbolizeOat(const char* oat_filename,
2924 const char* dex_filename,
2925 std::string& output_name,
2926 bool no_bits) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002927 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2928 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2929 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002930 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002931 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002932 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002933 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002934 /*executable=*/ false,
2935 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002936 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002937 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002938 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002939 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002940 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002941 return EXIT_FAILURE;
2942 }
2943
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002944 bool result;
2945 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2946 // files for 64-bit code in the past.
2947 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002948 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002949 result = oat_symbolizer.Symbolize();
2950 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002951 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002952 result = oat_symbolizer.Symbolize();
2953 }
2954 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002955 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002956 return EXIT_FAILURE;
2957 }
2958
2959 return EXIT_SUCCESS;
2960}
2961
Andreas Gampe9fded872016-09-25 16:08:35 -07002962class IMTDumper {
2963 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002964 static bool Dump(Runtime* runtime,
2965 const std::string& imt_file,
2966 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002967 const char* oat_filename,
2968 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002969 Thread* self = Thread::Current();
2970
2971 ScopedObjectAccess soa(self);
2972 StackHandleScope<1> scope(self);
2973 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2974 std::vector<const DexFile*> class_path;
2975
2976 if (oat_filename != nullptr) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002977 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2978 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2979 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002980 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002981 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002982 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002983 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002984 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002985 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002986 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002987 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002988 &error_msg));
2989 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002990 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002991 return false;
2992 }
2993
2994 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2995 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2996 } else {
2997 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2998 class_path = runtime->GetClassLinker()->GetBootClassPath();
2999 }
3000
3001 if (!imt_file.empty()) {
3002 return DumpImt(runtime, imt_file, class_loader);
3003 }
3004
3005 if (dump_imt_stats) {
3006 return DumpImtStats(runtime, class_path, class_loader);
3007 }
3008
3009 LOG(FATAL) << "Should not reach here";
3010 UNREACHABLE();
3011 }
3012
3013 private:
3014 static bool DumpImt(Runtime* runtime,
3015 const std::string& imt_file,
3016 Handle<mirror::ClassLoader> h_class_loader)
3017 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003018 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3019 std::unordered_set<std::string> prepared;
3020
3021 for (const std::string& line : lines) {
3022 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3023 // or a class descriptor and an interface method, in which case we will lookup the method,
3024 // determine its IMT slot, and check the class' IMT.
3025 size_t first_space = line.find(' ');
3026 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003027 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003028 } else {
3029 DumpIMTForMethod(runtime,
3030 line.substr(0, first_space),
3031 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003032 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003033 &prepared);
3034 }
3035 std::cerr << std::endl;
3036 }
3037
3038 return true;
3039 }
3040
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003041 static bool DumpImtStats(Runtime* runtime,
3042 const std::vector<const DexFile*>& dex_files,
3043 Handle<mirror::ClassLoader> h_class_loader)
3044 REQUIRES_SHARED(Locks::mutator_lock_) {
3045 size_t without_imt = 0;
3046 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003047 std::map<size_t, size_t> histogram;
3048
3049 ClassLinker* class_linker = runtime->GetClassLinker();
3050 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3051 std::unordered_set<std::string> prepared;
3052
3053 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003054 StackHandleScope<1> scope(self);
3055 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3056
3057 for (const DexFile* dex_file : dex_files) {
3058 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003059 class_def_index != dex_file->NumClassDefs();
3060 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003061 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07003062 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003063 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003064 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003065 std::cerr << "Warning: could not load " << descriptor << std::endl;
3066 continue;
3067 }
3068
3069 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003070 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003071 continue;
3072 }
3073
3074 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3075 if (im_table == nullptr) {
3076 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003077 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003078 continue;
3079 }
3080
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003081 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003082 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3083 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3084 if (ptr->IsRuntimeMethod()) {
3085 if (ptr->IsImtUnimplementedMethod()) {
3086 histogram[0]++;
3087 } else {
3088 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3089 histogram[current_table->NumEntries(pointer_size)]++;
3090 }
3091 } else {
3092 histogram[1]++;
3093 }
3094 }
3095 }
3096 }
3097
3098 std::cerr << "IMT stats:"
3099 << std::endl << std::endl;
3100
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003101 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003102 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003103 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003104 << std::endl << std::endl;
3105
3106 double sum_one = 0;
3107 size_t count_one = 0;
3108
3109 std::cerr << " " << "IMT histogram" << std::endl;
3110 for (auto& bucket : histogram) {
3111 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3112 if (bucket.first > 0) {
3113 sum_one += bucket.second * bucket.first;
3114 count_one += bucket.second;
3115 }
3116 }
3117
3118 double count_zero = count_one + histogram[0];
3119 std::cerr << " Stats:" << std::endl;
3120 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3121 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3122
3123 return true;
3124 }
3125
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003126 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003127 static bool HasNoIMT(Runtime* runtime,
3128 Handle<mirror::Class> klass,
3129 const PointerSize pointer_size,
3130 std::unordered_set<std::string>* prepared)
3131 REQUIRES_SHARED(Locks::mutator_lock_) {
3132 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3133 return true;
3134 }
3135
3136 if (klass->GetImt(pointer_size) == nullptr) {
3137 PrepareClass(runtime, klass, prepared);
3138 }
3139
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003140 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003141 DCHECK(object_class->IsObjectClass());
3142
3143 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3144
Mathieu Chartier6beced42016-11-15 15:51:31 -08003145 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003146 DCHECK(result);
3147 }
3148
3149 return result;
3150 }
3151
3152 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3153 REQUIRES_SHARED(Locks::mutator_lock_) {
3154 if (table == nullptr) {
3155 std::cerr << " <No IMT?>" << std::endl;
3156 return;
3157 }
3158 size_t table_index = 0;
3159 for (;;) {
3160 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3161 if (ptr == nullptr) {
3162 return;
3163 }
3164 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003165 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003166 }
3167 }
3168
3169 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3170 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003171 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003172 const std::string& class_name,
3173 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003174 /*out*/ ObjPtr<mirror::Class>* klass_out,
3175 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003176 REQUIRES_SHARED(Locks::mutator_lock_) {
3177 if (class_name.empty()) {
3178 return nullptr;
3179 }
3180
3181 std::string descriptor;
3182 if (class_name[0] == 'L') {
3183 descriptor = class_name;
3184 } else {
3185 descriptor = DotToDescriptor(class_name.c_str());
3186 }
3187
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003188 ObjPtr<mirror::Class> klass =
3189 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003190
3191 if (klass == nullptr) {
3192 self->ClearException();
3193 std::cerr << "Did not find " << class_name << std::endl;
3194 *klass_out = nullptr;
3195 return nullptr;
3196 }
3197
3198 StackHandleScope<1> scope(Thread::Current());
3199 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3200
3201 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3202 *klass_out = h_klass.Get();
3203 return ret;
3204 }
3205
3206 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3207 Handle<mirror::Class> h_klass,
3208 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003209 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003210 REQUIRES_SHARED(Locks::mutator_lock_) {
3211 PrepareClass(runtime, h_klass, prepared);
3212 return h_klass->GetImt(pointer_size);
3213 }
3214
3215 static void DumpIMTForClass(Runtime* runtime,
3216 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003217 Handle<mirror::ClassLoader> h_loader,
3218 std::unordered_set<std::string>* prepared)
3219 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003220 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003221 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003222 ImTable* imt = PrepareAndGetImTable(runtime,
3223 Thread::Current(),
3224 h_loader,
3225 class_name,
3226 pointer_size,
3227 &klass,
3228 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003229 if (imt == nullptr) {
3230 return;
3231 }
3232
3233 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3234 for (size_t index = 0; index < ImTable::kSize; ++index) {
3235 std::cerr << " " << index << ":" << std::endl;
3236 ArtMethod* ptr = imt->Get(index, pointer_size);
3237 if (ptr->IsRuntimeMethod()) {
3238 if (ptr->IsImtUnimplementedMethod()) {
3239 std::cerr << " <empty>" << std::endl;
3240 } else {
3241 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3242 PrintTable(current_table, pointer_size);
3243 }
3244 } else {
David Sehr709b0702016-10-13 09:12:37 -07003245 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003246 }
3247 }
3248
3249 std::cerr << " Interfaces:" << std::endl;
3250 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003251 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003252 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003253 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003254 std::string iface_name;
3255 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003256
Mathieu Chartier6beced42016-11-15 15:51:31 -08003257 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3258 uint32_t class_hash, name_hash, signature_hash;
3259 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3260 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3261 std::cerr << " " << iface_method.PrettyMethod(true)
3262 << " slot=" << imt_slot
3263 << std::hex
3264 << " class_hash=0x" << class_hash
3265 << " name_hash=0x" << name_hash
3266 << " signature_hash=0x" << signature_hash
3267 << std::dec
3268 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003269 }
3270 }
3271 }
3272
3273 static void DumpIMTForMethod(Runtime* runtime,
3274 const std::string& class_name,
3275 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003276 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003277 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003278 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003279 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003280 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003281 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003282 Thread::Current(),
3283 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003284 class_name,
3285 pointer_size,
3286 &klass,
3287 prepared);
3288 if (imt == nullptr) {
3289 return;
3290 }
3291
3292 std::cerr << class_name << " <" << method << ">" << std::endl;
3293 for (size_t index = 0; index < ImTable::kSize; ++index) {
3294 ArtMethod* ptr = imt->Get(index, pointer_size);
3295 if (ptr->IsRuntimeMethod()) {
3296 if (ptr->IsImtUnimplementedMethod()) {
3297 continue;
3298 }
3299
3300 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3301 if (current_table == nullptr) {
3302 continue;
3303 }
3304
3305 size_t table_index = 0;
3306 for (;;) {
3307 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3308 if (ptr2 == nullptr) {
3309 break;
3310 }
3311 table_index++;
3312
David Sehr709b0702016-10-13 09:12:37 -07003313 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003314 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003315 std::cerr << " Slot "
3316 << index
3317 << " ("
3318 << current_table->NumEntries(pointer_size)
3319 << ")"
3320 << std::endl;
3321 PrintTable(current_table, pointer_size);
3322 return;
3323 }
3324 }
3325 } else {
David Sehr709b0702016-10-13 09:12:37 -07003326 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003327 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003328 std::cerr << " Slot " << index << " (1)" << std::endl;
3329 std::cerr << " " << p_name << std::endl;
3330 } else {
3331 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003332 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003333 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003334 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003335 size_t num_methods = iface->NumDeclaredVirtualMethods();
3336 if (num_methods > 0) {
3337 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3338 if (ImTable::GetImtIndex(&iface_method) == index) {
3339 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003340 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003341 std::cerr << " Slot " << index << " (1)" << std::endl;
3342 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003343 }
3344 }
3345 }
3346 }
3347 }
3348 }
3349 }
3350 }
3351 }
3352
3353 // Read lines from the given stream, dropping comments and empty lines
3354 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3355 std::vector<std::string> output;
3356 while (in_stream.good()) {
3357 std::string dot;
3358 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003359 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003360 continue;
3361 }
3362 output.push_back(dot);
3363 }
3364 return output;
3365 }
3366
3367 // Read lines from the given file, dropping comments and empty lines.
3368 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3369 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3370 if (input_file.get() == nullptr) {
3371 LOG(ERROR) << "Failed to open input file " << input_filename;
3372 return std::vector<std::string>();
3373 }
3374 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3375 input_file->close();
3376 return result;
3377 }
3378
3379 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3380 // and note in the given set that the work was done.
3381 static void PrepareClass(Runtime* runtime,
3382 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003383 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003384 REQUIRES_SHARED(Locks::mutator_lock_) {
3385 if (!h_klass->ShouldHaveImt()) {
3386 return;
3387 }
3388
3389 std::string name;
3390 name = h_klass->GetDescriptor(&name);
3391
3392 if (done->find(name) != done->end()) {
3393 return;
3394 }
3395 done->insert(name);
3396
3397 if (h_klass->HasSuperClass()) {
3398 StackHandleScope<1> h(Thread::Current());
3399 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3400 }
3401
3402 if (!h_klass->IsTemp()) {
3403 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3404 }
3405 }
3406};
3407
Igor Murashkin37743352014-11-13 14:38:00 -08003408struct OatdumpArgs : public CmdlineArgs {
3409 protected:
3410 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003411
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003412 ParseStatus ParseCustom(const char* raw_option,
3413 size_t raw_option_length,
3414 std::string* error_msg) override {
3415 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003416 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003417 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003418 if (base_parse != kParseUnknownArgument) {
3419 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003420 }
3421 }
3422
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003423 std::string_view option(raw_option, raw_option_length);
3424 if (StartsWith(option, "--oat-file=")) {
3425 oat_filename_ = raw_option + strlen("--oat-file=");
3426 } else if (StartsWith(option, "--dex-file=")) {
3427 dex_filename_ = raw_option + strlen("--dex-file=");
3428 } else if (StartsWith(option, "--image=")) {
3429 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003430 } else if (option == "--no-dump:vmap") {
3431 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003432 } else if (option =="--dump:code_info_stack_maps") {
3433 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003434 } else if (option == "--no-disassemble") {
3435 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003436 } else if (option =="--header-only") {
3437 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003438 } else if (StartsWith(option, "--symbolize=")) {
3439 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003440 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003441 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003442 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003443 } else if (StartsWith(option, "--class-filter=")) {
3444 class_filter_ = raw_option + strlen("--class-filter=");
3445 } else if (StartsWith(option, "--method-filter=")) {
3446 method_filter_ = raw_option + strlen("--method-filter=");
3447 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003448 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003449 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003450 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003451 } else if (StartsWith(option, "--export-dex-to=")) {
3452 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3453 } else if (StartsWith(option, "--addr2instr=")) {
3454 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003455 *error_msg = "Address conversion failed";
3456 return kParseError;
3457 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003458 } else if (StartsWith(option, "--app-image=")) {
3459 app_image_ = raw_option + strlen("--app-image=");
3460 } else if (StartsWith(option, "--app-oat=")) {
3461 app_oat_ = raw_option + strlen("--app-oat=");
3462 } else if (StartsWith(option, "--dump-imt=")) {
3463 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003464 } else if (option == "--dump-imt-stats") {
3465 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003466 } else {
3467 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003468 }
3469
Igor Murashkin37743352014-11-13 14:38:00 -08003470 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003471 }
3472
Roland Levillainf73caca2018-08-24 17:19:07 +01003473 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003474 // Infer boot image location from the image location if possible.
3475 if (boot_image_location_ == nullptr) {
3476 boot_image_location_ = image_location_;
3477 }
3478
3479 // Perform the parent checks.
3480 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3481 if (parent_checks != kParseOk) {
3482 return parent_checks;
3483 }
3484
3485 // Perform our own checks.
3486 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3487 *error_msg = "Either --image or --oat-file must be specified";
3488 return kParseError;
3489 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3490 *error_msg = "Either --image or --oat-file must be specified but not both";
3491 return kParseError;
3492 }
3493
3494 return kParseOk;
3495 }
3496
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003497 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003498 std::string usage;
3499
3500 usage +=
3501 "Usage: oatdump [options] ...\n"
3502 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3503 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3504 "\n"
3505 // Either oat-file or image is required.
3506 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Mathieu Chartier2afa19d2019-09-10 16:07:21 -07003507 " Example: --oat-file=/system/framework/arm64/boot.oat\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003508 "\n"
3509 " --image=<file.art>: specifies an input image location.\n"
3510 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003511 "\n"
3512 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003513 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003514 " Example: --app-image=app.art\n"
3515 "\n"
3516 " --app-oat=<file.odex>: specifies an input app oat.\n"
3517 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003518 "\n";
3519
3520 usage += Base::GetUsage();
3521
3522 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003523 " --no-dump:vmap may be used to disable vmap dumping.\n"
3524 " Example: --no-dump:vmap\n"
3525 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003526 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3527 " Example: --dump:code_info_stack_maps\n"
3528 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003529 " --no-disassemble may be used to disable disassembly.\n"
3530 " Example: --no-disassemble\n"
3531 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003532 " --header-only may be used to print only the oat header.\n"
3533 " Example: --header-only\n"
3534 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003535 " --list-classes may be used to list target file classes (can be used with filters).\n"
3536 " Example: --list-classes\n"
3537 " Example: --list-classes --class-filter=com.example.foo\n"
3538 "\n"
3539 " --list-methods may be used to list target file methods (can be used with filters).\n"
3540 " Example: --list-methods\n"
3541 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3542 "\n"
3543 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3544 " Example: --symbolize=/system/framework/boot.oat\n"
3545 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003546 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3547 " .rodata and .text sections are omitted in the output file to save space.\n"
3548 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3549 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003550 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3551 " Example: --class-filter=com.example.foo\n"
3552 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003553 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3554 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003555 "\n"
3556 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3557 " Example: --export-dex-to=/data/local/tmp\n"
3558 "\n"
3559 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3560 " address (e.g. PC from crash dump)\n"
3561 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003562 "\n"
3563 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3564 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003565 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003566 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003567 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003568 " Example: --dump-imt=imt.txt\n"
3569 "\n"
3570 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3571 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003572 "\n";
3573
3574 return usage;
3575 }
3576
3577 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003578 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003579 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003580 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003581 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003582 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003583 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003584 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003585 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003586 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003587 bool disassemble_code_ = true;
3588 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003589 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003590 bool list_classes_ = false;
3591 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003592 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003593 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003594 uint32_t addr2instr_ = 0;
3595 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003596 const char* app_image_ = nullptr;
3597 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003598};
3599
Igor Murashkin37743352014-11-13 14:38:00 -08003600struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003601 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003602 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003603
Igor Murashkin37743352014-11-13 14:38:00 -08003604 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3605 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3606
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003607 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003608 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003609 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003610 args_->disassemble_code_,
3611 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003612 args_->class_filter_,
3613 args_->method_filter_,
3614 args_->list_classes_,
3615 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003616 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003617 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003618 args_->app_image_,
3619 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003620 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003621
Andreas Gampe9fded872016-09-25 16:08:35 -07003622 return (args_->boot_image_location_ != nullptr ||
3623 args_->image_location_ != nullptr ||
3624 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003625 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003626 }
3627
Roland Levillainf73caca2018-08-24 17:19:07 +01003628 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003629 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003630 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003631
Mathieu Chartierd424d082014-10-15 10:31:46 -07003632 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003633
Andreas Gampec24f3992014-12-17 20:40:11 -08003634 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003635 // ELF has special kind of section called SHT_NOBITS which allows us to create
3636 // sections which exist but their data is omitted from the ELF file to save space.
3637 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3638 // with only debug data. We use it in similar way to exclude .rodata and .text.
3639 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003640 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3641 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003642 } else {
3643 return DumpOat(nullptr,
3644 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003645 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003646 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003647 args_->os_) == EXIT_SUCCESS;
3648 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003649 }
3650
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003651 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003652 CHECK(args_ != nullptr);
3653
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003654 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3655 return IMTDumper::Dump(runtime,
3656 args_->imt_dump_,
3657 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003658 args_->oat_filename_,
3659 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003660 }
3661
Igor Murashkin37743352014-11-13 14:38:00 -08003662 if (args_->oat_filename_ != nullptr) {
3663 return DumpOat(runtime,
3664 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003665 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003666 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003667 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003668 }
Igor Murashkin37743352014-11-13 14:38:00 -08003669
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003670 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003671 }
3672
Igor Murashkin37743352014-11-13 14:38:00 -08003673 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3674};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003675
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003676} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003677
3678int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003679 // Output all logging to stderr.
3680 android::base::SetLogger(android::base::StderrLogger);
3681
Igor Murashkin37743352014-11-13 14:38:00 -08003682 art::OatdumpMain main;
3683 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003684}