blob: c0d0478bf95e83cf41c88096887939f7f3be0162 [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 Sehr8f4b0562018-03-02 12:01:51 -080037#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080038#include "base/safe_map.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000039#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080040#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070042#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010043#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000044#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000045#include "debug/elf_debug_writer.h"
46#include "debug/method_debug_info.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080048#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080049#include "dex/dex_file-inl.h"
50#include "dex/dex_instruction-inl.h"
David Sehre6564f42018-03-19 08:39:26 -070051#include "dex/string_reference.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080052#include "disassembler.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070053#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070054#include "gc/space/image_space.h"
55#include "gc/space/large_object_space.h"
56#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080057#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070058#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080059#include "indenter.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070060#include "subtype_check.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000061#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070062#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000063#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000064#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000065#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066#include "mirror/array-inl.h"
67#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010068#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069#include "mirror/object-inl.h"
70#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080071#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010072#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070073#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070074#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070075#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070076#include "stack_map.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080077#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070078#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010079#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080080#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000081#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070082#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070083
Igor Murashkin37743352014-11-13 14:38:00 -080084#include <sys/stat.h>
85#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070086
Igor Murashkin37743352014-11-13 14:38:00 -080087namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070088
Andreas Gampe46ee31b2016-12-14 10:11:49 -080089using android::base::StringPrintf;
90
Mathieu Chartiere401d142015-04-22 13:56:20 -070091const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080092 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070093 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070094 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010095 "kSaveAllCalleeSavesMethod",
96 "kSaveRefsOnlyMethod",
97 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010098 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -070099 "kSaveEverythingMethodForClinit",
100 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101};
102
103const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700104 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700105 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000106 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700107};
108
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700109// Map is so that we don't allocate multiple dex files for the same OatDexFile.
110static std::map<const OatFile::OatDexFile*,
111 std::unique_ptr<const DexFile>> opened_dex_files;
112
113const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
114 DCHECK(oat_dex_file != nullptr);
115 auto it = opened_dex_files.find(oat_dex_file);
116 if (it != opened_dex_files.end()) {
117 return it->second.get();
118 }
119 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
120 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
121 return ret;
122}
123
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800124template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100125class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700126 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000127 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
128 oat_file_(oat_file),
129 builder_(nullptr),
130 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
131 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 }
133
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700134 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800136 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000137 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
Andreas Gampe08c277c2017-04-26 22:22:15 -0700139 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
140 if (elf_file == nullptr) {
141 return false;
142 }
Vladimir Marko74527972016-11-29 15:57:32 +0000143 std::unique_ptr<linker::BufferedOutputStream> output_stream =
144 std::make_unique<linker::BufferedOutputStream>(
145 std::make_unique<linker::FileOutputStream>(elf_file.get()));
146 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000147
148 builder_->Start();
149
150 auto* rodata = builder_->GetRoData();
151 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000152
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153 const uint8_t* rodata_begin = oat_file_->Begin();
154 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000155 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000156 rodata->Start();
157 rodata->WriteFully(rodata_begin, rodata_size);
158 rodata->End();
159 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000160
David Srbecky6d8c8f02015-10-26 10:57:09 +0000161 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
162 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000163 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000164 text->Start();
165 text->WriteFully(text_begin, text_size);
166 text->End();
167 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000168
Vladimir Marko33bff252017-11-01 14:35:42 +0000169 if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700170 builder_->WriteMIPSabiflagsSection();
171 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000172 builder_->PrepareDynamicSection(elf_file->GetPath(),
173 rodata_size,
174 text_size,
175 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100176 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000177 oat_file_->BssRootsOffset(),
178 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000179 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180
Vladimir Marko1b404a82017-09-01 13:35:26 +0100181 const OatHeader& oat_header = oat_file_->GetOatHeader();
182 #define DO_TRAMPOLINE(fn_name) \
183 if (oat_header.Get ## fn_name ## Offset() != 0) { \
184 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000185 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100186 info.isa = oat_header.GetInstructionSet(); \
187 info.is_code_address_text_relative = true; \
188 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
189 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
190 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
191 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
192 method_debug_infos_.push_back(std::move(info)); \
193 }
194 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
195 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
196 DO_TRAMPOLINE(JniDlsymLookup);
197 DO_TRAMPOLINE(QuickGenericJniTrampoline);
198 DO_TRAMPOLINE(QuickImtConflictTrampoline);
199 DO_TRAMPOLINE(QuickResolutionTrampoline);
200 DO_TRAMPOLINE(QuickToInterpreterBridge);
201 #undef DO_TRAMPOLINE
202
David Srbecky5d950762016-03-07 20:47:29 +0000203 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100204
205 // TODO: Try to symbolize link-time thunks?
206 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700207
David Srbecky32210b92017-12-04 14:39:21 +0000208 // TODO: Add symbols for dex bytecode in the .dex section.
209
210 debug::DebugInfo debug_info{};
211 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
212
David Srbecky5d950762016-03-07 20:47:29 +0000213 debug::WriteDebugInfo(builder_.get(),
David Srbecky32210b92017-12-04 14:39:21 +0000214 debug_info,
David Srbecky5d950762016-03-07 20:47:29 +0000215 dwarf::DW_DEBUG_FRAME_FORMAT,
216 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217
David Srbecky6d8c8f02015-10-26 10:57:09 +0000218 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700219
Andreas Gampe08c277c2017-04-26 22:22:15 -0700220 bool ret_value = builder_->Good();
221
222 builder_.reset();
223 output_stream.reset();
224
225 if (elf_file->FlushCloseOrErase() != 0) {
226 return false;
227 }
228 elf_file.reset();
229
230 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 }
232
David Srbecky5d950762016-03-07 20:47:29 +0000233 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700234 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
235 for (size_t i = 0; i < oat_dex_files.size(); i++) {
236 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000238 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 }
240 }
241
David Srbecky5d950762016-03-07 20:47:29 +0000242 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700244 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
245 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 return;
247 }
248 for (size_t class_def_index = 0;
249 class_def_index < dex_file->NumClassDefs();
250 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
252 OatClassType type = oat_class.GetType();
253 switch (type) {
254 case kOatClassAllCompiled:
255 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000256 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 break;
258
259 case kOatClassNoneCompiled:
260 case kOatClassMax:
261 // Ignore.
262 break;
263 }
264 }
265 }
266
David Srbecky5d950762016-03-07 20:47:29 +0000267 void WalkOatClass(const OatFile::OatClass& oat_class,
268 const DexFile& dex_file,
269 uint32_t class_def_index) {
270 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700271 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 if (class_data == nullptr) { // empty class such as a marker interface?
273 return;
274 }
275 // Note: even if this is an interface or a native class, we still have to walk it, as there
276 // might be a static initializer.
277 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700278 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700279 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800280 for (; it.HasNextMethod(); it.Next()) {
David Srbecky5d950762016-03-07 20:47:29 +0000281 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
282 dex_file,
283 class_def_index,
284 it.GetMemberIndex(),
285 it.GetMethodCodeItem(),
286 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700287 }
288 DCHECK(!it.HasNext());
289 }
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
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700516 // Dumping the dex file overview is compact enough to do even if header only.
517 DexFileData cumulative;
518 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
519 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
520 CHECK(oat_dex_file != nullptr);
521 std::string error_msg;
522 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
523 if (dex_file == nullptr) {
524 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
525 << error_msg;
526 continue;
527 }
528 DexFileData data(*dex_file);
529 os << "Dex file data for " << dex_file->GetLocation() << "\n";
530 data.Dump(os);
531 os << "\n";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700532 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
533 if (layout_sections != nullptr) {
534 os << "Layout data\n";
535 os << *layout_sections;
536 os << "\n";
537 }
538
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700539 cumulative.Add(data);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000540
541 // Dump .bss entries.
542 DumpBssEntries(
543 os,
544 "ArtMethod",
545 oat_dex_file->GetMethodBssMapping(),
546 dex_file->NumMethodIds(),
547 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
548 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
549 DumpBssEntries(
550 os,
551 "Class",
552 oat_dex_file->GetTypeBssMapping(),
553 dex_file->NumTypeIds(),
554 sizeof(GcRoot<mirror::Class>),
555 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
556 DumpBssEntries(
557 os,
558 "String",
559 oat_dex_file->GetStringBssMapping(),
560 dex_file->NumStringIds(),
561 sizeof(GcRoot<mirror::Class>),
562 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700563 }
564 os << "Cumulative dex file data\n";
565 cumulative.Dump(os);
566 os << "\n";
567
David Brazdilc03d7b62016-03-02 12:18:03 +0000568 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000569 VariableIndentationOutputStream vios(&os);
Nicolas Geoffrayc334f832018-03-02 10:52:16 +0000570 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000571 if (vdex_header.IsValid()) {
572 std::string error_msg;
573 std::vector<const DexFile*> dex_files;
574 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
575 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
576 if (dex_file == nullptr) {
577 os << "Error opening dex file: " << error_msg << std::endl;
578 return false;
579 }
580 dex_files.push_back(dex_file);
581 }
582 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
583 deps.Dump(&vios);
584 } else {
585 os << "UNRECOGNIZED vdex file, magic "
586 << vdex_header.GetMagic()
Nicolas Geoffrayc334f832018-03-02 10:52:16 +0000587 << ", verifier deps version "
588 << vdex_header.GetVerifierDepsVersion()
589 << ", dex section version "
590 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000591 << "\n";
592 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000593 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
594 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
595 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300596 if (!DumpOatDexFile(os, *oat_dex_file)) {
597 success = false;
598 }
599 }
600 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800601
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300602 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000603 std::string error_msg;
604 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
605 if (!OS::FileExists(vdex_filename.c_str())) {
606 os << "File " << vdex_filename.c_str() << " does not exist\n";
607 return false;
608 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300609
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000610 DexFileUniqV vdex_dex_files;
611 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
612 &vdex_dex_files,
613 &error_msg);
614 if (vdex_file.get() == nullptr) {
615 os << "Failed to open vdex file: " << error_msg << "\n";
616 return false;
617 }
618 if (oat_dex_files_.size() != vdex_dex_files.size()) {
619 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
620 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
621 return false;
622 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300623
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000624 size_t i = 0;
625 for (const auto& vdex_dex_file : vdex_dex_files) {
626 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
627 CHECK(oat_dex_file != nullptr);
628 CHECK(vdex_dex_file != nullptr);
629 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get())) {
630 success = false;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300631 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000632 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700634 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000635
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800636 {
637 os << "OAT FILE STATS:\n";
638 VariableIndentationOutputStream vios(&os);
639 stats_.Dump(vios);
640 }
641
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700642 os << std::flush;
643 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700644 }
645
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800646 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700647 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
648 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800649 return 0; // Address not in oat file
650 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800651 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
652 reinterpret_cast<uintptr_t>(oat_file_.Begin());
653 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800654 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800656 return end_offset - begin_offset;
657 }
658
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800659 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700660 return oat_file_.GetOatHeader().GetInstructionSet();
661 }
662
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700663 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800664 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
665 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700666 CHECK(oat_dex_file != nullptr);
667 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700668 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
669 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700670 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
671 << "': " << error_msg;
672 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800673 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700674 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700675 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700676 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700677 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100678 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800679 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100680 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800681 }
682 }
683 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700684 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800685 }
686
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300687 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
688 // files are fully unquickened and stored in dex_files
689 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
690 /* out */ DexFileUniqV* dex_files,
691 /* out */ std::string* error_msg) {
692 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
693 if (file == nullptr) {
694 *error_msg = "Could not open file " + vdex_filename + " for reading.";
695 return nullptr;
696 }
697
698 int64_t vdex_length = file->GetLength();
699 if (vdex_length == -1) {
700 *error_msg = "Could not read the length of file " + vdex_filename;
701 return nullptr;
702 }
703
704 std::unique_ptr<MemMap> mmap(MemMap::MapFile(
705 file->GetLength(),
706 PROT_READ | PROT_WRITE,
707 MAP_PRIVATE,
708 file->Fd(),
709 /* start offset */ 0,
710 /* low_4gb */ false,
711 vdex_filename.c_str(),
712 error_msg));
713 if (mmap == nullptr) {
714 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
715 return nullptr;
716 }
717
718 std::unique_ptr<VdexFile> vdex_file(new VdexFile(mmap.release()));
719 if (!vdex_file->IsValid()) {
720 *error_msg = "Vdex file is not valid";
721 return nullptr;
722 }
723
724 DexFileUniqV tmp_dex_files;
725 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
726 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
727 return nullptr;
728 }
729
730 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300731 /* decompile_return_instruction */ true);
732
733 *dex_files = std::move(tmp_dex_files);
734 return vdex_file;
735 }
736
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800737 struct Stats {
738 enum ByteKind {
739 kByteKindCode,
740 kByteKindQuickMethodHeader,
741 kByteKindCodeInfoLocationCatalog,
742 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800743 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800744 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000745 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800746 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800747 kByteKindStackMapNativePc,
748 kByteKindStackMapDexPc,
749 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800750 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800751 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000752 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700753 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800754 kByteKindInlineInfoDexPc,
755 kByteKindInlineInfoExtraData,
756 kByteKindInlineInfoDexRegisterMap,
757 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800758 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800759 // Special ranges for std::accumulate convenience.
760 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000761 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700762 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800763 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800764 };
765 int64_t bits[kByteKindCount] = {};
766 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
767 // deduplicated code and tables.
768 std::unordered_set<const void*> seen;
769
770 // Returns true if it was newly added.
771 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
772 if (seen.insert(address).second == true) {
773 // True means the address was not already in the set.
774 AddBits(kind, count);
775 return true;
776 }
777 return false;
778 }
779
780 void AddBits(ByteKind kind, int64_t count) {
781 bits[kind] += count;
782 }
783
784 void Dump(VariableIndentationOutputStream& os) {
785 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
786 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
787 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800788 Dump(os, "Code ", bits[kByteKindCode], sum);
789 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
790 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
791 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
792 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800793 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
794 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800795 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800796 // Stack map section.
797 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
798 bits + kByteKindStackMapLast + 1,
799 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800800 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800801 {
802 ScopedIndentation indent1(&os);
803 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800804 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800805 bits[kByteKindStackMapNativePc],
806 stack_map_bits,
807 "stack map");
808 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800809 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800810 bits[kByteKindStackMapDexPc],
811 stack_map_bits,
812 "stack map");
813 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800814 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800815 bits[kByteKindStackMapDexRegisterMap],
816 stack_map_bits,
817 "stack map");
818 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800819 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800820 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800821 stack_map_bits,
822 "stack map");
823 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800824 "StackMapRegisterMaskIndex ",
825 bits[kByteKindStackMapRegisterMaskIndex],
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 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000830 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800831 stack_map_bits,
832 "stack map");
833 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800834 // Inline info section.
835 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
836 bits + kByteKindInlineInfoLast + 1,
837 0u);
838 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
839 {
840 ScopedIndentation indent1(&os);
841 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700842 "InlineInfoMethodIndexIdx ",
843 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800844 inline_info_bits,
845 "inline info");
846 Dump(os,
847 "InlineInfoDexPc ",
848 bits[kByteKindStackMapDexPc],
849 inline_info_bits,
850 "inline info");
851 Dump(os,
852 "InlineInfoExtraData ",
853 bits[kByteKindInlineInfoExtraData],
854 inline_info_bits,
855 "inline info");
856 Dump(os,
857 "InlineInfoDexRegisterMap ",
858 bits[kByteKindInlineInfoDexRegisterMap],
859 inline_info_bits,
860 "inline info");
861 Dump(os,
862 "InlineInfoIsLast ",
863 bits[kByteKindInlineInfoIsLast],
864 inline_info_bits,
865 "inline info");
866 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800867 }
868 os.Stream() << "\n" << std::flush;
869 }
870
871 private:
872 void Dump(VariableIndentationOutputStream& os,
873 const char* name,
874 int64_t size,
875 int64_t total,
876 const char* sum_of = "total") {
877 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
878 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
879 name,
880 size / kBitsPerByte,
881 percent,
882 sum_of);
883 }
884 };
885
Brian Carlstromaded5f72011-10-07 17:15:04 -0700886 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800887 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800888 // We don't know the length of the code for each method, but we need to know where to stop
889 // when disassembling. What we do know is that a region of code will be followed by some other
890 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
891 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800892 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
893 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700894 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700895 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700896 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
897 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700898 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
899 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800900 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800901 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800902 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800903 for (size_t class_def_index = 0;
904 class_def_index < dex_file->NumClassDefs();
905 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800906 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100907 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700908 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700909 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800910 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700911 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800912 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800913 while (it.HasNextMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100914 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800915 it.Next();
916 }
917 }
918 }
919 }
920
921 // If the last thing in the file is code for a method, there won't be an offset for the "next"
922 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
923 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800924 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800925 }
926
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
928 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
929 }
930
Elliott Hughese3c845c2012-02-28 17:23:01 -0800931 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800932 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000933 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800934 code_offset &= ~0x1;
935 }
936 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800937 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800938 }
939
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700940 // Dex file data, may be for multiple different dex files.
941 class DexFileData {
942 public:
943 DexFileData() {}
944
945 explicit DexFileData(const DexFile& dex_file)
946 : num_string_ids_(dex_file.NumStringIds()),
947 num_method_ids_(dex_file.NumMethodIds()),
948 num_field_ids_(dex_file.NumFieldIds()),
949 num_type_ids_(dex_file.NumTypeIds()),
950 num_class_defs_(dex_file.NumClassDefs()) {
951 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
952 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
953 WalkClass(dex_file, class_def);
954 }
955 }
956
957 void Add(const DexFileData& other) {
958 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
959 num_string_ids_from_code_ += other.num_string_ids_from_code_;
960 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
961 dex_code_bytes_ += other.dex_code_bytes_;
962 num_string_ids_ += other.num_string_ids_;
963 num_method_ids_ += other.num_method_ids_;
964 num_field_ids_ += other.num_field_ids_;
965 num_type_ids_ += other.num_type_ids_;
966 num_class_defs_ += other.num_class_defs_;
967 }
968
969 void Dump(std::ostream& os) {
970 os << "Num string ids: " << num_string_ids_ << "\n";
971 os << "Num method ids: " << num_method_ids_ << "\n";
972 os << "Num field ids: " << num_field_ids_ << "\n";
973 os << "Num type ids: " << num_type_ids_ << "\n";
974 os << "Num class defs: " << num_class_defs_ << "\n";
975 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
976 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
977 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
978 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
979 }
980
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800981 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800982 // All of the elements from one container to another.
983 template <typename Dest, typename Src>
984 static void AddAll(Dest& dest, const Src& src) {
985 dest.insert(src.begin(), src.end());
986 }
987
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700988 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
989 const uint8_t* class_data = dex_file.GetClassData(class_def);
990 if (class_data == nullptr) { // empty class such as a marker interface?
991 return;
992 }
993 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700994 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800995 while (it.HasNextMethod()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700996 WalkCodeItem(dex_file, it.GetMethodCodeItem());
997 it.Next();
998 }
999 DCHECK(!it.HasNext());
1000 }
1001
1002 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
1003 if (code_item == nullptr) {
1004 return;
1005 }
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001006 CodeItemInstructionAccessor instructions(dex_file, code_item);
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001007
1008 // If we inserted a new dex code item pointer, add to total code bytes.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001009 const uint16_t* code_ptr = instructions.Insns();
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001010 if (dex_code_item_ptrs_.insert(code_ptr).second) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001011 dex_code_bytes_ += instructions.InsnsSizeInCodeUnits() * sizeof(code_ptr[0]);
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001012 }
1013
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001014 for (const DexInstructionPcPair& inst : instructions) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001015 switch (inst->Opcode()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001016 case Instruction::CONST_STRING: {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001017 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001018 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
1019 ++num_string_ids_from_code_;
1020 break;
1021 }
1022 case Instruction::CONST_STRING_JUMBO: {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001023 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001024 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
1025 ++num_string_ids_from_code_;
1026 break;
1027 }
1028 default:
1029 break;
1030 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001031 }
1032 }
1033
1034 // Unique string ids loaded from dex code.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001035 std::set<StringReference> unique_string_ids_from_code_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001036
1037 // Total string ids loaded from dex code.
1038 size_t num_string_ids_from_code_ = 0;
1039
1040 // Unique code pointers.
1041 std::set<const void*> dex_code_item_ptrs_;
1042
1043 // Total "unique" dex code bytes.
1044 size_t dex_code_bytes_ = 0;
1045
1046 // Other dex ids.
1047 size_t num_string_ids_ = 0;
1048 size_t num_method_ids_ = 0;
1049 size_t num_field_ids_ = 0;
1050 size_t num_type_ids_ = 0;
1051 size_t num_class_defs_ = 0;
1052 };
1053
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1055 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001056 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001057 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -08001058 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -08001059 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001060
Andreas Gampe2ba88952016-04-29 17:52:07 -07001061 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001062 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
1063 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001064
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001065 // Print data range of the dex file embedded inside the corresponding vdex file.
1066 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
1067 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
1068 os << StringPrintf(
1069 "dex-file: 0x%08x..0x%08x\n",
1070 dex_offset,
1071 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
1072 } else {
1073 os << StringPrintf("dex-file not in VDEX file\n");
1074 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001075
Andreas Gampe2ba88952016-04-29 17:52:07 -07001076 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001077 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001078 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1079 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001080 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001081 os << std::flush;
1082 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001083 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001084
Andreas Gampe2ba88952016-04-29 17:52:07 -07001085 // Print lookup table, if it exists.
1086 if (oat_dex_file.GetLookupTableData() != nullptr) {
1087 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
1088 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -07001089 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -07001090 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
1091 table_offset,
1092 table_offset + table_size - 1);
1093 }
1094
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001095 VariableIndentationOutputStream vios(&os);
1096 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001097 for (size_t class_def_index = 0;
1098 class_def_index < dex_file->NumClassDefs();
1099 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001100 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
1101 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001102
1103 // TODO: Support regex
1104 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
1105 continue;
1106 }
1107
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001108 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001109 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -08001111 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001112 << " (" << oat_class.GetStatus() << ")"
1113 << " (" << oat_class.GetType() << ")\n";
1114 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001115 if (options_.list_classes_) {
1116 continue;
1117 }
1118 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001119 success = false;
1120 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001121 if (stop_analysis) {
1122 os << std::flush;
1123 return success;
1124 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001125 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001126 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001127 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001128 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001129 }
1130
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001131 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
1132 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
1133 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
1134 // original checksum.
1135 bool ExportDexFile(std::ostream& os,
1136 const OatFile::OatDexFile& oat_dex_file,
1137 const DexFile* dex_file) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001138 std::string error_msg;
1139 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001140 size_t fsize = oat_dex_file.FileSize();
1141
1142 // Some quick checks just in case
1143 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1144 os << "Invalid dex file\n";
1145 return false;
1146 }
1147
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001148 if (dex_file == nullptr) {
1149 // Exported bytecode is quickened (dex-to-dex transformations present)
1150 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1151 if (dex_file == nullptr) {
1152 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1153 return false;
1154 }
1155
1156 // Recompute checksum
1157 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
1158 dex_file->CalculateChecksum();
1159 } else {
1160 // Vdex unquicken output should match original input bytecode
1161 uint32_t orig_checksum =
1162 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001163 CHECK_EQ(orig_checksum, dex_file->CalculateChecksum());
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001164 if (orig_checksum != dex_file->CalculateChecksum()) {
1165 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1166 return false;
1167 }
1168 }
1169
Mathieu Chartierc3dbfcc2018-04-05 16:37:14 -07001170 // Update header for shared section.
1171 uint32_t shared_section_offset = 0u;
1172 uint32_t shared_section_size = 0u;
1173 if (dex_file->IsCompactDexFile()) {
1174 CompactDexFile::Header* const header =
1175 reinterpret_cast<CompactDexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()));
1176 shared_section_offset = header->data_off_;
1177 shared_section_size = header->data_size_;
1178 // The shared section will be serialized right after the dex file.
1179 header->data_off_ = header->file_size_;
1180 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001181 // Verify output directory exists
1182 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1183 // TODO: Extend OS::DirectoryExists if symlink support is required
1184 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1185 return false;
1186 }
1187
1188 // Beautify path names
1189 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1190 return false;
1191 }
1192
1193 std::string dex_orig_name;
1194 size_t dex_orig_pos = dex_file_location.rfind('/');
1195 if (dex_orig_pos == std::string::npos)
1196 dex_orig_name = dex_file_location;
1197 else
1198 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1199
1200 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001201 if (dex_orig_name.size() == 8 &&
1202 dex_orig_name.compare("base.apk") == 0 &&
1203 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001204 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1205 size_t apk_orig_pos = dex_file_location.rfind('/');
1206 if (apk_orig_pos != std::string::npos) {
1207 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1208 }
1209 }
1210
1211 std::string out_dex_path(options_.export_dex_location_);
1212 if (out_dex_path.back() != '/') {
1213 out_dex_path.append("/");
1214 }
1215 out_dex_path.append(dex_orig_name);
1216 out_dex_path.append("_export.dex");
1217 if (out_dex_path.length() > PATH_MAX) {
1218 return false;
1219 }
1220
1221 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1222 if (file.get() == nullptr) {
1223 os << "Failed to open output dex file " << out_dex_path;
1224 return false;
1225 }
1226
Mathieu Chartierc3dbfcc2018-04-05 16:37:14 -07001227 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001228 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001229 os << "Failed to write dex file";
1230 file->Erase();
1231 return false;
1232 }
1233
Mathieu Chartierc3dbfcc2018-04-05 16:37:14 -07001234 if (shared_section_size != 0) {
1235 success = file->WriteFully(dex_file->Begin() + shared_section_offset, shared_section_size);
1236 if (!success) {
1237 os << "Failed to write shared data section";
1238 file->Erase();
1239 return false;
1240 }
1241 }
1242
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001243 if (file->FlushCloseOrErase() != 0) {
1244 os << "Flush and close failed";
1245 return false;
1246 }
1247
1248 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1249 os << std::flush;
1250
1251 return true;
1252 }
1253
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001254 bool DumpOatClass(VariableIndentationOutputStream* vios,
1255 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001256 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001257 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001258 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001259 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001260 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001262 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001263 }
1264 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001265 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001266 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001267 while (it.HasNextMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001268 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001270 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001271 success = false;
1272 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001273 if (addr_found) {
1274 *stop_analysis = true;
1275 return success;
1276 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001277 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001278 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001279 }
Ian Rogers0571d352011-11-03 19:51:38 -07001280 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001281 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001283 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001284
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001285 static constexpr uint32_t kPrologueBytes = 16;
1286
1287 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1288 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1289
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001290 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1291 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001292 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001293 const OatFile::OatClass& oat_class,
1294 const DexFile& dex_file,
1295 uint32_t dex_method_idx,
1296 const DexFile::CodeItem* code_item,
1297 uint32_t method_access_flags,
1298 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001299 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001300
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001301 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001302
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001303 // TODO: Support regex
1304 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1305 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001306 return success;
1307 }
1308
David Sehr709b0702016-10-13 09:12:37 -07001309 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001310 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1311 class_method_index, pretty_method.c_str(),
1312 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001313 if (options_.list_methods_) {
1314 return success;
1315 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001316
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001317 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1318 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1319 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1320 uint32_t code_offset = oat_method.GetCodeOffset();
1321 uint32_t code_size = oat_method.GetQuickCodeSize();
1322 if (resolved_addr2instr_ != 0) {
1323 if (resolved_addr2instr_ > code_offset + code_size) {
1324 return success;
1325 } else {
1326 *addr_found = true; // stop analyzing file at next iteration
1327 }
1328 }
1329
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001330 // Everything below is indented at least once.
1331 ScopedIndentation indent1(vios);
1332
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001334 vios->Stream() << "DEX CODE:\n";
1335 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001336 if (code_item_accessor.HasCodeItem()) {
1337 for (const DexInstructionPcPair& inst : code_item_accessor) {
1338 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1339 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1340 }
1341 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001342 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001343
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001344 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001345 std::unique_ptr<verifier::MethodVerifier> verifier;
1346 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001347 // We need to have the handle scope stay live until after the verifier since the verifier has
1348 // a handle to the dex cache from hs.
1349 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001350 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1351 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001352 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001353 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001354 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001355 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001356 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001357 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001358 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001359 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001360 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001361 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001362 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001363 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001364 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1365 oat_method_offsets_offset, oat_file_.Size());
1366 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001367 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001368 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001369 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001370
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001371 ScopedIndentation indent2(vios);
1372 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001373 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1374 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 vios->Stream() << StringPrintf("WARNING: "
1376 "code offset 0x%08x is past end of file 0x%08zx.\n",
1377 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001378 success = false;
1379 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001380 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001381 }
1382 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001383 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001384 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1385 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001386 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1387 sizeof(*method_header) * kBitsPerByte,
1388 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001389 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001390 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001391 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001392 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001393 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001394 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001395 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1396 method_header_offset, oat_file_.Size());
1397 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001398 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001399 return false;
1400 }
1401
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001402 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001403 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001404 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001405 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001406 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001407 uint32_t vmap_table_offset = method_header ==
1408 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001409 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001410
1411 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001412 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001413 ? oat_file_.GetVdexFile()->Size()
1414 : method_header->GetCode() - oat_file_.Begin();
1415 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001416 vios->Stream() << StringPrintf("WARNING: "
1417 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1418 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001419 vmap_table_offset,
1420 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001421 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001422 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001423 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001424 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001426 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001427 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001428 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001429
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001430 ScopedIndentation indent2(vios);
1431 vios->Stream()
1432 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1433 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1434 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1435 vios->Stream() << "\n";
1436 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1437 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1438 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001439 }
1440 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001441 // Based on spill masks from QuickMethodFrameInfo so placed
1442 // after it is dumped, but useful for understanding quick
1443 // code, so dumped here.
1444 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001445 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001446 }
1447 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001449 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1450 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001451 ScopedIndentation indent2(vios);
1452 vios->Stream() << StringPrintf("WARNING: "
1453 "code size offset 0x%08x is past end of file 0x%08zx.",
1454 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001455 success = false;
1456 } else {
1457 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001458 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1459 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001460 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001461
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001462 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001463 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001464 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001465 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1466 code_offset,
1467 code_size_offset,
1468 code_size,
1469 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001470
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001471 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001472 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001473 vios->Stream() << StringPrintf("WARNING: "
1474 "start of code at 0x%08x is past end of file 0x%08zx.",
1475 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001476 success = false;
1477 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 vios->Stream() << StringPrintf(
1479 "WARNING: "
1480 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1481 "code size is 0x%08x loaded from offset 0x%08x.\n",
1482 aligned_code_end, oat_file_.Size(),
1483 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001484 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001485 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001486 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001487 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001488 }
1489 }
1490 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 vios->Stream() << StringPrintf(
1492 "WARNING: "
1493 "code size %d is bigger than max expected threshold of %d. "
1494 "code size is 0x%08x loaded from offset 0x%08x.\n",
1495 code_size, kMaxCodeSize,
1496 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001497 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001498 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001499 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001500 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001501 }
1502 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001503 } else if (options_.disassemble_code_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001504 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001505 }
1506 }
1507 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001508 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001509 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001510 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001511
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001512 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1513 if (spill_mask == 0) {
1514 return;
1515 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001516 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001517 for (size_t i = 0; i < 32; i++) {
1518 if ((spill_mask & (1 << i)) != 0) {
1519 if (is_float) {
1520 os << "fr" << i;
1521 } else {
1522 os << "r" << i;
1523 }
1524 spill_mask ^= 1 << i; // clear bit
1525 if (spill_mask != 0) {
1526 os << ", ";
1527 } else {
1528 break;
1529 }
1530 }
1531 }
1532 os << ")";
1533 }
1534
Roland Levillain442b46a2015-02-18 16:54:21 +00001535 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001536 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001537 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001538 const CodeItemDataAccessor& code_item_accessor) {
1539 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001540 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001541 const void* raw_code_info = oat_method.GetVmapTable();
1542 if (raw_code_info != nullptr) {
1543 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001544 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001545 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001546 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001547 DumpCodeInfo(vios, code_info, oat_method, code_item_accessor, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001548 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001549 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001550 // We don't encode the size in the table, so just emit that we have quickened
1551 // information.
1552 ScopedIndentation indent(vios);
1553 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001554 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001555 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001556 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001557 }
1558
1559 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001560 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001561 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001562 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001563 const CodeItemDataAccessor& code_item_accessor,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001564 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001565 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001566 oat_method.GetCodeOffset(),
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001567 code_item_accessor.RegistersSize(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001568 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001569 instruction_set_,
1570 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001571 }
1572
Andreas Gampe36a296f2017-06-13 14:11:11 -07001573 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1574 // According to stack model, the first out is above the Method referernce.
1575 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1576 }
1577
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001578 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001579 uint32_t core_spills,
1580 uint32_t fp_spills,
1581 size_t frame_size,
1582 int reg,
1583 InstructionSet isa) {
1584 PointerSize pointer_size = InstructionSetPointerSize(isa);
1585 if (kIsDebugBuild) {
1586 auto* runtime = Runtime::Current();
1587 if (runtime != nullptr) {
1588 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1589 }
1590 }
1591 DCHECK_ALIGNED(frame_size, kStackAlignment);
1592 DCHECK_NE(reg, -1);
1593 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1594 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1595 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001596 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1597 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001598 const int max_num_special_temps = 1;
1599 if (reg == temp_threshold) {
1600 // The current method pointer corresponds to special location on stack.
1601 return 0;
1602 } else if (reg >= temp_threshold + max_num_special_temps) {
1603 /*
1604 * Special temporaries may have custom locations and the logic above deals with that.
1605 * However, non-special temporaries are placed relative to the outs.
1606 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001607 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001608 + static_cast<size_t>(pointer_size) /* art method */;
1609 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1610 return temps_start + relative_offset;
1611 } else if (reg < num_regs) {
1612 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1613 return locals_start + (reg * sizeof(uint32_t));
1614 } else {
1615 // Handle ins.
1616 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1617 + static_cast<size_t>(pointer_size) /* art method */;
1618 }
1619 }
1620
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001621 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001622 const CodeItemDataAccessor& code_item_accessor) {
1623 if (code_item_accessor.HasCodeItem()) {
1624 size_t num_locals_ins = code_item_accessor.RegistersSize();
1625 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001626 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001627 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001628
1629 os << "vr_stack_locations:";
1630 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1631 // For readability, delimit the different kinds of VRs.
1632 if (reg == num_locals_ins) {
1633 os << "\n\tmethod*:";
1634 } else if (reg == num_locals && num_ins > 0) {
1635 os << "\n\tins:";
1636 } else if (reg == 0 && num_locals > 0) {
1637 os << "\n\tlocals:";
1638 }
1639
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001640 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001641 oat_method.GetCoreSpillMask(),
1642 oat_method.GetFpSpillMask(),
1643 oat_method.GetFrameSizeInBytes(),
1644 reg,
1645 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001646 os << " v" << reg << "[sp + #" << offset << "]";
1647 }
1648
1649 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1650 if (out_reg == 0) {
1651 os << "\n\touts:";
1652 }
1653
Andreas Gampe36a296f2017-06-13 14:11:11 -07001654 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001655 os << " v" << out_reg << "[sp + #" << offset << "]";
1656 }
1657
1658 os << "\n";
1659 }
1660 }
1661
Roland Levillainf2650d12015-05-28 14:53:28 +01001662 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1663 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001664 static bool IsMethodGeneratedByOptimizingCompiler(
1665 const OatFile::OatMethod& oat_method,
1666 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001667 // If the native GC map is null and the Dex `code_item` is not
1668 // null, then this method has been compiled with the optimizing
1669 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001670 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001671 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001672 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001673 }
1674
1675 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1676 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001677 static bool IsMethodGeneratedByDexToDexCompiler(
1678 const OatFile::OatMethod& oat_method,
1679 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001680 // If the quick code is null, the Dex `code_item` is not
1681 // null, and the vmap table is not null, then this method has been compiled
1682 // with the dextodex compiler.
1683 return oat_method.GetQuickCode() == nullptr &&
1684 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001685 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001686 }
1687
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001688 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001689 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001690 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001691 const DexFile* dex_file,
1692 const DexFile::ClassDef& class_def,
1693 const DexFile::CodeItem* code_item,
1694 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001695 if ((method_access_flags & kAccNative) == 0) {
1696 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001697 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001698 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001699 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1700 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1701 CHECK(dex_cache != nullptr);
Nicolas Geoffray6589af12017-11-13 15:16:22 +00001702 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1703 dex_method_idx, dex_cache, *options_.class_loader_);
1704 CHECK(method != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001705 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001706 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Nicolas Geoffray6589af12017-11-13 15:16:22 +00001707 class_def, code_item, method, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001709
1710 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 }
1712
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001713 // The StackMapsHelper provides the stack maps in the native PC order.
1714 // For identical native PCs, the order from the CodeInfo is preserved.
1715 class StackMapsHelper {
1716 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001717 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001718 : code_info_(raw_code_info),
1719 encoding_(code_info_.ExtractEncoding()),
1720 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1721 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001722 offset_(static_cast<uint32_t>(-1)),
1723 stack_map_index_(0u),
1724 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001725 if (number_of_stack_maps_ != 0u) {
1726 // Check if native PCs are ordered.
1727 bool ordered = true;
1728 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1729 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1730 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001731 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1732 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001733 ordered = false;
1734 break;
1735 }
1736 last = current;
1737 }
1738 if (!ordered) {
1739 // Create indirection indexes for access in native PC order. We do not optimize
1740 // for the fact that there can currently be only two separately ordered ranges,
1741 // namely normal stack maps and catch-point stack maps.
1742 indexes_.resize(number_of_stack_maps_);
1743 std::iota(indexes_.begin(), indexes_.end(), 0u);
1744 std::sort(indexes_.begin(),
1745 indexes_.end(),
1746 [this](size_t lhs, size_t rhs) {
1747 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001748 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001749 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001750 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001751 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001752 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001753 // If the PCs are the same, compare indexes to preserve the original order.
1754 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1755 });
1756 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001757 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001758 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001759 }
1760 }
1761
1762 const CodeInfo& GetCodeInfo() const {
1763 return code_info_;
1764 }
1765
1766 const CodeInfoEncoding& GetEncoding() const {
1767 return encoding_;
1768 }
1769
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001770 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001771 return offset_;
1772 }
1773
1774 StackMap GetStackMap() const {
1775 return GetStackMapAt(stack_map_index_);
1776 }
1777
1778 void Next() {
1779 ++stack_map_index_;
1780 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001781 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001782 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001783 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001784 }
1785
1786 private:
1787 StackMap GetStackMapAt(size_t i) const {
1788 if (!indexes_.empty()) {
1789 i = indexes_[i];
1790 }
1791 DCHECK_LT(i, number_of_stack_maps_);
1792 return code_info_.GetStackMapAt(i, encoding_);
1793 }
1794
1795 const CodeInfo code_info_;
1796 const CodeInfoEncoding encoding_;
1797 const size_t number_of_stack_maps_;
1798 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001799 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001800 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001801 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001802 };
1803
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001804 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001805 const OatFile::OatMethod& oat_method,
1806 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001807 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001808 const void* quick_code = oat_method.GetQuickCode();
1809
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001810 if (code_size == 0) {
1811 code_size = oat_method.GetQuickCodeSize();
1812 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001813 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001814 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001815 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001816 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1817 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001818 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001819 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001820 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001821 {
1822 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001823 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001824 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001825 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001826 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001827 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001828 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001829 stats_.AddBits(
1830 Stats::kByteKindStackMapNativePc,
1831 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1832 stats_.AddBits(
1833 Stats::kByteKindStackMapDexPc,
1834 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1835 stats_.AddBits(
1836 Stats::kByteKindStackMapDexRegisterMap,
1837 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1838 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001839 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001840 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1841 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001842 Stats::kByteKindStackMapRegisterMaskIndex,
1843 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001844 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001845 Stats::kByteKindStackMapStackMaskIndex,
1846 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001847
1848 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001849 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001850 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001851 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001852
1853 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001854 stats_.AddBits(
1855 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001856 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001857
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001858 // Invoke infos
1859 if (encoding.invoke_info.num_entries > 0u) {
1860 stats_.AddBits(
1861 Stats::kByteKindCodeInfoInvokeInfo,
1862 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1863 }
1864
Mathieu Chartier1e083792017-02-08 13:30:04 -08001865 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001866 const size_t location_catalog_bytes =
1867 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1868 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1869 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001870 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001871 const size_t dex_register_bytes =
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001872 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding,
1873 code_item_accessor.RegistersSize());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001874 stats_.AddBits(
1875 Stats::kByteKindCodeInfoDexRegisterMap,
1876 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001877
1878 // Inline infos.
1879 const size_t num_inline_infos = encoding.inline_info.num_entries;
1880 if (num_inline_infos > 0u) {
1881 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001882 Stats::kByteKindInlineInfoMethodIndexIdx,
1883 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1884 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001885 stats_.AddBits(
1886 Stats::kByteKindInlineInfoDexPc,
1887 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1888 stats_.AddBits(
1889 Stats::kByteKindInlineInfoExtraData,
1890 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1891 stats_.AddBits(
1892 Stats::kByteKindInlineInfoDexRegisterMap,
1893 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1894 num_inline_infos);
1895 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1896 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001897 }
1898 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001899 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1900 size_t offset = 0;
1901 while (offset < code_size) {
1902 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1903 if (offset == helper.GetOffset()) {
1904 ScopedIndentation indent1(vios);
1905 StackMap stack_map = helper.GetStackMap();
1906 DCHECK(stack_map.IsValid());
1907 stack_map.Dump(vios,
1908 helper.GetCodeInfo(),
1909 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001910 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001911 oat_method.GetCodeOffset(),
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001912 code_item_accessor.RegistersSize(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001913 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001914 do {
1915 helper.Next();
1916 // There may be multiple stack maps at a given PC. We display only the first one.
1917 } while (offset == helper.GetOffset());
1918 }
1919 DCHECK_LT(offset, helper.GetOffset());
1920 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001921 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001922 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1923 size_t offset = 0;
1924 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001925 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001927 }
1928 }
1929
Vladimir Markof3c52b42017-11-17 17:32:12 +00001930 template <typename NameGetter>
1931 void DumpBssEntries(std::ostream& os,
1932 const char* slot_type,
1933 const IndexBssMapping* mapping,
1934 uint32_t number_of_indexes,
1935 size_t slot_size,
1936 NameGetter name) {
1937 os << ".bss mapping for " << slot_type << ": ";
1938 if (mapping == nullptr) {
1939 os << "empty.\n";
1940 return;
1941 }
1942 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1943 size_t num_valid_indexes = 0u;
1944 for (const IndexBssMappingEntry& entry : *mapping) {
1945 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1946 }
1947 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1948 os << std::hex;
1949 for (const IndexBssMappingEntry& entry : *mapping) {
1950 uint32_t index = entry.GetIndex(index_bits);
1951 uint32_t mask = entry.GetMask(index_bits);
1952 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1953 for (uint32_t n : LowToHighBits(mask)) {
1954 size_t current_index = index - (32u - index_bits) + n;
1955 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1956 bss_offset += slot_size;
1957 }
1958 DCHECK_EQ(bss_offset, entry.bss_offset);
1959 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1960 }
1961 os << std::dec;
1962 }
1963
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001964 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001965 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001966 const OatDumperOptions& options_;
1967 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001968 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001969 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001970 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001971 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001972};
1973
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001975 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001976 ImageDumper(std::ostream* os,
1977 gc::space::ImageSpace& image_space,
1978 const ImageHeader& image_header,
1979 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001980 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001981 vios_(os),
1982 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001983 image_space_(image_space),
1984 image_header_(image_header),
1985 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001986
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001987 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001988 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001989 std::ostream& indent_os = vios_.Stream();
1990
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001991 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001992
Jeff Haodcdc85b2015-12-04 14:06:18 -08001993 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1994
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001995 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001996
Mathieu Chartiere401d142015-04-22 13:56:20 -07001997 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1998
1999 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
2000 auto section = static_cast<ImageHeader::ImageSections>(i);
2001 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
2002 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07002003
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002004 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002005
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002006 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002007
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002008 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
2009
2010 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
2011
2012 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002013
Alex Lighta59dd802014-07-02 16:28:08 -07002014 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
2015
Igor Murashkin46774762014-10-22 11:37:02 -07002016 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
2017
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002018 {
2019 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07002020 static_assert(arraysize(image_roots_descriptions_) ==
2021 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00002022 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
2023 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002024 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
2025 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002026 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002027 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00002028 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002029 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08002030 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002031 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002032 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
2033 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08002034 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002035 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
2036 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08002037 run++;
2038 } else {
2039 break;
2040 }
2041 }
2042 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002043 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08002044 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002045 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002046 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08002047 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002048 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002049 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002050 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002051 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002052 }
Ian Rogersd5b32602012-02-26 16:40:04 -08002053 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002054 }
2055 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002056 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002057
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002058 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002059 os << "METHOD ROOTS\n";
2060 static_assert(arraysize(image_methods_descriptions_) ==
2061 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
2062 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
2063 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
2064 const char* description = image_methods_descriptions_[i];
2065 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002066 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002067 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002068 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002069 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002070
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002071 Runtime* const runtime = Runtime::Current();
2072 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002073 std::string image_filename = image_space_.GetImageFilename();
2074 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002075 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002076 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002077 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002078 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07002079 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002080 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
2081 }
2082 if (oat_file == nullptr) {
2083 oat_file = OatFile::Open(oat_location,
2084 oat_location,
2085 nullptr,
2086 nullptr,
2087 false,
2088 /*low_4gb*/false,
2089 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002090 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002091 }
2092 if (oat_file == nullptr) {
2093 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
2094 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07002095 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002097
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002098 stats_.oat_file_bytes = oat_file->Size();
2099
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002100 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002101
Mathieu Chartier02e25112013-08-14 16:14:24 -07002102 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002103 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07002104 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
2105 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07002106 }
2107
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002108 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002109
Jeff Haodcdc85b2015-12-04 14:06:18 -08002110 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002111 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07002112 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002113 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08002114 {
2115 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
2116 heap->FlushAllocStack();
2117 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08002118 // Since FlushAllocStack() above resets the (active) allocation
2119 // stack. Need to revoke the thread-local allocation stacks that
2120 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002121 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07002122 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002123 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002124 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002125 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002126 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01002127 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002128 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08002129 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08002130 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002131 ObjPtr<mirror::DexCache> dex_cache =
2132 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002133 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002134 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002135 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002136 }
2137 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002138 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2139 DumpObject(obj);
2140 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002141 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002142 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07002143 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002144 indent_os << "\n";
2145 // TODO: Dump fields.
2146 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08002147 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002148 image_header_.VisitPackedArtMethods(&visitor,
2149 image_space_.Begin(),
2150 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07002152 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002153 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002154 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002155 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07002156 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002157 size_t data_size = image_header_.GetDataSize(); // stored size in file.
2158 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002159 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002160 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002161 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002162 // If the image is compressed, adjust to decompressed size.
2163 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
2164 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
2165 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
2166 }
2167 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07002168 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002169 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002170 const auto& object_section = image_header_.GetObjectsSection();
2171 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002172 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002173 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2174 const auto& intern_section = image_header_.GetInternedStringsSection();
2175 const auto& class_table_section = image_header_.GetClassTableSection();
2176 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002177
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002178 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002179
2180 // Objects are kObjectAlignment-aligned.
2181 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2182 if (object_section.Offset() > header_bytes) {
2183 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2184 }
2185
2186 // Field section is 4-byte aligned.
2187 constexpr size_t kFieldSectionAlignment = 4U;
2188 uint32_t end_objects = object_section.Offset() + object_section.Size();
2189 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2190 stats_.alignment_bytes += field_section.Offset() - end_objects;
2191
2192 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2193 uint32_t end_fields = field_section.Offset() + field_section.Size();
2194 CHECK_ALIGNED(method_section.Offset(), 4);
2195 stats_.alignment_bytes += method_section.Offset() - end_fields;
2196
2197 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2198 uint32_t end_methods = method_section.Offset() + method_section.Size();
2199 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2200 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2201
2202 // Intern table is 8-byte aligned.
2203 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002204 CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t));
Andreas Gampeace0dc12016-01-20 13:33:13 -08002205 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2206
2207 // Add space between intern table and class table.
2208 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2209 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2210
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002211 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2212 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002213 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002214 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002215
Mathieu Chartiere401d142015-04-22 13:56:20 -07002216 stats_.bitmap_bytes += bitmap_section.Size();
2217 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002218 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002219 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002220 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002221 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002222 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002223 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002224
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002226
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002227 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002228 }
2229
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002230 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002231 class DumpArtMethodVisitor : public ArtMethodVisitor {
2232 public:
2233 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002235 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002236 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002237 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002238 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002239 indent_os << "\n";
2240 }
2241
2242 private:
2243 ImageDumper* const image_dumper_;
2244 };
2245
Mathieu Chartier3398c782016-09-30 10:27:43 -07002246 static void PrettyObjectValue(std::ostream& os,
2247 ObjPtr<mirror::Class> type,
2248 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002249 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002250 CHECK(type != nullptr);
2251 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002252 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002253 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002254 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002256 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002257 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002258 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002259 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002260 } else {
David Sehr709b0702016-10-13 09:12:37 -07002261 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002262 }
2263 }
2264
Mathieu Chartier3398c782016-09-30 10:27:43 -07002265 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002266 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002267 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002268 switch (field->GetTypeAsPrimitiveType()) {
2269 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002270 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002271 break;
2272 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002274 break;
2275 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002277 break;
2278 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002279 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002280 break;
2281 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002282 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002283 break;
2284 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002285 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002286 break;
2287 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002288 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002289 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002290 break;
2291 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002292 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002293 break;
2294 case Primitive::kPrimNot: {
2295 // Get the value, don't compute the type unless it is non-null as we don't want
2296 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002297 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002298 if (value == nullptr) {
2299 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002300 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002301 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002302 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002303 if (field_type != nullptr) {
2304 PrettyObjectValue(os, field_type, value);
2305 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002306 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002307 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002308 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2309 }
Ian Rogers50239c72013-06-17 14:53:22 -07002310 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002311 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002312 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002313 default:
2314 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2315 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002316 }
2317 }
2318
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002319 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002320 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002321 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002322 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002324 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002325 for (ArtField& field : klass->GetIFields()) {
2326 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002327 }
2328 }
2329
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002330 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002331 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002332 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002333
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002334 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002335 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002336 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002337 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002338 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002339 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002340 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002341 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002342 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002343 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002344 }
2345
Mathieu Chartiere401d142015-04-22 13:56:20 -07002346 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002347 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002348 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2349 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002350 return 0;
2351 }
2352 return oat_code_begin[-1];
2353 }
2354
Mathieu Chartiere401d142015-04-22 13:56:20 -07002355 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002356 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002357 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002358 if (oat_code_begin == nullptr) {
2359 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002360 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002361 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002362 }
2363
Andreas Gampe0c183382017-07-13 22:26:24 -07002364 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002365 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002366 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002367 return;
2368 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002369
2370 size_t object_bytes = obj->SizeOf();
2371 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002372 stats_.object_bytes += object_bytes;
2373 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002374
Andreas Gampe0c183382017-07-13 22:26:24 -07002375 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002376
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002377 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002378 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002379 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002381 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002382 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002383 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2384 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002385 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002386 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002387 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002388 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002389 } else {
David Sehr709b0702016-10-13 09:12:37 -07002390 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002391 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002392 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002393 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002394 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002395 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002396 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2397 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002398 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002399 size_t run = 0;
2400 for (int32_t j = i + 1; j < length; j++) {
2401 if (value == obj_array->Get(j)) {
2402 run++;
2403 } else {
2404 break;
2405 }
2406 }
2407 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002408 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002409 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002410 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002411 i = i + run;
2412 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002413 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002414 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002415 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002416 }
2417 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002418 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002419
Vladimir Marko305c38b2018-02-14 11:50:07 +00002420 if (kBitstringSubtypeCheckEnabled) {
2421 os << "SUBTYPE_CHECK_BITS: ";
2422 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2423 os << "\n";
2424 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002425
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002426 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002427 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002428 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002429 for (ArtField& field : klass->GetSFields()) {
2430 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002431 }
2432 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002433 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002434 auto it = dex_caches_.find(obj);
2435 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002436 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002437 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002438 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002439 size_t num_methods = dex_cache->NumResolvedMethods();
2440 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002441 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002442 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002443 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002444 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002445 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2446 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002447 size_t run = 0;
2448 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002449 j != length &&
2450 elem == mirror::DexCache::GetNativePairPtrSize(
2451 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002452 ++j) {
2453 ++run;
2454 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002455 if (run == 0) {
2456 os << StringPrintf("%zd: ", i);
2457 } else {
2458 os << StringPrintf("%zd to %zd: ", i, i + run);
2459 i = i + run;
2460 }
2461 std::string msg;
2462 if (elem == nullptr) {
2463 msg = "null";
2464 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002465 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002466 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002467 } else {
2468 msg = "<not in method section>";
2469 }
2470 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002471 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002472 }
2473 size_t num_fields = dex_cache->NumResolvedFields();
2474 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002475 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002476 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002477 auto* resolved_fields = dex_cache->GetResolvedFields();
2478 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002479 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002480 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002481 size_t run = 0;
2482 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002483 j != length &&
2484 elem == mirror::DexCache::GetNativePairPtrSize(
2485 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002486 ++j) {
2487 ++run;
2488 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002489 if (run == 0) {
2490 os << StringPrintf("%zd: ", i);
2491 } else {
2492 os << StringPrintf("%zd to %zd: ", i, i + run);
2493 i = i + run;
2494 }
2495 std::string msg;
2496 if (elem == nullptr) {
2497 msg = "null";
2498 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002499 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002500 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002501 } else {
2502 msg = "<not in field section>";
2503 }
2504 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002505 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002506 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002507 size_t num_types = dex_cache->NumResolvedTypes();
2508 if (num_types != 0u) {
2509 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002510 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002511 auto* resolved_types = dex_cache->GetResolvedTypes();
2512 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002513 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002514 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002515 for (size_t j = i + 1; j != num_types; ++j) {
2516 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2517 if (pair.index != other_pair.index ||
2518 pair.object.Read() != other_pair.object.Read()) {
2519 break;
2520 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002521 ++run;
2522 }
2523 if (run == 0) {
2524 os << StringPrintf("%zd: ", i);
2525 } else {
2526 os << StringPrintf("%zd to %zd: ", i, i + run);
2527 i = i + run;
2528 }
2529 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002530 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002531 if (elem == nullptr) {
2532 msg = "null";
2533 } else {
David Sehr709b0702016-10-13 09:12:37 -07002534 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002535 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002536 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002537 }
2538 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002539 }
2540 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002541 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002542 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002543 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002544
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002545 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002546 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002547 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002548 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2549 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002550 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002551 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2552 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002553 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002554 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002555 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2556 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002557 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002558 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002559 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002560 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2561 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002562 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002563 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002564 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002565 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002566 } else if (method->IsRuntimeMethod()) {
2567 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2568 if (table != nullptr) {
2569 indent_os << "IMT conflict table " << table << " method: ";
2570 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002571 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2572 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002573 }
2574 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002575 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002576 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002577 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002578 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002579
2580 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002581 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002582 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002583 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002584 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002585 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002586 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002587 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002588 }
2589
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002590 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2591 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002592 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002593 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002594 if (method->IsConstructor()) {
2595 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002596 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002597 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002598 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002599 }
2600 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002601 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002602 }
2603 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002604 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002605
Igor Murashkin7617abd2015-07-10 18:27:47 -07002606 uint32_t method_access_flags = method->GetAccessFlags();
2607
Mathieu Chartiere401d142015-04-22 13:56:20 -07002608 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002609 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2610 dex_instruction_bytes,
2611 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002612 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002613
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002614 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002615 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002616
2617 double expansion =
2618 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002619 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002620 }
2621 }
2622
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002623 std::set<const void*> already_seen_;
2624 // Compute the size of the given data within the oat file and whether this is the first time
2625 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002626 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002627 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002628 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002629 already_seen_.insert(oat_data);
2630 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002631 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002632 }
2633 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002634 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002635
2636 public:
2637 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002638 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002639 size_t file_bytes;
2640
2641 size_t header_bytes;
2642 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002643 size_t art_field_bytes;
2644 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002645 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002646 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002647 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002648 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002649 size_t alignment_bytes;
2650
2651 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002652 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002653 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002654 size_t class_initializer_code_bytes;
2655 size_t large_initializer_code_bytes;
2656 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002657
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002658 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002659
2660 size_t dex_instruction_bytes;
2661
Mathieu Chartiere401d142015-04-22 13:56:20 -07002662 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002663 std::vector<size_t> method_outlier_size;
2664 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002665 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002666
Roland Levillain3887c462015-08-12 18:15:42 +01002667 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002668 : oat_file_bytes(0),
2669 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002670 header_bytes(0),
2671 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002672 art_field_bytes(0),
2673 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002674 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002675 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002676 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002677 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002678 alignment_bytes(0),
2679 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002680 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002681 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002682 class_initializer_code_bytes(0),
2683 large_initializer_code_bytes(0),
2684 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002685 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002686 dex_instruction_bytes(0) {}
2687
Elliott Hughesa0e18062012-04-13 15:59:59 -07002688 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002689 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002690 size_t bytes;
2691 size_t count;
2692 };
2693 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2694 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002695
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002696 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002697 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2698 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002699 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002700 it->second.count += 1;
2701 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002702 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002703 }
2704 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002705
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002706 double PercentOfOatBytes(size_t size) {
2707 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2708 }
2709
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002710 double PercentOfFileBytes(size_t size) {
2711 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2712 }
2713
2714 double PercentOfObjectBytes(size_t size) {
2715 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2716 }
2717
Mathieu Chartiere401d142015-04-22 13:56:20 -07002718 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002719 method_outlier_size.push_back(total_size);
2720 method_outlier_expansion.push_back(expansion);
2721 method_outlier.push_back(method);
2722 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002723
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002724 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002725 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002726 size_t sum_of_sizes = 0;
2727 size_t sum_of_sizes_squared = 0;
2728 size_t sum_of_expansion = 0;
2729 size_t sum_of_expansion_squared = 0;
2730 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002731 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002732 return;
2733 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002734 for (size_t i = 0; i < n; i++) {
2735 size_t cur_size = method_outlier_size[i];
2736 sum_of_sizes += cur_size;
2737 sum_of_sizes_squared += cur_size * cur_size;
2738 double cur_expansion = method_outlier_expansion[i];
2739 sum_of_expansion += cur_expansion;
2740 sum_of_expansion_squared += cur_expansion * cur_expansion;
2741 }
2742 size_t size_mean = sum_of_sizes / n;
2743 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2744 double expansion_mean = sum_of_expansion / n;
2745 double expansion_variance =
2746 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2747
2748 // Dump methods whose size is a certain number of standard deviations from the mean
2749 size_t dumped_values = 0;
2750 size_t skipped_values = 0;
2751 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2752 size_t cur_size_variance = i * i * size_variance;
2753 bool first = true;
2754 for (size_t j = 0; j < n; j++) {
2755 size_t cur_size = method_outlier_size[j];
2756 if (cur_size > size_mean) {
2757 size_t cur_var = cur_size - size_mean;
2758 cur_var = cur_var * cur_var;
2759 if (cur_var > cur_size_variance) {
2760 if (dumped_values > 20) {
2761 if (i == 1) {
2762 skipped_values++;
2763 } else {
2764 i = 2; // jump to counting for 1 standard deviation
2765 break;
2766 }
2767 } else {
2768 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002769 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002770 first = false;
2771 }
David Sehr709b0702016-10-13 09:12:37 -07002772 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002773 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002774 method_outlier_size[j] = 0; // don't consider this method again
2775 dumped_values++;
2776 }
2777 }
2778 }
2779 }
2780 }
2781 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002782 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002783 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002784 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002785 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002786
2787 // Dump methods whose expansion is a certain number of standard deviations from the mean
2788 dumped_values = 0;
2789 skipped_values = 0;
2790 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2791 double cur_expansion_variance = i * i * expansion_variance;
2792 bool first = true;
2793 for (size_t j = 0; j < n; j++) {
2794 double cur_expansion = method_outlier_expansion[j];
2795 if (cur_expansion > expansion_mean) {
2796 size_t cur_var = cur_expansion - expansion_mean;
2797 cur_var = cur_var * cur_var;
2798 if (cur_var > cur_expansion_variance) {
2799 if (dumped_values > 20) {
2800 if (i == 1) {
2801 skipped_values++;
2802 } else {
2803 i = 2; // jump to counting for 1 standard deviation
2804 break;
2805 }
2806 } else {
2807 if (first) {
2808 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002809 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002810 first = false;
2811 }
David Sehr709b0702016-10-13 09:12:37 -07002812 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002813 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002814 method_outlier_expansion[j] = 0.0; // don't consider this method again
2815 dumped_values++;
2816 }
2817 }
2818 }
2819 }
2820 }
2821 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002822 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002823 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002824 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002825 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002826 }
2827
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002828 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002829 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002830 {
2831 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2832 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002833 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2834 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2835 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2836 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2837 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2838 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002839 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002840 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2841 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002842 header_bytes, PercentOfFileBytes(header_bytes),
2843 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002844 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2845 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002846 dex_cache_arrays_bytes,
2847 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002848 interned_strings_bytes,
2849 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002850 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002851 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002852 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2853 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002854 CHECK_EQ(file_bytes,
2855 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2856 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2857 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002858 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002859
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002860 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002861 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002862 for (const auto& sizes_and_count : sizes_and_counts) {
2863 const std::string& descriptor(sizes_and_count.first);
2864 double average = static_cast<double>(sizes_and_count.second.bytes) /
2865 static_cast<double>(sizes_and_count.second.count);
2866 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002867 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002868 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002869 descriptor.c_str(), sizes_and_count.second.bytes,
2870 sizes_and_count.second.count, average, percent);
2871 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002872 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002873 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002874 CHECK_EQ(object_bytes, object_bytes_total);
2875
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002876 os << StringPrintf("oat_file_bytes = %8zd\n"
2877 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002878 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2879 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2880 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2881 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002882 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002883 managed_code_bytes,
2884 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002885 native_to_managed_code_bytes,
2886 PercentOfOatBytes(native_to_managed_code_bytes),
2887 class_initializer_code_bytes,
2888 PercentOfOatBytes(class_initializer_code_bytes),
2889 large_initializer_code_bytes,
2890 PercentOfOatBytes(large_initializer_code_bytes),
2891 large_method_code_bytes,
2892 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002893 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002894 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002895 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002896 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2897 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002898 }
2899
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002900 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002901 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002902 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002903
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002904 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2905 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002906 static_cast<double>(managed_code_bytes) /
2907 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002908 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002909 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002910 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002911
2912 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002913 }
2914 } stats_;
2915
2916 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002917 enum {
2918 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2919 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2920 kLargeConstructorDexBytes = 4000,
2921 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2922 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2923 kLargeMethodDexBytes = 16000
2924 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002925
2926 // For performance, use the *os_ directly for anything that doesn't need indentation
2927 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002928 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002929 VariableIndentationOutputStream vios_;
2930 ScopedIndentation indent1_;
2931
Ian Rogers1d54e732013-05-02 21:10:01 -07002932 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002933 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002934 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002935 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002936 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002937
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002938 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002939};
2940
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002941static int DumpImage(gc::space::ImageSpace* image_space,
2942 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002943 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002944 const ImageHeader& image_header = image_space->GetImageHeader();
2945 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002946 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002947 return EXIT_FAILURE;
2948 }
2949 ImageDumper image_dumper(os, *image_space, image_header, options);
2950 if (!image_dumper.Dump()) {
2951 return EXIT_FAILURE;
2952 }
2953 return EXIT_SUCCESS;
2954}
2955
2956static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002957 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002958 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002959 options->class_loader_ = &null_class_loader;
2960
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002961 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002962 if (options->app_image_ != nullptr) {
2963 if (options->app_oat_ == nullptr) {
2964 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002965 return EXIT_FAILURE;
2966 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002967 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2968 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2969 // pointers into 32 bit pointer sized ArtMethods.
2970 std::string error_msg;
2971 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2972 options->app_oat_,
2973 nullptr,
2974 nullptr,
2975 false,
2976 /*low_4gb*/true,
2977 nullptr,
2978 &error_msg));
2979 if (oat_file == nullptr) {
2980 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002981 return EXIT_FAILURE;
2982 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002983 std::unique_ptr<gc::space::ImageSpace> space(
2984 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2985 if (space == nullptr) {
2986 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2987 << error_msg;
2988 }
2989 // Open dex files for the image.
2990 std::vector<std::unique_ptr<const DexFile>> dex_files;
2991 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2992 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2993 << error_msg;
2994 }
2995 // Dump the actual image.
2996 int result = DumpImage(space.get(), options, os);
2997 if (result != EXIT_SUCCESS) {
2998 return result;
2999 }
3000 // Fall through to dump the boot images.
3001 }
3002
3003 gc::Heap* heap = runtime->GetHeap();
3004 CHECK(heap->HasBootImageSpace()) << "No image spaces";
3005 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
3006 int result = DumpImage(image_space, options, os);
3007 if (result != EXIT_SUCCESS) {
3008 return result;
3009 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07003010 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08003011 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07003012}
3013
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003014static jobject InstallOatFile(Runtime* runtime,
3015 std::unique_ptr<OatFile> oat_file,
3016 std::vector<const DexFile*>* class_path)
3017 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003018 Thread* self = Thread::Current();
3019 CHECK(self != nullptr);
3020 // Need well-known-classes.
3021 WellKnownClasses::Init(self->GetJniEnv());
3022
Vladimir Marko421087b2018-02-27 11:00:17 +00003023 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003024 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07003025 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003026 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
3027 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003028 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07003029 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003030 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003031 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003032 }
3033
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003034 // Need a class loader. Fake that we're a compiler.
3035 // Note: this will run initializers through the unstarted runtime, so make sure it's
3036 // initialized.
3037 interpreter::UnstartedRuntime::Initialize();
3038
3039 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
3040
Vladimir Marko421087b2018-02-27 11:00:17 +00003041 // Need to register dex files to get a working dex cache.
3042 for (const DexFile* dex_file : *class_path) {
3043 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
3044 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
3045 CHECK(dex_cache != nullptr);
3046 }
3047
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003048 return class_loader;
3049}
3050
3051static int DumpOatWithRuntime(Runtime* runtime,
3052 std::unique_ptr<OatFile> oat_file,
3053 OatDumperOptions* options,
3054 std::ostream* os) {
3055 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
3056 ScopedObjectAccess soa(Thread::Current());
3057
3058 OatFile* oat_file_ptr = oat_file.get();
3059 std::vector<const DexFile*> class_path;
3060 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003061
3062 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003063 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07003064 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07003065 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003066 options->class_loader_ = &loader_handle;
3067
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003068 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003069 bool success = oat_dumper.Dump(*os);
3070 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3071}
3072
3073static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003074 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003075 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08003076 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003077 options->class_loader_ = &null_class_loader;
3078
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003079 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003080 bool success = oat_dumper.Dump(*os);
3081 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3082}
3083
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003084static int DumpOat(Runtime* runtime,
3085 const char* oat_filename,
3086 const char* dex_filename,
3087 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07003088 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003089 if (dex_filename == nullptr) {
3090 LOG(WARNING) << "No dex filename provided, "
3091 << "oatdump might fail if the oat file does not contain the dex code.";
3092 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003093 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003094 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
3095 oat_filename,
3096 nullptr,
3097 nullptr,
3098 false,
3099 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003100 dex_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003101 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003102 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003103 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003104 return EXIT_FAILURE;
3105 }
3106
3107 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003108 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003109 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003110 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003111 }
3112}
3113
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003114static int SymbolizeOat(const char* oat_filename,
3115 const char* dex_filename,
3116 std::string& output_name,
3117 bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003118 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07003119 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
3120 oat_filename,
3121 nullptr,
3122 nullptr,
3123 false,
3124 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003125 dex_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07003126 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003127 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003128 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003129 return EXIT_FAILURE;
3130 }
3131
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003132 bool result;
3133 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
3134 // files for 64-bit code in the past.
3135 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003136 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003137 result = oat_symbolizer.Symbolize();
3138 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003139 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003140 result = oat_symbolizer.Symbolize();
3141 }
3142 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003143 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003144 return EXIT_FAILURE;
3145 }
3146
3147 return EXIT_SUCCESS;
3148}
3149
Andreas Gampe9fded872016-09-25 16:08:35 -07003150class IMTDumper {
3151 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003152 static bool Dump(Runtime* runtime,
3153 const std::string& imt_file,
3154 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003155 const char* oat_filename,
3156 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003157 Thread* self = Thread::Current();
3158
3159 ScopedObjectAccess soa(self);
3160 StackHandleScope<1> scope(self);
3161 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
3162 std::vector<const DexFile*> class_path;
3163
3164 if (oat_filename != nullptr) {
3165 std::string error_msg;
3166 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
3167 oat_filename,
3168 nullptr,
3169 nullptr,
3170 false,
3171 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003172 dex_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003173 &error_msg));
3174 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003175 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003176 return false;
3177 }
3178
3179 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
3180 InstallOatFile(runtime, std::move(oat_file), &class_path)));
3181 } else {
3182 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
3183 class_path = runtime->GetClassLinker()->GetBootClassPath();
3184 }
3185
3186 if (!imt_file.empty()) {
3187 return DumpImt(runtime, imt_file, class_loader);
3188 }
3189
3190 if (dump_imt_stats) {
3191 return DumpImtStats(runtime, class_path, class_loader);
3192 }
3193
3194 LOG(FATAL) << "Should not reach here";
3195 UNREACHABLE();
3196 }
3197
3198 private:
3199 static bool DumpImt(Runtime* runtime,
3200 const std::string& imt_file,
3201 Handle<mirror::ClassLoader> h_class_loader)
3202 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003203 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3204 std::unordered_set<std::string> prepared;
3205
3206 for (const std::string& line : lines) {
3207 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3208 // or a class descriptor and an interface method, in which case we will lookup the method,
3209 // determine its IMT slot, and check the class' IMT.
3210 size_t first_space = line.find(' ');
3211 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003212 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003213 } else {
3214 DumpIMTForMethod(runtime,
3215 line.substr(0, first_space),
3216 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003217 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003218 &prepared);
3219 }
3220 std::cerr << std::endl;
3221 }
3222
3223 return true;
3224 }
3225
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003226 static bool DumpImtStats(Runtime* runtime,
3227 const std::vector<const DexFile*>& dex_files,
3228 Handle<mirror::ClassLoader> h_class_loader)
3229 REQUIRES_SHARED(Locks::mutator_lock_) {
3230 size_t without_imt = 0;
3231 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003232 std::map<size_t, size_t> histogram;
3233
3234 ClassLinker* class_linker = runtime->GetClassLinker();
3235 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3236 std::unordered_set<std::string> prepared;
3237
3238 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003239 StackHandleScope<1> scope(self);
3240 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3241
3242 for (const DexFile* dex_file : dex_files) {
3243 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003244 class_def_index != dex_file->NumClassDefs();
3245 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003246 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3247 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003248 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003249 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003250 std::cerr << "Warning: could not load " << descriptor << std::endl;
3251 continue;
3252 }
3253
3254 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003255 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003256 continue;
3257 }
3258
3259 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3260 if (im_table == nullptr) {
3261 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003262 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003263 continue;
3264 }
3265
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003266 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003267 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3268 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3269 if (ptr->IsRuntimeMethod()) {
3270 if (ptr->IsImtUnimplementedMethod()) {
3271 histogram[0]++;
3272 } else {
3273 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3274 histogram[current_table->NumEntries(pointer_size)]++;
3275 }
3276 } else {
3277 histogram[1]++;
3278 }
3279 }
3280 }
3281 }
3282
3283 std::cerr << "IMT stats:"
3284 << std::endl << std::endl;
3285
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003286 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003287 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003288 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003289 << std::endl << std::endl;
3290
3291 double sum_one = 0;
3292 size_t count_one = 0;
3293
3294 std::cerr << " " << "IMT histogram" << std::endl;
3295 for (auto& bucket : histogram) {
3296 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3297 if (bucket.first > 0) {
3298 sum_one += bucket.second * bucket.first;
3299 count_one += bucket.second;
3300 }
3301 }
3302
3303 double count_zero = count_one + histogram[0];
3304 std::cerr << " Stats:" << std::endl;
3305 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3306 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3307
3308 return true;
3309 }
3310
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003311 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003312 static bool HasNoIMT(Runtime* runtime,
3313 Handle<mirror::Class> klass,
3314 const PointerSize pointer_size,
3315 std::unordered_set<std::string>* prepared)
3316 REQUIRES_SHARED(Locks::mutator_lock_) {
3317 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3318 return true;
3319 }
3320
3321 if (klass->GetImt(pointer_size) == nullptr) {
3322 PrepareClass(runtime, klass, prepared);
3323 }
3324
3325 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3326 DCHECK(object_class->IsObjectClass());
3327
3328 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3329
Mathieu Chartier6beced42016-11-15 15:51:31 -08003330 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003331 DCHECK(result);
3332 }
3333
3334 return result;
3335 }
3336
3337 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3338 REQUIRES_SHARED(Locks::mutator_lock_) {
3339 if (table == nullptr) {
3340 std::cerr << " <No IMT?>" << std::endl;
3341 return;
3342 }
3343 size_t table_index = 0;
3344 for (;;) {
3345 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3346 if (ptr == nullptr) {
3347 return;
3348 }
3349 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003350 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003351 }
3352 }
3353
3354 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3355 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003356 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003357 const std::string& class_name,
3358 const PointerSize pointer_size,
3359 mirror::Class** klass_out,
3360 std::unordered_set<std::string>* prepared)
3361 REQUIRES_SHARED(Locks::mutator_lock_) {
3362 if (class_name.empty()) {
3363 return nullptr;
3364 }
3365
3366 std::string descriptor;
3367 if (class_name[0] == 'L') {
3368 descriptor = class_name;
3369 } else {
3370 descriptor = DotToDescriptor(class_name.c_str());
3371 }
3372
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003373 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003374
3375 if (klass == nullptr) {
3376 self->ClearException();
3377 std::cerr << "Did not find " << class_name << std::endl;
3378 *klass_out = nullptr;
3379 return nullptr;
3380 }
3381
3382 StackHandleScope<1> scope(Thread::Current());
3383 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3384
3385 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3386 *klass_out = h_klass.Get();
3387 return ret;
3388 }
3389
3390 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3391 Handle<mirror::Class> h_klass,
3392 const PointerSize pointer_size,
3393 std::unordered_set<std::string>* prepared)
3394 REQUIRES_SHARED(Locks::mutator_lock_) {
3395 PrepareClass(runtime, h_klass, prepared);
3396 return h_klass->GetImt(pointer_size);
3397 }
3398
3399 static void DumpIMTForClass(Runtime* runtime,
3400 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003401 Handle<mirror::ClassLoader> h_loader,
3402 std::unordered_set<std::string>* prepared)
3403 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003404 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3405 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003406 ImTable* imt = PrepareAndGetImTable(runtime,
3407 Thread::Current(),
3408 h_loader,
3409 class_name,
3410 pointer_size,
3411 &klass,
3412 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003413 if (imt == nullptr) {
3414 return;
3415 }
3416
3417 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3418 for (size_t index = 0; index < ImTable::kSize; ++index) {
3419 std::cerr << " " << index << ":" << std::endl;
3420 ArtMethod* ptr = imt->Get(index, pointer_size);
3421 if (ptr->IsRuntimeMethod()) {
3422 if (ptr->IsImtUnimplementedMethod()) {
3423 std::cerr << " <empty>" << std::endl;
3424 } else {
3425 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3426 PrintTable(current_table, pointer_size);
3427 }
3428 } else {
David Sehr709b0702016-10-13 09:12:37 -07003429 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003430 }
3431 }
3432
3433 std::cerr << " Interfaces:" << std::endl;
3434 // Run through iftable, find methods that slot here, see if they fit.
3435 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003436 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3437 mirror::Class* iface = if_table->GetInterface(i);
3438 std::string iface_name;
3439 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003440
Mathieu Chartier6beced42016-11-15 15:51:31 -08003441 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3442 uint32_t class_hash, name_hash, signature_hash;
3443 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3444 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3445 std::cerr << " " << iface_method.PrettyMethod(true)
3446 << " slot=" << imt_slot
3447 << std::hex
3448 << " class_hash=0x" << class_hash
3449 << " name_hash=0x" << name_hash
3450 << " signature_hash=0x" << signature_hash
3451 << std::dec
3452 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003453 }
3454 }
3455 }
3456
3457 static void DumpIMTForMethod(Runtime* runtime,
3458 const std::string& class_name,
3459 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003460 Handle<mirror::ClassLoader> h_loader,
3461 std::unordered_set<std::string>* prepared)
3462 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003463 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3464 mirror::Class* klass;
3465 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003466 Thread::Current(),
3467 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003468 class_name,
3469 pointer_size,
3470 &klass,
3471 prepared);
3472 if (imt == nullptr) {
3473 return;
3474 }
3475
3476 std::cerr << class_name << " <" << method << ">" << std::endl;
3477 for (size_t index = 0; index < ImTable::kSize; ++index) {
3478 ArtMethod* ptr = imt->Get(index, pointer_size);
3479 if (ptr->IsRuntimeMethod()) {
3480 if (ptr->IsImtUnimplementedMethod()) {
3481 continue;
3482 }
3483
3484 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3485 if (current_table == nullptr) {
3486 continue;
3487 }
3488
3489 size_t table_index = 0;
3490 for (;;) {
3491 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3492 if (ptr2 == nullptr) {
3493 break;
3494 }
3495 table_index++;
3496
David Sehr709b0702016-10-13 09:12:37 -07003497 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003498 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003499 std::cerr << " Slot "
3500 << index
3501 << " ("
3502 << current_table->NumEntries(pointer_size)
3503 << ")"
3504 << std::endl;
3505 PrintTable(current_table, pointer_size);
3506 return;
3507 }
3508 }
3509 } else {
David Sehr709b0702016-10-13 09:12:37 -07003510 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003511 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003512 std::cerr << " Slot " << index << " (1)" << std::endl;
3513 std::cerr << " " << p_name << std::endl;
3514 } else {
3515 // Run through iftable, find methods that slot here, see if they fit.
3516 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003517 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3518 mirror::Class* iface = if_table->GetInterface(i);
3519 size_t num_methods = iface->NumDeclaredVirtualMethods();
3520 if (num_methods > 0) {
3521 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3522 if (ImTable::GetImtIndex(&iface_method) == index) {
3523 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003524 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003525 std::cerr << " Slot " << index << " (1)" << std::endl;
3526 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003527 }
3528 }
3529 }
3530 }
3531 }
3532 }
3533 }
3534 }
3535 }
3536
3537 // Read lines from the given stream, dropping comments and empty lines
3538 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3539 std::vector<std::string> output;
3540 while (in_stream.good()) {
3541 std::string dot;
3542 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003543 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003544 continue;
3545 }
3546 output.push_back(dot);
3547 }
3548 return output;
3549 }
3550
3551 // Read lines from the given file, dropping comments and empty lines.
3552 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3553 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3554 if (input_file.get() == nullptr) {
3555 LOG(ERROR) << "Failed to open input file " << input_filename;
3556 return std::vector<std::string>();
3557 }
3558 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3559 input_file->close();
3560 return result;
3561 }
3562
3563 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3564 // and note in the given set that the work was done.
3565 static void PrepareClass(Runtime* runtime,
3566 Handle<mirror::Class> h_klass,
3567 std::unordered_set<std::string>* done)
3568 REQUIRES_SHARED(Locks::mutator_lock_) {
3569 if (!h_klass->ShouldHaveImt()) {
3570 return;
3571 }
3572
3573 std::string name;
3574 name = h_klass->GetDescriptor(&name);
3575
3576 if (done->find(name) != done->end()) {
3577 return;
3578 }
3579 done->insert(name);
3580
3581 if (h_klass->HasSuperClass()) {
3582 StackHandleScope<1> h(Thread::Current());
3583 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3584 }
3585
3586 if (!h_klass->IsTemp()) {
3587 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3588 }
3589 }
3590};
3591
Igor Murashkin37743352014-11-13 14:38:00 -08003592struct OatdumpArgs : public CmdlineArgs {
3593 protected:
3594 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003595
Igor Murashkin37743352014-11-13 14:38:00 -08003596 virtual ParseStatus ParseCustom(const StringPiece& option,
3597 std::string* error_msg) OVERRIDE {
3598 {
3599 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3600 if (base_parse != kParseUnknownArgument) {
3601 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003602 }
3603 }
3604
Igor Murashkin37743352014-11-13 14:38:00 -08003605 if (option.starts_with("--oat-file=")) {
3606 oat_filename_ = option.substr(strlen("--oat-file=")).data();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003607 } else if (option.starts_with("--dex-file=")) {
3608 dex_filename_ = option.substr(strlen("--dex-file=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003609 } else if (option.starts_with("--image=")) {
3610 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003611 } else if (option == "--no-dump:vmap") {
3612 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003613 } else if (option =="--dump:code_info_stack_maps") {
3614 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003615 } else if (option == "--no-disassemble") {
3616 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003617 } else if (option =="--header-only") {
3618 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003619 } else if (option.starts_with("--symbolize=")) {
3620 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3621 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003622 } else if (option.starts_with("--only-keep-debug")) {
3623 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003624 } else if (option.starts_with("--class-filter=")) {
3625 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003626 } else if (option.starts_with("--method-filter=")) {
3627 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003628 } else if (option.starts_with("--list-classes")) {
3629 list_classes_ = true;
3630 } else if (option.starts_with("--list-methods")) {
3631 list_methods_ = true;
3632 } else if (option.starts_with("--export-dex-to=")) {
3633 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3634 } else if (option.starts_with("--addr2instr=")) {
3635 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3636 *error_msg = "Address conversion failed";
3637 return kParseError;
3638 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003639 } else if (option.starts_with("--app-image=")) {
3640 app_image_ = option.substr(strlen("--app-image=")).data();
3641 } else if (option.starts_with("--app-oat=")) {
3642 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003643 } else if (option.starts_with("--dump-imt=")) {
3644 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3645 } else if (option == "--dump-imt-stats") {
3646 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003647 } else {
3648 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003649 }
3650
Igor Murashkin37743352014-11-13 14:38:00 -08003651 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003652 }
3653
Igor Murashkin37743352014-11-13 14:38:00 -08003654 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3655 // Infer boot image location from the image location if possible.
3656 if (boot_image_location_ == nullptr) {
3657 boot_image_location_ = image_location_;
3658 }
3659
3660 // Perform the parent checks.
3661 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3662 if (parent_checks != kParseOk) {
3663 return parent_checks;
3664 }
3665
3666 // Perform our own checks.
3667 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3668 *error_msg = "Either --image or --oat-file must be specified";
3669 return kParseError;
3670 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3671 *error_msg = "Either --image or --oat-file must be specified but not both";
3672 return kParseError;
3673 }
3674
3675 return kParseOk;
3676 }
3677
3678 virtual std::string GetUsage() const {
3679 std::string usage;
3680
3681 usage +=
3682 "Usage: oatdump [options] ...\n"
3683 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3684 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3685 "\n"
3686 // Either oat-file or image is required.
3687 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3688 " Example: --oat-file=/system/framework/boot.oat\n"
3689 "\n"
3690 " --image=<file.art>: specifies an input image location.\n"
3691 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003692 "\n"
3693 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003694 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003695 " Example: --app-image=app.art\n"
3696 "\n"
3697 " --app-oat=<file.odex>: specifies an input app oat.\n"
3698 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003699 "\n";
3700
3701 usage += Base::GetUsage();
3702
3703 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003704 " --no-dump:vmap may be used to disable vmap dumping.\n"
3705 " Example: --no-dump:vmap\n"
3706 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003707 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3708 " Example: --dump:code_info_stack_maps\n"
3709 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003710 " --no-disassemble may be used to disable disassembly.\n"
3711 " Example: --no-disassemble\n"
3712 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003713 " --header-only may be used to print only the oat header.\n"
3714 " Example: --header-only\n"
3715 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003716 " --list-classes may be used to list target file classes (can be used with filters).\n"
3717 " Example: --list-classes\n"
3718 " Example: --list-classes --class-filter=com.example.foo\n"
3719 "\n"
3720 " --list-methods may be used to list target file methods (can be used with filters).\n"
3721 " Example: --list-methods\n"
3722 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3723 "\n"
3724 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3725 " Example: --symbolize=/system/framework/boot.oat\n"
3726 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003727 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3728 " .rodata and .text sections are omitted in the output file to save space.\n"
3729 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3730 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003731 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3732 " Example: --class-filter=com.example.foo\n"
3733 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003734 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3735 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003736 "\n"
3737 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3738 " Example: --export-dex-to=/data/local/tmp\n"
3739 "\n"
3740 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3741 " address (e.g. PC from crash dump)\n"
3742 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003743 "\n"
3744 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3745 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003746 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003747 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003748 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003749 " Example: --dump-imt=imt.txt\n"
3750 "\n"
3751 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3752 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003753 "\n";
3754
3755 return usage;
3756 }
3757
3758 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003759 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003760 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003761 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003762 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003763 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003764 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003765 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003766 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003767 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003768 bool disassemble_code_ = true;
3769 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003770 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003771 bool list_classes_ = false;
3772 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003773 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003774 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003775 uint32_t addr2instr_ = 0;
3776 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003777 const char* app_image_ = nullptr;
3778 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003779};
3780
Igor Murashkin37743352014-11-13 14:38:00 -08003781struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3782 virtual bool NeedsRuntime() OVERRIDE {
3783 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003784
Igor Murashkin37743352014-11-13 14:38:00 -08003785 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3786 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3787
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003788 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003789 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003790 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003791 args_->disassemble_code_,
3792 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003793 args_->class_filter_,
3794 args_->method_filter_,
3795 args_->list_classes_,
3796 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003797 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003798 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003799 args_->app_image_,
3800 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003801 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003802
Andreas Gampe9fded872016-09-25 16:08:35 -07003803 return (args_->boot_image_location_ != nullptr ||
3804 args_->image_location_ != nullptr ||
3805 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003806 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003807 }
3808
Igor Murashkin37743352014-11-13 14:38:00 -08003809 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3810 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003811 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003812
Mathieu Chartierd424d082014-10-15 10:31:46 -07003813 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003814
Andreas Gampec24f3992014-12-17 20:40:11 -08003815 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003816 // ELF has special kind of section called SHT_NOBITS which allows us to create
3817 // sections which exist but their data is omitted from the ELF file to save space.
3818 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3819 // with only debug data. We use it in similar way to exclude .rodata and .text.
3820 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003821 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3822 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003823 } else {
3824 return DumpOat(nullptr,
3825 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003826 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003827 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003828 args_->os_) == EXIT_SUCCESS;
3829 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003830 }
3831
Igor Murashkin37743352014-11-13 14:38:00 -08003832 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3833 CHECK(args_ != nullptr);
3834
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003835 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3836 return IMTDumper::Dump(runtime,
3837 args_->imt_dump_,
3838 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003839 args_->oat_filename_,
3840 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003841 }
3842
Igor Murashkin37743352014-11-13 14:38:00 -08003843 if (args_->oat_filename_ != nullptr) {
3844 return DumpOat(runtime,
3845 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003846 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003847 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003848 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003849 }
Igor Murashkin37743352014-11-13 14:38:00 -08003850
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003851 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003852 }
3853
Igor Murashkin37743352014-11-13 14:38:00 -08003854 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3855};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003856
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003857} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003858
3859int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003860 // Output all logging to stderr.
3861 android::base::SetLogger(android::base::StderrLogger);
3862
Igor Murashkin37743352014-11-13 14:38:00 -08003863 art::OatdumpMain main;
3864 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003865}