blob: 7b11258a8999ecb69cb7023c33ec9604d4cc852c [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 Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
31
Ian Rogersd582fa42014-11-05 23:46:43 -080032#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070033#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000035#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080036#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070038#include "class_linker.h"
David Srbecky5d950762016-03-07 20:47:29 +000039#include "debug/elf_debug_writer.h"
40#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070041#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070042#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080043#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070044#include "elf_builder.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070045#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/space/image_space.h"
47#include "gc/space/large_object_space.h"
48#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080049#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070050#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080051#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070052#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000053#include "linker/buffered_output_stream.h"
54#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "mirror/array-inl.h"
56#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010057#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058#include "mirror/object-inl.h"
59#include "mirror/object_array-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070060#include "nativehelper/ScopedLocalRef.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010062#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070063#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080064#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070065#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070066#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070067#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070068#include "stack_map.h"
69#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080070#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070071#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010072#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080073#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000074#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070075#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070076
Igor Murashkin37743352014-11-13 14:38:00 -080077#include <sys/stat.h>
78#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070079
Igor Murashkin37743352014-11-13 14:38:00 -080080namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070081
Andreas Gampe46ee31b2016-12-14 10:11:49 -080082using android::base::StringPrintf;
83
Mathieu Chartiere401d142015-04-22 13:56:20 -070084const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080085 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070086 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070087 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010088 "kSaveAllCalleeSavesMethod",
89 "kSaveRefsOnlyMethod",
90 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010091 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -070092 "kSaveEverythingMethodForClinit",
93 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -070094};
95
96const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070097 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070098 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000099 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700100};
101
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700102// Map is so that we don't allocate multiple dex files for the same OatDexFile.
103static std::map<const OatFile::OatDexFile*,
104 std::unique_ptr<const DexFile>> opened_dex_files;
105
106const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
107 DCHECK(oat_dex_file != nullptr);
108 auto it = opened_dex_files.find(oat_dex_file);
109 if (it != opened_dex_files.end()) {
110 return it->second.get();
111 }
112 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
113 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
114 return ret;
115}
116
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800117template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100118class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700119 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000120 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
121 oat_file_(oat_file),
122 builder_(nullptr),
123 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
124 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700125 }
126
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700127 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800129 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000130 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000131
Andreas Gampe08c277c2017-04-26 22:22:15 -0700132 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
133 if (elf_file == nullptr) {
134 return false;
135 }
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700136 std::unique_ptr<BufferedOutputStream> output_stream =
137 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file.get()));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800138 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139
140 builder_->Start();
141
142 auto* rodata = builder_->GetRoData();
143 auto* text = builder_->GetText();
144 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145
David Srbecky6d8c8f02015-10-26 10:57:09 +0000146 const uint8_t* rodata_begin = oat_file_->Begin();
147 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000148 if (no_bits_) {
149 rodata->WriteNoBitsSection(rodata_size);
150 } else {
151 rodata->Start();
152 rodata->WriteFully(rodata_begin, rodata_size);
153 rodata->End();
154 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155
David Srbecky6d8c8f02015-10-26 10:57:09 +0000156 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
157 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000158 if (no_bits_) {
159 text->WriteNoBitsSection(text_size);
160 } else {
161 text->Start();
162 text->WriteFully(text_begin, text_size);
163 text->End();
164 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165
166 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000167 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000168 }
169
Douglas Leung316a2182015-09-17 15:26:25 -0700170 if (isa == kMips || isa == kMips64) {
171 builder_->WriteMIPSabiflagsSection();
172 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000173 builder_->PrepareDynamicSection(elf_file->GetPath(),
174 rodata_size,
175 text_size,
176 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100177 oat_file_->BssMethodsOffset(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000178 oat_file_->BssRootsOffset());
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 = {}; \
185 info.trampoline_name = #fn_name; \
186 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 Srbecky5d950762016-03-07 20:47:29 +0000208 debug::WriteDebugInfo(builder_.get(),
209 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
210 dwarf::DW_DEBUG_FRAME_FORMAT,
211 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700212
David Srbecky6d8c8f02015-10-26 10:57:09 +0000213 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700214
Andreas Gampe08c277c2017-04-26 22:22:15 -0700215 bool ret_value = builder_->Good();
216
217 builder_.reset();
218 output_stream.reset();
219
220 if (elf_file->FlushCloseOrErase() != 0) {
221 return false;
222 }
223 elf_file.reset();
224
225 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700226 }
227
David Srbecky5d950762016-03-07 20:47:29 +0000228 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700229 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
230 for (size_t i = 0; i < oat_dex_files.size(); i++) {
231 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700232 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000233 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700234 }
235 }
236
David Srbecky5d950762016-03-07 20:47:29 +0000237 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700239 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
240 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 return;
242 }
243 for (size_t class_def_index = 0;
244 class_def_index < dex_file->NumClassDefs();
245 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
247 OatClassType type = oat_class.GetType();
248 switch (type) {
249 case kOatClassAllCompiled:
250 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000251 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700252 break;
253
254 case kOatClassNoneCompiled:
255 case kOatClassMax:
256 // Ignore.
257 break;
258 }
259 }
260 }
261
David Srbecky5d950762016-03-07 20:47:29 +0000262 void WalkOatClass(const OatFile::OatClass& oat_class,
263 const DexFile& dex_file,
264 uint32_t class_def_index) {
265 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700266 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700267 if (class_data == nullptr) { // empty class such as a marker interface?
268 return;
269 }
270 // Note: even if this is an interface or a native class, we still have to walk it, as there
271 // might be a static initializer.
272 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700274 it.SkipAllFields();
David Srbecky5d950762016-03-07 20:47:29 +0000275 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
276 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
277 dex_file,
278 class_def_index,
279 it.GetMemberIndex(),
280 it.GetMethodCodeItem(),
281 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 }
283 DCHECK(!it.HasNext());
284 }
285
David Srbecky5d950762016-03-07 20:47:29 +0000286 void WalkOatMethod(const OatFile::OatMethod& oat_method,
287 const DexFile& dex_file,
288 uint32_t class_def_index,
289 uint32_t dex_method_index,
290 const DexFile::CodeItem* code_item,
291 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700292 if ((method_access_flags & kAccAbstract) != 0) {
293 // Abstract method, no code.
294 return;
295 }
David Srbecky5d950762016-03-07 20:47:29 +0000296 const OatHeader& oat_header = oat_file_->GetOatHeader();
297 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
298 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700299 // No code.
300 return;
301 }
302
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100303 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
304 // Clear Thumb2 bit.
305 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
306
Vladimir Marko1b404a82017-09-01 13:35:26 +0100307 debug::MethodDebugInfo info = {};
308 DCHECK(info.trampoline_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000309 info.dex_file = &dex_file;
310 info.class_def_index = class_def_index;
311 info.dex_method_index = dex_method_index;
312 info.access_flags = method_access_flags;
313 info.code_item = code_item;
314 info.isa = oat_header.GetInstructionSet();
315 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
316 info.is_native_debuggable = oat_header.IsNativeDebuggable();
317 info.is_optimized = method_header->IsOptimized();
318 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100319 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000320 info.code_size = method_header->GetCodeSize();
321 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
322 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
323 info.cfi = ArrayRef<uint8_t>();
324 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700325 }
326
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700327 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700328 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800329 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000330 std::vector<debug::MethodDebugInfo> method_debug_infos_;
331 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700332 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000333 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700334};
335
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700336class OatDumperOptions {
337 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100338 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100339 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700340 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700341 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800342 const char* class_filter,
343 const char* method_filter,
344 bool list_classes,
345 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000346 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800347 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800348 const char* app_image,
349 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800350 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100351 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100352 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700353 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700354 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000356 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800357 list_classes_(list_classes),
358 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000359 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800361 app_image_(app_image),
362 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800363 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800364 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100367 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 const bool disassemble_code_;
369 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000371 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800372 const bool list_classes_;
373 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000374 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800376 const char* const app_image_;
377 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700379 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700380};
381
Elliott Hughese3c845c2012-02-28 17:23:01 -0800382class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700383 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100384 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000385 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800386 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700387 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800388 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800389 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
390 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700391 new DisassemblerOptions(
392 options_.absolute_addresses_,
393 oat_file.Begin(),
394 oat_file.End(),
395 true /* can_read_literals_ */,
396 Is64BitInstructionSet(instruction_set_)
397 ? &Thread::DumpThreadOffset<PointerSize::k64>
398 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800399 CHECK(options_.class_loader_ != nullptr);
400 CHECK(options_.class_filter_ != nullptr);
401 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800402 AddAllOffsets();
403 }
404
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700405 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700406 delete disassembler_;
407 }
408
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800409 InstructionSet GetInstructionSet() {
410 return instruction_set_;
411 }
412
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700413 bool Dump(std::ostream& os) {
414 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
417 os << "MAGIC:\n";
418 os << oat_header.GetMagic() << "\n\n";
419
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800420 os << "LOCATION:\n";
421 os << oat_file_.GetLocation() << "\n\n";
422
Brian Carlstromaded5f72011-10-07 17:15:04 -0700423 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800424 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425
Elliott Hughesa72ec822012-03-05 17:12:22 -0800426 os << "INSTRUCTION SET:\n";
427 os << oat_header.GetInstructionSet() << "\n\n";
428
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700429 {
430 std::unique_ptr<const InstructionSetFeatures> features(
431 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
432 oat_header.GetInstructionSetFeaturesBitmap()));
433 os << "INSTRUCTION SET FEATURES:\n";
434 os << features->GetFeatureString() << "\n\n";
435 }
Dave Allison70202782013-10-22 17:52:19 -0700436
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437 os << "DEX FILE COUNT:\n";
438 os << oat_header.GetDexFileCount() << "\n\n";
439
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800440#define DUMP_OAT_HEADER_OFFSET(label, offset) \
441 os << label " OFFSET:\n"; \
442 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800444 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
445 } \
446 os << StringPrintf("\n\n");
447
448 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
449 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
450 GetInterpreterToInterpreterBridgeOffset);
451 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
452 GetInterpreterToCompiledCodeBridgeOffset);
453 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
454 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800455 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
456 GetQuickGenericJniTrampolineOffset);
457 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
458 GetQuickImtConflictTrampolineOffset);
459 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
460 GetQuickResolutionTrampolineOffset);
461 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
462 GetQuickToInterpreterBridgeOffset);
463#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700464
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700465 os << "IMAGE PATCH DELTA:\n";
466 os << StringPrintf("%d (0x%08x)\n\n",
467 oat_header.GetImagePatchDelta(),
468 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700469
Brian Carlstrom28db0122012-10-18 16:20:41 -0700470 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
471 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
472
473 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800474 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700475
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700476 // Print the key-value store.
477 {
478 os << "KEY VALUE STORE:\n";
479 size_t index = 0;
480 const char* key;
481 const char* value;
482 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
483 os << key << " = " << value << "\n";
484 index++;
485 }
486 os << "\n";
487 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700488
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800489 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700490 os << "BEGIN:\n";
491 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700492
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700493 os << "END:\n";
494 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
495 }
496
497 os << "SIZE:\n";
498 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700499
500 os << std::flush;
501
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800502 // If set, adjust relative address to be searched
503 if (options_.addr2instr_ != 0) {
504 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
505 os << "SEARCH ADDRESS (executable offset + input):\n";
506 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
507 }
508
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700509 // Dumping the dex file overview is compact enough to do even if header only.
510 DexFileData cumulative;
511 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
512 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
513 CHECK(oat_dex_file != nullptr);
514 std::string error_msg;
515 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
516 if (dex_file == nullptr) {
517 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
518 << error_msg;
519 continue;
520 }
521 DexFileData data(*dex_file);
522 os << "Dex file data for " << dex_file->GetLocation() << "\n";
523 data.Dump(os);
524 os << "\n";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700525 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
526 if (layout_sections != nullptr) {
527 os << "Layout data\n";
528 os << *layout_sections;
529 os << "\n";
530 }
531
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700532 cumulative.Add(data);
533 }
534 os << "Cumulative dex file data\n";
535 cumulative.Dump(os);
536 os << "\n";
537
David Brazdilc03d7b62016-03-02 12:18:03 +0000538 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000539 VariableIndentationOutputStream vios(&os);
540 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
541 if (vdex_header.IsValid()) {
542 std::string error_msg;
543 std::vector<const DexFile*> dex_files;
544 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
545 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
546 if (dex_file == nullptr) {
547 os << "Error opening dex file: " << error_msg << std::endl;
548 return false;
549 }
550 dex_files.push_back(dex_file);
551 }
552 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
553 deps.Dump(&vios);
554 } else {
555 os << "UNRECOGNIZED vdex file, magic "
556 << vdex_header.GetMagic()
557 << ", version "
558 << vdex_header.GetVersion()
559 << "\n";
560 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000561 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
562 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
563 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800564
David Brazdilc03d7b62016-03-02 12:18:03 +0000565 // If file export selected skip file analysis
566 if (options_.export_dex_location_) {
567 if (!ExportDexFile(os, *oat_dex_file)) {
568 success = false;
569 }
570 } else {
571 if (!DumpOatDexFile(os, *oat_dex_file)) {
572 success = false;
573 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800574 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700575 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700576 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000577
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800578 {
579 os << "OAT FILE STATS:\n";
580 VariableIndentationOutputStream vios(&os);
581 stats_.Dump(vios);
582 }
583
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700584 os << std::flush;
585 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 }
587
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800588 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700589 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
590 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800591 return 0; // Address not in oat file
592 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800593 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
594 reinterpret_cast<uintptr_t>(oat_file_.Begin());
595 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800596 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800597 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 return end_offset - begin_offset;
599 }
600
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800601 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700602 return oat_file_.GetOatHeader().GetInstructionSet();
603 }
604
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800606 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
607 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700608 CHECK(oat_dex_file != nullptr);
609 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700610 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
611 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700612 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
613 << "': " << error_msg;
614 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800615 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700616 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700617 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700618 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700619 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100620 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800621 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100622 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800623 }
624 }
625 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700626 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800627 }
628
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800629 struct Stats {
630 enum ByteKind {
631 kByteKindCode,
632 kByteKindQuickMethodHeader,
633 kByteKindCodeInfoLocationCatalog,
634 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800635 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800636 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000637 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800638 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800639 kByteKindStackMapNativePc,
640 kByteKindStackMapDexPc,
641 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800642 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800643 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000644 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700645 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800646 kByteKindInlineInfoDexPc,
647 kByteKindInlineInfoExtraData,
648 kByteKindInlineInfoDexRegisterMap,
649 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800650 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800651 // Special ranges for std::accumulate convenience.
652 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000653 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700654 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800655 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800656 };
657 int64_t bits[kByteKindCount] = {};
658 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
659 // deduplicated code and tables.
660 std::unordered_set<const void*> seen;
661
662 // Returns true if it was newly added.
663 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
664 if (seen.insert(address).second == true) {
665 // True means the address was not already in the set.
666 AddBits(kind, count);
667 return true;
668 }
669 return false;
670 }
671
672 void AddBits(ByteKind kind, int64_t count) {
673 bits[kind] += count;
674 }
675
676 void Dump(VariableIndentationOutputStream& os) {
677 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
678 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
679 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800680 Dump(os, "Code ", bits[kByteKindCode], sum);
681 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
682 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
683 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
684 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800685 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
686 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800687 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800688 // Stack map section.
689 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
690 bits + kByteKindStackMapLast + 1,
691 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800692 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800693 {
694 ScopedIndentation indent1(&os);
695 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800696 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800697 bits[kByteKindStackMapNativePc],
698 stack_map_bits,
699 "stack map");
700 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800701 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800702 bits[kByteKindStackMapDexPc],
703 stack_map_bits,
704 "stack map");
705 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800706 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800707 bits[kByteKindStackMapDexRegisterMap],
708 stack_map_bits,
709 "stack map");
710 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800711 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800712 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800713 stack_map_bits,
714 "stack map");
715 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800716 "StackMapRegisterMaskIndex ",
717 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800718 stack_map_bits,
719 "stack map");
720 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800721 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000722 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800723 stack_map_bits,
724 "stack map");
725 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800726 // Inline info section.
727 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
728 bits + kByteKindInlineInfoLast + 1,
729 0u);
730 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
731 {
732 ScopedIndentation indent1(&os);
733 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700734 "InlineInfoMethodIndexIdx ",
735 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800736 inline_info_bits,
737 "inline info");
738 Dump(os,
739 "InlineInfoDexPc ",
740 bits[kByteKindStackMapDexPc],
741 inline_info_bits,
742 "inline info");
743 Dump(os,
744 "InlineInfoExtraData ",
745 bits[kByteKindInlineInfoExtraData],
746 inline_info_bits,
747 "inline info");
748 Dump(os,
749 "InlineInfoDexRegisterMap ",
750 bits[kByteKindInlineInfoDexRegisterMap],
751 inline_info_bits,
752 "inline info");
753 Dump(os,
754 "InlineInfoIsLast ",
755 bits[kByteKindInlineInfoIsLast],
756 inline_info_bits,
757 "inline info");
758 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800759 }
760 os.Stream() << "\n" << std::flush;
761 }
762
763 private:
764 void Dump(VariableIndentationOutputStream& os,
765 const char* name,
766 int64_t size,
767 int64_t total,
768 const char* sum_of = "total") {
769 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
770 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
771 name,
772 size / kBitsPerByte,
773 percent,
774 sum_of);
775 }
776 };
777
Brian Carlstromaded5f72011-10-07 17:15:04 -0700778 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800779 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800780 // We don't know the length of the code for each method, but we need to know where to stop
781 // when disassembling. What we do know is that a region of code will be followed by some other
782 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
783 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800784 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
785 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700786 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700787 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700788 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
789 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700790 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
791 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800792 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800793 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800794 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800795 for (size_t class_def_index = 0;
796 class_def_index < dex_file->NumClassDefs();
797 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800798 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100799 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700800 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700801 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800802 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700803 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800804 uint32_t class_method_index = 0;
805 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100806 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800807 it.Next();
808 }
809 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100810 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800811 it.Next();
812 }
813 }
814 }
815 }
816
817 // If the last thing in the file is code for a method, there won't be an offset for the "next"
818 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
819 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800820 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800821 }
822
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700823 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
824 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
825 }
826
Elliott Hughese3c845c2012-02-28 17:23:01 -0800827 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800828 uint32_t code_offset = oat_method.GetCodeOffset();
829 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
830 code_offset &= ~0x1;
831 }
832 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800833 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800834 }
835
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700836 // Dex file data, may be for multiple different dex files.
837 class DexFileData {
838 public:
839 DexFileData() {}
840
841 explicit DexFileData(const DexFile& dex_file)
842 : num_string_ids_(dex_file.NumStringIds()),
843 num_method_ids_(dex_file.NumMethodIds()),
844 num_field_ids_(dex_file.NumFieldIds()),
845 num_type_ids_(dex_file.NumTypeIds()),
846 num_class_defs_(dex_file.NumClassDefs()) {
847 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
848 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
849 WalkClass(dex_file, class_def);
850 }
851 }
852
853 void Add(const DexFileData& other) {
854 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
855 num_string_ids_from_code_ += other.num_string_ids_from_code_;
856 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
857 dex_code_bytes_ += other.dex_code_bytes_;
858 num_string_ids_ += other.num_string_ids_;
859 num_method_ids_ += other.num_method_ids_;
860 num_field_ids_ += other.num_field_ids_;
861 num_type_ids_ += other.num_type_ids_;
862 num_class_defs_ += other.num_class_defs_;
863 }
864
865 void Dump(std::ostream& os) {
866 os << "Num string ids: " << num_string_ids_ << "\n";
867 os << "Num method ids: " << num_method_ids_ << "\n";
868 os << "Num field ids: " << num_field_ids_ << "\n";
869 os << "Num type ids: " << num_type_ids_ << "\n";
870 os << "Num class defs: " << num_class_defs_ << "\n";
871 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
872 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
873 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
874 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
875 }
876
877 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800878 // All of the elements from one container to another.
879 template <typename Dest, typename Src>
880 static void AddAll(Dest& dest, const Src& src) {
881 dest.insert(src.begin(), src.end());
882 }
883
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700884 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
885 const uint8_t* class_data = dex_file.GetClassData(class_def);
886 if (class_data == nullptr) { // empty class such as a marker interface?
887 return;
888 }
889 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700890 it.SkipAllFields();
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700891 while (it.HasNextDirectMethod()) {
892 WalkCodeItem(dex_file, it.GetMethodCodeItem());
893 it.Next();
894 }
895 while (it.HasNextVirtualMethod()) {
896 WalkCodeItem(dex_file, it.GetMethodCodeItem());
897 it.Next();
898 }
899 DCHECK(!it.HasNext());
900 }
901
902 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
903 if (code_item == nullptr) {
904 return;
905 }
906 const size_t code_item_size = code_item->insns_size_in_code_units_;
907 const uint16_t* code_ptr = code_item->insns_;
908 const uint16_t* code_end = code_item->insns_ + code_item_size;
909
910 // If we inserted a new dex code item pointer, add to total code bytes.
911 if (dex_code_item_ptrs_.insert(code_ptr).second) {
912 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
913 }
914
915 while (code_ptr < code_end) {
916 const Instruction* inst = Instruction::At(code_ptr);
917 switch (inst->Opcode()) {
918 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800919 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700920 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
921 ++num_string_ids_from_code_;
922 break;
923 }
924 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800925 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700926 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
927 ++num_string_ids_from_code_;
928 break;
929 }
930 default:
931 break;
932 }
933
934 code_ptr += inst->SizeInCodeUnits();
935 }
936 }
937
938 // Unique string ids loaded from dex code.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700939 std::set<StringReference> unique_string_ids_from_code_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700940
941 // Total string ids loaded from dex code.
942 size_t num_string_ids_from_code_ = 0;
943
944 // Unique code pointers.
945 std::set<const void*> dex_code_item_ptrs_;
946
947 // Total "unique" dex code bytes.
948 size_t dex_code_bytes_ = 0;
949
950 // Other dex ids.
951 size_t num_string_ids_ = 0;
952 size_t num_method_ids_ = 0;
953 size_t num_field_ids_ = 0;
954 size_t num_type_ids_ = 0;
955 size_t num_class_defs_ = 0;
956 };
957
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
959 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800960 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800962 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800963 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700964
Andreas Gampe2ba88952016-04-29 17:52:07 -0700965 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100966 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
967
968 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700969 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100970 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700971 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
972 dex_offset,
973 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700974
Andreas Gampe2ba88952016-04-29 17:52:07 -0700975 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700976 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700977 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
978 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700979 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700980 os << std::flush;
981 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700982 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100983
Andreas Gampe2ba88952016-04-29 17:52:07 -0700984 // Print lookup table, if it exists.
985 if (oat_dex_file.GetLookupTableData() != nullptr) {
986 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
987 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700988 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700989 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
990 table_offset,
991 table_offset + table_size - 1);
992 }
993
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 VariableIndentationOutputStream vios(&os);
995 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800996 for (size_t class_def_index = 0;
997 class_def_index < dex_file->NumClassDefs();
998 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700999 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
1000 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001001
1002 // TODO: Support regex
1003 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
1004 continue;
1005 }
1006
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001007 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001008 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001009 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -08001010 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001011 << " (" << oat_class.GetStatus() << ")"
1012 << " (" << oat_class.GetType() << ")\n";
1013 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001014 if (options_.list_classes_) {
1015 continue;
1016 }
1017 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001018 success = false;
1019 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001020 if (stop_analysis) {
1021 os << std::flush;
1022 return success;
1023 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001024 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001025 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001026 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001028 }
1029
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001030 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1031 std::string error_msg;
1032 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
1033
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001034 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001035 if (dex_file == nullptr) {
1036 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1037 return false;
1038 }
1039 size_t fsize = oat_dex_file.FileSize();
1040
1041 // Some quick checks just in case
1042 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1043 os << "Invalid dex file\n";
1044 return false;
1045 }
1046
1047 // Verify output directory exists
1048 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1049 // TODO: Extend OS::DirectoryExists if symlink support is required
1050 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1051 return false;
1052 }
1053
1054 // Beautify path names
1055 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1056 return false;
1057 }
1058
1059 std::string dex_orig_name;
1060 size_t dex_orig_pos = dex_file_location.rfind('/');
1061 if (dex_orig_pos == std::string::npos)
1062 dex_orig_name = dex_file_location;
1063 else
1064 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1065
1066 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001067 if (dex_orig_name.size() == 8 &&
1068 dex_orig_name.compare("base.apk") == 0 &&
1069 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001070 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1071 size_t apk_orig_pos = dex_file_location.rfind('/');
1072 if (apk_orig_pos != std::string::npos) {
1073 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1074 }
1075 }
1076
1077 std::string out_dex_path(options_.export_dex_location_);
1078 if (out_dex_path.back() != '/') {
1079 out_dex_path.append("/");
1080 }
1081 out_dex_path.append(dex_orig_name);
1082 out_dex_path.append("_export.dex");
1083 if (out_dex_path.length() > PATH_MAX) {
1084 return false;
1085 }
1086
1087 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1088 if (file.get() == nullptr) {
1089 os << "Failed to open output dex file " << out_dex_path;
1090 return false;
1091 }
1092
1093 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1094 os << "Failed to write dex file";
1095 file->Erase();
1096 return false;
1097 }
1098
1099 if (file->FlushCloseOrErase() != 0) {
1100 os << "Flush and close failed";
1101 return false;
1102 }
1103
1104 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1105 os << std::flush;
1106
1107 return true;
1108 }
1109
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001110 bool DumpOatClass(VariableIndentationOutputStream* vios,
1111 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001112 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001114 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001115 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001116 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001117 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001118 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001119 }
1120 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001121 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001122 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001123 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001124 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001125 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001126 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001127 success = false;
1128 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001129 if (addr_found) {
1130 *stop_analysis = true;
1131 return success;
1132 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001133 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001134 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001135 }
Ian Rogers0571d352011-11-03 19:51:38 -07001136 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001137 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001138 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001139 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001140 success = false;
1141 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001142 if (addr_found) {
1143 *stop_analysis = true;
1144 return success;
1145 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001146 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001147 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001148 }
Ian Rogers0571d352011-11-03 19:51:38 -07001149 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001150 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001151 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001152 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001153
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001154 static constexpr uint32_t kPrologueBytes = 16;
1155
1156 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1157 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1158
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001159 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1160 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001161 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001162 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001164 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001165 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001166
1167 // TODO: Support regex
1168 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1169 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001170 return success;
1171 }
1172
David Sehr709b0702016-10-13 09:12:37 -07001173 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001174 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1175 class_method_index, pretty_method.c_str(),
1176 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001177 if (options_.list_methods_) return success;
1178
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001179 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1180 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1181 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1182 uint32_t code_offset = oat_method.GetCodeOffset();
1183 uint32_t code_size = oat_method.GetQuickCodeSize();
1184 if (resolved_addr2instr_ != 0) {
1185 if (resolved_addr2instr_ > code_offset + code_size) {
1186 return success;
1187 } else {
1188 *addr_found = true; // stop analyzing file at next iteration
1189 }
1190 }
1191
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 // Everything below is indented at least once.
1193 ScopedIndentation indent1(vios);
1194
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 vios->Stream() << "DEX CODE:\n";
1197 ScopedIndentation indent2(vios);
1198 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001199 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001200
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001201 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001202 std::unique_ptr<verifier::MethodVerifier> verifier;
1203 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001204 // We need to have the handle scope stay live until after the verifier since the verifier has
1205 // a handle to the dex cache from hs.
1206 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1208 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001209 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001211 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001212 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001213 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001214 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001215 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001217 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001219 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001221 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1222 oat_method_offsets_offset, oat_file_.Size());
1223 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001224 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001225 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001226 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 ScopedIndentation indent2(vios);
1229 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001230 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1231 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001232 vios->Stream() << StringPrintf("WARNING: "
1233 "code offset 0x%08x is past end of file 0x%08zx.\n",
1234 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001235 success = false;
1236 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001238 }
1239 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1242 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001243 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1244 sizeof(*method_header) * kBitsPerByte,
1245 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001246 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001247 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001248 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001249 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001250 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001251 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001252 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1253 method_header_offset, oat_file_.Size());
1254 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001256 return false;
1257 }
1258
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001261 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001263 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001264 uint32_t vmap_table_offset = method_header ==
1265 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001266 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001267
1268 size_t vmap_table_offset_limit =
1269 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1270 ? oat_file_.GetVdexFile()->Size()
1271 : method_header->GetCode() - oat_file_.Begin();
1272 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 vios->Stream() << StringPrintf("WARNING: "
1274 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1275 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001276 vmap_table_offset,
1277 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001278 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001279 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001280 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001281 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001286
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001287 ScopedIndentation indent2(vios);
1288 vios->Stream()
1289 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1290 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1291 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1292 vios->Stream() << "\n";
1293 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1294 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1295 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001296 }
1297 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001298 // Based on spill masks from QuickMethodFrameInfo so placed
1299 // after it is dumped, but useful for understanding quick
1300 // code, so dumped here.
1301 ScopedIndentation indent2(vios);
1302 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001303 }
1304 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001305 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001306 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1307 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001308 ScopedIndentation indent2(vios);
1309 vios->Stream() << StringPrintf("WARNING: "
1310 "code size offset 0x%08x is past end of file 0x%08zx.",
1311 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001312 success = false;
1313 } else {
1314 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001315 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1316 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001317 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001318
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001319 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001320 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001321 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001322 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1323 code_offset,
1324 code_size_offset,
1325 code_size,
1326 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001327
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001328 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001329 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001330 vios->Stream() << StringPrintf("WARNING: "
1331 "start of code at 0x%08x is past end of file 0x%08zx.",
1332 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001333 success = false;
1334 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001335 vios->Stream() << StringPrintf(
1336 "WARNING: "
1337 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1338 "code size is 0x%08x loaded from offset 0x%08x.\n",
1339 aligned_code_end, oat_file_.Size(),
1340 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001341 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001342 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001343 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001344 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001345 }
1346 }
1347 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001348 vios->Stream() << StringPrintf(
1349 "WARNING: "
1350 "code size %d is bigger than max expected threshold of %d. "
1351 "code size is 0x%08x loaded from offset 0x%08x.\n",
1352 code_size, kMaxCodeSize,
1353 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001354 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001355 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001356 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001357 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 }
1359 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001360 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001361 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001362 }
1363 }
1364 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001365 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001366 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001367 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001368
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001369 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1370 if (spill_mask == 0) {
1371 return;
1372 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001373 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001374 for (size_t i = 0; i < 32; i++) {
1375 if ((spill_mask & (1 << i)) != 0) {
1376 if (is_float) {
1377 os << "fr" << i;
1378 } else {
1379 os << "r" << i;
1380 }
1381 spill_mask ^= 1 << i; // clear bit
1382 if (spill_mask != 0) {
1383 os << ", ";
1384 } else {
1385 break;
1386 }
1387 }
1388 }
1389 os << ")";
1390 }
1391
Roland Levillain442b46a2015-02-18 16:54:21 +00001392 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001393 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001394 const OatFile::OatMethod& oat_method,
1395 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001396 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1397 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001398 const void* raw_code_info = oat_method.GetVmapTable();
1399 if (raw_code_info != nullptr) {
1400 CodeInfo code_info(raw_code_info);
1401 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001402 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001403 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1404 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001405 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001406 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1407 // We don't encode the size in the table, so just emit that we have quickened
1408 // information.
1409 ScopedIndentation indent(vios);
1410 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001411 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001412 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001413 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001414 }
1415
1416 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001417 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001418 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001419 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001420 const DexFile::CodeItem& code_item,
1421 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001422 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001423 oat_method.GetCodeOffset(),
1424 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001425 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001426 instruction_set_,
1427 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001428 }
1429
Andreas Gampe36a296f2017-06-13 14:11:11 -07001430 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1431 // According to stack model, the first out is above the Method referernce.
1432 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1433 }
1434
1435 static uint32_t GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
1436 uint32_t core_spills,
1437 uint32_t fp_spills,
1438 size_t frame_size,
1439 int reg,
1440 InstructionSet isa) {
1441 PointerSize pointer_size = InstructionSetPointerSize(isa);
1442 if (kIsDebugBuild) {
1443 auto* runtime = Runtime::Current();
1444 if (runtime != nullptr) {
1445 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1446 }
1447 }
1448 DCHECK_ALIGNED(frame_size, kStackAlignment);
1449 DCHECK_NE(reg, -1);
1450 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1451 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1452 + sizeof(uint32_t); // Filler.
1453 int num_regs = code_item->registers_size_ - code_item->ins_size_;
1454 int temp_threshold = code_item->registers_size_;
1455 const int max_num_special_temps = 1;
1456 if (reg == temp_threshold) {
1457 // The current method pointer corresponds to special location on stack.
1458 return 0;
1459 } else if (reg >= temp_threshold + max_num_special_temps) {
1460 /*
1461 * Special temporaries may have custom locations and the logic above deals with that.
1462 * However, non-special temporaries are placed relative to the outs.
1463 */
1464 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
1465 + static_cast<size_t>(pointer_size) /* art method */;
1466 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1467 return temps_start + relative_offset;
1468 } else if (reg < num_regs) {
1469 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1470 return locals_start + (reg * sizeof(uint32_t));
1471 } else {
1472 // Handle ins.
1473 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1474 + static_cast<size_t>(pointer_size) /* art method */;
1475 }
1476 }
1477
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001478 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1479 const DexFile::CodeItem* code_item) {
1480 if (code_item != nullptr) {
1481 size_t num_locals_ins = code_item->registers_size_;
1482 size_t num_ins = code_item->ins_size_;
1483 size_t num_locals = num_locals_ins - num_ins;
1484 size_t num_outs = code_item->outs_size_;
1485
1486 os << "vr_stack_locations:";
1487 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1488 // For readability, delimit the different kinds of VRs.
1489 if (reg == num_locals_ins) {
1490 os << "\n\tmethod*:";
1491 } else if (reg == num_locals && num_ins > 0) {
1492 os << "\n\tins:";
1493 } else if (reg == 0 && num_locals > 0) {
1494 os << "\n\tlocals:";
1495 }
1496
Andreas Gampe36a296f2017-06-13 14:11:11 -07001497 uint32_t offset = GetVRegOffsetFromQuickCode(code_item,
1498 oat_method.GetCoreSpillMask(),
1499 oat_method.GetFpSpillMask(),
1500 oat_method.GetFrameSizeInBytes(),
1501 reg,
1502 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001503 os << " v" << reg << "[sp + #" << offset << "]";
1504 }
1505
1506 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1507 if (out_reg == 0) {
1508 os << "\n\touts:";
1509 }
1510
Andreas Gampe36a296f2017-06-13 14:11:11 -07001511 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001512 os << " v" << out_reg << "[sp + #" << offset << "]";
1513 }
1514
1515 os << "\n";
1516 }
1517 }
1518
Ian Rogersb23a7722012-10-09 16:54:26 -07001519 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001520 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001521 size_t i = 0;
1522 while (i < code_item->insns_size_in_code_units_) {
1523 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001524 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1525 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001526 i += instruction->SizeInCodeUnits();
1527 }
1528 }
1529 }
1530
Roland Levillainf2650d12015-05-28 14:53:28 +01001531 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1532 // the optimizing compiler?
1533 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1534 const DexFile::CodeItem* code_item) {
1535 // If the native GC map is null and the Dex `code_item` is not
1536 // null, then this method has been compiled with the optimizing
1537 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001538 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001539 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001540 code_item != nullptr;
1541 }
1542
1543 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1544 // the dextodex compiler?
1545 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1546 const DexFile::CodeItem* code_item) {
1547 // If the quick code is null, the Dex `code_item` is not
1548 // null, and the vmap table is not null, then this method has been compiled
1549 // with the dextodex compiler.
1550 return oat_method.GetQuickCode() == nullptr &&
1551 oat_method.GetVmapTable() != nullptr &&
1552 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001553 }
1554
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001555 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001556 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001557 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001558 const DexFile* dex_file,
1559 const DexFile::ClassDef& class_def,
1560 const DexFile::CodeItem* code_item,
1561 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 if ((method_access_flags & kAccNative) == 0) {
1563 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001564 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001565 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001566 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001567 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001568 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001569 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001570 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001571 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001573
1574 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001575 }
1576
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001577 // The StackMapsHelper provides the stack maps in the native PC order.
1578 // For identical native PCs, the order from the CodeInfo is preserved.
1579 class StackMapsHelper {
1580 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001581 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001582 : code_info_(raw_code_info),
1583 encoding_(code_info_.ExtractEncoding()),
1584 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1585 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001586 offset_(static_cast<uint32_t>(-1)),
1587 stack_map_index_(0u),
1588 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001589 if (number_of_stack_maps_ != 0u) {
1590 // Check if native PCs are ordered.
1591 bool ordered = true;
1592 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1593 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1594 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001595 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1596 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001597 ordered = false;
1598 break;
1599 }
1600 last = current;
1601 }
1602 if (!ordered) {
1603 // Create indirection indexes for access in native PC order. We do not optimize
1604 // for the fact that there can currently be only two separately ordered ranges,
1605 // namely normal stack maps and catch-point stack maps.
1606 indexes_.resize(number_of_stack_maps_);
1607 std::iota(indexes_.begin(), indexes_.end(), 0u);
1608 std::sort(indexes_.begin(),
1609 indexes_.end(),
1610 [this](size_t lhs, size_t rhs) {
1611 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001612 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001613 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001614 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001615 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001616 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001617 // If the PCs are the same, compare indexes to preserve the original order.
1618 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1619 });
1620 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001621 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001622 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001623 }
1624 }
1625
1626 const CodeInfo& GetCodeInfo() const {
1627 return code_info_;
1628 }
1629
1630 const CodeInfoEncoding& GetEncoding() const {
1631 return encoding_;
1632 }
1633
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001634 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001635 return offset_;
1636 }
1637
1638 StackMap GetStackMap() const {
1639 return GetStackMapAt(stack_map_index_);
1640 }
1641
1642 void Next() {
1643 ++stack_map_index_;
1644 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001645 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001646 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001647 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001648 }
1649
1650 private:
1651 StackMap GetStackMapAt(size_t i) const {
1652 if (!indexes_.empty()) {
1653 i = indexes_[i];
1654 }
1655 DCHECK_LT(i, number_of_stack_maps_);
1656 return code_info_.GetStackMapAt(i, encoding_);
1657 }
1658
1659 const CodeInfo code_info_;
1660 const CodeInfoEncoding encoding_;
1661 const size_t number_of_stack_maps_;
1662 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001663 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001664 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001665 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001666 };
1667
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001668 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001669 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1670 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001671 const void* quick_code = oat_method.GetQuickCode();
1672
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001673 if (code_size == 0) {
1674 code_size = oat_method.GetQuickCodeSize();
1675 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001676 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001677 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001678 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001679 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1680 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001681 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001682 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001683 {
1684 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001685 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001686 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001687 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001688 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001689 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001690 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001691 stats_.AddBits(
1692 Stats::kByteKindStackMapNativePc,
1693 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1694 stats_.AddBits(
1695 Stats::kByteKindStackMapDexPc,
1696 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1697 stats_.AddBits(
1698 Stats::kByteKindStackMapDexRegisterMap,
1699 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1700 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001701 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001702 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1703 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001704 Stats::kByteKindStackMapRegisterMaskIndex,
1705 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001706 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001707 Stats::kByteKindStackMapStackMaskIndex,
1708 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001709
1710 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001711 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001712 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001713 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001714
1715 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001716 stats_.AddBits(
1717 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001718 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001719
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001720 // Invoke infos
1721 if (encoding.invoke_info.num_entries > 0u) {
1722 stats_.AddBits(
1723 Stats::kByteKindCodeInfoInvokeInfo,
1724 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1725 }
1726
Mathieu Chartier1e083792017-02-08 13:30:04 -08001727 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001728 const size_t location_catalog_bytes =
1729 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1730 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1731 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001732 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001733 const size_t dex_register_bytes =
1734 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1735 stats_.AddBits(
1736 Stats::kByteKindCodeInfoDexRegisterMap,
1737 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001738
1739 // Inline infos.
1740 const size_t num_inline_infos = encoding.inline_info.num_entries;
1741 if (num_inline_infos > 0u) {
1742 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001743 Stats::kByteKindInlineInfoMethodIndexIdx,
1744 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1745 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001746 stats_.AddBits(
1747 Stats::kByteKindInlineInfoDexPc,
1748 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1749 stats_.AddBits(
1750 Stats::kByteKindInlineInfoExtraData,
1751 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1752 stats_.AddBits(
1753 Stats::kByteKindInlineInfoDexRegisterMap,
1754 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1755 num_inline_infos);
1756 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1757 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001758 }
1759 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001760 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1761 size_t offset = 0;
1762 while (offset < code_size) {
1763 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1764 if (offset == helper.GetOffset()) {
1765 ScopedIndentation indent1(vios);
1766 StackMap stack_map = helper.GetStackMap();
1767 DCHECK(stack_map.IsValid());
1768 stack_map.Dump(vios,
1769 helper.GetCodeInfo(),
1770 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001771 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001772 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001773 code_item->registers_size_,
1774 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001775 do {
1776 helper.Next();
1777 // There may be multiple stack maps at a given PC. We display only the first one.
1778 } while (offset == helper.GetOffset());
1779 }
1780 DCHECK_LT(offset, helper.GetOffset());
1781 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001782 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001783 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1784 size_t offset = 0;
1785 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001786 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001787 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001788 }
1789 }
1790
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001791 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001792 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001793 const OatDumperOptions& options_;
1794 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001795 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001796 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001797 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001798 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001799};
1800
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001801class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001802 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001803 ImageDumper(std::ostream* os,
1804 gc::space::ImageSpace& image_space,
1805 const ImageHeader& image_header,
1806 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001807 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001808 vios_(os),
1809 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001810 image_space_(image_space),
1811 image_header_(image_header),
1812 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001813
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001814 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001815 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001816 std::ostream& indent_os = vios_.Stream();
1817
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001818 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001819
Jeff Haodcdc85b2015-12-04 14:06:18 -08001820 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1821
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001823
Mathieu Chartiere401d142015-04-22 13:56:20 -07001824 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1825
1826 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1827 auto section = static_cast<ImageHeader::ImageSections>(i);
1828 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1829 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001830
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001832
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001833 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001834
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001835 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1836
1837 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1838
1839 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001840
Alex Lighta59dd802014-07-02 16:28:08 -07001841 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1842
Igor Murashkin46774762014-10-22 11:37:02 -07001843 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1844
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001845 {
1846 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001847 static_assert(arraysize(image_roots_descriptions_) ==
1848 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001849 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1850 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001851 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1852 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001853 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001855 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001856 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001857 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001858 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001859 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1860 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001861 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001862 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1863 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001864 run++;
1865 } else {
1866 break;
1867 }
1868 }
1869 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001870 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001871 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001872 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001873 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001874 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001875 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001876 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001878 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001879 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001880 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001881 }
1882 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001883 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001884
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001885 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886 os << "METHOD ROOTS\n";
1887 static_assert(arraysize(image_methods_descriptions_) ==
1888 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1889 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1890 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1891 const char* description = image_methods_descriptions_[i];
1892 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001893 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001894 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001895 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001896 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001897
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001898 Runtime* const runtime = Runtime::Current();
1899 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001900 std::string image_filename = image_space_.GetImageFilename();
1901 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001902 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001903 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001904 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001905 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001906 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001907 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1908 }
1909 if (oat_file == nullptr) {
1910 oat_file = OatFile::Open(oat_location,
1911 oat_location,
1912 nullptr,
1913 nullptr,
1914 false,
1915 /*low_4gb*/false,
1916 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001917 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001918 }
1919 if (oat_file == nullptr) {
1920 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1921 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001922 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001923 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001924
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001925 stats_.oat_file_bytes = oat_file->Size();
1926
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001927 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001928
Mathieu Chartier02e25112013-08-14 16:14:24 -07001929 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001930 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001931 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1932 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001933 }
1934
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001935 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001936
Jeff Haodcdc85b2015-12-04 14:06:18 -08001937 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001938 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001939 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001940 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001941 {
1942 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1943 heap->FlushAllocStack();
1944 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001945 // Since FlushAllocStack() above resets the (active) allocation
1946 // stack. Need to revoke the thread-local allocation stacks that
1947 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001948 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001949 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001950 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001951 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001952 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001953 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001954 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001955 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001956 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001957 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001958 ObjPtr<mirror::DexCache> dex_cache =
1959 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001960 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001961 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001962 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001963 }
1964 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001965 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1966 DumpObject(obj);
1967 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001968 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001969 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001970 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001971 indent_os << "\n";
1972 // TODO: Dump fields.
1973 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001974 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001975 image_header_.VisitPackedArtMethods(&visitor,
1976 image_space_.Begin(),
1977 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001978 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001979 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001980 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001981 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001982 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001983 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001984 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1985 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001986 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001987 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001988 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001989 // If the image is compressed, adjust to decompressed size.
1990 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1991 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1992 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1993 }
1994 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001995 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001996 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001997 const auto& object_section = image_header_.GetObjectsSection();
1998 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002000 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2001 const auto& intern_section = image_header_.GetInternedStringsSection();
2002 const auto& class_table_section = image_header_.GetClassTableSection();
2003 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002004
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002005 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002006
2007 // Objects are kObjectAlignment-aligned.
2008 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2009 if (object_section.Offset() > header_bytes) {
2010 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2011 }
2012
2013 // Field section is 4-byte aligned.
2014 constexpr size_t kFieldSectionAlignment = 4U;
2015 uint32_t end_objects = object_section.Offset() + object_section.Size();
2016 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2017 stats_.alignment_bytes += field_section.Offset() - end_objects;
2018
2019 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2020 uint32_t end_fields = field_section.Offset() + field_section.Size();
2021 CHECK_ALIGNED(method_section.Offset(), 4);
2022 stats_.alignment_bytes += method_section.Offset() - end_fields;
2023
2024 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2025 uint32_t end_methods = method_section.Offset() + method_section.Size();
2026 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2027 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2028
2029 // Intern table is 8-byte aligned.
2030 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002031 CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t));
Andreas Gampeace0dc12016-01-20 13:33:13 -08002032 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2033
2034 // Add space between intern table and class table.
2035 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2036 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2037
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002038 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2039 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002040 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002041 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002042
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 stats_.bitmap_bytes += bitmap_section.Size();
2044 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002045 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002046 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002047 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002048 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002049 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002050 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002051
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002052 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002053
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002054 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002055 }
2056
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002057 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002058 class DumpArtMethodVisitor : public ArtMethodVisitor {
2059 public:
2060 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2061
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002062 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002063 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002064 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002065 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002066 indent_os << "\n";
2067 }
2068
2069 private:
2070 ImageDumper* const image_dumper_;
2071 };
2072
Mathieu Chartier3398c782016-09-30 10:27:43 -07002073 static void PrettyObjectValue(std::ostream& os,
2074 ObjPtr<mirror::Class> type,
2075 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002076 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002077 CHECK(type != nullptr);
2078 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002079 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002080 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002081 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002082 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002083 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002084 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002085 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002086 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002087 } else {
David Sehr709b0702016-10-13 09:12:37 -07002088 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002089 }
2090 }
2091
Mathieu Chartier3398c782016-09-30 10:27:43 -07002092 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002093 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002094 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002095 switch (field->GetTypeAsPrimitiveType()) {
2096 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002097 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002098 break;
2099 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002100 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002101 break;
2102 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002103 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002104 break;
2105 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002107 break;
2108 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002109 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002110 break;
2111 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002112 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002113 break;
2114 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002115 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002116 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002117 break;
2118 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002119 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002120 break;
2121 case Primitive::kPrimNot: {
2122 // Get the value, don't compute the type unless it is non-null as we don't want
2123 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002124 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002125 if (value == nullptr) {
2126 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002127 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002128 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002129 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002130 if (field_type != nullptr) {
2131 PrettyObjectValue(os, field_type, value);
2132 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002133 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002134 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002135 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2136 }
Ian Rogers50239c72013-06-17 14:53:22 -07002137 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002138 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002139 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002140 default:
2141 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2142 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002143 }
2144 }
2145
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002146 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002147 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002148 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002149 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002150 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002151 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002152 for (ArtField& field : klass->GetIFields()) {
2153 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002154 }
2155 }
2156
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002157 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002158 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002159 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002160
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002161 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002162 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002163 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002164 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002165 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002166 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002167 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002168 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002169 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002170 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002171 }
2172
Mathieu Chartiere401d142015-04-22 13:56:20 -07002173 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002174 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002175 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2176 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002177 return 0;
2178 }
2179 return oat_code_begin[-1];
2180 }
2181
Mathieu Chartiere401d142015-04-22 13:56:20 -07002182 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002183 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002184 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002185 if (oat_code_begin == nullptr) {
2186 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002187 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002188 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002189 }
2190
Andreas Gampe0c183382017-07-13 22:26:24 -07002191 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002192 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002193 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002194 return;
2195 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002196
2197 size_t object_bytes = obj->SizeOf();
2198 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002199 stats_.object_bytes += object_bytes;
2200 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002201
Andreas Gampe0c183382017-07-13 22:26:24 -07002202 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002203
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002204 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002205 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002206 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002207 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002208 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002209 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002210 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2211 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002212 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002213 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002214 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002215 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002216 } else {
David Sehr709b0702016-10-13 09:12:37 -07002217 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002218 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002219 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002220 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002221 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002222 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002223 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2224 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002225 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002226 size_t run = 0;
2227 for (int32_t j = i + 1; j < length; j++) {
2228 if (value == obj_array->Get(j)) {
2229 run++;
2230 } else {
2231 break;
2232 }
2233 }
2234 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002235 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002236 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002237 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002238 i = i + run;
2239 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002240 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002241 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002242 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002243 }
2244 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002245 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002246 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002247 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002248 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002249 for (ArtField& field : klass->GetSFields()) {
2250 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002251 }
2252 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002253 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002254 auto it = dex_caches_.find(obj);
2255 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002256 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002257 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002258 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002259 size_t num_methods = dex_cache->NumResolvedMethods();
2260 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002261 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002262 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002263 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002264 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002265 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2266 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002267 size_t run = 0;
2268 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002269 j != length &&
2270 elem == mirror::DexCache::GetNativePairPtrSize(
2271 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002272 ++j) {
2273 ++run;
2274 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002275 if (run == 0) {
2276 os << StringPrintf("%zd: ", i);
2277 } else {
2278 os << StringPrintf("%zd to %zd: ", i, i + run);
2279 i = i + run;
2280 }
2281 std::string msg;
2282 if (elem == nullptr) {
2283 msg = "null";
2284 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002285 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002286 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002287 } else {
2288 msg = "<not in method section>";
2289 }
2290 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002291 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002292 }
2293 size_t num_fields = dex_cache->NumResolvedFields();
2294 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002295 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002296 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002297 auto* resolved_fields = dex_cache->GetResolvedFields();
2298 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002299 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002300 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002301 size_t run = 0;
2302 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002303 j != length &&
2304 elem == mirror::DexCache::GetNativePairPtrSize(
2305 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002306 ++j) {
2307 ++run;
2308 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002309 if (run == 0) {
2310 os << StringPrintf("%zd: ", i);
2311 } else {
2312 os << StringPrintf("%zd to %zd: ", i, i + run);
2313 i = i + run;
2314 }
2315 std::string msg;
2316 if (elem == nullptr) {
2317 msg = "null";
2318 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002319 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002320 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002321 } else {
2322 msg = "<not in field section>";
2323 }
2324 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002325 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002326 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002327 size_t num_types = dex_cache->NumResolvedTypes();
2328 if (num_types != 0u) {
2329 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002330 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002331 auto* resolved_types = dex_cache->GetResolvedTypes();
2332 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002333 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002334 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002335 for (size_t j = i + 1; j != num_types; ++j) {
2336 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2337 if (pair.index != other_pair.index ||
2338 pair.object.Read() != other_pair.object.Read()) {
2339 break;
2340 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002341 ++run;
2342 }
2343 if (run == 0) {
2344 os << StringPrintf("%zd: ", i);
2345 } else {
2346 os << StringPrintf("%zd to %zd: ", i, i + run);
2347 i = i + run;
2348 }
2349 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002350 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002351 if (elem == nullptr) {
2352 msg = "null";
2353 } else {
David Sehr709b0702016-10-13 09:12:37 -07002354 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002355 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002356 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002357 }
2358 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002359 }
2360 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002361 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002362 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002363 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002364
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002365 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002366 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002367 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002368 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2369 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002370 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002371 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2372 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002373 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002374 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002375 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2376 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002377 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002378 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002379 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002380 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2381 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002382 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002383 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002384 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002385 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002386 } else if (method->IsRuntimeMethod()) {
2387 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2388 if (table != nullptr) {
2389 indent_os << "IMT conflict table " << table << " method: ";
2390 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002391 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2392 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002393 }
2394 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002395 } else {
2396 const DexFile::CodeItem* code_item = method->GetCodeItem();
2397 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002398 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002399
2400 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002401 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002402 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002403 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002404 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002405 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002406 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002407 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002408 }
2409
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002410 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2411 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002412 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002413 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414 if (method->IsConstructor()) {
2415 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002416 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002417 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002418 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002419 }
2420 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002421 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002422 }
2423 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002424 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002425
Igor Murashkin7617abd2015-07-10 18:27:47 -07002426 uint32_t method_access_flags = method->GetAccessFlags();
2427
Mathieu Chartiere401d142015-04-22 13:56:20 -07002428 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002429 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2430 dex_instruction_bytes,
2431 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002432 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002433
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002434 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002435 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002436
2437 double expansion =
2438 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002439 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002440 }
2441 }
2442
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002443 std::set<const void*> already_seen_;
2444 // Compute the size of the given data within the oat file and whether this is the first time
2445 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002446 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002447 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002448 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002449 already_seen_.insert(oat_data);
2450 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002451 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002452 }
2453 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002454 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002455
2456 public:
2457 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002458 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002459 size_t file_bytes;
2460
2461 size_t header_bytes;
2462 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002463 size_t art_field_bytes;
2464 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002465 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002466 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002467 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002468 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002469 size_t alignment_bytes;
2470
2471 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002472 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002473 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002474 size_t class_initializer_code_bytes;
2475 size_t large_initializer_code_bytes;
2476 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002477
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002478 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002479
2480 size_t dex_instruction_bytes;
2481
Mathieu Chartiere401d142015-04-22 13:56:20 -07002482 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002483 std::vector<size_t> method_outlier_size;
2484 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002485 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002486
Roland Levillain3887c462015-08-12 18:15:42 +01002487 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002488 : oat_file_bytes(0),
2489 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002490 header_bytes(0),
2491 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002492 art_field_bytes(0),
2493 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002494 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002495 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002496 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002497 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002498 alignment_bytes(0),
2499 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002500 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002501 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002502 class_initializer_code_bytes(0),
2503 large_initializer_code_bytes(0),
2504 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002505 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002506 dex_instruction_bytes(0) {}
2507
Elliott Hughesa0e18062012-04-13 15:59:59 -07002508 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002509 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002510 size_t bytes;
2511 size_t count;
2512 };
2513 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2514 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002515
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002516 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002517 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2518 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002519 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002520 it->second.count += 1;
2521 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002522 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002523 }
2524 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002525
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002526 double PercentOfOatBytes(size_t size) {
2527 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2528 }
2529
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002530 double PercentOfFileBytes(size_t size) {
2531 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2532 }
2533
2534 double PercentOfObjectBytes(size_t size) {
2535 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2536 }
2537
Mathieu Chartiere401d142015-04-22 13:56:20 -07002538 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002539 method_outlier_size.push_back(total_size);
2540 method_outlier_expansion.push_back(expansion);
2541 method_outlier.push_back(method);
2542 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002543
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002544 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002545 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002546 size_t sum_of_sizes = 0;
2547 size_t sum_of_sizes_squared = 0;
2548 size_t sum_of_expansion = 0;
2549 size_t sum_of_expansion_squared = 0;
2550 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002551 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002552 return;
2553 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002554 for (size_t i = 0; i < n; i++) {
2555 size_t cur_size = method_outlier_size[i];
2556 sum_of_sizes += cur_size;
2557 sum_of_sizes_squared += cur_size * cur_size;
2558 double cur_expansion = method_outlier_expansion[i];
2559 sum_of_expansion += cur_expansion;
2560 sum_of_expansion_squared += cur_expansion * cur_expansion;
2561 }
2562 size_t size_mean = sum_of_sizes / n;
2563 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2564 double expansion_mean = sum_of_expansion / n;
2565 double expansion_variance =
2566 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2567
2568 // Dump methods whose size is a certain number of standard deviations from the mean
2569 size_t dumped_values = 0;
2570 size_t skipped_values = 0;
2571 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2572 size_t cur_size_variance = i * i * size_variance;
2573 bool first = true;
2574 for (size_t j = 0; j < n; j++) {
2575 size_t cur_size = method_outlier_size[j];
2576 if (cur_size > size_mean) {
2577 size_t cur_var = cur_size - size_mean;
2578 cur_var = cur_var * cur_var;
2579 if (cur_var > cur_size_variance) {
2580 if (dumped_values > 20) {
2581 if (i == 1) {
2582 skipped_values++;
2583 } else {
2584 i = 2; // jump to counting for 1 standard deviation
2585 break;
2586 }
2587 } else {
2588 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002589 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002590 first = false;
2591 }
David Sehr709b0702016-10-13 09:12:37 -07002592 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002593 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002594 method_outlier_size[j] = 0; // don't consider this method again
2595 dumped_values++;
2596 }
2597 }
2598 }
2599 }
2600 }
2601 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002602 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002603 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002604 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002605 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002606
2607 // Dump methods whose expansion is a certain number of standard deviations from the mean
2608 dumped_values = 0;
2609 skipped_values = 0;
2610 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2611 double cur_expansion_variance = i * i * expansion_variance;
2612 bool first = true;
2613 for (size_t j = 0; j < n; j++) {
2614 double cur_expansion = method_outlier_expansion[j];
2615 if (cur_expansion > expansion_mean) {
2616 size_t cur_var = cur_expansion - expansion_mean;
2617 cur_var = cur_var * cur_var;
2618 if (cur_var > cur_expansion_variance) {
2619 if (dumped_values > 20) {
2620 if (i == 1) {
2621 skipped_values++;
2622 } else {
2623 i = 2; // jump to counting for 1 standard deviation
2624 break;
2625 }
2626 } else {
2627 if (first) {
2628 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002629 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002630 first = false;
2631 }
David Sehr709b0702016-10-13 09:12:37 -07002632 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002633 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002634 method_outlier_expansion[j] = 0.0; // don't consider this method again
2635 dumped_values++;
2636 }
2637 }
2638 }
2639 }
2640 }
2641 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002642 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002643 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002644 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002645 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002646 }
2647
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002648 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002649 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 {
2651 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2652 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002653 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2654 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2655 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2656 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2657 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2658 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002659 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002660 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2661 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002662 header_bytes, PercentOfFileBytes(header_bytes),
2663 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002664 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2665 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002666 dex_cache_arrays_bytes,
2667 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002668 interned_strings_bytes,
2669 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002670 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002671 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002672 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2673 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002674 CHECK_EQ(file_bytes,
2675 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2676 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2677 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002679
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002680 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002681 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002682 for (const auto& sizes_and_count : sizes_and_counts) {
2683 const std::string& descriptor(sizes_and_count.first);
2684 double average = static_cast<double>(sizes_and_count.second.bytes) /
2685 static_cast<double>(sizes_and_count.second.count);
2686 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002687 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002688 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002689 descriptor.c_str(), sizes_and_count.second.bytes,
2690 sizes_and_count.second.count, average, percent);
2691 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002692 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002693 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002694 CHECK_EQ(object_bytes, object_bytes_total);
2695
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 os << StringPrintf("oat_file_bytes = %8zd\n"
2697 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2699 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2700 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2701 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002702 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002703 managed_code_bytes,
2704 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002705 native_to_managed_code_bytes,
2706 PercentOfOatBytes(native_to_managed_code_bytes),
2707 class_initializer_code_bytes,
2708 PercentOfOatBytes(class_initializer_code_bytes),
2709 large_initializer_code_bytes,
2710 PercentOfOatBytes(large_initializer_code_bytes),
2711 large_method_code_bytes,
2712 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002713 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002714 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002715 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002716 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2717 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002718 }
2719
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002720 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002721 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002722 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002723
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002724 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2725 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002726 static_cast<double>(managed_code_bytes) /
2727 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002728 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002729 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002730 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002731
2732 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002733 }
2734 } stats_;
2735
2736 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002737 enum {
2738 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2739 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2740 kLargeConstructorDexBytes = 4000,
2741 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2742 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2743 kLargeMethodDexBytes = 16000
2744 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002745
2746 // For performance, use the *os_ directly for anything that doesn't need indentation
2747 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002748 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002749 VariableIndentationOutputStream vios_;
2750 ScopedIndentation indent1_;
2751
Ian Rogers1d54e732013-05-02 21:10:01 -07002752 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002753 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002754 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002755 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002756 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002757
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002758 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002759};
2760
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002761static int DumpImage(gc::space::ImageSpace* image_space,
2762 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002763 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002764 const ImageHeader& image_header = image_space->GetImageHeader();
2765 if (!image_header.IsValid()) {
2766 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2767 return EXIT_FAILURE;
2768 }
2769 ImageDumper image_dumper(os, *image_space, image_header, options);
2770 if (!image_dumper.Dump()) {
2771 return EXIT_FAILURE;
2772 }
2773 return EXIT_SUCCESS;
2774}
2775
2776static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002777 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002778 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002779 options->class_loader_ = &null_class_loader;
2780
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002781 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002782 if (options->app_image_ != nullptr) {
2783 if (options->app_oat_ == nullptr) {
2784 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002785 return EXIT_FAILURE;
2786 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002787 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2788 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2789 // pointers into 32 bit pointer sized ArtMethods.
2790 std::string error_msg;
2791 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2792 options->app_oat_,
2793 nullptr,
2794 nullptr,
2795 false,
2796 /*low_4gb*/true,
2797 nullptr,
2798 &error_msg));
2799 if (oat_file == nullptr) {
2800 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002801 return EXIT_FAILURE;
2802 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002803 std::unique_ptr<gc::space::ImageSpace> space(
2804 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2805 if (space == nullptr) {
2806 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2807 << error_msg;
2808 }
2809 // Open dex files for the image.
2810 std::vector<std::unique_ptr<const DexFile>> dex_files;
2811 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2812 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2813 << error_msg;
2814 }
2815 // Dump the actual image.
2816 int result = DumpImage(space.get(), options, os);
2817 if (result != EXIT_SUCCESS) {
2818 return result;
2819 }
2820 // Fall through to dump the boot images.
2821 }
2822
2823 gc::Heap* heap = runtime->GetHeap();
2824 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2825 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2826 int result = DumpImage(image_space, options, os);
2827 if (result != EXIT_SUCCESS) {
2828 return result;
2829 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002830 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002831 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002832}
2833
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002834static jobject InstallOatFile(Runtime* runtime,
2835 std::unique_ptr<OatFile> oat_file,
2836 std::vector<const DexFile*>* class_path)
2837 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002838 Thread* self = Thread::Current();
2839 CHECK(self != nullptr);
2840 // Need well-known-classes.
2841 WellKnownClasses::Init(self->GetJniEnv());
2842
2843 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002844 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002845 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002846 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2847 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002848 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002849 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002850 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002851 ObjPtr<mirror::DexCache> dex_cache =
2852 class_linker->RegisterDexFile(*dex_file, nullptr);
2853 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002854 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002855 }
2856
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002857 // Need a class loader. Fake that we're a compiler.
2858 // Note: this will run initializers through the unstarted runtime, so make sure it's
2859 // initialized.
2860 interpreter::UnstartedRuntime::Initialize();
2861
2862 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2863
2864 return class_loader;
2865}
2866
2867static int DumpOatWithRuntime(Runtime* runtime,
2868 std::unique_ptr<OatFile> oat_file,
2869 OatDumperOptions* options,
2870 std::ostream* os) {
2871 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2872 ScopedObjectAccess soa(Thread::Current());
2873
2874 OatFile* oat_file_ptr = oat_file.get();
2875 std::vector<const DexFile*> class_path;
2876 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002877
2878 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002879 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002880 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002881 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002882 options->class_loader_ = &loader_handle;
2883
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002884 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002885 bool success = oat_dumper.Dump(*os);
2886 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2887}
2888
2889static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002890 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002891 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002892 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002893 options->class_loader_ = &null_class_loader;
2894
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002895 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002896 bool success = oat_dumper.Dump(*os);
2897 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2898}
2899
2900static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2901 std::ostream* os) {
2902 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002903 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2904 oat_filename,
2905 nullptr,
2906 nullptr,
2907 false,
2908 /*low_4gb*/false,
2909 nullptr,
2910 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002911 if (oat_file == nullptr) {
2912 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2913 return EXIT_FAILURE;
2914 }
2915
2916 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002917 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002918 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002919 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002920 }
2921}
2922
David Srbecky2fdd03c2016-03-10 15:32:37 +00002923static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002924 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002925 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2926 oat_filename,
2927 nullptr,
2928 nullptr,
2929 false,
2930 /*low_4gb*/false,
2931 nullptr,
2932 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002933 if (oat_file == nullptr) {
2934 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2935 return EXIT_FAILURE;
2936 }
2937
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002938 bool result;
2939 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2940 // files for 64-bit code in the past.
2941 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002942 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002943 result = oat_symbolizer.Symbolize();
2944 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002945 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002946 result = oat_symbolizer.Symbolize();
2947 }
2948 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002949 fprintf(stderr, "Failed to symbolize\n");
2950 return EXIT_FAILURE;
2951 }
2952
2953 return EXIT_SUCCESS;
2954}
2955
Andreas Gampe9fded872016-09-25 16:08:35 -07002956class IMTDumper {
2957 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002958 static bool Dump(Runtime* runtime,
2959 const std::string& imt_file,
2960 bool dump_imt_stats,
2961 const char* oat_filename) {
2962 Thread* self = Thread::Current();
2963
2964 ScopedObjectAccess soa(self);
2965 StackHandleScope<1> scope(self);
2966 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2967 std::vector<const DexFile*> class_path;
2968
2969 if (oat_filename != nullptr) {
2970 std::string error_msg;
2971 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2972 oat_filename,
2973 nullptr,
2974 nullptr,
2975 false,
2976 /*low_4gb*/false,
2977 nullptr,
2978 &error_msg));
2979 if (oat_file == nullptr) {
2980 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2981 return false;
2982 }
2983
2984 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2985 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2986 } else {
2987 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2988 class_path = runtime->GetClassLinker()->GetBootClassPath();
2989 }
2990
2991 if (!imt_file.empty()) {
2992 return DumpImt(runtime, imt_file, class_loader);
2993 }
2994
2995 if (dump_imt_stats) {
2996 return DumpImtStats(runtime, class_path, class_loader);
2997 }
2998
2999 LOG(FATAL) << "Should not reach here";
3000 UNREACHABLE();
3001 }
3002
3003 private:
3004 static bool DumpImt(Runtime* runtime,
3005 const std::string& imt_file,
3006 Handle<mirror::ClassLoader> h_class_loader)
3007 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003008 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3009 std::unordered_set<std::string> prepared;
3010
3011 for (const std::string& line : lines) {
3012 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3013 // or a class descriptor and an interface method, in which case we will lookup the method,
3014 // determine its IMT slot, and check the class' IMT.
3015 size_t first_space = line.find(' ');
3016 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003017 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003018 } else {
3019 DumpIMTForMethod(runtime,
3020 line.substr(0, first_space),
3021 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003022 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003023 &prepared);
3024 }
3025 std::cerr << std::endl;
3026 }
3027
3028 return true;
3029 }
3030
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003031 static bool DumpImtStats(Runtime* runtime,
3032 const std::vector<const DexFile*>& dex_files,
3033 Handle<mirror::ClassLoader> h_class_loader)
3034 REQUIRES_SHARED(Locks::mutator_lock_) {
3035 size_t without_imt = 0;
3036 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003037 std::map<size_t, size_t> histogram;
3038
3039 ClassLinker* class_linker = runtime->GetClassLinker();
3040 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3041 std::unordered_set<std::string> prepared;
3042
3043 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003044 StackHandleScope<1> scope(self);
3045 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3046
3047 for (const DexFile* dex_file : dex_files) {
3048 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003049 class_def_index != dex_file->NumClassDefs();
3050 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003051 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3052 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003053 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003054 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003055 std::cerr << "Warning: could not load " << descriptor << std::endl;
3056 continue;
3057 }
3058
3059 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003060 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003061 continue;
3062 }
3063
3064 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3065 if (im_table == nullptr) {
3066 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003067 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003068 continue;
3069 }
3070
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003071 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003072 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3073 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3074 if (ptr->IsRuntimeMethod()) {
3075 if (ptr->IsImtUnimplementedMethod()) {
3076 histogram[0]++;
3077 } else {
3078 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3079 histogram[current_table->NumEntries(pointer_size)]++;
3080 }
3081 } else {
3082 histogram[1]++;
3083 }
3084 }
3085 }
3086 }
3087
3088 std::cerr << "IMT stats:"
3089 << std::endl << std::endl;
3090
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003091 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003092 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003093 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003094 << std::endl << std::endl;
3095
3096 double sum_one = 0;
3097 size_t count_one = 0;
3098
3099 std::cerr << " " << "IMT histogram" << std::endl;
3100 for (auto& bucket : histogram) {
3101 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3102 if (bucket.first > 0) {
3103 sum_one += bucket.second * bucket.first;
3104 count_one += bucket.second;
3105 }
3106 }
3107
3108 double count_zero = count_one + histogram[0];
3109 std::cerr << " Stats:" << std::endl;
3110 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3111 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3112
3113 return true;
3114 }
3115
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003116 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003117 static bool HasNoIMT(Runtime* runtime,
3118 Handle<mirror::Class> klass,
3119 const PointerSize pointer_size,
3120 std::unordered_set<std::string>* prepared)
3121 REQUIRES_SHARED(Locks::mutator_lock_) {
3122 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3123 return true;
3124 }
3125
3126 if (klass->GetImt(pointer_size) == nullptr) {
3127 PrepareClass(runtime, klass, prepared);
3128 }
3129
3130 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3131 DCHECK(object_class->IsObjectClass());
3132
3133 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3134
Mathieu Chartier6beced42016-11-15 15:51:31 -08003135 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003136 DCHECK(result);
3137 }
3138
3139 return result;
3140 }
3141
3142 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3143 REQUIRES_SHARED(Locks::mutator_lock_) {
3144 if (table == nullptr) {
3145 std::cerr << " <No IMT?>" << std::endl;
3146 return;
3147 }
3148 size_t table_index = 0;
3149 for (;;) {
3150 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3151 if (ptr == nullptr) {
3152 return;
3153 }
3154 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003155 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003156 }
3157 }
3158
3159 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3160 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003161 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003162 const std::string& class_name,
3163 const PointerSize pointer_size,
3164 mirror::Class** klass_out,
3165 std::unordered_set<std::string>* prepared)
3166 REQUIRES_SHARED(Locks::mutator_lock_) {
3167 if (class_name.empty()) {
3168 return nullptr;
3169 }
3170
3171 std::string descriptor;
3172 if (class_name[0] == 'L') {
3173 descriptor = class_name;
3174 } else {
3175 descriptor = DotToDescriptor(class_name.c_str());
3176 }
3177
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003178 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003179
3180 if (klass == nullptr) {
3181 self->ClearException();
3182 std::cerr << "Did not find " << class_name << std::endl;
3183 *klass_out = nullptr;
3184 return nullptr;
3185 }
3186
3187 StackHandleScope<1> scope(Thread::Current());
3188 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3189
3190 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3191 *klass_out = h_klass.Get();
3192 return ret;
3193 }
3194
3195 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3196 Handle<mirror::Class> h_klass,
3197 const PointerSize pointer_size,
3198 std::unordered_set<std::string>* prepared)
3199 REQUIRES_SHARED(Locks::mutator_lock_) {
3200 PrepareClass(runtime, h_klass, prepared);
3201 return h_klass->GetImt(pointer_size);
3202 }
3203
3204 static void DumpIMTForClass(Runtime* runtime,
3205 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003206 Handle<mirror::ClassLoader> h_loader,
3207 std::unordered_set<std::string>* prepared)
3208 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003209 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3210 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003211 ImTable* imt = PrepareAndGetImTable(runtime,
3212 Thread::Current(),
3213 h_loader,
3214 class_name,
3215 pointer_size,
3216 &klass,
3217 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003218 if (imt == nullptr) {
3219 return;
3220 }
3221
3222 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3223 for (size_t index = 0; index < ImTable::kSize; ++index) {
3224 std::cerr << " " << index << ":" << std::endl;
3225 ArtMethod* ptr = imt->Get(index, pointer_size);
3226 if (ptr->IsRuntimeMethod()) {
3227 if (ptr->IsImtUnimplementedMethod()) {
3228 std::cerr << " <empty>" << std::endl;
3229 } else {
3230 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3231 PrintTable(current_table, pointer_size);
3232 }
3233 } else {
David Sehr709b0702016-10-13 09:12:37 -07003234 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003235 }
3236 }
3237
3238 std::cerr << " Interfaces:" << std::endl;
3239 // Run through iftable, find methods that slot here, see if they fit.
3240 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003241 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3242 mirror::Class* iface = if_table->GetInterface(i);
3243 std::string iface_name;
3244 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003245
Mathieu Chartier6beced42016-11-15 15:51:31 -08003246 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3247 uint32_t class_hash, name_hash, signature_hash;
3248 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3249 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3250 std::cerr << " " << iface_method.PrettyMethod(true)
3251 << " slot=" << imt_slot
3252 << std::hex
3253 << " class_hash=0x" << class_hash
3254 << " name_hash=0x" << name_hash
3255 << " signature_hash=0x" << signature_hash
3256 << std::dec
3257 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003258 }
3259 }
3260 }
3261
3262 static void DumpIMTForMethod(Runtime* runtime,
3263 const std::string& class_name,
3264 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003265 Handle<mirror::ClassLoader> h_loader,
3266 std::unordered_set<std::string>* prepared)
3267 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003268 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3269 mirror::Class* klass;
3270 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003271 Thread::Current(),
3272 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003273 class_name,
3274 pointer_size,
3275 &klass,
3276 prepared);
3277 if (imt == nullptr) {
3278 return;
3279 }
3280
3281 std::cerr << class_name << " <" << method << ">" << std::endl;
3282 for (size_t index = 0; index < ImTable::kSize; ++index) {
3283 ArtMethod* ptr = imt->Get(index, pointer_size);
3284 if (ptr->IsRuntimeMethod()) {
3285 if (ptr->IsImtUnimplementedMethod()) {
3286 continue;
3287 }
3288
3289 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3290 if (current_table == nullptr) {
3291 continue;
3292 }
3293
3294 size_t table_index = 0;
3295 for (;;) {
3296 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3297 if (ptr2 == nullptr) {
3298 break;
3299 }
3300 table_index++;
3301
David Sehr709b0702016-10-13 09:12:37 -07003302 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003303 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003304 std::cerr << " Slot "
3305 << index
3306 << " ("
3307 << current_table->NumEntries(pointer_size)
3308 << ")"
3309 << std::endl;
3310 PrintTable(current_table, pointer_size);
3311 return;
3312 }
3313 }
3314 } else {
David Sehr709b0702016-10-13 09:12:37 -07003315 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003316 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003317 std::cerr << " Slot " << index << " (1)" << std::endl;
3318 std::cerr << " " << p_name << std::endl;
3319 } else {
3320 // Run through iftable, find methods that slot here, see if they fit.
3321 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003322 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3323 mirror::Class* iface = if_table->GetInterface(i);
3324 size_t num_methods = iface->NumDeclaredVirtualMethods();
3325 if (num_methods > 0) {
3326 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3327 if (ImTable::GetImtIndex(&iface_method) == index) {
3328 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003329 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003330 std::cerr << " Slot " << index << " (1)" << std::endl;
3331 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003332 }
3333 }
3334 }
3335 }
3336 }
3337 }
3338 }
3339 }
3340 }
3341
3342 // Read lines from the given stream, dropping comments and empty lines
3343 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3344 std::vector<std::string> output;
3345 while (in_stream.good()) {
3346 std::string dot;
3347 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003348 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003349 continue;
3350 }
3351 output.push_back(dot);
3352 }
3353 return output;
3354 }
3355
3356 // Read lines from the given file, dropping comments and empty lines.
3357 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3358 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3359 if (input_file.get() == nullptr) {
3360 LOG(ERROR) << "Failed to open input file " << input_filename;
3361 return std::vector<std::string>();
3362 }
3363 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3364 input_file->close();
3365 return result;
3366 }
3367
3368 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3369 // and note in the given set that the work was done.
3370 static void PrepareClass(Runtime* runtime,
3371 Handle<mirror::Class> h_klass,
3372 std::unordered_set<std::string>* done)
3373 REQUIRES_SHARED(Locks::mutator_lock_) {
3374 if (!h_klass->ShouldHaveImt()) {
3375 return;
3376 }
3377
3378 std::string name;
3379 name = h_klass->GetDescriptor(&name);
3380
3381 if (done->find(name) != done->end()) {
3382 return;
3383 }
3384 done->insert(name);
3385
3386 if (h_klass->HasSuperClass()) {
3387 StackHandleScope<1> h(Thread::Current());
3388 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3389 }
3390
3391 if (!h_klass->IsTemp()) {
3392 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3393 }
3394 }
3395};
3396
Igor Murashkin37743352014-11-13 14:38:00 -08003397struct OatdumpArgs : public CmdlineArgs {
3398 protected:
3399 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003400
Igor Murashkin37743352014-11-13 14:38:00 -08003401 virtual ParseStatus ParseCustom(const StringPiece& option,
3402 std::string* error_msg) OVERRIDE {
3403 {
3404 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3405 if (base_parse != kParseUnknownArgument) {
3406 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003407 }
3408 }
3409
Igor Murashkin37743352014-11-13 14:38:00 -08003410 if (option.starts_with("--oat-file=")) {
3411 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3412 } else if (option.starts_with("--image=")) {
3413 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003414 } else if (option == "--no-dump:vmap") {
3415 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003416 } else if (option =="--dump:code_info_stack_maps") {
3417 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003418 } else if (option == "--no-disassemble") {
3419 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003420 } else if (option =="--header-only") {
3421 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003422 } else if (option.starts_with("--symbolize=")) {
3423 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3424 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003425 } else if (option.starts_with("--only-keep-debug")) {
3426 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003427 } else if (option.starts_with("--class-filter=")) {
3428 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003429 } else if (option.starts_with("--method-filter=")) {
3430 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003431 } else if (option.starts_with("--list-classes")) {
3432 list_classes_ = true;
3433 } else if (option.starts_with("--list-methods")) {
3434 list_methods_ = true;
3435 } else if (option.starts_with("--export-dex-to=")) {
3436 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3437 } else if (option.starts_with("--addr2instr=")) {
3438 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3439 *error_msg = "Address conversion failed";
3440 return kParseError;
3441 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003442 } else if (option.starts_with("--app-image=")) {
3443 app_image_ = option.substr(strlen("--app-image=")).data();
3444 } else if (option.starts_with("--app-oat=")) {
3445 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003446 } else if (option.starts_with("--dump-imt=")) {
3447 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3448 } else if (option == "--dump-imt-stats") {
3449 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003450 } else {
3451 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003452 }
3453
Igor Murashkin37743352014-11-13 14:38:00 -08003454 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003455 }
3456
Igor Murashkin37743352014-11-13 14:38:00 -08003457 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3458 // Infer boot image location from the image location if possible.
3459 if (boot_image_location_ == nullptr) {
3460 boot_image_location_ = image_location_;
3461 }
3462
3463 // Perform the parent checks.
3464 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3465 if (parent_checks != kParseOk) {
3466 return parent_checks;
3467 }
3468
3469 // Perform our own checks.
3470 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3471 *error_msg = "Either --image or --oat-file must be specified";
3472 return kParseError;
3473 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3474 *error_msg = "Either --image or --oat-file must be specified but not both";
3475 return kParseError;
3476 }
3477
3478 return kParseOk;
3479 }
3480
3481 virtual std::string GetUsage() const {
3482 std::string usage;
3483
3484 usage +=
3485 "Usage: oatdump [options] ...\n"
3486 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3487 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3488 "\n"
3489 // Either oat-file or image is required.
3490 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3491 " Example: --oat-file=/system/framework/boot.oat\n"
3492 "\n"
3493 " --image=<file.art>: specifies an input image location.\n"
3494 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003495 "\n"
3496 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003497 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003498 " Example: --app-image=app.art\n"
3499 "\n"
3500 " --app-oat=<file.odex>: specifies an input app oat.\n"
3501 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003502 "\n";
3503
3504 usage += Base::GetUsage();
3505
3506 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003507 " --no-dump:vmap may be used to disable vmap dumping.\n"
3508 " Example: --no-dump:vmap\n"
3509 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003510 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3511 " Example: --dump:code_info_stack_maps\n"
3512 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003513 " --no-disassemble may be used to disable disassembly.\n"
3514 " Example: --no-disassemble\n"
3515 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003516 " --header-only may be used to print only the oat header.\n"
3517 " Example: --header-only\n"
3518 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003519 " --list-classes may be used to list target file classes (can be used with filters).\n"
3520 " Example: --list-classes\n"
3521 " Example: --list-classes --class-filter=com.example.foo\n"
3522 "\n"
3523 " --list-methods may be used to list target file methods (can be used with filters).\n"
3524 " Example: --list-methods\n"
3525 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3526 "\n"
3527 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3528 " Example: --symbolize=/system/framework/boot.oat\n"
3529 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003530 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3531 " .rodata and .text sections are omitted in the output file to save space.\n"
3532 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3533 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003534 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3535 " Example: --class-filter=com.example.foo\n"
3536 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003537 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3538 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003539 "\n"
3540 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3541 " Example: --export-dex-to=/data/local/tmp\n"
3542 "\n"
3543 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3544 " address (e.g. PC from crash dump)\n"
3545 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003546 "\n"
3547 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3548 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003549 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003550 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003551 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003552 " Example: --dump-imt=imt.txt\n"
3553 "\n"
3554 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3555 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003556 "\n";
3557
3558 return usage;
3559 }
3560
3561 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003562 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003563 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003564 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003565 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003566 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003567 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003568 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003569 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003570 bool disassemble_code_ = true;
3571 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003572 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003573 bool list_classes_ = false;
3574 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003575 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003576 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003577 uint32_t addr2instr_ = 0;
3578 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003579 const char* app_image_ = nullptr;
3580 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003581};
3582
Igor Murashkin37743352014-11-13 14:38:00 -08003583struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3584 virtual bool NeedsRuntime() OVERRIDE {
3585 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003586
Igor Murashkin37743352014-11-13 14:38:00 -08003587 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3588 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3589
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003590 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003591 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003592 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003593 args_->disassemble_code_,
3594 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003595 args_->class_filter_,
3596 args_->method_filter_,
3597 args_->list_classes_,
3598 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003599 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003600 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003601 args_->app_image_,
3602 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003603 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003604
Andreas Gampe9fded872016-09-25 16:08:35 -07003605 return (args_->boot_image_location_ != nullptr ||
3606 args_->image_location_ != nullptr ||
3607 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003608 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003609 }
3610
Igor Murashkin37743352014-11-13 14:38:00 -08003611 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3612 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003613 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003614
Mathieu Chartierd424d082014-10-15 10:31:46 -07003615 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003616
Andreas Gampec24f3992014-12-17 20:40:11 -08003617 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003618 // ELF has special kind of section called SHT_NOBITS which allows us to create
3619 // sections which exist but their data is omitted from the ELF file to save space.
3620 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3621 // with only debug data. We use it in similar way to exclude .rodata and .text.
3622 bool no_bits = args_->only_keep_debug_;
3623 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003624 } else {
3625 return DumpOat(nullptr,
3626 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003627 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003628 args_->os_) == EXIT_SUCCESS;
3629 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003630 }
3631
Igor Murashkin37743352014-11-13 14:38:00 -08003632 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3633 CHECK(args_ != nullptr);
3634
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003635 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3636 return IMTDumper::Dump(runtime,
3637 args_->imt_dump_,
3638 args_->imt_stat_dump_,
3639 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003640 }
3641
Igor Murashkin37743352014-11-13 14:38:00 -08003642 if (args_->oat_filename_ != nullptr) {
3643 return DumpOat(runtime,
3644 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003645 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003646 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003647 }
Igor Murashkin37743352014-11-13 14:38:00 -08003648
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003649 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003650 }
3651
Igor Murashkin37743352014-11-13 14:38:00 -08003652 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3653};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003654
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003655} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003656
3657int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003658 art::OatdumpMain main;
3659 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003660}