blob: 453e9da1065fbd843c1db0ed8b3b8c25e0c01e12 [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>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070026#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include <vector>
28
Andreas Gampe221d9812018-01-22 17:48:56 -080029#include "android-base/logging.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080031#include "android-base/strings.h"
32
Ian Rogersd582fa42014-11-05 23:46:43 -080033#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070034#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "art_method-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000036#include "base/bit_utils_iterator.h"
David Sehr9c4a0152018-04-05 12:23:54 -070037#include "base/indenter.h"
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080039#include "base/safe_map.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000040#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080041#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070043#include "class_linker.h"
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010044#include "class_root.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010045#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000046#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000047#include "debug/elf_debug_writer.h"
48#include "debug/method_debug_info.h"
Mathieu Chartier3d092992018-05-24 13:36:23 -070049#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080050#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080051#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080052#include "dex/dex_file-inl.h"
53#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070054#include "dex/string_reference.h"
David Sehr9c4a0152018-04-05 12:23:54 -070055#include "dex/type_lookup_table.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080056#include "disassembler.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070057#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070058#include "gc/space/image_space.h"
59#include "gc/space/large_object_space.h"
60#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080061#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070062#include "imtable-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070063#include "subtype_check.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000064#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070065#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000066#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000067#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000068#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069#include "mirror/array-inl.h"
70#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010071#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072#include "mirror/object-inl.h"
73#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080074#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010075#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070076#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070077#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070078#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070079#include "stack_map.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080080#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010081#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080082#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000083#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070084#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070085
Igor Murashkin37743352014-11-13 14:38:00 -080086#include <sys/stat.h>
87#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070088
Igor Murashkin37743352014-11-13 14:38:00 -080089namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070090
Andreas Gampe46ee31b2016-12-14 10:11:49 -080091using android::base::StringPrintf;
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080094 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070095 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070096 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010097 "kSaveAllCalleeSavesMethod",
98 "kSaveRefsOnlyMethod",
99 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +0100100 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -0700101 "kSaveEverythingMethodForClinit",
102 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103};
104
105const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700106 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700107 "kClassRoots",
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100108 "kOomeWhenThrowingException",
109 "kOomeWhenThrowingOome",
110 "kOomeWhenHandlingStackOverflow",
111 "kNoClassDefFoundError",
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000112 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700113};
114
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700115// Map is so that we don't allocate multiple dex files for the same OatDexFile.
116static std::map<const OatFile::OatDexFile*,
117 std::unique_ptr<const DexFile>> opened_dex_files;
118
119const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
120 DCHECK(oat_dex_file != nullptr);
121 auto it = opened_dex_files.find(oat_dex_file);
122 if (it != opened_dex_files.end()) {
123 return it->second.get();
124 }
125 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
126 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
127 return ret;
128}
129
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800130template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100131class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000133 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
134 oat_file_(oat_file),
135 builder_(nullptr),
136 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
137 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700138 }
139
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700140 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800142 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000143 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
Andreas Gampe08c277c2017-04-26 22:22:15 -0700145 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
146 if (elf_file == nullptr) {
147 return false;
148 }
Vladimir Marko74527972016-11-29 15:57:32 +0000149 std::unique_ptr<linker::BufferedOutputStream> output_stream =
150 std::make_unique<linker::BufferedOutputStream>(
151 std::make_unique<linker::FileOutputStream>(elf_file.get()));
152 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153
154 builder_->Start();
155
156 auto* rodata = builder_->GetRoData();
157 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 const uint8_t* rodata_begin = oat_file_->Begin();
160 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000161 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000162 rodata->Start();
163 rodata->WriteFully(rodata_begin, rodata_size);
164 rodata->End();
165 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
168 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000169 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000170 text->Start();
171 text->WriteFully(text_begin, text_size);
172 text->End();
173 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174
Vladimir Marko33bff252017-11-01 14:35:42 +0000175 if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700176 builder_->WriteMIPSabiflagsSection();
177 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000178 builder_->PrepareDynamicSection(elf_file->GetPath(),
179 rodata_size,
180 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000181 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000182 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100183 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000184 oat_file_->BssRootsOffset(),
185 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000186 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700187
Vladimir Marko1b404a82017-09-01 13:35:26 +0100188 const OatHeader& oat_header = oat_file_->GetOatHeader();
189 #define DO_TRAMPOLINE(fn_name) \
190 if (oat_header.Get ## fn_name ## Offset() != 0) { \
191 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000192 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100193 info.isa = oat_header.GetInstructionSet(); \
194 info.is_code_address_text_relative = true; \
195 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
196 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
197 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
198 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
199 method_debug_infos_.push_back(std::move(info)); \
200 }
201 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
202 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
203 DO_TRAMPOLINE(JniDlsymLookup);
204 DO_TRAMPOLINE(QuickGenericJniTrampoline);
205 DO_TRAMPOLINE(QuickImtConflictTrampoline);
206 DO_TRAMPOLINE(QuickResolutionTrampoline);
207 DO_TRAMPOLINE(QuickToInterpreterBridge);
208 #undef DO_TRAMPOLINE
209
David Srbecky5d950762016-03-07 20:47:29 +0000210 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100211
212 // TODO: Try to symbolize link-time thunks?
213 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700214
David Srbecky32210b92017-12-04 14:39:21 +0000215 // TODO: Add symbols for dex bytecode in the .dex section.
216
217 debug::DebugInfo debug_info{};
218 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
219
David Srbecky5d950762016-03-07 20:47:29 +0000220 debug::WriteDebugInfo(builder_.get(),
David Srbecky32210b92017-12-04 14:39:21 +0000221 debug_info,
David Srbecky5d950762016-03-07 20:47:29 +0000222 dwarf::DW_DEBUG_FRAME_FORMAT,
223 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224
David Srbecky6d8c8f02015-10-26 10:57:09 +0000225 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700226
Andreas Gampe08c277c2017-04-26 22:22:15 -0700227 bool ret_value = builder_->Good();
228
229 builder_.reset();
230 output_stream.reset();
231
232 if (elf_file->FlushCloseOrErase() != 0) {
233 return false;
234 }
235 elf_file.reset();
236
237 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 }
239
David Srbecky5d950762016-03-07 20:47:29 +0000240 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
242 for (size_t i = 0; i < oat_dex_files.size(); i++) {
243 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000245 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 }
247 }
248
David Srbecky5d950762016-03-07 20:47:29 +0000249 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700250 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700251 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
252 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253 return;
254 }
255 for (size_t class_def_index = 0;
256 class_def_index < dex_file->NumClassDefs();
257 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700258 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
259 OatClassType type = oat_class.GetType();
260 switch (type) {
261 case kOatClassAllCompiled:
262 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000263 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700264 break;
265
266 case kOatClassNoneCompiled:
267 case kOatClassMax:
268 // Ignore.
269 break;
270 }
271 }
272 }
273
David Srbecky5d950762016-03-07 20:47:29 +0000274 void WalkOatClass(const OatFile::OatClass& oat_class,
275 const DexFile& dex_file,
276 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700277 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700278 // Note: even if this is an interface or a native class, we still have to walk it, as there
279 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700280 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700281 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000282 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
283 dex_file,
284 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700285 method.GetIndex(),
286 method.GetCodeItem(),
287 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 }
290
David Srbecky5d950762016-03-07 20:47:29 +0000291 void WalkOatMethod(const OatFile::OatMethod& oat_method,
292 const DexFile& dex_file,
293 uint32_t class_def_index,
294 uint32_t dex_method_index,
295 const DexFile::CodeItem* code_item,
296 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700297 if ((method_access_flags & kAccAbstract) != 0) {
298 // Abstract method, no code.
299 return;
300 }
David Srbecky5d950762016-03-07 20:47:29 +0000301 const OatHeader& oat_header = oat_file_->GetOatHeader();
302 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
303 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700304 // No code.
305 return;
306 }
307
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100308 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
309 // Clear Thumb2 bit.
310 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
311
Vladimir Marko1b404a82017-09-01 13:35:26 +0100312 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000313 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000314 info.dex_file = &dex_file;
315 info.class_def_index = class_def_index;
316 info.dex_method_index = dex_method_index;
317 info.access_flags = method_access_flags;
318 info.code_item = code_item;
319 info.isa = oat_header.GetInstructionSet();
320 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
321 info.is_native_debuggable = oat_header.IsNativeDebuggable();
322 info.is_optimized = method_header->IsOptimized();
323 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100324 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000325 info.code_size = method_header->GetCodeSize();
326 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
327 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
328 info.cfi = ArrayRef<uint8_t>();
329 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700330 }
331
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700332 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700333 const OatFile* oat_file_;
Vladimir Marko74527972016-11-29 15:57:32 +0000334 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000335 std::vector<debug::MethodDebugInfo> method_debug_infos_;
336 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700337 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000338 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700339};
340
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341class OatDumperOptions {
342 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100343 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100344 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700346 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800347 const char* class_filter,
348 const char* method_filter,
349 bool list_classes,
350 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000351 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800352 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800353 const char* app_image,
354 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100356 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100357 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700358 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700359 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000361 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800362 list_classes_(list_classes),
363 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000364 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800365 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800366 app_image_(app_image),
367 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800368 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800369 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100372 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373 const bool disassemble_code_;
374 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000376 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 const bool list_classes_;
378 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000379 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800380 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800381 const char* const app_image_;
382 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800383 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700384 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700385};
386
Elliott Hughese3c845c2012-02-28 17:23:01 -0800387class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700388 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100389 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000390 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800391 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700392 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800393 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800394 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
395 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700396 new DisassemblerOptions(
397 options_.absolute_addresses_,
398 oat_file.Begin(),
399 oat_file.End(),
400 true /* can_read_literals_ */,
401 Is64BitInstructionSet(instruction_set_)
402 ? &Thread::DumpThreadOffset<PointerSize::k64>
403 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800404 CHECK(options_.class_loader_ != nullptr);
405 CHECK(options_.class_filter_ != nullptr);
406 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800407 AddAllOffsets();
408 }
409
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700410 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700411 delete disassembler_;
412 }
413
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800414 InstructionSet GetInstructionSet() {
415 return instruction_set_;
416 }
417
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300418 typedef std::vector<std::unique_ptr<const DexFile>> DexFileUniqV;
419
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700420 bool Dump(std::ostream& os) {
421 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800422 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700423
424 os << "MAGIC:\n";
425 os << oat_header.GetMagic() << "\n\n";
426
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800427 os << "LOCATION:\n";
428 os << oat_file_.GetLocation() << "\n\n";
429
Brian Carlstromaded5f72011-10-07 17:15:04 -0700430 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800431 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700432
Elliott Hughesa72ec822012-03-05 17:12:22 -0800433 os << "INSTRUCTION SET:\n";
434 os << oat_header.GetInstructionSet() << "\n\n";
435
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700436 {
437 std::unique_ptr<const InstructionSetFeatures> features(
438 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
439 oat_header.GetInstructionSetFeaturesBitmap()));
440 os << "INSTRUCTION SET FEATURES:\n";
441 os << features->GetFeatureString() << "\n\n";
442 }
Dave Allison70202782013-10-22 17:52:19 -0700443
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444 os << "DEX FILE COUNT:\n";
445 os << oat_header.GetDexFileCount() << "\n\n";
446
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800447#define DUMP_OAT_HEADER_OFFSET(label, offset) \
448 os << label " OFFSET:\n"; \
449 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800450 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800451 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
452 } \
453 os << StringPrintf("\n\n");
454
455 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
456 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
457 GetInterpreterToInterpreterBridgeOffset);
458 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
459 GetInterpreterToCompiledCodeBridgeOffset);
460 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
461 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800462 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
463 GetQuickGenericJniTrampolineOffset);
464 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
465 GetQuickImtConflictTrampolineOffset);
466 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
467 GetQuickResolutionTrampolineOffset);
468 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
469 GetQuickToInterpreterBridgeOffset);
470#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700471
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700472 os << "IMAGE PATCH DELTA:\n";
473 os << StringPrintf("%d (0x%08x)\n\n",
474 oat_header.GetImagePatchDelta(),
475 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700476
Brian Carlstrom28db0122012-10-18 16:20:41 -0700477 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
478 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
479
480 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800481 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700482
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700483 // Print the key-value store.
484 {
485 os << "KEY VALUE STORE:\n";
486 size_t index = 0;
487 const char* key;
488 const char* value;
489 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
490 os << key << " = " << value << "\n";
491 index++;
492 }
493 os << "\n";
494 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700495
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800496 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700497 os << "BEGIN:\n";
498 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700499
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700500 os << "END:\n";
501 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
502 }
503
504 os << "SIZE:\n";
505 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700506
507 os << std::flush;
508
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800509 // If set, adjust relative address to be searched
510 if (options_.addr2instr_ != 0) {
511 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
512 os << "SEARCH ADDRESS (executable offset + input):\n";
513 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
514 }
515
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000516 // Dump .data.bimg.rel.ro entries.
517 DumpDataBimgRelRoEntries(os);
518
519 // Dump .bss summary, individual entries are dumped per dex file.
520 os << ".bss: ";
521 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
522 os << "empty.\n\n";
523 } else {
524 os << oat_file_.GetBssMethods().size() << " methods, ";
525 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
526 }
527
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700528 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700529 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
530 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
531 CHECK(oat_dex_file != nullptr);
532 std::string error_msg;
533 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
534 if (dex_file == nullptr) {
535 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
536 << error_msg;
537 continue;
538 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700539
Mathieu Chartier120aa282017-08-05 16:03:03 -0700540 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
541 if (layout_sections != nullptr) {
542 os << "Layout data\n";
543 os << *layout_sections;
544 os << "\n";
545 }
546
Vladimir Markof3c52b42017-11-17 17:32:12 +0000547 // Dump .bss entries.
548 DumpBssEntries(
549 os,
550 "ArtMethod",
551 oat_dex_file->GetMethodBssMapping(),
552 dex_file->NumMethodIds(),
553 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
554 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
555 DumpBssEntries(
556 os,
557 "Class",
558 oat_dex_file->GetTypeBssMapping(),
559 dex_file->NumTypeIds(),
560 sizeof(GcRoot<mirror::Class>),
561 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
562 DumpBssEntries(
563 os,
564 "String",
565 oat_dex_file->GetStringBssMapping(),
566 dex_file->NumStringIds(),
567 sizeof(GcRoot<mirror::Class>),
568 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700569 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700570
David Brazdilc03d7b62016-03-02 12:18:03 +0000571 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000572 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000573 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000574 if (vdex_header.IsValid()) {
575 std::string error_msg;
576 std::vector<const DexFile*> dex_files;
577 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
578 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
579 if (dex_file == nullptr) {
580 os << "Error opening dex file: " << error_msg << std::endl;
581 return false;
582 }
583 dex_files.push_back(dex_file);
584 }
585 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
586 deps.Dump(&vios);
587 } else {
588 os << "UNRECOGNIZED vdex file, magic "
589 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000590 << ", verifier deps version "
591 << vdex_header.GetVerifierDepsVersion()
592 << ", dex section version "
593 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000594 << "\n";
595 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000596 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
597 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
598 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300599 if (!DumpOatDexFile(os, *oat_dex_file)) {
600 success = false;
601 }
602 }
603 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800604
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300605 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000606 std::string error_msg;
607 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
608 if (!OS::FileExists(vdex_filename.c_str())) {
609 os << "File " << vdex_filename.c_str() << " does not exist\n";
610 return false;
611 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300612
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000613 DexFileUniqV vdex_dex_files;
614 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
615 &vdex_dex_files,
616 &error_msg);
617 if (vdex_file.get() == nullptr) {
618 os << "Failed to open vdex file: " << error_msg << "\n";
619 return false;
620 }
621 if (oat_dex_files_.size() != vdex_dex_files.size()) {
622 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
623 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
624 return false;
625 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300626
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000627 size_t i = 0;
628 for (const auto& vdex_dex_file : vdex_dex_files) {
629 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
630 CHECK(oat_dex_file != nullptr);
631 CHECK(vdex_dex_file != nullptr);
632 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get())) {
633 success = false;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300634 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000635 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700636 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700637 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000638
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800639 {
640 os << "OAT FILE STATS:\n";
641 VariableIndentationOutputStream vios(&os);
642 stats_.Dump(vios);
643 }
644
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700645 os << std::flush;
646 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647 }
648
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800649 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700650 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
651 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800652 return 0; // Address not in oat file
653 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800654 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
655 reinterpret_cast<uintptr_t>(oat_file_.Begin());
656 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800657 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800658 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 return end_offset - begin_offset;
660 }
661
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800662 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700663 return oat_file_.GetOatHeader().GetInstructionSet();
664 }
665
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700666 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800667 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
668 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700669 CHECK(oat_dex_file != nullptr);
670 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700671 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
672 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700673 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
674 << "': " << error_msg;
675 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800676 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700677 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700678 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700679 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700680 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100681 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800682 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100683 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800684 }
685 }
686 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700687 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800688 }
689
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300690 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
691 // files are fully unquickened and stored in dex_files
692 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
693 /* out */ DexFileUniqV* dex_files,
694 /* out */ std::string* error_msg) {
695 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
696 if (file == nullptr) {
697 *error_msg = "Could not open file " + vdex_filename + " for reading.";
698 return nullptr;
699 }
700
701 int64_t vdex_length = file->GetLength();
702 if (vdex_length == -1) {
703 *error_msg = "Could not read the length of file " + vdex_filename;
704 return nullptr;
705 }
706
707 std::unique_ptr<MemMap> mmap(MemMap::MapFile(
708 file->GetLength(),
709 PROT_READ | PROT_WRITE,
710 MAP_PRIVATE,
711 file->Fd(),
712 /* start offset */ 0,
713 /* low_4gb */ false,
714 vdex_filename.c_str(),
715 error_msg));
716 if (mmap == nullptr) {
717 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
718 return nullptr;
719 }
720
721 std::unique_ptr<VdexFile> vdex_file(new VdexFile(mmap.release()));
722 if (!vdex_file->IsValid()) {
723 *error_msg = "Vdex file is not valid";
724 return nullptr;
725 }
726
727 DexFileUniqV tmp_dex_files;
728 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
729 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
730 return nullptr;
731 }
732
733 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300734 /* decompile_return_instruction */ true);
735
736 *dex_files = std::move(tmp_dex_files);
737 return vdex_file;
738 }
739
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800740 struct Stats {
741 enum ByteKind {
742 kByteKindCode,
743 kByteKindQuickMethodHeader,
744 kByteKindCodeInfoLocationCatalog,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100745 kByteKindCodeInfoDexRegisterMask,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800746 kByteKindCodeInfoDexRegisterMap,
David Srbecky052f8ca2018-04-26 15:42:54 +0100747 kByteKindCodeInfo,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800748 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000749 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800750 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800751 kByteKindStackMapNativePc,
752 kByteKindStackMapDexPc,
753 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800754 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800755 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000756 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700757 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800758 kByteKindInlineInfoDexPc,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100759 kByteKindInlineInfoArtMethod,
David Srbecky6de88332018-06-03 12:00:11 +0100760 kByteKindInlineInfoNumDexRegisters,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800761 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800762 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800763 // Special ranges for std::accumulate convenience.
764 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000765 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700766 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800767 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800768 };
769 int64_t bits[kByteKindCount] = {};
770 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
771 // deduplicated code and tables.
772 std::unordered_set<const void*> seen;
773
774 // Returns true if it was newly added.
775 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
776 if (seen.insert(address).second == true) {
777 // True means the address was not already in the set.
778 AddBits(kind, count);
779 return true;
780 }
781 return false;
782 }
783
784 void AddBits(ByteKind kind, int64_t count) {
785 bits[kind] += count;
786 }
787
788 void Dump(VariableIndentationOutputStream& os) {
789 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
790 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
791 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800792 Dump(os, "Code ", bits[kByteKindCode], sum);
793 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
David Srbecky052f8ca2018-04-26 15:42:54 +0100794 Dump(os, "CodeInfo ", bits[kByteKindCodeInfo], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800795 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100796 Dump(os, "CodeInfoDexRegisterMask ", bits[kByteKindCodeInfoDexRegisterMask], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800797 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800798 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
799 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800800 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800801 // Stack map section.
802 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
803 bits + kByteKindStackMapLast + 1,
804 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800805 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800806 {
807 ScopedIndentation indent1(&os);
808 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800809 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800810 bits[kByteKindStackMapNativePc],
811 stack_map_bits,
812 "stack map");
813 Dump(os,
David Srbecky052f8ca2018-04-26 15:42:54 +0100814 "StackMapDexPc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800815 bits[kByteKindStackMapDexPc],
816 stack_map_bits,
817 "stack map");
818 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800819 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800820 bits[kByteKindStackMapDexRegisterMap],
821 stack_map_bits,
822 "stack map");
823 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800824 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800825 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800826 stack_map_bits,
827 "stack map");
828 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800829 "StackMapRegisterMaskIndex ",
830 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800831 stack_map_bits,
832 "stack map");
833 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800834 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000835 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800836 stack_map_bits,
837 "stack map");
838 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800839 // Inline info section.
840 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
841 bits + kByteKindInlineInfoLast + 1,
842 0u);
843 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
844 {
845 ScopedIndentation indent1(&os);
846 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700847 "InlineInfoMethodIndexIdx ",
848 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800849 inline_info_bits,
850 "inline info");
851 Dump(os,
852 "InlineInfoDexPc ",
853 bits[kByteKindStackMapDexPc],
854 inline_info_bits,
855 "inline info");
856 Dump(os,
David Srbecky71ec1cc2018-05-18 15:57:25 +0100857 "InlineInfoArtMethod ",
858 bits[kByteKindInlineInfoArtMethod],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800859 inline_info_bits,
860 "inline info");
861 Dump(os,
David Srbecky6de88332018-06-03 12:00:11 +0100862 "InlineInfoNumDexRegisters ",
863 bits[kByteKindInlineInfoNumDexRegisters],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800864 inline_info_bits,
865 "inline info");
866 Dump(os,
867 "InlineInfoIsLast ",
868 bits[kByteKindInlineInfoIsLast],
869 inline_info_bits,
870 "inline info");
871 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800872 }
873 os.Stream() << "\n" << std::flush;
874 }
875
876 private:
877 void Dump(VariableIndentationOutputStream& os,
878 const char* name,
879 int64_t size,
880 int64_t total,
881 const char* sum_of = "total") {
882 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
883 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
884 name,
885 size / kBitsPerByte,
886 percent,
887 sum_of);
888 }
889 };
890
Brian Carlstromaded5f72011-10-07 17:15:04 -0700891 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800892 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800893 // We don't know the length of the code for each method, but we need to know where to stop
894 // when disassembling. What we do know is that a region of code will be followed by some other
895 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
896 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800897 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
898 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700899 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700900 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700901 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
902 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700903 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
904 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800905 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800906 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800907 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700908 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700909 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700910 for (uint32_t class_method_index = 0;
911 class_method_index < accessor.NumMethods();
912 ++class_method_index) {
913 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800914 }
915 }
916 }
917
918 // If the last thing in the file is code for a method, there won't be an offset for the "next"
919 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
920 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800921 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800922 }
923
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700924 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
925 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
926 }
927
Elliott Hughese3c845c2012-02-28 17:23:01 -0800928 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800929 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000930 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800931 code_offset &= ~0x1;
932 }
933 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800934 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800935 }
936
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
938 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800939 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800941 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800942 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700943
Andreas Gampe2ba88952016-04-29 17:52:07 -0700944 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000945 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
946 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100947
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000948 // Print data range of the dex file embedded inside the corresponding vdex file.
949 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
950 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
951 os << StringPrintf(
952 "dex-file: 0x%08x..0x%08x\n",
953 dex_offset,
954 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
955 } else {
956 os << StringPrintf("dex-file not in VDEX file\n");
957 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700958
Andreas Gampe2ba88952016-04-29 17:52:07 -0700959 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700960 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700961 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
962 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700963 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 os << std::flush;
965 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700966 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100967
Andreas Gampe2ba88952016-04-29 17:52:07 -0700968 // Print lookup table, if it exists.
969 if (oat_dex_file.GetLookupTableData() != nullptr) {
970 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
971 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700972 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700973 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
974 table_offset,
975 table_offset + table_size - 1);
976 }
977
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100978 VariableIndentationOutputStream vios(&os);
979 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800980 for (size_t class_def_index = 0;
981 class_def_index < dex_file->NumClassDefs();
982 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700983 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
984 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800985
986 // TODO: Support regex
987 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
988 continue;
989 }
990
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700991 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100992 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700993 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800994 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100995 << " (" << oat_class.GetStatus() << ")"
996 << " (" << oat_class.GetType() << ")\n";
997 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700998 if (options_.list_classes_) {
999 continue;
1000 }
1001 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001002 success = false;
1003 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001004 if (stop_analysis) {
1005 os << std::flush;
1006 return success;
1007 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001008 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001009 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001010 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001012 }
1013
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001014 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
1015 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
1016 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
1017 // original checksum.
1018 bool ExportDexFile(std::ostream& os,
1019 const OatFile::OatDexFile& oat_dex_file,
1020 const DexFile* dex_file) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001021 std::string error_msg;
1022 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001023 size_t fsize = oat_dex_file.FileSize();
1024
1025 // Some quick checks just in case
1026 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1027 os << "Invalid dex file\n";
1028 return false;
1029 }
1030
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001031 if (dex_file == nullptr) {
1032 // Exported bytecode is quickened (dex-to-dex transformations present)
1033 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1034 if (dex_file == nullptr) {
1035 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1036 return false;
1037 }
1038
1039 // Recompute checksum
1040 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
1041 dex_file->CalculateChecksum();
1042 } else {
1043 // Vdex unquicken output should match original input bytecode
1044 uint32_t orig_checksum =
1045 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001046 CHECK_EQ(orig_checksum, dex_file->CalculateChecksum());
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001047 if (orig_checksum != dex_file->CalculateChecksum()) {
1048 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1049 return false;
1050 }
1051 }
1052
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001053 // Update header for shared section.
1054 uint32_t shared_section_offset = 0u;
1055 uint32_t shared_section_size = 0u;
1056 if (dex_file->IsCompactDexFile()) {
1057 CompactDexFile::Header* const header =
1058 reinterpret_cast<CompactDexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()));
1059 shared_section_offset = header->data_off_;
1060 shared_section_size = header->data_size_;
1061 // The shared section will be serialized right after the dex file.
1062 header->data_off_ = header->file_size_;
1063 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001064 // Verify output directory exists
1065 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1066 // TODO: Extend OS::DirectoryExists if symlink support is required
1067 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1068 return false;
1069 }
1070
1071 // Beautify path names
1072 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1073 return false;
1074 }
1075
1076 std::string dex_orig_name;
1077 size_t dex_orig_pos = dex_file_location.rfind('/');
1078 if (dex_orig_pos == std::string::npos)
1079 dex_orig_name = dex_file_location;
1080 else
1081 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1082
1083 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001084 if (dex_orig_name.size() == 8 &&
1085 dex_orig_name.compare("base.apk") == 0 &&
1086 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001087 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1088 size_t apk_orig_pos = dex_file_location.rfind('/');
1089 if (apk_orig_pos != std::string::npos) {
1090 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1091 }
1092 }
1093
1094 std::string out_dex_path(options_.export_dex_location_);
1095 if (out_dex_path.back() != '/') {
1096 out_dex_path.append("/");
1097 }
1098 out_dex_path.append(dex_orig_name);
1099 out_dex_path.append("_export.dex");
1100 if (out_dex_path.length() > PATH_MAX) {
1101 return false;
1102 }
1103
1104 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1105 if (file.get() == nullptr) {
1106 os << "Failed to open output dex file " << out_dex_path;
1107 return false;
1108 }
1109
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001110 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001111 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001112 os << "Failed to write dex file";
1113 file->Erase();
1114 return false;
1115 }
1116
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001117 if (shared_section_size != 0) {
1118 success = file->WriteFully(dex_file->Begin() + shared_section_offset, shared_section_size);
1119 if (!success) {
1120 os << "Failed to write shared data section";
1121 file->Erase();
1122 return false;
1123 }
1124 }
1125
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001126 if (file->FlushCloseOrErase() != 0) {
1127 os << "Flush and close failed";
1128 return false;
1129 }
1130
1131 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1132 os << std::flush;
1133
1134 return true;
1135 }
1136
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001137 bool DumpOatClass(VariableIndentationOutputStream* vios,
1138 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001139 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001140 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001141 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001142 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001143 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001144 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001145 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001146 }
1147 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001148 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001149 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001150 while (it.HasNextMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001151 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001152 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001153 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001154 success = false;
1155 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001156 if (addr_found) {
1157 *stop_analysis = true;
1158 return success;
1159 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001160 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001161 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001162 }
Ian Rogers0571d352011-11-03 19:51:38 -07001163 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001165 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001166 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001167
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001168 static constexpr uint32_t kPrologueBytes = 16;
1169
1170 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1171 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1172
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001173 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1174 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001175 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001176 const OatFile::OatClass& oat_class,
1177 const DexFile& dex_file,
1178 uint32_t dex_method_idx,
1179 const DexFile::CodeItem* code_item,
1180 uint32_t method_access_flags,
1181 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001182 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001183
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001184 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001185
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001186 // TODO: Support regex
1187 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1188 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001189 return success;
1190 }
1191
David Sehr709b0702016-10-13 09:12:37 -07001192 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1194 class_method_index, pretty_method.c_str(),
1195 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001196 if (options_.list_methods_) {
1197 return success;
1198 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001199
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001200 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1201 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1202 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1203 uint32_t code_offset = oat_method.GetCodeOffset();
1204 uint32_t code_size = oat_method.GetQuickCodeSize();
1205 if (resolved_addr2instr_ != 0) {
1206 if (resolved_addr2instr_ > code_offset + code_size) {
1207 return success;
1208 } else {
1209 *addr_found = true; // stop analyzing file at next iteration
1210 }
1211 }
1212
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001213 // Everything below is indented at least once.
1214 ScopedIndentation indent1(vios);
1215
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001216 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 vios->Stream() << "DEX CODE:\n";
1218 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001219 if (code_item_accessor.HasCodeItem()) {
1220 for (const DexInstructionPcPair& inst : code_item_accessor) {
1221 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1222 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1223 }
1224 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001225 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001226
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001227 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001228 std::unique_ptr<verifier::MethodVerifier> verifier;
1229 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001230 // We need to have the handle scope stay live until after the verifier since the verifier has
1231 // a handle to the dex cache from hs.
1232 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001233 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1234 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001235 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001236 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001237 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001238 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001239 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001241 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001243 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001245 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001246 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1248 oat_method_offsets_offset, oat_file_.Size());
1249 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001250 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001251 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001252 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001254 ScopedIndentation indent2(vios);
1255 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001256 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1257 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001258 vios->Stream() << StringPrintf("WARNING: "
1259 "code offset 0x%08x is past end of file 0x%08zx.\n",
1260 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001261 success = false;
1262 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001264 }
1265 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001266 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001267 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1268 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001269 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1270 sizeof(*method_header) * kBitsPerByte,
1271 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001272 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001276 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001277 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001278 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1279 method_header_offset, oat_file_.Size());
1280 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001281 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282 return false;
1283 }
1284
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001286 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001287 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001288 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001289 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001290 uint32_t vmap_table_offset = method_header ==
1291 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001292 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001293
1294 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001295 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001296 ? oat_file_.GetVdexFile()->Size()
1297 : method_header->GetCode() - oat_file_.Begin();
1298 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001299 vios->Stream() << StringPrintf("WARNING: "
1300 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1301 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001302 vmap_table_offset,
1303 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001305 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001306 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001307 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001308 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001309 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001310 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001311 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001312
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001313 ScopedIndentation indent2(vios);
1314 vios->Stream()
1315 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1316 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1317 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1318 vios->Stream() << "\n";
1319 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1320 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1321 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001322 }
1323 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001324 // Based on spill masks from QuickMethodFrameInfo so placed
1325 // after it is dumped, but useful for understanding quick
1326 // code, so dumped here.
1327 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001328 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001329 }
1330 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001331 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001332 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1333 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001334 ScopedIndentation indent2(vios);
1335 vios->Stream() << StringPrintf("WARNING: "
1336 "code size offset 0x%08x is past end of file 0x%08zx.",
1337 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001338 success = false;
1339 } else {
1340 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001341 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1342 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001343 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001344
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001345 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001346 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001347 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001348 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1349 code_offset,
1350 code_size_offset,
1351 code_size,
1352 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001353
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001354 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001355 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001356 vios->Stream() << StringPrintf("WARNING: "
1357 "start of code at 0x%08x is past end of file 0x%08zx.",
1358 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001359 success = false;
1360 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001361 vios->Stream() << StringPrintf(
1362 "WARNING: "
1363 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1364 "code size is 0x%08x loaded from offset 0x%08x.\n",
1365 aligned_code_end, oat_file_.Size(),
1366 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001367 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001368 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001369 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001370 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001371 }
1372 }
1373 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001374 vios->Stream() << StringPrintf(
1375 "WARNING: "
1376 "code size %d is bigger than max expected threshold of %d. "
1377 "code size is 0x%08x loaded from offset 0x%08x.\n",
1378 code_size, kMaxCodeSize,
1379 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001380 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001381 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001382 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001383 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001384 }
1385 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001386 } else if (options_.disassemble_code_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001387 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001388 }
1389 }
1390 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001391 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001392 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001393 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001394
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001395 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1396 if (spill_mask == 0) {
1397 return;
1398 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001399 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001400 for (size_t i = 0; i < 32; i++) {
1401 if ((spill_mask & (1 << i)) != 0) {
1402 if (is_float) {
1403 os << "fr" << i;
1404 } else {
1405 os << "r" << i;
1406 }
1407 spill_mask ^= 1 << i; // clear bit
1408 if (spill_mask != 0) {
1409 os << ", ";
1410 } else {
1411 break;
1412 }
1413 }
1414 }
1415 os << ")";
1416 }
1417
Roland Levillain442b46a2015-02-18 16:54:21 +00001418 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001419 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001420 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001421 const CodeItemDataAccessor& code_item_accessor) {
1422 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001423 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001424 const void* raw_code_info = oat_method.GetVmapTable();
1425 if (raw_code_info != nullptr) {
1426 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001427 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001428 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001429 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
David Srbeckyfd89b072018-06-03 12:00:22 +01001430 DumpCodeInfo(vios, code_info, oat_method, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001431 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001432 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001433 // We don't encode the size in the table, so just emit that we have quickened
1434 // information.
1435 ScopedIndentation indent(vios);
1436 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001437 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001438 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001439 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001440 }
1441
1442 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001443 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001444 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001445 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001446 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001447 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001448 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001449 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001450 instruction_set_,
1451 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001452 }
1453
Andreas Gampe36a296f2017-06-13 14:11:11 -07001454 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1455 // According to stack model, the first out is above the Method referernce.
1456 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1457 }
1458
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001459 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001460 uint32_t core_spills,
1461 uint32_t fp_spills,
1462 size_t frame_size,
1463 int reg,
1464 InstructionSet isa) {
1465 PointerSize pointer_size = InstructionSetPointerSize(isa);
1466 if (kIsDebugBuild) {
1467 auto* runtime = Runtime::Current();
1468 if (runtime != nullptr) {
1469 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1470 }
1471 }
1472 DCHECK_ALIGNED(frame_size, kStackAlignment);
1473 DCHECK_NE(reg, -1);
1474 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1475 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1476 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001477 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1478 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001479 const int max_num_special_temps = 1;
1480 if (reg == temp_threshold) {
1481 // The current method pointer corresponds to special location on stack.
1482 return 0;
1483 } else if (reg >= temp_threshold + max_num_special_temps) {
1484 /*
1485 * Special temporaries may have custom locations and the logic above deals with that.
1486 * However, non-special temporaries are placed relative to the outs.
1487 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001488 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001489 + static_cast<size_t>(pointer_size) /* art method */;
1490 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1491 return temps_start + relative_offset;
1492 } else if (reg < num_regs) {
1493 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1494 return locals_start + (reg * sizeof(uint32_t));
1495 } else {
1496 // Handle ins.
1497 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1498 + static_cast<size_t>(pointer_size) /* art method */;
1499 }
1500 }
1501
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001502 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001503 const CodeItemDataAccessor& code_item_accessor) {
1504 if (code_item_accessor.HasCodeItem()) {
1505 size_t num_locals_ins = code_item_accessor.RegistersSize();
1506 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001507 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001508 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001509
1510 os << "vr_stack_locations:";
1511 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1512 // For readability, delimit the different kinds of VRs.
1513 if (reg == num_locals_ins) {
1514 os << "\n\tmethod*:";
1515 } else if (reg == num_locals && num_ins > 0) {
1516 os << "\n\tins:";
1517 } else if (reg == 0 && num_locals > 0) {
1518 os << "\n\tlocals:";
1519 }
1520
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001521 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001522 oat_method.GetCoreSpillMask(),
1523 oat_method.GetFpSpillMask(),
1524 oat_method.GetFrameSizeInBytes(),
1525 reg,
1526 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001527 os << " v" << reg << "[sp + #" << offset << "]";
1528 }
1529
1530 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1531 if (out_reg == 0) {
1532 os << "\n\touts:";
1533 }
1534
Andreas Gampe36a296f2017-06-13 14:11:11 -07001535 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001536 os << " v" << out_reg << "[sp + #" << offset << "]";
1537 }
1538
1539 os << "\n";
1540 }
1541 }
1542
Roland Levillainf2650d12015-05-28 14:53:28 +01001543 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1544 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001545 static bool IsMethodGeneratedByOptimizingCompiler(
1546 const OatFile::OatMethod& oat_method,
1547 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001548 // If the native GC map is null and the Dex `code_item` is not
1549 // null, then this method has been compiled with the optimizing
1550 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001551 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001552 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001553 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001554 }
1555
1556 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1557 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001558 static bool IsMethodGeneratedByDexToDexCompiler(
1559 const OatFile::OatMethod& oat_method,
1560 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001561 // If the quick code is null, the Dex `code_item` is not
1562 // null, and the vmap table is not null, then this method has been compiled
1563 // with the dextodex compiler.
1564 return oat_method.GetQuickCode() == nullptr &&
1565 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001566 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001567 }
1568
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001569 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001570 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001571 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001572 const DexFile* dex_file,
1573 const DexFile::ClassDef& class_def,
1574 const DexFile::CodeItem* code_item,
1575 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 if ((method_access_flags & kAccNative) == 0) {
1577 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001578 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001579 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001580 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1581 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1582 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001583 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1584 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001585 if (method == nullptr) {
1586 soa.Self()->ClearException();
1587 return nullptr;
1588 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001589 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001590 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001591 class_def, code_item, method, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001593
1594 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 }
1596
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001597 // The StackMapsHelper provides the stack maps in the native PC order.
1598 // For identical native PCs, the order from the CodeInfo is preserved.
1599 class StackMapsHelper {
1600 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001601 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001602 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001603 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001604 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001605 offset_(static_cast<uint32_t>(-1)),
1606 stack_map_index_(0u),
1607 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001608 if (number_of_stack_maps_ != 0u) {
1609 // Check if native PCs are ordered.
1610 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001611 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001612 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001613 StackMap current = code_info_.GetStackMapAt(i);
1614 if (last.GetNativePcOffset(instruction_set) >
1615 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001616 ordered = false;
1617 break;
1618 }
1619 last = current;
1620 }
1621 if (!ordered) {
1622 // Create indirection indexes for access in native PC order. We do not optimize
1623 // for the fact that there can currently be only two separately ordered ranges,
1624 // namely normal stack maps and catch-point stack maps.
1625 indexes_.resize(number_of_stack_maps_);
1626 std::iota(indexes_.begin(), indexes_.end(), 0u);
1627 std::sort(indexes_.begin(),
1628 indexes_.end(),
1629 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001630 StackMap left = code_info_.GetStackMapAt(lhs);
1631 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1632 StackMap right = code_info_.GetStackMapAt(rhs);
1633 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001634 // If the PCs are the same, compare indexes to preserve the original order.
1635 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1636 });
1637 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001638 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001639 }
1640 }
1641
1642 const CodeInfo& GetCodeInfo() const {
1643 return code_info_;
1644 }
1645
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001646 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001647 return offset_;
1648 }
1649
1650 StackMap GetStackMap() const {
1651 return GetStackMapAt(stack_map_index_);
1652 }
1653
1654 void Next() {
1655 ++stack_map_index_;
1656 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001657 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001658 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001659 }
1660
1661 private:
1662 StackMap GetStackMapAt(size_t i) const {
1663 if (!indexes_.empty()) {
1664 i = indexes_[i];
1665 }
1666 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001667 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001668 }
1669
1670 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001671 const size_t number_of_stack_maps_;
1672 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001673 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001674 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001675 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001676 };
1677
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001678 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001679 const OatFile::OatMethod& oat_method,
1680 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001681 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001682 const void* quick_code = oat_method.GetQuickCode();
1683
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001684 if (code_size == 0) {
1685 code_size = oat_method.GetQuickCodeSize();
1686 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001687 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001688 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001689 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001690 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1691 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001692 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001693 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001694 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001695 {
David Srbecky052f8ca2018-04-26 15:42:54 +01001696 const CodeInfo code_info = helper.GetCodeInfo();
1697 const BitTable<StackMap::kCount>& stack_maps = code_info.stack_maps_;
1698 const size_t num_stack_maps = stack_maps.NumRows();
1699 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfo,
1700 code_info.size_ * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001701 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001702 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001703 stats_.AddBits(
1704 Stats::kByteKindStackMapNativePc,
David Srbeckyd02b23f2018-05-29 23:27:22 +01001705 stack_maps.NumColumnBits(StackMap::kPackedNativePc) * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001706 stats_.AddBits(
1707 Stats::kByteKindStackMapDexPc,
David Srbecky052f8ca2018-04-26 15:42:54 +01001708 stack_maps.NumColumnBits(StackMap::kDexPc) * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001709 stats_.AddBits(
1710 Stats::kByteKindStackMapDexRegisterMap,
David Srbecky71ec1cc2018-05-18 15:57:25 +01001711 stack_maps.NumColumnBits(StackMap::kDexRegisterMapIndex) * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001712 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001713 Stats::kByteKindStackMapInlineInfoIndex,
David Srbecky052f8ca2018-04-26 15:42:54 +01001714 stack_maps.NumColumnBits(StackMap::kInlineInfoIndex) * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001715 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001716 Stats::kByteKindStackMapRegisterMaskIndex,
David Srbecky052f8ca2018-04-26 15:42:54 +01001717 stack_maps.NumColumnBits(StackMap::kRegisterMaskIndex) * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001718 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001719 Stats::kByteKindStackMapStackMaskIndex,
David Srbecky052f8ca2018-04-26 15:42:54 +01001720 stack_maps.NumColumnBits(StackMap::kStackMaskIndex) * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001721
1722 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001723 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001724 Stats::kByteKindCodeInfoStackMasks,
David Srbecky4b59d102018-05-29 21:46:10 +00001725 code_info.stack_masks_.DataBitSize());
Mathieu Chartier1e083792017-02-08 13:30:04 -08001726
1727 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001728 stats_.AddBits(
1729 Stats::kByteKindCodeInfoRegisterMasks,
David Srbecky052f8ca2018-04-26 15:42:54 +01001730 code_info.register_masks_.DataBitSize());
Mathieu Chartier1e083792017-02-08 13:30:04 -08001731
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001732 // Invoke infos
David Srbecky052f8ca2018-04-26 15:42:54 +01001733 stats_.AddBits(
1734 Stats::kByteKindCodeInfoInvokeInfo,
1735 code_info.invoke_infos_.DataBitSize());
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001736
Mathieu Chartier1e083792017-02-08 13:30:04 -08001737 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001738 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
David Srbecky71ec1cc2018-05-18 15:57:25 +01001739 code_info.dex_register_catalog_.DataBitSize());
1740 stats_.AddBits(Stats::kByteKindCodeInfoDexRegisterMask,
1741 code_info.dex_register_masks_.DataBitSize());
1742 stats_.AddBits(Stats::kByteKindCodeInfoDexRegisterMap,
1743 code_info.dex_register_maps_.DataBitSize());
Mathieu Chartier1e083792017-02-08 13:30:04 -08001744
1745 // Inline infos.
David Srbecky052f8ca2018-04-26 15:42:54 +01001746 const BitTable<InlineInfo::kCount>& inline_infos = code_info.inline_infos_;
1747 const size_t num_inline_infos = inline_infos.NumRows();
Mathieu Chartier1e083792017-02-08 13:30:04 -08001748 if (num_inline_infos > 0u) {
1749 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001750 Stats::kByteKindInlineInfoMethodIndexIdx,
David Srbeckyd97e0822018-06-03 12:00:24 +01001751 inline_infos.NumColumnBits(InlineInfo::kMethodInfoIndex) * num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001752 stats_.AddBits(
1753 Stats::kByteKindInlineInfoDexPc,
David Srbecky052f8ca2018-04-26 15:42:54 +01001754 inline_infos.NumColumnBits(InlineInfo::kDexPc) * num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001755 stats_.AddBits(
David Srbecky71ec1cc2018-05-18 15:57:25 +01001756 Stats::kByteKindInlineInfoArtMethod,
1757 inline_infos.NumColumnBits(InlineInfo::kArtMethodHi) * num_inline_infos +
1758 inline_infos.NumColumnBits(InlineInfo::kArtMethodLo) * num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001759 stats_.AddBits(
David Srbecky6de88332018-06-03 12:00:11 +01001760 Stats::kByteKindInlineInfoNumDexRegisters,
1761 inline_infos.NumColumnBits(InlineInfo::kNumberOfDexRegisters) * num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001762 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1763 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001764 }
1765 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001766 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1767 size_t offset = 0;
1768 while (offset < code_size) {
1769 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1770 if (offset == helper.GetOffset()) {
1771 ScopedIndentation indent1(vios);
1772 StackMap stack_map = helper.GetStackMap();
1773 DCHECK(stack_map.IsValid());
1774 stack_map.Dump(vios,
1775 helper.GetCodeInfo(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001776 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001777 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001778 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001779 do {
1780 helper.Next();
1781 // There may be multiple stack maps at a given PC. We display only the first one.
1782 } while (offset == helper.GetOffset());
1783 }
1784 DCHECK_LT(offset, helper.GetOffset());
1785 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001786 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001787 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1788 size_t offset = 0;
1789 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001790 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001792 }
1793 }
1794
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001795 void DumpDataBimgRelRoEntries(std::ostream& os) {
1796 os << ".data.bimg.rel.ro: ";
1797 if (oat_file_.GetBootImageRelocations().empty()) {
1798 os << "empty.\n\n";
1799 return;
1800 }
1801
1802 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1803 Runtime* runtime = Runtime::Current();
1804 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1805 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1806 runtime->GetHeap()->GetBootImageSpaces();
1807 ScopedObjectAccess soa(Thread::Current());
1808 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1809 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1810 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1811 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
1812 uint8_t* object = boot_image_spaces[0]->Begin() + object_offset;
1813 bool found = false;
1814 for (gc::space::ImageSpace* space : boot_image_spaces) {
1815 uint64_t local_offset = object - space->Begin();
1816 if (local_offset < space->GetImageHeader().GetImageSize()) {
1817 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
1818 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(object);
1819 if (o->IsString()) {
1820 os << " String: " << o->AsString()->ToModifiedUtf8();
1821 } else if (o->IsClass()) {
1822 os << " Class: " << o->AsClass()->PrettyDescriptor();
1823 } else {
1824 os << StringPrintf(" 0x%08x %s",
1825 object_offset,
1826 o->GetClass()->PrettyDescriptor().c_str());
1827 }
1828 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
1829 ArtMethod* m = reinterpret_cast<ArtMethod*>(object);
1830 os << " ArtMethod: " << m->PrettyMethod();
1831 } else {
1832 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1833 object_offset,
1834 space->GetImageFilename().c_str());
1835 }
1836 found = true;
1837 break;
1838 }
1839 }
1840 if (!found) {
1841 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1842 }
1843 os << "\n";
1844 }
1845 } else {
1846 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1847 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1848 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1849 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1850 }
1851 }
1852 os << "\n";
1853 }
1854
Vladimir Markof3c52b42017-11-17 17:32:12 +00001855 template <typename NameGetter>
1856 void DumpBssEntries(std::ostream& os,
1857 const char* slot_type,
1858 const IndexBssMapping* mapping,
1859 uint32_t number_of_indexes,
1860 size_t slot_size,
1861 NameGetter name) {
1862 os << ".bss mapping for " << slot_type << ": ";
1863 if (mapping == nullptr) {
1864 os << "empty.\n";
1865 return;
1866 }
1867 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1868 size_t num_valid_indexes = 0u;
1869 for (const IndexBssMappingEntry& entry : *mapping) {
1870 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1871 }
1872 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1873 os << std::hex;
1874 for (const IndexBssMappingEntry& entry : *mapping) {
1875 uint32_t index = entry.GetIndex(index_bits);
1876 uint32_t mask = entry.GetMask(index_bits);
1877 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1878 for (uint32_t n : LowToHighBits(mask)) {
1879 size_t current_index = index - (32u - index_bits) + n;
1880 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1881 bss_offset += slot_size;
1882 }
1883 DCHECK_EQ(bss_offset, entry.bss_offset);
1884 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1885 }
1886 os << std::dec;
1887 }
1888
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001889 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001890 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001891 const OatDumperOptions& options_;
1892 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001893 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001894 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001895 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001896 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001897};
1898
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001899class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001900 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001901 ImageDumper(std::ostream* os,
1902 gc::space::ImageSpace& image_space,
1903 const ImageHeader& image_header,
1904 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001905 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001906 vios_(os),
1907 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001908 image_space_(image_space),
1909 image_header_(image_header),
1910 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001911
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001912 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001914 std::ostream& indent_os = vios_.Stream();
1915
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001917
Jeff Haodcdc85b2015-12-04 14:06:18 -08001918 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1919
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001920 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001921
Mathieu Chartiere401d142015-04-22 13:56:20 -07001922 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1923
1924 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1925 auto section = static_cast<ImageHeader::ImageSections>(i);
1926 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1927 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001928
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001929 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001930
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001931 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001932
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001933 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1934
1935 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1936
1937 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001938
Alex Lighta59dd802014-07-02 16:28:08 -07001939 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1940
Igor Murashkin46774762014-10-22 11:37:02 -07001941 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1942
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001943 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001944 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001945 static_assert(arraysize(image_roots_descriptions_) ==
1946 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001947 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1948 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1950 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001951 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1952 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001953 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001954 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001955 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001956 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001957 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1958 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001959 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001960 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1961 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001962 run++;
1963 } else {
1964 break;
1965 }
1966 }
1967 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001968 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001969 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001970 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001971 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001972 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001973 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001974 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001975 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001976 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001977 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001978 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001979 }
1980 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001981 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001982
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001983 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001984 os << "METHOD ROOTS\n";
1985 static_assert(arraysize(image_methods_descriptions_) ==
1986 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1987 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1988 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1989 const char* description = image_methods_descriptions_[i];
1990 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001991 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001992 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001993 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001994 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001995
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001996 Runtime* const runtime = Runtime::Current();
1997 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001998 std::string image_filename = image_space_.GetImageFilename();
1999 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002000 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002001 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002002 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002003 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07002004 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002005 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
2006 }
2007 if (oat_file == nullptr) {
Nicolas Geoffray30025092018-04-19 14:43:29 +01002008 oat_file = OatFile::Open(/* zip_fd */ -1,
2009 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002010 oat_location,
2011 nullptr,
2012 nullptr,
2013 false,
2014 /*low_4gb*/false,
2015 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002016 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002017 }
2018 if (oat_file == nullptr) {
2019 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
2020 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07002021 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002022 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002023
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002024 stats_.oat_file_bytes = oat_file->Size();
2025
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002026 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002027
Mathieu Chartier02e25112013-08-14 16:14:24 -07002028 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002029 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07002030 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
2031 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07002032 }
2033
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002034 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002035
Jeff Haodcdc85b2015-12-04 14:06:18 -08002036 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002037 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07002038 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002039 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08002040 {
2041 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
2042 heap->FlushAllocStack();
2043 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08002044 // Since FlushAllocStack() above resets the (active) allocation
2045 // stack. Need to revoke the thread-local allocation stacks that
2046 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002047 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07002048 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002049 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002050 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002051 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01002053 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002054 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08002055 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08002056 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002057 ObjPtr<mirror::DexCache> dex_cache =
2058 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002059 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002060 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002061 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002062 }
2063 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002064 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2065 DumpObject(obj);
2066 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002067 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002068 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07002069 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002070 indent_os << "\n";
2071 // TODO: Dump fields.
2072 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08002073 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002074 image_header_.VisitPackedArtMethods(&visitor,
2075 image_space_.Begin(),
2076 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002077 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07002078 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002079 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002080 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07002082 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002083 size_t data_size = image_header_.GetDataSize(); // stored size in file.
2084 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002085 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002086 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002087 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002088 // If the image is compressed, adjust to decompressed size.
2089 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
2090 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
2091 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
2092 }
2093 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07002094 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002095 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002096 const auto& object_section = image_header_.GetObjectsSection();
2097 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002098 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002099 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2100 const auto& intern_section = image_header_.GetInternedStringsSection();
2101 const auto& class_table_section = image_header_.GetClassTableSection();
2102 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002103
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002104 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002105
2106 // Objects are kObjectAlignment-aligned.
2107 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2108 if (object_section.Offset() > header_bytes) {
2109 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2110 }
2111
2112 // Field section is 4-byte aligned.
2113 constexpr size_t kFieldSectionAlignment = 4U;
2114 uint32_t end_objects = object_section.Offset() + object_section.Size();
2115 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2116 stats_.alignment_bytes += field_section.Offset() - end_objects;
2117
2118 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2119 uint32_t end_fields = field_section.Offset() + field_section.Size();
2120 CHECK_ALIGNED(method_section.Offset(), 4);
2121 stats_.alignment_bytes += method_section.Offset() - end_fields;
2122
2123 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2124 uint32_t end_methods = method_section.Offset() + method_section.Size();
2125 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2126 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2127
2128 // Intern table is 8-byte aligned.
2129 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002130 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002131 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2132
2133 // Add space between intern table and class table.
2134 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2135 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2136
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002137 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2138 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002139 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002140 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002141
Mathieu Chartiere401d142015-04-22 13:56:20 -07002142 stats_.bitmap_bytes += bitmap_section.Size();
2143 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002144 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002145 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002146 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002147 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002148 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002150
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002152
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002153 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002154 }
2155
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002156 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002157 class DumpArtMethodVisitor : public ArtMethodVisitor {
2158 public:
2159 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2160
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002161 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002162 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002163 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002164 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002165 indent_os << "\n";
2166 }
2167
2168 private:
2169 ImageDumper* const image_dumper_;
2170 };
2171
Mathieu Chartier3398c782016-09-30 10:27:43 -07002172 static void PrettyObjectValue(std::ostream& os,
2173 ObjPtr<mirror::Class> type,
2174 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002175 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002176 CHECK(type != nullptr);
2177 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002178 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002179 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002180 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002181 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002182 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002183 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002184 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002185 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002186 } else {
David Sehr709b0702016-10-13 09:12:37 -07002187 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002188 }
2189 }
2190
Mathieu Chartier3398c782016-09-30 10:27:43 -07002191 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002192 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002193 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002194 switch (field->GetTypeAsPrimitiveType()) {
2195 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002196 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002197 break;
2198 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002199 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002200 break;
2201 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002202 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002203 break;
2204 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002205 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002206 break;
2207 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002208 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002209 break;
2210 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002211 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002212 break;
2213 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002214 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002215 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002216 break;
2217 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002218 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002219 break;
2220 case Primitive::kPrimNot: {
2221 // Get the value, don't compute the type unless it is non-null as we don't want
2222 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002223 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002224 if (value == nullptr) {
2225 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002226 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002227 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002228 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002229 if (field_type != nullptr) {
2230 PrettyObjectValue(os, field_type, value);
2231 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002232 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002233 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002234 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2235 }
Ian Rogers50239c72013-06-17 14:53:22 -07002236 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002237 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002238 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002239 default:
2240 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2241 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002242 }
2243 }
2244
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002245 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002246 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002247 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002248 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002250 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002251 for (ArtField& field : klass->GetIFields()) {
2252 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002253 }
2254 }
2255
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002256 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002257 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002258 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002259
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002260 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002261 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002262 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002263 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002264 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002265 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002266 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002267 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002268 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002269 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002270 }
2271
Mathieu Chartiere401d142015-04-22 13:56:20 -07002272 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002273 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002274 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2275 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002276 return 0;
2277 }
2278 return oat_code_begin[-1];
2279 }
2280
Mathieu Chartiere401d142015-04-22 13:56:20 -07002281 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002282 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002283 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002284 if (oat_code_begin == nullptr) {
2285 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002286 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002287 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002288 }
2289
Andreas Gampe0c183382017-07-13 22:26:24 -07002290 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002291 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002292 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002293 return;
2294 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002295
2296 size_t object_bytes = obj->SizeOf();
2297 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002298 stats_.object_bytes += object_bytes;
2299 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002300
Andreas Gampe0c183382017-07-13 22:26:24 -07002301 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002302
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002303 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002304 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002305 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002307 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002308 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002309 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2310 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002312 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002314 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002315 } else {
David Sehr709b0702016-10-13 09:12:37 -07002316 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002317 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002318 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002319 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002320 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002321 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002322 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2323 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002324 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002325 size_t run = 0;
2326 for (int32_t j = i + 1; j < length; j++) {
2327 if (value == obj_array->Get(j)) {
2328 run++;
2329 } else {
2330 break;
2331 }
2332 }
2333 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002334 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002335 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002336 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002337 i = i + run;
2338 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002339 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002340 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002341 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002342 }
2343 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002344 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002345
Vladimir Marko305c38b2018-02-14 11:50:07 +00002346 if (kBitstringSubtypeCheckEnabled) {
2347 os << "SUBTYPE_CHECK_BITS: ";
2348 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2349 os << "\n";
2350 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002351
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002352 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002353 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002354 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002355 for (ArtField& field : klass->GetSFields()) {
2356 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002357 }
2358 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002360 auto it = dex_caches_.find(obj);
2361 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002362 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002363 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002364 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002365 size_t num_methods = dex_cache->NumResolvedMethods();
2366 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002367 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002368 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002369 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002370 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002371 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2372 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002373 size_t run = 0;
2374 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002375 j != length &&
2376 elem == mirror::DexCache::GetNativePairPtrSize(
2377 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002378 ++j) {
2379 ++run;
2380 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002381 if (run == 0) {
2382 os << StringPrintf("%zd: ", i);
2383 } else {
2384 os << StringPrintf("%zd to %zd: ", i, i + run);
2385 i = i + run;
2386 }
2387 std::string msg;
2388 if (elem == nullptr) {
2389 msg = "null";
2390 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002391 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002392 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002393 } else {
2394 msg = "<not in method section>";
2395 }
2396 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002397 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002398 }
2399 size_t num_fields = dex_cache->NumResolvedFields();
2400 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002401 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002402 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002403 auto* resolved_fields = dex_cache->GetResolvedFields();
2404 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002405 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002406 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002407 size_t run = 0;
2408 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002409 j != length &&
2410 elem == mirror::DexCache::GetNativePairPtrSize(
2411 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002412 ++j) {
2413 ++run;
2414 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002415 if (run == 0) {
2416 os << StringPrintf("%zd: ", i);
2417 } else {
2418 os << StringPrintf("%zd to %zd: ", i, i + run);
2419 i = i + run;
2420 }
2421 std::string msg;
2422 if (elem == nullptr) {
2423 msg = "null";
2424 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002425 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002426 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002427 } else {
2428 msg = "<not in field section>";
2429 }
2430 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002431 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002432 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002433 size_t num_types = dex_cache->NumResolvedTypes();
2434 if (num_types != 0u) {
2435 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002436 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002437 auto* resolved_types = dex_cache->GetResolvedTypes();
2438 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002439 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002440 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002441 for (size_t j = i + 1; j != num_types; ++j) {
2442 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2443 if (pair.index != other_pair.index ||
2444 pair.object.Read() != other_pair.object.Read()) {
2445 break;
2446 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002447 ++run;
2448 }
2449 if (run == 0) {
2450 os << StringPrintf("%zd: ", i);
2451 } else {
2452 os << StringPrintf("%zd to %zd: ", i, i + run);
2453 i = i + run;
2454 }
2455 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002456 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002457 if (elem == nullptr) {
2458 msg = "null";
2459 } else {
David Sehr709b0702016-10-13 09:12:37 -07002460 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002461 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002462 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002463 }
2464 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002465 }
2466 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002467 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002468 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002469 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002470
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002471 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002472 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002473 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002474 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2475 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002476 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002477 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2478 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002479 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002480 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002481 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2482 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002483 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002484 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002485 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002486 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2487 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002488 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002489 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002490 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002491 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002492 } else if (method->IsRuntimeMethod()) {
2493 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2494 if (table != nullptr) {
2495 indent_os << "IMT conflict table " << table << " method: ";
2496 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002497 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2498 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002499 }
2500 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002501 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002502 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002503 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002504 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002505
2506 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002507 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002508 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002509 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002510 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002511 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002512 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002513 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002514 }
2515
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002516 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2517 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002518 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002519 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002520 if (method->IsConstructor()) {
2521 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002522 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002523 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002524 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002525 }
2526 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002527 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002528 }
2529 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002530 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002531
Igor Murashkin7617abd2015-07-10 18:27:47 -07002532 uint32_t method_access_flags = method->GetAccessFlags();
2533
Mathieu Chartiere401d142015-04-22 13:56:20 -07002534 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002535 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2536 dex_instruction_bytes,
2537 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002538 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002539
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002540 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002541 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002542
2543 double expansion =
2544 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002545 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002546 }
2547 }
2548
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002549 std::set<const void*> already_seen_;
2550 // Compute the size of the given data within the oat file and whether this is the first time
2551 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002552 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002553 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002554 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002555 already_seen_.insert(oat_data);
2556 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002557 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002558 }
2559 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002560 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002561
2562 public:
2563 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002564 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002565 size_t file_bytes;
2566
2567 size_t header_bytes;
2568 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002569 size_t art_field_bytes;
2570 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002571 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002572 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002573 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002574 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002575 size_t alignment_bytes;
2576
2577 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002578 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002579 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002580 size_t class_initializer_code_bytes;
2581 size_t large_initializer_code_bytes;
2582 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002583
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002584 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002585
2586 size_t dex_instruction_bytes;
2587
Mathieu Chartiere401d142015-04-22 13:56:20 -07002588 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002589 std::vector<size_t> method_outlier_size;
2590 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002591 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002592
Roland Levillain3887c462015-08-12 18:15:42 +01002593 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002594 : oat_file_bytes(0),
2595 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002596 header_bytes(0),
2597 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002598 art_field_bytes(0),
2599 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002600 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002601 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002602 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002603 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002604 alignment_bytes(0),
2605 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002606 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002607 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002608 class_initializer_code_bytes(0),
2609 large_initializer_code_bytes(0),
2610 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002611 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002612 dex_instruction_bytes(0) {}
2613
Elliott Hughesa0e18062012-04-13 15:59:59 -07002614 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002615 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002616 size_t bytes;
2617 size_t count;
2618 };
2619 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2620 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002621
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002622 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002623 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2624 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002625 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002626 it->second.count += 1;
2627 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002628 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002629 }
2630 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002631
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002632 double PercentOfOatBytes(size_t size) {
2633 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2634 }
2635
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002636 double PercentOfFileBytes(size_t size) {
2637 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2638 }
2639
2640 double PercentOfObjectBytes(size_t size) {
2641 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2642 }
2643
Mathieu Chartiere401d142015-04-22 13:56:20 -07002644 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002645 method_outlier_size.push_back(total_size);
2646 method_outlier_expansion.push_back(expansion);
2647 method_outlier.push_back(method);
2648 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002649
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002650 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002651 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002652 size_t sum_of_sizes = 0;
2653 size_t sum_of_sizes_squared = 0;
2654 size_t sum_of_expansion = 0;
2655 size_t sum_of_expansion_squared = 0;
2656 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002657 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002658 return;
2659 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002660 for (size_t i = 0; i < n; i++) {
2661 size_t cur_size = method_outlier_size[i];
2662 sum_of_sizes += cur_size;
2663 sum_of_sizes_squared += cur_size * cur_size;
2664 double cur_expansion = method_outlier_expansion[i];
2665 sum_of_expansion += cur_expansion;
2666 sum_of_expansion_squared += cur_expansion * cur_expansion;
2667 }
2668 size_t size_mean = sum_of_sizes / n;
2669 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2670 double expansion_mean = sum_of_expansion / n;
2671 double expansion_variance =
2672 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2673
2674 // Dump methods whose size is a certain number of standard deviations from the mean
2675 size_t dumped_values = 0;
2676 size_t skipped_values = 0;
2677 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2678 size_t cur_size_variance = i * i * size_variance;
2679 bool first = true;
2680 for (size_t j = 0; j < n; j++) {
2681 size_t cur_size = method_outlier_size[j];
2682 if (cur_size > size_mean) {
2683 size_t cur_var = cur_size - size_mean;
2684 cur_var = cur_var * cur_var;
2685 if (cur_var > cur_size_variance) {
2686 if (dumped_values > 20) {
2687 if (i == 1) {
2688 skipped_values++;
2689 } else {
2690 i = 2; // jump to counting for 1 standard deviation
2691 break;
2692 }
2693 } else {
2694 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002695 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002696 first = false;
2697 }
David Sehr709b0702016-10-13 09:12:37 -07002698 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002699 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002700 method_outlier_size[j] = 0; // don't consider this method again
2701 dumped_values++;
2702 }
2703 }
2704 }
2705 }
2706 }
2707 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002708 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002709 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002710 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002711 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002712
2713 // Dump methods whose expansion is a certain number of standard deviations from the mean
2714 dumped_values = 0;
2715 skipped_values = 0;
2716 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2717 double cur_expansion_variance = i * i * expansion_variance;
2718 bool first = true;
2719 for (size_t j = 0; j < n; j++) {
2720 double cur_expansion = method_outlier_expansion[j];
2721 if (cur_expansion > expansion_mean) {
2722 size_t cur_var = cur_expansion - expansion_mean;
2723 cur_var = cur_var * cur_var;
2724 if (cur_var > cur_expansion_variance) {
2725 if (dumped_values > 20) {
2726 if (i == 1) {
2727 skipped_values++;
2728 } else {
2729 i = 2; // jump to counting for 1 standard deviation
2730 break;
2731 }
2732 } else {
2733 if (first) {
2734 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002735 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002736 first = false;
2737 }
David Sehr709b0702016-10-13 09:12:37 -07002738 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002739 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002740 method_outlier_expansion[j] = 0.0; // don't consider this method again
2741 dumped_values++;
2742 }
2743 }
2744 }
2745 }
2746 }
2747 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002748 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002749 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002750 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002751 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002752 }
2753
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002754 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002755 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002756 {
2757 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2758 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002759 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2760 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2761 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2762 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2763 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2764 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002765 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002766 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2767 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002768 header_bytes, PercentOfFileBytes(header_bytes),
2769 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002770 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2771 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002772 dex_cache_arrays_bytes,
2773 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002774 interned_strings_bytes,
2775 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002776 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002777 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002778 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2779 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002780 CHECK_EQ(file_bytes,
2781 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2782 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2783 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002784 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002785
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002786 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002787 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002788 for (const auto& sizes_and_count : sizes_and_counts) {
2789 const std::string& descriptor(sizes_and_count.first);
2790 double average = static_cast<double>(sizes_and_count.second.bytes) /
2791 static_cast<double>(sizes_and_count.second.count);
2792 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002793 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002794 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002795 descriptor.c_str(), sizes_and_count.second.bytes,
2796 sizes_and_count.second.count, average, percent);
2797 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002798 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002799 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002800 CHECK_EQ(object_bytes, object_bytes_total);
2801
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002802 os << StringPrintf("oat_file_bytes = %8zd\n"
2803 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002804 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2805 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2806 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2807 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002808 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002809 managed_code_bytes,
2810 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002811 native_to_managed_code_bytes,
2812 PercentOfOatBytes(native_to_managed_code_bytes),
2813 class_initializer_code_bytes,
2814 PercentOfOatBytes(class_initializer_code_bytes),
2815 large_initializer_code_bytes,
2816 PercentOfOatBytes(large_initializer_code_bytes),
2817 large_method_code_bytes,
2818 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002819 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002820 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002821 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002822 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2823 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002824 }
2825
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002826 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002827 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002828 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002829
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002830 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2831 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002832 static_cast<double>(managed_code_bytes) /
2833 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002834 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002835 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002836 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002837
2838 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002839 }
2840 } stats_;
2841
2842 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002843 enum {
2844 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2845 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2846 kLargeConstructorDexBytes = 4000,
2847 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2848 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2849 kLargeMethodDexBytes = 16000
2850 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002851
2852 // For performance, use the *os_ directly for anything that doesn't need indentation
2853 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002854 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002855 VariableIndentationOutputStream vios_;
2856 ScopedIndentation indent1_;
2857
Ian Rogers1d54e732013-05-02 21:10:01 -07002858 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002859 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002860 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002861 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002862 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002863
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002864 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002865};
2866
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002867static int DumpImage(gc::space::ImageSpace* image_space,
2868 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002869 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002870 const ImageHeader& image_header = image_space->GetImageHeader();
2871 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002872 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002873 return EXIT_FAILURE;
2874 }
2875 ImageDumper image_dumper(os, *image_space, image_header, options);
2876 if (!image_dumper.Dump()) {
2877 return EXIT_FAILURE;
2878 }
2879 return EXIT_SUCCESS;
2880}
2881
2882static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002883 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002884 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002885 options->class_loader_ = &null_class_loader;
2886
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002887 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002888 if (options->app_image_ != nullptr) {
2889 if (options->app_oat_ == nullptr) {
2890 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002891 return EXIT_FAILURE;
2892 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002893 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2894 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2895 // pointers into 32 bit pointer sized ArtMethods.
2896 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01002897 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
2898 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002899 options->app_oat_,
2900 nullptr,
2901 nullptr,
2902 false,
2903 /*low_4gb*/true,
2904 nullptr,
2905 &error_msg));
2906 if (oat_file == nullptr) {
2907 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002908 return EXIT_FAILURE;
2909 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002910 std::unique_ptr<gc::space::ImageSpace> space(
2911 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2912 if (space == nullptr) {
2913 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2914 << error_msg;
2915 }
2916 // Open dex files for the image.
2917 std::vector<std::unique_ptr<const DexFile>> dex_files;
2918 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2919 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2920 << error_msg;
2921 }
2922 // Dump the actual image.
2923 int result = DumpImage(space.get(), options, os);
2924 if (result != EXIT_SUCCESS) {
2925 return result;
2926 }
2927 // Fall through to dump the boot images.
2928 }
2929
2930 gc::Heap* heap = runtime->GetHeap();
2931 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2932 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2933 int result = DumpImage(image_space, options, os);
2934 if (result != EXIT_SUCCESS) {
2935 return result;
2936 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002937 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002938 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002939}
2940
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002941static jobject InstallOatFile(Runtime* runtime,
2942 std::unique_ptr<OatFile> oat_file,
2943 std::vector<const DexFile*>* class_path)
2944 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002945 Thread* self = Thread::Current();
2946 CHECK(self != nullptr);
2947 // Need well-known-classes.
2948 WellKnownClasses::Init(self->GetJniEnv());
2949
Vladimir Marko421087b2018-02-27 11:00:17 +00002950 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002951 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002952 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002953 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2954 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002955 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002956 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002957 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002958 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002959 }
2960
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002961 // Need a class loader. Fake that we're a compiler.
2962 // Note: this will run initializers through the unstarted runtime, so make sure it's
2963 // initialized.
2964 interpreter::UnstartedRuntime::Initialize();
2965
2966 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2967
Vladimir Marko421087b2018-02-27 11:00:17 +00002968 // Need to register dex files to get a working dex cache.
2969 for (const DexFile* dex_file : *class_path) {
2970 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2971 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2972 CHECK(dex_cache != nullptr);
2973 }
2974
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002975 return class_loader;
2976}
2977
2978static int DumpOatWithRuntime(Runtime* runtime,
2979 std::unique_ptr<OatFile> oat_file,
2980 OatDumperOptions* options,
2981 std::ostream* os) {
2982 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2983 ScopedObjectAccess soa(Thread::Current());
2984
2985 OatFile* oat_file_ptr = oat_file.get();
2986 std::vector<const DexFile*> class_path;
2987 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002988
2989 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002990 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002991 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002992 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002993 options->class_loader_ = &loader_handle;
2994
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002995 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002996 bool success = oat_dumper.Dump(*os);
2997 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2998}
2999
3000static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003001 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003002 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08003003 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003004 options->class_loader_ = &null_class_loader;
3005
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003006 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003007 bool success = oat_dumper.Dump(*os);
3008 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3009}
3010
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003011static int DumpOat(Runtime* runtime,
3012 const char* oat_filename,
3013 const char* dex_filename,
3014 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07003015 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003016 if (dex_filename == nullptr) {
3017 LOG(WARNING) << "No dex filename provided, "
3018 << "oatdump might fail if the oat file does not contain the dex code.";
3019 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003020 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003021 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3022 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003023 oat_filename,
3024 nullptr,
3025 nullptr,
3026 false,
3027 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003028 dex_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003029 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003030 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003031 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003032 return EXIT_FAILURE;
3033 }
3034
3035 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003036 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003037 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003038 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003039 }
3040}
3041
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003042static int SymbolizeOat(const char* oat_filename,
3043 const char* dex_filename,
3044 std::string& output_name,
3045 bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003046 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003047 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3048 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07003049 oat_filename,
3050 nullptr,
3051 nullptr,
3052 false,
3053 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003054 dex_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07003055 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003056 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003057 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003058 return EXIT_FAILURE;
3059 }
3060
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003061 bool result;
3062 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
3063 // files for 64-bit code in the past.
3064 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003065 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003066 result = oat_symbolizer.Symbolize();
3067 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003068 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003069 result = oat_symbolizer.Symbolize();
3070 }
3071 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003072 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003073 return EXIT_FAILURE;
3074 }
3075
3076 return EXIT_SUCCESS;
3077}
3078
Andreas Gampe9fded872016-09-25 16:08:35 -07003079class IMTDumper {
3080 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003081 static bool Dump(Runtime* runtime,
3082 const std::string& imt_file,
3083 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003084 const char* oat_filename,
3085 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003086 Thread* self = Thread::Current();
3087
3088 ScopedObjectAccess soa(self);
3089 StackHandleScope<1> scope(self);
3090 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
3091 std::vector<const DexFile*> class_path;
3092
3093 if (oat_filename != nullptr) {
3094 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003095 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3096 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003097 oat_filename,
3098 nullptr,
3099 nullptr,
3100 false,
3101 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003102 dex_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003103 &error_msg));
3104 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003105 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003106 return false;
3107 }
3108
3109 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
3110 InstallOatFile(runtime, std::move(oat_file), &class_path)));
3111 } else {
3112 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
3113 class_path = runtime->GetClassLinker()->GetBootClassPath();
3114 }
3115
3116 if (!imt_file.empty()) {
3117 return DumpImt(runtime, imt_file, class_loader);
3118 }
3119
3120 if (dump_imt_stats) {
3121 return DumpImtStats(runtime, class_path, class_loader);
3122 }
3123
3124 LOG(FATAL) << "Should not reach here";
3125 UNREACHABLE();
3126 }
3127
3128 private:
3129 static bool DumpImt(Runtime* runtime,
3130 const std::string& imt_file,
3131 Handle<mirror::ClassLoader> h_class_loader)
3132 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003133 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3134 std::unordered_set<std::string> prepared;
3135
3136 for (const std::string& line : lines) {
3137 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3138 // or a class descriptor and an interface method, in which case we will lookup the method,
3139 // determine its IMT slot, and check the class' IMT.
3140 size_t first_space = line.find(' ');
3141 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003142 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003143 } else {
3144 DumpIMTForMethod(runtime,
3145 line.substr(0, first_space),
3146 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003147 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003148 &prepared);
3149 }
3150 std::cerr << std::endl;
3151 }
3152
3153 return true;
3154 }
3155
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003156 static bool DumpImtStats(Runtime* runtime,
3157 const std::vector<const DexFile*>& dex_files,
3158 Handle<mirror::ClassLoader> h_class_loader)
3159 REQUIRES_SHARED(Locks::mutator_lock_) {
3160 size_t without_imt = 0;
3161 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003162 std::map<size_t, size_t> histogram;
3163
3164 ClassLinker* class_linker = runtime->GetClassLinker();
3165 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3166 std::unordered_set<std::string> prepared;
3167
3168 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003169 StackHandleScope<1> scope(self);
3170 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3171
3172 for (const DexFile* dex_file : dex_files) {
3173 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003174 class_def_index != dex_file->NumClassDefs();
3175 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003176 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3177 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003178 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003179 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003180 std::cerr << "Warning: could not load " << descriptor << std::endl;
3181 continue;
3182 }
3183
3184 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003185 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003186 continue;
3187 }
3188
3189 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3190 if (im_table == nullptr) {
3191 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003192 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003193 continue;
3194 }
3195
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003196 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003197 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3198 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3199 if (ptr->IsRuntimeMethod()) {
3200 if (ptr->IsImtUnimplementedMethod()) {
3201 histogram[0]++;
3202 } else {
3203 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3204 histogram[current_table->NumEntries(pointer_size)]++;
3205 }
3206 } else {
3207 histogram[1]++;
3208 }
3209 }
3210 }
3211 }
3212
3213 std::cerr << "IMT stats:"
3214 << std::endl << std::endl;
3215
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003216 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003217 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003218 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003219 << std::endl << std::endl;
3220
3221 double sum_one = 0;
3222 size_t count_one = 0;
3223
3224 std::cerr << " " << "IMT histogram" << std::endl;
3225 for (auto& bucket : histogram) {
3226 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3227 if (bucket.first > 0) {
3228 sum_one += bucket.second * bucket.first;
3229 count_one += bucket.second;
3230 }
3231 }
3232
3233 double count_zero = count_one + histogram[0];
3234 std::cerr << " Stats:" << std::endl;
3235 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3236 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3237
3238 return true;
3239 }
3240
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003241 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003242 static bool HasNoIMT(Runtime* runtime,
3243 Handle<mirror::Class> klass,
3244 const PointerSize pointer_size,
3245 std::unordered_set<std::string>* prepared)
3246 REQUIRES_SHARED(Locks::mutator_lock_) {
3247 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3248 return true;
3249 }
3250
3251 if (klass->GetImt(pointer_size) == nullptr) {
3252 PrepareClass(runtime, klass, prepared);
3253 }
3254
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003255 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003256 DCHECK(object_class->IsObjectClass());
3257
3258 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3259
Mathieu Chartier6beced42016-11-15 15:51:31 -08003260 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003261 DCHECK(result);
3262 }
3263
3264 return result;
3265 }
3266
3267 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3268 REQUIRES_SHARED(Locks::mutator_lock_) {
3269 if (table == nullptr) {
3270 std::cerr << " <No IMT?>" << std::endl;
3271 return;
3272 }
3273 size_t table_index = 0;
3274 for (;;) {
3275 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3276 if (ptr == nullptr) {
3277 return;
3278 }
3279 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003280 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003281 }
3282 }
3283
3284 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3285 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003286 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003287 const std::string& class_name,
3288 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003289 /*out*/ ObjPtr<mirror::Class>* klass_out,
3290 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003291 REQUIRES_SHARED(Locks::mutator_lock_) {
3292 if (class_name.empty()) {
3293 return nullptr;
3294 }
3295
3296 std::string descriptor;
3297 if (class_name[0] == 'L') {
3298 descriptor = class_name;
3299 } else {
3300 descriptor = DotToDescriptor(class_name.c_str());
3301 }
3302
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003303 ObjPtr<mirror::Class> klass =
3304 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003305
3306 if (klass == nullptr) {
3307 self->ClearException();
3308 std::cerr << "Did not find " << class_name << std::endl;
3309 *klass_out = nullptr;
3310 return nullptr;
3311 }
3312
3313 StackHandleScope<1> scope(Thread::Current());
3314 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3315
3316 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3317 *klass_out = h_klass.Get();
3318 return ret;
3319 }
3320
3321 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3322 Handle<mirror::Class> h_klass,
3323 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003324 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003325 REQUIRES_SHARED(Locks::mutator_lock_) {
3326 PrepareClass(runtime, h_klass, prepared);
3327 return h_klass->GetImt(pointer_size);
3328 }
3329
3330 static void DumpIMTForClass(Runtime* runtime,
3331 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003332 Handle<mirror::ClassLoader> h_loader,
3333 std::unordered_set<std::string>* prepared)
3334 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003335 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003336 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003337 ImTable* imt = PrepareAndGetImTable(runtime,
3338 Thread::Current(),
3339 h_loader,
3340 class_name,
3341 pointer_size,
3342 &klass,
3343 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003344 if (imt == nullptr) {
3345 return;
3346 }
3347
3348 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3349 for (size_t index = 0; index < ImTable::kSize; ++index) {
3350 std::cerr << " " << index << ":" << std::endl;
3351 ArtMethod* ptr = imt->Get(index, pointer_size);
3352 if (ptr->IsRuntimeMethod()) {
3353 if (ptr->IsImtUnimplementedMethod()) {
3354 std::cerr << " <empty>" << std::endl;
3355 } else {
3356 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3357 PrintTable(current_table, pointer_size);
3358 }
3359 } else {
David Sehr709b0702016-10-13 09:12:37 -07003360 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003361 }
3362 }
3363
3364 std::cerr << " Interfaces:" << std::endl;
3365 // Run through iftable, find methods that slot here, see if they fit.
3366 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003367 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3368 mirror::Class* iface = if_table->GetInterface(i);
3369 std::string iface_name;
3370 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003371
Mathieu Chartier6beced42016-11-15 15:51:31 -08003372 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3373 uint32_t class_hash, name_hash, signature_hash;
3374 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3375 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3376 std::cerr << " " << iface_method.PrettyMethod(true)
3377 << " slot=" << imt_slot
3378 << std::hex
3379 << " class_hash=0x" << class_hash
3380 << " name_hash=0x" << name_hash
3381 << " signature_hash=0x" << signature_hash
3382 << std::dec
3383 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003384 }
3385 }
3386 }
3387
3388 static void DumpIMTForMethod(Runtime* runtime,
3389 const std::string& class_name,
3390 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003391 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003392 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003393 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003394 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003395 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003396 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003397 Thread::Current(),
3398 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003399 class_name,
3400 pointer_size,
3401 &klass,
3402 prepared);
3403 if (imt == nullptr) {
3404 return;
3405 }
3406
3407 std::cerr << class_name << " <" << method << ">" << std::endl;
3408 for (size_t index = 0; index < ImTable::kSize; ++index) {
3409 ArtMethod* ptr = imt->Get(index, pointer_size);
3410 if (ptr->IsRuntimeMethod()) {
3411 if (ptr->IsImtUnimplementedMethod()) {
3412 continue;
3413 }
3414
3415 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3416 if (current_table == nullptr) {
3417 continue;
3418 }
3419
3420 size_t table_index = 0;
3421 for (;;) {
3422 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3423 if (ptr2 == nullptr) {
3424 break;
3425 }
3426 table_index++;
3427
David Sehr709b0702016-10-13 09:12:37 -07003428 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003429 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003430 std::cerr << " Slot "
3431 << index
3432 << " ("
3433 << current_table->NumEntries(pointer_size)
3434 << ")"
3435 << std::endl;
3436 PrintTable(current_table, pointer_size);
3437 return;
3438 }
3439 }
3440 } else {
David Sehr709b0702016-10-13 09:12:37 -07003441 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003442 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003443 std::cerr << " Slot " << index << " (1)" << std::endl;
3444 std::cerr << " " << p_name << std::endl;
3445 } else {
3446 // Run through iftable, find methods that slot here, see if they fit.
3447 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003448 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3449 mirror::Class* iface = if_table->GetInterface(i);
3450 size_t num_methods = iface->NumDeclaredVirtualMethods();
3451 if (num_methods > 0) {
3452 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3453 if (ImTable::GetImtIndex(&iface_method) == index) {
3454 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003455 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003456 std::cerr << " Slot " << index << " (1)" << std::endl;
3457 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003458 }
3459 }
3460 }
3461 }
3462 }
3463 }
3464 }
3465 }
3466 }
3467
3468 // Read lines from the given stream, dropping comments and empty lines
3469 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3470 std::vector<std::string> output;
3471 while (in_stream.good()) {
3472 std::string dot;
3473 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003474 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003475 continue;
3476 }
3477 output.push_back(dot);
3478 }
3479 return output;
3480 }
3481
3482 // Read lines from the given file, dropping comments and empty lines.
3483 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3484 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3485 if (input_file.get() == nullptr) {
3486 LOG(ERROR) << "Failed to open input file " << input_filename;
3487 return std::vector<std::string>();
3488 }
3489 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3490 input_file->close();
3491 return result;
3492 }
3493
3494 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3495 // and note in the given set that the work was done.
3496 static void PrepareClass(Runtime* runtime,
3497 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003498 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003499 REQUIRES_SHARED(Locks::mutator_lock_) {
3500 if (!h_klass->ShouldHaveImt()) {
3501 return;
3502 }
3503
3504 std::string name;
3505 name = h_klass->GetDescriptor(&name);
3506
3507 if (done->find(name) != done->end()) {
3508 return;
3509 }
3510 done->insert(name);
3511
3512 if (h_klass->HasSuperClass()) {
3513 StackHandleScope<1> h(Thread::Current());
3514 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3515 }
3516
3517 if (!h_klass->IsTemp()) {
3518 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3519 }
3520 }
3521};
3522
Igor Murashkin37743352014-11-13 14:38:00 -08003523struct OatdumpArgs : public CmdlineArgs {
3524 protected:
3525 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003526
Igor Murashkin37743352014-11-13 14:38:00 -08003527 virtual ParseStatus ParseCustom(const StringPiece& option,
3528 std::string* error_msg) OVERRIDE {
3529 {
3530 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3531 if (base_parse != kParseUnknownArgument) {
3532 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003533 }
3534 }
3535
Igor Murashkin37743352014-11-13 14:38:00 -08003536 if (option.starts_with("--oat-file=")) {
3537 oat_filename_ = option.substr(strlen("--oat-file=")).data();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003538 } else if (option.starts_with("--dex-file=")) {
3539 dex_filename_ = option.substr(strlen("--dex-file=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003540 } else if (option.starts_with("--image=")) {
3541 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003542 } else if (option == "--no-dump:vmap") {
3543 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003544 } else if (option =="--dump:code_info_stack_maps") {
3545 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003546 } else if (option == "--no-disassemble") {
3547 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003548 } else if (option =="--header-only") {
3549 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003550 } else if (option.starts_with("--symbolize=")) {
3551 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3552 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003553 } else if (option.starts_with("--only-keep-debug")) {
3554 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003555 } else if (option.starts_with("--class-filter=")) {
3556 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003557 } else if (option.starts_with("--method-filter=")) {
3558 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003559 } else if (option.starts_with("--list-classes")) {
3560 list_classes_ = true;
3561 } else if (option.starts_with("--list-methods")) {
3562 list_methods_ = true;
3563 } else if (option.starts_with("--export-dex-to=")) {
3564 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3565 } else if (option.starts_with("--addr2instr=")) {
3566 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3567 *error_msg = "Address conversion failed";
3568 return kParseError;
3569 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003570 } else if (option.starts_with("--app-image=")) {
3571 app_image_ = option.substr(strlen("--app-image=")).data();
3572 } else if (option.starts_with("--app-oat=")) {
3573 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003574 } else if (option.starts_with("--dump-imt=")) {
3575 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3576 } else if (option == "--dump-imt-stats") {
3577 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003578 } else {
3579 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003580 }
3581
Igor Murashkin37743352014-11-13 14:38:00 -08003582 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003583 }
3584
Igor Murashkin37743352014-11-13 14:38:00 -08003585 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3586 // Infer boot image location from the image location if possible.
3587 if (boot_image_location_ == nullptr) {
3588 boot_image_location_ = image_location_;
3589 }
3590
3591 // Perform the parent checks.
3592 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3593 if (parent_checks != kParseOk) {
3594 return parent_checks;
3595 }
3596
3597 // Perform our own checks.
3598 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3599 *error_msg = "Either --image or --oat-file must be specified";
3600 return kParseError;
3601 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3602 *error_msg = "Either --image or --oat-file must be specified but not both";
3603 return kParseError;
3604 }
3605
3606 return kParseOk;
3607 }
3608
3609 virtual std::string GetUsage() const {
3610 std::string usage;
3611
3612 usage +=
3613 "Usage: oatdump [options] ...\n"
3614 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3615 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3616 "\n"
3617 // Either oat-file or image is required.
3618 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3619 " Example: --oat-file=/system/framework/boot.oat\n"
3620 "\n"
3621 " --image=<file.art>: specifies an input image location.\n"
3622 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003623 "\n"
3624 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003625 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003626 " Example: --app-image=app.art\n"
3627 "\n"
3628 " --app-oat=<file.odex>: specifies an input app oat.\n"
3629 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003630 "\n";
3631
3632 usage += Base::GetUsage();
3633
3634 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003635 " --no-dump:vmap may be used to disable vmap dumping.\n"
3636 " Example: --no-dump:vmap\n"
3637 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003638 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3639 " Example: --dump:code_info_stack_maps\n"
3640 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003641 " --no-disassemble may be used to disable disassembly.\n"
3642 " Example: --no-disassemble\n"
3643 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003644 " --header-only may be used to print only the oat header.\n"
3645 " Example: --header-only\n"
3646 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003647 " --list-classes may be used to list target file classes (can be used with filters).\n"
3648 " Example: --list-classes\n"
3649 " Example: --list-classes --class-filter=com.example.foo\n"
3650 "\n"
3651 " --list-methods may be used to list target file methods (can be used with filters).\n"
3652 " Example: --list-methods\n"
3653 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3654 "\n"
3655 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3656 " Example: --symbolize=/system/framework/boot.oat\n"
3657 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003658 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3659 " .rodata and .text sections are omitted in the output file to save space.\n"
3660 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3661 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003662 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3663 " Example: --class-filter=com.example.foo\n"
3664 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003665 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3666 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003667 "\n"
3668 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3669 " Example: --export-dex-to=/data/local/tmp\n"
3670 "\n"
3671 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3672 " address (e.g. PC from crash dump)\n"
3673 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003674 "\n"
3675 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3676 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003677 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003678 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003679 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003680 " Example: --dump-imt=imt.txt\n"
3681 "\n"
3682 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3683 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003684 "\n";
3685
3686 return usage;
3687 }
3688
3689 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003690 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003691 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003692 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003693 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003694 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003695 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003696 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003697 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003698 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003699 bool disassemble_code_ = true;
3700 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003701 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003702 bool list_classes_ = false;
3703 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003704 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003705 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003706 uint32_t addr2instr_ = 0;
3707 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003708 const char* app_image_ = nullptr;
3709 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003710};
3711
Igor Murashkin37743352014-11-13 14:38:00 -08003712struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3713 virtual bool NeedsRuntime() OVERRIDE {
3714 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003715
Igor Murashkin37743352014-11-13 14:38:00 -08003716 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3717 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3718
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003719 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003720 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003721 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003722 args_->disassemble_code_,
3723 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003724 args_->class_filter_,
3725 args_->method_filter_,
3726 args_->list_classes_,
3727 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003728 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003729 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003730 args_->app_image_,
3731 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003732 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003733
Andreas Gampe9fded872016-09-25 16:08:35 -07003734 return (args_->boot_image_location_ != nullptr ||
3735 args_->image_location_ != nullptr ||
3736 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003737 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003738 }
3739
Igor Murashkin37743352014-11-13 14:38:00 -08003740 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3741 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003742 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003743
Mathieu Chartierd424d082014-10-15 10:31:46 -07003744 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003745
Andreas Gampec24f3992014-12-17 20:40:11 -08003746 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003747 // ELF has special kind of section called SHT_NOBITS which allows us to create
3748 // sections which exist but their data is omitted from the ELF file to save space.
3749 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3750 // with only debug data. We use it in similar way to exclude .rodata and .text.
3751 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003752 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3753 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003754 } else {
3755 return DumpOat(nullptr,
3756 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003757 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003758 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003759 args_->os_) == EXIT_SUCCESS;
3760 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003761 }
3762
Igor Murashkin37743352014-11-13 14:38:00 -08003763 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3764 CHECK(args_ != nullptr);
3765
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003766 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3767 return IMTDumper::Dump(runtime,
3768 args_->imt_dump_,
3769 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003770 args_->oat_filename_,
3771 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003772 }
3773
Igor Murashkin37743352014-11-13 14:38:00 -08003774 if (args_->oat_filename_ != nullptr) {
3775 return DumpOat(runtime,
3776 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003777 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003778 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003779 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003780 }
Igor Murashkin37743352014-11-13 14:38:00 -08003781
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003782 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003783 }
3784
Igor Murashkin37743352014-11-13 14:38:00 -08003785 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3786};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003787
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003788} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003789
3790int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003791 // Output all logging to stderr.
3792 android::base::SetLogger(android::base::StderrLogger);
3793
Igor Murashkin37743352014-11-13 14:38:00 -08003794 art::OatdumpMain main;
3795 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003796}