blob: 5b7535d010f8091de0e56dd960d516b1095e1d97 [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",
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100113 "kOomeWhenThrowingException",
114 "kOomeWhenThrowingOome",
115 "kOomeWhenHandlingStackOverflow",
116 "kNoClassDefFoundError",
Alex Light885f0c12019-01-09 13:48:55 -0800117 "kSpecialRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700118};
119
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700121static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700122
Andreas Gampeb40d3612018-06-26 15:49:42 -0700123const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700124 DCHECK(oat_dex_file != nullptr);
125 auto it = opened_dex_files.find(oat_dex_file);
126 if (it != opened_dex_files.end()) {
127 return it->second.get();
128 }
129 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
130 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
131 return ret;
132}
133
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800134template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100135class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700136 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000137 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
138 oat_file_(oat_file),
139 builder_(nullptr),
140 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
141 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700142 }
143
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700144 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800146 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000147 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
Andreas Gampe08c277c2017-04-26 22:22:15 -0700149 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
150 if (elf_file == nullptr) {
151 return false;
152 }
David Srbecky2faab002019-02-12 16:35:48 +0000153 std::unique_ptr<BufferedOutputStream> output_stream =
154 std::make_unique<BufferedOutputStream>(
155 std::make_unique<FileOutputStream>(elf_file.get()));
156 builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000157
158 builder_->Start();
159
160 auto* rodata = builder_->GetRoData();
161 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163 const uint8_t* rodata_begin = oat_file_->Begin();
164 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000165 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000166 rodata->Start();
167 rodata->WriteFully(rodata_begin, rodata_size);
168 rodata->End();
169 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170
David Srbecky6d8c8f02015-10-26 10:57:09 +0000171 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
172 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000173 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000174 text->Start();
175 text->WriteFully(text_begin, text_size);
176 text->End();
177 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000178
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179 builder_->PrepareDynamicSection(elf_file->GetPath(),
180 rodata_size,
181 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000182 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000183 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100184 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000185 oat_file_->BssRootsOffset(),
186 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000187 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700188
Vladimir Marko1b404a82017-09-01 13:35:26 +0100189 const OatHeader& oat_header = oat_file_->GetOatHeader();
190 #define DO_TRAMPOLINE(fn_name) \
191 if (oat_header.Get ## fn_name ## Offset() != 0) { \
192 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000193 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100194 info.isa = oat_header.GetInstructionSet(); \
195 info.is_code_address_text_relative = true; \
196 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
197 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
198 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
199 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
200 method_debug_infos_.push_back(std::move(info)); \
201 }
Vladimir Marko1b404a82017-09-01 13:35:26 +0100202 DO_TRAMPOLINE(JniDlsymLookup);
203 DO_TRAMPOLINE(QuickGenericJniTrampoline);
204 DO_TRAMPOLINE(QuickImtConflictTrampoline);
205 DO_TRAMPOLINE(QuickResolutionTrampoline);
206 DO_TRAMPOLINE(QuickToInterpreterBridge);
207 #undef DO_TRAMPOLINE
208
David Srbecky5d950762016-03-07 20:47:29 +0000209 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100210
211 // TODO: Try to symbolize link-time thunks?
212 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700213
David Srbecky32210b92017-12-04 14:39:21 +0000214 // TODO: Add symbols for dex bytecode in the .dex section.
215
216 debug::DebugInfo debug_info{};
217 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
218
David Srbecky7370d922019-02-12 14:00:30 +0000219 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700220
David Srbecky6d8c8f02015-10-26 10:57:09 +0000221 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700222
Andreas Gampe08c277c2017-04-26 22:22:15 -0700223 bool ret_value = builder_->Good();
224
225 builder_.reset();
226 output_stream.reset();
227
228 if (elf_file->FlushCloseOrErase() != 0) {
229 return false;
230 }
231 elf_file.reset();
232
233 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700234 }
235
David Srbecky5d950762016-03-07 20:47:29 +0000236 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700237 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700239 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700240 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000241 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700242 }
243 }
244
Andreas Gampeb40d3612018-06-26 15:49:42 -0700245 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700247 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
248 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700249 return;
250 }
251 for (size_t class_def_index = 0;
252 class_def_index < dex_file->NumClassDefs();
253 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
255 OatClassType type = oat_class.GetType();
256 switch (type) {
257 case kOatClassAllCompiled:
258 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000259 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700260 break;
261
262 case kOatClassNoneCompiled:
263 case kOatClassMax:
264 // Ignore.
265 break;
266 }
267 }
268 }
269
David Srbecky5d950762016-03-07 20:47:29 +0000270 void WalkOatClass(const OatFile::OatClass& oat_class,
271 const DexFile& dex_file,
272 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700273 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274 // Note: even if this is an interface or a native class, we still have to walk it, as there
275 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700276 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700277 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000278 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
279 dex_file,
280 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700281 method.GetIndex(),
282 method.GetCodeItem(),
283 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700284 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 }
286
David Srbecky5d950762016-03-07 20:47:29 +0000287 void WalkOatMethod(const OatFile::OatMethod& oat_method,
288 const DexFile& dex_file,
289 uint32_t class_def_index,
290 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800291 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000292 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700293 if ((method_access_flags & kAccAbstract) != 0) {
294 // Abstract method, no code.
295 return;
296 }
David Srbecky5d950762016-03-07 20:47:29 +0000297 const OatHeader& oat_header = oat_file_->GetOatHeader();
298 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
299 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700300 // No code.
301 return;
302 }
303
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100304 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
305 // Clear Thumb2 bit.
306 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
307
Vladimir Marko1b404a82017-09-01 13:35:26 +0100308 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000309 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000310 info.dex_file = &dex_file;
311 info.class_def_index = class_def_index;
312 info.dex_method_index = dex_method_index;
313 info.access_flags = method_access_flags;
314 info.code_item = code_item;
315 info.isa = oat_header.GetInstructionSet();
316 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
317 info.is_native_debuggable = oat_header.IsNativeDebuggable();
318 info.is_optimized = method_header->IsOptimized();
319 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100320 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000321 info.code_size = method_header->GetCodeSize();
322 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
323 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
324 info.cfi = ArrayRef<uint8_t>();
325 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 }
327
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700328 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700329 const OatFile* oat_file_;
David Srbecky2faab002019-02-12 16:35:48 +0000330 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000331 std::vector<debug::MethodDebugInfo> method_debug_infos_;
332 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700333 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000334 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700335};
336
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700337class OatDumperOptions {
338 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100339 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100340 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700342 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800343 const char* class_filter,
344 const char* method_filter,
345 bool list_classes,
346 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000347 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800349 const char* app_image,
350 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800351 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100352 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100353 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700355 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000357 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 list_classes_(list_classes),
359 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000360 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800361 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800362 app_image_(app_image),
363 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800364 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800365 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100368 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369 const bool disassemble_code_;
370 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800371 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000372 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 const bool list_classes_;
374 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000375 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800377 const char* const app_image_;
378 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800379 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700380 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381};
382
Elliott Hughese3c845c2012-02-28 17:23:01 -0800383class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700384 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100385 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000386 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800387 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800389 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800390 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
391 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700392 new DisassemblerOptions(
393 options_.absolute_addresses_,
394 oat_file.Begin(),
395 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700396 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700397 Is64BitInstructionSet(instruction_set_)
398 ? &Thread::DumpThreadOffset<PointerSize::k64>
399 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800400 CHECK(options_.class_loader_ != nullptr);
401 CHECK(options_.class_filter_ != nullptr);
402 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800403 AddAllOffsets();
404 }
405
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700406 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700407 delete disassembler_;
408 }
409
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800410 InstructionSet GetInstructionSet() {
411 return instruction_set_;
412 }
413
Andreas Gampec55bb392018-09-21 00:02:02 +0000414 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300415
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700416 bool Dump(std::ostream& os) {
417 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800418 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700419
420 os << "MAGIC:\n";
421 os << oat_header.GetMagic() << "\n\n";
422
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800423 os << "LOCATION:\n";
424 os << oat_file_.GetLocation() << "\n\n";
425
Brian Carlstromaded5f72011-10-07 17:15:04 -0700426 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800427 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700428
Elliott Hughesa72ec822012-03-05 17:12:22 -0800429 os << "INSTRUCTION SET:\n";
430 os << oat_header.GetInstructionSet() << "\n\n";
431
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700432 {
433 std::unique_ptr<const InstructionSetFeatures> features(
434 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
435 oat_header.GetInstructionSetFeaturesBitmap()));
436 os << "INSTRUCTION SET FEATURES:\n";
437 os << features->GetFeatureString() << "\n\n";
438 }
Dave Allison70202782013-10-22 17:52:19 -0700439
Brian Carlstromaded5f72011-10-07 17:15:04 -0700440 os << "DEX FILE COUNT:\n";
441 os << oat_header.GetDexFileCount() << "\n\n";
442
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800443#define DUMP_OAT_HEADER_OFFSET(label, offset) \
444 os << label " OFFSET:\n"; \
445 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800446 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800447 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
448 } \
449 os << StringPrintf("\n\n");
450
451 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800452 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
453 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800454 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
455 GetQuickGenericJniTrampolineOffset);
456 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
457 GetQuickImtConflictTrampolineOffset);
458 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
459 GetQuickResolutionTrampolineOffset);
460 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
461 GetQuickToInterpreterBridgeOffset);
462#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700463
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700464 // Print the key-value store.
465 {
466 os << "KEY VALUE STORE:\n";
467 size_t index = 0;
468 const char* key;
469 const char* value;
470 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
471 os << key << " = " << value << "\n";
472 index++;
473 }
474 os << "\n";
475 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700476
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800477 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700478 os << "BEGIN:\n";
479 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700481 os << "END:\n";
482 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
483 }
484
485 os << "SIZE:\n";
486 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700487
488 os << std::flush;
489
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800490 // If set, adjust relative address to be searched
491 if (options_.addr2instr_ != 0) {
492 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
493 os << "SEARCH ADDRESS (executable offset + input):\n";
494 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
495 }
496
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000497 // Dump .data.bimg.rel.ro entries.
498 DumpDataBimgRelRoEntries(os);
499
500 // Dump .bss summary, individual entries are dumped per dex file.
501 os << ".bss: ";
502 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
503 os << "empty.\n\n";
504 } else {
505 os << oat_file_.GetBssMethods().size() << " methods, ";
506 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
507 }
508
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700509 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700510 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700511 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700512 CHECK(oat_dex_file != nullptr);
513 std::string error_msg;
514 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
515 if (dex_file == nullptr) {
516 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
517 << error_msg;
518 continue;
519 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700520
Mathieu Chartier120aa282017-08-05 16:03:03 -0700521 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
522 if (layout_sections != nullptr) {
523 os << "Layout data\n";
524 os << *layout_sections;
525 os << "\n";
526 }
527
Vladimir Markobacb8e42018-06-13 14:45:30 +0100528 if (!options_.dump_header_only_) {
529 // Dump .bss entries.
530 DumpBssEntries(
531 os,
532 "ArtMethod",
533 oat_dex_file->GetMethodBssMapping(),
534 dex_file->NumMethodIds(),
535 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
536 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
537 DumpBssEntries(
538 os,
539 "Class",
540 oat_dex_file->GetTypeBssMapping(),
541 dex_file->NumTypeIds(),
542 sizeof(GcRoot<mirror::Class>),
543 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
544 DumpBssEntries(
545 os,
546 "String",
547 oat_dex_file->GetStringBssMapping(),
548 dex_file->NumStringIds(),
549 sizeof(GcRoot<mirror::Class>),
550 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
551 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700552 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700553
David Brazdilc03d7b62016-03-02 12:18:03 +0000554 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000555 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000556 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000557 if (vdex_header.IsValid()) {
558 std::string error_msg;
559 std::vector<const DexFile*> dex_files;
560 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
561 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
562 if (dex_file == nullptr) {
563 os << "Error opening dex file: " << error_msg << std::endl;
564 return false;
565 }
566 dex_files.push_back(dex_file);
567 }
568 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
569 deps.Dump(&vios);
570 } else {
571 os << "UNRECOGNIZED vdex file, magic "
572 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000573 << ", verifier deps version "
574 << vdex_header.GetVerifierDepsVersion()
575 << ", dex section version "
576 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000577 << "\n";
578 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000579 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700580 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000581 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300582 if (!DumpOatDexFile(os, *oat_dex_file)) {
583 success = false;
584 }
585 }
586 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800587
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300588 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000589 std::string error_msg;
590 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
591 if (!OS::FileExists(vdex_filename.c_str())) {
592 os << "File " << vdex_filename.c_str() << " does not exist\n";
593 return false;
594 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300595
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000596 DexFileUniqV vdex_dex_files;
597 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
598 &vdex_dex_files,
599 &error_msg);
600 if (vdex_file.get() == nullptr) {
601 os << "Failed to open vdex file: " << error_msg << "\n";
602 return false;
603 }
604 if (oat_dex_files_.size() != vdex_dex_files.size()) {
605 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
606 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
607 return false;
608 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300609
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000610 size_t i = 0;
611 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700612 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000613 CHECK(oat_dex_file != nullptr);
614 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300615
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300616 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
617 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
618 // the original input Dex file, the `update_checksum_` option is used to recompute the
619 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
620 // false), ExportDexFile() enforces a reproducible checksum verification.
621 if (vdex_dex_file->IsCompactDexFile()) {
622 Options options;
623 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
624 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700625 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300626 std::unique_ptr<art::DexContainer> dex_container;
627 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
628 vdex_dex_file.get(),
629 i,
630 &dex_container,
631 &error_msg);
632 if (!result) {
633 os << "DexLayout failed to process Dex file: " + error_msg;
634 success = false;
635 break;
636 }
637 DexContainer::Section* main_section = dex_container->GetMainSection();
638 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
639
640 const ArtDexFileLoader dex_file_loader;
641 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
642 main_section->Begin(),
643 main_section->Size(),
644 vdex_dex_file->GetLocation(),
645 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700646 /*oat_dex_file=*/ nullptr,
647 /*verify=*/ false,
648 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300649 &error_msg));
650 if (dex == nullptr) {
651 os << "Failed to load DexFile from layout container: " + error_msg;
652 success = false;
653 break;
654 }
655 if (dex->IsCompactDexFile()) {
656 os <<"CompactDex conversion to StandardDex failed";
657 success = false;
658 break;
659 }
660
Andreas Gampe9b031f72018-10-04 11:03:34 -0700661 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300662 success = false;
663 break;
664 }
665 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700666 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300667 success = false;
668 break;
669 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300670 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000671 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700672 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700673 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000674
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800675 {
676 os << "OAT FILE STATS:\n";
677 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100678 stats_.AddBytes(oat_file_.Size());
679 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800680 }
681
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700682 os << std::flush;
683 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700684 }
685
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800686 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700687 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
688 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800689 return 0; // Address not in oat file
690 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800691 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
692 reinterpret_cast<uintptr_t>(oat_file_.Begin());
693 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800694 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800695 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800696 return end_offset - begin_offset;
697 }
698
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800699 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700700 return oat_file_.GetOatHeader().GetInstructionSet();
701 }
702
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700703 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800704 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700705 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700706 CHECK(oat_dex_file != nullptr);
707 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700708 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
709 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700710 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
711 << "': " << error_msg;
712 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800713 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800714 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700715 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700716 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700717 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100718 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Vladimir Markoe00e2012019-05-14 14:42:02 +0100719 uint32_t oat_method_index;
720 if (m->IsStatic() || m->IsDirect()) {
721 // Simple case where the oat method index was stashed at load time.
722 oat_method_index = m->GetMethodIndex();
723 } else {
724 // Compute the oat_method_index by search for its position in the class def.
725 ClassAccessor accessor(*dex_file, *class_def);
726 oat_method_index = accessor.NumDirectMethods();
727 bool found_virtual = false;
728 for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
729 // Check method index instead of identity in case of duplicate method definitions.
730 if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
731 found_virtual = true;
732 break;
733 }
734 ++oat_method_index;
735 }
736 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
737 << dex_file->PrettyMethod(m->GetDexMethodIndex());
738 }
739 return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800740 }
741 }
742 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700743 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800744 }
745
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300746 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
747 // files are fully unquickened and stored in dex_files
748 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
749 /* out */ DexFileUniqV* dex_files,
750 /* out */ std::string* error_msg) {
751 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
752 if (file == nullptr) {
753 *error_msg = "Could not open file " + vdex_filename + " for reading.";
754 return nullptr;
755 }
756
757 int64_t vdex_length = file->GetLength();
758 if (vdex_length == -1) {
759 *error_msg = "Could not read the length of file " + vdex_filename;
760 return nullptr;
761 }
762
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100763 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300764 file->GetLength(),
765 PROT_READ | PROT_WRITE,
766 MAP_PRIVATE,
767 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700768 /* start offset= */ 0,
769 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300770 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100771 error_msg);
772 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300773 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
774 return nullptr;
775 }
776
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100777 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300778 if (!vdex_file->IsValid()) {
779 *error_msg = "Vdex file is not valid";
780 return nullptr;
781 }
782
783 DexFileUniqV tmp_dex_files;
784 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
785 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
786 return nullptr;
787 }
788
789 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700790 /* decompile_return_instruction= */ true);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300791
792 *dex_files = std::move(tmp_dex_files);
793 return vdex_file;
794 }
795
David Srbecky86decb62018-06-05 06:41:10 +0100796 bool AddStatsObject(const void* address) {
797 return seen_stats_objects_.insert(address).second; // Inserted new entry.
798 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800799
David Srbecky86decb62018-06-05 06:41:10 +0100800 void DumpStats(VariableIndentationOutputStream& os,
801 const std::string& name,
802 const Stats& stats,
803 double total) {
804 if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
805 double percent = 100.0 * stats.Value() / total;
806 os.Stream()
807 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
808 << std::setw(8) << stats.Count() << " "
809 << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
810 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
811
812 // Sort all children by largest value first, than by name.
813 std::map<std::pair<double, std::string>, const Stats&> sorted_children;
814 for (const auto& it : stats.Children()) {
815 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800816 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800817
David Srbecky86decb62018-06-05 06:41:10 +0100818 // Add "other" row to represent any amount not account for by the children.
819 Stats other;
820 other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
821 if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
822 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800823 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800824
David Srbecky86decb62018-06-05 06:41:10 +0100825 // Print the data.
826 ScopedIndentation indent1(&os);
827 for (const auto& it : sorted_children) {
828 DumpStats(os, it.first.second, it.second, total);
829 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800830 }
David Srbecky86decb62018-06-05 06:41:10 +0100831 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800832
Brian Carlstromaded5f72011-10-07 17:15:04 -0700833 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800834 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800835 // We don't know the length of the code for each method, but we need to know where to stop
836 // when disassembling. What we do know is that a region of code will be followed by some other
837 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
838 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800839 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700840 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700841 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700842 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700843 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
844 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700845 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
846 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800847 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800848 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800849 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700850 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700851 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700852 for (uint32_t class_method_index = 0;
853 class_method_index < accessor.NumMethods();
854 ++class_method_index) {
855 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800856 }
857 }
858 }
859
860 // If the last thing in the file is code for a method, there won't be an offset for the "next"
861 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
862 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800863 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800864 }
865
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700866 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
867 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
868 }
869
Elliott Hughese3c845c2012-02-28 17:23:01 -0800870 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800871 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000872 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800873 code_offset &= ~0x1;
874 }
875 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800876 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800877 }
878
Andreas Gampeb40d3612018-06-26 15:49:42 -0700879 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700880 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800881 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800883 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800884 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700885
Andreas Gampe2ba88952016-04-29 17:52:07 -0700886 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000887 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
888 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100889
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000890 // Print data range of the dex file embedded inside the corresponding vdex file.
891 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
892 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
893 os << StringPrintf(
894 "dex-file: 0x%08x..0x%08x\n",
895 dex_offset,
896 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
897 } else {
898 os << StringPrintf("dex-file not in VDEX file\n");
899 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700900
Andreas Gampe2ba88952016-04-29 17:52:07 -0700901 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700902 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700903 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
904 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700905 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 os << std::flush;
907 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700908 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909
Andreas Gampe2ba88952016-04-29 17:52:07 -0700910 // Print lookup table, if it exists.
911 if (oat_dex_file.GetLookupTableData() != nullptr) {
912 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
913 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700914 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700915 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
916 table_offset,
917 table_offset + table_size - 1);
918 }
919
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 VariableIndentationOutputStream vios(&os);
921 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700922 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800923 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700924 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800925 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
926 continue;
927 }
928
Mathieu Chartier98dad402018-06-04 18:00:12 -0700929 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100931 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700932 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700933 static_cast<ssize_t>(class_def_index),
934 descriptor,
935 oat_class_offset,
936 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100937 << " (" << oat_class.GetStatus() << ")"
938 << " (" << oat_class.GetType() << ")\n";
939 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700940 if (options_.list_classes_) {
941 continue;
942 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700943 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700944 success = false;
945 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800946 if (stop_analysis) {
947 os << std::flush;
948 return success;
949 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700950 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700951 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700952 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700954 }
955
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300956 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
957 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
958 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
959 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300960 bool ExportDexFile(std::ostream& os,
961 const OatDexFile& oat_dex_file,
962 const DexFile* dex_file,
963 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800964 std::string error_msg;
965 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300966
967 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
968 // same as the one extracted from the Oat container (pre-oreo)
969 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800970
971 // Some quick checks just in case
972 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
973 os << "Invalid dex file\n";
974 return false;
975 }
976
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300977 if (dex_file == nullptr) {
978 // Exported bytecode is quickened (dex-to-dex transformations present)
979 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
980 if (dex_file == nullptr) {
981 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
982 return false;
983 }
984
985 // Recompute checksum
986 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
987 dex_file->CalculateChecksum();
988 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300989 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
990 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
991 if (!used_dexlayout) {
992 // Vdex unquicken output should match original input bytecode
993 uint32_t orig_checksum =
994 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
995 if (orig_checksum != dex_file->CalculateChecksum()) {
996 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
997 return false;
998 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300999 }
1000 }
1001
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001002 // Verify output directory exists
1003 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1004 // TODO: Extend OS::DirectoryExists if symlink support is required
1005 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1006 return false;
1007 }
1008
1009 // Beautify path names
1010 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1011 return false;
1012 }
1013
1014 std::string dex_orig_name;
1015 size_t dex_orig_pos = dex_file_location.rfind('/');
1016 if (dex_orig_pos == std::string::npos)
1017 dex_orig_name = dex_file_location;
1018 else
1019 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1020
1021 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001022 if (dex_orig_name.size() == 8 &&
1023 dex_orig_name.compare("base.apk") == 0 &&
1024 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001025 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1026 size_t apk_orig_pos = dex_file_location.rfind('/');
1027 if (apk_orig_pos != std::string::npos) {
1028 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1029 }
1030 }
1031
1032 std::string out_dex_path(options_.export_dex_location_);
1033 if (out_dex_path.back() != '/') {
1034 out_dex_path.append("/");
1035 }
1036 out_dex_path.append(dex_orig_name);
1037 out_dex_path.append("_export.dex");
1038 if (out_dex_path.length() > PATH_MAX) {
1039 return false;
1040 }
1041
1042 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1043 if (file.get() == nullptr) {
1044 os << "Failed to open output dex file " << out_dex_path;
1045 return false;
1046 }
1047
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001048 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001049 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001050 os << "Failed to write dex file";
1051 file->Erase();
1052 return false;
1053 }
1054
1055 if (file->FlushCloseOrErase() != 0) {
1056 os << "Flush and close failed";
1057 return false;
1058 }
1059
1060 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1061 os << std::flush;
1062
1063 return true;
1064 }
1065
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001066 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001067 const OatFile::OatClass& oat_class,
1068 const DexFile& dex_file,
1069 const ClassAccessor& class_accessor,
1070 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001071 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001072 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001073 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001074 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1075 if (!DumpOatMethod(vios,
1076 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1077 class_method_index,
1078 oat_class,
1079 dex_file,
1080 method.GetIndex(),
1081 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001082 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001083 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001084 success = false;
1085 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001086 if (addr_found) {
1087 *stop_analysis = true;
1088 return success;
1089 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001090 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001091 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001092 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001093 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001094 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001095
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 static constexpr uint32_t kPrologueBytes = 16;
1097
1098 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1099 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1100
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001101 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001102 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001103 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001104 const OatFile::OatClass& oat_class,
1105 const DexFile& dex_file,
1106 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001107 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001108 uint32_t method_access_flags,
1109 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001111
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001112 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001113
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001114 // TODO: Support regex
1115 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1116 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001117 return success;
1118 }
1119
David Sehr709b0702016-10-13 09:12:37 -07001120 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001121 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1122 class_method_index, pretty_method.c_str(),
1123 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001124 if (options_.list_methods_) {
1125 return success;
1126 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001127
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001128 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1129 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1130 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1131 uint32_t code_offset = oat_method.GetCodeOffset();
1132 uint32_t code_size = oat_method.GetQuickCodeSize();
1133 if (resolved_addr2instr_ != 0) {
1134 if (resolved_addr2instr_ > code_offset + code_size) {
1135 return success;
1136 } else {
1137 *addr_found = true; // stop analyzing file at next iteration
1138 }
1139 }
1140
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001141 // Everything below is indented at least once.
1142 ScopedIndentation indent1(vios);
1143
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001144 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001145 vios->Stream() << "DEX CODE:\n";
1146 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001147 if (code_item_accessor.HasCodeItem()) {
1148 for (const DexInstructionPcPair& inst : code_item_accessor) {
1149 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1150 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1151 }
1152 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001153 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001154
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001155 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001156 std::unique_ptr<verifier::MethodVerifier> verifier;
1157 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001158 // We need to have the handle scope stay live until after the verifier since the verifier has
1159 // a handle to the dex cache from hs.
1160 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001161 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1162 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001163 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001165 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001166 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001167 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001168 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001169 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001170 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001171 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001172 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001173 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001174 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001175 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1176 oat_method_offsets_offset, oat_file_.Size());
1177 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001178 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001179 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001180 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001181
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001182 ScopedIndentation indent2(vios);
1183 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001184 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1185 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001186 vios->Stream() << StringPrintf("WARNING: "
1187 "code offset 0x%08x is past end of file 0x%08zx.\n",
1188 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001189 success = false;
1190 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001191 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001192 }
1193 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001194 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001195 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1196 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001197 if (AddStatsObject(method_header)) {
1198 stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1199 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001200 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001202 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001203 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001204 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001205 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001206 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1207 method_header_offset, oat_file_.Size());
1208 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001209 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001210 return false;
1211 }
1212
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001213 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001214 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001215 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001217 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001218 uint32_t vmap_table_offset = method_header ==
1219 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001221
1222 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001223 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001224 ? oat_file_.GetVdexFile()->Size()
1225 : method_header->GetCode() - oat_file_.Begin();
1226 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001227 vios->Stream() << StringPrintf("WARNING: "
1228 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1229 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001230 vmap_table_offset,
1231 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001232 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001233 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001234 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001235 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001236 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001237 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001238 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001239 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001240
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001241 ScopedIndentation indent2(vios);
1242 vios->Stream()
1243 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1244 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1245 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1246 vios->Stream() << "\n";
1247 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1248 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1249 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001250 }
1251 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001252 // Based on spill masks from QuickMethodFrameInfo so placed
1253 // after it is dumped, but useful for understanding quick
1254 // code, so dumped here.
1255 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001256 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001257 }
1258 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 vios->Stream() << "CODE: ";
Nicolas Geoffray57083762019-03-05 09:24:45 +00001260 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1261 if (code_size_offset > oat_file_.Size()) {
1262 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 vios->Stream() << StringPrintf("WARNING: "
Nicolas Geoffray57083762019-03-05 09:24:45 +00001264 "code size offset 0x%08x is past end of file 0x%08zx.",
1265 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001266 success = false;
Nicolas Geoffray57083762019-03-05 09:24:45 +00001267 } else {
1268 const void* code = oat_method.GetQuickCode();
1269 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1270 uint64_t aligned_code_end = aligned_code_begin + code_size;
1271 if (AddStatsObject(code)) {
1272 stats_.Child("Code")->AddBytes(code_size);
1273 }
1274
1275 if (options_.absolute_addresses_) {
1276 vios->Stream() << StringPrintf("%p ", code);
1277 }
1278 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1279 code_offset,
1280 code_size_offset,
1281 code_size,
1282 code != nullptr ? "..." : "");
1283
1284 ScopedIndentation indent2(vios);
1285 if (aligned_code_begin > oat_file_.Size()) {
1286 vios->Stream() << StringPrintf("WARNING: "
1287 "start of code at 0x%08x is past end of file 0x%08zx.",
1288 aligned_code_begin, oat_file_.Size());
1289 success = false;
1290 } else if (aligned_code_end > oat_file_.Size()) {
1291 vios->Stream() << StringPrintf(
1292 "WARNING: "
1293 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1294 "code size is 0x%08x loaded from offset 0x%08x.\n",
1295 aligned_code_end, oat_file_.Size(),
1296 code_size, code_size_offset);
1297 success = false;
1298 if (options_.disassemble_code_) {
1299 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1300 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1301 }
1302 }
1303 } else if (code_size > kMaxCodeSize) {
1304 vios->Stream() << StringPrintf(
1305 "WARNING: "
1306 "code size %d is bigger than max expected threshold of %d. "
1307 "code size is 0x%08x loaded from offset 0x%08x.\n",
1308 code_size, kMaxCodeSize,
1309 code_size, code_size_offset);
1310 success = false;
1311 if (options_.disassemble_code_) {
1312 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1313 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1314 }
1315 }
1316 } else if (options_.disassemble_code_) {
1317 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1318 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001319 }
1320 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001321 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001322 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001323 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001324
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001325 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1326 if (spill_mask == 0) {
1327 return;
1328 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001329 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001330 for (size_t i = 0; i < 32; i++) {
1331 if ((spill_mask & (1 << i)) != 0) {
1332 if (is_float) {
1333 os << "fr" << i;
1334 } else {
1335 os << "r" << i;
1336 }
1337 spill_mask ^= 1 << i; // clear bit
1338 if (spill_mask != 0) {
1339 os << ", ";
1340 } else {
1341 break;
1342 }
1343 }
1344 }
1345 os << ")";
1346 }
1347
Roland Levillain442b46a2015-02-18 16:54:21 +00001348 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001349 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001350 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001351 const CodeItemDataAccessor& code_item_accessor) {
1352 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001353 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001354 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001355 if (raw_code_info != nullptr) {
1356 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001357 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001358 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001359 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001360 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001361 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001362 // We don't encode the size in the table, so just emit that we have quickened
1363 // information.
1364 ScopedIndentation indent(vios);
1365 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001366 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001367 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001368 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001369 }
1370
1371 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001372 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001373 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001374 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001376 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001377 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001378 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001379 }
1380
Andreas Gampe36a296f2017-06-13 14:11:11 -07001381 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1382 // According to stack model, the first out is above the Method referernce.
1383 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1384 }
1385
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001386 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001387 uint32_t core_spills,
1388 uint32_t fp_spills,
1389 size_t frame_size,
1390 int reg,
1391 InstructionSet isa) {
1392 PointerSize pointer_size = InstructionSetPointerSize(isa);
1393 if (kIsDebugBuild) {
1394 auto* runtime = Runtime::Current();
1395 if (runtime != nullptr) {
1396 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1397 }
1398 }
1399 DCHECK_ALIGNED(frame_size, kStackAlignment);
1400 DCHECK_NE(reg, -1);
1401 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1402 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1403 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001404 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1405 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001406 const int max_num_special_temps = 1;
1407 if (reg == temp_threshold) {
1408 // The current method pointer corresponds to special location on stack.
1409 return 0;
1410 } else if (reg >= temp_threshold + max_num_special_temps) {
1411 /*
1412 * Special temporaries may have custom locations and the logic above deals with that.
1413 * However, non-special temporaries are placed relative to the outs.
1414 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001415 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001416 + static_cast<size_t>(pointer_size) /* art method */;
1417 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1418 return temps_start + relative_offset;
1419 } else if (reg < num_regs) {
1420 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1421 return locals_start + (reg * sizeof(uint32_t));
1422 } else {
1423 // Handle ins.
1424 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1425 + static_cast<size_t>(pointer_size) /* art method */;
1426 }
1427 }
1428
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001429 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001430 const CodeItemDataAccessor& code_item_accessor) {
1431 if (code_item_accessor.HasCodeItem()) {
1432 size_t num_locals_ins = code_item_accessor.RegistersSize();
1433 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001434 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001435 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001436
1437 os << "vr_stack_locations:";
1438 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1439 // For readability, delimit the different kinds of VRs.
1440 if (reg == num_locals_ins) {
1441 os << "\n\tmethod*:";
1442 } else if (reg == num_locals && num_ins > 0) {
1443 os << "\n\tins:";
1444 } else if (reg == 0 && num_locals > 0) {
1445 os << "\n\tlocals:";
1446 }
1447
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001448 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001449 oat_method.GetCoreSpillMask(),
1450 oat_method.GetFpSpillMask(),
1451 oat_method.GetFrameSizeInBytes(),
1452 reg,
1453 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001454 os << " v" << reg << "[sp + #" << offset << "]";
1455 }
1456
1457 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1458 if (out_reg == 0) {
1459 os << "\n\touts:";
1460 }
1461
Andreas Gampe36a296f2017-06-13 14:11:11 -07001462 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001463 os << " v" << out_reg << "[sp + #" << offset << "]";
1464 }
1465
1466 os << "\n";
1467 }
1468 }
1469
Roland Levillainf2650d12015-05-28 14:53:28 +01001470 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1471 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001472 static bool IsMethodGeneratedByOptimizingCompiler(
1473 const OatFile::OatMethod& oat_method,
1474 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001475 // If the native GC map is null and the Dex `code_item` is not
1476 // null, then this method has been compiled with the optimizing
1477 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001478 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001479 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001480 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001481 }
1482
1483 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1484 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001485 static bool IsMethodGeneratedByDexToDexCompiler(
1486 const OatFile::OatMethod& oat_method,
1487 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001488 // If the quick code is null, the Dex `code_item` is not
1489 // null, and the vmap table is not null, then this method has been compiled
1490 // with the dextodex compiler.
1491 return oat_method.GetQuickCode() == nullptr &&
1492 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001493 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001494 }
1495
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001496 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001497 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001498 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001499 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001500 const dex::ClassDef& class_def,
1501 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001502 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 if ((method_access_flags & kAccNative) == 0) {
1504 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001505 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001506 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001507 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1508 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1509 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001510 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1511 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001512 if (method == nullptr) {
1513 soa.Self()->ClearException();
1514 return nullptr;
1515 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001516 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001517 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001518 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001520
1521 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 }
1523
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001524 // The StackMapsHelper provides the stack maps in the native PC order.
1525 // For identical native PCs, the order from the CodeInfo is preserved.
1526 class StackMapsHelper {
1527 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001528 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001529 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001530 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001531 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001532 offset_(static_cast<uint32_t>(-1)),
1533 stack_map_index_(0u),
1534 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001535 if (number_of_stack_maps_ != 0u) {
1536 // Check if native PCs are ordered.
1537 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001538 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001539 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001540 StackMap current = code_info_.GetStackMapAt(i);
1541 if (last.GetNativePcOffset(instruction_set) >
1542 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001543 ordered = false;
1544 break;
1545 }
1546 last = current;
1547 }
1548 if (!ordered) {
1549 // Create indirection indexes for access in native PC order. We do not optimize
1550 // for the fact that there can currently be only two separately ordered ranges,
1551 // namely normal stack maps and catch-point stack maps.
1552 indexes_.resize(number_of_stack_maps_);
1553 std::iota(indexes_.begin(), indexes_.end(), 0u);
1554 std::sort(indexes_.begin(),
1555 indexes_.end(),
1556 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001557 StackMap left = code_info_.GetStackMapAt(lhs);
1558 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1559 StackMap right = code_info_.GetStackMapAt(rhs);
1560 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001561 // If the PCs are the same, compare indexes to preserve the original order.
1562 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1563 });
1564 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001565 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001566 }
1567 }
1568
1569 const CodeInfo& GetCodeInfo() const {
1570 return code_info_;
1571 }
1572
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001573 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001574 return offset_;
1575 }
1576
1577 StackMap GetStackMap() const {
1578 return GetStackMapAt(stack_map_index_);
1579 }
1580
1581 void Next() {
1582 ++stack_map_index_;
1583 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001584 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001585 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001586 }
1587
1588 private:
1589 StackMap GetStackMapAt(size_t i) const {
1590 if (!indexes_.empty()) {
1591 i = indexes_[i];
1592 }
1593 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001594 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001595 }
1596
1597 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001598 const size_t number_of_stack_maps_;
1599 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001600 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001601 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001602 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001603 };
1604
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001605 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001606 const OatFile::OatMethod& oat_method,
1607 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001608 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001609 const void* quick_code = oat_method.GetQuickCode();
1610
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001611 if (code_size == 0) {
1612 code_size = oat_method.GetQuickCodeSize();
1613 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001614 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001615 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001616 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001617 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1618 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001619 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001620 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
David Srbecky86decb62018-06-05 06:41:10 +01001621 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky42deda82018-08-10 11:23:27 +01001622 helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
David Srbecky86decb62018-06-05 06:41:10 +01001623 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001624 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1625 size_t offset = 0;
1626 while (offset < code_size) {
1627 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1628 if (offset == helper.GetOffset()) {
1629 ScopedIndentation indent1(vios);
1630 StackMap stack_map = helper.GetStackMap();
1631 DCHECK(stack_map.IsValid());
1632 stack_map.Dump(vios,
1633 helper.GetCodeInfo(),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001634 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001635 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001636 do {
1637 helper.Next();
1638 // There may be multiple stack maps at a given PC. We display only the first one.
1639 } while (offset == helper.GetOffset());
1640 }
1641 DCHECK_LT(offset, helper.GetOffset());
1642 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001643 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001644 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1645 size_t offset = 0;
1646 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001647 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001648 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001649 }
1650 }
1651
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001652 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1653 REQUIRES_SHARED(Locks::mutator_lock_) {
1654 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1655 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1656 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1657 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1658 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1659 DCHECK(boot_image_live_objects != nullptr);
1660 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1661 const uint8_t* boot_image_live_objects_address =
1662 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1663 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1664 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1665 boot_image_live_objects->GetLength()).Uint32Value();
1666 return std::make_pair(boot_image_live_objects_address + begin_offset,
1667 boot_image_live_objects_address + end_offset);
1668 }
1669
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001670 void DumpDataBimgRelRoEntries(std::ostream& os) {
1671 os << ".data.bimg.rel.ro: ";
1672 if (oat_file_.GetBootImageRelocations().empty()) {
1673 os << "empty.\n\n";
1674 return;
1675 }
1676
1677 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1678 Runtime* runtime = Runtime::Current();
1679 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1680 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1681 runtime->GetHeap()->GetBootImageSpaces();
1682 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001683 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1684 const uint8_t* live_objects_begin = live_objects.first;
1685 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001686 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1687 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1688 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1689 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001690 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001691 bool found = false;
1692 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001693 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001694 if (local_offset < space->GetImageHeader().GetImageSize()) {
1695 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001696 if (address >= live_objects_begin && address < live_objects_end) {
1697 size_t index =
1698 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1699 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001700 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001701 index);
1702 } else {
1703 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1704 if (o->IsString()) {
1705 os << " String: " << o->AsString()->ToModifiedUtf8();
1706 } else if (o->IsClass()) {
1707 os << " Class: " << o->AsClass()->PrettyDescriptor();
1708 } else {
1709 os << StringPrintf(" 0x%08x %s",
1710 object_offset,
1711 o->GetClass()->PrettyDescriptor().c_str());
1712 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001713 }
1714 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001715 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001716 os << " ArtMethod: " << m->PrettyMethod();
1717 } else {
1718 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1719 object_offset,
1720 space->GetImageFilename().c_str());
1721 }
1722 found = true;
1723 break;
1724 }
1725 }
1726 if (!found) {
1727 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1728 }
1729 os << "\n";
1730 }
1731 } else {
1732 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1733 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1734 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1735 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1736 }
1737 }
1738 os << "\n";
1739 }
1740
Vladimir Markof3c52b42017-11-17 17:32:12 +00001741 template <typename NameGetter>
1742 void DumpBssEntries(std::ostream& os,
1743 const char* slot_type,
1744 const IndexBssMapping* mapping,
1745 uint32_t number_of_indexes,
1746 size_t slot_size,
1747 NameGetter name) {
1748 os << ".bss mapping for " << slot_type << ": ";
1749 if (mapping == nullptr) {
1750 os << "empty.\n";
1751 return;
1752 }
1753 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1754 size_t num_valid_indexes = 0u;
1755 for (const IndexBssMappingEntry& entry : *mapping) {
1756 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1757 }
1758 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1759 os << std::hex;
1760 for (const IndexBssMappingEntry& entry : *mapping) {
1761 uint32_t index = entry.GetIndex(index_bits);
1762 uint32_t mask = entry.GetMask(index_bits);
1763 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1764 for (uint32_t n : LowToHighBits(mask)) {
1765 size_t current_index = index - (32u - index_bits) + n;
1766 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1767 bss_offset += slot_size;
1768 }
1769 DCHECK_EQ(bss_offset, entry.bss_offset);
1770 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1771 }
1772 os << std::dec;
1773 }
1774
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001775 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001776 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001777 const OatDumperOptions& options_;
1778 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001779 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001780 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001781 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001782 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001783 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001784};
1785
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001786class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001787 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001788 ImageDumper(std::ostream* os,
1789 gc::space::ImageSpace& image_space,
1790 const ImageHeader& image_header,
1791 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001792 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001793 vios_(os),
1794 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001795 image_space_(image_space),
1796 image_header_(image_header),
1797 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001798
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001799 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001801 std::ostream& indent_os = vios_.Stream();
1802
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001804
Jeff Haodcdc85b2015-12-04 14:06:18 -08001805 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1806
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001807 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1808 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1809 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001810
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001811 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1812 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1813 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1814 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1815 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1816
1817 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1818 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001819 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001820
1821 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1822 auto section = static_cast<ImageHeader::ImageSections>(i);
1823 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1824 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001825
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001826 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001827 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001828 static_assert(arraysize(image_roots_descriptions_) ==
1829 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001830 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1831 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001832 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1833 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001834 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1835 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001836 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001837 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001838 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001839 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001840 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001841 ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001842 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001843 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1844 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001845 run++;
1846 } else {
1847 break;
1848 }
1849 }
1850 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001851 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001852 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001853 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001854 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001855 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001856 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001857 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001858 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001859 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001860 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001861 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001862 }
1863 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001864 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001865
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001866 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001867 os << "METHOD ROOTS\n";
1868 static_assert(arraysize(image_methods_descriptions_) ==
1869 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1870 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1871 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1872 const char* description = image_methods_descriptions_[i];
1873 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001874 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001875 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001876 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001878
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001879 Runtime* const runtime = Runtime::Current();
1880 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001881 std::string image_filename = image_space_.GetImageFilename();
1882 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001884 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001885 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001886 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001887 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001888 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1889 }
1890 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001891 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001892 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001893 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001894 /*executable=*/ false,
1895 /*low_4gb=*/ false,
1896 /*abs_dex_location=*/ nullptr,
1897 /*reservation=*/ nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001898 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001899 }
1900 if (oat_file == nullptr) {
1901 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1902 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001903 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001904 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001905
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001906 stats_.oat_file_bytes = oat_file->Size();
1907
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001908 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001909
Andreas Gampeb40d3612018-06-26 15:49:42 -07001910 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001911 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001912 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1913 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001914 }
1915
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001917
Jeff Haodcdc85b2015-12-04 14:06:18 -08001918 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001919 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001920 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001921 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001922 {
1923 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1924 heap->FlushAllocStack();
1925 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001926 // Since FlushAllocStack() above resets the (active) allocation
1927 // stack. Need to revoke the thread-local allocation stacks that
1928 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001929 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001930 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001931 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001932 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001934 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001935 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001936 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001937 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001938 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001939 ObjPtr<mirror::DexCache> dex_cache =
1940 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001941 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001942 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001943 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001944 }
1945 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001946 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1947 DumpObject(obj);
1948 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001950 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001951 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001952 indent_os << "\n";
1953 // TODO: Dump fields.
1954 // Dump methods after.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -07001955 image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1956 REQUIRES_SHARED(Locks::mutator_lock_) {
1957 std::ostream& indent_os = vios_.Stream();
1958 indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1959 DumpMethod(&method, indent_os);
1960 indent_os << "\n";
1961 }, image_space_.Begin(), image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001962 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001963 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001964 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001965 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001966 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001967 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001968 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1969 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001970 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001971 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001972 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001973 // If the image is compressed, adjust to decompressed size.
1974 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartier1a842962018-11-13 15:09:51 -08001975 if (image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001976 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1977 }
1978 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001979 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001980 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001981 const auto& object_section = image_header_.GetObjectsSection();
1982 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001983 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001984 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1985 const auto& intern_section = image_header_.GetInternedStringsSection();
1986 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07001987 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1988 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001989 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001990
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001991 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001992
1993 // Objects are kObjectAlignment-aligned.
1994 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1995 if (object_section.Offset() > header_bytes) {
1996 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1997 }
1998
1999 // Field section is 4-byte aligned.
2000 constexpr size_t kFieldSectionAlignment = 4U;
2001 uint32_t end_objects = object_section.Offset() + object_section.Size();
2002 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2003 stats_.alignment_bytes += field_section.Offset() - end_objects;
2004
2005 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2006 uint32_t end_fields = field_section.Offset() + field_section.Size();
2007 CHECK_ALIGNED(method_section.Offset(), 4);
2008 stats_.alignment_bytes += method_section.Offset() - end_fields;
2009
2010 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2011 uint32_t end_methods = method_section.Offset() + method_section.Size();
2012 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2013 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2014
2015 // Intern table is 8-byte aligned.
2016 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002017 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002018 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2019
2020 // Add space between intern table and class table.
2021 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2022 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2023
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002024 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2025 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002026 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002027 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002028
Mathieu Chartiere401d142015-04-22 13:56:20 -07002029 stats_.bitmap_bytes += bitmap_section.Size();
2030 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002031 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002032 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002033 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002034 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002035 stats_.sro_offset_bytes += sro_section.Size();
2036 stats_.metadata_bytes += metadata_section.Size();
2037
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002038 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002039 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002040
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002041 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002042
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002043 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002044 }
2045
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002046 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -07002047 static void PrettyObjectValue(std::ostream& os,
2048 ObjPtr<mirror::Class> type,
2049 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002050 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002051 CHECK(type != nullptr);
2052 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002053 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002054 } else if (type->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002055 ObjPtr<mirror::String> string = value->AsString();
2056 os << StringPrintf("%p String: %s\n",
2057 string.Ptr(),
Ian Rogers68b56852014-08-29 20:19:11 -07002058 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002059 } else if (type->IsClassClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002060 ObjPtr<mirror::Class> klass = value->AsClass();
2061 os << StringPrintf("%p Class: %s\n",
2062 klass.Ptr(),
2063 mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002064 } else {
David Sehr709b0702016-10-13 09:12:37 -07002065 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002066 }
2067 }
2068
Mathieu Chartier3398c782016-09-30 10:27:43 -07002069 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002070 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002071 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002072 switch (field->GetTypeAsPrimitiveType()) {
2073 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002074 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002075 break;
2076 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002077 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002078 break;
2079 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002081 break;
2082 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002083 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002084 break;
2085 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002086 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002087 break;
2088 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002089 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002090 break;
2091 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002092 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002093 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002094 break;
2095 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002096 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002097 break;
2098 case Primitive::kPrimNot: {
2099 // Get the value, don't compute the type unless it is non-null as we don't want
2100 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002101 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002102 if (value == nullptr) {
2103 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002104 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002105 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002106 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002107 if (field_type != nullptr) {
2108 PrettyObjectValue(os, field_type, value);
2109 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002110 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002111 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002112 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2113 }
Ian Rogers50239c72013-06-17 14:53:22 -07002114 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002115 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002116 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002117 default:
2118 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2119 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002120 }
2121 }
2122
Andreas Gampe98104992018-10-16 12:49:47 -07002123 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002124 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002125 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002126 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002127 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002128 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002129 for (ArtField& field : klass->GetIFields()) {
2130 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002131 }
2132 }
2133
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002134 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002135 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002136 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002138 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002139 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002140 image_header_.GetPointerSize());
David Srbeckya54e0cc2019-04-15 15:03:26 +01002141 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2142 if (class_linker->IsQuickResolutionStub(quick_code) ||
2143 class_linker->IsQuickToInterpreterBridge(quick_code) ||
2144 class_linker->IsQuickGenericJniStub(quick_code) ||
2145 class_linker->IsJniDlsymLookupStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002146 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002147 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002148 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002149 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002150 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002151 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002152 }
2153
Mathieu Chartiere401d142015-04-22 13:56:20 -07002154 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002155 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002156 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2157 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002158 return 0;
2159 }
David Srbeckya54e0cc2019-04-15 15:03:26 +01002160 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2161 reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2162 return method_header->GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002163 }
2164
Mathieu Chartiere401d142015-04-22 13:56:20 -07002165 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002166 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002167 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002168 if (oat_code_begin == nullptr) {
2169 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002170 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002171 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002172 }
2173
Andreas Gampe0c183382017-07-13 22:26:24 -07002174 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002175 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002176 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002177 return;
2178 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002179
2180 size_t object_bytes = obj->SizeOf();
2181 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002182 stats_.object_bytes += object_bytes;
2183 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002184
Andreas Gampe0c183382017-07-13 22:26:24 -07002185 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002186
Vladimir Marko4617d582019-03-28 13:48:31 +00002187 ObjPtr<mirror::Class> obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002188 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002189 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002191 } else if (obj->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002192 ObjPtr<mirror::Class> klass = obj->AsClass();
2193 os << StringPrintf("%p: java.lang.Class \"%s\" (",
2194 obj,
David Sehr709b0702016-10-13 09:12:37 -07002195 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002196 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002197 } else if (obj_class->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002198 os << StringPrintf("%p: java.lang.String %s\n",
2199 obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002200 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002201 } else {
David Sehr709b0702016-10-13 09:12:37 -07002202 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002203 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002204 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002205 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002206 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002207 if (obj->IsObjectArray()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002208 ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002209 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00002210 ObjPtr<mirror::Object> value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002211 size_t run = 0;
2212 for (int32_t j = i + 1; j < length; j++) {
2213 if (value == obj_array->Get(j)) {
2214 run++;
2215 } else {
2216 break;
2217 }
2218 }
2219 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002220 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002221 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002222 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002223 i = i + run;
2224 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002225 ObjPtr<mirror::Class> value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002226 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002227 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002228 }
2229 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002230 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002231
Vladimir Marko305c38b2018-02-14 11:50:07 +00002232 if (kBitstringSubtypeCheckEnabled) {
2233 os << "SUBTYPE_CHECK_BITS: ";
2234 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2235 os << "\n";
2236 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002237
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002238 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002239 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002240 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002241 for (ArtField& field : klass->GetSFields()) {
2242 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002243 }
2244 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002245 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002246 auto it = dex_caches_.find(obj);
2247 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002248 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002249 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002250 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002251 size_t num_methods = dex_cache->NumResolvedMethods();
2252 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002253 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002254 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002255 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002256 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002257 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2258 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002259 size_t run = 0;
2260 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002261 j != length &&
2262 elem == mirror::DexCache::GetNativePairPtrSize(
2263 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002264 ++j) {
2265 ++run;
2266 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002267 if (run == 0) {
2268 os << StringPrintf("%zd: ", i);
2269 } else {
2270 os << StringPrintf("%zd to %zd: ", i, i + run);
2271 i = i + run;
2272 }
2273 std::string msg;
2274 if (elem == nullptr) {
2275 msg = "null";
2276 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002277 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002278 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002279 } else {
2280 msg = "<not in method section>";
2281 }
2282 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002283 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002284 }
2285 size_t num_fields = dex_cache->NumResolvedFields();
2286 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002287 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002288 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002289 auto* resolved_fields = dex_cache->GetResolvedFields();
2290 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002291 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002292 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002293 size_t run = 0;
2294 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002295 j != length &&
2296 elem == mirror::DexCache::GetNativePairPtrSize(
2297 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002298 ++j) {
2299 ++run;
2300 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002301 if (run == 0) {
2302 os << StringPrintf("%zd: ", i);
2303 } else {
2304 os << StringPrintf("%zd to %zd: ", i, i + run);
2305 i = i + run;
2306 }
2307 std::string msg;
2308 if (elem == nullptr) {
2309 msg = "null";
2310 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002311 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002312 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002313 } else {
2314 msg = "<not in field section>";
2315 }
2316 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002317 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002318 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002319 size_t num_types = dex_cache->NumResolvedTypes();
2320 if (num_types != 0u) {
2321 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002322 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002323 auto* resolved_types = dex_cache->GetResolvedTypes();
2324 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002325 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002326 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002327 for (size_t j = i + 1; j != num_types; ++j) {
2328 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2329 if (pair.index != other_pair.index ||
2330 pair.object.Read() != other_pair.object.Read()) {
2331 break;
2332 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002333 ++run;
2334 }
2335 if (run == 0) {
2336 os << StringPrintf("%zd: ", i);
2337 } else {
2338 os << StringPrintf("%zd to %zd: ", i, i + run);
2339 i = i + run;
2340 }
2341 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002342 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002343 if (elem == nullptr) {
2344 msg = "null";
2345 } else {
David Sehr709b0702016-10-13 09:12:37 -07002346 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002347 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002348 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002349 }
2350 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002351 }
2352 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002353 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002354 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002355 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002356
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002357 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002358 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 DCHECK(method != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002360 const PointerSize pointer_size = image_header_.GetPointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361 if (method->IsNative()) {
David Srbeckya54e0cc2019-04-15 15:03:26 +01002362 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002363 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002364 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2365 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002366 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002367 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002368 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002369 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2370 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002371 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002372 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002373 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002374 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002375 } else if (method->IsRuntimeMethod()) {
2376 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2377 if (table != nullptr) {
2378 indent_os << "IMT conflict table " << table << " method: ";
2379 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002380 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2381 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002382 }
2383 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002384 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002385 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002386 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002387 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002388
David Srbeckya54e0cc2019-04-15 15:03:26 +01002389 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2390 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2391
Mathieu Chartiere401d142015-04-22 13:56:20 -07002392 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002393 size_t vmap_table_bytes = 0u;
David Srbeckya54e0cc2019-04-15 15:03:26 +01002394 if (quick_oat_code_begin != nullptr) {
2395 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2396 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2397 vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2398 &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002399 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002400 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002401 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002402 }
2403
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002404 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2405 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002406 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002407 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002408 if (method->IsConstructor()) {
2409 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002410 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002411 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002412 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002413 }
2414 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002415 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002416 }
2417 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002418 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002419
Igor Murashkin7617abd2015-07-10 18:27:47 -07002420 uint32_t method_access_flags = method->GetAccessFlags();
2421
Mathieu Chartiere401d142015-04-22 13:56:20 -07002422 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002423 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2424 dex_instruction_bytes,
2425 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002426 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002427
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002428 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002429 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002430
2431 double expansion =
2432 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002433 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002434 }
2435 }
2436
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002437 std::set<const void*> already_seen_;
2438 // Compute the size of the given data within the oat file and whether this is the first time
2439 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002440 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002441 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002442 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002443 already_seen_.insert(oat_data);
2444 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002445 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002446 }
2447 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002448 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002449
2450 public:
2451 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002452 size_t oat_file_bytes = 0u;
2453 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002454
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002455 size_t header_bytes = 0u;
2456 size_t object_bytes = 0u;
2457 size_t art_field_bytes = 0u;
2458 size_t art_method_bytes = 0u;
2459 size_t dex_cache_arrays_bytes = 0u;
2460 size_t interned_strings_bytes = 0u;
2461 size_t class_table_bytes = 0u;
2462 size_t sro_offset_bytes = 0u;
2463 size_t metadata_bytes = 0u;
2464 size_t bitmap_bytes = 0u;
2465 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002466
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002467 size_t managed_code_bytes = 0u;
2468 size_t managed_code_bytes_ignoring_deduplication = 0u;
2469 size_t native_to_managed_code_bytes = 0u;
2470 size_t class_initializer_code_bytes = 0u;
2471 size_t large_initializer_code_bytes = 0u;
2472 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002473
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002474 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002476 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002477
Mathieu Chartiere401d142015-04-22 13:56:20 -07002478 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002479 std::vector<size_t> method_outlier_size;
2480 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002481 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002482
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002483 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002484
Elliott Hughesa0e18062012-04-13 15:59:59 -07002485 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002486 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002487 size_t bytes;
2488 size_t count;
2489 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002490 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002491 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002492
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002493 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002494 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2495 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002496 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002497 it->second.count += 1;
2498 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002499 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002500 }
2501 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002502
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002503 double PercentOfOatBytes(size_t size) {
2504 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2505 }
2506
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002507 double PercentOfFileBytes(size_t size) {
2508 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2509 }
2510
2511 double PercentOfObjectBytes(size_t size) {
2512 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2513 }
2514
Mathieu Chartiere401d142015-04-22 13:56:20 -07002515 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002516 method_outlier_size.push_back(total_size);
2517 method_outlier_expansion.push_back(expansion);
2518 method_outlier.push_back(method);
2519 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002520
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002521 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002522 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002523 size_t sum_of_sizes = 0;
2524 size_t sum_of_sizes_squared = 0;
2525 size_t sum_of_expansion = 0;
2526 size_t sum_of_expansion_squared = 0;
2527 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002528 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002529 return;
2530 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002531 for (size_t i = 0; i < n; i++) {
2532 size_t cur_size = method_outlier_size[i];
2533 sum_of_sizes += cur_size;
2534 sum_of_sizes_squared += cur_size * cur_size;
2535 double cur_expansion = method_outlier_expansion[i];
2536 sum_of_expansion += cur_expansion;
2537 sum_of_expansion_squared += cur_expansion * cur_expansion;
2538 }
2539 size_t size_mean = sum_of_sizes / n;
2540 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2541 double expansion_mean = sum_of_expansion / n;
2542 double expansion_variance =
2543 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2544
2545 // Dump methods whose size is a certain number of standard deviations from the mean
2546 size_t dumped_values = 0;
2547 size_t skipped_values = 0;
2548 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2549 size_t cur_size_variance = i * i * size_variance;
2550 bool first = true;
2551 for (size_t j = 0; j < n; j++) {
2552 size_t cur_size = method_outlier_size[j];
2553 if (cur_size > size_mean) {
2554 size_t cur_var = cur_size - size_mean;
2555 cur_var = cur_var * cur_var;
2556 if (cur_var > cur_size_variance) {
2557 if (dumped_values > 20) {
2558 if (i == 1) {
2559 skipped_values++;
2560 } else {
2561 i = 2; // jump to counting for 1 standard deviation
2562 break;
2563 }
2564 } else {
2565 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002566 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002567 first = false;
2568 }
David Sehr709b0702016-10-13 09:12:37 -07002569 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002570 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002571 method_outlier_size[j] = 0; // don't consider this method again
2572 dumped_values++;
2573 }
2574 }
2575 }
2576 }
2577 }
2578 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002579 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002580 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002581 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002582 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002583
2584 // Dump methods whose expansion is a certain number of standard deviations from the mean
2585 dumped_values = 0;
2586 skipped_values = 0;
2587 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2588 double cur_expansion_variance = i * i * expansion_variance;
2589 bool first = true;
2590 for (size_t j = 0; j < n; j++) {
2591 double cur_expansion = method_outlier_expansion[j];
2592 if (cur_expansion > expansion_mean) {
2593 size_t cur_var = cur_expansion - expansion_mean;
2594 cur_var = cur_var * cur_var;
2595 if (cur_var > cur_expansion_variance) {
2596 if (dumped_values > 20) {
2597 if (i == 1) {
2598 skipped_values++;
2599 } else {
2600 i = 2; // jump to counting for 1 standard deviation
2601 break;
2602 }
2603 } else {
2604 if (first) {
2605 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002606 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002607 first = false;
2608 }
David Sehr709b0702016-10-13 09:12:37 -07002609 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002610 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002611 method_outlier_expansion[j] = 0.0; // don't consider this method again
2612 dumped_values++;
2613 }
2614 }
2615 }
2616 }
2617 }
2618 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002619 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002620 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002621 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002622 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002623 }
2624
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002625 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002626 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002627 {
2628 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2629 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002630 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2631 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2632 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2633 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2634 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2635 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002636 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002637 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2638 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002639 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2640 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002641 header_bytes, PercentOfFileBytes(header_bytes),
2642 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002643 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2644 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002645 dex_cache_arrays_bytes,
2646 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002647 interned_strings_bytes,
2648 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002649 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002650 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2651 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002652 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002653 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2654 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002655 CHECK_EQ(file_bytes,
2656 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2657 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002658 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002660
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002661 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002662 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002663 for (const auto& sizes_and_count : sizes_and_counts) {
2664 const std::string& descriptor(sizes_and_count.first);
2665 double average = static_cast<double>(sizes_and_count.second.bytes) /
2666 static_cast<double>(sizes_and_count.second.count);
2667 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002668 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002669 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002670 descriptor.c_str(), sizes_and_count.second.bytes,
2671 sizes_and_count.second.count, average, percent);
2672 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002673 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002674 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002675 CHECK_EQ(object_bytes, object_bytes_total);
2676
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002677 os << StringPrintf("oat_file_bytes = %8zd\n"
2678 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002679 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2680 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2681 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2682 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002683 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002684 managed_code_bytes,
2685 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002686 native_to_managed_code_bytes,
2687 PercentOfOatBytes(native_to_managed_code_bytes),
2688 class_initializer_code_bytes,
2689 PercentOfOatBytes(class_initializer_code_bytes),
2690 large_initializer_code_bytes,
2691 PercentOfOatBytes(large_initializer_code_bytes),
2692 large_method_code_bytes,
2693 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002695 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002697 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2698 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002699 }
2700
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002701 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002702 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002703 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002704
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002705 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2706 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002707 static_cast<double>(managed_code_bytes) /
2708 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002709 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002710 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002711 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002712
2713 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002714 }
2715 } stats_;
2716
2717 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002718 enum {
2719 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2720 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2721 kLargeConstructorDexBytes = 4000,
2722 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2723 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2724 kLargeMethodDexBytes = 16000
2725 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002726
2727 // For performance, use the *os_ directly for anything that doesn't need indentation
2728 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002729 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002730 VariableIndentationOutputStream vios_;
2731 ScopedIndentation indent1_;
2732
Ian Rogers1d54e732013-05-02 21:10:01 -07002733 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002734 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002735 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002736 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002737 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002738
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002739 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002740};
2741
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002742static int DumpImage(gc::space::ImageSpace* image_space,
2743 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002744 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002745 const ImageHeader& image_header = image_space->GetImageHeader();
2746 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002747 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002748 return EXIT_FAILURE;
2749 }
2750 ImageDumper image_dumper(os, *image_space, image_header, options);
2751 if (!image_dumper.Dump()) {
2752 return EXIT_FAILURE;
2753 }
2754 return EXIT_SUCCESS;
2755}
2756
2757static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002759 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002760 options->class_loader_ = &null_class_loader;
2761
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002762 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002763 if (options->app_image_ != nullptr) {
2764 if (options->app_oat_ == nullptr) {
2765 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002766 return EXIT_FAILURE;
2767 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002768 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2769 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2770 // pointers into 32 bit pointer sized ArtMethods.
2771 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002772 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002773 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002774 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002775 /*executable=*/ false,
2776 /*low_4gb=*/ true,
2777 /*abs_dex_location=*/ nullptr,
2778 /*reservation=*/ nullptr,
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;
2789 }
2790 // Open dex files for the image.
2791 std::vector<std::unique_ptr<const DexFile>> dex_files;
2792 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2793 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2794 << error_msg;
2795 }
2796 // Dump the actual image.
2797 int result = DumpImage(space.get(), options, os);
2798 if (result != EXIT_SUCCESS) {
2799 return result;
2800 }
2801 // Fall through to dump the boot images.
2802 }
2803
2804 gc::Heap* heap = runtime->GetHeap();
2805 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2806 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2807 int result = DumpImage(image_space, options, os);
2808 if (result != EXIT_SUCCESS) {
2809 return result;
2810 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002811 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002812 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002813}
2814
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002815static jobject InstallOatFile(Runtime* runtime,
2816 std::unique_ptr<OatFile> oat_file,
2817 std::vector<const DexFile*>* class_path)
2818 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002819 Thread* self = Thread::Current();
2820 CHECK(self != nullptr);
2821 // Need well-known-classes.
2822 WellKnownClasses::Init(self->GetJniEnv());
2823
Vladimir Marko421087b2018-02-27 11:00:17 +00002824 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002825 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002826 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002827 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002828 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002829 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002830 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002831 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002832 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002833 }
2834
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002835 // Need a class loader. Fake that we're a compiler.
2836 // Note: this will run initializers through the unstarted runtime, so make sure it's
2837 // initialized.
2838 interpreter::UnstartedRuntime::Initialize();
2839
2840 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2841
Vladimir Marko421087b2018-02-27 11:00:17 +00002842 // Need to register dex files to get a working dex cache.
2843 for (const DexFile* dex_file : *class_path) {
2844 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2845 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2846 CHECK(dex_cache != nullptr);
2847 }
2848
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002849 return class_loader;
2850}
2851
2852static int DumpOatWithRuntime(Runtime* runtime,
2853 std::unique_ptr<OatFile> oat_file,
2854 OatDumperOptions* options,
2855 std::ostream* os) {
2856 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2857 ScopedObjectAccess soa(Thread::Current());
2858
2859 OatFile* oat_file_ptr = oat_file.get();
2860 std::vector<const DexFile*> class_path;
2861 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002862
2863 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002864 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002865 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002866 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002867 options->class_loader_ = &loader_handle;
2868
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002869 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002870 bool success = oat_dumper.Dump(*os);
2871 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2872}
2873
2874static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002875 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002876 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002877 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002878 options->class_loader_ = &null_class_loader;
2879
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002880 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002881 bool success = oat_dumper.Dump(*os);
2882 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2883}
2884
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002885static int DumpOat(Runtime* runtime,
2886 const char* oat_filename,
2887 const char* dex_filename,
2888 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002889 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002890 if (dex_filename == nullptr) {
2891 LOG(WARNING) << "No dex filename provided, "
2892 << "oatdump might fail if the oat file does not contain the dex code.";
2893 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002894 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002895 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002896 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002897 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002898 /*executable=*/ false,
2899 /*low_4gb=*/ false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002900 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002901 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002902 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002903 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002904 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002905 return EXIT_FAILURE;
2906 }
2907
2908 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002909 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002910 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002911 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002912 }
2913}
2914
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002915static int SymbolizeOat(const char* oat_filename,
2916 const char* dex_filename,
2917 std::string& output_name,
2918 bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002919 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002920 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002921 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002922 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002923 /*executable=*/ false,
2924 /*low_4gb=*/ false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002925 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002926 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002927 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002928 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002929 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002930 return EXIT_FAILURE;
2931 }
2932
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002933 bool result;
2934 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2935 // files for 64-bit code in the past.
2936 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002937 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002938 result = oat_symbolizer.Symbolize();
2939 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002940 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002941 result = oat_symbolizer.Symbolize();
2942 }
2943 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002944 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002945 return EXIT_FAILURE;
2946 }
2947
2948 return EXIT_SUCCESS;
2949}
2950
Andreas Gampe9fded872016-09-25 16:08:35 -07002951class IMTDumper {
2952 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002953 static bool Dump(Runtime* runtime,
2954 const std::string& imt_file,
2955 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002956 const char* oat_filename,
2957 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002958 Thread* self = Thread::Current();
2959
2960 ScopedObjectAccess soa(self);
2961 StackHandleScope<1> scope(self);
2962 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2963 std::vector<const DexFile*> class_path;
2964
2965 if (oat_filename != nullptr) {
2966 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002967 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002968 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002969 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002970 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002971 /*low_4gb=*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002972 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002973 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002974 &error_msg));
2975 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002976 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002977 return false;
2978 }
2979
2980 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2981 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2982 } else {
2983 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2984 class_path = runtime->GetClassLinker()->GetBootClassPath();
2985 }
2986
2987 if (!imt_file.empty()) {
2988 return DumpImt(runtime, imt_file, class_loader);
2989 }
2990
2991 if (dump_imt_stats) {
2992 return DumpImtStats(runtime, class_path, class_loader);
2993 }
2994
2995 LOG(FATAL) << "Should not reach here";
2996 UNREACHABLE();
2997 }
2998
2999 private:
3000 static bool DumpImt(Runtime* runtime,
3001 const std::string& imt_file,
3002 Handle<mirror::ClassLoader> h_class_loader)
3003 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003004 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3005 std::unordered_set<std::string> prepared;
3006
3007 for (const std::string& line : lines) {
3008 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3009 // or a class descriptor and an interface method, in which case we will lookup the method,
3010 // determine its IMT slot, and check the class' IMT.
3011 size_t first_space = line.find(' ');
3012 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003013 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003014 } else {
3015 DumpIMTForMethod(runtime,
3016 line.substr(0, first_space),
3017 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003018 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003019 &prepared);
3020 }
3021 std::cerr << std::endl;
3022 }
3023
3024 return true;
3025 }
3026
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003027 static bool DumpImtStats(Runtime* runtime,
3028 const std::vector<const DexFile*>& dex_files,
3029 Handle<mirror::ClassLoader> h_class_loader)
3030 REQUIRES_SHARED(Locks::mutator_lock_) {
3031 size_t without_imt = 0;
3032 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003033 std::map<size_t, size_t> histogram;
3034
3035 ClassLinker* class_linker = runtime->GetClassLinker();
3036 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3037 std::unordered_set<std::string> prepared;
3038
3039 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003040 StackHandleScope<1> scope(self);
3041 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3042
3043 for (const DexFile* dex_file : dex_files) {
3044 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003045 class_def_index != dex_file->NumClassDefs();
3046 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003047 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07003048 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003049 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003050 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003051 std::cerr << "Warning: could not load " << descriptor << std::endl;
3052 continue;
3053 }
3054
3055 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003056 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003057 continue;
3058 }
3059
3060 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3061 if (im_table == nullptr) {
3062 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003063 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003064 continue;
3065 }
3066
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003067 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003068 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3069 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3070 if (ptr->IsRuntimeMethod()) {
3071 if (ptr->IsImtUnimplementedMethod()) {
3072 histogram[0]++;
3073 } else {
3074 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3075 histogram[current_table->NumEntries(pointer_size)]++;
3076 }
3077 } else {
3078 histogram[1]++;
3079 }
3080 }
3081 }
3082 }
3083
3084 std::cerr << "IMT stats:"
3085 << std::endl << std::endl;
3086
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003087 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003088 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003089 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003090 << std::endl << std::endl;
3091
3092 double sum_one = 0;
3093 size_t count_one = 0;
3094
3095 std::cerr << " " << "IMT histogram" << std::endl;
3096 for (auto& bucket : histogram) {
3097 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3098 if (bucket.first > 0) {
3099 sum_one += bucket.second * bucket.first;
3100 count_one += bucket.second;
3101 }
3102 }
3103
3104 double count_zero = count_one + histogram[0];
3105 std::cerr << " Stats:" << std::endl;
3106 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3107 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3108
3109 return true;
3110 }
3111
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003112 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003113 static bool HasNoIMT(Runtime* runtime,
3114 Handle<mirror::Class> klass,
3115 const PointerSize pointer_size,
3116 std::unordered_set<std::string>* prepared)
3117 REQUIRES_SHARED(Locks::mutator_lock_) {
3118 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3119 return true;
3120 }
3121
3122 if (klass->GetImt(pointer_size) == nullptr) {
3123 PrepareClass(runtime, klass, prepared);
3124 }
3125
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003126 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003127 DCHECK(object_class->IsObjectClass());
3128
3129 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3130
Mathieu Chartier6beced42016-11-15 15:51:31 -08003131 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003132 DCHECK(result);
3133 }
3134
3135 return result;
3136 }
3137
3138 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3139 REQUIRES_SHARED(Locks::mutator_lock_) {
3140 if (table == nullptr) {
3141 std::cerr << " <No IMT?>" << std::endl;
3142 return;
3143 }
3144 size_t table_index = 0;
3145 for (;;) {
3146 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3147 if (ptr == nullptr) {
3148 return;
3149 }
3150 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003151 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003152 }
3153 }
3154
3155 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3156 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003157 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003158 const std::string& class_name,
3159 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003160 /*out*/ ObjPtr<mirror::Class>* klass_out,
3161 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003162 REQUIRES_SHARED(Locks::mutator_lock_) {
3163 if (class_name.empty()) {
3164 return nullptr;
3165 }
3166
3167 std::string descriptor;
3168 if (class_name[0] == 'L') {
3169 descriptor = class_name;
3170 } else {
3171 descriptor = DotToDescriptor(class_name.c_str());
3172 }
3173
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003174 ObjPtr<mirror::Class> klass =
3175 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003176
3177 if (klass == nullptr) {
3178 self->ClearException();
3179 std::cerr << "Did not find " << class_name << std::endl;
3180 *klass_out = nullptr;
3181 return nullptr;
3182 }
3183
3184 StackHandleScope<1> scope(Thread::Current());
3185 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3186
3187 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3188 *klass_out = h_klass.Get();
3189 return ret;
3190 }
3191
3192 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3193 Handle<mirror::Class> h_klass,
3194 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003195 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003196 REQUIRES_SHARED(Locks::mutator_lock_) {
3197 PrepareClass(runtime, h_klass, prepared);
3198 return h_klass->GetImt(pointer_size);
3199 }
3200
3201 static void DumpIMTForClass(Runtime* runtime,
3202 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003203 Handle<mirror::ClassLoader> h_loader,
3204 std::unordered_set<std::string>* prepared)
3205 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003206 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003207 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003208 ImTable* imt = PrepareAndGetImTable(runtime,
3209 Thread::Current(),
3210 h_loader,
3211 class_name,
3212 pointer_size,
3213 &klass,
3214 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003215 if (imt == nullptr) {
3216 return;
3217 }
3218
3219 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3220 for (size_t index = 0; index < ImTable::kSize; ++index) {
3221 std::cerr << " " << index << ":" << std::endl;
3222 ArtMethod* ptr = imt->Get(index, pointer_size);
3223 if (ptr->IsRuntimeMethod()) {
3224 if (ptr->IsImtUnimplementedMethod()) {
3225 std::cerr << " <empty>" << std::endl;
3226 } else {
3227 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3228 PrintTable(current_table, pointer_size);
3229 }
3230 } else {
David Sehr709b0702016-10-13 09:12:37 -07003231 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003232 }
3233 }
3234
3235 std::cerr << " Interfaces:" << std::endl;
3236 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003237 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003238 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003239 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003240 std::string iface_name;
3241 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003242
Mathieu Chartier6beced42016-11-15 15:51:31 -08003243 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3244 uint32_t class_hash, name_hash, signature_hash;
3245 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3246 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3247 std::cerr << " " << iface_method.PrettyMethod(true)
3248 << " slot=" << imt_slot
3249 << std::hex
3250 << " class_hash=0x" << class_hash
3251 << " name_hash=0x" << name_hash
3252 << " signature_hash=0x" << signature_hash
3253 << std::dec
3254 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003255 }
3256 }
3257 }
3258
3259 static void DumpIMTForMethod(Runtime* runtime,
3260 const std::string& class_name,
3261 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003262 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003263 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003264 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003265 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003266 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003267 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003268 Thread::Current(),
3269 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003270 class_name,
3271 pointer_size,
3272 &klass,
3273 prepared);
3274 if (imt == nullptr) {
3275 return;
3276 }
3277
3278 std::cerr << class_name << " <" << method << ">" << std::endl;
3279 for (size_t index = 0; index < ImTable::kSize; ++index) {
3280 ArtMethod* ptr = imt->Get(index, pointer_size);
3281 if (ptr->IsRuntimeMethod()) {
3282 if (ptr->IsImtUnimplementedMethod()) {
3283 continue;
3284 }
3285
3286 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3287 if (current_table == nullptr) {
3288 continue;
3289 }
3290
3291 size_t table_index = 0;
3292 for (;;) {
3293 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3294 if (ptr2 == nullptr) {
3295 break;
3296 }
3297 table_index++;
3298
David Sehr709b0702016-10-13 09:12:37 -07003299 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003300 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003301 std::cerr << " Slot "
3302 << index
3303 << " ("
3304 << current_table->NumEntries(pointer_size)
3305 << ")"
3306 << std::endl;
3307 PrintTable(current_table, pointer_size);
3308 return;
3309 }
3310 }
3311 } else {
David Sehr709b0702016-10-13 09:12:37 -07003312 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003313 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003314 std::cerr << " Slot " << index << " (1)" << std::endl;
3315 std::cerr << " " << p_name << std::endl;
3316 } else {
3317 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003318 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003319 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003320 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003321 size_t num_methods = iface->NumDeclaredVirtualMethods();
3322 if (num_methods > 0) {
3323 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3324 if (ImTable::GetImtIndex(&iface_method) == index) {
3325 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003326 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003327 std::cerr << " Slot " << index << " (1)" << std::endl;
3328 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003329 }
3330 }
3331 }
3332 }
3333 }
3334 }
3335 }
3336 }
3337 }
3338
3339 // Read lines from the given stream, dropping comments and empty lines
3340 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3341 std::vector<std::string> output;
3342 while (in_stream.good()) {
3343 std::string dot;
3344 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003345 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003346 continue;
3347 }
3348 output.push_back(dot);
3349 }
3350 return output;
3351 }
3352
3353 // Read lines from the given file, dropping comments and empty lines.
3354 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3355 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3356 if (input_file.get() == nullptr) {
3357 LOG(ERROR) << "Failed to open input file " << input_filename;
3358 return std::vector<std::string>();
3359 }
3360 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3361 input_file->close();
3362 return result;
3363 }
3364
3365 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3366 // and note in the given set that the work was done.
3367 static void PrepareClass(Runtime* runtime,
3368 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003369 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003370 REQUIRES_SHARED(Locks::mutator_lock_) {
3371 if (!h_klass->ShouldHaveImt()) {
3372 return;
3373 }
3374
3375 std::string name;
3376 name = h_klass->GetDescriptor(&name);
3377
3378 if (done->find(name) != done->end()) {
3379 return;
3380 }
3381 done->insert(name);
3382
3383 if (h_klass->HasSuperClass()) {
3384 StackHandleScope<1> h(Thread::Current());
3385 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3386 }
3387
3388 if (!h_klass->IsTemp()) {
3389 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3390 }
3391 }
3392};
3393
Igor Murashkin37743352014-11-13 14:38:00 -08003394struct OatdumpArgs : public CmdlineArgs {
3395 protected:
3396 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003397
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003398 ParseStatus ParseCustom(const char* raw_option,
3399 size_t raw_option_length,
3400 std::string* error_msg) override {
3401 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003402 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003403 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003404 if (base_parse != kParseUnknownArgument) {
3405 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003406 }
3407 }
3408
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003409 std::string_view option(raw_option, raw_option_length);
3410 if (StartsWith(option, "--oat-file=")) {
3411 oat_filename_ = raw_option + strlen("--oat-file=");
3412 } else if (StartsWith(option, "--dex-file=")) {
3413 dex_filename_ = raw_option + strlen("--dex-file=");
3414 } else if (StartsWith(option, "--image=")) {
3415 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003416 } else if (option == "--no-dump:vmap") {
3417 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003418 } else if (option =="--dump:code_info_stack_maps") {
3419 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003420 } else if (option == "--no-disassemble") {
3421 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003422 } else if (option =="--header-only") {
3423 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003424 } else if (StartsWith(option, "--symbolize=")) {
3425 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003426 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003427 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003428 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003429 } else if (StartsWith(option, "--class-filter=")) {
3430 class_filter_ = raw_option + strlen("--class-filter=");
3431 } else if (StartsWith(option, "--method-filter=")) {
3432 method_filter_ = raw_option + strlen("--method-filter=");
3433 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003434 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003435 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003436 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003437 } else if (StartsWith(option, "--export-dex-to=")) {
3438 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3439 } else if (StartsWith(option, "--addr2instr=")) {
3440 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003441 *error_msg = "Address conversion failed";
3442 return kParseError;
3443 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003444 } else if (StartsWith(option, "--app-image=")) {
3445 app_image_ = raw_option + strlen("--app-image=");
3446 } else if (StartsWith(option, "--app-oat=")) {
3447 app_oat_ = raw_option + strlen("--app-oat=");
3448 } else if (StartsWith(option, "--dump-imt=")) {
3449 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003450 } else if (option == "--dump-imt-stats") {
3451 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003452 } else {
3453 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003454 }
3455
Igor Murashkin37743352014-11-13 14:38:00 -08003456 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003457 }
3458
Roland Levillainf73caca2018-08-24 17:19:07 +01003459 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003460 // Infer boot image location from the image location if possible.
3461 if (boot_image_location_ == nullptr) {
3462 boot_image_location_ = image_location_;
3463 }
3464
3465 // Perform the parent checks.
3466 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3467 if (parent_checks != kParseOk) {
3468 return parent_checks;
3469 }
3470
3471 // Perform our own checks.
3472 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3473 *error_msg = "Either --image or --oat-file must be specified";
3474 return kParseError;
3475 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3476 *error_msg = "Either --image or --oat-file must be specified but not both";
3477 return kParseError;
3478 }
3479
3480 return kParseOk;
3481 }
3482
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003483 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003484 std::string usage;
3485
3486 usage +=
3487 "Usage: oatdump [options] ...\n"
3488 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3489 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3490 "\n"
3491 // Either oat-file or image is required.
3492 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3493 " Example: --oat-file=/system/framework/boot.oat\n"
3494 "\n"
3495 " --image=<file.art>: specifies an input image location.\n"
3496 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003497 "\n"
3498 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003499 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003500 " Example: --app-image=app.art\n"
3501 "\n"
3502 " --app-oat=<file.odex>: specifies an input app oat.\n"
3503 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003504 "\n";
3505
3506 usage += Base::GetUsage();
3507
3508 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003509 " --no-dump:vmap may be used to disable vmap dumping.\n"
3510 " Example: --no-dump:vmap\n"
3511 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003512 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3513 " Example: --dump:code_info_stack_maps\n"
3514 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003515 " --no-disassemble may be used to disable disassembly.\n"
3516 " Example: --no-disassemble\n"
3517 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003518 " --header-only may be used to print only the oat header.\n"
3519 " Example: --header-only\n"
3520 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003521 " --list-classes may be used to list target file classes (can be used with filters).\n"
3522 " Example: --list-classes\n"
3523 " Example: --list-classes --class-filter=com.example.foo\n"
3524 "\n"
3525 " --list-methods may be used to list target file methods (can be used with filters).\n"
3526 " Example: --list-methods\n"
3527 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3528 "\n"
3529 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3530 " Example: --symbolize=/system/framework/boot.oat\n"
3531 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003532 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3533 " .rodata and .text sections are omitted in the output file to save space.\n"
3534 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3535 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003536 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3537 " Example: --class-filter=com.example.foo\n"
3538 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003539 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3540 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003541 "\n"
3542 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3543 " Example: --export-dex-to=/data/local/tmp\n"
3544 "\n"
3545 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3546 " address (e.g. PC from crash dump)\n"
3547 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003548 "\n"
3549 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3550 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003551 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003552 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003553 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003554 " Example: --dump-imt=imt.txt\n"
3555 "\n"
3556 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3557 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003558 "\n";
3559
3560 return usage;
3561 }
3562
3563 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003564 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003565 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003566 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003567 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003568 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003569 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003570 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003571 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003572 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003573 bool disassemble_code_ = true;
3574 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003575 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003576 bool list_classes_ = false;
3577 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003578 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003579 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003580 uint32_t addr2instr_ = 0;
3581 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003582 const char* app_image_ = nullptr;
3583 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003584};
3585
Igor Murashkin37743352014-11-13 14:38:00 -08003586struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003587 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003588 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003589
Igor Murashkin37743352014-11-13 14:38:00 -08003590 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3591 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3592
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003593 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003594 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003595 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003596 args_->disassemble_code_,
3597 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003598 args_->class_filter_,
3599 args_->method_filter_,
3600 args_->list_classes_,
3601 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003602 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003603 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003604 args_->app_image_,
3605 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003606 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003607
Andreas Gampe9fded872016-09-25 16:08:35 -07003608 return (args_->boot_image_location_ != nullptr ||
3609 args_->image_location_ != nullptr ||
3610 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003611 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003612 }
3613
Roland Levillainf73caca2018-08-24 17:19:07 +01003614 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003615 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003616 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003617
Mathieu Chartierd424d082014-10-15 10:31:46 -07003618 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003619
Andreas Gampec24f3992014-12-17 20:40:11 -08003620 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003621 // ELF has special kind of section called SHT_NOBITS which allows us to create
3622 // sections which exist but their data is omitted from the ELF file to save space.
3623 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3624 // with only debug data. We use it in similar way to exclude .rodata and .text.
3625 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003626 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3627 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003628 } else {
3629 return DumpOat(nullptr,
3630 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003631 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003632 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003633 args_->os_) == EXIT_SUCCESS;
3634 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003635 }
3636
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003637 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003638 CHECK(args_ != nullptr);
3639
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003640 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3641 return IMTDumper::Dump(runtime,
3642 args_->imt_dump_,
3643 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003644 args_->oat_filename_,
3645 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003646 }
3647
Igor Murashkin37743352014-11-13 14:38:00 -08003648 if (args_->oat_filename_ != nullptr) {
3649 return DumpOat(runtime,
3650 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003651 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003652 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003653 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003654 }
Igor Murashkin37743352014-11-13 14:38:00 -08003655
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003656 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003657 }
3658
Igor Murashkin37743352014-11-13 14:38:00 -08003659 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3660};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003661
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003662} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003663
3664int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003665 // Output all logging to stderr.
3666 android::base::SetLogger(android::base::StderrLogger);
3667
Igor Murashkin37743352014-11-13 14:38:00 -08003668 art::OatdumpMain main;
3669 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003670}