blob: 02636e24ea0fc6682d28acb72b25128af4c76ced [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 Marko5868ada2020-05-12 11:50:34 +010047#include "class_root-inl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010048#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000049#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000050#include "debug/elf_debug_writer.h"
51#include "debug/method_debug_info.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030052#include "dex/art_dex_file_loader.h"
Mathieu Chartier3d092992018-05-24 13:36:23 -070053#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080054#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080055#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080056#include "dex/dex_file-inl.h"
57#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070058#include "dex/string_reference.h"
David Sehr9c4a0152018-04-05 12:23:54 -070059#include "dex/type_lookup_table.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030060#include "dexlayout.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080061#include "disassembler.h"
David Srbecky2faab002019-02-12 16:35:48 +000062#include "elf/elf_builder.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070063#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070064#include "gc/space/image_space.h"
65#include "gc/space/large_object_space.h"
66#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080067#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070068#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000069#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070070#include "interpreter/unstarted_runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071#include "mirror/array-inl.h"
72#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010073#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074#include "mirror/object-inl.h"
75#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010077#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070079#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070080#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070081#include "stack_map.h"
David Srbecky2faab002019-02-12 16:35:48 +000082#include "stream/buffered_output_stream.h"
83#include "stream/file_output_stream.h"
84#include "subtype_check.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080085#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010086#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000088#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070089#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070090
Igor Murashkin37743352014-11-13 14:38:00 -080091#include <sys/stat.h>
92#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070093
Igor Murashkin37743352014-11-13 14:38:00 -080094namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070095
Andreas Gampe46ee31b2016-12-14 10:11:49 -080096using android::base::StringPrintf;
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080099 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700100 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700101 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100102 "kSaveAllCalleeSavesMethod",
103 "kSaveRefsOnlyMethod",
104 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +0100105 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -0700106 "kSaveEverythingMethodForClinit",
107 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108};
109
110const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700111 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700112 "kClassRoots",
Alex Light885f0c12019-01-09 13:48:55 -0800113 "kSpecialRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700114};
115
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700116// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700117static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700118
Andreas Gampeb40d3612018-06-26 15:49:42 -0700119const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120 DCHECK(oat_dex_file != nullptr);
121 auto it = opened_dex_files.find(oat_dex_file);
122 if (it != opened_dex_files.end()) {
123 return it->second.get();
124 }
125 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
126 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
127 return ret;
128}
129
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800130template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000133 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
134 oat_file_(oat_file),
135 builder_(nullptr),
136 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
137 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700138 }
139
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700140 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800142 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000143 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
Andreas Gampe08c277c2017-04-26 22:22:15 -0700145 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
146 if (elf_file == nullptr) {
147 return false;
148 }
David Srbecky2faab002019-02-12 16:35:48 +0000149 std::unique_ptr<BufferedOutputStream> output_stream =
150 std::make_unique<BufferedOutputStream>(
151 std::make_unique<FileOutputStream>(elf_file.get()));
152 builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153
154 builder_->Start();
155
156 auto* rodata = builder_->GetRoData();
157 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 const uint8_t* rodata_begin = oat_file_->Begin();
160 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000161 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000162 rodata->Start();
163 rodata->WriteFully(rodata_begin, rodata_size);
164 rodata->End();
165 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
168 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000169 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000170 text->Start();
171 text->WriteFully(text_begin, text_size);
172 text->End();
173 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 builder_->PrepareDynamicSection(elf_file->GetPath(),
176 rodata_size,
177 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000178 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100180 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000181 oat_file_->BssRootsOffset(),
182 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000183 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184
Vladimir Marko1b404a82017-09-01 13:35:26 +0100185 const OatHeader& oat_header = oat_file_->GetOatHeader();
186 #define DO_TRAMPOLINE(fn_name) \
187 if (oat_header.Get ## fn_name ## Offset() != 0) { \
188 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000189 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100190 info.isa = oat_header.GetInstructionSet(); \
191 info.is_code_address_text_relative = true; \
192 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
193 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
194 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
195 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
196 method_debug_infos_.push_back(std::move(info)); \
197 }
Vladimir Marko7dac8642019-11-06 17:09:30 +0000198 DO_TRAMPOLINE(JniDlsymLookupTrampoline);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000199 DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);
Vladimir Marko1b404a82017-09-01 13:35:26 +0100200 DO_TRAMPOLINE(QuickGenericJniTrampoline);
201 DO_TRAMPOLINE(QuickImtConflictTrampoline);
202 DO_TRAMPOLINE(QuickResolutionTrampoline);
203 DO_TRAMPOLINE(QuickToInterpreterBridge);
204 #undef DO_TRAMPOLINE
205
David Srbecky5d950762016-03-07 20:47:29 +0000206 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100207
208 // TODO: Try to symbolize link-time thunks?
209 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700210
David Srbecky32210b92017-12-04 14:39:21 +0000211 // TODO: Add symbols for dex bytecode in the .dex section.
212
213 debug::DebugInfo debug_info{};
214 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
215
David Srbecky7370d922019-02-12 14:00:30 +0000216 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217
David Srbecky6d8c8f02015-10-26 10:57:09 +0000218 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700219
Andreas Gampe08c277c2017-04-26 22:22:15 -0700220 bool ret_value = builder_->Good();
221
222 builder_.reset();
223 output_stream.reset();
224
225 if (elf_file->FlushCloseOrErase() != 0) {
226 return false;
227 }
228 elf_file.reset();
229
230 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 }
232
David Srbecky5d950762016-03-07 20:47:29 +0000233 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700234 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700235 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700236 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000238 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 }
240 }
241
Andreas Gampeb40d3612018-06-26 15:49:42 -0700242 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700244 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
245 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 return;
247 }
248 for (size_t class_def_index = 0;
249 class_def_index < dex_file->NumClassDefs();
250 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
252 OatClassType type = oat_class.GetType();
253 switch (type) {
254 case kOatClassAllCompiled:
255 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000256 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 break;
258
259 case kOatClassNoneCompiled:
260 case kOatClassMax:
261 // Ignore.
262 break;
263 }
264 }
265 }
266
David Srbecky5d950762016-03-07 20:47:29 +0000267 void WalkOatClass(const OatFile::OatClass& oat_class,
268 const DexFile& dex_file,
269 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700270 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 // Note: even if this is an interface or a native class, we still have to walk it, as there
272 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700274 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000275 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
276 dex_file,
277 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700278 method.GetIndex(),
279 method.GetCodeItem(),
280 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 }
283
David Srbecky5d950762016-03-07 20:47:29 +0000284 void WalkOatMethod(const OatFile::OatMethod& oat_method,
285 const DexFile& dex_file,
286 uint32_t class_def_index,
287 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800288 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000289 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700290 if ((method_access_flags & kAccAbstract) != 0) {
291 // Abstract method, no code.
292 return;
293 }
David Srbecky5d950762016-03-07 20:47:29 +0000294 const OatHeader& oat_header = oat_file_->GetOatHeader();
295 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
296 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700297 // No code.
298 return;
299 }
300
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100301 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
302 // Clear Thumb2 bit.
303 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
304
Vladimir Marko1b404a82017-09-01 13:35:26 +0100305 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000306 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000307 info.dex_file = &dex_file;
308 info.class_def_index = class_def_index;
309 info.dex_method_index = dex_method_index;
310 info.access_flags = method_access_flags;
311 info.code_item = code_item;
312 info.isa = oat_header.GetInstructionSet();
313 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
314 info.is_native_debuggable = oat_header.IsNativeDebuggable();
315 info.is_optimized = method_header->IsOptimized();
316 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100317 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000318 info.code_size = method_header->GetCodeSize();
319 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
320 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
321 info.cfi = ArrayRef<uint8_t>();
322 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700323 }
324
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700325 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 const OatFile* oat_file_;
David Srbecky2faab002019-02-12 16:35:48 +0000327 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000328 std::vector<debug::MethodDebugInfo> method_debug_infos_;
329 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700330 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000331 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700332};
333
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334class OatDumperOptions {
335 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100336 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100337 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700338 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700339 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 const char* class_filter,
341 const char* method_filter,
342 bool list_classes,
343 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000344 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800346 const char* app_image,
347 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100349 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100350 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700351 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700352 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000354 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 list_classes_(list_classes),
356 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000357 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800359 app_image_(app_image),
360 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800361 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800362 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700364 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100365 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 const bool disassemble_code_;
367 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800368 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000369 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 const bool list_classes_;
371 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000372 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800374 const char* const app_image_;
375 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700377 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700378};
379
Elliott Hughese3c845c2012-02-28 17:23:01 -0800380class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700381 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100382 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000383 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800384 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700385 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800386 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800387 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
388 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700389 new DisassemblerOptions(
390 options_.absolute_addresses_,
391 oat_file.Begin(),
392 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700393 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700394 Is64BitInstructionSet(instruction_set_)
395 ? &Thread::DumpThreadOffset<PointerSize::k64>
396 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800397 CHECK(options_.class_loader_ != nullptr);
398 CHECK(options_.class_filter_ != nullptr);
399 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800400 AddAllOffsets();
401 }
402
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700403 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 delete disassembler_;
405 }
406
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800407 InstructionSet GetInstructionSet() {
408 return instruction_set_;
409 }
410
Andreas Gampec55bb392018-09-21 00:02:02 +0000411 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300412
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700413 bool Dump(std::ostream& os) {
414 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
417 os << "MAGIC:\n";
418 os << oat_header.GetMagic() << "\n\n";
419
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800420 os << "LOCATION:\n";
421 os << oat_file_.GetLocation() << "\n\n";
422
Brian Carlstromaded5f72011-10-07 17:15:04 -0700423 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800424 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425
Elliott Hughesa72ec822012-03-05 17:12:22 -0800426 os << "INSTRUCTION SET:\n";
427 os << oat_header.GetInstructionSet() << "\n\n";
428
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700429 {
430 std::unique_ptr<const InstructionSetFeatures> features(
431 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
432 oat_header.GetInstructionSetFeaturesBitmap()));
433 os << "INSTRUCTION SET FEATURES:\n";
434 os << features->GetFeatureString() << "\n\n";
435 }
Dave Allison70202782013-10-22 17:52:19 -0700436
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437 os << "DEX FILE COUNT:\n";
438 os << oat_header.GetDexFileCount() << "\n\n";
439
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800440#define DUMP_OAT_HEADER_OFFSET(label, offset) \
441 os << label " OFFSET:\n"; \
442 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800444 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
445 } \
446 os << StringPrintf("\n\n");
447
448 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
Vladimir Marko7dac8642019-11-06 17:09:30 +0000449 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
450 GetJniDlsymLookupTrampolineOffset);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000451 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP CRITICAL TRAMPOLINE",
452 GetJniDlsymLookupCriticalTrampolineOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800453 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
454 GetQuickGenericJniTrampolineOffset);
455 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
456 GetQuickImtConflictTrampolineOffset);
457 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
458 GetQuickResolutionTrampolineOffset);
459 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
460 GetQuickToInterpreterBridgeOffset);
461#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700462
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700463 // Print the key-value store.
464 {
465 os << "KEY VALUE STORE:\n";
466 size_t index = 0;
467 const char* key;
468 const char* value;
469 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
470 os << key << " = " << value << "\n";
471 index++;
472 }
473 os << "\n";
474 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700475
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800476 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700477 os << "BEGIN:\n";
478 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700479
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700480 os << "END:\n";
481 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
482 }
483
484 os << "SIZE:\n";
485 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486
487 os << std::flush;
488
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800489 // If set, adjust relative address to be searched
490 if (options_.addr2instr_ != 0) {
491 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
492 os << "SEARCH ADDRESS (executable offset + input):\n";
493 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
494 }
495
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000496 // Dump .data.bimg.rel.ro entries.
497 DumpDataBimgRelRoEntries(os);
498
499 // Dump .bss summary, individual entries are dumped per dex file.
500 os << ".bss: ";
501 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
502 os << "empty.\n\n";
503 } else {
504 os << oat_file_.GetBssMethods().size() << " methods, ";
505 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
506 }
507
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700508 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700509 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700510 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700511 CHECK(oat_dex_file != nullptr);
512 std::string error_msg;
513 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
514 if (dex_file == nullptr) {
515 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
516 << error_msg;
517 continue;
518 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700519
Mathieu Chartier120aa282017-08-05 16:03:03 -0700520 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
521 if (layout_sections != nullptr) {
522 os << "Layout data\n";
523 os << *layout_sections;
524 os << "\n";
525 }
526
Vladimir Markobacb8e42018-06-13 14:45:30 +0100527 if (!options_.dump_header_only_) {
528 // Dump .bss entries.
529 DumpBssEntries(
530 os,
531 "ArtMethod",
532 oat_dex_file->GetMethodBssMapping(),
533 dex_file->NumMethodIds(),
534 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
535 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
536 DumpBssEntries(
537 os,
538 "Class",
539 oat_dex_file->GetTypeBssMapping(),
540 dex_file->NumTypeIds(),
541 sizeof(GcRoot<mirror::Class>),
542 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
543 DumpBssEntries(
544 os,
Vladimir Markobaade402020-09-30 14:45:39 +0000545 "Public Class",
546 oat_dex_file->GetPublicTypeBssMapping(),
547 dex_file->NumTypeIds(),
548 sizeof(GcRoot<mirror::Class>),
549 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
550 DumpBssEntries(
551 os,
552 "Package Class",
553 oat_dex_file->GetPackageTypeBssMapping(),
554 dex_file->NumTypeIds(),
555 sizeof(GcRoot<mirror::Class>),
556 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
557 DumpBssEntries(
558 os,
Vladimir Markobacb8e42018-06-13 14:45:30 +0100559 "String",
560 oat_dex_file->GetStringBssMapping(),
561 dex_file->NumStringIds(),
562 sizeof(GcRoot<mirror::Class>),
563 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
564 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700565 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700566
David Brazdilc03d7b62016-03-02 12:18:03 +0000567 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000568 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000569 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000570 if (vdex_header.IsValid()) {
571 std::string error_msg;
572 std::vector<const DexFile*> dex_files;
573 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
574 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
575 if (dex_file == nullptr) {
576 os << "Error opening dex file: " << error_msg << std::endl;
577 return false;
578 }
579 dex_files.push_back(dex_file);
580 }
Vladimir Markoc3908792020-04-06 14:52:04 +0100581 verifier::VerifierDeps deps(dex_files, /*output_only=*/ false);
582 if (!deps.ParseStoredData(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData())) {
583 os << "Error parsing verifier dependencies." << std::endl;
584 return false;
585 }
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000586 deps.Dump(&vios);
587 } else {
588 os << "UNRECOGNIZED vdex file, magic "
589 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000590 << ", verifier deps version "
591 << vdex_header.GetVerifierDepsVersion()
592 << ", dex section version "
593 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000594 << "\n";
595 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000596 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700597 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000598 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300599 if (!DumpOatDexFile(os, *oat_dex_file)) {
600 success = false;
601 }
602 }
603 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800604
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300605 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000606 std::string error_msg;
607 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
608 if (!OS::FileExists(vdex_filename.c_str())) {
609 os << "File " << vdex_filename.c_str() << " does not exist\n";
610 return false;
611 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300612
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000613 DexFileUniqV vdex_dex_files;
614 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
615 &vdex_dex_files,
616 &error_msg);
617 if (vdex_file.get() == nullptr) {
618 os << "Failed to open vdex file: " << error_msg << "\n";
619 return false;
620 }
621 if (oat_dex_files_.size() != vdex_dex_files.size()) {
622 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
623 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
624 return false;
625 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300626
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000627 size_t i = 0;
628 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700629 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000630 CHECK(oat_dex_file != nullptr);
631 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300632
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300633 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
634 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
635 // the original input Dex file, the `update_checksum_` option is used to recompute the
636 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
637 // false), ExportDexFile() enforces a reproducible checksum verification.
638 if (vdex_dex_file->IsCompactDexFile()) {
639 Options options;
640 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
641 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700642 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300643 std::unique_ptr<art::DexContainer> dex_container;
644 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
645 vdex_dex_file.get(),
646 i,
647 &dex_container,
648 &error_msg);
649 if (!result) {
650 os << "DexLayout failed to process Dex file: " + error_msg;
651 success = false;
652 break;
653 }
654 DexContainer::Section* main_section = dex_container->GetMainSection();
655 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
656
657 const ArtDexFileLoader dex_file_loader;
658 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
659 main_section->Begin(),
660 main_section->Size(),
661 vdex_dex_file->GetLocation(),
662 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700663 /*oat_dex_file=*/ nullptr,
664 /*verify=*/ false,
665 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300666 &error_msg));
667 if (dex == nullptr) {
668 os << "Failed to load DexFile from layout container: " + error_msg;
669 success = false;
670 break;
671 }
672 if (dex->IsCompactDexFile()) {
673 os <<"CompactDex conversion to StandardDex failed";
674 success = false;
675 break;
676 }
677
Andreas Gampe9b031f72018-10-04 11:03:34 -0700678 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300679 success = false;
680 break;
681 }
682 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700683 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300684 success = false;
685 break;
686 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300687 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000688 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700689 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700690 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000691
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800692 {
693 os << "OAT FILE STATS:\n";
694 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100695 stats_.AddBytes(oat_file_.Size());
696 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800697 }
698
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700699 os << std::flush;
700 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700701 }
702
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800703 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700704 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
705 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800706 return 0; // Address not in oat file
707 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800708 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
709 reinterpret_cast<uintptr_t>(oat_file_.Begin());
710 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800711 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800712 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800713 return end_offset - begin_offset;
714 }
715
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800716 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700717 return oat_file_.GetOatHeader().GetInstructionSet();
718 }
719
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700720 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800721 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700722 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700723 CHECK(oat_dex_file != nullptr);
724 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700725 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
726 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700727 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
728 << "': " << error_msg;
729 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800730 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800731 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700732 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700733 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700734 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100735 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Vladimir Markoe00e2012019-05-14 14:42:02 +0100736 uint32_t oat_method_index;
737 if (m->IsStatic() || m->IsDirect()) {
738 // Simple case where the oat method index was stashed at load time.
739 oat_method_index = m->GetMethodIndex();
740 } else {
741 // Compute the oat_method_index by search for its position in the class def.
742 ClassAccessor accessor(*dex_file, *class_def);
743 oat_method_index = accessor.NumDirectMethods();
744 bool found_virtual = false;
745 for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
746 // Check method index instead of identity in case of duplicate method definitions.
747 if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
748 found_virtual = true;
749 break;
750 }
751 ++oat_method_index;
752 }
753 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
754 << dex_file->PrettyMethod(m->GetDexMethodIndex());
755 }
756 return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800757 }
758 }
759 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700760 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800761 }
762
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300763 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
764 // files are fully unquickened and stored in dex_files
765 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
766 /* out */ DexFileUniqV* dex_files,
767 /* out */ std::string* error_msg) {
768 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
769 if (file == nullptr) {
770 *error_msg = "Could not open file " + vdex_filename + " for reading.";
771 return nullptr;
772 }
773
774 int64_t vdex_length = file->GetLength();
775 if (vdex_length == -1) {
776 *error_msg = "Could not read the length of file " + vdex_filename;
777 return nullptr;
778 }
779
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100780 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300781 file->GetLength(),
782 PROT_READ | PROT_WRITE,
783 MAP_PRIVATE,
784 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700785 /* start offset= */ 0,
786 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300787 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100788 error_msg);
789 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300790 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
791 return nullptr;
792 }
793
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100794 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300795 if (!vdex_file->IsValid()) {
796 *error_msg = "Vdex file is not valid";
797 return nullptr;
798 }
799
800 DexFileUniqV tmp_dex_files;
801 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
802 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
803 return nullptr;
804 }
805
806 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700807 /* decompile_return_instruction= */ true);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300808
809 *dex_files = std::move(tmp_dex_files);
810 return vdex_file;
811 }
812
David Srbecky86decb62018-06-05 06:41:10 +0100813 bool AddStatsObject(const void* address) {
814 return seen_stats_objects_.insert(address).second; // Inserted new entry.
815 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800816
David Srbecky86decb62018-06-05 06:41:10 +0100817 void DumpStats(VariableIndentationOutputStream& os,
818 const std::string& name,
819 const Stats& stats,
820 double total) {
821 if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
822 double percent = 100.0 * stats.Value() / total;
823 os.Stream()
824 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
825 << std::setw(8) << stats.Count() << " "
826 << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
827 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
828
829 // Sort all children by largest value first, than by name.
830 std::map<std::pair<double, std::string>, const Stats&> sorted_children;
831 for (const auto& it : stats.Children()) {
832 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800833 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800834
David Srbecky86decb62018-06-05 06:41:10 +0100835 // Add "other" row to represent any amount not account for by the children.
836 Stats other;
837 other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
838 if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
839 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800840 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800841
David Srbecky86decb62018-06-05 06:41:10 +0100842 // Print the data.
843 ScopedIndentation indent1(&os);
844 for (const auto& it : sorted_children) {
845 DumpStats(os, it.first.second, it.second, total);
846 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800847 }
David Srbecky86decb62018-06-05 06:41:10 +0100848 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800849
Brian Carlstromaded5f72011-10-07 17:15:04 -0700850 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800851 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800852 // We don't know the length of the code for each method, but we need to know where to stop
853 // when disassembling. What we do know is that a region of code will be followed by some other
854 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
855 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800856 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700857 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700858 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700859 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700860 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
861 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700862 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
863 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800864 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800865 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800866 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700867 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700868 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700869 for (uint32_t class_method_index = 0;
870 class_method_index < accessor.NumMethods();
871 ++class_method_index) {
872 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800873 }
874 }
875 }
876
877 // If the last thing in the file is code for a method, there won't be an offset for the "next"
878 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
879 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800880 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800881 }
882
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700883 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
884 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
885 }
886
Elliott Hughese3c845c2012-02-28 17:23:01 -0800887 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800888 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000889 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800890 code_offset &= ~0x1;
891 }
892 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800893 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800894 }
895
Andreas Gampeb40d3612018-06-26 15:49:42 -0700896 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800898 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700899 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800900 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800901 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700902
Andreas Gampe2ba88952016-04-29 17:52:07 -0700903 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000904 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
905 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100906
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000907 // Print data range of the dex file embedded inside the corresponding vdex file.
908 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
909 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
910 os << StringPrintf(
911 "dex-file: 0x%08x..0x%08x\n",
912 dex_offset,
913 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
914 } else {
915 os << StringPrintf("dex-file not in VDEX file\n");
916 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700917
Andreas Gampe2ba88952016-04-29 17:52:07 -0700918 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700919 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700920 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
921 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700922 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700923 os << std::flush;
924 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700925 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926
Andreas Gampe2ba88952016-04-29 17:52:07 -0700927 // Print lookup table, if it exists.
928 if (oat_dex_file.GetLookupTableData() != nullptr) {
929 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
930 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700931 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700932 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
933 table_offset,
934 table_offset + table_size - 1);
935 }
936
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100937 VariableIndentationOutputStream vios(&os);
938 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700939 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800940 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700941 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800942 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
943 continue;
944 }
945
Mathieu Chartier98dad402018-06-04 18:00:12 -0700946 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700947 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100948 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700950 static_cast<ssize_t>(class_def_index),
951 descriptor,
952 oat_class_offset,
953 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100954 << " (" << oat_class.GetStatus() << ")"
955 << " (" << oat_class.GetType() << ")\n";
956 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700957 if (options_.list_classes_) {
958 continue;
959 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700960 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 success = false;
962 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800963 if (stop_analysis) {
964 os << std::flush;
965 return success;
966 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700967 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700968 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700969 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700971 }
972
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300973 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
974 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
975 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
976 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300977 bool ExportDexFile(std::ostream& os,
978 const OatDexFile& oat_dex_file,
979 const DexFile* dex_file,
980 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800981 std::string error_msg;
982 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300983
984 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
985 // same as the one extracted from the Oat container (pre-oreo)
986 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800987
988 // Some quick checks just in case
989 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
990 os << "Invalid dex file\n";
991 return false;
992 }
993
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300994 if (dex_file == nullptr) {
995 // Exported bytecode is quickened (dex-to-dex transformations present)
996 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
997 if (dex_file == nullptr) {
998 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
999 return false;
1000 }
1001
1002 // Recompute checksum
1003 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
1004 dex_file->CalculateChecksum();
1005 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +03001006 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
1007 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
1008 if (!used_dexlayout) {
1009 // Vdex unquicken output should match original input bytecode
1010 uint32_t orig_checksum =
1011 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
1012 if (orig_checksum != dex_file->CalculateChecksum()) {
1013 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1014 return false;
1015 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001016 }
1017 }
1018
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001019 // Verify output directory exists
1020 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1021 // TODO: Extend OS::DirectoryExists if symlink support is required
1022 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1023 return false;
1024 }
1025
1026 // Beautify path names
1027 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1028 return false;
1029 }
1030
1031 std::string dex_orig_name;
1032 size_t dex_orig_pos = dex_file_location.rfind('/');
1033 if (dex_orig_pos == std::string::npos)
1034 dex_orig_name = dex_file_location;
1035 else
1036 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1037
1038 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001039 if (dex_orig_name.size() == 8 &&
1040 dex_orig_name.compare("base.apk") == 0 &&
1041 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001042 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1043 size_t apk_orig_pos = dex_file_location.rfind('/');
1044 if (apk_orig_pos != std::string::npos) {
1045 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1046 }
1047 }
1048
1049 std::string out_dex_path(options_.export_dex_location_);
1050 if (out_dex_path.back() != '/') {
1051 out_dex_path.append("/");
1052 }
1053 out_dex_path.append(dex_orig_name);
1054 out_dex_path.append("_export.dex");
1055 if (out_dex_path.length() > PATH_MAX) {
1056 return false;
1057 }
1058
1059 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1060 if (file.get() == nullptr) {
1061 os << "Failed to open output dex file " << out_dex_path;
1062 return false;
1063 }
1064
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001065 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001066 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001067 os << "Failed to write dex file";
1068 file->Erase();
1069 return false;
1070 }
1071
1072 if (file->FlushCloseOrErase() != 0) {
1073 os << "Flush and close failed";
1074 return false;
1075 }
1076
1077 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1078 os << std::flush;
1079
1080 return true;
1081 }
1082
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001083 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001084 const OatFile::OatClass& oat_class,
1085 const DexFile& dex_file,
1086 const ClassAccessor& class_accessor,
1087 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001089 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001090 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001091 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1092 if (!DumpOatMethod(vios,
1093 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1094 class_method_index,
1095 oat_class,
1096 dex_file,
1097 method.GetIndex(),
1098 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001099 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001100 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001101 success = false;
1102 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001103 if (addr_found) {
1104 *stop_analysis = true;
1105 return success;
1106 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001107 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001108 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001109 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001111 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001112
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 static constexpr uint32_t kPrologueBytes = 16;
1114
1115 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1116 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1117
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001118 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001119 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001120 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001121 const OatFile::OatClass& oat_class,
1122 const DexFile& dex_file,
1123 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001124 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001125 uint32_t method_access_flags,
1126 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001127 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001128
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001129 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001130
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001131 // TODO: Support regex
1132 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1133 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001134 return success;
1135 }
1136
David Sehr709b0702016-10-13 09:12:37 -07001137 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001138 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1139 class_method_index, pretty_method.c_str(),
1140 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001141 if (options_.list_methods_) {
1142 return success;
1143 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001144
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001145 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1146 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1147 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1148 uint32_t code_offset = oat_method.GetCodeOffset();
1149 uint32_t code_size = oat_method.GetQuickCodeSize();
1150 if (resolved_addr2instr_ != 0) {
1151 if (resolved_addr2instr_ > code_offset + code_size) {
1152 return success;
1153 } else {
1154 *addr_found = true; // stop analyzing file at next iteration
1155 }
1156 }
1157
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001158 // Everything below is indented at least once.
1159 ScopedIndentation indent1(vios);
1160
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001161 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001162 vios->Stream() << "DEX CODE:\n";
1163 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001164 if (code_item_accessor.HasCodeItem()) {
1165 for (const DexInstructionPcPair& inst : code_item_accessor) {
1166 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1167 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1168 }
1169 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001170 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001171
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001172 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001173 std::unique_ptr<verifier::MethodVerifier> verifier;
1174 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001175 // We need to have the handle scope stay live until after the verifier since the verifier has
1176 // a handle to the dex cache from hs.
1177 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001178 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1179 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001180 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001181 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001182 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001183 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001184 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001185 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001186 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001187 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001188 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001189 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001190 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001191 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001192 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1193 oat_method_offsets_offset, oat_file_.Size());
1194 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001195 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001196 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001197 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001199 ScopedIndentation indent2(vios);
1200 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001201 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1202 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001203 vios->Stream() << StringPrintf("WARNING: "
1204 "code offset 0x%08x is past end of file 0x%08zx.\n",
1205 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001206 success = false;
1207 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001209 }
1210 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001211 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001212 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1213 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001214 if (AddStatsObject(method_header)) {
1215 stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1216 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001217 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001219 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001221 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001222 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001223 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1224 method_header_offset, oat_file_.Size());
1225 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001226 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227 return false;
1228 }
1229
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001230 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001232 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001233 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001235 uint32_t vmap_table_offset = method_header ==
1236 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001238
1239 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001240 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001241 ? oat_file_.GetVdexFile()->Size()
1242 : method_header->GetCode() - oat_file_.Begin();
1243 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 vios->Stream() << StringPrintf("WARNING: "
1245 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1246 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001247 vmap_table_offset,
1248 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001249 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001250 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001251 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001252 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001254 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001255 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001256 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001257
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001258 ScopedIndentation indent2(vios);
1259 vios->Stream()
1260 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1261 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1262 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1263 vios->Stream() << "\n";
1264 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1265 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1266 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001267 }
1268 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001269 // Based on spill masks from QuickMethodFrameInfo so placed
1270 // after it is dumped, but useful for understanding quick
1271 // code, so dumped here.
1272 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001273 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 }
1275 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001276 vios->Stream() << "CODE: ";
Nicolas Geoffray57083762019-03-05 09:24:45 +00001277 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1278 if (code_size_offset > oat_file_.Size()) {
1279 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001280 vios->Stream() << StringPrintf("WARNING: "
Nicolas Geoffray57083762019-03-05 09:24:45 +00001281 "code size offset 0x%08x is past end of file 0x%08zx.",
1282 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001283 success = false;
Nicolas Geoffray57083762019-03-05 09:24:45 +00001284 } else {
1285 const void* code = oat_method.GetQuickCode();
1286 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1287 uint64_t aligned_code_end = aligned_code_begin + code_size;
1288 if (AddStatsObject(code)) {
1289 stats_.Child("Code")->AddBytes(code_size);
1290 }
1291
1292 if (options_.absolute_addresses_) {
1293 vios->Stream() << StringPrintf("%p ", code);
1294 }
1295 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1296 code_offset,
1297 code_size_offset,
1298 code_size,
1299 code != nullptr ? "..." : "");
1300
1301 ScopedIndentation indent2(vios);
1302 if (aligned_code_begin > oat_file_.Size()) {
1303 vios->Stream() << StringPrintf("WARNING: "
1304 "start of code at 0x%08x is past end of file 0x%08zx.",
1305 aligned_code_begin, oat_file_.Size());
1306 success = false;
1307 } else if (aligned_code_end > oat_file_.Size()) {
1308 vios->Stream() << StringPrintf(
1309 "WARNING: "
1310 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1311 "code size is 0x%08x loaded from offset 0x%08x.\n",
1312 aligned_code_end, oat_file_.Size(),
1313 code_size, code_size_offset);
1314 success = false;
1315 if (options_.disassemble_code_) {
1316 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1317 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1318 }
1319 }
1320 } else if (code_size > kMaxCodeSize) {
1321 vios->Stream() << StringPrintf(
1322 "WARNING: "
1323 "code size %d is bigger than max expected threshold of %d. "
1324 "code size is 0x%08x loaded from offset 0x%08x.\n",
1325 code_size, kMaxCodeSize,
1326 code_size, code_size_offset);
1327 success = false;
1328 if (options_.disassemble_code_) {
1329 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1330 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1331 }
1332 }
1333 } else if (options_.disassemble_code_) {
1334 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1335 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001336 }
1337 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001338 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001339 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001340 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001341
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001342 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1343 if (spill_mask == 0) {
1344 return;
1345 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001346 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001347 for (size_t i = 0; i < 32; i++) {
1348 if ((spill_mask & (1 << i)) != 0) {
1349 if (is_float) {
1350 os << "fr" << i;
1351 } else {
1352 os << "r" << i;
1353 }
1354 spill_mask ^= 1 << i; // clear bit
1355 if (spill_mask != 0) {
1356 os << ", ";
1357 } else {
1358 break;
1359 }
1360 }
1361 }
1362 os << ")";
1363 }
1364
Roland Levillain442b46a2015-02-18 16:54:21 +00001365 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001366 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001367 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001368 const CodeItemDataAccessor& code_item_accessor) {
1369 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001370 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001371 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001372 if (raw_code_info != nullptr) {
1373 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001374 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001376 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001377 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001378 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001379 // We don't encode the size in the table, so just emit that we have quickened
1380 // information.
1381 ScopedIndentation indent(vios);
1382 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001383 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001384 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001385 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001386 }
1387
1388 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001389 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001390 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001391 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001392 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001393 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001394 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001395 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001396 }
1397
Andreas Gampe36a296f2017-06-13 14:11:11 -07001398 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1399 // According to stack model, the first out is above the Method referernce.
1400 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1401 }
1402
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001403 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001404 uint32_t core_spills,
1405 uint32_t fp_spills,
1406 size_t frame_size,
1407 int reg,
1408 InstructionSet isa) {
1409 PointerSize pointer_size = InstructionSetPointerSize(isa);
1410 if (kIsDebugBuild) {
1411 auto* runtime = Runtime::Current();
1412 if (runtime != nullptr) {
1413 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1414 }
1415 }
1416 DCHECK_ALIGNED(frame_size, kStackAlignment);
1417 DCHECK_NE(reg, -1);
1418 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1419 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1420 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001421 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1422 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001423 const int max_num_special_temps = 1;
1424 if (reg == temp_threshold) {
1425 // The current method pointer corresponds to special location on stack.
1426 return 0;
1427 } else if (reg >= temp_threshold + max_num_special_temps) {
1428 /*
1429 * Special temporaries may have custom locations and the logic above deals with that.
1430 * However, non-special temporaries are placed relative to the outs.
1431 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001432 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001433 + static_cast<size_t>(pointer_size) /* art method */;
1434 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1435 return temps_start + relative_offset;
1436 } else if (reg < num_regs) {
1437 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1438 return locals_start + (reg * sizeof(uint32_t));
1439 } else {
1440 // Handle ins.
1441 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1442 + static_cast<size_t>(pointer_size) /* art method */;
1443 }
1444 }
1445
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001446 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001447 const CodeItemDataAccessor& code_item_accessor) {
1448 if (code_item_accessor.HasCodeItem()) {
1449 size_t num_locals_ins = code_item_accessor.RegistersSize();
1450 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001451 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001452 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001453
1454 os << "vr_stack_locations:";
1455 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1456 // For readability, delimit the different kinds of VRs.
1457 if (reg == num_locals_ins) {
1458 os << "\n\tmethod*:";
1459 } else if (reg == num_locals && num_ins > 0) {
1460 os << "\n\tins:";
1461 } else if (reg == 0 && num_locals > 0) {
1462 os << "\n\tlocals:";
1463 }
1464
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001465 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001466 oat_method.GetCoreSpillMask(),
1467 oat_method.GetFpSpillMask(),
1468 oat_method.GetFrameSizeInBytes(),
1469 reg,
1470 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001471 os << " v" << reg << "[sp + #" << offset << "]";
1472 }
1473
1474 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1475 if (out_reg == 0) {
1476 os << "\n\touts:";
1477 }
1478
Andreas Gampe36a296f2017-06-13 14:11:11 -07001479 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001480 os << " v" << out_reg << "[sp + #" << offset << "]";
1481 }
1482
1483 os << "\n";
1484 }
1485 }
1486
Roland Levillainf2650d12015-05-28 14:53:28 +01001487 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1488 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001489 static bool IsMethodGeneratedByOptimizingCompiler(
1490 const OatFile::OatMethod& oat_method,
1491 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001492 // If the native GC map is null and the Dex `code_item` is not
1493 // null, then this method has been compiled with the optimizing
1494 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001495 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001496 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001497 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001498 }
1499
1500 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1501 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001502 static bool IsMethodGeneratedByDexToDexCompiler(
1503 const OatFile::OatMethod& oat_method,
1504 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001505 // If the quick code is null, the Dex `code_item` is not
1506 // null, and the vmap table is not null, then this method has been compiled
1507 // with the dextodex compiler.
1508 return oat_method.GetQuickCode() == nullptr &&
1509 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001510 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001511 }
1512
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001513 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001514 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001515 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001516 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001517 const dex::ClassDef& class_def,
1518 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001519 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001520 if ((method_access_flags & kAccNative) == 0) {
1521 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001522 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001523 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001524 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1525 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1526 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001527 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1528 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001529 if (method == nullptr) {
1530 soa.Self()->ClearException();
1531 return nullptr;
1532 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001533 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001534 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001535 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001536 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001537
1538 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 }
1540
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001541 // The StackMapsHelper provides the stack maps in the native PC order.
1542 // For identical native PCs, the order from the CodeInfo is preserved.
1543 class StackMapsHelper {
1544 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001545 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001546 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001547 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001548 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001549 offset_(static_cast<uint32_t>(-1)),
1550 stack_map_index_(0u),
1551 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001552 if (number_of_stack_maps_ != 0u) {
1553 // Check if native PCs are ordered.
1554 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001555 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001556 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001557 StackMap current = code_info_.GetStackMapAt(i);
1558 if (last.GetNativePcOffset(instruction_set) >
1559 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001560 ordered = false;
1561 break;
1562 }
1563 last = current;
1564 }
1565 if (!ordered) {
1566 // Create indirection indexes for access in native PC order. We do not optimize
1567 // for the fact that there can currently be only two separately ordered ranges,
1568 // namely normal stack maps and catch-point stack maps.
1569 indexes_.resize(number_of_stack_maps_);
1570 std::iota(indexes_.begin(), indexes_.end(), 0u);
1571 std::sort(indexes_.begin(),
1572 indexes_.end(),
1573 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001574 StackMap left = code_info_.GetStackMapAt(lhs);
1575 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1576 StackMap right = code_info_.GetStackMapAt(rhs);
1577 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001578 // If the PCs are the same, compare indexes to preserve the original order.
1579 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1580 });
1581 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001582 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001583 }
1584 }
1585
1586 const CodeInfo& GetCodeInfo() const {
1587 return code_info_;
1588 }
1589
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001590 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001591 return offset_;
1592 }
1593
1594 StackMap GetStackMap() const {
1595 return GetStackMapAt(stack_map_index_);
1596 }
1597
1598 void Next() {
1599 ++stack_map_index_;
1600 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001601 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001602 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001603 }
1604
1605 private:
1606 StackMap GetStackMapAt(size_t i) const {
1607 if (!indexes_.empty()) {
1608 i = indexes_[i];
1609 }
1610 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001611 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001612 }
1613
1614 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001615 const size_t number_of_stack_maps_;
1616 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001617 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001618 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001619 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001620 };
1621
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001622 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001623 const OatFile::OatMethod& oat_method,
1624 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001625 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001626 const void* quick_code = oat_method.GetQuickCode();
1627
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001628 if (code_size == 0) {
1629 code_size = oat_method.GetQuickCodeSize();
1630 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001631 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001632 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001633 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001634 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1635 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001636 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001637 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
David Srbecky86decb62018-06-05 06:41:10 +01001638 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky42deda82018-08-10 11:23:27 +01001639 helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
David Srbecky86decb62018-06-05 06:41:10 +01001640 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001641 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1642 size_t offset = 0;
1643 while (offset < code_size) {
1644 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1645 if (offset == helper.GetOffset()) {
1646 ScopedIndentation indent1(vios);
1647 StackMap stack_map = helper.GetStackMap();
1648 DCHECK(stack_map.IsValid());
1649 stack_map.Dump(vios,
1650 helper.GetCodeInfo(),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001651 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001652 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001653 do {
1654 helper.Next();
1655 // There may be multiple stack maps at a given PC. We display only the first one.
1656 } while (offset == helper.GetOffset());
1657 }
1658 DCHECK_LT(offset, helper.GetOffset());
1659 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001660 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001661 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1662 size_t offset = 0;
1663 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001664 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001665 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001666 }
1667 }
1668
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001669 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1670 REQUIRES_SHARED(Locks::mutator_lock_) {
1671 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1672 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1673 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1674 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1675 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1676 DCHECK(boot_image_live_objects != nullptr);
1677 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1678 const uint8_t* boot_image_live_objects_address =
1679 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1680 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1681 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1682 boot_image_live_objects->GetLength()).Uint32Value();
1683 return std::make_pair(boot_image_live_objects_address + begin_offset,
1684 boot_image_live_objects_address + end_offset);
1685 }
1686
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001687 void DumpDataBimgRelRoEntries(std::ostream& os) {
1688 os << ".data.bimg.rel.ro: ";
1689 if (oat_file_.GetBootImageRelocations().empty()) {
1690 os << "empty.\n\n";
1691 return;
1692 }
1693
1694 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1695 Runtime* runtime = Runtime::Current();
1696 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1697 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1698 runtime->GetHeap()->GetBootImageSpaces();
1699 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001700 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1701 const uint8_t* live_objects_begin = live_objects.first;
1702 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001703 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1704 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1705 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1706 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001707 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001708 bool found = false;
1709 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001710 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001711 if (local_offset < space->GetImageHeader().GetImageSize()) {
1712 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001713 if (address >= live_objects_begin && address < live_objects_end) {
1714 size_t index =
1715 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1716 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001717 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001718 index);
1719 } else {
1720 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1721 if (o->IsString()) {
1722 os << " String: " << o->AsString()->ToModifiedUtf8();
1723 } else if (o->IsClass()) {
1724 os << " Class: " << o->AsClass()->PrettyDescriptor();
1725 } else {
1726 os << StringPrintf(" 0x%08x %s",
1727 object_offset,
1728 o->GetClass()->PrettyDescriptor().c_str());
1729 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001730 }
1731 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001732 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001733 os << " ArtMethod: " << m->PrettyMethod();
1734 } else {
1735 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1736 object_offset,
1737 space->GetImageFilename().c_str());
1738 }
1739 found = true;
1740 break;
1741 }
1742 }
1743 if (!found) {
1744 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1745 }
1746 os << "\n";
1747 }
1748 } else {
1749 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1750 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1751 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1752 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1753 }
1754 }
1755 os << "\n";
1756 }
1757
Vladimir Markof3c52b42017-11-17 17:32:12 +00001758 template <typename NameGetter>
1759 void DumpBssEntries(std::ostream& os,
1760 const char* slot_type,
1761 const IndexBssMapping* mapping,
1762 uint32_t number_of_indexes,
1763 size_t slot_size,
1764 NameGetter name) {
1765 os << ".bss mapping for " << slot_type << ": ";
1766 if (mapping == nullptr) {
1767 os << "empty.\n";
1768 return;
1769 }
1770 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1771 size_t num_valid_indexes = 0u;
1772 for (const IndexBssMappingEntry& entry : *mapping) {
1773 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1774 }
1775 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1776 os << std::hex;
1777 for (const IndexBssMappingEntry& entry : *mapping) {
1778 uint32_t index = entry.GetIndex(index_bits);
1779 uint32_t mask = entry.GetMask(index_bits);
1780 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1781 for (uint32_t n : LowToHighBits(mask)) {
1782 size_t current_index = index - (32u - index_bits) + n;
1783 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1784 bss_offset += slot_size;
1785 }
1786 DCHECK_EQ(bss_offset, entry.bss_offset);
1787 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1788 }
1789 os << std::dec;
1790 }
1791
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001792 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001793 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001794 const OatDumperOptions& options_;
1795 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001796 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001798 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001799 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001800 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001801};
1802
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001803class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001804 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001805 ImageDumper(std::ostream* os,
1806 gc::space::ImageSpace& image_space,
1807 const ImageHeader& image_header,
1808 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001809 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001810 vios_(os),
1811 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001812 image_space_(image_space),
1813 image_header_(image_header),
1814 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001815
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001816 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001817 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001818 std::ostream& indent_os = vios_.Stream();
1819
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001820 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001821
Jeff Haodcdc85b2015-12-04 14:06:18 -08001822 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1823
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001824 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1825 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1826 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001827
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001828 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1829 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1830 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1831 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1832 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1833
1834 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1835 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001836 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001837
1838 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1839 auto section = static_cast<ImageHeader::ImageSections>(i);
1840 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1841 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001842
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001843 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001844 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001845 static_assert(arraysize(image_roots_descriptions_) ==
1846 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001847 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1848 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001849 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1850 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001851 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1852 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001853 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001854 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001855 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001856 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001857 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001858 ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001859 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001860 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1861 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001862 run++;
1863 } else {
1864 break;
1865 }
1866 }
1867 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001868 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001869 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001870 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001871 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001872 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001873 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001874 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001876 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001878 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001879 }
1880 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001881 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001882
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001883 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001884 os << "METHOD ROOTS\n";
1885 static_assert(arraysize(image_methods_descriptions_) ==
1886 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1887 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1888 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1889 const char* description = image_methods_descriptions_[i];
1890 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001891 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001892 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001893 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001895
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001896 Runtime* const runtime = Runtime::Current();
1897 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001898 std::string image_filename = image_space_.GetImageFilename();
1899 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001900 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001901 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001902 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001903 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001904 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001905 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1906 }
1907 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001908 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001909 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001910 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001911 /*executable=*/ false,
1912 /*low_4gb=*/ false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001913 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001914 }
1915 if (oat_file == nullptr) {
1916 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1917 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001918 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001920
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001921 stats_.oat_file_bytes = oat_file->Size();
1922
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001923 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001924
Andreas Gampeb40d3612018-06-26 15:49:42 -07001925 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001926 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001927 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1928 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001929 }
1930
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001932
Jeff Haodcdc85b2015-12-04 14:06:18 -08001933 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001934 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001935 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001936 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001937 {
1938 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1939 heap->FlushAllocStack();
1940 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001941 // Since FlushAllocStack() above resets the (active) allocation
1942 // stack. Need to revoke the thread-local allocation stacks that
1943 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001944 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001945 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001946 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001947 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001948 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001949 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001950 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001951 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001952 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001953 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001954 ObjPtr<mirror::DexCache> dex_cache =
1955 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001956 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001957 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001958 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001959 }
1960 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001961 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1962 DumpObject(obj);
1963 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001964 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001965 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001966 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001967 indent_os << "\n";
1968 // TODO: Dump fields.
1969 // Dump methods after.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -07001970 image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1971 REQUIRES_SHARED(Locks::mutator_lock_) {
1972 std::ostream& indent_os = vios_.Stream();
1973 indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1974 DumpMethod(&method, indent_os);
1975 indent_os << "\n";
1976 }, image_space_.Begin(), image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001977 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001978 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001979 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001980 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001981 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001982 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001983 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1984 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001985 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001986 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001987 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001988 // If the image is compressed, adjust to decompressed size.
1989 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartiera5c01d72019-07-26 14:39:28 -07001990 if (!image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001991 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1992 }
1993 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001994 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001995 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001996 const auto& object_section = image_header_.GetObjectsSection();
1997 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001998 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001999 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2000 const auto& intern_section = image_header_.GetInternedStringsSection();
2001 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002002 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
2003 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002004 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002005
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002006 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002007
2008 // Objects are kObjectAlignment-aligned.
2009 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2010 if (object_section.Offset() > header_bytes) {
2011 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2012 }
2013
2014 // Field section is 4-byte aligned.
2015 constexpr size_t kFieldSectionAlignment = 4U;
2016 uint32_t end_objects = object_section.Offset() + object_section.Size();
2017 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2018 stats_.alignment_bytes += field_section.Offset() - end_objects;
2019
2020 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2021 uint32_t end_fields = field_section.Offset() + field_section.Size();
2022 CHECK_ALIGNED(method_section.Offset(), 4);
2023 stats_.alignment_bytes += method_section.Offset() - end_fields;
2024
2025 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2026 uint32_t end_methods = method_section.Offset() + method_section.Size();
2027 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2028 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2029
2030 // Intern table is 8-byte aligned.
2031 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002032 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002033 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2034
2035 // Add space between intern table and class table.
2036 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2037 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2038
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002039 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2040 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002041 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002042 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002043
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 stats_.bitmap_bytes += bitmap_section.Size();
2045 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002046 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002047 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002048 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002049 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002050 stats_.sro_offset_bytes += sro_section.Size();
2051 stats_.metadata_bytes += metadata_section.Size();
2052
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002053 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002054 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002055
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002056 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002057
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002058 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002059 }
2060
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002061 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -07002062 static void PrettyObjectValue(std::ostream& os,
2063 ObjPtr<mirror::Class> type,
2064 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002065 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002066 CHECK(type != nullptr);
2067 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002068 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002069 } else if (type->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002070 ObjPtr<mirror::String> string = value->AsString();
2071 os << StringPrintf("%p String: %s\n",
2072 string.Ptr(),
Ian Rogers68b56852014-08-29 20:19:11 -07002073 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002074 } else if (type->IsClassClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002075 ObjPtr<mirror::Class> klass = value->AsClass();
2076 os << StringPrintf("%p Class: %s\n",
2077 klass.Ptr(),
2078 mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002079 } else {
David Sehr709b0702016-10-13 09:12:37 -07002080 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002081 }
2082 }
2083
Mathieu Chartier3398c782016-09-30 10:27:43 -07002084 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002085 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002086 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002087 switch (field->GetTypeAsPrimitiveType()) {
2088 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002089 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002090 break;
2091 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002092 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002093 break;
2094 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002095 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002096 break;
2097 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002098 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002099 break;
2100 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002101 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002102 break;
2103 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002104 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002105 break;
2106 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002107 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002108 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002109 break;
2110 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002111 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002112 break;
2113 case Primitive::kPrimNot: {
2114 // Get the value, don't compute the type unless it is non-null as we don't want
2115 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002116 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002117 if (value == nullptr) {
2118 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002119 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002120 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002121 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002122 if (field_type != nullptr) {
2123 PrettyObjectValue(os, field_type, value);
2124 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002125 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002126 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002127 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2128 }
Ian Rogers50239c72013-06-17 14:53:22 -07002129 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002130 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002131 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002132 default:
2133 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2134 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002135 }
2136 }
2137
Andreas Gampe98104992018-10-16 12:49:47 -07002138 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002139 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002140 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002141 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002142 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002143 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002144 for (ArtField& field : klass->GetIFields()) {
2145 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002146 }
2147 }
2148
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002149 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002150 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002151 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002152
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002153 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002154 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002155 image_header_.GetPointerSize());
David Srbeckya54e0cc2019-04-15 15:03:26 +01002156 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2157 if (class_linker->IsQuickResolutionStub(quick_code) ||
2158 class_linker->IsQuickToInterpreterBridge(quick_code) ||
2159 class_linker->IsQuickGenericJniStub(quick_code) ||
Vladimir Markofa458ac2020-02-12 14:08:07 +00002160 class_linker->IsJniDlsymLookupStub(quick_code) ||
2161 class_linker->IsJniDlsymLookupCriticalStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002162 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002163 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002164 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002165 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002166 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002167 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002168 }
2169
Mathieu Chartiere401d142015-04-22 13:56:20 -07002170 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002171 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002172 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2173 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002174 return 0;
2175 }
David Srbeckya54e0cc2019-04-15 15:03:26 +01002176 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2177 reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2178 return method_header->GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002179 }
2180
Mathieu Chartiere401d142015-04-22 13:56:20 -07002181 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002182 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002183 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002184 if (oat_code_begin == nullptr) {
2185 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002186 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002187 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002188 }
2189
Andreas Gampe0c183382017-07-13 22:26:24 -07002190 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002191 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002192 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002193 return;
2194 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002195
2196 size_t object_bytes = obj->SizeOf();
2197 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002198 stats_.object_bytes += object_bytes;
2199 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002200
Andreas Gampe0c183382017-07-13 22:26:24 -07002201 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002202
Vladimir Marko4617d582019-03-28 13:48:31 +00002203 ObjPtr<mirror::Class> obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002204 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002205 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002206 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002207 } else if (obj->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002208 ObjPtr<mirror::Class> klass = obj->AsClass();
2209 os << StringPrintf("%p: java.lang.Class \"%s\" (",
2210 obj,
David Sehr709b0702016-10-13 09:12:37 -07002211 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002212 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002213 } else if (obj_class->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002214 os << StringPrintf("%p: java.lang.String %s\n",
2215 obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002216 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002217 } else {
David Sehr709b0702016-10-13 09:12:37 -07002218 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002219 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002220 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002221 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002222 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002223 if (obj->IsObjectArray()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002224 ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002225 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00002226 ObjPtr<mirror::Object> value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002227 size_t run = 0;
2228 for (int32_t j = i + 1; j < length; j++) {
2229 if (value == obj_array->Get(j)) {
2230 run++;
2231 } else {
2232 break;
2233 }
2234 }
2235 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002236 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002237 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002238 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002239 i = i + run;
2240 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002241 ObjPtr<mirror::Class> value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002242 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002243 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002244 }
2245 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002246 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002247
Vladimir Marko305c38b2018-02-14 11:50:07 +00002248 if (kBitstringSubtypeCheckEnabled) {
2249 os << "SUBTYPE_CHECK_BITS: ";
2250 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2251 os << "\n";
2252 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002253
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002254 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002255 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002256 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002257 for (ArtField& field : klass->GetSFields()) {
2258 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002259 }
2260 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002261 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002262 auto it = dex_caches_.find(obj);
2263 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002264 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002265 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002266 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002267 size_t num_methods = dex_cache->NumResolvedMethods();
2268 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002269 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002270 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002271 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002272 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002273 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2274 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002275 size_t run = 0;
2276 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002277 j != length &&
2278 elem == mirror::DexCache::GetNativePairPtrSize(
2279 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002280 ++j) {
2281 ++run;
2282 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002283 if (run == 0) {
2284 os << StringPrintf("%zd: ", i);
2285 } else {
2286 os << StringPrintf("%zd to %zd: ", i, i + run);
2287 i = i + run;
2288 }
2289 std::string msg;
2290 if (elem == nullptr) {
2291 msg = "null";
2292 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002293 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002294 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002295 } else {
2296 msg = "<not in method section>";
2297 }
2298 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002299 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002300 }
2301 size_t num_fields = dex_cache->NumResolvedFields();
2302 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002303 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002304 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002305 auto* resolved_fields = dex_cache->GetResolvedFields();
2306 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002307 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002308 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002309 size_t run = 0;
2310 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002311 j != length &&
2312 elem == mirror::DexCache::GetNativePairPtrSize(
2313 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002314 ++j) {
2315 ++run;
2316 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002317 if (run == 0) {
2318 os << StringPrintf("%zd: ", i);
2319 } else {
2320 os << StringPrintf("%zd to %zd: ", i, i + run);
2321 i = i + run;
2322 }
2323 std::string msg;
2324 if (elem == nullptr) {
2325 msg = "null";
2326 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002327 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002328 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002329 } else {
2330 msg = "<not in field section>";
2331 }
2332 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002334 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002335 size_t num_types = dex_cache->NumResolvedTypes();
2336 if (num_types != 0u) {
2337 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002338 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002339 auto* resolved_types = dex_cache->GetResolvedTypes();
2340 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002341 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002342 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002343 for (size_t j = i + 1; j != num_types; ++j) {
2344 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2345 if (pair.index != other_pair.index ||
2346 pair.object.Read() != other_pair.object.Read()) {
2347 break;
2348 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002349 ++run;
2350 }
2351 if (run == 0) {
2352 os << StringPrintf("%zd: ", i);
2353 } else {
2354 os << StringPrintf("%zd to %zd: ", i, i + run);
2355 i = i + run;
2356 }
2357 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002358 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002359 if (elem == nullptr) {
2360 msg = "null";
2361 } else {
David Sehr709b0702016-10-13 09:12:37 -07002362 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002363 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002364 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002365 }
2366 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002367 }
2368 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002369 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002370 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002371 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002372
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002373 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002374 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002375 DCHECK(method != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002376 const PointerSize pointer_size = image_header_.GetPointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002377 if (method->IsNative()) {
David Srbeckya54e0cc2019-04-15 15:03:26 +01002378 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002379 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002380 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2381 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002383 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002384 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002385 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2386 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002387 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002388 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002389 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002390 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002391 } else if (method->IsRuntimeMethod()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002392 if (method == Runtime::Current()->GetResolutionMethod()) {
2393 const void* resolution_trampoline =
2394 method->GetEntryPointFromQuickCompiledCodePtrSize(image_header_.GetPointerSize());
2395 indent_os << StringPrintf("Resolution trampoline: %p\n", resolution_trampoline);
2396 const void* critical_native_resolution_trampoline =
2397 method->GetEntryPointFromJniPtrSize(image_header_.GetPointerSize());
2398 indent_os << StringPrintf("Resolution trampoline for @CriticalNative: %p\n",
2399 critical_native_resolution_trampoline);
2400 } else {
2401 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2402 if (table != nullptr) {
2403 indent_os << "IMT conflict table " << table << " method: ";
2404 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
2405 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2406 << " ";
2407 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002408 }
2409 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002411 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002412 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002413 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414
David Srbeckya54e0cc2019-04-15 15:03:26 +01002415 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2416 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2417
Mathieu Chartiere401d142015-04-22 13:56:20 -07002418 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002419 size_t vmap_table_bytes = 0u;
David Srbeckya54e0cc2019-04-15 15:03:26 +01002420 if (quick_oat_code_begin != nullptr) {
2421 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2422 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2423 vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2424 &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002425 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002426 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002427 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002428 }
2429
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002430 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2431 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002432 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002433 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002434 if (method->IsConstructor()) {
2435 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002436 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002437 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002438 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002439 }
2440 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002441 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002442 }
2443 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002444 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002445
Igor Murashkin7617abd2015-07-10 18:27:47 -07002446 uint32_t method_access_flags = method->GetAccessFlags();
2447
Mathieu Chartiere401d142015-04-22 13:56:20 -07002448 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002449 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2450 dex_instruction_bytes,
2451 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002452 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002453
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002454 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002455 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002456
2457 double expansion =
2458 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002459 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002460 }
2461 }
2462
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002463 std::set<const void*> already_seen_;
2464 // Compute the size of the given data within the oat file and whether this is the first time
2465 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002466 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002467 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002468 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002469 already_seen_.insert(oat_data);
2470 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002471 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002472 }
2473 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002474 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475
2476 public:
2477 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002478 size_t oat_file_bytes = 0u;
2479 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002480
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002481 size_t header_bytes = 0u;
2482 size_t object_bytes = 0u;
2483 size_t art_field_bytes = 0u;
2484 size_t art_method_bytes = 0u;
2485 size_t dex_cache_arrays_bytes = 0u;
2486 size_t interned_strings_bytes = 0u;
2487 size_t class_table_bytes = 0u;
2488 size_t sro_offset_bytes = 0u;
2489 size_t metadata_bytes = 0u;
2490 size_t bitmap_bytes = 0u;
2491 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002492
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002493 size_t managed_code_bytes = 0u;
2494 size_t managed_code_bytes_ignoring_deduplication = 0u;
2495 size_t native_to_managed_code_bytes = 0u;
2496 size_t class_initializer_code_bytes = 0u;
2497 size_t large_initializer_code_bytes = 0u;
2498 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002499
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002500 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002501
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002502 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002503
Mathieu Chartiere401d142015-04-22 13:56:20 -07002504 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002505 std::vector<size_t> method_outlier_size;
2506 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002507 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002508
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002509 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002510
Elliott Hughesa0e18062012-04-13 15:59:59 -07002511 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002512 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002513 size_t bytes;
2514 size_t count;
2515 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002516 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002517 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002518
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002519 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002520 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2521 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002522 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002523 it->second.count += 1;
2524 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002525 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002526 }
2527 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002528
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002529 double PercentOfOatBytes(size_t size) {
2530 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2531 }
2532
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002533 double PercentOfFileBytes(size_t size) {
2534 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2535 }
2536
2537 double PercentOfObjectBytes(size_t size) {
2538 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2539 }
2540
Mathieu Chartiere401d142015-04-22 13:56:20 -07002541 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002542 method_outlier_size.push_back(total_size);
2543 method_outlier_expansion.push_back(expansion);
2544 method_outlier.push_back(method);
2545 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002546
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002547 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002548 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002549 size_t sum_of_sizes = 0;
2550 size_t sum_of_sizes_squared = 0;
2551 size_t sum_of_expansion = 0;
2552 size_t sum_of_expansion_squared = 0;
2553 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002554 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002555 return;
2556 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002557 for (size_t i = 0; i < n; i++) {
2558 size_t cur_size = method_outlier_size[i];
2559 sum_of_sizes += cur_size;
2560 sum_of_sizes_squared += cur_size * cur_size;
2561 double cur_expansion = method_outlier_expansion[i];
2562 sum_of_expansion += cur_expansion;
2563 sum_of_expansion_squared += cur_expansion * cur_expansion;
2564 }
2565 size_t size_mean = sum_of_sizes / n;
2566 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2567 double expansion_mean = sum_of_expansion / n;
2568 double expansion_variance =
2569 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2570
2571 // Dump methods whose size is a certain number of standard deviations from the mean
2572 size_t dumped_values = 0;
2573 size_t skipped_values = 0;
2574 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2575 size_t cur_size_variance = i * i * size_variance;
2576 bool first = true;
2577 for (size_t j = 0; j < n; j++) {
2578 size_t cur_size = method_outlier_size[j];
2579 if (cur_size > size_mean) {
2580 size_t cur_var = cur_size - size_mean;
2581 cur_var = cur_var * cur_var;
2582 if (cur_var > cur_size_variance) {
2583 if (dumped_values > 20) {
2584 if (i == 1) {
2585 skipped_values++;
2586 } else {
2587 i = 2; // jump to counting for 1 standard deviation
2588 break;
2589 }
2590 } else {
2591 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002592 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002593 first = false;
2594 }
David Sehr709b0702016-10-13 09:12:37 -07002595 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002596 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002597 method_outlier_size[j] = 0; // don't consider this method again
2598 dumped_values++;
2599 }
2600 }
2601 }
2602 }
2603 }
2604 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002606 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002607 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002608 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002609
2610 // Dump methods whose expansion is a certain number of standard deviations from the mean
2611 dumped_values = 0;
2612 skipped_values = 0;
2613 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2614 double cur_expansion_variance = i * i * expansion_variance;
2615 bool first = true;
2616 for (size_t j = 0; j < n; j++) {
2617 double cur_expansion = method_outlier_expansion[j];
2618 if (cur_expansion > expansion_mean) {
2619 size_t cur_var = cur_expansion - expansion_mean;
2620 cur_var = cur_var * cur_var;
2621 if (cur_var > cur_expansion_variance) {
2622 if (dumped_values > 20) {
2623 if (i == 1) {
2624 skipped_values++;
2625 } else {
2626 i = 2; // jump to counting for 1 standard deviation
2627 break;
2628 }
2629 } else {
2630 if (first) {
2631 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002632 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002633 first = false;
2634 }
David Sehr709b0702016-10-13 09:12:37 -07002635 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002636 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002637 method_outlier_expansion[j] = 0.0; // don't consider this method again
2638 dumped_values++;
2639 }
2640 }
2641 }
2642 }
2643 }
2644 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002645 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002646 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002647 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002648 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002649 }
2650
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002651 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002652 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002653 {
2654 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2655 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002656 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2657 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2658 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2659 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2660 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2661 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002662 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002663 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2664 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002665 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2666 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002667 header_bytes, PercentOfFileBytes(header_bytes),
2668 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002669 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2670 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002671 dex_cache_arrays_bytes,
2672 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002673 interned_strings_bytes,
2674 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002675 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002676 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2677 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002678 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002679 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2680 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002681 CHECK_EQ(file_bytes,
2682 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2683 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002684 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002685 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002686
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002687 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002688 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002689 for (const auto& sizes_and_count : sizes_and_counts) {
2690 const std::string& descriptor(sizes_and_count.first);
2691 double average = static_cast<double>(sizes_and_count.second.bytes) /
2692 static_cast<double>(sizes_and_count.second.count);
2693 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002695 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002696 descriptor.c_str(), sizes_and_count.second.bytes,
2697 sizes_and_count.second.count, average, percent);
2698 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002699 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002700 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002701 CHECK_EQ(object_bytes, object_bytes_total);
2702
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 os << StringPrintf("oat_file_bytes = %8zd\n"
2704 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002705 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2706 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2707 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2708 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002709 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002710 managed_code_bytes,
2711 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002712 native_to_managed_code_bytes,
2713 PercentOfOatBytes(native_to_managed_code_bytes),
2714 class_initializer_code_bytes,
2715 PercentOfOatBytes(class_initializer_code_bytes),
2716 large_initializer_code_bytes,
2717 PercentOfOatBytes(large_initializer_code_bytes),
2718 large_method_code_bytes,
2719 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002720 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002721 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002723 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2724 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002725 }
2726
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002727 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002728 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002729 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002730
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002731 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2732 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002733 static_cast<double>(managed_code_bytes) /
2734 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002735 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002736 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002737 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002738
2739 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002740 }
2741 } stats_;
2742
2743 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002744 enum {
2745 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2746 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2747 kLargeConstructorDexBytes = 4000,
2748 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2749 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2750 kLargeMethodDexBytes = 16000
2751 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002752
2753 // For performance, use the *os_ directly for anything that doesn't need indentation
2754 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002755 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002756 VariableIndentationOutputStream vios_;
2757 ScopedIndentation indent1_;
2758
Ian Rogers1d54e732013-05-02 21:10:01 -07002759 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002760 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002761 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002762 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002763 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002764
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002765 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002766};
2767
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002768static int DumpImage(gc::space::ImageSpace* image_space,
2769 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002770 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002771 const ImageHeader& image_header = image_space->GetImageHeader();
2772 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002773 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002774 return EXIT_FAILURE;
2775 }
2776 ImageDumper image_dumper(os, *image_space, image_header, options);
2777 if (!image_dumper.Dump()) {
2778 return EXIT_FAILURE;
2779 }
2780 return EXIT_SUCCESS;
2781}
2782
2783static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002784 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002785 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002786 options->class_loader_ = &null_class_loader;
2787
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002788 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002789 if (options->app_image_ != nullptr) {
2790 if (options->app_oat_ == nullptr) {
2791 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002792 return EXIT_FAILURE;
2793 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002794 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2795 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2796 // pointers into 32 bit pointer sized ArtMethods.
2797 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002798 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002799 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002800 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002801 /*executable=*/ false,
2802 /*low_4gb=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002803 &error_msg));
2804 if (oat_file == nullptr) {
2805 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002806 return EXIT_FAILURE;
2807 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002808 std::unique_ptr<gc::space::ImageSpace> space(
2809 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2810 if (space == nullptr) {
2811 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2812 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002813 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002814 }
2815 // Open dex files for the image.
2816 std::vector<std::unique_ptr<const DexFile>> dex_files;
2817 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2818 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2819 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002820 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002821 }
2822 // Dump the actual image.
2823 int result = DumpImage(space.get(), options, os);
2824 if (result != EXIT_SUCCESS) {
2825 return result;
2826 }
2827 // Fall through to dump the boot images.
2828 }
2829
2830 gc::Heap* heap = runtime->GetHeap();
Vladimir Markodd09f712019-12-06 12:37:10 +00002831 if (!heap->HasBootImageSpace()) {
2832 LOG(ERROR) << "No image spaces";
2833 return EXIT_FAILURE;
2834 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002835 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2836 int result = DumpImage(image_space, options, os);
2837 if (result != EXIT_SUCCESS) {
2838 return result;
2839 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002840 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002841 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002842}
2843
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002844static jobject InstallOatFile(Runtime* runtime,
2845 std::unique_ptr<OatFile> oat_file,
2846 std::vector<const DexFile*>* class_path)
2847 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002848 Thread* self = Thread::Current();
2849 CHECK(self != nullptr);
2850 // Need well-known-classes.
2851 WellKnownClasses::Init(self->GetJniEnv());
2852
Vladimir Marko421087b2018-02-27 11:00:17 +00002853 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002854 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002855 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002856 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002857 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002858 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002859 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002860 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002861 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002862 }
2863
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002864 // Need a class loader. Fake that we're a compiler.
2865 // Note: this will run initializers through the unstarted runtime, so make sure it's
2866 // initialized.
2867 interpreter::UnstartedRuntime::Initialize();
2868
2869 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2870
Vladimir Marko421087b2018-02-27 11:00:17 +00002871 // Need to register dex files to get a working dex cache.
2872 for (const DexFile* dex_file : *class_path) {
2873 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2874 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2875 CHECK(dex_cache != nullptr);
2876 }
2877
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002878 return class_loader;
2879}
2880
2881static int DumpOatWithRuntime(Runtime* runtime,
2882 std::unique_ptr<OatFile> oat_file,
2883 OatDumperOptions* options,
2884 std::ostream* os) {
2885 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2886 ScopedObjectAccess soa(Thread::Current());
2887
2888 OatFile* oat_file_ptr = oat_file.get();
2889 std::vector<const DexFile*> class_path;
2890 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002891
2892 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002893 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002894 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002895 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002896 options->class_loader_ = &loader_handle;
2897
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002898 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002899 bool success = oat_dumper.Dump(*os);
2900 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2901}
2902
2903static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002904 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002905 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002906 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002907 options->class_loader_ = &null_class_loader;
2908
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002909 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002910 bool success = oat_dumper.Dump(*os);
2911 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2912}
2913
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002914static int DumpOat(Runtime* runtime,
2915 const char* oat_filename,
2916 const char* dex_filename,
2917 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002918 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002919 if (dex_filename == nullptr) {
2920 LOG(WARNING) << "No dex filename provided, "
2921 << "oatdump might fail if the oat file does not contain the dex code.";
2922 }
Vladimir Markob7bf8432019-12-03 13:18:50 +00002923 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2924 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2925 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002926 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002927 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002928 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002929 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002930 /*executable=*/ false,
2931 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002932 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002933 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002934 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002935 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002936 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002937 return EXIT_FAILURE;
2938 }
2939
2940 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002941 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002942 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002943 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002944 }
2945}
2946
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002947static int SymbolizeOat(const char* oat_filename,
2948 const char* dex_filename,
2949 std::string& output_name,
2950 bool no_bits) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002951 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2952 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2953 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002954 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002955 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002956 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002957 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002958 /*executable=*/ false,
2959 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002960 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002961 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002962 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002963 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002964 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002965 return EXIT_FAILURE;
2966 }
2967
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002968 bool result;
2969 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2970 // files for 64-bit code in the past.
2971 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002972 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002973 result = oat_symbolizer.Symbolize();
2974 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002975 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002976 result = oat_symbolizer.Symbolize();
2977 }
2978 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002979 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002980 return EXIT_FAILURE;
2981 }
2982
2983 return EXIT_SUCCESS;
2984}
2985
Andreas Gampe9fded872016-09-25 16:08:35 -07002986class IMTDumper {
2987 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002988 static bool Dump(Runtime* runtime,
2989 const std::string& imt_file,
2990 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002991 const char* oat_filename,
2992 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002993 Thread* self = Thread::Current();
2994
2995 ScopedObjectAccess soa(self);
2996 StackHandleScope<1> scope(self);
2997 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2998 std::vector<const DexFile*> class_path;
2999
3000 if (oat_filename != nullptr) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00003001 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
3002 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
3003 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003004 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01003005 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01003006 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003007 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01003008 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07003009 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00003010 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01003011 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003012 &error_msg));
3013 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003014 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003015 return false;
3016 }
3017
3018 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
3019 InstallOatFile(runtime, std::move(oat_file), &class_path)));
3020 } else {
3021 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
3022 class_path = runtime->GetClassLinker()->GetBootClassPath();
3023 }
3024
3025 if (!imt_file.empty()) {
3026 return DumpImt(runtime, imt_file, class_loader);
3027 }
3028
3029 if (dump_imt_stats) {
3030 return DumpImtStats(runtime, class_path, class_loader);
3031 }
3032
3033 LOG(FATAL) << "Should not reach here";
3034 UNREACHABLE();
3035 }
3036
3037 private:
3038 static bool DumpImt(Runtime* runtime,
3039 const std::string& imt_file,
3040 Handle<mirror::ClassLoader> h_class_loader)
3041 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003042 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3043 std::unordered_set<std::string> prepared;
3044
3045 for (const std::string& line : lines) {
3046 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3047 // or a class descriptor and an interface method, in which case we will lookup the method,
3048 // determine its IMT slot, and check the class' IMT.
3049 size_t first_space = line.find(' ');
3050 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003051 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003052 } else {
3053 DumpIMTForMethod(runtime,
3054 line.substr(0, first_space),
3055 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003056 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003057 &prepared);
3058 }
3059 std::cerr << std::endl;
3060 }
3061
3062 return true;
3063 }
3064
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003065 static bool DumpImtStats(Runtime* runtime,
3066 const std::vector<const DexFile*>& dex_files,
3067 Handle<mirror::ClassLoader> h_class_loader)
3068 REQUIRES_SHARED(Locks::mutator_lock_) {
3069 size_t without_imt = 0;
3070 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003071 std::map<size_t, size_t> histogram;
3072
3073 ClassLinker* class_linker = runtime->GetClassLinker();
3074 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3075 std::unordered_set<std::string> prepared;
3076
3077 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003078 StackHandleScope<1> scope(self);
3079 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3080
3081 for (const DexFile* dex_file : dex_files) {
3082 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003083 class_def_index != dex_file->NumClassDefs();
3084 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003085 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07003086 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003087 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003088 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003089 std::cerr << "Warning: could not load " << descriptor << std::endl;
3090 continue;
3091 }
3092
3093 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003094 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003095 continue;
3096 }
3097
3098 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3099 if (im_table == nullptr) {
3100 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003101 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003102 continue;
3103 }
3104
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003105 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003106 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3107 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3108 if (ptr->IsRuntimeMethod()) {
3109 if (ptr->IsImtUnimplementedMethod()) {
3110 histogram[0]++;
3111 } else {
3112 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3113 histogram[current_table->NumEntries(pointer_size)]++;
3114 }
3115 } else {
3116 histogram[1]++;
3117 }
3118 }
3119 }
3120 }
3121
3122 std::cerr << "IMT stats:"
3123 << std::endl << std::endl;
3124
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003125 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003126 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003127 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003128 << std::endl << std::endl;
3129
3130 double sum_one = 0;
3131 size_t count_one = 0;
3132
3133 std::cerr << " " << "IMT histogram" << std::endl;
3134 for (auto& bucket : histogram) {
3135 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3136 if (bucket.first > 0) {
3137 sum_one += bucket.second * bucket.first;
3138 count_one += bucket.second;
3139 }
3140 }
3141
3142 double count_zero = count_one + histogram[0];
3143 std::cerr << " Stats:" << std::endl;
3144 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3145 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3146
3147 return true;
3148 }
3149
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003150 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003151 static bool HasNoIMT(Runtime* runtime,
3152 Handle<mirror::Class> klass,
3153 const PointerSize pointer_size,
3154 std::unordered_set<std::string>* prepared)
3155 REQUIRES_SHARED(Locks::mutator_lock_) {
3156 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3157 return true;
3158 }
3159
3160 if (klass->GetImt(pointer_size) == nullptr) {
3161 PrepareClass(runtime, klass, prepared);
3162 }
3163
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003164 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003165 DCHECK(object_class->IsObjectClass());
3166
3167 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3168
Mathieu Chartier6beced42016-11-15 15:51:31 -08003169 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003170 DCHECK(result);
3171 }
3172
3173 return result;
3174 }
3175
3176 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3177 REQUIRES_SHARED(Locks::mutator_lock_) {
3178 if (table == nullptr) {
3179 std::cerr << " <No IMT?>" << std::endl;
3180 return;
3181 }
3182 size_t table_index = 0;
3183 for (;;) {
3184 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3185 if (ptr == nullptr) {
3186 return;
3187 }
3188 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003189 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003190 }
3191 }
3192
3193 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3194 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003195 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003196 const std::string& class_name,
3197 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003198 /*out*/ ObjPtr<mirror::Class>* klass_out,
3199 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003200 REQUIRES_SHARED(Locks::mutator_lock_) {
3201 if (class_name.empty()) {
3202 return nullptr;
3203 }
3204
3205 std::string descriptor;
3206 if (class_name[0] == 'L') {
3207 descriptor = class_name;
3208 } else {
3209 descriptor = DotToDescriptor(class_name.c_str());
3210 }
3211
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003212 ObjPtr<mirror::Class> klass =
3213 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003214
3215 if (klass == nullptr) {
3216 self->ClearException();
3217 std::cerr << "Did not find " << class_name << std::endl;
3218 *klass_out = nullptr;
3219 return nullptr;
3220 }
3221
3222 StackHandleScope<1> scope(Thread::Current());
3223 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3224
3225 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3226 *klass_out = h_klass.Get();
3227 return ret;
3228 }
3229
3230 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3231 Handle<mirror::Class> h_klass,
3232 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003233 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003234 REQUIRES_SHARED(Locks::mutator_lock_) {
3235 PrepareClass(runtime, h_klass, prepared);
3236 return h_klass->GetImt(pointer_size);
3237 }
3238
3239 static void DumpIMTForClass(Runtime* runtime,
3240 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003241 Handle<mirror::ClassLoader> h_loader,
3242 std::unordered_set<std::string>* prepared)
3243 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003244 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003245 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003246 ImTable* imt = PrepareAndGetImTable(runtime,
3247 Thread::Current(),
3248 h_loader,
3249 class_name,
3250 pointer_size,
3251 &klass,
3252 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003253 if (imt == nullptr) {
3254 return;
3255 }
3256
3257 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3258 for (size_t index = 0; index < ImTable::kSize; ++index) {
3259 std::cerr << " " << index << ":" << std::endl;
3260 ArtMethod* ptr = imt->Get(index, pointer_size);
3261 if (ptr->IsRuntimeMethod()) {
3262 if (ptr->IsImtUnimplementedMethod()) {
3263 std::cerr << " <empty>" << std::endl;
3264 } else {
3265 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3266 PrintTable(current_table, pointer_size);
3267 }
3268 } else {
David Sehr709b0702016-10-13 09:12:37 -07003269 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003270 }
3271 }
3272
3273 std::cerr << " Interfaces:" << std::endl;
3274 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003275 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003276 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003277 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003278 std::string iface_name;
3279 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003280
Mathieu Chartier6beced42016-11-15 15:51:31 -08003281 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3282 uint32_t class_hash, name_hash, signature_hash;
3283 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3284 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3285 std::cerr << " " << iface_method.PrettyMethod(true)
3286 << " slot=" << imt_slot
3287 << std::hex
3288 << " class_hash=0x" << class_hash
3289 << " name_hash=0x" << name_hash
3290 << " signature_hash=0x" << signature_hash
3291 << std::dec
3292 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003293 }
3294 }
3295 }
3296
3297 static void DumpIMTForMethod(Runtime* runtime,
3298 const std::string& class_name,
3299 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003300 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003301 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003302 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003303 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003304 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003305 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003306 Thread::Current(),
3307 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003308 class_name,
3309 pointer_size,
3310 &klass,
3311 prepared);
3312 if (imt == nullptr) {
3313 return;
3314 }
3315
3316 std::cerr << class_name << " <" << method << ">" << std::endl;
3317 for (size_t index = 0; index < ImTable::kSize; ++index) {
3318 ArtMethod* ptr = imt->Get(index, pointer_size);
3319 if (ptr->IsRuntimeMethod()) {
3320 if (ptr->IsImtUnimplementedMethod()) {
3321 continue;
3322 }
3323
3324 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3325 if (current_table == nullptr) {
3326 continue;
3327 }
3328
3329 size_t table_index = 0;
3330 for (;;) {
3331 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3332 if (ptr2 == nullptr) {
3333 break;
3334 }
3335 table_index++;
3336
David Sehr709b0702016-10-13 09:12:37 -07003337 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003338 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003339 std::cerr << " Slot "
3340 << index
3341 << " ("
3342 << current_table->NumEntries(pointer_size)
3343 << ")"
3344 << std::endl;
3345 PrintTable(current_table, pointer_size);
3346 return;
3347 }
3348 }
3349 } else {
David Sehr709b0702016-10-13 09:12:37 -07003350 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003351 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003352 std::cerr << " Slot " << index << " (1)" << std::endl;
3353 std::cerr << " " << p_name << std::endl;
3354 } else {
3355 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003356 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003357 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003358 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003359 size_t num_methods = iface->NumDeclaredVirtualMethods();
3360 if (num_methods > 0) {
3361 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3362 if (ImTable::GetImtIndex(&iface_method) == index) {
3363 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003364 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003365 std::cerr << " Slot " << index << " (1)" << std::endl;
3366 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003367 }
3368 }
3369 }
3370 }
3371 }
3372 }
3373 }
3374 }
3375 }
3376
3377 // Read lines from the given stream, dropping comments and empty lines
3378 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3379 std::vector<std::string> output;
3380 while (in_stream.good()) {
3381 std::string dot;
3382 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003383 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003384 continue;
3385 }
3386 output.push_back(dot);
3387 }
3388 return output;
3389 }
3390
3391 // Read lines from the given file, dropping comments and empty lines.
3392 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3393 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3394 if (input_file.get() == nullptr) {
3395 LOG(ERROR) << "Failed to open input file " << input_filename;
3396 return std::vector<std::string>();
3397 }
3398 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3399 input_file->close();
3400 return result;
3401 }
3402
3403 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3404 // and note in the given set that the work was done.
3405 static void PrepareClass(Runtime* runtime,
3406 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003407 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003408 REQUIRES_SHARED(Locks::mutator_lock_) {
3409 if (!h_klass->ShouldHaveImt()) {
3410 return;
3411 }
3412
3413 std::string name;
3414 name = h_klass->GetDescriptor(&name);
3415
3416 if (done->find(name) != done->end()) {
3417 return;
3418 }
3419 done->insert(name);
3420
3421 if (h_klass->HasSuperClass()) {
3422 StackHandleScope<1> h(Thread::Current());
3423 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3424 }
3425
3426 if (!h_klass->IsTemp()) {
3427 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3428 }
3429 }
3430};
3431
Igor Murashkin37743352014-11-13 14:38:00 -08003432struct OatdumpArgs : public CmdlineArgs {
3433 protected:
3434 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003435
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003436 ParseStatus ParseCustom(const char* raw_option,
3437 size_t raw_option_length,
3438 std::string* error_msg) override {
3439 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003440 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003441 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003442 if (base_parse != kParseUnknownArgument) {
3443 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003444 }
3445 }
3446
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003447 std::string_view option(raw_option, raw_option_length);
3448 if (StartsWith(option, "--oat-file=")) {
3449 oat_filename_ = raw_option + strlen("--oat-file=");
3450 } else if (StartsWith(option, "--dex-file=")) {
3451 dex_filename_ = raw_option + strlen("--dex-file=");
3452 } else if (StartsWith(option, "--image=")) {
3453 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003454 } else if (option == "--no-dump:vmap") {
3455 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003456 } else if (option =="--dump:code_info_stack_maps") {
3457 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003458 } else if (option == "--no-disassemble") {
3459 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003460 } else if (option =="--header-only") {
3461 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003462 } else if (StartsWith(option, "--symbolize=")) {
3463 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003464 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003465 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003466 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003467 } else if (StartsWith(option, "--class-filter=")) {
3468 class_filter_ = raw_option + strlen("--class-filter=");
3469 } else if (StartsWith(option, "--method-filter=")) {
3470 method_filter_ = raw_option + strlen("--method-filter=");
3471 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003472 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003473 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003474 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003475 } else if (StartsWith(option, "--export-dex-to=")) {
3476 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3477 } else if (StartsWith(option, "--addr2instr=")) {
3478 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003479 *error_msg = "Address conversion failed";
3480 return kParseError;
3481 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003482 } else if (StartsWith(option, "--app-image=")) {
3483 app_image_ = raw_option + strlen("--app-image=");
3484 } else if (StartsWith(option, "--app-oat=")) {
3485 app_oat_ = raw_option + strlen("--app-oat=");
3486 } else if (StartsWith(option, "--dump-imt=")) {
3487 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003488 } else if (option == "--dump-imt-stats") {
3489 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003490 } else {
3491 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003492 }
3493
Igor Murashkin37743352014-11-13 14:38:00 -08003494 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003495 }
3496
Roland Levillainf73caca2018-08-24 17:19:07 +01003497 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003498 // Infer boot image location from the image location if possible.
3499 if (boot_image_location_ == nullptr) {
3500 boot_image_location_ = image_location_;
3501 }
3502
3503 // Perform the parent checks.
3504 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3505 if (parent_checks != kParseOk) {
3506 return parent_checks;
3507 }
3508
3509 // Perform our own checks.
3510 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3511 *error_msg = "Either --image or --oat-file must be specified";
3512 return kParseError;
3513 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3514 *error_msg = "Either --image or --oat-file must be specified but not both";
3515 return kParseError;
3516 }
3517
3518 return kParseOk;
3519 }
3520
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003521 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003522 std::string usage;
3523
3524 usage +=
3525 "Usage: oatdump [options] ...\n"
3526 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3527 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3528 "\n"
3529 // Either oat-file or image is required.
3530 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Mathieu Chartier2afa19d2019-09-10 16:07:21 -07003531 " Example: --oat-file=/system/framework/arm64/boot.oat\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003532 "\n"
3533 " --image=<file.art>: specifies an input image location.\n"
3534 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003535 "\n"
3536 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003537 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003538 " Example: --app-image=app.art\n"
3539 "\n"
3540 " --app-oat=<file.odex>: specifies an input app oat.\n"
3541 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003542 "\n";
3543
3544 usage += Base::GetUsage();
3545
3546 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003547 " --no-dump:vmap may be used to disable vmap dumping.\n"
3548 " Example: --no-dump:vmap\n"
3549 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003550 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3551 " Example: --dump:code_info_stack_maps\n"
3552 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003553 " --no-disassemble may be used to disable disassembly.\n"
3554 " Example: --no-disassemble\n"
3555 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003556 " --header-only may be used to print only the oat header.\n"
3557 " Example: --header-only\n"
3558 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003559 " --list-classes may be used to list target file classes (can be used with filters).\n"
3560 " Example: --list-classes\n"
3561 " Example: --list-classes --class-filter=com.example.foo\n"
3562 "\n"
3563 " --list-methods may be used to list target file methods (can be used with filters).\n"
3564 " Example: --list-methods\n"
3565 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3566 "\n"
3567 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3568 " Example: --symbolize=/system/framework/boot.oat\n"
3569 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003570 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3571 " .rodata and .text sections are omitted in the output file to save space.\n"
3572 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3573 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003574 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3575 " Example: --class-filter=com.example.foo\n"
3576 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003577 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3578 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003579 "\n"
3580 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3581 " Example: --export-dex-to=/data/local/tmp\n"
3582 "\n"
3583 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3584 " address (e.g. PC from crash dump)\n"
3585 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003586 "\n"
3587 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3588 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003589 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003590 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003591 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003592 " Example: --dump-imt=imt.txt\n"
3593 "\n"
3594 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3595 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003596 "\n";
3597
3598 return usage;
3599 }
3600
3601 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003602 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003603 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003604 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003605 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003606 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003607 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003608 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003609 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003610 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003611 bool disassemble_code_ = true;
3612 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003613 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003614 bool list_classes_ = false;
3615 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003616 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003617 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003618 uint32_t addr2instr_ = 0;
3619 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003620 const char* app_image_ = nullptr;
3621 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003622};
3623
Igor Murashkin37743352014-11-13 14:38:00 -08003624struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003625 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003626 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003627
Igor Murashkin37743352014-11-13 14:38:00 -08003628 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3629 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3630
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003631 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003632 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003633 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003634 args_->disassemble_code_,
3635 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003636 args_->class_filter_,
3637 args_->method_filter_,
3638 args_->list_classes_,
3639 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003640 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003641 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003642 args_->app_image_,
3643 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003644 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003645
Andreas Gampe9fded872016-09-25 16:08:35 -07003646 return (args_->boot_image_location_ != nullptr ||
3647 args_->image_location_ != nullptr ||
3648 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003649 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003650 }
3651
Roland Levillainf73caca2018-08-24 17:19:07 +01003652 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003653 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003654 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003655
Mathieu Chartierd424d082014-10-15 10:31:46 -07003656 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003657
Andreas Gampec24f3992014-12-17 20:40:11 -08003658 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003659 // ELF has special kind of section called SHT_NOBITS which allows us to create
3660 // sections which exist but their data is omitted from the ELF file to save space.
3661 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3662 // with only debug data. We use it in similar way to exclude .rodata and .text.
3663 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003664 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3665 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003666 } else {
3667 return DumpOat(nullptr,
3668 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003669 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003670 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003671 args_->os_) == EXIT_SUCCESS;
3672 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003673 }
3674
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003675 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003676 CHECK(args_ != nullptr);
3677
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003678 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3679 return IMTDumper::Dump(runtime,
3680 args_->imt_dump_,
3681 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003682 args_->oat_filename_,
3683 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003684 }
3685
Igor Murashkin37743352014-11-13 14:38:00 -08003686 if (args_->oat_filename_ != nullptr) {
3687 return DumpOat(runtime,
3688 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003689 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003690 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003691 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003692 }
Igor Murashkin37743352014-11-13 14:38:00 -08003693
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003694 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003695 }
3696
Igor Murashkin37743352014-11-13 14:38:00 -08003697 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3698};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003699
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003700} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003701
3702int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003703 // Output all logging to stderr.
3704 android::base::SetLogger(android::base::StderrLogger);
3705
Igor Murashkin37743352014-11-13 14:38:00 -08003706 art::OatdumpMain main;
3707 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003708}