blob: 0610b971b22ed9fb1d8578122d2fd4acf8ee4012 [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"
Andreas Gampe0c183382017-07-13 22:26:24 -070062#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070063#include "gc/space/image_space.h"
64#include "gc/space/large_object_space.h"
65#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080066#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070067#include "imtable-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070068#include "subtype_check.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"
Vladimir Marko131980f2015-12-03 18:29:23 +000071#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000072#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000073#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074#include "mirror/array-inl.h"
75#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010076#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077#include "mirror/object-inl.h"
78#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010080#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070081#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070082#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070083#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070084#include "stack_map.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080085#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010086#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000088#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070089#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070090
Igor Murashkin37743352014-11-13 14:38:00 -080091#include <sys/stat.h>
92#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070093
Igor Murashkin37743352014-11-13 14:38:00 -080094namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070095
Andreas Gampe46ee31b2016-12-14 10:11:49 -080096using android::base::StringPrintf;
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080099 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700100 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700101 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100102 "kSaveAllCalleeSavesMethod",
103 "kSaveRefsOnlyMethod",
104 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +0100105 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -0700106 "kSaveEverythingMethodForClinit",
107 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108};
109
110const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700111 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700112 "kClassRoots",
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100113 "kOomeWhenThrowingException",
114 "kOomeWhenThrowingOome",
115 "kOomeWhenHandlingStackOverflow",
116 "kNoClassDefFoundError",
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000117 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700118};
119
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700121static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700122
Andreas Gampeb40d3612018-06-26 15:49:42 -0700123const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700124 DCHECK(oat_dex_file != nullptr);
125 auto it = opened_dex_files.find(oat_dex_file);
126 if (it != opened_dex_files.end()) {
127 return it->second.get();
128 }
129 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
130 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
131 return ret;
132}
133
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800134template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100135class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700136 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000137 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
138 oat_file_(oat_file),
139 builder_(nullptr),
140 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
141 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700142 }
143
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700144 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800146 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000147 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
Andreas Gampe08c277c2017-04-26 22:22:15 -0700149 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
150 if (elf_file == nullptr) {
151 return false;
152 }
Vladimir Marko74527972016-11-29 15:57:32 +0000153 std::unique_ptr<linker::BufferedOutputStream> output_stream =
154 std::make_unique<linker::BufferedOutputStream>(
155 std::make_unique<linker::FileOutputStream>(elf_file.get()));
156 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000157
158 builder_->Start();
159
160 auto* rodata = builder_->GetRoData();
161 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163 const uint8_t* rodata_begin = oat_file_->Begin();
164 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000165 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000166 rodata->Start();
167 rodata->WriteFully(rodata_begin, rodata_size);
168 rodata->End();
169 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170
David Srbecky6d8c8f02015-10-26 10:57:09 +0000171 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
172 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000173 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000174 text->Start();
175 text->WriteFully(text_begin, text_size);
176 text->End();
177 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000178
Vladimir Marko33bff252017-11-01 14:35:42 +0000179 if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700180 builder_->WriteMIPSabiflagsSection();
181 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000182 builder_->PrepareDynamicSection(elf_file->GetPath(),
183 rodata_size,
184 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000185 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000186 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100187 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000188 oat_file_->BssRootsOffset(),
189 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000190 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191
Vladimir Marko1b404a82017-09-01 13:35:26 +0100192 const OatHeader& oat_header = oat_file_->GetOatHeader();
193 #define DO_TRAMPOLINE(fn_name) \
194 if (oat_header.Get ## fn_name ## Offset() != 0) { \
195 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000196 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100197 info.isa = oat_header.GetInstructionSet(); \
198 info.is_code_address_text_relative = true; \
199 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
200 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
201 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
202 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
203 method_debug_infos_.push_back(std::move(info)); \
204 }
205 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
206 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
207 DO_TRAMPOLINE(JniDlsymLookup);
208 DO_TRAMPOLINE(QuickGenericJniTrampoline);
209 DO_TRAMPOLINE(QuickImtConflictTrampoline);
210 DO_TRAMPOLINE(QuickResolutionTrampoline);
211 DO_TRAMPOLINE(QuickToInterpreterBridge);
212 #undef DO_TRAMPOLINE
213
David Srbecky5d950762016-03-07 20:47:29 +0000214 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100215
216 // TODO: Try to symbolize link-time thunks?
217 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218
David Srbecky32210b92017-12-04 14:39:21 +0000219 // TODO: Add symbols for dex bytecode in the .dex section.
220
221 debug::DebugInfo debug_info{};
222 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
223
David Srbecky5d950762016-03-07 20:47:29 +0000224 debug::WriteDebugInfo(builder_.get(),
David Srbecky32210b92017-12-04 14:39:21 +0000225 debug_info,
David Srbecky5d950762016-03-07 20:47:29 +0000226 dwarf::DW_DEBUG_FRAME_FORMAT,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700227 /* write_oat_patches= */ true);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228
David Srbecky6d8c8f02015-10-26 10:57:09 +0000229 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230
Andreas Gampe08c277c2017-04-26 22:22:15 -0700231 bool ret_value = builder_->Good();
232
233 builder_.reset();
234 output_stream.reset();
235
236 if (elf_file->FlushCloseOrErase() != 0) {
237 return false;
238 }
239 elf_file.reset();
240
241 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700242 }
243
David Srbecky5d950762016-03-07 20:47:29 +0000244 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700245 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700247 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700248 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000249 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700250 }
251 }
252
Andreas Gampeb40d3612018-06-26 15:49:42 -0700253 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700255 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
256 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 return;
258 }
259 for (size_t class_def_index = 0;
260 class_def_index < dex_file->NumClassDefs();
261 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700262 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
263 OatClassType type = oat_class.GetType();
264 switch (type) {
265 case kOatClassAllCompiled:
266 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000267 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700268 break;
269
270 case kOatClassNoneCompiled:
271 case kOatClassMax:
272 // Ignore.
273 break;
274 }
275 }
276 }
277
David Srbecky5d950762016-03-07 20:47:29 +0000278 void WalkOatClass(const OatFile::OatClass& oat_class,
279 const DexFile& dex_file,
280 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700281 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 // Note: even if this is an interface or a native class, we still have to walk it, as there
283 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700284 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700285 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000286 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
287 dex_file,
288 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700289 method.GetIndex(),
290 method.GetCodeItem(),
291 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700292 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700293 }
294
David Srbecky5d950762016-03-07 20:47:29 +0000295 void WalkOatMethod(const OatFile::OatMethod& oat_method,
296 const DexFile& dex_file,
297 uint32_t class_def_index,
298 uint32_t dex_method_index,
299 const DexFile::CodeItem* code_item,
300 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700301 if ((method_access_flags & kAccAbstract) != 0) {
302 // Abstract method, no code.
303 return;
304 }
David Srbecky5d950762016-03-07 20:47:29 +0000305 const OatHeader& oat_header = oat_file_->GetOatHeader();
306 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
307 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700308 // No code.
309 return;
310 }
311
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100312 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
313 // Clear Thumb2 bit.
314 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
315
Vladimir Marko1b404a82017-09-01 13:35:26 +0100316 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000317 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000318 info.dex_file = &dex_file;
319 info.class_def_index = class_def_index;
320 info.dex_method_index = dex_method_index;
321 info.access_flags = method_access_flags;
322 info.code_item = code_item;
323 info.isa = oat_header.GetInstructionSet();
324 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
325 info.is_native_debuggable = oat_header.IsNativeDebuggable();
326 info.is_optimized = method_header->IsOptimized();
327 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100328 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000329 info.code_size = method_header->GetCodeSize();
330 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
331 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
332 info.cfi = ArrayRef<uint8_t>();
333 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700334 }
335
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700336 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700337 const OatFile* oat_file_;
Vladimir Marko74527972016-11-29 15:57:32 +0000338 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000339 std::vector<debug::MethodDebugInfo> method_debug_infos_;
340 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700341 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000342 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700343};
344
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345class OatDumperOptions {
346 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100347 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100348 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700349 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700350 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800351 const char* class_filter,
352 const char* method_filter,
353 bool list_classes,
354 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000355 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800357 const char* app_image,
358 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800359 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100360 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100361 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700363 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800364 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000365 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800366 list_classes_(list_classes),
367 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000368 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800369 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800370 app_image_(app_image),
371 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800372 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800373 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700375 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100376 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700377 const bool disassemble_code_;
378 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800379 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000380 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800381 const bool list_classes_;
382 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000383 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800384 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800385 const char* const app_image_;
386 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800387 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700388 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700389};
390
Elliott Hughese3c845c2012-02-28 17:23:01 -0800391class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700392 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100393 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000394 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800395 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700396 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800397 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800398 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
399 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700400 new DisassemblerOptions(
401 options_.absolute_addresses_,
402 oat_file.Begin(),
403 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700404 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700405 Is64BitInstructionSet(instruction_set_)
406 ? &Thread::DumpThreadOffset<PointerSize::k64>
407 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800408 CHECK(options_.class_loader_ != nullptr);
409 CHECK(options_.class_filter_ != nullptr);
410 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800411 AddAllOffsets();
412 }
413
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700414 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700415 delete disassembler_;
416 }
417
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800418 InstructionSet GetInstructionSet() {
419 return instruction_set_;
420 }
421
Andreas Gampec55bb392018-09-21 00:02:02 +0000422 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300423
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700424 bool Dump(std::ostream& os) {
425 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800426 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700427
428 os << "MAGIC:\n";
429 os << oat_header.GetMagic() << "\n\n";
430
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800431 os << "LOCATION:\n";
432 os << oat_file_.GetLocation() << "\n\n";
433
Brian Carlstromaded5f72011-10-07 17:15:04 -0700434 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800435 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700436
Elliott Hughesa72ec822012-03-05 17:12:22 -0800437 os << "INSTRUCTION SET:\n";
438 os << oat_header.GetInstructionSet() << "\n\n";
439
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700440 {
441 std::unique_ptr<const InstructionSetFeatures> features(
442 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
443 oat_header.GetInstructionSetFeaturesBitmap()));
444 os << "INSTRUCTION SET FEATURES:\n";
445 os << features->GetFeatureString() << "\n\n";
446 }
Dave Allison70202782013-10-22 17:52:19 -0700447
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448 os << "DEX FILE COUNT:\n";
449 os << oat_header.GetDexFileCount() << "\n\n";
450
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800451#define DUMP_OAT_HEADER_OFFSET(label, offset) \
452 os << label " OFFSET:\n"; \
453 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800454 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800455 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
456 } \
457 os << StringPrintf("\n\n");
458
459 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
460 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
461 GetInterpreterToInterpreterBridgeOffset);
462 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
463 GetInterpreterToCompiledCodeBridgeOffset);
464 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
465 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800466 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
467 GetQuickGenericJniTrampolineOffset);
468 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
469 GetQuickImtConflictTrampolineOffset);
470 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
471 GetQuickResolutionTrampolineOffset);
472 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
473 GetQuickToInterpreterBridgeOffset);
474#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475
Brian Carlstrom28db0122012-10-18 16:20:41 -0700476 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
477 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
478
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700479 // Print the key-value store.
480 {
481 os << "KEY VALUE STORE:\n";
482 size_t index = 0;
483 const char* key;
484 const char* value;
485 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
486 os << key << " = " << value << "\n";
487 index++;
488 }
489 os << "\n";
490 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700491
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800492 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700493 os << "BEGIN:\n";
494 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700495
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700496 os << "END:\n";
497 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
498 }
499
500 os << "SIZE:\n";
501 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502
503 os << std::flush;
504
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800505 // If set, adjust relative address to be searched
506 if (options_.addr2instr_ != 0) {
507 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
508 os << "SEARCH ADDRESS (executable offset + input):\n";
509 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
510 }
511
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000512 // Dump .data.bimg.rel.ro entries.
513 DumpDataBimgRelRoEntries(os);
514
515 // Dump .bss summary, individual entries are dumped per dex file.
516 os << ".bss: ";
517 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
518 os << "empty.\n\n";
519 } else {
520 os << oat_file_.GetBssMethods().size() << " methods, ";
521 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
522 }
523
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700524 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700525 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700526 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700527 CHECK(oat_dex_file != nullptr);
528 std::string error_msg;
529 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
530 if (dex_file == nullptr) {
531 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
532 << error_msg;
533 continue;
534 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700535
Mathieu Chartier120aa282017-08-05 16:03:03 -0700536 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
537 if (layout_sections != nullptr) {
538 os << "Layout data\n";
539 os << *layout_sections;
540 os << "\n";
541 }
542
Vladimir Markobacb8e42018-06-13 14:45:30 +0100543 if (!options_.dump_header_only_) {
544 // Dump .bss entries.
545 DumpBssEntries(
546 os,
547 "ArtMethod",
548 oat_dex_file->GetMethodBssMapping(),
549 dex_file->NumMethodIds(),
550 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
551 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
552 DumpBssEntries(
553 os,
554 "Class",
555 oat_dex_file->GetTypeBssMapping(),
556 dex_file->NumTypeIds(),
557 sizeof(GcRoot<mirror::Class>),
558 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
559 DumpBssEntries(
560 os,
561 "String",
562 oat_dex_file->GetStringBssMapping(),
563 dex_file->NumStringIds(),
564 sizeof(GcRoot<mirror::Class>),
565 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
566 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700567 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700568
David Brazdilc03d7b62016-03-02 12:18:03 +0000569 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000570 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000571 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000572 if (vdex_header.IsValid()) {
573 std::string error_msg;
574 std::vector<const DexFile*> dex_files;
575 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
576 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
577 if (dex_file == nullptr) {
578 os << "Error opening dex file: " << error_msg << std::endl;
579 return false;
580 }
581 dex_files.push_back(dex_file);
582 }
583 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
584 deps.Dump(&vios);
585 } else {
586 os << "UNRECOGNIZED vdex file, magic "
587 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000588 << ", verifier deps version "
589 << vdex_header.GetVerifierDepsVersion()
590 << ", dex section version "
591 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000592 << "\n";
593 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000594 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700595 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000596 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300597 if (!DumpOatDexFile(os, *oat_dex_file)) {
598 success = false;
599 }
600 }
601 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800602
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300603 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000604 std::string error_msg;
605 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
606 if (!OS::FileExists(vdex_filename.c_str())) {
607 os << "File " << vdex_filename.c_str() << " does not exist\n";
608 return false;
609 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300610
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000611 DexFileUniqV vdex_dex_files;
612 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
613 &vdex_dex_files,
614 &error_msg);
615 if (vdex_file.get() == nullptr) {
616 os << "Failed to open vdex file: " << error_msg << "\n";
617 return false;
618 }
619 if (oat_dex_files_.size() != vdex_dex_files.size()) {
620 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
621 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
622 return false;
623 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300624
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000625 size_t i = 0;
626 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700627 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000628 CHECK(oat_dex_file != nullptr);
629 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300630
Luman Qud33d3182018-10-26 16:53:25 +0000631 // Remove hiddenapis
632 vdex_dex_file->UnhideApis();
633
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300634 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
635 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
636 // the original input Dex file, the `update_checksum_` option is used to recompute the
637 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
638 // false), ExportDexFile() enforces a reproducible checksum verification.
639 if (vdex_dex_file->IsCompactDexFile()) {
640 Options options;
641 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
642 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700643 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300644 std::unique_ptr<art::DexContainer> dex_container;
645 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
646 vdex_dex_file.get(),
647 i,
648 &dex_container,
649 &error_msg);
650 if (!result) {
651 os << "DexLayout failed to process Dex file: " + error_msg;
652 success = false;
653 break;
654 }
655 DexContainer::Section* main_section = dex_container->GetMainSection();
656 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
657
658 const ArtDexFileLoader dex_file_loader;
659 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
660 main_section->Begin(),
661 main_section->Size(),
662 vdex_dex_file->GetLocation(),
663 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700664 /*oat_dex_file=*/ nullptr,
665 /*verify=*/ false,
666 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300667 &error_msg));
668 if (dex == nullptr) {
669 os << "Failed to load DexFile from layout container: " + error_msg;
670 success = false;
671 break;
672 }
673 if (dex->IsCompactDexFile()) {
674 os <<"CompactDex conversion to StandardDex failed";
675 success = false;
676 break;
677 }
678
Andreas Gampe9b031f72018-10-04 11:03:34 -0700679 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300680 success = false;
681 break;
682 }
683 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700684 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300685 success = false;
686 break;
687 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300688 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000689 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700690 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700691 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000692
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800693 {
694 os << "OAT FILE STATS:\n";
695 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100696 stats_.AddBytes(oat_file_.Size());
697 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800698 }
699
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700700 os << std::flush;
701 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700702 }
703
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800704 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700705 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
706 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800707 return 0; // Address not in oat file
708 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800709 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
710 reinterpret_cast<uintptr_t>(oat_file_.Begin());
711 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800712 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800713 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800714 return end_offset - begin_offset;
715 }
716
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800717 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700718 return oat_file_.GetOatHeader().GetInstructionSet();
719 }
720
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700721 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800722 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700723 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700724 CHECK(oat_dex_file != nullptr);
725 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700726 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
727 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700728 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
729 << "': " << error_msg;
730 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800731 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700732 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700733 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700734 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700735 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100736 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800737 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100738 return oat_class.GetOatMethod(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(),
Luman Qud33d3182018-10-26 16:53:25 +00001081 method.GetRawAccessFlags(),
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,
1101 const DexFile::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,
1106 const DexFile::CodeItem* code_item,
1107 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: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001259 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1260 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 ScopedIndentation indent2(vios);
1262 vios->Stream() << StringPrintf("WARNING: "
1263 "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;
1266 } else {
1267 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001268 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1269 uint64_t aligned_code_end = aligned_code_begin + code_size;
David Srbecky86decb62018-06-05 06:41:10 +01001270 if (AddStatsObject(code)) {
1271 stats_.Child("Code")->AddBytes(code_size);
1272 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001273
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001274 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001276 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001277 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 ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001283 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 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());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001288 success = false;
1289 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001290 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);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001296 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001297 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001298 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001299 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001300 }
1301 }
1302 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001303 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);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001309 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001310 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001311 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001312 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001313 }
1314 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001315 } else if (options_.disassemble_code_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001316 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001317 }
1318 }
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,
1499 const DexFile::ClassDef& class_def,
1500 const DexFile::CodeItem* code_item,
1501 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 Marko812fb4d2018-03-14 13:07:21 +00001651 void DumpDataBimgRelRoEntries(std::ostream& os) {
1652 os << ".data.bimg.rel.ro: ";
1653 if (oat_file_.GetBootImageRelocations().empty()) {
1654 os << "empty.\n\n";
1655 return;
1656 }
1657
1658 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1659 Runtime* runtime = Runtime::Current();
1660 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1661 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1662 runtime->GetHeap()->GetBootImageSpaces();
1663 ScopedObjectAccess soa(Thread::Current());
1664 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1665 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1666 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1667 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
1668 uint8_t* object = boot_image_spaces[0]->Begin() + object_offset;
1669 bool found = false;
1670 for (gc::space::ImageSpace* space : boot_image_spaces) {
1671 uint64_t local_offset = object - space->Begin();
1672 if (local_offset < space->GetImageHeader().GetImageSize()) {
1673 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
1674 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(object);
1675 if (o->IsString()) {
1676 os << " String: " << o->AsString()->ToModifiedUtf8();
1677 } else if (o->IsClass()) {
1678 os << " Class: " << o->AsClass()->PrettyDescriptor();
1679 } else {
1680 os << StringPrintf(" 0x%08x %s",
1681 object_offset,
1682 o->GetClass()->PrettyDescriptor().c_str());
1683 }
1684 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
1685 ArtMethod* m = reinterpret_cast<ArtMethod*>(object);
1686 os << " ArtMethod: " << m->PrettyMethod();
1687 } else {
1688 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1689 object_offset,
1690 space->GetImageFilename().c_str());
1691 }
1692 found = true;
1693 break;
1694 }
1695 }
1696 if (!found) {
1697 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1698 }
1699 os << "\n";
1700 }
1701 } else {
1702 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1703 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1704 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1705 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1706 }
1707 }
1708 os << "\n";
1709 }
1710
Vladimir Markof3c52b42017-11-17 17:32:12 +00001711 template <typename NameGetter>
1712 void DumpBssEntries(std::ostream& os,
1713 const char* slot_type,
1714 const IndexBssMapping* mapping,
1715 uint32_t number_of_indexes,
1716 size_t slot_size,
1717 NameGetter name) {
1718 os << ".bss mapping for " << slot_type << ": ";
1719 if (mapping == nullptr) {
1720 os << "empty.\n";
1721 return;
1722 }
1723 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1724 size_t num_valid_indexes = 0u;
1725 for (const IndexBssMappingEntry& entry : *mapping) {
1726 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1727 }
1728 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1729 os << std::hex;
1730 for (const IndexBssMappingEntry& entry : *mapping) {
1731 uint32_t index = entry.GetIndex(index_bits);
1732 uint32_t mask = entry.GetMask(index_bits);
1733 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1734 for (uint32_t n : LowToHighBits(mask)) {
1735 size_t current_index = index - (32u - index_bits) + n;
1736 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1737 bss_offset += slot_size;
1738 }
1739 DCHECK_EQ(bss_offset, entry.bss_offset);
1740 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1741 }
1742 os << std::dec;
1743 }
1744
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001745 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001746 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001747 const OatDumperOptions& options_;
1748 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001749 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001750 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001751 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001752 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001753 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001754};
1755
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001756class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001757 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001758 ImageDumper(std::ostream* os,
1759 gc::space::ImageSpace& image_space,
1760 const ImageHeader& image_header,
1761 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001762 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001763 vios_(os),
1764 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001765 image_space_(image_space),
1766 image_header_(image_header),
1767 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001768
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001769 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001770 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001771 std::ostream& indent_os = vios_.Stream();
1772
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001773 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001774
Jeff Haodcdc85b2015-12-04 14:06:18 -08001775 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1776
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001777 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001778
Mathieu Chartiere401d142015-04-22 13:56:20 -07001779 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1780
1781 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1782 auto section = static_cast<ImageHeader::ImageSections>(i);
1783 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1784 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001785
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001786 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001787
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001788 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001789
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001790 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1791
1792 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1793
1794 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001795
Alex Lighta59dd802014-07-02 16:28:08 -07001796 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1797
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001798 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001799 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800 static_assert(arraysize(image_roots_descriptions_) ==
1801 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001802 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1803 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1805 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001806 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1807 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001808 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001809 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001810 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001811 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001812 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1813 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001814 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001815 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1816 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001817 run++;
1818 } else {
1819 break;
1820 }
1821 }
1822 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001823 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001824 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001825 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001826 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001827 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001828 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001829 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001831 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001832 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001833 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001834 }
1835 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001836 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001837
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001838 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001839 os << "METHOD ROOTS\n";
1840 static_assert(arraysize(image_methods_descriptions_) ==
1841 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1842 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1843 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1844 const char* description = image_methods_descriptions_[i];
1845 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001846 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001847 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001848 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001849 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001850
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001851 Runtime* const runtime = Runtime::Current();
1852 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001853 std::string image_filename = image_space_.GetImageFilename();
1854 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001855 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001856 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001857 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001858 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001859 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001860 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1861 }
1862 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001863 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001864 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001865 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001866 /*executable=*/ false,
1867 /*low_4gb=*/ false,
1868 /*abs_dex_location=*/ nullptr,
1869 /*reservation=*/ nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001870 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001871 }
1872 if (oat_file == nullptr) {
1873 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1874 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001875 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001876 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001877
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001878 stats_.oat_file_bytes = oat_file->Size();
1879
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001880 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001881
Andreas Gampeb40d3612018-06-26 15:49:42 -07001882 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001883 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001884 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1885 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001886 }
1887
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001889
Jeff Haodcdc85b2015-12-04 14:06:18 -08001890 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001891 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001892 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001893 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001894 {
1895 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1896 heap->FlushAllocStack();
1897 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001898 // Since FlushAllocStack() above resets the (active) allocation
1899 // stack. Need to revoke the thread-local allocation stacks that
1900 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001901 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001902 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001903 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001904 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001905 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001906 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001907 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001908 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001909 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001910 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001911 ObjPtr<mirror::DexCache> dex_cache =
1912 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001913 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001914 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001915 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001916 }
1917 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001918 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1919 DumpObject(obj);
1920 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001921 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001922 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001923 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001924 indent_os << "\n";
1925 // TODO: Dump fields.
1926 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001927 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001928 image_header_.VisitPackedArtMethods(&visitor,
1929 image_space_.Begin(),
1930 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001932 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001934 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001935 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001936 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001937 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1938 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001939 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001940 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001941 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001942 // If the image is compressed, adjust to decompressed size.
1943 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1944 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1945 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1946 }
1947 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001948 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001949 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001950 const auto& object_section = image_header_.GetObjectsSection();
1951 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001952 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001953 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1954 const auto& intern_section = image_header_.GetInternedStringsSection();
1955 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07001956 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1957 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001958 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001959
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001960 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001961
1962 // Objects are kObjectAlignment-aligned.
1963 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1964 if (object_section.Offset() > header_bytes) {
1965 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1966 }
1967
1968 // Field section is 4-byte aligned.
1969 constexpr size_t kFieldSectionAlignment = 4U;
1970 uint32_t end_objects = object_section.Offset() + object_section.Size();
1971 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1972 stats_.alignment_bytes += field_section.Offset() - end_objects;
1973
1974 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1975 uint32_t end_fields = field_section.Offset() + field_section.Size();
1976 CHECK_ALIGNED(method_section.Offset(), 4);
1977 stats_.alignment_bytes += method_section.Offset() - end_fields;
1978
1979 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1980 uint32_t end_methods = method_section.Offset() + method_section.Size();
1981 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1982 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1983
1984 // Intern table is 8-byte aligned.
1985 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001986 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08001987 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1988
1989 // Add space between intern table and class table.
1990 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1991 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1992
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001993 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1994 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001995 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001996 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001997
Mathieu Chartiere401d142015-04-22 13:56:20 -07001998 stats_.bitmap_bytes += bitmap_section.Size();
1999 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002000 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002001 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002002 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002003 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002004 stats_.sro_offset_bytes += sro_section.Size();
2005 stats_.metadata_bytes += metadata_section.Size();
2006
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002007 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002008 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002009
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002010 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002011
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002012 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002013 }
2014
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002015 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002016 class DumpArtMethodVisitor : public ArtMethodVisitor {
2017 public:
2018 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2019
Roland Levillainf73caca2018-08-24 17:19:07 +01002020 void Visit(ArtMethod* method) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002021 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002022 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002023 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002024 indent_os << "\n";
2025 }
2026
2027 private:
2028 ImageDumper* const image_dumper_;
2029 };
2030
Mathieu Chartier3398c782016-09-30 10:27:43 -07002031 static void PrettyObjectValue(std::ostream& os,
2032 ObjPtr<mirror::Class> type,
2033 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002034 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002035 CHECK(type != nullptr);
2036 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002037 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002038 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002039 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002040 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002041 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002042 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002043 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002044 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002045 } else {
David Sehr709b0702016-10-13 09:12:37 -07002046 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002047 }
2048 }
2049
Mathieu Chartier3398c782016-09-30 10:27:43 -07002050 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002051 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002052 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002053 switch (field->GetTypeAsPrimitiveType()) {
2054 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002055 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002056 break;
2057 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002058 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002059 break;
2060 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002061 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002062 break;
2063 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002065 break;
2066 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002067 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002068 break;
2069 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002070 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002071 break;
2072 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002073 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002074 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002075 break;
2076 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002077 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002078 break;
2079 case Primitive::kPrimNot: {
2080 // Get the value, don't compute the type unless it is non-null as we don't want
2081 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002082 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002083 if (value == nullptr) {
2084 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002085 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002086 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002087 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002088 if (field_type != nullptr) {
2089 PrettyObjectValue(os, field_type, value);
2090 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002091 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002092 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002093 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2094 }
Ian Rogers50239c72013-06-17 14:53:22 -07002095 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002096 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002097 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002098 default:
2099 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2100 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002101 }
2102 }
2103
Andreas Gampe98104992018-10-16 12:49:47 -07002104 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002105 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002106 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002107 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002108 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002109 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002110 for (ArtField& field : klass->GetIFields()) {
2111 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002112 }
2113 }
2114
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002115 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002116 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002117 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002118
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002119 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002120 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002121 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002122 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002123 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002124 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002125 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002126 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002127 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002128 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002129 }
2130
Mathieu Chartiere401d142015-04-22 13:56:20 -07002131 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002132 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002133 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2134 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002135 return 0;
2136 }
2137 return oat_code_begin[-1];
2138 }
2139
Mathieu Chartiere401d142015-04-22 13:56:20 -07002140 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002141 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002142 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002143 if (oat_code_begin == nullptr) {
2144 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002145 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002146 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002147 }
2148
Andreas Gampe0c183382017-07-13 22:26:24 -07002149 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002150 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002151 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002152 return;
2153 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002154
2155 size_t object_bytes = obj->SizeOf();
2156 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002157 stats_.object_bytes += object_bytes;
2158 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002159
Andreas Gampe0c183382017-07-13 22:26:24 -07002160 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002161
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002162 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002163 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002164 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002165 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002166 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002167 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002168 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2169 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002171 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002172 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002173 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002174 } else {
David Sehr709b0702016-10-13 09:12:37 -07002175 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002176 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002177 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002178 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002179 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002180 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002181 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2182 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002183 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002184 size_t run = 0;
2185 for (int32_t j = i + 1; j < length; j++) {
2186 if (value == obj_array->Get(j)) {
2187 run++;
2188 } else {
2189 break;
2190 }
2191 }
2192 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002193 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002194 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002195 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002196 i = i + run;
2197 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002198 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002199 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002200 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002201 }
2202 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002203 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002204
Vladimir Marko305c38b2018-02-14 11:50:07 +00002205 if (kBitstringSubtypeCheckEnabled) {
2206 os << "SUBTYPE_CHECK_BITS: ";
2207 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2208 os << "\n";
2209 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002210
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002211 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002212 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002213 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002214 for (ArtField& field : klass->GetSFields()) {
2215 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002216 }
2217 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002218 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002219 auto it = dex_caches_.find(obj);
2220 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002221 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002222 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002223 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002224 size_t num_methods = dex_cache->NumResolvedMethods();
2225 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002226 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002227 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002228 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002229 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002230 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2231 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002232 size_t run = 0;
2233 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002234 j != length &&
2235 elem == mirror::DexCache::GetNativePairPtrSize(
2236 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002237 ++j) {
2238 ++run;
2239 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002240 if (run == 0) {
2241 os << StringPrintf("%zd: ", i);
2242 } else {
2243 os << StringPrintf("%zd to %zd: ", i, i + run);
2244 i = i + run;
2245 }
2246 std::string msg;
2247 if (elem == nullptr) {
2248 msg = "null";
2249 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002250 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002251 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002252 } else {
2253 msg = "<not in method section>";
2254 }
2255 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002256 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002257 }
2258 size_t num_fields = dex_cache->NumResolvedFields();
2259 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002260 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002261 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002262 auto* resolved_fields = dex_cache->GetResolvedFields();
2263 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002264 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002265 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002266 size_t run = 0;
2267 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002268 j != length &&
2269 elem == mirror::DexCache::GetNativePairPtrSize(
2270 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002271 ++j) {
2272 ++run;
2273 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002274 if (run == 0) {
2275 os << StringPrintf("%zd: ", i);
2276 } else {
2277 os << StringPrintf("%zd to %zd: ", i, i + run);
2278 i = i + run;
2279 }
2280 std::string msg;
2281 if (elem == nullptr) {
2282 msg = "null";
2283 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002284 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002285 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002286 } else {
2287 msg = "<not in field section>";
2288 }
2289 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002290 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002291 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002292 size_t num_types = dex_cache->NumResolvedTypes();
2293 if (num_types != 0u) {
2294 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002295 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002296 auto* resolved_types = dex_cache->GetResolvedTypes();
2297 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002298 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002299 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002300 for (size_t j = i + 1; j != num_types; ++j) {
2301 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2302 if (pair.index != other_pair.index ||
2303 pair.object.Read() != other_pair.object.Read()) {
2304 break;
2305 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002306 ++run;
2307 }
2308 if (run == 0) {
2309 os << StringPrintf("%zd: ", i);
2310 } else {
2311 os << StringPrintf("%zd to %zd: ", i, i + run);
2312 i = i + run;
2313 }
2314 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002315 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002316 if (elem == nullptr) {
2317 msg = "null";
2318 } else {
David Sehr709b0702016-10-13 09:12:37 -07002319 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002320 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002321 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002322 }
2323 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002324 }
2325 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002326 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002327 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002328 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002329
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002330 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002331 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002332 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002333 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2334 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002335 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002336 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2337 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002338 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002340 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2341 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002342 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002343 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002344 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002345 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2346 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002347 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002348 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002349 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002350 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002351 } else if (method->IsRuntimeMethod()) {
2352 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2353 if (table != nullptr) {
2354 indent_os << "IMT conflict table " << table << " method: ";
2355 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002356 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2357 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002358 }
2359 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002360 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002361 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002362 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002363 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002364
2365 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002366 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002367 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002368 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002369 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002370 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002371 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002372 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002373 }
2374
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002375 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2376 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002377 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002378 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002379 if (method->IsConstructor()) {
2380 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002381 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002383 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002384 }
2385 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002386 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002387 }
2388 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002389 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390
Igor Murashkin7617abd2015-07-10 18:27:47 -07002391 uint32_t method_access_flags = method->GetAccessFlags();
2392
Mathieu Chartiere401d142015-04-22 13:56:20 -07002393 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002394 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2395 dex_instruction_bytes,
2396 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002397 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002398
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002399 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002400 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401
2402 double expansion =
2403 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002404 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002405 }
2406 }
2407
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002408 std::set<const void*> already_seen_;
2409 // Compute the size of the given data within the oat file and whether this is the first time
2410 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002411 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002412 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002413 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002414 already_seen_.insert(oat_data);
2415 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002416 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002417 }
2418 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002419 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002420
2421 public:
2422 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002423 size_t oat_file_bytes = 0u;
2424 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002425
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002426 size_t header_bytes = 0u;
2427 size_t object_bytes = 0u;
2428 size_t art_field_bytes = 0u;
2429 size_t art_method_bytes = 0u;
2430 size_t dex_cache_arrays_bytes = 0u;
2431 size_t interned_strings_bytes = 0u;
2432 size_t class_table_bytes = 0u;
2433 size_t sro_offset_bytes = 0u;
2434 size_t metadata_bytes = 0u;
2435 size_t bitmap_bytes = 0u;
2436 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002437
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002438 size_t managed_code_bytes = 0u;
2439 size_t managed_code_bytes_ignoring_deduplication = 0u;
2440 size_t native_to_managed_code_bytes = 0u;
2441 size_t class_initializer_code_bytes = 0u;
2442 size_t large_initializer_code_bytes = 0u;
2443 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002444
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002445 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002446
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002447 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002448
Mathieu Chartiere401d142015-04-22 13:56:20 -07002449 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002450 std::vector<size_t> method_outlier_size;
2451 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002452 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002453
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002454 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002455
Elliott Hughesa0e18062012-04-13 15:59:59 -07002456 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002457 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002458 size_t bytes;
2459 size_t count;
2460 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002461 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002462 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002463
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002464 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002465 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2466 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002467 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002468 it->second.count += 1;
2469 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002470 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002471 }
2472 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002473
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002474 double PercentOfOatBytes(size_t size) {
2475 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2476 }
2477
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002478 double PercentOfFileBytes(size_t size) {
2479 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2480 }
2481
2482 double PercentOfObjectBytes(size_t size) {
2483 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2484 }
2485
Mathieu Chartiere401d142015-04-22 13:56:20 -07002486 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002487 method_outlier_size.push_back(total_size);
2488 method_outlier_expansion.push_back(expansion);
2489 method_outlier.push_back(method);
2490 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002491
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002492 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002493 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002494 size_t sum_of_sizes = 0;
2495 size_t sum_of_sizes_squared = 0;
2496 size_t sum_of_expansion = 0;
2497 size_t sum_of_expansion_squared = 0;
2498 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002499 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002500 return;
2501 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002502 for (size_t i = 0; i < n; i++) {
2503 size_t cur_size = method_outlier_size[i];
2504 sum_of_sizes += cur_size;
2505 sum_of_sizes_squared += cur_size * cur_size;
2506 double cur_expansion = method_outlier_expansion[i];
2507 sum_of_expansion += cur_expansion;
2508 sum_of_expansion_squared += cur_expansion * cur_expansion;
2509 }
2510 size_t size_mean = sum_of_sizes / n;
2511 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2512 double expansion_mean = sum_of_expansion / n;
2513 double expansion_variance =
2514 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2515
2516 // Dump methods whose size is a certain number of standard deviations from the mean
2517 size_t dumped_values = 0;
2518 size_t skipped_values = 0;
2519 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2520 size_t cur_size_variance = i * i * size_variance;
2521 bool first = true;
2522 for (size_t j = 0; j < n; j++) {
2523 size_t cur_size = method_outlier_size[j];
2524 if (cur_size > size_mean) {
2525 size_t cur_var = cur_size - size_mean;
2526 cur_var = cur_var * cur_var;
2527 if (cur_var > cur_size_variance) {
2528 if (dumped_values > 20) {
2529 if (i == 1) {
2530 skipped_values++;
2531 } else {
2532 i = 2; // jump to counting for 1 standard deviation
2533 break;
2534 }
2535 } else {
2536 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002537 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002538 first = false;
2539 }
David Sehr709b0702016-10-13 09:12:37 -07002540 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002541 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002542 method_outlier_size[j] = 0; // don't consider this method again
2543 dumped_values++;
2544 }
2545 }
2546 }
2547 }
2548 }
2549 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002551 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002552 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002553 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002554
2555 // Dump methods whose expansion is a certain number of standard deviations from the mean
2556 dumped_values = 0;
2557 skipped_values = 0;
2558 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2559 double cur_expansion_variance = i * i * expansion_variance;
2560 bool first = true;
2561 for (size_t j = 0; j < n; j++) {
2562 double cur_expansion = method_outlier_expansion[j];
2563 if (cur_expansion > expansion_mean) {
2564 size_t cur_var = cur_expansion - expansion_mean;
2565 cur_var = cur_var * cur_var;
2566 if (cur_var > cur_expansion_variance) {
2567 if (dumped_values > 20) {
2568 if (i == 1) {
2569 skipped_values++;
2570 } else {
2571 i = 2; // jump to counting for 1 standard deviation
2572 break;
2573 }
2574 } else {
2575 if (first) {
2576 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002577 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002578 first = false;
2579 }
David Sehr709b0702016-10-13 09:12:37 -07002580 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002581 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002582 method_outlier_expansion[j] = 0.0; // don't consider this method again
2583 dumped_values++;
2584 }
2585 }
2586 }
2587 }
2588 }
2589 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002590 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002591 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002592 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002593 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002594 }
2595
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002596 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002597 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002598 {
2599 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2600 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002601 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2602 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2603 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2604 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2605 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2606 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002607 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002608 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2609 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002610 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2611 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002612 header_bytes, PercentOfFileBytes(header_bytes),
2613 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002614 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2615 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002616 dex_cache_arrays_bytes,
2617 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002618 interned_strings_bytes,
2619 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002620 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002621 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2622 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002623 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002624 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2625 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002626 CHECK_EQ(file_bytes,
2627 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2628 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002629 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002631
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002633 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002634 for (const auto& sizes_and_count : sizes_and_counts) {
2635 const std::string& descriptor(sizes_and_count.first);
2636 double average = static_cast<double>(sizes_and_count.second.bytes) /
2637 static_cast<double>(sizes_and_count.second.count);
2638 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002639 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002640 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002641 descriptor.c_str(), sizes_and_count.second.bytes,
2642 sizes_and_count.second.count, average, percent);
2643 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002644 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002645 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002646 CHECK_EQ(object_bytes, object_bytes_total);
2647
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002648 os << StringPrintf("oat_file_bytes = %8zd\n"
2649 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2651 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2652 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2653 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002654 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002655 managed_code_bytes,
2656 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002657 native_to_managed_code_bytes,
2658 PercentOfOatBytes(native_to_managed_code_bytes),
2659 class_initializer_code_bytes,
2660 PercentOfOatBytes(class_initializer_code_bytes),
2661 large_initializer_code_bytes,
2662 PercentOfOatBytes(large_initializer_code_bytes),
2663 large_method_code_bytes,
2664 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002665 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002666 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002667 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002668 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2669 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002670 }
2671
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002672 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002673 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002674 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002675
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002676 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2677 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002678 static_cast<double>(managed_code_bytes) /
2679 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002680 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002681 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002682 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002683
2684 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002685 }
2686 } stats_;
2687
2688 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002689 enum {
2690 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2691 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2692 kLargeConstructorDexBytes = 4000,
2693 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2694 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2695 kLargeMethodDexBytes = 16000
2696 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002697
2698 // For performance, use the *os_ directly for anything that doesn't need indentation
2699 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002700 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002701 VariableIndentationOutputStream vios_;
2702 ScopedIndentation indent1_;
2703
Ian Rogers1d54e732013-05-02 21:10:01 -07002704 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002705 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002706 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002707 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002708 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002709
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002710 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002711};
2712
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002713static int DumpImage(gc::space::ImageSpace* image_space,
2714 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002715 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002716 const ImageHeader& image_header = image_space->GetImageHeader();
2717 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002718 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002719 return EXIT_FAILURE;
2720 }
2721 ImageDumper image_dumper(os, *image_space, image_header, options);
2722 if (!image_dumper.Dump()) {
2723 return EXIT_FAILURE;
2724 }
2725 return EXIT_SUCCESS;
2726}
2727
2728static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002729 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002730 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002731 options->class_loader_ = &null_class_loader;
2732
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002733 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002734 if (options->app_image_ != nullptr) {
2735 if (options->app_oat_ == nullptr) {
2736 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002737 return EXIT_FAILURE;
2738 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002739 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2740 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2741 // pointers into 32 bit pointer sized ArtMethods.
2742 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002743 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002744 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002745 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002746 /*executable=*/ false,
2747 /*low_4gb=*/ true,
2748 /*abs_dex_location=*/ nullptr,
2749 /*reservation=*/ nullptr,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002750 &error_msg));
2751 if (oat_file == nullptr) {
2752 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002753 return EXIT_FAILURE;
2754 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002755 std::unique_ptr<gc::space::ImageSpace> space(
2756 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2757 if (space == nullptr) {
2758 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2759 << error_msg;
2760 }
2761 // Open dex files for the image.
2762 std::vector<std::unique_ptr<const DexFile>> dex_files;
2763 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2764 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2765 << error_msg;
2766 }
2767 // Dump the actual image.
2768 int result = DumpImage(space.get(), options, os);
2769 if (result != EXIT_SUCCESS) {
2770 return result;
2771 }
2772 // Fall through to dump the boot images.
2773 }
2774
2775 gc::Heap* heap = runtime->GetHeap();
2776 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2777 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2778 int result = DumpImage(image_space, options, os);
2779 if (result != EXIT_SUCCESS) {
2780 return result;
2781 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002782 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002783 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002784}
2785
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002786static jobject InstallOatFile(Runtime* runtime,
2787 std::unique_ptr<OatFile> oat_file,
2788 std::vector<const DexFile*>* class_path)
2789 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002790 Thread* self = Thread::Current();
2791 CHECK(self != nullptr);
2792 // Need well-known-classes.
2793 WellKnownClasses::Init(self->GetJniEnv());
2794
Vladimir Marko421087b2018-02-27 11:00:17 +00002795 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002796 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002797 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002798 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002799 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002800 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002801 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002802 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002803 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002804 }
2805
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002806 // Need a class loader. Fake that we're a compiler.
2807 // Note: this will run initializers through the unstarted runtime, so make sure it's
2808 // initialized.
2809 interpreter::UnstartedRuntime::Initialize();
2810
2811 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2812
Vladimir Marko421087b2018-02-27 11:00:17 +00002813 // Need to register dex files to get a working dex cache.
2814 for (const DexFile* dex_file : *class_path) {
2815 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2816 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2817 CHECK(dex_cache != nullptr);
2818 }
2819
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002820 return class_loader;
2821}
2822
2823static int DumpOatWithRuntime(Runtime* runtime,
2824 std::unique_ptr<OatFile> oat_file,
2825 OatDumperOptions* options,
2826 std::ostream* os) {
2827 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2828 ScopedObjectAccess soa(Thread::Current());
2829
2830 OatFile* oat_file_ptr = oat_file.get();
2831 std::vector<const DexFile*> class_path;
2832 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002833
2834 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002835 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002836 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002837 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002838 options->class_loader_ = &loader_handle;
2839
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002840 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002841 bool success = oat_dumper.Dump(*os);
2842 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2843}
2844
2845static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002846 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002847 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002848 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002849 options->class_loader_ = &null_class_loader;
2850
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002851 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002852 bool success = oat_dumper.Dump(*os);
2853 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2854}
2855
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002856static int DumpOat(Runtime* runtime,
2857 const char* oat_filename,
2858 const char* dex_filename,
2859 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002860 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002861 if (dex_filename == nullptr) {
2862 LOG(WARNING) << "No dex filename provided, "
2863 << "oatdump might fail if the oat file does not contain the dex code.";
2864 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002865 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002866 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002867 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002868 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002869 /*executable=*/ false,
2870 /*low_4gb=*/ false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002871 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002872 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002873 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002874 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002875 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002876 return EXIT_FAILURE;
2877 }
2878
2879 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002880 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002881 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002882 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002883 }
2884}
2885
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002886static int SymbolizeOat(const char* oat_filename,
2887 const char* dex_filename,
2888 std::string& output_name,
2889 bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002890 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002891 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002892 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002893 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002894 /*executable=*/ false,
2895 /*low_4gb=*/ false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002896 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002897 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002898 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002899 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002900 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002901 return EXIT_FAILURE;
2902 }
2903
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002904 bool result;
2905 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2906 // files for 64-bit code in the past.
2907 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002908 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002909 result = oat_symbolizer.Symbolize();
2910 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002911 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002912 result = oat_symbolizer.Symbolize();
2913 }
2914 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002915 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002916 return EXIT_FAILURE;
2917 }
2918
2919 return EXIT_SUCCESS;
2920}
2921
Andreas Gampe9fded872016-09-25 16:08:35 -07002922class IMTDumper {
2923 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002924 static bool Dump(Runtime* runtime,
2925 const std::string& imt_file,
2926 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002927 const char* oat_filename,
2928 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002929 Thread* self = Thread::Current();
2930
2931 ScopedObjectAccess soa(self);
2932 StackHandleScope<1> scope(self);
2933 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2934 std::vector<const DexFile*> class_path;
2935
2936 if (oat_filename != nullptr) {
2937 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002938 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002939 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002940 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002941 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002942 /*low_4gb=*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002943 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002944 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002945 &error_msg));
2946 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002947 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002948 return false;
2949 }
2950
2951 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2952 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2953 } else {
2954 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2955 class_path = runtime->GetClassLinker()->GetBootClassPath();
2956 }
2957
2958 if (!imt_file.empty()) {
2959 return DumpImt(runtime, imt_file, class_loader);
2960 }
2961
2962 if (dump_imt_stats) {
2963 return DumpImtStats(runtime, class_path, class_loader);
2964 }
2965
2966 LOG(FATAL) << "Should not reach here";
2967 UNREACHABLE();
2968 }
2969
2970 private:
2971 static bool DumpImt(Runtime* runtime,
2972 const std::string& imt_file,
2973 Handle<mirror::ClassLoader> h_class_loader)
2974 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002975 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2976 std::unordered_set<std::string> prepared;
2977
2978 for (const std::string& line : lines) {
2979 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2980 // or a class descriptor and an interface method, in which case we will lookup the method,
2981 // determine its IMT slot, and check the class' IMT.
2982 size_t first_space = line.find(' ');
2983 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002984 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002985 } else {
2986 DumpIMTForMethod(runtime,
2987 line.substr(0, first_space),
2988 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002989 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002990 &prepared);
2991 }
2992 std::cerr << std::endl;
2993 }
2994
2995 return true;
2996 }
2997
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002998 static bool DumpImtStats(Runtime* runtime,
2999 const std::vector<const DexFile*>& dex_files,
3000 Handle<mirror::ClassLoader> h_class_loader)
3001 REQUIRES_SHARED(Locks::mutator_lock_) {
3002 size_t without_imt = 0;
3003 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003004 std::map<size_t, size_t> histogram;
3005
3006 ClassLinker* class_linker = runtime->GetClassLinker();
3007 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3008 std::unordered_set<std::string> prepared;
3009
3010 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003011 StackHandleScope<1> scope(self);
3012 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3013
3014 for (const DexFile* dex_file : dex_files) {
3015 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003016 class_def_index != dex_file->NumClassDefs();
3017 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003018 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3019 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003020 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003021 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003022 std::cerr << "Warning: could not load " << descriptor << std::endl;
3023 continue;
3024 }
3025
3026 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003027 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003028 continue;
3029 }
3030
3031 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3032 if (im_table == nullptr) {
3033 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003034 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003035 continue;
3036 }
3037
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003038 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003039 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3040 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3041 if (ptr->IsRuntimeMethod()) {
3042 if (ptr->IsImtUnimplementedMethod()) {
3043 histogram[0]++;
3044 } else {
3045 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3046 histogram[current_table->NumEntries(pointer_size)]++;
3047 }
3048 } else {
3049 histogram[1]++;
3050 }
3051 }
3052 }
3053 }
3054
3055 std::cerr << "IMT stats:"
3056 << std::endl << std::endl;
3057
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003058 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003059 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003060 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003061 << std::endl << std::endl;
3062
3063 double sum_one = 0;
3064 size_t count_one = 0;
3065
3066 std::cerr << " " << "IMT histogram" << std::endl;
3067 for (auto& bucket : histogram) {
3068 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3069 if (bucket.first > 0) {
3070 sum_one += bucket.second * bucket.first;
3071 count_one += bucket.second;
3072 }
3073 }
3074
3075 double count_zero = count_one + histogram[0];
3076 std::cerr << " Stats:" << std::endl;
3077 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3078 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3079
3080 return true;
3081 }
3082
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003083 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003084 static bool HasNoIMT(Runtime* runtime,
3085 Handle<mirror::Class> klass,
3086 const PointerSize pointer_size,
3087 std::unordered_set<std::string>* prepared)
3088 REQUIRES_SHARED(Locks::mutator_lock_) {
3089 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3090 return true;
3091 }
3092
3093 if (klass->GetImt(pointer_size) == nullptr) {
3094 PrepareClass(runtime, klass, prepared);
3095 }
3096
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003097 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003098 DCHECK(object_class->IsObjectClass());
3099
3100 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3101
Mathieu Chartier6beced42016-11-15 15:51:31 -08003102 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003103 DCHECK(result);
3104 }
3105
3106 return result;
3107 }
3108
3109 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3110 REQUIRES_SHARED(Locks::mutator_lock_) {
3111 if (table == nullptr) {
3112 std::cerr << " <No IMT?>" << std::endl;
3113 return;
3114 }
3115 size_t table_index = 0;
3116 for (;;) {
3117 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3118 if (ptr == nullptr) {
3119 return;
3120 }
3121 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003122 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003123 }
3124 }
3125
3126 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3127 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003128 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003129 const std::string& class_name,
3130 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003131 /*out*/ ObjPtr<mirror::Class>* klass_out,
3132 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003133 REQUIRES_SHARED(Locks::mutator_lock_) {
3134 if (class_name.empty()) {
3135 return nullptr;
3136 }
3137
3138 std::string descriptor;
3139 if (class_name[0] == 'L') {
3140 descriptor = class_name;
3141 } else {
3142 descriptor = DotToDescriptor(class_name.c_str());
3143 }
3144
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003145 ObjPtr<mirror::Class> klass =
3146 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003147
3148 if (klass == nullptr) {
3149 self->ClearException();
3150 std::cerr << "Did not find " << class_name << std::endl;
3151 *klass_out = nullptr;
3152 return nullptr;
3153 }
3154
3155 StackHandleScope<1> scope(Thread::Current());
3156 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3157
3158 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3159 *klass_out = h_klass.Get();
3160 return ret;
3161 }
3162
3163 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3164 Handle<mirror::Class> h_klass,
3165 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003166 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003167 REQUIRES_SHARED(Locks::mutator_lock_) {
3168 PrepareClass(runtime, h_klass, prepared);
3169 return h_klass->GetImt(pointer_size);
3170 }
3171
3172 static void DumpIMTForClass(Runtime* runtime,
3173 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003174 Handle<mirror::ClassLoader> h_loader,
3175 std::unordered_set<std::string>* prepared)
3176 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003177 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003178 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003179 ImTable* imt = PrepareAndGetImTable(runtime,
3180 Thread::Current(),
3181 h_loader,
3182 class_name,
3183 pointer_size,
3184 &klass,
3185 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003186 if (imt == nullptr) {
3187 return;
3188 }
3189
3190 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3191 for (size_t index = 0; index < ImTable::kSize; ++index) {
3192 std::cerr << " " << index << ":" << std::endl;
3193 ArtMethod* ptr = imt->Get(index, pointer_size);
3194 if (ptr->IsRuntimeMethod()) {
3195 if (ptr->IsImtUnimplementedMethod()) {
3196 std::cerr << " <empty>" << std::endl;
3197 } else {
3198 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3199 PrintTable(current_table, pointer_size);
3200 }
3201 } else {
David Sehr709b0702016-10-13 09:12:37 -07003202 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003203 }
3204 }
3205
3206 std::cerr << " Interfaces:" << std::endl;
3207 // Run through iftable, find methods that slot here, see if they fit.
3208 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003209 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3210 mirror::Class* iface = if_table->GetInterface(i);
3211 std::string iface_name;
3212 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003213
Mathieu Chartier6beced42016-11-15 15:51:31 -08003214 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3215 uint32_t class_hash, name_hash, signature_hash;
3216 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3217 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3218 std::cerr << " " << iface_method.PrettyMethod(true)
3219 << " slot=" << imt_slot
3220 << std::hex
3221 << " class_hash=0x" << class_hash
3222 << " name_hash=0x" << name_hash
3223 << " signature_hash=0x" << signature_hash
3224 << std::dec
3225 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003226 }
3227 }
3228 }
3229
3230 static void DumpIMTForMethod(Runtime* runtime,
3231 const std::string& class_name,
3232 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003233 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003234 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003235 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003236 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003237 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003238 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003239 Thread::Current(),
3240 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003241 class_name,
3242 pointer_size,
3243 &klass,
3244 prepared);
3245 if (imt == nullptr) {
3246 return;
3247 }
3248
3249 std::cerr << class_name << " <" << method << ">" << std::endl;
3250 for (size_t index = 0; index < ImTable::kSize; ++index) {
3251 ArtMethod* ptr = imt->Get(index, pointer_size);
3252 if (ptr->IsRuntimeMethod()) {
3253 if (ptr->IsImtUnimplementedMethod()) {
3254 continue;
3255 }
3256
3257 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3258 if (current_table == nullptr) {
3259 continue;
3260 }
3261
3262 size_t table_index = 0;
3263 for (;;) {
3264 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3265 if (ptr2 == nullptr) {
3266 break;
3267 }
3268 table_index++;
3269
David Sehr709b0702016-10-13 09:12:37 -07003270 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003271 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003272 std::cerr << " Slot "
3273 << index
3274 << " ("
3275 << current_table->NumEntries(pointer_size)
3276 << ")"
3277 << std::endl;
3278 PrintTable(current_table, pointer_size);
3279 return;
3280 }
3281 }
3282 } else {
David Sehr709b0702016-10-13 09:12:37 -07003283 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003284 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003285 std::cerr << " Slot " << index << " (1)" << std::endl;
3286 std::cerr << " " << p_name << std::endl;
3287 } else {
3288 // Run through iftable, find methods that slot here, see if they fit.
3289 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003290 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3291 mirror::Class* iface = if_table->GetInterface(i);
3292 size_t num_methods = iface->NumDeclaredVirtualMethods();
3293 if (num_methods > 0) {
3294 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3295 if (ImTable::GetImtIndex(&iface_method) == index) {
3296 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003297 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003298 std::cerr << " Slot " << index << " (1)" << std::endl;
3299 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003300 }
3301 }
3302 }
3303 }
3304 }
3305 }
3306 }
3307 }
3308 }
3309
3310 // Read lines from the given stream, dropping comments and empty lines
3311 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3312 std::vector<std::string> output;
3313 while (in_stream.good()) {
3314 std::string dot;
3315 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003316 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003317 continue;
3318 }
3319 output.push_back(dot);
3320 }
3321 return output;
3322 }
3323
3324 // Read lines from the given file, dropping comments and empty lines.
3325 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3326 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3327 if (input_file.get() == nullptr) {
3328 LOG(ERROR) << "Failed to open input file " << input_filename;
3329 return std::vector<std::string>();
3330 }
3331 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3332 input_file->close();
3333 return result;
3334 }
3335
3336 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3337 // and note in the given set that the work was done.
3338 static void PrepareClass(Runtime* runtime,
3339 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003340 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003341 REQUIRES_SHARED(Locks::mutator_lock_) {
3342 if (!h_klass->ShouldHaveImt()) {
3343 return;
3344 }
3345
3346 std::string name;
3347 name = h_klass->GetDescriptor(&name);
3348
3349 if (done->find(name) != done->end()) {
3350 return;
3351 }
3352 done->insert(name);
3353
3354 if (h_klass->HasSuperClass()) {
3355 StackHandleScope<1> h(Thread::Current());
3356 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3357 }
3358
3359 if (!h_klass->IsTemp()) {
3360 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3361 }
3362 }
3363};
3364
Igor Murashkin37743352014-11-13 14:38:00 -08003365struct OatdumpArgs : public CmdlineArgs {
3366 protected:
3367 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003368
Roland Levillainf73caca2018-08-24 17:19:07 +01003369 ParseStatus ParseCustom(const StringPiece& option, std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003370 {
3371 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3372 if (base_parse != kParseUnknownArgument) {
3373 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003374 }
3375 }
3376
Igor Murashkin37743352014-11-13 14:38:00 -08003377 if (option.starts_with("--oat-file=")) {
3378 oat_filename_ = option.substr(strlen("--oat-file=")).data();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003379 } else if (option.starts_with("--dex-file=")) {
3380 dex_filename_ = option.substr(strlen("--dex-file=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003381 } else if (option.starts_with("--image=")) {
3382 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003383 } else if (option == "--no-dump:vmap") {
3384 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003385 } else if (option =="--dump:code_info_stack_maps") {
3386 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003387 } else if (option == "--no-disassemble") {
3388 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003389 } else if (option =="--header-only") {
3390 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003391 } else if (option.starts_with("--symbolize=")) {
3392 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3393 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003394 } else if (option.starts_with("--only-keep-debug")) {
3395 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003396 } else if (option.starts_with("--class-filter=")) {
3397 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003398 } else if (option.starts_with("--method-filter=")) {
3399 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003400 } else if (option.starts_with("--list-classes")) {
3401 list_classes_ = true;
3402 } else if (option.starts_with("--list-methods")) {
3403 list_methods_ = true;
3404 } else if (option.starts_with("--export-dex-to=")) {
3405 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3406 } else if (option.starts_with("--addr2instr=")) {
Andreas Gampef9411702018-09-06 17:16:57 -07003407 if (!android::base::ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003408 *error_msg = "Address conversion failed";
3409 return kParseError;
3410 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003411 } else if (option.starts_with("--app-image=")) {
3412 app_image_ = option.substr(strlen("--app-image=")).data();
3413 } else if (option.starts_with("--app-oat=")) {
3414 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003415 } else if (option.starts_with("--dump-imt=")) {
3416 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3417 } else if (option == "--dump-imt-stats") {
3418 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003419 } else {
3420 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003421 }
3422
Igor Murashkin37743352014-11-13 14:38:00 -08003423 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003424 }
3425
Roland Levillainf73caca2018-08-24 17:19:07 +01003426 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003427 // Infer boot image location from the image location if possible.
3428 if (boot_image_location_ == nullptr) {
3429 boot_image_location_ = image_location_;
3430 }
3431
3432 // Perform the parent checks.
3433 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3434 if (parent_checks != kParseOk) {
3435 return parent_checks;
3436 }
3437
3438 // Perform our own checks.
3439 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3440 *error_msg = "Either --image or --oat-file must be specified";
3441 return kParseError;
3442 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3443 *error_msg = "Either --image or --oat-file must be specified but not both";
3444 return kParseError;
3445 }
3446
3447 return kParseOk;
3448 }
3449
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003450 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003451 std::string usage;
3452
3453 usage +=
3454 "Usage: oatdump [options] ...\n"
3455 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3456 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3457 "\n"
3458 // Either oat-file or image is required.
3459 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3460 " Example: --oat-file=/system/framework/boot.oat\n"
3461 "\n"
3462 " --image=<file.art>: specifies an input image location.\n"
3463 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003464 "\n"
3465 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003466 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003467 " Example: --app-image=app.art\n"
3468 "\n"
3469 " --app-oat=<file.odex>: specifies an input app oat.\n"
3470 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003471 "\n";
3472
3473 usage += Base::GetUsage();
3474
3475 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003476 " --no-dump:vmap may be used to disable vmap dumping.\n"
3477 " Example: --no-dump:vmap\n"
3478 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003479 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3480 " Example: --dump:code_info_stack_maps\n"
3481 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003482 " --no-disassemble may be used to disable disassembly.\n"
3483 " Example: --no-disassemble\n"
3484 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003485 " --header-only may be used to print only the oat header.\n"
3486 " Example: --header-only\n"
3487 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003488 " --list-classes may be used to list target file classes (can be used with filters).\n"
3489 " Example: --list-classes\n"
3490 " Example: --list-classes --class-filter=com.example.foo\n"
3491 "\n"
3492 " --list-methods may be used to list target file methods (can be used with filters).\n"
3493 " Example: --list-methods\n"
3494 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3495 "\n"
3496 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3497 " Example: --symbolize=/system/framework/boot.oat\n"
3498 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003499 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3500 " .rodata and .text sections are omitted in the output file to save space.\n"
3501 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3502 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003503 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3504 " Example: --class-filter=com.example.foo\n"
3505 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003506 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3507 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003508 "\n"
3509 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3510 " Example: --export-dex-to=/data/local/tmp\n"
3511 "\n"
3512 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3513 " address (e.g. PC from crash dump)\n"
3514 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003515 "\n"
3516 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3517 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003518 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003519 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003520 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003521 " Example: --dump-imt=imt.txt\n"
3522 "\n"
3523 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3524 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003525 "\n";
3526
3527 return usage;
3528 }
3529
3530 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003531 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003532 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003533 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003534 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003535 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003536 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003537 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003538 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003539 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003540 bool disassemble_code_ = true;
3541 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003542 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003543 bool list_classes_ = false;
3544 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003545 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003546 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003547 uint32_t addr2instr_ = 0;
3548 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003549 const char* app_image_ = nullptr;
3550 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003551};
3552
Igor Murashkin37743352014-11-13 14:38:00 -08003553struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003554 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003555 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003556
Igor Murashkin37743352014-11-13 14:38:00 -08003557 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3558 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3559
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003560 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003561 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003562 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003563 args_->disassemble_code_,
3564 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003565 args_->class_filter_,
3566 args_->method_filter_,
3567 args_->list_classes_,
3568 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003569 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003570 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003571 args_->app_image_,
3572 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003573 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003574
Andreas Gampe9fded872016-09-25 16:08:35 -07003575 return (args_->boot_image_location_ != nullptr ||
3576 args_->image_location_ != nullptr ||
3577 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003578 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003579 }
3580
Roland Levillainf73caca2018-08-24 17:19:07 +01003581 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003582 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003583 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003584
Mathieu Chartierd424d082014-10-15 10:31:46 -07003585 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003586
Andreas Gampec24f3992014-12-17 20:40:11 -08003587 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003588 // ELF has special kind of section called SHT_NOBITS which allows us to create
3589 // sections which exist but their data is omitted from the ELF file to save space.
3590 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3591 // with only debug data. We use it in similar way to exclude .rodata and .text.
3592 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003593 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3594 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003595 } else {
3596 return DumpOat(nullptr,
3597 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003598 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003599 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003600 args_->os_) == EXIT_SUCCESS;
3601 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003602 }
3603
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003604 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003605 CHECK(args_ != nullptr);
3606
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003607 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3608 return IMTDumper::Dump(runtime,
3609 args_->imt_dump_,
3610 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003611 args_->oat_filename_,
3612 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003613 }
3614
Igor Murashkin37743352014-11-13 14:38:00 -08003615 if (args_->oat_filename_ != nullptr) {
3616 return DumpOat(runtime,
3617 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003618 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003619 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003620 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003621 }
Igor Murashkin37743352014-11-13 14:38:00 -08003622
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003623 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003624 }
3625
Igor Murashkin37743352014-11-13 14:38:00 -08003626 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3627};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003628
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003629} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003630
3631int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003632 // Output all logging to stderr.
3633 android::base::SetLogger(android::base::StderrLogger);
3634
Igor Murashkin37743352014-11-13 14:38:00 -08003635 art::OatdumpMain main;
3636 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003637}