blob: 8d339706d00f2daa85adfe069f61473cdbbc5adc [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
David Srbecky86decb62018-06-05 06:41:10 +010021#include <iomanip>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070022#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080023#include <map>
24#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070025#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070026#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070027#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070028#include <vector>
29
Andreas Gampe221d9812018-01-22 17:48:56 -080030#include "android-base/logging.h"
Andreas Gampef9411702018-09-06 17:16:57 -070031#include "android-base/parseint.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080032#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080033#include "android-base/strings.h"
34
Ian Rogersd582fa42014-11-05 23:46:43 -080035#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070036#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "art_method-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000038#include "base/bit_utils_iterator.h"
David Sehr9c4a0152018-04-05 12:23:54 -070039#include "base/indenter.h"
David Sehrc431b9d2018-03-02 12:01:51 -080040#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080041#include "base/safe_map.h"
David Srbecky86decb62018-06-05 06:41:10 +010042#include "base/stats.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000043#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080044#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070046#include "class_linker.h"
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010047#include "class_root.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010048#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000049#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000050#include "debug/elf_debug_writer.h"
51#include "debug/method_debug_info.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030052#include "dex/art_dex_file_loader.h"
Mathieu Chartier3d092992018-05-24 13:36:23 -070053#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080054#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080055#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080056#include "dex/dex_file-inl.h"
57#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070058#include "dex/string_reference.h"
David Sehr9c4a0152018-04-05 12:23:54 -070059#include "dex/type_lookup_table.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030060#include "dexlayout.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080061#include "disassembler.h"
David Srbecky2faab002019-02-12 16:35:48 +000062#include "elf/elf_builder.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070063#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070064#include "gc/space/image_space.h"
65#include "gc/space/large_object_space.h"
66#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080067#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070068#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000069#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070070#include "interpreter/unstarted_runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071#include "mirror/array-inl.h"
72#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010073#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074#include "mirror/object-inl.h"
75#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010077#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070079#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070080#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070081#include "stack_map.h"
David Srbecky2faab002019-02-12 16:35:48 +000082#include "stream/buffered_output_stream.h"
83#include "stream/file_output_stream.h"
84#include "subtype_check.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080085#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010086#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000088#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070089#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070090
Igor Murashkin37743352014-11-13 14:38:00 -080091#include <sys/stat.h>
92#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070093
Igor Murashkin37743352014-11-13 14:38:00 -080094namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070095
Andreas Gampe46ee31b2016-12-14 10:11:49 -080096using android::base::StringPrintf;
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080099 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700100 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700101 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100102 "kSaveAllCalleeSavesMethod",
103 "kSaveRefsOnlyMethod",
104 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +0100105 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -0700106 "kSaveEverythingMethodForClinit",
107 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108};
109
110const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700111 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700112 "kClassRoots",
Alex Light885f0c12019-01-09 13:48:55 -0800113 "kSpecialRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700114};
115
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700116// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700117static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700118
Andreas Gampeb40d3612018-06-26 15:49:42 -0700119const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120 DCHECK(oat_dex_file != nullptr);
121 auto it = opened_dex_files.find(oat_dex_file);
122 if (it != opened_dex_files.end()) {
123 return it->second.get();
124 }
125 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
126 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
127 return ret;
128}
129
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800130template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000133 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
134 oat_file_(oat_file),
135 builder_(nullptr),
136 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
137 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700138 }
139
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700140 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800142 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000143 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
Andreas Gampe08c277c2017-04-26 22:22:15 -0700145 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
146 if (elf_file == nullptr) {
147 return false;
148 }
David Srbecky2faab002019-02-12 16:35:48 +0000149 std::unique_ptr<BufferedOutputStream> output_stream =
150 std::make_unique<BufferedOutputStream>(
151 std::make_unique<FileOutputStream>(elf_file.get()));
152 builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153
154 builder_->Start();
155
156 auto* rodata = builder_->GetRoData();
157 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 const uint8_t* rodata_begin = oat_file_->Begin();
160 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000161 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000162 rodata->Start();
163 rodata->WriteFully(rodata_begin, rodata_size);
164 rodata->End();
165 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
168 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000169 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000170 text->Start();
171 text->WriteFully(text_begin, text_size);
172 text->End();
173 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 builder_->PrepareDynamicSection(elf_file->GetPath(),
176 rodata_size,
177 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000178 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100180 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000181 oat_file_->BssRootsOffset(),
182 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000183 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184
Vladimir Marko1b404a82017-09-01 13:35:26 +0100185 const OatHeader& oat_header = oat_file_->GetOatHeader();
186 #define DO_TRAMPOLINE(fn_name) \
187 if (oat_header.Get ## fn_name ## Offset() != 0) { \
188 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000189 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100190 info.isa = oat_header.GetInstructionSet(); \
191 info.is_code_address_text_relative = true; \
192 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
193 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
194 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
195 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
196 method_debug_infos_.push_back(std::move(info)); \
197 }
Vladimir Marko7dac8642019-11-06 17:09:30 +0000198 DO_TRAMPOLINE(JniDlsymLookupTrampoline);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000199 DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);
Vladimir Marko1b404a82017-09-01 13:35:26 +0100200 DO_TRAMPOLINE(QuickGenericJniTrampoline);
201 DO_TRAMPOLINE(QuickImtConflictTrampoline);
202 DO_TRAMPOLINE(QuickResolutionTrampoline);
203 DO_TRAMPOLINE(QuickToInterpreterBridge);
204 #undef DO_TRAMPOLINE
205
David Srbecky5d950762016-03-07 20:47:29 +0000206 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100207
208 // TODO: Try to symbolize link-time thunks?
209 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700210
David Srbecky32210b92017-12-04 14:39:21 +0000211 // TODO: Add symbols for dex bytecode in the .dex section.
212
213 debug::DebugInfo debug_info{};
214 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
215
David Srbecky7370d922019-02-12 14:00:30 +0000216 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217
David Srbecky6d8c8f02015-10-26 10:57:09 +0000218 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700219
Andreas Gampe08c277c2017-04-26 22:22:15 -0700220 bool ret_value = builder_->Good();
221
222 builder_.reset();
223 output_stream.reset();
224
225 if (elf_file->FlushCloseOrErase() != 0) {
226 return false;
227 }
228 elf_file.reset();
229
230 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 }
232
David Srbecky5d950762016-03-07 20:47:29 +0000233 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700234 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700235 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700236 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000238 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 }
240 }
241
Andreas Gampeb40d3612018-06-26 15:49:42 -0700242 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700244 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
245 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 return;
247 }
248 for (size_t class_def_index = 0;
249 class_def_index < dex_file->NumClassDefs();
250 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
252 OatClassType type = oat_class.GetType();
253 switch (type) {
254 case kOatClassAllCompiled:
255 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000256 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 break;
258
259 case kOatClassNoneCompiled:
260 case kOatClassMax:
261 // Ignore.
262 break;
263 }
264 }
265 }
266
David Srbecky5d950762016-03-07 20:47:29 +0000267 void WalkOatClass(const OatFile::OatClass& oat_class,
268 const DexFile& dex_file,
269 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700270 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 // Note: even if this is an interface or a native class, we still have to walk it, as there
272 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700274 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000275 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
276 dex_file,
277 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700278 method.GetIndex(),
279 method.GetCodeItem(),
280 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 }
283
David Srbecky5d950762016-03-07 20:47:29 +0000284 void WalkOatMethod(const OatFile::OatMethod& oat_method,
285 const DexFile& dex_file,
286 uint32_t class_def_index,
287 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800288 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000289 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700290 if ((method_access_flags & kAccAbstract) != 0) {
291 // Abstract method, no code.
292 return;
293 }
David Srbecky5d950762016-03-07 20:47:29 +0000294 const OatHeader& oat_header = oat_file_->GetOatHeader();
295 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
296 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700297 // No code.
298 return;
299 }
300
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100301 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
302 // Clear Thumb2 bit.
303 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
304
Vladimir Marko1b404a82017-09-01 13:35:26 +0100305 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000306 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000307 info.dex_file = &dex_file;
308 info.class_def_index = class_def_index;
309 info.dex_method_index = dex_method_index;
310 info.access_flags = method_access_flags;
311 info.code_item = code_item;
312 info.isa = oat_header.GetInstructionSet();
313 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
314 info.is_native_debuggable = oat_header.IsNativeDebuggable();
315 info.is_optimized = method_header->IsOptimized();
316 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100317 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000318 info.code_size = method_header->GetCodeSize();
319 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
320 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
321 info.cfi = ArrayRef<uint8_t>();
322 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700323 }
324
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700325 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 const OatFile* oat_file_;
David Srbecky2faab002019-02-12 16:35:48 +0000327 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000328 std::vector<debug::MethodDebugInfo> method_debug_infos_;
329 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700330 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000331 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700332};
333
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334class OatDumperOptions {
335 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100336 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100337 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700338 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700339 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 const char* class_filter,
341 const char* method_filter,
342 bool list_classes,
343 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000344 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800346 const char* app_image,
347 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100349 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100350 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700351 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700352 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000354 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 list_classes_(list_classes),
356 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000357 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800359 app_image_(app_image),
360 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800361 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800362 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700364 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100365 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 const bool disassemble_code_;
367 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800368 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000369 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 const bool list_classes_;
371 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000372 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800374 const char* const app_image_;
375 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700377 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700378};
379
Elliott Hughese3c845c2012-02-28 17:23:01 -0800380class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700381 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100382 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000383 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800384 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700385 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800386 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800387 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
388 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700389 new DisassemblerOptions(
390 options_.absolute_addresses_,
391 oat_file.Begin(),
392 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700393 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700394 Is64BitInstructionSet(instruction_set_)
395 ? &Thread::DumpThreadOffset<PointerSize::k64>
396 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800397 CHECK(options_.class_loader_ != nullptr);
398 CHECK(options_.class_filter_ != nullptr);
399 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800400 AddAllOffsets();
401 }
402
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700403 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 delete disassembler_;
405 }
406
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800407 InstructionSet GetInstructionSet() {
408 return instruction_set_;
409 }
410
Andreas Gampec55bb392018-09-21 00:02:02 +0000411 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300412
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700413 bool Dump(std::ostream& os) {
414 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
417 os << "MAGIC:\n";
418 os << oat_header.GetMagic() << "\n\n";
419
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800420 os << "LOCATION:\n";
421 os << oat_file_.GetLocation() << "\n\n";
422
Brian Carlstromaded5f72011-10-07 17:15:04 -0700423 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800424 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425
Elliott Hughesa72ec822012-03-05 17:12:22 -0800426 os << "INSTRUCTION SET:\n";
427 os << oat_header.GetInstructionSet() << "\n\n";
428
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700429 {
430 std::unique_ptr<const InstructionSetFeatures> features(
431 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
432 oat_header.GetInstructionSetFeaturesBitmap()));
433 os << "INSTRUCTION SET FEATURES:\n";
434 os << features->GetFeatureString() << "\n\n";
435 }
Dave Allison70202782013-10-22 17:52:19 -0700436
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437 os << "DEX FILE COUNT:\n";
438 os << oat_header.GetDexFileCount() << "\n\n";
439
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800440#define DUMP_OAT_HEADER_OFFSET(label, offset) \
441 os << label " OFFSET:\n"; \
442 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800444 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
445 } \
446 os << StringPrintf("\n\n");
447
448 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
Vladimir Marko7dac8642019-11-06 17:09:30 +0000449 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
450 GetJniDlsymLookupTrampolineOffset);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000451 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP CRITICAL TRAMPOLINE",
452 GetJniDlsymLookupCriticalTrampolineOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800453 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
454 GetQuickGenericJniTrampolineOffset);
455 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
456 GetQuickImtConflictTrampolineOffset);
457 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
458 GetQuickResolutionTrampolineOffset);
459 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
460 GetQuickToInterpreterBridgeOffset);
461#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700462
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700463 // Print the key-value store.
464 {
465 os << "KEY VALUE STORE:\n";
466 size_t index = 0;
467 const char* key;
468 const char* value;
469 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
470 os << key << " = " << value << "\n";
471 index++;
472 }
473 os << "\n";
474 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700475
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800476 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700477 os << "BEGIN:\n";
478 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700479
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700480 os << "END:\n";
481 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
482 }
483
484 os << "SIZE:\n";
485 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486
487 os << std::flush;
488
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800489 // If set, adjust relative address to be searched
490 if (options_.addr2instr_ != 0) {
491 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
492 os << "SEARCH ADDRESS (executable offset + input):\n";
493 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
494 }
495
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000496 // Dump .data.bimg.rel.ro entries.
497 DumpDataBimgRelRoEntries(os);
498
499 // Dump .bss summary, individual entries are dumped per dex file.
500 os << ".bss: ";
501 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
502 os << "empty.\n\n";
503 } else {
504 os << oat_file_.GetBssMethods().size() << " methods, ";
505 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
506 }
507
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700508 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700509 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700510 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700511 CHECK(oat_dex_file != nullptr);
512 std::string error_msg;
513 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
514 if (dex_file == nullptr) {
515 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
516 << error_msg;
517 continue;
518 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700519
Mathieu Chartier120aa282017-08-05 16:03:03 -0700520 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
521 if (layout_sections != nullptr) {
522 os << "Layout data\n";
523 os << *layout_sections;
524 os << "\n";
525 }
526
Vladimir Markobacb8e42018-06-13 14:45:30 +0100527 if (!options_.dump_header_only_) {
528 // Dump .bss entries.
529 DumpBssEntries(
530 os,
531 "ArtMethod",
532 oat_dex_file->GetMethodBssMapping(),
533 dex_file->NumMethodIds(),
534 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
535 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
536 DumpBssEntries(
537 os,
538 "Class",
539 oat_dex_file->GetTypeBssMapping(),
540 dex_file->NumTypeIds(),
541 sizeof(GcRoot<mirror::Class>),
542 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
543 DumpBssEntries(
544 os,
545 "String",
546 oat_dex_file->GetStringBssMapping(),
547 dex_file->NumStringIds(),
548 sizeof(GcRoot<mirror::Class>),
549 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
550 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700551 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700552
David Brazdilc03d7b62016-03-02 12:18:03 +0000553 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000554 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000555 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000556 if (vdex_header.IsValid()) {
557 std::string error_msg;
558 std::vector<const DexFile*> dex_files;
559 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
560 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
561 if (dex_file == nullptr) {
562 os << "Error opening dex file: " << error_msg << std::endl;
563 return false;
564 }
565 dex_files.push_back(dex_file);
566 }
567 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
568 deps.Dump(&vios);
569 } else {
570 os << "UNRECOGNIZED vdex file, magic "
571 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000572 << ", verifier deps version "
573 << vdex_header.GetVerifierDepsVersion()
574 << ", dex section version "
575 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000576 << "\n";
577 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000578 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700579 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000580 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300581 if (!DumpOatDexFile(os, *oat_dex_file)) {
582 success = false;
583 }
584 }
585 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800586
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300587 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000588 std::string error_msg;
589 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
590 if (!OS::FileExists(vdex_filename.c_str())) {
591 os << "File " << vdex_filename.c_str() << " does not exist\n";
592 return false;
593 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300594
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000595 DexFileUniqV vdex_dex_files;
596 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
597 &vdex_dex_files,
598 &error_msg);
599 if (vdex_file.get() == nullptr) {
600 os << "Failed to open vdex file: " << error_msg << "\n";
601 return false;
602 }
603 if (oat_dex_files_.size() != vdex_dex_files.size()) {
604 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
605 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
606 return false;
607 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300608
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000609 size_t i = 0;
610 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700611 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000612 CHECK(oat_dex_file != nullptr);
613 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300614
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300615 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
616 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
617 // the original input Dex file, the `update_checksum_` option is used to recompute the
618 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
619 // false), ExportDexFile() enforces a reproducible checksum verification.
620 if (vdex_dex_file->IsCompactDexFile()) {
621 Options options;
622 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
623 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700624 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300625 std::unique_ptr<art::DexContainer> dex_container;
626 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
627 vdex_dex_file.get(),
628 i,
629 &dex_container,
630 &error_msg);
631 if (!result) {
632 os << "DexLayout failed to process Dex file: " + error_msg;
633 success = false;
634 break;
635 }
636 DexContainer::Section* main_section = dex_container->GetMainSection();
637 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
638
639 const ArtDexFileLoader dex_file_loader;
640 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
641 main_section->Begin(),
642 main_section->Size(),
643 vdex_dex_file->GetLocation(),
644 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700645 /*oat_dex_file=*/ nullptr,
646 /*verify=*/ false,
647 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300648 &error_msg));
649 if (dex == nullptr) {
650 os << "Failed to load DexFile from layout container: " + error_msg;
651 success = false;
652 break;
653 }
654 if (dex->IsCompactDexFile()) {
655 os <<"CompactDex conversion to StandardDex failed";
656 success = false;
657 break;
658 }
659
Andreas Gampe9b031f72018-10-04 11:03:34 -0700660 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300661 success = false;
662 break;
663 }
664 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700665 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300666 success = false;
667 break;
668 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300669 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000670 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700671 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700672 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000673
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800674 {
675 os << "OAT FILE STATS:\n";
676 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100677 stats_.AddBytes(oat_file_.Size());
678 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800679 }
680
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700681 os << std::flush;
682 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700683 }
684
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800685 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700686 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
687 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800688 return 0; // Address not in oat file
689 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800690 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
691 reinterpret_cast<uintptr_t>(oat_file_.Begin());
692 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800693 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800694 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800695 return end_offset - begin_offset;
696 }
697
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800698 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700699 return oat_file_.GetOatHeader().GetInstructionSet();
700 }
701
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700702 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800703 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700704 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700705 CHECK(oat_dex_file != nullptr);
706 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700707 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
708 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700709 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
710 << "': " << error_msg;
711 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800712 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800713 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700714 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700715 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700716 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100717 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Vladimir Markoe00e2012019-05-14 14:42:02 +0100718 uint32_t oat_method_index;
719 if (m->IsStatic() || m->IsDirect()) {
720 // Simple case where the oat method index was stashed at load time.
721 oat_method_index = m->GetMethodIndex();
722 } else {
723 // Compute the oat_method_index by search for its position in the class def.
724 ClassAccessor accessor(*dex_file, *class_def);
725 oat_method_index = accessor.NumDirectMethods();
726 bool found_virtual = false;
727 for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
728 // Check method index instead of identity in case of duplicate method definitions.
729 if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
730 found_virtual = true;
731 break;
732 }
733 ++oat_method_index;
734 }
735 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
736 << dex_file->PrettyMethod(m->GetDexMethodIndex());
737 }
738 return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800739 }
740 }
741 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700742 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 }
744
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300745 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
746 // files are fully unquickened and stored in dex_files
747 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
748 /* out */ DexFileUniqV* dex_files,
749 /* out */ std::string* error_msg) {
750 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
751 if (file == nullptr) {
752 *error_msg = "Could not open file " + vdex_filename + " for reading.";
753 return nullptr;
754 }
755
756 int64_t vdex_length = file->GetLength();
757 if (vdex_length == -1) {
758 *error_msg = "Could not read the length of file " + vdex_filename;
759 return nullptr;
760 }
761
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100762 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300763 file->GetLength(),
764 PROT_READ | PROT_WRITE,
765 MAP_PRIVATE,
766 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700767 /* start offset= */ 0,
768 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300769 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100770 error_msg);
771 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300772 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
773 return nullptr;
774 }
775
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100776 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300777 if (!vdex_file->IsValid()) {
778 *error_msg = "Vdex file is not valid";
779 return nullptr;
780 }
781
782 DexFileUniqV tmp_dex_files;
783 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
784 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
785 return nullptr;
786 }
787
788 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700789 /* decompile_return_instruction= */ true);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300790
791 *dex_files = std::move(tmp_dex_files);
792 return vdex_file;
793 }
794
David Srbecky86decb62018-06-05 06:41:10 +0100795 bool AddStatsObject(const void* address) {
796 return seen_stats_objects_.insert(address).second; // Inserted new entry.
797 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800798
David Srbecky86decb62018-06-05 06:41:10 +0100799 void DumpStats(VariableIndentationOutputStream& os,
800 const std::string& name,
801 const Stats& stats,
802 double total) {
803 if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
804 double percent = 100.0 * stats.Value() / total;
805 os.Stream()
806 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
807 << std::setw(8) << stats.Count() << " "
808 << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
809 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
810
811 // Sort all children by largest value first, than by name.
812 std::map<std::pair<double, std::string>, const Stats&> sorted_children;
813 for (const auto& it : stats.Children()) {
814 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800815 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800816
David Srbecky86decb62018-06-05 06:41:10 +0100817 // Add "other" row to represent any amount not account for by the children.
818 Stats other;
819 other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
820 if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
821 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800822 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800823
David Srbecky86decb62018-06-05 06:41:10 +0100824 // Print the data.
825 ScopedIndentation indent1(&os);
826 for (const auto& it : sorted_children) {
827 DumpStats(os, it.first.second, it.second, total);
828 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800829 }
David Srbecky86decb62018-06-05 06:41:10 +0100830 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800831
Brian Carlstromaded5f72011-10-07 17:15:04 -0700832 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800833 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800834 // We don't know the length of the code for each method, but we need to know where to stop
835 // when disassembling. What we do know is that a region of code will be followed by some other
836 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
837 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800838 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700839 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700840 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700841 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700842 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
843 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700844 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
845 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800846 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800847 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800848 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700849 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700850 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700851 for (uint32_t class_method_index = 0;
852 class_method_index < accessor.NumMethods();
853 ++class_method_index) {
854 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800855 }
856 }
857 }
858
859 // If the last thing in the file is code for a method, there won't be an offset for the "next"
860 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
861 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800862 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800863 }
864
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700865 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
866 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
867 }
868
Elliott Hughese3c845c2012-02-28 17:23:01 -0800869 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800870 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000871 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800872 code_offset &= ~0x1;
873 }
874 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800875 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800876 }
877
Andreas Gampeb40d3612018-06-26 15:49:42 -0700878 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800880 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700881 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800882 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800883 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700884
Andreas Gampe2ba88952016-04-29 17:52:07 -0700885 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000886 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
887 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100888
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000889 // Print data range of the dex file embedded inside the corresponding vdex file.
890 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
891 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
892 os << StringPrintf(
893 "dex-file: 0x%08x..0x%08x\n",
894 dex_offset,
895 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
896 } else {
897 os << StringPrintf("dex-file not in VDEX file\n");
898 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700899
Andreas Gampe2ba88952016-04-29 17:52:07 -0700900 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700901 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700902 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
903 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700904 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700905 os << std::flush;
906 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700907 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908
Andreas Gampe2ba88952016-04-29 17:52:07 -0700909 // Print lookup table, if it exists.
910 if (oat_dex_file.GetLookupTableData() != nullptr) {
911 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
912 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700913 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700914 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
915 table_offset,
916 table_offset + table_size - 1);
917 }
918
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 VariableIndentationOutputStream vios(&os);
920 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700921 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800922 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700923 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800924 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
925 continue;
926 }
927
Mathieu Chartier98dad402018-06-04 18:00:12 -0700928 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700929 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100930 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700932 static_cast<ssize_t>(class_def_index),
933 descriptor,
934 oat_class_offset,
935 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100936 << " (" << oat_class.GetStatus() << ")"
937 << " (" << oat_class.GetType() << ")\n";
938 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700939 if (options_.list_classes_) {
940 continue;
941 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700942 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 success = false;
944 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800945 if (stop_analysis) {
946 os << std::flush;
947 return success;
948 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700949 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700950 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700951 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700952 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700953 }
954
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300955 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
956 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
957 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
958 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300959 bool ExportDexFile(std::ostream& os,
960 const OatDexFile& oat_dex_file,
961 const DexFile* dex_file,
962 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800963 std::string error_msg;
964 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300965
966 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
967 // same as the one extracted from the Oat container (pre-oreo)
968 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800969
970 // Some quick checks just in case
971 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
972 os << "Invalid dex file\n";
973 return false;
974 }
975
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300976 if (dex_file == nullptr) {
977 // Exported bytecode is quickened (dex-to-dex transformations present)
978 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
979 if (dex_file == nullptr) {
980 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
981 return false;
982 }
983
984 // Recompute checksum
985 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
986 dex_file->CalculateChecksum();
987 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300988 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
989 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
990 if (!used_dexlayout) {
991 // Vdex unquicken output should match original input bytecode
992 uint32_t orig_checksum =
993 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
994 if (orig_checksum != dex_file->CalculateChecksum()) {
995 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
996 return false;
997 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300998 }
999 }
1000
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001001 // Verify output directory exists
1002 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1003 // TODO: Extend OS::DirectoryExists if symlink support is required
1004 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1005 return false;
1006 }
1007
1008 // Beautify path names
1009 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1010 return false;
1011 }
1012
1013 std::string dex_orig_name;
1014 size_t dex_orig_pos = dex_file_location.rfind('/');
1015 if (dex_orig_pos == std::string::npos)
1016 dex_orig_name = dex_file_location;
1017 else
1018 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1019
1020 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001021 if (dex_orig_name.size() == 8 &&
1022 dex_orig_name.compare("base.apk") == 0 &&
1023 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001024 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1025 size_t apk_orig_pos = dex_file_location.rfind('/');
1026 if (apk_orig_pos != std::string::npos) {
1027 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1028 }
1029 }
1030
1031 std::string out_dex_path(options_.export_dex_location_);
1032 if (out_dex_path.back() != '/') {
1033 out_dex_path.append("/");
1034 }
1035 out_dex_path.append(dex_orig_name);
1036 out_dex_path.append("_export.dex");
1037 if (out_dex_path.length() > PATH_MAX) {
1038 return false;
1039 }
1040
1041 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1042 if (file.get() == nullptr) {
1043 os << "Failed to open output dex file " << out_dex_path;
1044 return false;
1045 }
1046
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001047 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001048 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001049 os << "Failed to write dex file";
1050 file->Erase();
1051 return false;
1052 }
1053
1054 if (file->FlushCloseOrErase() != 0) {
1055 os << "Flush and close failed";
1056 return false;
1057 }
1058
1059 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1060 os << std::flush;
1061
1062 return true;
1063 }
1064
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001065 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001066 const OatFile::OatClass& oat_class,
1067 const DexFile& dex_file,
1068 const ClassAccessor& class_accessor,
1069 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001070 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001071 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001072 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001073 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1074 if (!DumpOatMethod(vios,
1075 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1076 class_method_index,
1077 oat_class,
1078 dex_file,
1079 method.GetIndex(),
1080 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001081 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001082 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083 success = false;
1084 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001085 if (addr_found) {
1086 *stop_analysis = true;
1087 return success;
1088 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001089 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001090 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001091 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001092 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001093 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001094
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001095 static constexpr uint32_t kPrologueBytes = 16;
1096
1097 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1098 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1099
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001100 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001101 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001102 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001103 const OatFile::OatClass& oat_class,
1104 const DexFile& dex_file,
1105 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001106 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001107 uint32_t method_access_flags,
1108 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001109 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001110
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001111 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001112
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001113 // TODO: Support regex
1114 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1115 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001116 return success;
1117 }
1118
David Sehr709b0702016-10-13 09:12:37 -07001119 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001120 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1121 class_method_index, pretty_method.c_str(),
1122 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001123 if (options_.list_methods_) {
1124 return success;
1125 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001126
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001127 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1128 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1129 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1130 uint32_t code_offset = oat_method.GetCodeOffset();
1131 uint32_t code_size = oat_method.GetQuickCodeSize();
1132 if (resolved_addr2instr_ != 0) {
1133 if (resolved_addr2instr_ > code_offset + code_size) {
1134 return success;
1135 } else {
1136 *addr_found = true; // stop analyzing file at next iteration
1137 }
1138 }
1139
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001140 // Everything below is indented at least once.
1141 ScopedIndentation indent1(vios);
1142
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001143 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001144 vios->Stream() << "DEX CODE:\n";
1145 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001146 if (code_item_accessor.HasCodeItem()) {
1147 for (const DexInstructionPcPair& inst : code_item_accessor) {
1148 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1149 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1150 }
1151 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001152 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001153
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001154 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001155 std::unique_ptr<verifier::MethodVerifier> verifier;
1156 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001157 // We need to have the handle scope stay live until after the verifier since the verifier has
1158 // a handle to the dex cache from hs.
1159 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001160 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1161 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001162 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001163 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001164 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001165 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001166 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001167 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001168 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001169 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001170 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001171 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001172 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001173 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001174 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1175 oat_method_offsets_offset, oat_file_.Size());
1176 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001177 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001178 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001179 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001180
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001181 ScopedIndentation indent2(vios);
1182 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001183 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1184 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001185 vios->Stream() << StringPrintf("WARNING: "
1186 "code offset 0x%08x is past end of file 0x%08zx.\n",
1187 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001188 success = false;
1189 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001190 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001191 }
1192 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001194 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1195 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001196 if (AddStatsObject(method_header)) {
1197 stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1198 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001199 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001200 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001201 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001202 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001203 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001205 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1206 method_header_offset, oat_file_.Size());
1207 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001209 return false;
1210 }
1211
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001212 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001213 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001214 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001216 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001217 uint32_t vmap_table_offset = method_header ==
1218 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001220
1221 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001222 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001223 ? oat_file_.GetVdexFile()->Size()
1224 : method_header->GetCode() - oat_file_.Begin();
1225 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001226 vios->Stream() << StringPrintf("WARNING: "
1227 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1228 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001229 vmap_table_offset,
1230 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001233 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001234 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001235 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001236 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001237 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001238 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001239
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 ScopedIndentation indent2(vios);
1241 vios->Stream()
1242 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1243 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1244 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1245 vios->Stream() << "\n";
1246 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1247 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1248 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 }
1250 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001251 // Based on spill masks from QuickMethodFrameInfo so placed
1252 // after it is dumped, but useful for understanding quick
1253 // code, so dumped here.
1254 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001255 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001256 }
1257 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001258 vios->Stream() << "CODE: ";
Nicolas Geoffray57083762019-03-05 09:24:45 +00001259 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1260 if (code_size_offset > oat_file_.Size()) {
1261 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 vios->Stream() << StringPrintf("WARNING: "
Nicolas Geoffray57083762019-03-05 09:24:45 +00001263 "code size offset 0x%08x is past end of file 0x%08zx.",
1264 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001265 success = false;
Nicolas Geoffray57083762019-03-05 09:24:45 +00001266 } else {
1267 const void* code = oat_method.GetQuickCode();
1268 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1269 uint64_t aligned_code_end = aligned_code_begin + code_size;
1270 if (AddStatsObject(code)) {
1271 stats_.Child("Code")->AddBytes(code_size);
1272 }
1273
1274 if (options_.absolute_addresses_) {
1275 vios->Stream() << StringPrintf("%p ", code);
1276 }
1277 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1278 code_offset,
1279 code_size_offset,
1280 code_size,
1281 code != nullptr ? "..." : "");
1282
1283 ScopedIndentation indent2(vios);
1284 if (aligned_code_begin > oat_file_.Size()) {
1285 vios->Stream() << StringPrintf("WARNING: "
1286 "start of code at 0x%08x is past end of file 0x%08zx.",
1287 aligned_code_begin, oat_file_.Size());
1288 success = false;
1289 } else if (aligned_code_end > oat_file_.Size()) {
1290 vios->Stream() << StringPrintf(
1291 "WARNING: "
1292 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1293 "code size is 0x%08x loaded from offset 0x%08x.\n",
1294 aligned_code_end, oat_file_.Size(),
1295 code_size, code_size_offset);
1296 success = false;
1297 if (options_.disassemble_code_) {
1298 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1299 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1300 }
1301 }
1302 } else if (code_size > kMaxCodeSize) {
1303 vios->Stream() << StringPrintf(
1304 "WARNING: "
1305 "code size %d is bigger than max expected threshold of %d. "
1306 "code size is 0x%08x loaded from offset 0x%08x.\n",
1307 code_size, kMaxCodeSize,
1308 code_size, code_size_offset);
1309 success = false;
1310 if (options_.disassemble_code_) {
1311 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1312 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1313 }
1314 }
1315 } else if (options_.disassemble_code_) {
1316 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1317 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001318 }
1319 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001320 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001321 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001322 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001323
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001324 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1325 if (spill_mask == 0) {
1326 return;
1327 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001329 for (size_t i = 0; i < 32; i++) {
1330 if ((spill_mask & (1 << i)) != 0) {
1331 if (is_float) {
1332 os << "fr" << i;
1333 } else {
1334 os << "r" << i;
1335 }
1336 spill_mask ^= 1 << i; // clear bit
1337 if (spill_mask != 0) {
1338 os << ", ";
1339 } else {
1340 break;
1341 }
1342 }
1343 }
1344 os << ")";
1345 }
1346
Roland Levillain442b46a2015-02-18 16:54:21 +00001347 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001348 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001349 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001350 const CodeItemDataAccessor& code_item_accessor) {
1351 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001352 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001353 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001354 if (raw_code_info != nullptr) {
1355 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001356 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001357 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001358 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001359 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001360 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001361 // We don't encode the size in the table, so just emit that we have quickened
1362 // information.
1363 ScopedIndentation indent(vios);
1364 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001365 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001366 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001367 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001368 }
1369
1370 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001371 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001372 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001373 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001374 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001375 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001376 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001377 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001378 }
1379
Andreas Gampe36a296f2017-06-13 14:11:11 -07001380 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1381 // According to stack model, the first out is above the Method referernce.
1382 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1383 }
1384
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001385 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001386 uint32_t core_spills,
1387 uint32_t fp_spills,
1388 size_t frame_size,
1389 int reg,
1390 InstructionSet isa) {
1391 PointerSize pointer_size = InstructionSetPointerSize(isa);
1392 if (kIsDebugBuild) {
1393 auto* runtime = Runtime::Current();
1394 if (runtime != nullptr) {
1395 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1396 }
1397 }
1398 DCHECK_ALIGNED(frame_size, kStackAlignment);
1399 DCHECK_NE(reg, -1);
1400 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1401 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1402 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001403 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1404 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001405 const int max_num_special_temps = 1;
1406 if (reg == temp_threshold) {
1407 // The current method pointer corresponds to special location on stack.
1408 return 0;
1409 } else if (reg >= temp_threshold + max_num_special_temps) {
1410 /*
1411 * Special temporaries may have custom locations and the logic above deals with that.
1412 * However, non-special temporaries are placed relative to the outs.
1413 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001414 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001415 + static_cast<size_t>(pointer_size) /* art method */;
1416 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1417 return temps_start + relative_offset;
1418 } else if (reg < num_regs) {
1419 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1420 return locals_start + (reg * sizeof(uint32_t));
1421 } else {
1422 // Handle ins.
1423 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1424 + static_cast<size_t>(pointer_size) /* art method */;
1425 }
1426 }
1427
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001428 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001429 const CodeItemDataAccessor& code_item_accessor) {
1430 if (code_item_accessor.HasCodeItem()) {
1431 size_t num_locals_ins = code_item_accessor.RegistersSize();
1432 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001433 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001434 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001435
1436 os << "vr_stack_locations:";
1437 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1438 // For readability, delimit the different kinds of VRs.
1439 if (reg == num_locals_ins) {
1440 os << "\n\tmethod*:";
1441 } else if (reg == num_locals && num_ins > 0) {
1442 os << "\n\tins:";
1443 } else if (reg == 0 && num_locals > 0) {
1444 os << "\n\tlocals:";
1445 }
1446
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001447 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001448 oat_method.GetCoreSpillMask(),
1449 oat_method.GetFpSpillMask(),
1450 oat_method.GetFrameSizeInBytes(),
1451 reg,
1452 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001453 os << " v" << reg << "[sp + #" << offset << "]";
1454 }
1455
1456 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1457 if (out_reg == 0) {
1458 os << "\n\touts:";
1459 }
1460
Andreas Gampe36a296f2017-06-13 14:11:11 -07001461 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001462 os << " v" << out_reg << "[sp + #" << offset << "]";
1463 }
1464
1465 os << "\n";
1466 }
1467 }
1468
Roland Levillainf2650d12015-05-28 14:53:28 +01001469 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1470 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001471 static bool IsMethodGeneratedByOptimizingCompiler(
1472 const OatFile::OatMethod& oat_method,
1473 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001474 // If the native GC map is null and the Dex `code_item` is not
1475 // null, then this method has been compiled with the optimizing
1476 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001477 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001478 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001479 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001480 }
1481
1482 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1483 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001484 static bool IsMethodGeneratedByDexToDexCompiler(
1485 const OatFile::OatMethod& oat_method,
1486 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001487 // If the quick code is null, the Dex `code_item` is not
1488 // null, and the vmap table is not null, then this method has been compiled
1489 // with the dextodex compiler.
1490 return oat_method.GetQuickCode() == nullptr &&
1491 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001492 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001493 }
1494
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001495 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001496 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001498 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001499 const dex::ClassDef& class_def,
1500 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001501 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001502 if ((method_access_flags & kAccNative) == 0) {
1503 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001504 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001505 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001506 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1507 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1508 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001509 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1510 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001511 if (method == nullptr) {
1512 soa.Self()->ClearException();
1513 return nullptr;
1514 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001515 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001516 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001517 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001518 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001519
1520 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 }
1522
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001523 // The StackMapsHelper provides the stack maps in the native PC order.
1524 // For identical native PCs, the order from the CodeInfo is preserved.
1525 class StackMapsHelper {
1526 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001527 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001528 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001529 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001530 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001531 offset_(static_cast<uint32_t>(-1)),
1532 stack_map_index_(0u),
1533 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001534 if (number_of_stack_maps_ != 0u) {
1535 // Check if native PCs are ordered.
1536 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001537 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001538 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001539 StackMap current = code_info_.GetStackMapAt(i);
1540 if (last.GetNativePcOffset(instruction_set) >
1541 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001542 ordered = false;
1543 break;
1544 }
1545 last = current;
1546 }
1547 if (!ordered) {
1548 // Create indirection indexes for access in native PC order. We do not optimize
1549 // for the fact that there can currently be only two separately ordered ranges,
1550 // namely normal stack maps and catch-point stack maps.
1551 indexes_.resize(number_of_stack_maps_);
1552 std::iota(indexes_.begin(), indexes_.end(), 0u);
1553 std::sort(indexes_.begin(),
1554 indexes_.end(),
1555 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001556 StackMap left = code_info_.GetStackMapAt(lhs);
1557 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1558 StackMap right = code_info_.GetStackMapAt(rhs);
1559 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001560 // If the PCs are the same, compare indexes to preserve the original order.
1561 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1562 });
1563 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001564 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001565 }
1566 }
1567
1568 const CodeInfo& GetCodeInfo() const {
1569 return code_info_;
1570 }
1571
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001572 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001573 return offset_;
1574 }
1575
1576 StackMap GetStackMap() const {
1577 return GetStackMapAt(stack_map_index_);
1578 }
1579
1580 void Next() {
1581 ++stack_map_index_;
1582 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001583 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001584 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001585 }
1586
1587 private:
1588 StackMap GetStackMapAt(size_t i) const {
1589 if (!indexes_.empty()) {
1590 i = indexes_[i];
1591 }
1592 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001593 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001594 }
1595
1596 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001597 const size_t number_of_stack_maps_;
1598 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001599 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001600 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001601 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001602 };
1603
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001604 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001605 const OatFile::OatMethod& oat_method,
1606 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001607 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001608 const void* quick_code = oat_method.GetQuickCode();
1609
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001610 if (code_size == 0) {
1611 code_size = oat_method.GetQuickCodeSize();
1612 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001613 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001614 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001615 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001616 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1617 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001618 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001619 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
David Srbecky86decb62018-06-05 06:41:10 +01001620 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky42deda82018-08-10 11:23:27 +01001621 helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
David Srbecky86decb62018-06-05 06:41:10 +01001622 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001623 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1624 size_t offset = 0;
1625 while (offset < code_size) {
1626 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1627 if (offset == helper.GetOffset()) {
1628 ScopedIndentation indent1(vios);
1629 StackMap stack_map = helper.GetStackMap();
1630 DCHECK(stack_map.IsValid());
1631 stack_map.Dump(vios,
1632 helper.GetCodeInfo(),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001633 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001634 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001635 do {
1636 helper.Next();
1637 // There may be multiple stack maps at a given PC. We display only the first one.
1638 } while (offset == helper.GetOffset());
1639 }
1640 DCHECK_LT(offset, helper.GetOffset());
1641 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001642 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001643 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1644 size_t offset = 0;
1645 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001646 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001647 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001648 }
1649 }
1650
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001651 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1652 REQUIRES_SHARED(Locks::mutator_lock_) {
1653 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1654 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1655 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1656 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1657 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1658 DCHECK(boot_image_live_objects != nullptr);
1659 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1660 const uint8_t* boot_image_live_objects_address =
1661 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1662 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1663 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1664 boot_image_live_objects->GetLength()).Uint32Value();
1665 return std::make_pair(boot_image_live_objects_address + begin_offset,
1666 boot_image_live_objects_address + end_offset);
1667 }
1668
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001669 void DumpDataBimgRelRoEntries(std::ostream& os) {
1670 os << ".data.bimg.rel.ro: ";
1671 if (oat_file_.GetBootImageRelocations().empty()) {
1672 os << "empty.\n\n";
1673 return;
1674 }
1675
1676 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1677 Runtime* runtime = Runtime::Current();
1678 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1679 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1680 runtime->GetHeap()->GetBootImageSpaces();
1681 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001682 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1683 const uint8_t* live_objects_begin = live_objects.first;
1684 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001685 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1686 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1687 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1688 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001689 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001690 bool found = false;
1691 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001692 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001693 if (local_offset < space->GetImageHeader().GetImageSize()) {
1694 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001695 if (address >= live_objects_begin && address < live_objects_end) {
1696 size_t index =
1697 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1698 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001699 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001700 index);
1701 } else {
1702 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1703 if (o->IsString()) {
1704 os << " String: " << o->AsString()->ToModifiedUtf8();
1705 } else if (o->IsClass()) {
1706 os << " Class: " << o->AsClass()->PrettyDescriptor();
1707 } else {
1708 os << StringPrintf(" 0x%08x %s",
1709 object_offset,
1710 o->GetClass()->PrettyDescriptor().c_str());
1711 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001712 }
1713 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001714 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001715 os << " ArtMethod: " << m->PrettyMethod();
1716 } else {
1717 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1718 object_offset,
1719 space->GetImageFilename().c_str());
1720 }
1721 found = true;
1722 break;
1723 }
1724 }
1725 if (!found) {
1726 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1727 }
1728 os << "\n";
1729 }
1730 } else {
1731 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1732 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1733 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1734 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1735 }
1736 }
1737 os << "\n";
1738 }
1739
Vladimir Markof3c52b42017-11-17 17:32:12 +00001740 template <typename NameGetter>
1741 void DumpBssEntries(std::ostream& os,
1742 const char* slot_type,
1743 const IndexBssMapping* mapping,
1744 uint32_t number_of_indexes,
1745 size_t slot_size,
1746 NameGetter name) {
1747 os << ".bss mapping for " << slot_type << ": ";
1748 if (mapping == nullptr) {
1749 os << "empty.\n";
1750 return;
1751 }
1752 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1753 size_t num_valid_indexes = 0u;
1754 for (const IndexBssMappingEntry& entry : *mapping) {
1755 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1756 }
1757 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1758 os << std::hex;
1759 for (const IndexBssMappingEntry& entry : *mapping) {
1760 uint32_t index = entry.GetIndex(index_bits);
1761 uint32_t mask = entry.GetMask(index_bits);
1762 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1763 for (uint32_t n : LowToHighBits(mask)) {
1764 size_t current_index = index - (32u - index_bits) + n;
1765 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1766 bss_offset += slot_size;
1767 }
1768 DCHECK_EQ(bss_offset, entry.bss_offset);
1769 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1770 }
1771 os << std::dec;
1772 }
1773
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001774 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001775 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001776 const OatDumperOptions& options_;
1777 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001778 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001779 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001780 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001781 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001782 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001783};
1784
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001785class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001786 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001787 ImageDumper(std::ostream* os,
1788 gc::space::ImageSpace& image_space,
1789 const ImageHeader& image_header,
1790 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001791 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001792 vios_(os),
1793 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001794 image_space_(image_space),
1795 image_header_(image_header),
1796 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001797
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001798 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001800 std::ostream& indent_os = vios_.Stream();
1801
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001803
Jeff Haodcdc85b2015-12-04 14:06:18 -08001804 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1805
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001806 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1807 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1808 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001809
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001810 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1811 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1812 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1813 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1814 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1815
1816 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1817 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001818 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001819
1820 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1821 auto section = static_cast<ImageHeader::ImageSections>(i);
1822 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1823 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001824
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001825 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001826 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001827 static_assert(arraysize(image_roots_descriptions_) ==
1828 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001829 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1830 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1832 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001833 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1834 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001835 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001836 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001837 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001838 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001839 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001840 ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001841 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001842 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1843 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001844 run++;
1845 } else {
1846 break;
1847 }
1848 }
1849 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001850 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001851 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001852 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001853 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001854 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001855 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001856 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001857 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001858 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001860 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001861 }
1862 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001863 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001864
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001865 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001866 os << "METHOD ROOTS\n";
1867 static_assert(arraysize(image_methods_descriptions_) ==
1868 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1869 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1870 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1871 const char* description = image_methods_descriptions_[i];
1872 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001873 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001874 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001875 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001876 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001877
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001878 Runtime* const runtime = Runtime::Current();
1879 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001880 std::string image_filename = image_space_.GetImageFilename();
1881 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001882 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001884 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001885 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001886 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001887 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1888 }
1889 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001890 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001891 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001892 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001893 /*executable=*/ false,
1894 /*low_4gb=*/ false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001895 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001896 }
1897 if (oat_file == nullptr) {
1898 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1899 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001900 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001901 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001902
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001903 stats_.oat_file_bytes = oat_file->Size();
1904
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001905 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001906
Andreas Gampeb40d3612018-06-26 15:49:42 -07001907 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001908 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001909 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1910 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001911 }
1912
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001914
Jeff Haodcdc85b2015-12-04 14:06:18 -08001915 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001916 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001917 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001918 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001919 {
1920 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1921 heap->FlushAllocStack();
1922 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001923 // Since FlushAllocStack() above resets the (active) allocation
1924 // stack. Need to revoke the thread-local allocation stacks that
1925 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001926 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001927 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001928 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001929 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001930 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001931 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001932 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001933 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001934 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001935 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001936 ObjPtr<mirror::DexCache> dex_cache =
1937 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001938 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001939 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001940 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001941 }
1942 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001943 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1944 DumpObject(obj);
1945 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001946 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001947 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001948 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001949 indent_os << "\n";
1950 // TODO: Dump fields.
1951 // Dump methods after.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -07001952 image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1953 REQUIRES_SHARED(Locks::mutator_lock_) {
1954 std::ostream& indent_os = vios_.Stream();
1955 indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1956 DumpMethod(&method, indent_os);
1957 indent_os << "\n";
1958 }, image_space_.Begin(), image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001959 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001960 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001961 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001962 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001963 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001964 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001965 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1966 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001967 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001968 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001969 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001970 // If the image is compressed, adjust to decompressed size.
1971 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartiera5c01d72019-07-26 14:39:28 -07001972 if (!image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001973 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1974 }
1975 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001976 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001977 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001978 const auto& object_section = image_header_.GetObjectsSection();
1979 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001981 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1982 const auto& intern_section = image_header_.GetInternedStringsSection();
1983 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07001984 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1985 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001986 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001987
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001988 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001989
1990 // Objects are kObjectAlignment-aligned.
1991 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1992 if (object_section.Offset() > header_bytes) {
1993 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1994 }
1995
1996 // Field section is 4-byte aligned.
1997 constexpr size_t kFieldSectionAlignment = 4U;
1998 uint32_t end_objects = object_section.Offset() + object_section.Size();
1999 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2000 stats_.alignment_bytes += field_section.Offset() - end_objects;
2001
2002 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2003 uint32_t end_fields = field_section.Offset() + field_section.Size();
2004 CHECK_ALIGNED(method_section.Offset(), 4);
2005 stats_.alignment_bytes += method_section.Offset() - end_fields;
2006
2007 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2008 uint32_t end_methods = method_section.Offset() + method_section.Size();
2009 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2010 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2011
2012 // Intern table is 8-byte aligned.
2013 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002014 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002015 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2016
2017 // Add space between intern table and class table.
2018 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2019 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2020
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002021 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2022 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002023 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002024 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002025
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 stats_.bitmap_bytes += bitmap_section.Size();
2027 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002028 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002029 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002030 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002031 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002032 stats_.sro_offset_bytes += sro_section.Size();
2033 stats_.metadata_bytes += metadata_section.Size();
2034
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002035 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002036 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002037
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002038 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002039
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002040 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002041 }
2042
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002043 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -07002044 static void PrettyObjectValue(std::ostream& os,
2045 ObjPtr<mirror::Class> type,
2046 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002047 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002048 CHECK(type != nullptr);
2049 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002050 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002051 } else if (type->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002052 ObjPtr<mirror::String> string = value->AsString();
2053 os << StringPrintf("%p String: %s\n",
2054 string.Ptr(),
Ian Rogers68b56852014-08-29 20:19:11 -07002055 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002056 } else if (type->IsClassClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002057 ObjPtr<mirror::Class> klass = value->AsClass();
2058 os << StringPrintf("%p Class: %s\n",
2059 klass.Ptr(),
2060 mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002061 } else {
David Sehr709b0702016-10-13 09:12:37 -07002062 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002063 }
2064 }
2065
Mathieu Chartier3398c782016-09-30 10:27:43 -07002066 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002067 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002068 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002069 switch (field->GetTypeAsPrimitiveType()) {
2070 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002071 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002072 break;
2073 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002074 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002075 break;
2076 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002077 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002078 break;
2079 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002081 break;
2082 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002083 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002084 break;
2085 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002086 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002087 break;
2088 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002089 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002090 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 break;
2092 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002093 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002094 break;
2095 case Primitive::kPrimNot: {
2096 // Get the value, don't compute the type unless it is non-null as we don't want
2097 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002098 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002099 if (value == nullptr) {
2100 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002101 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002102 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002103 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002104 if (field_type != nullptr) {
2105 PrettyObjectValue(os, field_type, value);
2106 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002107 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002108 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002109 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2110 }
Ian Rogers50239c72013-06-17 14:53:22 -07002111 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002112 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002113 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002114 default:
2115 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2116 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002117 }
2118 }
2119
Andreas Gampe98104992018-10-16 12:49:47 -07002120 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002121 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002122 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002123 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002124 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002125 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002126 for (ArtField& field : klass->GetIFields()) {
2127 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002128 }
2129 }
2130
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002131 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002132 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002133 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002134
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002135 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002136 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002137 image_header_.GetPointerSize());
David Srbeckya54e0cc2019-04-15 15:03:26 +01002138 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2139 if (class_linker->IsQuickResolutionStub(quick_code) ||
2140 class_linker->IsQuickToInterpreterBridge(quick_code) ||
2141 class_linker->IsQuickGenericJniStub(quick_code) ||
Vladimir Markofa458ac2020-02-12 14:08:07 +00002142 class_linker->IsJniDlsymLookupStub(quick_code) ||
2143 class_linker->IsJniDlsymLookupCriticalStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002144 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002145 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002146 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002147 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002148 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002149 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002150 }
2151
Mathieu Chartiere401d142015-04-22 13:56:20 -07002152 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002153 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002154 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2155 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002156 return 0;
2157 }
David Srbeckya54e0cc2019-04-15 15:03:26 +01002158 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2159 reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2160 return method_header->GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002161 }
2162
Mathieu Chartiere401d142015-04-22 13:56:20 -07002163 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002164 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002165 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002166 if (oat_code_begin == nullptr) {
2167 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002168 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002169 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002170 }
2171
Andreas Gampe0c183382017-07-13 22:26:24 -07002172 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002173 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002174 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002175 return;
2176 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002177
2178 size_t object_bytes = obj->SizeOf();
2179 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002180 stats_.object_bytes += object_bytes;
2181 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002182
Andreas Gampe0c183382017-07-13 22:26:24 -07002183 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002184
Vladimir Marko4617d582019-03-28 13:48:31 +00002185 ObjPtr<mirror::Class> obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002186 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002187 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002188 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002189 } else if (obj->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002190 ObjPtr<mirror::Class> klass = obj->AsClass();
2191 os << StringPrintf("%p: java.lang.Class \"%s\" (",
2192 obj,
David Sehr709b0702016-10-13 09:12:37 -07002193 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002194 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002195 } else if (obj_class->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002196 os << StringPrintf("%p: java.lang.String %s\n",
2197 obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002198 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002199 } else {
David Sehr709b0702016-10-13 09:12:37 -07002200 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002201 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002202 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002203 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002204 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002205 if (obj->IsObjectArray()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002206 ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002207 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00002208 ObjPtr<mirror::Object> value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002209 size_t run = 0;
2210 for (int32_t j = i + 1; j < length; j++) {
2211 if (value == obj_array->Get(j)) {
2212 run++;
2213 } else {
2214 break;
2215 }
2216 }
2217 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002218 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002219 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002220 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002221 i = i + run;
2222 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002223 ObjPtr<mirror::Class> value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002224 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002225 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002226 }
2227 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002228 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002229
Vladimir Marko305c38b2018-02-14 11:50:07 +00002230 if (kBitstringSubtypeCheckEnabled) {
2231 os << "SUBTYPE_CHECK_BITS: ";
2232 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2233 os << "\n";
2234 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002235
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002236 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002237 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002238 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002239 for (ArtField& field : klass->GetSFields()) {
2240 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002241 }
2242 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002243 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002244 auto it = dex_caches_.find(obj);
2245 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002246 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002247 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002248 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002249 size_t num_methods = dex_cache->NumResolvedMethods();
2250 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002251 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002252 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002253 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002254 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002255 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2256 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002257 size_t run = 0;
2258 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002259 j != length &&
2260 elem == mirror::DexCache::GetNativePairPtrSize(
2261 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002262 ++j) {
2263 ++run;
2264 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002265 if (run == 0) {
2266 os << StringPrintf("%zd: ", i);
2267 } else {
2268 os << StringPrintf("%zd to %zd: ", i, i + run);
2269 i = i + run;
2270 }
2271 std::string msg;
2272 if (elem == nullptr) {
2273 msg = "null";
2274 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002275 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002276 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002277 } else {
2278 msg = "<not in method section>";
2279 }
2280 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002281 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002282 }
2283 size_t num_fields = dex_cache->NumResolvedFields();
2284 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002285 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002286 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002287 auto* resolved_fields = dex_cache->GetResolvedFields();
2288 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002289 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002290 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002291 size_t run = 0;
2292 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002293 j != length &&
2294 elem == mirror::DexCache::GetNativePairPtrSize(
2295 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002296 ++j) {
2297 ++run;
2298 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002299 if (run == 0) {
2300 os << StringPrintf("%zd: ", i);
2301 } else {
2302 os << StringPrintf("%zd to %zd: ", i, i + run);
2303 i = i + run;
2304 }
2305 std::string msg;
2306 if (elem == nullptr) {
2307 msg = "null";
2308 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002309 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002310 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002311 } else {
2312 msg = "<not in field section>";
2313 }
2314 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002316 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002317 size_t num_types = dex_cache->NumResolvedTypes();
2318 if (num_types != 0u) {
2319 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002320 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002321 auto* resolved_types = dex_cache->GetResolvedTypes();
2322 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002323 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002324 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002325 for (size_t j = i + 1; j != num_types; ++j) {
2326 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2327 if (pair.index != other_pair.index ||
2328 pair.object.Read() != other_pair.object.Read()) {
2329 break;
2330 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002331 ++run;
2332 }
2333 if (run == 0) {
2334 os << StringPrintf("%zd: ", i);
2335 } else {
2336 os << StringPrintf("%zd to %zd: ", i, i + run);
2337 i = i + run;
2338 }
2339 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002340 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002341 if (elem == nullptr) {
2342 msg = "null";
2343 } else {
David Sehr709b0702016-10-13 09:12:37 -07002344 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002345 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002346 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002347 }
2348 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002349 }
2350 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002351 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002352 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002353 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002354
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002355 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002356 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 DCHECK(method != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002358 const PointerSize pointer_size = image_header_.GetPointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 if (method->IsNative()) {
David Srbeckya54e0cc2019-04-15 15:03:26 +01002360 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002362 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2363 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002364 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002365 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002366 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002367 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2368 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002369 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002370 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002371 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002372 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002373 } else if (method->IsRuntimeMethod()) {
2374 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2375 if (table != nullptr) {
2376 indent_os << "IMT conflict table " << table << " method: ";
2377 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002378 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2379 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002380 }
2381 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002383 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002384 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002385 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002386
David Srbeckya54e0cc2019-04-15 15:03:26 +01002387 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2388 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2389
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002391 size_t vmap_table_bytes = 0u;
David Srbeckya54e0cc2019-04-15 15:03:26 +01002392 if (quick_oat_code_begin != nullptr) {
2393 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2394 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2395 vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2396 &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002397 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002398 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002399 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002400 }
2401
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002402 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2403 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002404 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002405 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002406 if (method->IsConstructor()) {
2407 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002408 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002409 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002410 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002411 }
2412 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002413 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414 }
2415 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002416 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002417
Igor Murashkin7617abd2015-07-10 18:27:47 -07002418 uint32_t method_access_flags = method->GetAccessFlags();
2419
Mathieu Chartiere401d142015-04-22 13:56:20 -07002420 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002421 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2422 dex_instruction_bytes,
2423 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002424 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002425
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002426 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002427 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002428
2429 double expansion =
2430 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002431 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002432 }
2433 }
2434
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002435 std::set<const void*> already_seen_;
2436 // Compute the size of the given data within the oat file and whether this is the first time
2437 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002438 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002439 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002440 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002441 already_seen_.insert(oat_data);
2442 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002443 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002444 }
2445 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002446 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002447
2448 public:
2449 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002450 size_t oat_file_bytes = 0u;
2451 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002452
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002453 size_t header_bytes = 0u;
2454 size_t object_bytes = 0u;
2455 size_t art_field_bytes = 0u;
2456 size_t art_method_bytes = 0u;
2457 size_t dex_cache_arrays_bytes = 0u;
2458 size_t interned_strings_bytes = 0u;
2459 size_t class_table_bytes = 0u;
2460 size_t sro_offset_bytes = 0u;
2461 size_t metadata_bytes = 0u;
2462 size_t bitmap_bytes = 0u;
2463 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002464
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002465 size_t managed_code_bytes = 0u;
2466 size_t managed_code_bytes_ignoring_deduplication = 0u;
2467 size_t native_to_managed_code_bytes = 0u;
2468 size_t class_initializer_code_bytes = 0u;
2469 size_t large_initializer_code_bytes = 0u;
2470 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002471
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002472 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002473
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002474 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475
Mathieu Chartiere401d142015-04-22 13:56:20 -07002476 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002477 std::vector<size_t> method_outlier_size;
2478 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002479 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002480
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002481 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002482
Elliott Hughesa0e18062012-04-13 15:59:59 -07002483 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002484 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002485 size_t bytes;
2486 size_t count;
2487 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002488 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002489 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002490
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002491 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002492 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2493 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002494 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002495 it->second.count += 1;
2496 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002497 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002498 }
2499 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002500
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002501 double PercentOfOatBytes(size_t size) {
2502 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2503 }
2504
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002505 double PercentOfFileBytes(size_t size) {
2506 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2507 }
2508
2509 double PercentOfObjectBytes(size_t size) {
2510 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2511 }
2512
Mathieu Chartiere401d142015-04-22 13:56:20 -07002513 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002514 method_outlier_size.push_back(total_size);
2515 method_outlier_expansion.push_back(expansion);
2516 method_outlier.push_back(method);
2517 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002518
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002519 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002520 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002521 size_t sum_of_sizes = 0;
2522 size_t sum_of_sizes_squared = 0;
2523 size_t sum_of_expansion = 0;
2524 size_t sum_of_expansion_squared = 0;
2525 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002526 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002527 return;
2528 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002529 for (size_t i = 0; i < n; i++) {
2530 size_t cur_size = method_outlier_size[i];
2531 sum_of_sizes += cur_size;
2532 sum_of_sizes_squared += cur_size * cur_size;
2533 double cur_expansion = method_outlier_expansion[i];
2534 sum_of_expansion += cur_expansion;
2535 sum_of_expansion_squared += cur_expansion * cur_expansion;
2536 }
2537 size_t size_mean = sum_of_sizes / n;
2538 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2539 double expansion_mean = sum_of_expansion / n;
2540 double expansion_variance =
2541 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2542
2543 // Dump methods whose size is a certain number of standard deviations from the mean
2544 size_t dumped_values = 0;
2545 size_t skipped_values = 0;
2546 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2547 size_t cur_size_variance = i * i * size_variance;
2548 bool first = true;
2549 for (size_t j = 0; j < n; j++) {
2550 size_t cur_size = method_outlier_size[j];
2551 if (cur_size > size_mean) {
2552 size_t cur_var = cur_size - size_mean;
2553 cur_var = cur_var * cur_var;
2554 if (cur_var > cur_size_variance) {
2555 if (dumped_values > 20) {
2556 if (i == 1) {
2557 skipped_values++;
2558 } else {
2559 i = 2; // jump to counting for 1 standard deviation
2560 break;
2561 }
2562 } else {
2563 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002564 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002565 first = false;
2566 }
David Sehr709b0702016-10-13 09:12:37 -07002567 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002568 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002569 method_outlier_size[j] = 0; // don't consider this method again
2570 dumped_values++;
2571 }
2572 }
2573 }
2574 }
2575 }
2576 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002578 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002579 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002580 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002581
2582 // Dump methods whose expansion is a certain number of standard deviations from the mean
2583 dumped_values = 0;
2584 skipped_values = 0;
2585 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2586 double cur_expansion_variance = i * i * expansion_variance;
2587 bool first = true;
2588 for (size_t j = 0; j < n; j++) {
2589 double cur_expansion = method_outlier_expansion[j];
2590 if (cur_expansion > expansion_mean) {
2591 size_t cur_var = cur_expansion - expansion_mean;
2592 cur_var = cur_var * cur_var;
2593 if (cur_var > cur_expansion_variance) {
2594 if (dumped_values > 20) {
2595 if (i == 1) {
2596 skipped_values++;
2597 } else {
2598 i = 2; // jump to counting for 1 standard deviation
2599 break;
2600 }
2601 } else {
2602 if (first) {
2603 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002604 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002605 first = false;
2606 }
David Sehr709b0702016-10-13 09:12:37 -07002607 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002608 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002609 method_outlier_expansion[j] = 0.0; // don't consider this method again
2610 dumped_values++;
2611 }
2612 }
2613 }
2614 }
2615 }
2616 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002617 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002618 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002619 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002620 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002621 }
2622
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002623 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002624 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002625 {
2626 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2627 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002628 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2629 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2630 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2631 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2632 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2633 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002634 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002635 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2636 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002637 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2638 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002639 header_bytes, PercentOfFileBytes(header_bytes),
2640 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002641 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2642 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002643 dex_cache_arrays_bytes,
2644 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002645 interned_strings_bytes,
2646 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002647 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002648 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2649 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002650 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002651 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2652 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002653 CHECK_EQ(file_bytes,
2654 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2655 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002656 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002657 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002658
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002660 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002661 for (const auto& sizes_and_count : sizes_and_counts) {
2662 const std::string& descriptor(sizes_and_count.first);
2663 double average = static_cast<double>(sizes_and_count.second.bytes) /
2664 static_cast<double>(sizes_and_count.second.count);
2665 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002667 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002668 descriptor.c_str(), sizes_and_count.second.bytes,
2669 sizes_and_count.second.count, average, percent);
2670 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002671 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002672 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002673 CHECK_EQ(object_bytes, object_bytes_total);
2674
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002675 os << StringPrintf("oat_file_bytes = %8zd\n"
2676 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002677 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2678 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2679 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2680 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002681 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002682 managed_code_bytes,
2683 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002684 native_to_managed_code_bytes,
2685 PercentOfOatBytes(native_to_managed_code_bytes),
2686 class_initializer_code_bytes,
2687 PercentOfOatBytes(class_initializer_code_bytes),
2688 large_initializer_code_bytes,
2689 PercentOfOatBytes(large_initializer_code_bytes),
2690 large_method_code_bytes,
2691 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002692 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002693 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002695 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2696 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002697 }
2698
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002699 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002700 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002701 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002702
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2704 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002705 static_cast<double>(managed_code_bytes) /
2706 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002707 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002708 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002709 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002710
2711 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002712 }
2713 } stats_;
2714
2715 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002716 enum {
2717 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2718 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2719 kLargeConstructorDexBytes = 4000,
2720 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2721 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2722 kLargeMethodDexBytes = 16000
2723 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002724
2725 // For performance, use the *os_ directly for anything that doesn't need indentation
2726 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002727 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002728 VariableIndentationOutputStream vios_;
2729 ScopedIndentation indent1_;
2730
Ian Rogers1d54e732013-05-02 21:10:01 -07002731 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002732 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002733 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002734 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002735 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002736
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002737 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002738};
2739
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002740static int DumpImage(gc::space::ImageSpace* image_space,
2741 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002742 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002743 const ImageHeader& image_header = image_space->GetImageHeader();
2744 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002745 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002746 return EXIT_FAILURE;
2747 }
2748 ImageDumper image_dumper(os, *image_space, image_header, options);
2749 if (!image_dumper.Dump()) {
2750 return EXIT_FAILURE;
2751 }
2752 return EXIT_SUCCESS;
2753}
2754
2755static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002756 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002757 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758 options->class_loader_ = &null_class_loader;
2759
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002760 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002761 if (options->app_image_ != nullptr) {
2762 if (options->app_oat_ == nullptr) {
2763 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002764 return EXIT_FAILURE;
2765 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002766 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2767 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2768 // pointers into 32 bit pointer sized ArtMethods.
2769 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002770 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002771 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002772 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002773 /*executable=*/ false,
2774 /*low_4gb=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002775 &error_msg));
2776 if (oat_file == nullptr) {
2777 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002778 return EXIT_FAILURE;
2779 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002780 std::unique_ptr<gc::space::ImageSpace> space(
2781 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2782 if (space == nullptr) {
2783 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2784 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002785 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002786 }
2787 // Open dex files for the image.
2788 std::vector<std::unique_ptr<const DexFile>> dex_files;
2789 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2790 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2791 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002792 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002793 }
2794 // Dump the actual image.
2795 int result = DumpImage(space.get(), options, os);
2796 if (result != EXIT_SUCCESS) {
2797 return result;
2798 }
2799 // Fall through to dump the boot images.
2800 }
2801
2802 gc::Heap* heap = runtime->GetHeap();
Vladimir Markodd09f712019-12-06 12:37:10 +00002803 if (!heap->HasBootImageSpace()) {
2804 LOG(ERROR) << "No image spaces";
2805 return EXIT_FAILURE;
2806 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002807 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2808 int result = DumpImage(image_space, options, os);
2809 if (result != EXIT_SUCCESS) {
2810 return result;
2811 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002812 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002813 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002814}
2815
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002816static jobject InstallOatFile(Runtime* runtime,
2817 std::unique_ptr<OatFile> oat_file,
2818 std::vector<const DexFile*>* class_path)
2819 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002820 Thread* self = Thread::Current();
2821 CHECK(self != nullptr);
2822 // Need well-known-classes.
2823 WellKnownClasses::Init(self->GetJniEnv());
2824
Vladimir Marko421087b2018-02-27 11:00:17 +00002825 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002826 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002827 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002828 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002829 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002830 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002831 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002832 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002833 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002834 }
2835
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002836 // Need a class loader. Fake that we're a compiler.
2837 // Note: this will run initializers through the unstarted runtime, so make sure it's
2838 // initialized.
2839 interpreter::UnstartedRuntime::Initialize();
2840
2841 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2842
Vladimir Marko421087b2018-02-27 11:00:17 +00002843 // Need to register dex files to get a working dex cache.
2844 for (const DexFile* dex_file : *class_path) {
2845 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2846 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2847 CHECK(dex_cache != nullptr);
2848 }
2849
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002850 return class_loader;
2851}
2852
2853static int DumpOatWithRuntime(Runtime* runtime,
2854 std::unique_ptr<OatFile> oat_file,
2855 OatDumperOptions* options,
2856 std::ostream* os) {
2857 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2858 ScopedObjectAccess soa(Thread::Current());
2859
2860 OatFile* oat_file_ptr = oat_file.get();
2861 std::vector<const DexFile*> class_path;
2862 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002863
2864 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002865 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002866 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002867 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002868 options->class_loader_ = &loader_handle;
2869
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002870 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002871 bool success = oat_dumper.Dump(*os);
2872 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2873}
2874
2875static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002876 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002877 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002878 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002879 options->class_loader_ = &null_class_loader;
2880
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002881 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002882 bool success = oat_dumper.Dump(*os);
2883 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2884}
2885
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002886static int DumpOat(Runtime* runtime,
2887 const char* oat_filename,
2888 const char* dex_filename,
2889 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002890 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002891 if (dex_filename == nullptr) {
2892 LOG(WARNING) << "No dex filename provided, "
2893 << "oatdump might fail if the oat file does not contain the dex code.";
2894 }
Vladimir Markob7bf8432019-12-03 13:18:50 +00002895 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2896 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2897 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002898 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002899 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002900 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002901 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002902 /*executable=*/ false,
2903 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002904 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002905 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002906 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002907 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002908 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002909 return EXIT_FAILURE;
2910 }
2911
2912 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002913 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002914 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002915 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002916 }
2917}
2918
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002919static int SymbolizeOat(const char* oat_filename,
2920 const char* dex_filename,
2921 std::string& output_name,
2922 bool no_bits) {
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 Gampe08c277c2017-04-26 22:22:15 -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 Gampe08c277c2017-04-26 22:22:15 -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
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002940 bool result;
2941 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2942 // files for 64-bit code in the past.
2943 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002944 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002945 result = oat_symbolizer.Symbolize();
2946 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002947 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002948 result = oat_symbolizer.Symbolize();
2949 }
2950 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002951 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002952 return EXIT_FAILURE;
2953 }
2954
2955 return EXIT_SUCCESS;
2956}
2957
Andreas Gampe9fded872016-09-25 16:08:35 -07002958class IMTDumper {
2959 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002960 static bool Dump(Runtime* runtime,
2961 const std::string& imt_file,
2962 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002963 const char* oat_filename,
2964 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002965 Thread* self = Thread::Current();
2966
2967 ScopedObjectAccess soa(self);
2968 StackHandleScope<1> scope(self);
2969 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2970 std::vector<const DexFile*> class_path;
2971
2972 if (oat_filename != nullptr) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002973 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2974 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2975 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002976 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002977 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002978 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002979 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002980 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002981 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002982 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002983 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002984 &error_msg));
2985 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002986 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002987 return false;
2988 }
2989
2990 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2991 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2992 } else {
2993 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2994 class_path = runtime->GetClassLinker()->GetBootClassPath();
2995 }
2996
2997 if (!imt_file.empty()) {
2998 return DumpImt(runtime, imt_file, class_loader);
2999 }
3000
3001 if (dump_imt_stats) {
3002 return DumpImtStats(runtime, class_path, class_loader);
3003 }
3004
3005 LOG(FATAL) << "Should not reach here";
3006 UNREACHABLE();
3007 }
3008
3009 private:
3010 static bool DumpImt(Runtime* runtime,
3011 const std::string& imt_file,
3012 Handle<mirror::ClassLoader> h_class_loader)
3013 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003014 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3015 std::unordered_set<std::string> prepared;
3016
3017 for (const std::string& line : lines) {
3018 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3019 // or a class descriptor and an interface method, in which case we will lookup the method,
3020 // determine its IMT slot, and check the class' IMT.
3021 size_t first_space = line.find(' ');
3022 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003023 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003024 } else {
3025 DumpIMTForMethod(runtime,
3026 line.substr(0, first_space),
3027 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003028 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003029 &prepared);
3030 }
3031 std::cerr << std::endl;
3032 }
3033
3034 return true;
3035 }
3036
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003037 static bool DumpImtStats(Runtime* runtime,
3038 const std::vector<const DexFile*>& dex_files,
3039 Handle<mirror::ClassLoader> h_class_loader)
3040 REQUIRES_SHARED(Locks::mutator_lock_) {
3041 size_t without_imt = 0;
3042 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003043 std::map<size_t, size_t> histogram;
3044
3045 ClassLinker* class_linker = runtime->GetClassLinker();
3046 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3047 std::unordered_set<std::string> prepared;
3048
3049 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003050 StackHandleScope<1> scope(self);
3051 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3052
3053 for (const DexFile* dex_file : dex_files) {
3054 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003055 class_def_index != dex_file->NumClassDefs();
3056 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003057 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07003058 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003059 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003060 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003061 std::cerr << "Warning: could not load " << descriptor << std::endl;
3062 continue;
3063 }
3064
3065 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003066 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003067 continue;
3068 }
3069
3070 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3071 if (im_table == nullptr) {
3072 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003073 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003074 continue;
3075 }
3076
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003077 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003078 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3079 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3080 if (ptr->IsRuntimeMethod()) {
3081 if (ptr->IsImtUnimplementedMethod()) {
3082 histogram[0]++;
3083 } else {
3084 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3085 histogram[current_table->NumEntries(pointer_size)]++;
3086 }
3087 } else {
3088 histogram[1]++;
3089 }
3090 }
3091 }
3092 }
3093
3094 std::cerr << "IMT stats:"
3095 << std::endl << std::endl;
3096
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003097 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003098 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003099 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003100 << std::endl << std::endl;
3101
3102 double sum_one = 0;
3103 size_t count_one = 0;
3104
3105 std::cerr << " " << "IMT histogram" << std::endl;
3106 for (auto& bucket : histogram) {
3107 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3108 if (bucket.first > 0) {
3109 sum_one += bucket.second * bucket.first;
3110 count_one += bucket.second;
3111 }
3112 }
3113
3114 double count_zero = count_one + histogram[0];
3115 std::cerr << " Stats:" << std::endl;
3116 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3117 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3118
3119 return true;
3120 }
3121
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003122 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003123 static bool HasNoIMT(Runtime* runtime,
3124 Handle<mirror::Class> klass,
3125 const PointerSize pointer_size,
3126 std::unordered_set<std::string>* prepared)
3127 REQUIRES_SHARED(Locks::mutator_lock_) {
3128 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3129 return true;
3130 }
3131
3132 if (klass->GetImt(pointer_size) == nullptr) {
3133 PrepareClass(runtime, klass, prepared);
3134 }
3135
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003136 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003137 DCHECK(object_class->IsObjectClass());
3138
3139 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3140
Mathieu Chartier6beced42016-11-15 15:51:31 -08003141 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003142 DCHECK(result);
3143 }
3144
3145 return result;
3146 }
3147
3148 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3149 REQUIRES_SHARED(Locks::mutator_lock_) {
3150 if (table == nullptr) {
3151 std::cerr << " <No IMT?>" << std::endl;
3152 return;
3153 }
3154 size_t table_index = 0;
3155 for (;;) {
3156 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3157 if (ptr == nullptr) {
3158 return;
3159 }
3160 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003161 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003162 }
3163 }
3164
3165 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3166 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003167 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003168 const std::string& class_name,
3169 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003170 /*out*/ ObjPtr<mirror::Class>* klass_out,
3171 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003172 REQUIRES_SHARED(Locks::mutator_lock_) {
3173 if (class_name.empty()) {
3174 return nullptr;
3175 }
3176
3177 std::string descriptor;
3178 if (class_name[0] == 'L') {
3179 descriptor = class_name;
3180 } else {
3181 descriptor = DotToDescriptor(class_name.c_str());
3182 }
3183
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003184 ObjPtr<mirror::Class> klass =
3185 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003186
3187 if (klass == nullptr) {
3188 self->ClearException();
3189 std::cerr << "Did not find " << class_name << std::endl;
3190 *klass_out = nullptr;
3191 return nullptr;
3192 }
3193
3194 StackHandleScope<1> scope(Thread::Current());
3195 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3196
3197 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3198 *klass_out = h_klass.Get();
3199 return ret;
3200 }
3201
3202 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3203 Handle<mirror::Class> h_klass,
3204 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003205 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003206 REQUIRES_SHARED(Locks::mutator_lock_) {
3207 PrepareClass(runtime, h_klass, prepared);
3208 return h_klass->GetImt(pointer_size);
3209 }
3210
3211 static void DumpIMTForClass(Runtime* runtime,
3212 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003213 Handle<mirror::ClassLoader> h_loader,
3214 std::unordered_set<std::string>* prepared)
3215 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003216 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003217 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003218 ImTable* imt = PrepareAndGetImTable(runtime,
3219 Thread::Current(),
3220 h_loader,
3221 class_name,
3222 pointer_size,
3223 &klass,
3224 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003225 if (imt == nullptr) {
3226 return;
3227 }
3228
3229 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3230 for (size_t index = 0; index < ImTable::kSize; ++index) {
3231 std::cerr << " " << index << ":" << std::endl;
3232 ArtMethod* ptr = imt->Get(index, pointer_size);
3233 if (ptr->IsRuntimeMethod()) {
3234 if (ptr->IsImtUnimplementedMethod()) {
3235 std::cerr << " <empty>" << std::endl;
3236 } else {
3237 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3238 PrintTable(current_table, pointer_size);
3239 }
3240 } else {
David Sehr709b0702016-10-13 09:12:37 -07003241 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003242 }
3243 }
3244
3245 std::cerr << " Interfaces:" << std::endl;
3246 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003247 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003248 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003249 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003250 std::string iface_name;
3251 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003252
Mathieu Chartier6beced42016-11-15 15:51:31 -08003253 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3254 uint32_t class_hash, name_hash, signature_hash;
3255 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3256 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3257 std::cerr << " " << iface_method.PrettyMethod(true)
3258 << " slot=" << imt_slot
3259 << std::hex
3260 << " class_hash=0x" << class_hash
3261 << " name_hash=0x" << name_hash
3262 << " signature_hash=0x" << signature_hash
3263 << std::dec
3264 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003265 }
3266 }
3267 }
3268
3269 static void DumpIMTForMethod(Runtime* runtime,
3270 const std::string& class_name,
3271 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003272 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003273 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003274 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003275 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003276 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003277 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003278 Thread::Current(),
3279 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003280 class_name,
3281 pointer_size,
3282 &klass,
3283 prepared);
3284 if (imt == nullptr) {
3285 return;
3286 }
3287
3288 std::cerr << class_name << " <" << method << ">" << std::endl;
3289 for (size_t index = 0; index < ImTable::kSize; ++index) {
3290 ArtMethod* ptr = imt->Get(index, pointer_size);
3291 if (ptr->IsRuntimeMethod()) {
3292 if (ptr->IsImtUnimplementedMethod()) {
3293 continue;
3294 }
3295
3296 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3297 if (current_table == nullptr) {
3298 continue;
3299 }
3300
3301 size_t table_index = 0;
3302 for (;;) {
3303 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3304 if (ptr2 == nullptr) {
3305 break;
3306 }
3307 table_index++;
3308
David Sehr709b0702016-10-13 09:12:37 -07003309 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003310 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003311 std::cerr << " Slot "
3312 << index
3313 << " ("
3314 << current_table->NumEntries(pointer_size)
3315 << ")"
3316 << std::endl;
3317 PrintTable(current_table, pointer_size);
3318 return;
3319 }
3320 }
3321 } else {
David Sehr709b0702016-10-13 09:12:37 -07003322 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003323 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003324 std::cerr << " Slot " << index << " (1)" << std::endl;
3325 std::cerr << " " << p_name << std::endl;
3326 } else {
3327 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003328 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003329 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003330 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003331 size_t num_methods = iface->NumDeclaredVirtualMethods();
3332 if (num_methods > 0) {
3333 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3334 if (ImTable::GetImtIndex(&iface_method) == index) {
3335 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003336 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003337 std::cerr << " Slot " << index << " (1)" << std::endl;
3338 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003339 }
3340 }
3341 }
3342 }
3343 }
3344 }
3345 }
3346 }
3347 }
3348
3349 // Read lines from the given stream, dropping comments and empty lines
3350 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3351 std::vector<std::string> output;
3352 while (in_stream.good()) {
3353 std::string dot;
3354 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003355 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003356 continue;
3357 }
3358 output.push_back(dot);
3359 }
3360 return output;
3361 }
3362
3363 // Read lines from the given file, dropping comments and empty lines.
3364 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3365 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3366 if (input_file.get() == nullptr) {
3367 LOG(ERROR) << "Failed to open input file " << input_filename;
3368 return std::vector<std::string>();
3369 }
3370 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3371 input_file->close();
3372 return result;
3373 }
3374
3375 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3376 // and note in the given set that the work was done.
3377 static void PrepareClass(Runtime* runtime,
3378 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003379 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003380 REQUIRES_SHARED(Locks::mutator_lock_) {
3381 if (!h_klass->ShouldHaveImt()) {
3382 return;
3383 }
3384
3385 std::string name;
3386 name = h_klass->GetDescriptor(&name);
3387
3388 if (done->find(name) != done->end()) {
3389 return;
3390 }
3391 done->insert(name);
3392
3393 if (h_klass->HasSuperClass()) {
3394 StackHandleScope<1> h(Thread::Current());
3395 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3396 }
3397
3398 if (!h_klass->IsTemp()) {
3399 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3400 }
3401 }
3402};
3403
Igor Murashkin37743352014-11-13 14:38:00 -08003404struct OatdumpArgs : public CmdlineArgs {
3405 protected:
3406 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003407
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003408 ParseStatus ParseCustom(const char* raw_option,
3409 size_t raw_option_length,
3410 std::string* error_msg) override {
3411 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003412 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003413 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003414 if (base_parse != kParseUnknownArgument) {
3415 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003416 }
3417 }
3418
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003419 std::string_view option(raw_option, raw_option_length);
3420 if (StartsWith(option, "--oat-file=")) {
3421 oat_filename_ = raw_option + strlen("--oat-file=");
3422 } else if (StartsWith(option, "--dex-file=")) {
3423 dex_filename_ = raw_option + strlen("--dex-file=");
3424 } else if (StartsWith(option, "--image=")) {
3425 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003426 } else if (option == "--no-dump:vmap") {
3427 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003428 } else if (option =="--dump:code_info_stack_maps") {
3429 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003430 } else if (option == "--no-disassemble") {
3431 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003432 } else if (option =="--header-only") {
3433 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003434 } else if (StartsWith(option, "--symbolize=")) {
3435 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003436 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003437 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003438 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003439 } else if (StartsWith(option, "--class-filter=")) {
3440 class_filter_ = raw_option + strlen("--class-filter=");
3441 } else if (StartsWith(option, "--method-filter=")) {
3442 method_filter_ = raw_option + strlen("--method-filter=");
3443 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003444 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003445 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003446 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003447 } else if (StartsWith(option, "--export-dex-to=")) {
3448 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3449 } else if (StartsWith(option, "--addr2instr=")) {
3450 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003451 *error_msg = "Address conversion failed";
3452 return kParseError;
3453 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003454 } else if (StartsWith(option, "--app-image=")) {
3455 app_image_ = raw_option + strlen("--app-image=");
3456 } else if (StartsWith(option, "--app-oat=")) {
3457 app_oat_ = raw_option + strlen("--app-oat=");
3458 } else if (StartsWith(option, "--dump-imt=")) {
3459 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003460 } else if (option == "--dump-imt-stats") {
3461 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003462 } else {
3463 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003464 }
3465
Igor Murashkin37743352014-11-13 14:38:00 -08003466 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003467 }
3468
Roland Levillainf73caca2018-08-24 17:19:07 +01003469 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003470 // Infer boot image location from the image location if possible.
3471 if (boot_image_location_ == nullptr) {
3472 boot_image_location_ = image_location_;
3473 }
3474
3475 // Perform the parent checks.
3476 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3477 if (parent_checks != kParseOk) {
3478 return parent_checks;
3479 }
3480
3481 // Perform our own checks.
3482 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3483 *error_msg = "Either --image or --oat-file must be specified";
3484 return kParseError;
3485 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3486 *error_msg = "Either --image or --oat-file must be specified but not both";
3487 return kParseError;
3488 }
3489
3490 return kParseOk;
3491 }
3492
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003493 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003494 std::string usage;
3495
3496 usage +=
3497 "Usage: oatdump [options] ...\n"
3498 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3499 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3500 "\n"
3501 // Either oat-file or image is required.
3502 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Mathieu Chartier2afa19d2019-09-10 16:07:21 -07003503 " Example: --oat-file=/system/framework/arm64/boot.oat\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003504 "\n"
3505 " --image=<file.art>: specifies an input image location.\n"
3506 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003507 "\n"
3508 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003509 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003510 " Example: --app-image=app.art\n"
3511 "\n"
3512 " --app-oat=<file.odex>: specifies an input app oat.\n"
3513 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003514 "\n";
3515
3516 usage += Base::GetUsage();
3517
3518 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003519 " --no-dump:vmap may be used to disable vmap dumping.\n"
3520 " Example: --no-dump:vmap\n"
3521 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003522 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3523 " Example: --dump:code_info_stack_maps\n"
3524 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003525 " --no-disassemble may be used to disable disassembly.\n"
3526 " Example: --no-disassemble\n"
3527 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003528 " --header-only may be used to print only the oat header.\n"
3529 " Example: --header-only\n"
3530 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003531 " --list-classes may be used to list target file classes (can be used with filters).\n"
3532 " Example: --list-classes\n"
3533 " Example: --list-classes --class-filter=com.example.foo\n"
3534 "\n"
3535 " --list-methods may be used to list target file methods (can be used with filters).\n"
3536 " Example: --list-methods\n"
3537 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3538 "\n"
3539 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3540 " Example: --symbolize=/system/framework/boot.oat\n"
3541 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003542 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3543 " .rodata and .text sections are omitted in the output file to save space.\n"
3544 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3545 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003546 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3547 " Example: --class-filter=com.example.foo\n"
3548 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003549 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3550 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003551 "\n"
3552 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3553 " Example: --export-dex-to=/data/local/tmp\n"
3554 "\n"
3555 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3556 " address (e.g. PC from crash dump)\n"
3557 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003558 "\n"
3559 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3560 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003561 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003562 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003563 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003564 " Example: --dump-imt=imt.txt\n"
3565 "\n"
3566 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3567 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003568 "\n";
3569
3570 return usage;
3571 }
3572
3573 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003574 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003575 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003576 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003577 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003578 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003579 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003580 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003581 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003582 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003583 bool disassemble_code_ = true;
3584 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003585 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003586 bool list_classes_ = false;
3587 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003588 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003589 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003590 uint32_t addr2instr_ = 0;
3591 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003592 const char* app_image_ = nullptr;
3593 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003594};
3595
Igor Murashkin37743352014-11-13 14:38:00 -08003596struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003597 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003598 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003599
Igor Murashkin37743352014-11-13 14:38:00 -08003600 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3601 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3602
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003603 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003604 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003605 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003606 args_->disassemble_code_,
3607 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003608 args_->class_filter_,
3609 args_->method_filter_,
3610 args_->list_classes_,
3611 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003612 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003613 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003614 args_->app_image_,
3615 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003616 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003617
Andreas Gampe9fded872016-09-25 16:08:35 -07003618 return (args_->boot_image_location_ != nullptr ||
3619 args_->image_location_ != nullptr ||
3620 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003621 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003622 }
3623
Roland Levillainf73caca2018-08-24 17:19:07 +01003624 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003625 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003626 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003627
Mathieu Chartierd424d082014-10-15 10:31:46 -07003628 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003629
Andreas Gampec24f3992014-12-17 20:40:11 -08003630 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003631 // ELF has special kind of section called SHT_NOBITS which allows us to create
3632 // sections which exist but their data is omitted from the ELF file to save space.
3633 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3634 // with only debug data. We use it in similar way to exclude .rodata and .text.
3635 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003636 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3637 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003638 } else {
3639 return DumpOat(nullptr,
3640 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003641 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003642 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003643 args_->os_) == EXIT_SUCCESS;
3644 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003645 }
3646
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003647 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003648 CHECK(args_ != nullptr);
3649
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003650 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3651 return IMTDumper::Dump(runtime,
3652 args_->imt_dump_,
3653 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003654 args_->oat_filename_,
3655 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003656 }
3657
Igor Murashkin37743352014-11-13 14:38:00 -08003658 if (args_->oat_filename_ != nullptr) {
3659 return DumpOat(runtime,
3660 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003661 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003662 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003663 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003664 }
Igor Murashkin37743352014-11-13 14:38:00 -08003665
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003666 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003667 }
3668
Igor Murashkin37743352014-11-13 14:38:00 -08003669 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3670};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003671
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003672} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003673
3674int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003675 // Output all logging to stderr.
3676 android::base::SetLogger(android::base::StderrLogger);
3677
Igor Murashkin37743352014-11-13 14:38:00 -08003678 art::OatdumpMain main;
3679 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003680}