blob: bcf007b87ae9dbf8c5bfeb1d0281a9372976fa1c [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"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010039#include "compiled_method.h"
David Srbecky5d950762016-03-07 20:47:29 +000040#include "debug/elf_debug_writer.h"
41#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070042#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070043#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080044#include "disassembler.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"
Vladimir Marko74527972016-11-29 15:57:32 +000054#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000055#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056#include "mirror/array-inl.h"
57#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010058#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059#include "mirror/object-inl.h"
60#include "mirror/object_array-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070061#include "nativehelper/ScopedLocalRef.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080062#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010063#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070064#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080065#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070066#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070067#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070068#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070069#include "stack_map.h"
70#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080071#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070072#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010073#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080074#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000075#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070076#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070077
Igor Murashkin37743352014-11-13 14:38:00 -080078#include <sys/stat.h>
79#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070080
Igor Murashkin37743352014-11-13 14:38:00 -080081namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070082
Andreas Gampe46ee31b2016-12-14 10:11:49 -080083using android::base::StringPrintf;
84
Mathieu Chartiere401d142015-04-22 13:56:20 -070085const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080086 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070087 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070088 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010089 "kSaveAllCalleeSavesMethod",
90 "kSaveRefsOnlyMethod",
91 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010092 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -070093 "kSaveEverythingMethodForClinit",
94 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -070095};
96
97const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070098 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070099 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000100 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700101};
102
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700103// Map is so that we don't allocate multiple dex files for the same OatDexFile.
104static std::map<const OatFile::OatDexFile*,
105 std::unique_ptr<const DexFile>> opened_dex_files;
106
107const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
108 DCHECK(oat_dex_file != nullptr);
109 auto it = opened_dex_files.find(oat_dex_file);
110 if (it != opened_dex_files.end()) {
111 return it->second.get();
112 }
113 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
114 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
115 return ret;
116}
117
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800118template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100119class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700120 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000121 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
122 oat_file_(oat_file),
123 builder_(nullptr),
124 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
125 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700126 }
127
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700128 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800130 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000131 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000132
Andreas Gampe08c277c2017-04-26 22:22:15 -0700133 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
134 if (elf_file == nullptr) {
135 return false;
136 }
Vladimir Marko74527972016-11-29 15:57:32 +0000137 std::unique_ptr<linker::BufferedOutputStream> output_stream =
138 std::make_unique<linker::BufferedOutputStream>(
139 std::make_unique<linker::FileOutputStream>(elf_file.get()));
140 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141
142 builder_->Start();
143
144 auto* rodata = builder_->GetRoData();
145 auto* text = builder_->GetText();
146 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000147
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148 const uint8_t* rodata_begin = oat_file_->Begin();
149 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000150 if (no_bits_) {
151 rodata->WriteNoBitsSection(rodata_size);
152 } else {
153 rodata->Start();
154 rodata->WriteFully(rodata_begin, rodata_size);
155 rodata->End();
156 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000157
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
159 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000160 if (no_bits_) {
161 text->WriteNoBitsSection(text_size);
162 } else {
163 text->Start();
164 text->WriteFully(text_begin, text_size);
165 text->End();
166 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167
168 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000169 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170 }
171
Douglas Leung316a2182015-09-17 15:26:25 -0700172 if (isa == kMips || isa == kMips64) {
173 builder_->WriteMIPSabiflagsSection();
174 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 builder_->PrepareDynamicSection(elf_file->GetPath(),
176 rodata_size,
177 text_size,
178 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100179 oat_file_->BssMethodsOffset(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000180 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000181 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182
Vladimir Marko1b404a82017-09-01 13:35:26 +0100183 const OatHeader& oat_header = oat_file_->GetOatHeader();
184 #define DO_TRAMPOLINE(fn_name) \
185 if (oat_header.Get ## fn_name ## Offset() != 0) { \
186 debug::MethodDebugInfo info = {}; \
187 info.trampoline_name = #fn_name; \
188 info.isa = oat_header.GetInstructionSet(); \
189 info.is_code_address_text_relative = true; \
190 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
191 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
192 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
193 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
194 method_debug_infos_.push_back(std::move(info)); \
195 }
196 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
197 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
198 DO_TRAMPOLINE(JniDlsymLookup);
199 DO_TRAMPOLINE(QuickGenericJniTrampoline);
200 DO_TRAMPOLINE(QuickImtConflictTrampoline);
201 DO_TRAMPOLINE(QuickResolutionTrampoline);
202 DO_TRAMPOLINE(QuickToInterpreterBridge);
203 #undef DO_TRAMPOLINE
204
David Srbecky5d950762016-03-07 20:47:29 +0000205 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100206
207 // TODO: Try to symbolize link-time thunks?
208 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700209
David Srbecky5d950762016-03-07 20:47:29 +0000210 debug::WriteDebugInfo(builder_.get(),
211 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
212 dwarf::DW_DEBUG_FRAME_FORMAT,
213 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700214
David Srbecky6d8c8f02015-10-26 10:57:09 +0000215 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216
Andreas Gampe08c277c2017-04-26 22:22:15 -0700217 bool ret_value = builder_->Good();
218
219 builder_.reset();
220 output_stream.reset();
221
222 if (elf_file->FlushCloseOrErase() != 0) {
223 return false;
224 }
225 elf_file.reset();
226
227 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228 }
229
David Srbecky5d950762016-03-07 20:47:29 +0000230 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
232 for (size_t i = 0; i < oat_dex_files.size(); i++) {
233 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000235 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 }
237 }
238
David Srbecky5d950762016-03-07 20:47:29 +0000239 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700241 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
242 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 return;
244 }
245 for (size_t class_def_index = 0;
246 class_def_index < dex_file->NumClassDefs();
247 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700248 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
249 OatClassType type = oat_class.GetType();
250 switch (type) {
251 case kOatClassAllCompiled:
252 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000253 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 break;
255
256 case kOatClassNoneCompiled:
257 case kOatClassMax:
258 // Ignore.
259 break;
260 }
261 }
262 }
263
David Srbecky5d950762016-03-07 20:47:29 +0000264 void WalkOatClass(const OatFile::OatClass& oat_class,
265 const DexFile& dex_file,
266 uint32_t class_def_index) {
267 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700268 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700269 if (class_data == nullptr) { // empty class such as a marker interface?
270 return;
271 }
272 // Note: even if this is an interface or a native class, we still have to walk it, as there
273 // might be a static initializer.
274 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700275 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700276 it.SkipAllFields();
David Srbecky5d950762016-03-07 20:47:29 +0000277 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
278 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
279 dex_file,
280 class_def_index,
281 it.GetMemberIndex(),
282 it.GetMethodCodeItem(),
283 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700284 }
285 DCHECK(!it.HasNext());
286 }
287
David Srbecky5d950762016-03-07 20:47:29 +0000288 void WalkOatMethod(const OatFile::OatMethod& oat_method,
289 const DexFile& dex_file,
290 uint32_t class_def_index,
291 uint32_t dex_method_index,
292 const DexFile::CodeItem* code_item,
293 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700294 if ((method_access_flags & kAccAbstract) != 0) {
295 // Abstract method, no code.
296 return;
297 }
David Srbecky5d950762016-03-07 20:47:29 +0000298 const OatHeader& oat_header = oat_file_->GetOatHeader();
299 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
300 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700301 // No code.
302 return;
303 }
304
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100305 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
306 // Clear Thumb2 bit.
307 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
308
Vladimir Marko1b404a82017-09-01 13:35:26 +0100309 debug::MethodDebugInfo info = {};
310 DCHECK(info.trampoline_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000311 info.dex_file = &dex_file;
312 info.class_def_index = class_def_index;
313 info.dex_method_index = dex_method_index;
314 info.access_flags = method_access_flags;
315 info.code_item = code_item;
316 info.isa = oat_header.GetInstructionSet();
317 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
318 info.is_native_debuggable = oat_header.IsNativeDebuggable();
319 info.is_optimized = method_header->IsOptimized();
320 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100321 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000322 info.code_size = method_header->GetCodeSize();
323 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
324 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
325 info.cfi = ArrayRef<uint8_t>();
326 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700327 }
328
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700329 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700330 const OatFile* oat_file_;
Vladimir Marko74527972016-11-29 15:57:32 +0000331 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000332 std::vector<debug::MethodDebugInfo> method_debug_infos_;
333 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700334 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000335 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700336};
337
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700338class OatDumperOptions {
339 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100340 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100341 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700342 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700343 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800344 const char* class_filter,
345 const char* method_filter,
346 bool list_classes,
347 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000348 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800349 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800350 const char* app_image,
351 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800352 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100353 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100354 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700356 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800357 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000358 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800359 list_classes_(list_classes),
360 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000361 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800362 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800363 app_image_(app_image),
364 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800365 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800366 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100369 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370 const bool disassemble_code_;
371 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800372 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000373 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800374 const bool list_classes_;
375 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000376 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800378 const char* const app_image_;
379 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800380 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700381 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700382};
383
Elliott Hughese3c845c2012-02-28 17:23:01 -0800384class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700385 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100386 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000387 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800388 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700389 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800390 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800391 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
392 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700393 new DisassemblerOptions(
394 options_.absolute_addresses_,
395 oat_file.Begin(),
396 oat_file.End(),
397 true /* can_read_literals_ */,
398 Is64BitInstructionSet(instruction_set_)
399 ? &Thread::DumpThreadOffset<PointerSize::k64>
400 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800401 CHECK(options_.class_loader_ != nullptr);
402 CHECK(options_.class_filter_ != nullptr);
403 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800404 AddAllOffsets();
405 }
406
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700407 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700408 delete disassembler_;
409 }
410
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800411 InstructionSet GetInstructionSet() {
412 return instruction_set_;
413 }
414
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700415 bool Dump(std::ostream& os) {
416 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800417 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700418
419 os << "MAGIC:\n";
420 os << oat_header.GetMagic() << "\n\n";
421
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800422 os << "LOCATION:\n";
423 os << oat_file_.GetLocation() << "\n\n";
424
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800426 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700427
Elliott Hughesa72ec822012-03-05 17:12:22 -0800428 os << "INSTRUCTION SET:\n";
429 os << oat_header.GetInstructionSet() << "\n\n";
430
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700431 {
432 std::unique_ptr<const InstructionSetFeatures> features(
433 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
434 oat_header.GetInstructionSetFeaturesBitmap()));
435 os << "INSTRUCTION SET FEATURES:\n";
436 os << features->GetFeatureString() << "\n\n";
437 }
Dave Allison70202782013-10-22 17:52:19 -0700438
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439 os << "DEX FILE COUNT:\n";
440 os << oat_header.GetDexFileCount() << "\n\n";
441
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800442#define DUMP_OAT_HEADER_OFFSET(label, offset) \
443 os << label " OFFSET:\n"; \
444 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800445 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800446 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
447 } \
448 os << StringPrintf("\n\n");
449
450 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
451 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
452 GetInterpreterToInterpreterBridgeOffset);
453 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
454 GetInterpreterToCompiledCodeBridgeOffset);
455 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
456 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800457 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
458 GetQuickGenericJniTrampolineOffset);
459 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
460 GetQuickImtConflictTrampolineOffset);
461 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
462 GetQuickResolutionTrampolineOffset);
463 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
464 GetQuickToInterpreterBridgeOffset);
465#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700466
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700467 os << "IMAGE PATCH DELTA:\n";
468 os << StringPrintf("%d (0x%08x)\n\n",
469 oat_header.GetImagePatchDelta(),
470 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700471
Brian Carlstrom28db0122012-10-18 16:20:41 -0700472 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
473 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
474
475 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800476 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700477
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700478 // Print the key-value store.
479 {
480 os << "KEY VALUE STORE:\n";
481 size_t index = 0;
482 const char* key;
483 const char* value;
484 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
485 os << key << " = " << value << "\n";
486 index++;
487 }
488 os << "\n";
489 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700490
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800491 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700492 os << "BEGIN:\n";
493 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700494
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700495 os << "END:\n";
496 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
497 }
498
499 os << "SIZE:\n";
500 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700501
502 os << std::flush;
503
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800504 // If set, adjust relative address to be searched
505 if (options_.addr2instr_ != 0) {
506 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
507 os << "SEARCH ADDRESS (executable offset + input):\n";
508 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
509 }
510
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700511 // Dumping the dex file overview is compact enough to do even if header only.
512 DexFileData cumulative;
513 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
514 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
515 CHECK(oat_dex_file != nullptr);
516 std::string error_msg;
517 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
518 if (dex_file == nullptr) {
519 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
520 << error_msg;
521 continue;
522 }
523 DexFileData data(*dex_file);
524 os << "Dex file data for " << dex_file->GetLocation() << "\n";
525 data.Dump(os);
526 os << "\n";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700527 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
528 if (layout_sections != nullptr) {
529 os << "Layout data\n";
530 os << *layout_sections;
531 os << "\n";
532 }
533
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700534 cumulative.Add(data);
535 }
536 os << "Cumulative dex file data\n";
537 cumulative.Dump(os);
538 os << "\n";
539
David Brazdilc03d7b62016-03-02 12:18:03 +0000540 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000541 VariableIndentationOutputStream vios(&os);
542 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
543 if (vdex_header.IsValid()) {
544 std::string error_msg;
545 std::vector<const DexFile*> dex_files;
546 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
547 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
548 if (dex_file == nullptr) {
549 os << "Error opening dex file: " << error_msg << std::endl;
550 return false;
551 }
552 dex_files.push_back(dex_file);
553 }
554 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
555 deps.Dump(&vios);
556 } else {
557 os << "UNRECOGNIZED vdex file, magic "
558 << vdex_header.GetMagic()
559 << ", version "
560 << vdex_header.GetVersion()
561 << "\n";
562 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000563 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
564 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
565 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800566
David Brazdilc03d7b62016-03-02 12:18:03 +0000567 // If file export selected skip file analysis
568 if (options_.export_dex_location_) {
569 if (!ExportDexFile(os, *oat_dex_file)) {
570 success = false;
571 }
572 } else {
573 if (!DumpOatDexFile(os, *oat_dex_file)) {
574 success = false;
575 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800576 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700577 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700578 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000579
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800580 {
581 os << "OAT FILE STATS:\n";
582 VariableIndentationOutputStream vios(&os);
583 stats_.Dump(vios);
584 }
585
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700586 os << std::flush;
587 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700588 }
589
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800590 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700591 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
592 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800593 return 0; // Address not in oat file
594 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800595 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
596 reinterpret_cast<uintptr_t>(oat_file_.Begin());
597 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800599 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800600 return end_offset - begin_offset;
601 }
602
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800603 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700604 return oat_file_.GetOatHeader().GetInstructionSet();
605 }
606
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700607 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800608 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
609 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700610 CHECK(oat_dex_file != nullptr);
611 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700612 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
613 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700614 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
615 << "': " << error_msg;
616 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800617 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700618 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700619 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700620 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700621 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100622 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800623 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100624 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800625 }
626 }
627 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700628 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800629 }
630
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800631 struct Stats {
632 enum ByteKind {
633 kByteKindCode,
634 kByteKindQuickMethodHeader,
635 kByteKindCodeInfoLocationCatalog,
636 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800637 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800638 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000639 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800640 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800641 kByteKindStackMapNativePc,
642 kByteKindStackMapDexPc,
643 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800644 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800645 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000646 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700647 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800648 kByteKindInlineInfoDexPc,
649 kByteKindInlineInfoExtraData,
650 kByteKindInlineInfoDexRegisterMap,
651 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800652 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800653 // Special ranges for std::accumulate convenience.
654 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000655 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700656 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800657 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800658 };
659 int64_t bits[kByteKindCount] = {};
660 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
661 // deduplicated code and tables.
662 std::unordered_set<const void*> seen;
663
664 // Returns true if it was newly added.
665 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
666 if (seen.insert(address).second == true) {
667 // True means the address was not already in the set.
668 AddBits(kind, count);
669 return true;
670 }
671 return false;
672 }
673
674 void AddBits(ByteKind kind, int64_t count) {
675 bits[kind] += count;
676 }
677
678 void Dump(VariableIndentationOutputStream& os) {
679 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
680 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
681 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800682 Dump(os, "Code ", bits[kByteKindCode], sum);
683 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
684 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
685 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
686 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800687 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
688 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800689 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800690 // Stack map section.
691 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
692 bits + kByteKindStackMapLast + 1,
693 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800694 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800695 {
696 ScopedIndentation indent1(&os);
697 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800698 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800699 bits[kByteKindStackMapNativePc],
700 stack_map_bits,
701 "stack map");
702 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800703 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800704 bits[kByteKindStackMapDexPc],
705 stack_map_bits,
706 "stack map");
707 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800708 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800709 bits[kByteKindStackMapDexRegisterMap],
710 stack_map_bits,
711 "stack map");
712 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800713 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800714 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800715 stack_map_bits,
716 "stack map");
717 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800718 "StackMapRegisterMaskIndex ",
719 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800720 stack_map_bits,
721 "stack map");
722 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800723 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000724 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800725 stack_map_bits,
726 "stack map");
727 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800728 // Inline info section.
729 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
730 bits + kByteKindInlineInfoLast + 1,
731 0u);
732 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
733 {
734 ScopedIndentation indent1(&os);
735 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700736 "InlineInfoMethodIndexIdx ",
737 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800738 inline_info_bits,
739 "inline info");
740 Dump(os,
741 "InlineInfoDexPc ",
742 bits[kByteKindStackMapDexPc],
743 inline_info_bits,
744 "inline info");
745 Dump(os,
746 "InlineInfoExtraData ",
747 bits[kByteKindInlineInfoExtraData],
748 inline_info_bits,
749 "inline info");
750 Dump(os,
751 "InlineInfoDexRegisterMap ",
752 bits[kByteKindInlineInfoDexRegisterMap],
753 inline_info_bits,
754 "inline info");
755 Dump(os,
756 "InlineInfoIsLast ",
757 bits[kByteKindInlineInfoIsLast],
758 inline_info_bits,
759 "inline info");
760 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800761 }
762 os.Stream() << "\n" << std::flush;
763 }
764
765 private:
766 void Dump(VariableIndentationOutputStream& os,
767 const char* name,
768 int64_t size,
769 int64_t total,
770 const char* sum_of = "total") {
771 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
772 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
773 name,
774 size / kBitsPerByte,
775 percent,
776 sum_of);
777 }
778 };
779
Brian Carlstromaded5f72011-10-07 17:15:04 -0700780 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800781 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800782 // We don't know the length of the code for each method, but we need to know where to stop
783 // when disassembling. What we do know is that a region of code will be followed by some other
784 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
785 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800786 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
787 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700788 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700789 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700790 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
791 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700792 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
793 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800794 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800795 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800796 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800797 for (size_t class_def_index = 0;
798 class_def_index < dex_file->NumClassDefs();
799 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800800 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100801 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700802 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700803 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800804 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700805 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800806 uint32_t class_method_index = 0;
807 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100808 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800809 it.Next();
810 }
811 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100812 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800813 it.Next();
814 }
815 }
816 }
817 }
818
819 // If the last thing in the file is code for a method, there won't be an offset for the "next"
820 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
821 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800822 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800823 }
824
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
826 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
827 }
828
Elliott Hughese3c845c2012-02-28 17:23:01 -0800829 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800830 uint32_t code_offset = oat_method.GetCodeOffset();
831 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
832 code_offset &= ~0x1;
833 }
834 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800835 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800836 }
837
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700838 // Dex file data, may be for multiple different dex files.
839 class DexFileData {
840 public:
841 DexFileData() {}
842
843 explicit DexFileData(const DexFile& dex_file)
844 : num_string_ids_(dex_file.NumStringIds()),
845 num_method_ids_(dex_file.NumMethodIds()),
846 num_field_ids_(dex_file.NumFieldIds()),
847 num_type_ids_(dex_file.NumTypeIds()),
848 num_class_defs_(dex_file.NumClassDefs()) {
849 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
850 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
851 WalkClass(dex_file, class_def);
852 }
853 }
854
855 void Add(const DexFileData& other) {
856 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
857 num_string_ids_from_code_ += other.num_string_ids_from_code_;
858 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
859 dex_code_bytes_ += other.dex_code_bytes_;
860 num_string_ids_ += other.num_string_ids_;
861 num_method_ids_ += other.num_method_ids_;
862 num_field_ids_ += other.num_field_ids_;
863 num_type_ids_ += other.num_type_ids_;
864 num_class_defs_ += other.num_class_defs_;
865 }
866
867 void Dump(std::ostream& os) {
868 os << "Num string ids: " << num_string_ids_ << "\n";
869 os << "Num method ids: " << num_method_ids_ << "\n";
870 os << "Num field ids: " << num_field_ids_ << "\n";
871 os << "Num type ids: " << num_type_ids_ << "\n";
872 os << "Num class defs: " << num_class_defs_ << "\n";
873 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
874 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
875 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
876 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
877 }
878
879 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800880 // All of the elements from one container to another.
881 template <typename Dest, typename Src>
882 static void AddAll(Dest& dest, const Src& src) {
883 dest.insert(src.begin(), src.end());
884 }
885
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700886 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
887 const uint8_t* class_data = dex_file.GetClassData(class_def);
888 if (class_data == nullptr) { // empty class such as a marker interface?
889 return;
890 }
891 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700892 it.SkipAllFields();
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700893 while (it.HasNextDirectMethod()) {
894 WalkCodeItem(dex_file, it.GetMethodCodeItem());
895 it.Next();
896 }
897 while (it.HasNextVirtualMethod()) {
898 WalkCodeItem(dex_file, it.GetMethodCodeItem());
899 it.Next();
900 }
901 DCHECK(!it.HasNext());
902 }
903
904 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
905 if (code_item == nullptr) {
906 return;
907 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700908
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700909 const uint16_t* code_ptr = code_item->insns_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700910 // If we inserted a new dex code item pointer, add to total code bytes.
911 if (dex_code_item_ptrs_.insert(code_ptr).second) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700912 dex_code_bytes_ += code_item->insns_size_in_code_units_ * sizeof(code_ptr[0]);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700913 }
914
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700915 for (const Instruction& inst : code_item->Instructions()) {
916 switch (inst.Opcode()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700917 case Instruction::CONST_STRING: {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700918 const dex::StringIndex string_index(inst.VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700919 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
920 ++num_string_ids_from_code_;
921 break;
922 }
923 case Instruction::CONST_STRING_JUMBO: {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700924 const dex::StringIndex string_index(inst.VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700925 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
926 ++num_string_ids_from_code_;
927 break;
928 }
929 default:
930 break;
931 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700932 }
933 }
934
935 // Unique string ids loaded from dex code.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700936 std::set<StringReference> unique_string_ids_from_code_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700937
938 // Total string ids loaded from dex code.
939 size_t num_string_ids_from_code_ = 0;
940
941 // Unique code pointers.
942 std::set<const void*> dex_code_item_ptrs_;
943
944 // Total "unique" dex code bytes.
945 size_t dex_code_bytes_ = 0;
946
947 // Other dex ids.
948 size_t num_string_ids_ = 0;
949 size_t num_method_ids_ = 0;
950 size_t num_field_ids_ = 0;
951 size_t num_type_ids_ = 0;
952 size_t num_class_defs_ = 0;
953 };
954
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700955 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
956 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800957 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800959 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800960 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700961
Andreas Gampe2ba88952016-04-29 17:52:07 -0700962 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100963 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
964
965 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700966 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100967 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700968 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
969 dex_offset,
970 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700971
Andreas Gampe2ba88952016-04-29 17:52:07 -0700972 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700973 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700974 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
975 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700976 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 os << std::flush;
978 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700979 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980
Andreas Gampe2ba88952016-04-29 17:52:07 -0700981 // Print lookup table, if it exists.
982 if (oat_dex_file.GetLookupTableData() != nullptr) {
983 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
984 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700985 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700986 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
987 table_offset,
988 table_offset + table_size - 1);
989 }
990
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100991 VariableIndentationOutputStream vios(&os);
992 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800993 for (size_t class_def_index = 0;
994 class_def_index < dex_file->NumClassDefs();
995 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700996 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
997 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800998
999 // TODO: Support regex
1000 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
1001 continue;
1002 }
1003
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001004 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001005 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001006 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -08001007 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001008 << " (" << oat_class.GetStatus() << ")"
1009 << " (" << oat_class.GetType() << ")\n";
1010 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001011 if (options_.list_classes_) {
1012 continue;
1013 }
1014 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001015 success = false;
1016 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001017 if (stop_analysis) {
1018 os << std::flush;
1019 return success;
1020 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001021 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001022 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001023 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001024 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001025 }
1026
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001027 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1028 std::string error_msg;
1029 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
1030
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001031 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001032 if (dex_file == nullptr) {
1033 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1034 return false;
1035 }
1036 size_t fsize = oat_dex_file.FileSize();
1037
1038 // Some quick checks just in case
1039 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1040 os << "Invalid dex file\n";
1041 return false;
1042 }
1043
1044 // Verify output directory exists
1045 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1046 // TODO: Extend OS::DirectoryExists if symlink support is required
1047 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1048 return false;
1049 }
1050
1051 // Beautify path names
1052 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1053 return false;
1054 }
1055
1056 std::string dex_orig_name;
1057 size_t dex_orig_pos = dex_file_location.rfind('/');
1058 if (dex_orig_pos == std::string::npos)
1059 dex_orig_name = dex_file_location;
1060 else
1061 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1062
1063 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001064 if (dex_orig_name.size() == 8 &&
1065 dex_orig_name.compare("base.apk") == 0 &&
1066 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001067 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1068 size_t apk_orig_pos = dex_file_location.rfind('/');
1069 if (apk_orig_pos != std::string::npos) {
1070 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1071 }
1072 }
1073
1074 std::string out_dex_path(options_.export_dex_location_);
1075 if (out_dex_path.back() != '/') {
1076 out_dex_path.append("/");
1077 }
1078 out_dex_path.append(dex_orig_name);
1079 out_dex_path.append("_export.dex");
1080 if (out_dex_path.length() > PATH_MAX) {
1081 return false;
1082 }
1083
1084 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1085 if (file.get() == nullptr) {
1086 os << "Failed to open output dex file " << out_dex_path;
1087 return false;
1088 }
1089
1090 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1091 os << "Failed to write dex file";
1092 file->Erase();
1093 return false;
1094 }
1095
1096 if (file->FlushCloseOrErase() != 0) {
1097 os << "Flush and close failed";
1098 return false;
1099 }
1100
1101 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1102 os << std::flush;
1103
1104 return true;
1105 }
1106
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001107 bool DumpOatClass(VariableIndentationOutputStream* vios,
1108 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001109 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001111 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001112 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001113 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001114 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001115 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001116 }
1117 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001118 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001119 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001120 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001121 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001122 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001123 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 success = false;
1125 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001126 if (addr_found) {
1127 *stop_analysis = true;
1128 return success;
1129 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001130 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001131 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001132 }
Ian Rogers0571d352011-11-03 19:51:38 -07001133 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001134 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001135 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001136 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001137 success = false;
1138 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001139 if (addr_found) {
1140 *stop_analysis = true;
1141 return success;
1142 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001143 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001144 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001145 }
Ian Rogers0571d352011-11-03 19:51:38 -07001146 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001147 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001148 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001149 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001150
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001151 static constexpr uint32_t kPrologueBytes = 16;
1152
1153 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1154 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1155
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001156 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1157 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001158 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001159 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001160 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001161 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001162 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001163
1164 // TODO: Support regex
1165 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1166 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001167 return success;
1168 }
1169
David Sehr709b0702016-10-13 09:12:37 -07001170 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001171 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1172 class_method_index, pretty_method.c_str(),
1173 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001174 if (options_.list_methods_) return success;
1175
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001176 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1177 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1178 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1179 uint32_t code_offset = oat_method.GetCodeOffset();
1180 uint32_t code_size = oat_method.GetQuickCodeSize();
1181 if (resolved_addr2instr_ != 0) {
1182 if (resolved_addr2instr_ > code_offset + code_size) {
1183 return success;
1184 } else {
1185 *addr_found = true; // stop analyzing file at next iteration
1186 }
1187 }
1188
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001189 // Everything below is indented at least once.
1190 ScopedIndentation indent1(vios);
1191
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001192 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << "DEX CODE:\n";
1194 ScopedIndentation indent2(vios);
1195 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001196 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001197
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001198 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001199 std::unique_ptr<verifier::MethodVerifier> verifier;
1200 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001201 // We need to have the handle scope stay live until after the verifier since the verifier has
1202 // a handle to the dex cache from hs.
1203 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1205 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001206 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001208 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001209 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001210 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001211 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001212 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001213 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001214 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001216 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001218 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1219 oat_method_offsets_offset, oat_file_.Size());
1220 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001221 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001222 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001223 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001224
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 ScopedIndentation indent2(vios);
1226 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1228 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 vios->Stream() << StringPrintf("WARNING: "
1230 "code offset 0x%08x is past end of file 0x%08zx.\n",
1231 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 success = false;
1233 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001234 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001235 }
1236 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001238 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1239 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001240 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1241 sizeof(*method_header) * kBitsPerByte,
1242 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001243 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001245 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001246 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001248 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1250 method_header_offset, oat_file_.Size());
1251 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001252 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253 return false;
1254 }
1255
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001256 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001257 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001258 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001260 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001261 uint32_t vmap_table_offset = method_header ==
1262 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001264
1265 size_t vmap_table_offset_limit =
1266 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1267 ? oat_file_.GetVdexFile()->Size()
1268 : method_header->GetCode() - oat_file_.Begin();
1269 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001270 vios->Stream() << StringPrintf("WARNING: "
1271 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1272 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001273 vmap_table_offset,
1274 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001276 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001277 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001278 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001279 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001280 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001282 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001283
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001284 ScopedIndentation indent2(vios);
1285 vios->Stream()
1286 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1287 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1288 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1289 vios->Stream() << "\n";
1290 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1291 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1292 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001293 }
1294 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001295 // Based on spill masks from QuickMethodFrameInfo so placed
1296 // after it is dumped, but useful for understanding quick
1297 // code, so dumped here.
1298 ScopedIndentation indent2(vios);
1299 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001300 }
1301 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001302 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001303 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1304 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001305 ScopedIndentation indent2(vios);
1306 vios->Stream() << StringPrintf("WARNING: "
1307 "code size offset 0x%08x is past end of file 0x%08zx.",
1308 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001309 success = false;
1310 } else {
1311 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001312 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1313 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001314 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001315
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001316 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001317 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001318 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001319 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1320 code_offset,
1321 code_size_offset,
1322 code_size,
1323 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001324
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001326 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001327 vios->Stream() << StringPrintf("WARNING: "
1328 "start of code at 0x%08x is past end of file 0x%08zx.",
1329 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001330 success = false;
1331 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001332 vios->Stream() << StringPrintf(
1333 "WARNING: "
1334 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1335 "code size is 0x%08x loaded from offset 0x%08x.\n",
1336 aligned_code_end, oat_file_.Size(),
1337 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001338 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001339 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001340 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001341 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001342 }
1343 }
1344 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001345 vios->Stream() << StringPrintf(
1346 "WARNING: "
1347 "code size %d is bigger than max expected threshold of %d. "
1348 "code size is 0x%08x loaded from offset 0x%08x.\n",
1349 code_size, kMaxCodeSize,
1350 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001351 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001352 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001353 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001354 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001355 }
1356 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001357 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001358 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001359 }
1360 }
1361 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001362 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001363 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001364 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001365
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001366 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1367 if (spill_mask == 0) {
1368 return;
1369 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001370 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001371 for (size_t i = 0; i < 32; i++) {
1372 if ((spill_mask & (1 << i)) != 0) {
1373 if (is_float) {
1374 os << "fr" << i;
1375 } else {
1376 os << "r" << i;
1377 }
1378 spill_mask ^= 1 << i; // clear bit
1379 if (spill_mask != 0) {
1380 os << ", ";
1381 } else {
1382 break;
1383 }
1384 }
1385 }
1386 os << ")";
1387 }
1388
Roland Levillain442b46a2015-02-18 16:54:21 +00001389 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001390 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001391 const OatFile::OatMethod& oat_method,
1392 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001393 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1394 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001395 const void* raw_code_info = oat_method.GetVmapTable();
1396 if (raw_code_info != nullptr) {
1397 CodeInfo code_info(raw_code_info);
1398 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001399 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001400 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1401 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001402 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001403 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1404 // We don't encode the size in the table, so just emit that we have quickened
1405 // information.
1406 ScopedIndentation indent(vios);
1407 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001408 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001409 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001410 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001411 }
1412
1413 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001414 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001415 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001416 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001417 const DexFile::CodeItem& code_item,
1418 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001419 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001420 oat_method.GetCodeOffset(),
1421 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001422 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001423 instruction_set_,
1424 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001425 }
1426
Andreas Gampe36a296f2017-06-13 14:11:11 -07001427 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1428 // According to stack model, the first out is above the Method referernce.
1429 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1430 }
1431
1432 static uint32_t GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
1433 uint32_t core_spills,
1434 uint32_t fp_spills,
1435 size_t frame_size,
1436 int reg,
1437 InstructionSet isa) {
1438 PointerSize pointer_size = InstructionSetPointerSize(isa);
1439 if (kIsDebugBuild) {
1440 auto* runtime = Runtime::Current();
1441 if (runtime != nullptr) {
1442 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1443 }
1444 }
1445 DCHECK_ALIGNED(frame_size, kStackAlignment);
1446 DCHECK_NE(reg, -1);
1447 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1448 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1449 + sizeof(uint32_t); // Filler.
1450 int num_regs = code_item->registers_size_ - code_item->ins_size_;
1451 int temp_threshold = code_item->registers_size_;
1452 const int max_num_special_temps = 1;
1453 if (reg == temp_threshold) {
1454 // The current method pointer corresponds to special location on stack.
1455 return 0;
1456 } else if (reg >= temp_threshold + max_num_special_temps) {
1457 /*
1458 * Special temporaries may have custom locations and the logic above deals with that.
1459 * However, non-special temporaries are placed relative to the outs.
1460 */
1461 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
1462 + static_cast<size_t>(pointer_size) /* art method */;
1463 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1464 return temps_start + relative_offset;
1465 } else if (reg < num_regs) {
1466 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1467 return locals_start + (reg * sizeof(uint32_t));
1468 } else {
1469 // Handle ins.
1470 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1471 + static_cast<size_t>(pointer_size) /* art method */;
1472 }
1473 }
1474
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001475 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1476 const DexFile::CodeItem* code_item) {
1477 if (code_item != nullptr) {
1478 size_t num_locals_ins = code_item->registers_size_;
1479 size_t num_ins = code_item->ins_size_;
1480 size_t num_locals = num_locals_ins - num_ins;
1481 size_t num_outs = code_item->outs_size_;
1482
1483 os << "vr_stack_locations:";
1484 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1485 // For readability, delimit the different kinds of VRs.
1486 if (reg == num_locals_ins) {
1487 os << "\n\tmethod*:";
1488 } else if (reg == num_locals && num_ins > 0) {
1489 os << "\n\tins:";
1490 } else if (reg == 0 && num_locals > 0) {
1491 os << "\n\tlocals:";
1492 }
1493
Andreas Gampe36a296f2017-06-13 14:11:11 -07001494 uint32_t offset = GetVRegOffsetFromQuickCode(code_item,
1495 oat_method.GetCoreSpillMask(),
1496 oat_method.GetFpSpillMask(),
1497 oat_method.GetFrameSizeInBytes(),
1498 reg,
1499 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001500 os << " v" << reg << "[sp + #" << offset << "]";
1501 }
1502
1503 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1504 if (out_reg == 0) {
1505 os << "\n\touts:";
1506 }
1507
Andreas Gampe36a296f2017-06-13 14:11:11 -07001508 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001509 os << " v" << out_reg << "[sp + #" << offset << "]";
1510 }
1511
1512 os << "\n";
1513 }
1514 }
1515
Ian Rogersb23a7722012-10-09 16:54:26 -07001516 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001517 if (code_item != nullptr) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001518 IterationRange<DexInstructionIterator> instructions = code_item->Instructions();
1519 for (auto it = instructions.begin(); it != instructions.end(); ++it) {
1520 const size_t dex_pc = it.GetDexPC(instructions.begin());
1521 os << StringPrintf("0x%04zx: ", dex_pc) << it->DumpHexLE(5)
1522 << StringPrintf("\t| %s\n", it->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001523 }
1524 }
1525 }
1526
Roland Levillainf2650d12015-05-28 14:53:28 +01001527 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1528 // the optimizing compiler?
1529 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1530 const DexFile::CodeItem* code_item) {
1531 // If the native GC map is null and the Dex `code_item` is not
1532 // null, then this method has been compiled with the optimizing
1533 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001534 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001535 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001536 code_item != nullptr;
1537 }
1538
1539 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1540 // the dextodex compiler?
1541 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1542 const DexFile::CodeItem* code_item) {
1543 // If the quick code is null, the Dex `code_item` is not
1544 // null, and the vmap table is not null, then this method has been compiled
1545 // with the dextodex compiler.
1546 return oat_method.GetQuickCode() == nullptr &&
1547 oat_method.GetVmapTable() != nullptr &&
1548 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001549 }
1550
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001551 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001552 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001553 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001554 const DexFile* dex_file,
1555 const DexFile::ClassDef& class_def,
1556 const DexFile::CodeItem* code_item,
1557 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 if ((method_access_flags & kAccNative) == 0) {
1559 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001560 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001561 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001562 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001563 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001564 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001565 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001566 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001567 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001569
1570 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 }
1572
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001573 // The StackMapsHelper provides the stack maps in the native PC order.
1574 // For identical native PCs, the order from the CodeInfo is preserved.
1575 class StackMapsHelper {
1576 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001577 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001578 : code_info_(raw_code_info),
1579 encoding_(code_info_.ExtractEncoding()),
1580 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1581 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001582 offset_(static_cast<uint32_t>(-1)),
1583 stack_map_index_(0u),
1584 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001585 if (number_of_stack_maps_ != 0u) {
1586 // Check if native PCs are ordered.
1587 bool ordered = true;
1588 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1589 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1590 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001591 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1592 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001593 ordered = false;
1594 break;
1595 }
1596 last = current;
1597 }
1598 if (!ordered) {
1599 // Create indirection indexes for access in native PC order. We do not optimize
1600 // for the fact that there can currently be only two separately ordered ranges,
1601 // namely normal stack maps and catch-point stack maps.
1602 indexes_.resize(number_of_stack_maps_);
1603 std::iota(indexes_.begin(), indexes_.end(), 0u);
1604 std::sort(indexes_.begin(),
1605 indexes_.end(),
1606 [this](size_t lhs, size_t rhs) {
1607 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001608 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001609 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001610 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001611 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001612 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001613 // If the PCs are the same, compare indexes to preserve the original order.
1614 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1615 });
1616 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001617 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001618 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001619 }
1620 }
1621
1622 const CodeInfo& GetCodeInfo() const {
1623 return code_info_;
1624 }
1625
1626 const CodeInfoEncoding& GetEncoding() const {
1627 return encoding_;
1628 }
1629
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001630 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001631 return offset_;
1632 }
1633
1634 StackMap GetStackMap() const {
1635 return GetStackMapAt(stack_map_index_);
1636 }
1637
1638 void Next() {
1639 ++stack_map_index_;
1640 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001641 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001642 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001643 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001644 }
1645
1646 private:
1647 StackMap GetStackMapAt(size_t i) const {
1648 if (!indexes_.empty()) {
1649 i = indexes_[i];
1650 }
1651 DCHECK_LT(i, number_of_stack_maps_);
1652 return code_info_.GetStackMapAt(i, encoding_);
1653 }
1654
1655 const CodeInfo code_info_;
1656 const CodeInfoEncoding encoding_;
1657 const size_t number_of_stack_maps_;
1658 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001659 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001660 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001661 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001662 };
1663
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001664 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001665 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1666 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001667 const void* quick_code = oat_method.GetQuickCode();
1668
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001669 if (code_size == 0) {
1670 code_size = oat_method.GetQuickCodeSize();
1671 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001672 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001673 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001674 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001675 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1676 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001677 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001678 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001679 {
1680 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001681 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001682 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001683 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001684 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001685 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001686 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001687 stats_.AddBits(
1688 Stats::kByteKindStackMapNativePc,
1689 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1690 stats_.AddBits(
1691 Stats::kByteKindStackMapDexPc,
1692 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1693 stats_.AddBits(
1694 Stats::kByteKindStackMapDexRegisterMap,
1695 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1696 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001697 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001698 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1699 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001700 Stats::kByteKindStackMapRegisterMaskIndex,
1701 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001702 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001703 Stats::kByteKindStackMapStackMaskIndex,
1704 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001705
1706 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001707 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001708 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001709 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001710
1711 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001712 stats_.AddBits(
1713 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001714 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001715
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001716 // Invoke infos
1717 if (encoding.invoke_info.num_entries > 0u) {
1718 stats_.AddBits(
1719 Stats::kByteKindCodeInfoInvokeInfo,
1720 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1721 }
1722
Mathieu Chartier1e083792017-02-08 13:30:04 -08001723 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001724 const size_t location_catalog_bytes =
1725 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1726 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1727 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001728 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001729 const size_t dex_register_bytes =
1730 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1731 stats_.AddBits(
1732 Stats::kByteKindCodeInfoDexRegisterMap,
1733 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001734
1735 // Inline infos.
1736 const size_t num_inline_infos = encoding.inline_info.num_entries;
1737 if (num_inline_infos > 0u) {
1738 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001739 Stats::kByteKindInlineInfoMethodIndexIdx,
1740 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1741 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001742 stats_.AddBits(
1743 Stats::kByteKindInlineInfoDexPc,
1744 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1745 stats_.AddBits(
1746 Stats::kByteKindInlineInfoExtraData,
1747 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1748 stats_.AddBits(
1749 Stats::kByteKindInlineInfoDexRegisterMap,
1750 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1751 num_inline_infos);
1752 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1753 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001754 }
1755 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001756 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1757 size_t offset = 0;
1758 while (offset < code_size) {
1759 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1760 if (offset == helper.GetOffset()) {
1761 ScopedIndentation indent1(vios);
1762 StackMap stack_map = helper.GetStackMap();
1763 DCHECK(stack_map.IsValid());
1764 stack_map.Dump(vios,
1765 helper.GetCodeInfo(),
1766 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001767 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001768 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001769 code_item->registers_size_,
1770 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001771 do {
1772 helper.Next();
1773 // There may be multiple stack maps at a given PC. We display only the first one.
1774 } while (offset == helper.GetOffset());
1775 }
1776 DCHECK_LT(offset, helper.GetOffset());
1777 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001778 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001779 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1780 size_t offset = 0;
1781 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001782 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001783 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001784 }
1785 }
1786
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001787 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001788 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001789 const OatDumperOptions& options_;
1790 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001791 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001792 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001793 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001794 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001795};
1796
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001797class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001798 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001799 ImageDumper(std::ostream* os,
1800 gc::space::ImageSpace& image_space,
1801 const ImageHeader& image_header,
1802 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001803 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001804 vios_(os),
1805 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001806 image_space_(image_space),
1807 image_header_(image_header),
1808 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001809
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001810 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001811 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001812 std::ostream& indent_os = vios_.Stream();
1813
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001814 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001815
Jeff Haodcdc85b2015-12-04 14:06:18 -08001816 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1817
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001818 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001819
Mathieu Chartiere401d142015-04-22 13:56:20 -07001820 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1821
1822 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1823 auto section = static_cast<ImageHeader::ImageSections>(i);
1824 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1825 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001826
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001827 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001828
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001829 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001830
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001831 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1832
1833 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1834
1835 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001836
Alex Lighta59dd802014-07-02 16:28:08 -07001837 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1838
Igor Murashkin46774762014-10-22 11:37:02 -07001839 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1840
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001841 {
1842 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001843 static_assert(arraysize(image_roots_descriptions_) ==
1844 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001845 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1846 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1848 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001849 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001850 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001851 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001852 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001853 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001855 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1856 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001857 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001858 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1859 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001860 run++;
1861 } else {
1862 break;
1863 }
1864 }
1865 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001866 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001867 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001868 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001869 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001870 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001871 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001872 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001874 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001876 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001877 }
1878 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001879 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001880
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001881 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001882 os << "METHOD ROOTS\n";
1883 static_assert(arraysize(image_methods_descriptions_) ==
1884 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1885 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1886 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1887 const char* description = image_methods_descriptions_[i];
1888 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001889 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001890 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001891 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001892 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001893
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001894 Runtime* const runtime = Runtime::Current();
1895 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001896 std::string image_filename = image_space_.GetImageFilename();
1897 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001899 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001900 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001901 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001902 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001903 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1904 }
1905 if (oat_file == nullptr) {
1906 oat_file = OatFile::Open(oat_location,
1907 oat_location,
1908 nullptr,
1909 nullptr,
1910 false,
1911 /*low_4gb*/false,
1912 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001913 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001914 }
1915 if (oat_file == nullptr) {
1916 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1917 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001918 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001920
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001921 stats_.oat_file_bytes = oat_file->Size();
1922
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001923 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001924
Mathieu Chartier02e25112013-08-14 16:14:24 -07001925 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001926 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001927 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1928 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001929 }
1930
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001932
Jeff Haodcdc85b2015-12-04 14:06:18 -08001933 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001934 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001935 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001936 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001937 {
1938 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1939 heap->FlushAllocStack();
1940 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001941 // Since FlushAllocStack() above resets the (active) allocation
1942 // stack. Need to revoke the thread-local allocation stacks that
1943 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001944 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001945 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001946 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001947 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001948 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001949 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001950 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001951 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001952 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001953 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001954 ObjPtr<mirror::DexCache> dex_cache =
1955 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001956 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001957 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001958 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001959 }
1960 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001961 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1962 DumpObject(obj);
1963 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001964 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001965 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001966 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001967 indent_os << "\n";
1968 // TODO: Dump fields.
1969 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001970 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001971 image_header_.VisitPackedArtMethods(&visitor,
1972 image_space_.Begin(),
1973 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001974 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001975 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001976 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001977 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001978 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001979 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001980 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1981 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001982 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001983 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001984 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001985 // If the image is compressed, adjust to decompressed size.
1986 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1987 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1988 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1989 }
1990 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001991 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001992 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001993 const auto& object_section = image_header_.GetObjectsSection();
1994 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001996 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1997 const auto& intern_section = image_header_.GetInternedStringsSection();
1998 const auto& class_table_section = image_header_.GetClassTableSection();
1999 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002000
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002001 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002002
2003 // Objects are kObjectAlignment-aligned.
2004 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2005 if (object_section.Offset() > header_bytes) {
2006 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2007 }
2008
2009 // Field section is 4-byte aligned.
2010 constexpr size_t kFieldSectionAlignment = 4U;
2011 uint32_t end_objects = object_section.Offset() + object_section.Size();
2012 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2013 stats_.alignment_bytes += field_section.Offset() - end_objects;
2014
2015 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2016 uint32_t end_fields = field_section.Offset() + field_section.Size();
2017 CHECK_ALIGNED(method_section.Offset(), 4);
2018 stats_.alignment_bytes += method_section.Offset() - end_fields;
2019
2020 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2021 uint32_t end_methods = method_section.Offset() + method_section.Size();
2022 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2023 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2024
2025 // Intern table is 8-byte aligned.
2026 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002027 CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t));
Andreas Gampeace0dc12016-01-20 13:33:13 -08002028 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2029
2030 // Add space between intern table and class table.
2031 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2032 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2033
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002034 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2035 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002036 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002037 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002038
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039 stats_.bitmap_bytes += bitmap_section.Size();
2040 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002041 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002042 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002043 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002044 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002045 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002046 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002047
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002048 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002049
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002050 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002051 }
2052
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002053 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002054 class DumpArtMethodVisitor : public ArtMethodVisitor {
2055 public:
2056 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2057
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002058 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002059 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002060 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002061 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002062 indent_os << "\n";
2063 }
2064
2065 private:
2066 ImageDumper* const image_dumper_;
2067 };
2068
Mathieu Chartier3398c782016-09-30 10:27:43 -07002069 static void PrettyObjectValue(std::ostream& os,
2070 ObjPtr<mirror::Class> type,
2071 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002072 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002073 CHECK(type != nullptr);
2074 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002075 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002076 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002077 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002078 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002079 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002080 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002081 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002082 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002083 } else {
David Sehr709b0702016-10-13 09:12:37 -07002084 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002085 }
2086 }
2087
Mathieu Chartier3398c782016-09-30 10:27:43 -07002088 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002089 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002090 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 switch (field->GetTypeAsPrimitiveType()) {
2092 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002093 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002094 break;
2095 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002097 break;
2098 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002099 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002100 break;
2101 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002102 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002103 break;
2104 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002105 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002106 break;
2107 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002108 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002109 break;
2110 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002111 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002112 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002113 break;
2114 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002115 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002116 break;
2117 case Primitive::kPrimNot: {
2118 // Get the value, don't compute the type unless it is non-null as we don't want
2119 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002120 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002121 if (value == nullptr) {
2122 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002123 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002124 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002125 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002126 if (field_type != nullptr) {
2127 PrettyObjectValue(os, field_type, value);
2128 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002129 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002130 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002131 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2132 }
Ian Rogers50239c72013-06-17 14:53:22 -07002133 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002134 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002135 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002136 default:
2137 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2138 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002139 }
2140 }
2141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002142 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002143 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002144 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002145 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002146 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002147 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002148 for (ArtField& field : klass->GetIFields()) {
2149 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002150 }
2151 }
2152
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002153 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002154 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002155 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002156
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002157 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002158 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002159 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002160 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002161 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002162 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002163 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002164 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002165 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002166 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002167 }
2168
Mathieu Chartiere401d142015-04-22 13:56:20 -07002169 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002170 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002171 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2172 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002173 return 0;
2174 }
2175 return oat_code_begin[-1];
2176 }
2177
Mathieu Chartiere401d142015-04-22 13:56:20 -07002178 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002179 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002180 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002181 if (oat_code_begin == nullptr) {
2182 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002183 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002184 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002185 }
2186
Andreas Gampe0c183382017-07-13 22:26:24 -07002187 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002188 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002189 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002190 return;
2191 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002192
2193 size_t object_bytes = obj->SizeOf();
2194 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002195 stats_.object_bytes += object_bytes;
2196 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002197
Andreas Gampe0c183382017-07-13 22:26:24 -07002198 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002199
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002200 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002201 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002202 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002203 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002204 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002205 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002206 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2207 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002208 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002209 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002210 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002211 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002212 } else {
David Sehr709b0702016-10-13 09:12:37 -07002213 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002214 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002215 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002216 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002217 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002218 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002219 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2220 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002221 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002222 size_t run = 0;
2223 for (int32_t j = i + 1; j < length; j++) {
2224 if (value == obj_array->Get(j)) {
2225 run++;
2226 } else {
2227 break;
2228 }
2229 }
2230 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002231 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002232 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002233 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002234 i = i + run;
2235 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002236 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002237 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002238 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002239 }
2240 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002241 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002242 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002243 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002244 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002245 for (ArtField& field : klass->GetSFields()) {
2246 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002247 }
2248 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002249 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002250 auto it = dex_caches_.find(obj);
2251 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002252 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002253 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002254 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002255 size_t num_methods = dex_cache->NumResolvedMethods();
2256 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002257 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002258 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002259 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002260 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002261 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2262 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002263 size_t run = 0;
2264 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002265 j != length &&
2266 elem == mirror::DexCache::GetNativePairPtrSize(
2267 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002268 ++j) {
2269 ++run;
2270 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002271 if (run == 0) {
2272 os << StringPrintf("%zd: ", i);
2273 } else {
2274 os << StringPrintf("%zd to %zd: ", i, i + run);
2275 i = i + run;
2276 }
2277 std::string msg;
2278 if (elem == nullptr) {
2279 msg = "null";
2280 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002281 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002282 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002283 } else {
2284 msg = "<not in method section>";
2285 }
2286 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002287 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002288 }
2289 size_t num_fields = dex_cache->NumResolvedFields();
2290 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002291 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002292 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002293 auto* resolved_fields = dex_cache->GetResolvedFields();
2294 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002295 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002296 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002297 size_t run = 0;
2298 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002299 j != length &&
2300 elem == mirror::DexCache::GetNativePairPtrSize(
2301 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002302 ++j) {
2303 ++run;
2304 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002305 if (run == 0) {
2306 os << StringPrintf("%zd: ", i);
2307 } else {
2308 os << StringPrintf("%zd to %zd: ", i, i + run);
2309 i = i + run;
2310 }
2311 std::string msg;
2312 if (elem == nullptr) {
2313 msg = "null";
2314 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002315 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002316 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002317 } else {
2318 msg = "<not in field section>";
2319 }
2320 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002321 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002322 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002323 size_t num_types = dex_cache->NumResolvedTypes();
2324 if (num_types != 0u) {
2325 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002326 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002327 auto* resolved_types = dex_cache->GetResolvedTypes();
2328 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002329 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002330 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002331 for (size_t j = i + 1; j != num_types; ++j) {
2332 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2333 if (pair.index != other_pair.index ||
2334 pair.object.Read() != other_pair.object.Read()) {
2335 break;
2336 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002337 ++run;
2338 }
2339 if (run == 0) {
2340 os << StringPrintf("%zd: ", i);
2341 } else {
2342 os << StringPrintf("%zd to %zd: ", i, i + run);
2343 i = i + run;
2344 }
2345 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002346 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002347 if (elem == nullptr) {
2348 msg = "null";
2349 } else {
David Sehr709b0702016-10-13 09:12:37 -07002350 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002351 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002352 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002353 }
2354 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002355 }
2356 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002357 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002358 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002359 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002360
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002361 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002362 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002363 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002364 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2365 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002366 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002367 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2368 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002369 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002370 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002371 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2372 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002373 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002374 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002375 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002376 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2377 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002378 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002379 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002380 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002381 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002382 } else if (method->IsRuntimeMethod()) {
2383 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2384 if (table != nullptr) {
2385 indent_os << "IMT conflict table " << table << " method: ";
2386 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002387 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2388 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002389 }
2390 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002391 } else {
2392 const DexFile::CodeItem* code_item = method->GetCodeItem();
2393 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002394 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002395
2396 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002397 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002398 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002399 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002400 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002401 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002402 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002403 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002404 }
2405
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002406 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2407 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002408 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002409 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410 if (method->IsConstructor()) {
2411 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002412 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002413 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002414 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002415 }
2416 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002418 }
2419 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002420 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002421
Igor Murashkin7617abd2015-07-10 18:27:47 -07002422 uint32_t method_access_flags = method->GetAccessFlags();
2423
Mathieu Chartiere401d142015-04-22 13:56:20 -07002424 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002425 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2426 dex_instruction_bytes,
2427 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002428 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002429
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002430 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002431 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002432
2433 double expansion =
2434 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002435 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002436 }
2437 }
2438
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002439 std::set<const void*> already_seen_;
2440 // Compute the size of the given data within the oat file and whether this is the first time
2441 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002442 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002443 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002444 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002445 already_seen_.insert(oat_data);
2446 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002447 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002448 }
2449 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002450 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002451
2452 public:
2453 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002454 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002455 size_t file_bytes;
2456
2457 size_t header_bytes;
2458 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002459 size_t art_field_bytes;
2460 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002461 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002462 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002463 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002464 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002465 size_t alignment_bytes;
2466
2467 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002468 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002469 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002470 size_t class_initializer_code_bytes;
2471 size_t large_initializer_code_bytes;
2472 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002473
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002474 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475
2476 size_t dex_instruction_bytes;
2477
Mathieu Chartiere401d142015-04-22 13:56:20 -07002478 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002479 std::vector<size_t> method_outlier_size;
2480 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002481 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002482
Roland Levillain3887c462015-08-12 18:15:42 +01002483 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002484 : oat_file_bytes(0),
2485 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002486 header_bytes(0),
2487 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002488 art_field_bytes(0),
2489 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002490 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002491 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002492 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002493 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002494 alignment_bytes(0),
2495 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002496 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002497 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002498 class_initializer_code_bytes(0),
2499 large_initializer_code_bytes(0),
2500 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002501 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002502 dex_instruction_bytes(0) {}
2503
Elliott Hughesa0e18062012-04-13 15:59:59 -07002504 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002505 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002506 size_t bytes;
2507 size_t count;
2508 };
2509 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2510 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002511
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002512 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002513 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2514 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002515 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002516 it->second.count += 1;
2517 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002518 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002519 }
2520 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002521
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002522 double PercentOfOatBytes(size_t size) {
2523 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2524 }
2525
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002526 double PercentOfFileBytes(size_t size) {
2527 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2528 }
2529
2530 double PercentOfObjectBytes(size_t size) {
2531 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2532 }
2533
Mathieu Chartiere401d142015-04-22 13:56:20 -07002534 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002535 method_outlier_size.push_back(total_size);
2536 method_outlier_expansion.push_back(expansion);
2537 method_outlier.push_back(method);
2538 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002539
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002540 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002541 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002542 size_t sum_of_sizes = 0;
2543 size_t sum_of_sizes_squared = 0;
2544 size_t sum_of_expansion = 0;
2545 size_t sum_of_expansion_squared = 0;
2546 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002547 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002548 return;
2549 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002550 for (size_t i = 0; i < n; i++) {
2551 size_t cur_size = method_outlier_size[i];
2552 sum_of_sizes += cur_size;
2553 sum_of_sizes_squared += cur_size * cur_size;
2554 double cur_expansion = method_outlier_expansion[i];
2555 sum_of_expansion += cur_expansion;
2556 sum_of_expansion_squared += cur_expansion * cur_expansion;
2557 }
2558 size_t size_mean = sum_of_sizes / n;
2559 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2560 double expansion_mean = sum_of_expansion / n;
2561 double expansion_variance =
2562 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2563
2564 // Dump methods whose size is a certain number of standard deviations from the mean
2565 size_t dumped_values = 0;
2566 size_t skipped_values = 0;
2567 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2568 size_t cur_size_variance = i * i * size_variance;
2569 bool first = true;
2570 for (size_t j = 0; j < n; j++) {
2571 size_t cur_size = method_outlier_size[j];
2572 if (cur_size > size_mean) {
2573 size_t cur_var = cur_size - size_mean;
2574 cur_var = cur_var * cur_var;
2575 if (cur_var > cur_size_variance) {
2576 if (dumped_values > 20) {
2577 if (i == 1) {
2578 skipped_values++;
2579 } else {
2580 i = 2; // jump to counting for 1 standard deviation
2581 break;
2582 }
2583 } else {
2584 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002585 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002586 first = false;
2587 }
David Sehr709b0702016-10-13 09:12:37 -07002588 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002589 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002590 method_outlier_size[j] = 0; // don't consider this method again
2591 dumped_values++;
2592 }
2593 }
2594 }
2595 }
2596 }
2597 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002598 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002599 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002600 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002601 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002602
2603 // Dump methods whose expansion is a certain number of standard deviations from the mean
2604 dumped_values = 0;
2605 skipped_values = 0;
2606 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2607 double cur_expansion_variance = i * i * expansion_variance;
2608 bool first = true;
2609 for (size_t j = 0; j < n; j++) {
2610 double cur_expansion = method_outlier_expansion[j];
2611 if (cur_expansion > expansion_mean) {
2612 size_t cur_var = cur_expansion - expansion_mean;
2613 cur_var = cur_var * cur_var;
2614 if (cur_var > cur_expansion_variance) {
2615 if (dumped_values > 20) {
2616 if (i == 1) {
2617 skipped_values++;
2618 } else {
2619 i = 2; // jump to counting for 1 standard deviation
2620 break;
2621 }
2622 } else {
2623 if (first) {
2624 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002625 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002626 first = false;
2627 }
David Sehr709b0702016-10-13 09:12:37 -07002628 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002629 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002630 method_outlier_expansion[j] = 0.0; // don't consider this method again
2631 dumped_values++;
2632 }
2633 }
2634 }
2635 }
2636 }
2637 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002638 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002639 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002640 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002641 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002642 }
2643
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002644 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002645 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 {
2647 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2648 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002649 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2650 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2651 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2652 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2653 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2654 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002655 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002656 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2657 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002658 header_bytes, PercentOfFileBytes(header_bytes),
2659 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002660 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2661 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002662 dex_cache_arrays_bytes,
2663 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002664 interned_strings_bytes,
2665 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002666 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002667 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002668 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2669 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002670 CHECK_EQ(file_bytes,
2671 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2672 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2673 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002675
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002676 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002677 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002678 for (const auto& sizes_and_count : sizes_and_counts) {
2679 const std::string& descriptor(sizes_and_count.first);
2680 double average = static_cast<double>(sizes_and_count.second.bytes) /
2681 static_cast<double>(sizes_and_count.second.count);
2682 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002683 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002684 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002685 descriptor.c_str(), sizes_and_count.second.bytes,
2686 sizes_and_count.second.count, average, percent);
2687 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002688 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002689 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002690 CHECK_EQ(object_bytes, object_bytes_total);
2691
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002692 os << StringPrintf("oat_file_bytes = %8zd\n"
2693 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2695 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2696 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2697 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002698 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002699 managed_code_bytes,
2700 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002701 native_to_managed_code_bytes,
2702 PercentOfOatBytes(native_to_managed_code_bytes),
2703 class_initializer_code_bytes,
2704 PercentOfOatBytes(class_initializer_code_bytes),
2705 large_initializer_code_bytes,
2706 PercentOfOatBytes(large_initializer_code_bytes),
2707 large_method_code_bytes,
2708 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002709 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002710 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002711 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002712 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2713 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002714 }
2715
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002716 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002717 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002718 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002719
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002720 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2721 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002722 static_cast<double>(managed_code_bytes) /
2723 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002724 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002725 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002726 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002727
2728 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002729 }
2730 } stats_;
2731
2732 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002733 enum {
2734 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2735 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2736 kLargeConstructorDexBytes = 4000,
2737 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2738 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2739 kLargeMethodDexBytes = 16000
2740 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002741
2742 // For performance, use the *os_ directly for anything that doesn't need indentation
2743 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002745 VariableIndentationOutputStream vios_;
2746 ScopedIndentation indent1_;
2747
Ian Rogers1d54e732013-05-02 21:10:01 -07002748 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002749 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002750 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002751 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002752 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002753
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002754 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002755};
2756
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002757static int DumpImage(gc::space::ImageSpace* image_space,
2758 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002759 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002760 const ImageHeader& image_header = image_space->GetImageHeader();
2761 if (!image_header.IsValid()) {
2762 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2763 return EXIT_FAILURE;
2764 }
2765 ImageDumper image_dumper(os, *image_space, image_header, options);
2766 if (!image_dumper.Dump()) {
2767 return EXIT_FAILURE;
2768 }
2769 return EXIT_SUCCESS;
2770}
2771
2772static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002773 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002774 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002775 options->class_loader_ = &null_class_loader;
2776
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002777 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002778 if (options->app_image_ != nullptr) {
2779 if (options->app_oat_ == nullptr) {
2780 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002781 return EXIT_FAILURE;
2782 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002783 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2784 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2785 // pointers into 32 bit pointer sized ArtMethods.
2786 std::string error_msg;
2787 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2788 options->app_oat_,
2789 nullptr,
2790 nullptr,
2791 false,
2792 /*low_4gb*/true,
2793 nullptr,
2794 &error_msg));
2795 if (oat_file == nullptr) {
2796 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002797 return EXIT_FAILURE;
2798 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002799 std::unique_ptr<gc::space::ImageSpace> space(
2800 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2801 if (space == nullptr) {
2802 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2803 << error_msg;
2804 }
2805 // Open dex files for the image.
2806 std::vector<std::unique_ptr<const DexFile>> dex_files;
2807 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2808 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2809 << error_msg;
2810 }
2811 // Dump the actual image.
2812 int result = DumpImage(space.get(), options, os);
2813 if (result != EXIT_SUCCESS) {
2814 return result;
2815 }
2816 // Fall through to dump the boot images.
2817 }
2818
2819 gc::Heap* heap = runtime->GetHeap();
2820 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2821 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2822 int result = DumpImage(image_space, options, os);
2823 if (result != EXIT_SUCCESS) {
2824 return result;
2825 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002826 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002827 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002828}
2829
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002830static jobject InstallOatFile(Runtime* runtime,
2831 std::unique_ptr<OatFile> oat_file,
2832 std::vector<const DexFile*>* class_path)
2833 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002834 Thread* self = Thread::Current();
2835 CHECK(self != nullptr);
2836 // Need well-known-classes.
2837 WellKnownClasses::Init(self->GetJniEnv());
2838
2839 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002840 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002841 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002842 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2843 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002844 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002845 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002846 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002847 ObjPtr<mirror::DexCache> dex_cache =
2848 class_linker->RegisterDexFile(*dex_file, nullptr);
2849 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002850 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002851 }
2852
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002853 // Need a class loader. Fake that we're a compiler.
2854 // Note: this will run initializers through the unstarted runtime, so make sure it's
2855 // initialized.
2856 interpreter::UnstartedRuntime::Initialize();
2857
2858 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2859
2860 return class_loader;
2861}
2862
2863static int DumpOatWithRuntime(Runtime* runtime,
2864 std::unique_ptr<OatFile> oat_file,
2865 OatDumperOptions* options,
2866 std::ostream* os) {
2867 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2868 ScopedObjectAccess soa(Thread::Current());
2869
2870 OatFile* oat_file_ptr = oat_file.get();
2871 std::vector<const DexFile*> class_path;
2872 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002873
2874 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002875 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002876 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002877 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002878 options->class_loader_ = &loader_handle;
2879
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002880 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002881 bool success = oat_dumper.Dump(*os);
2882 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2883}
2884
2885static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002886 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002887 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002888 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002889 options->class_loader_ = &null_class_loader;
2890
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002891 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002892 bool success = oat_dumper.Dump(*os);
2893 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2894}
2895
2896static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2897 std::ostream* os) {
2898 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002899 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2900 oat_filename,
2901 nullptr,
2902 nullptr,
2903 false,
2904 /*low_4gb*/false,
2905 nullptr,
2906 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002907 if (oat_file == nullptr) {
2908 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2909 return EXIT_FAILURE;
2910 }
2911
2912 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002913 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002914 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002915 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002916 }
2917}
2918
David Srbecky2fdd03c2016-03-10 15:32:37 +00002919static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002920 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002921 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2922 oat_filename,
2923 nullptr,
2924 nullptr,
2925 false,
2926 /*low_4gb*/false,
2927 nullptr,
2928 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002929 if (oat_file == nullptr) {
2930 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2931 return EXIT_FAILURE;
2932 }
2933
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002934 bool result;
2935 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2936 // files for 64-bit code in the past.
2937 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002938 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002939 result = oat_symbolizer.Symbolize();
2940 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002941 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002942 result = oat_symbolizer.Symbolize();
2943 }
2944 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002945 fprintf(stderr, "Failed to symbolize\n");
2946 return EXIT_FAILURE;
2947 }
2948
2949 return EXIT_SUCCESS;
2950}
2951
Andreas Gampe9fded872016-09-25 16:08:35 -07002952class IMTDumper {
2953 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002954 static bool Dump(Runtime* runtime,
2955 const std::string& imt_file,
2956 bool dump_imt_stats,
2957 const char* oat_filename) {
2958 Thread* self = Thread::Current();
2959
2960 ScopedObjectAccess soa(self);
2961 StackHandleScope<1> scope(self);
2962 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2963 std::vector<const DexFile*> class_path;
2964
2965 if (oat_filename != nullptr) {
2966 std::string error_msg;
2967 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2968 oat_filename,
2969 nullptr,
2970 nullptr,
2971 false,
2972 /*low_4gb*/false,
2973 nullptr,
2974 &error_msg));
2975 if (oat_file == nullptr) {
2976 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2977 return false;
2978 }
2979
2980 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2981 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2982 } else {
2983 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2984 class_path = runtime->GetClassLinker()->GetBootClassPath();
2985 }
2986
2987 if (!imt_file.empty()) {
2988 return DumpImt(runtime, imt_file, class_loader);
2989 }
2990
2991 if (dump_imt_stats) {
2992 return DumpImtStats(runtime, class_path, class_loader);
2993 }
2994
2995 LOG(FATAL) << "Should not reach here";
2996 UNREACHABLE();
2997 }
2998
2999 private:
3000 static bool DumpImt(Runtime* runtime,
3001 const std::string& imt_file,
3002 Handle<mirror::ClassLoader> h_class_loader)
3003 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003004 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3005 std::unordered_set<std::string> prepared;
3006
3007 for (const std::string& line : lines) {
3008 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3009 // or a class descriptor and an interface method, in which case we will lookup the method,
3010 // determine its IMT slot, and check the class' IMT.
3011 size_t first_space = line.find(' ');
3012 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003013 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003014 } else {
3015 DumpIMTForMethod(runtime,
3016 line.substr(0, first_space),
3017 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003018 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003019 &prepared);
3020 }
3021 std::cerr << std::endl;
3022 }
3023
3024 return true;
3025 }
3026
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003027 static bool DumpImtStats(Runtime* runtime,
3028 const std::vector<const DexFile*>& dex_files,
3029 Handle<mirror::ClassLoader> h_class_loader)
3030 REQUIRES_SHARED(Locks::mutator_lock_) {
3031 size_t without_imt = 0;
3032 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003033 std::map<size_t, size_t> histogram;
3034
3035 ClassLinker* class_linker = runtime->GetClassLinker();
3036 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3037 std::unordered_set<std::string> prepared;
3038
3039 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003040 StackHandleScope<1> scope(self);
3041 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3042
3043 for (const DexFile* dex_file : dex_files) {
3044 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003045 class_def_index != dex_file->NumClassDefs();
3046 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003047 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3048 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003049 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003050 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003051 std::cerr << "Warning: could not load " << descriptor << std::endl;
3052 continue;
3053 }
3054
3055 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003056 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003057 continue;
3058 }
3059
3060 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3061 if (im_table == nullptr) {
3062 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003063 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003064 continue;
3065 }
3066
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003067 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003068 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3069 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3070 if (ptr->IsRuntimeMethod()) {
3071 if (ptr->IsImtUnimplementedMethod()) {
3072 histogram[0]++;
3073 } else {
3074 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3075 histogram[current_table->NumEntries(pointer_size)]++;
3076 }
3077 } else {
3078 histogram[1]++;
3079 }
3080 }
3081 }
3082 }
3083
3084 std::cerr << "IMT stats:"
3085 << std::endl << std::endl;
3086
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003087 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003088 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003089 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003090 << std::endl << std::endl;
3091
3092 double sum_one = 0;
3093 size_t count_one = 0;
3094
3095 std::cerr << " " << "IMT histogram" << std::endl;
3096 for (auto& bucket : histogram) {
3097 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3098 if (bucket.first > 0) {
3099 sum_one += bucket.second * bucket.first;
3100 count_one += bucket.second;
3101 }
3102 }
3103
3104 double count_zero = count_one + histogram[0];
3105 std::cerr << " Stats:" << std::endl;
3106 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3107 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3108
3109 return true;
3110 }
3111
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003112 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003113 static bool HasNoIMT(Runtime* runtime,
3114 Handle<mirror::Class> klass,
3115 const PointerSize pointer_size,
3116 std::unordered_set<std::string>* prepared)
3117 REQUIRES_SHARED(Locks::mutator_lock_) {
3118 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3119 return true;
3120 }
3121
3122 if (klass->GetImt(pointer_size) == nullptr) {
3123 PrepareClass(runtime, klass, prepared);
3124 }
3125
3126 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3127 DCHECK(object_class->IsObjectClass());
3128
3129 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3130
Mathieu Chartier6beced42016-11-15 15:51:31 -08003131 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003132 DCHECK(result);
3133 }
3134
3135 return result;
3136 }
3137
3138 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3139 REQUIRES_SHARED(Locks::mutator_lock_) {
3140 if (table == nullptr) {
3141 std::cerr << " <No IMT?>" << std::endl;
3142 return;
3143 }
3144 size_t table_index = 0;
3145 for (;;) {
3146 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3147 if (ptr == nullptr) {
3148 return;
3149 }
3150 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003151 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003152 }
3153 }
3154
3155 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3156 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003157 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003158 const std::string& class_name,
3159 const PointerSize pointer_size,
3160 mirror::Class** klass_out,
3161 std::unordered_set<std::string>* prepared)
3162 REQUIRES_SHARED(Locks::mutator_lock_) {
3163 if (class_name.empty()) {
3164 return nullptr;
3165 }
3166
3167 std::string descriptor;
3168 if (class_name[0] == 'L') {
3169 descriptor = class_name;
3170 } else {
3171 descriptor = DotToDescriptor(class_name.c_str());
3172 }
3173
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003174 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003175
3176 if (klass == nullptr) {
3177 self->ClearException();
3178 std::cerr << "Did not find " << class_name << std::endl;
3179 *klass_out = nullptr;
3180 return nullptr;
3181 }
3182
3183 StackHandleScope<1> scope(Thread::Current());
3184 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3185
3186 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3187 *klass_out = h_klass.Get();
3188 return ret;
3189 }
3190
3191 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3192 Handle<mirror::Class> h_klass,
3193 const PointerSize pointer_size,
3194 std::unordered_set<std::string>* prepared)
3195 REQUIRES_SHARED(Locks::mutator_lock_) {
3196 PrepareClass(runtime, h_klass, prepared);
3197 return h_klass->GetImt(pointer_size);
3198 }
3199
3200 static void DumpIMTForClass(Runtime* runtime,
3201 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003202 Handle<mirror::ClassLoader> h_loader,
3203 std::unordered_set<std::string>* prepared)
3204 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003205 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3206 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003207 ImTable* imt = PrepareAndGetImTable(runtime,
3208 Thread::Current(),
3209 h_loader,
3210 class_name,
3211 pointer_size,
3212 &klass,
3213 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003214 if (imt == nullptr) {
3215 return;
3216 }
3217
3218 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3219 for (size_t index = 0; index < ImTable::kSize; ++index) {
3220 std::cerr << " " << index << ":" << std::endl;
3221 ArtMethod* ptr = imt->Get(index, pointer_size);
3222 if (ptr->IsRuntimeMethod()) {
3223 if (ptr->IsImtUnimplementedMethod()) {
3224 std::cerr << " <empty>" << std::endl;
3225 } else {
3226 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3227 PrintTable(current_table, pointer_size);
3228 }
3229 } else {
David Sehr709b0702016-10-13 09:12:37 -07003230 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003231 }
3232 }
3233
3234 std::cerr << " Interfaces:" << std::endl;
3235 // Run through iftable, find methods that slot here, see if they fit.
3236 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003237 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3238 mirror::Class* iface = if_table->GetInterface(i);
3239 std::string iface_name;
3240 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003241
Mathieu Chartier6beced42016-11-15 15:51:31 -08003242 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3243 uint32_t class_hash, name_hash, signature_hash;
3244 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3245 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3246 std::cerr << " " << iface_method.PrettyMethod(true)
3247 << " slot=" << imt_slot
3248 << std::hex
3249 << " class_hash=0x" << class_hash
3250 << " name_hash=0x" << name_hash
3251 << " signature_hash=0x" << signature_hash
3252 << std::dec
3253 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003254 }
3255 }
3256 }
3257
3258 static void DumpIMTForMethod(Runtime* runtime,
3259 const std::string& class_name,
3260 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003261 Handle<mirror::ClassLoader> h_loader,
3262 std::unordered_set<std::string>* prepared)
3263 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003264 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3265 mirror::Class* klass;
3266 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003267 Thread::Current(),
3268 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003269 class_name,
3270 pointer_size,
3271 &klass,
3272 prepared);
3273 if (imt == nullptr) {
3274 return;
3275 }
3276
3277 std::cerr << class_name << " <" << method << ">" << std::endl;
3278 for (size_t index = 0; index < ImTable::kSize; ++index) {
3279 ArtMethod* ptr = imt->Get(index, pointer_size);
3280 if (ptr->IsRuntimeMethod()) {
3281 if (ptr->IsImtUnimplementedMethod()) {
3282 continue;
3283 }
3284
3285 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3286 if (current_table == nullptr) {
3287 continue;
3288 }
3289
3290 size_t table_index = 0;
3291 for (;;) {
3292 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3293 if (ptr2 == nullptr) {
3294 break;
3295 }
3296 table_index++;
3297
David Sehr709b0702016-10-13 09:12:37 -07003298 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003299 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003300 std::cerr << " Slot "
3301 << index
3302 << " ("
3303 << current_table->NumEntries(pointer_size)
3304 << ")"
3305 << std::endl;
3306 PrintTable(current_table, pointer_size);
3307 return;
3308 }
3309 }
3310 } else {
David Sehr709b0702016-10-13 09:12:37 -07003311 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003312 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003313 std::cerr << " Slot " << index << " (1)" << std::endl;
3314 std::cerr << " " << p_name << std::endl;
3315 } else {
3316 // Run through iftable, find methods that slot here, see if they fit.
3317 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003318 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3319 mirror::Class* iface = if_table->GetInterface(i);
3320 size_t num_methods = iface->NumDeclaredVirtualMethods();
3321 if (num_methods > 0) {
3322 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3323 if (ImTable::GetImtIndex(&iface_method) == index) {
3324 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003325 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003326 std::cerr << " Slot " << index << " (1)" << std::endl;
3327 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003328 }
3329 }
3330 }
3331 }
3332 }
3333 }
3334 }
3335 }
3336 }
3337
3338 // Read lines from the given stream, dropping comments and empty lines
3339 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3340 std::vector<std::string> output;
3341 while (in_stream.good()) {
3342 std::string dot;
3343 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003344 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003345 continue;
3346 }
3347 output.push_back(dot);
3348 }
3349 return output;
3350 }
3351
3352 // Read lines from the given file, dropping comments and empty lines.
3353 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3354 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3355 if (input_file.get() == nullptr) {
3356 LOG(ERROR) << "Failed to open input file " << input_filename;
3357 return std::vector<std::string>();
3358 }
3359 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3360 input_file->close();
3361 return result;
3362 }
3363
3364 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3365 // and note in the given set that the work was done.
3366 static void PrepareClass(Runtime* runtime,
3367 Handle<mirror::Class> h_klass,
3368 std::unordered_set<std::string>* done)
3369 REQUIRES_SHARED(Locks::mutator_lock_) {
3370 if (!h_klass->ShouldHaveImt()) {
3371 return;
3372 }
3373
3374 std::string name;
3375 name = h_klass->GetDescriptor(&name);
3376
3377 if (done->find(name) != done->end()) {
3378 return;
3379 }
3380 done->insert(name);
3381
3382 if (h_klass->HasSuperClass()) {
3383 StackHandleScope<1> h(Thread::Current());
3384 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3385 }
3386
3387 if (!h_klass->IsTemp()) {
3388 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3389 }
3390 }
3391};
3392
Igor Murashkin37743352014-11-13 14:38:00 -08003393struct OatdumpArgs : public CmdlineArgs {
3394 protected:
3395 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003396
Igor Murashkin37743352014-11-13 14:38:00 -08003397 virtual ParseStatus ParseCustom(const StringPiece& option,
3398 std::string* error_msg) OVERRIDE {
3399 {
3400 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3401 if (base_parse != kParseUnknownArgument) {
3402 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003403 }
3404 }
3405
Igor Murashkin37743352014-11-13 14:38:00 -08003406 if (option.starts_with("--oat-file=")) {
3407 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3408 } else if (option.starts_with("--image=")) {
3409 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003410 } else if (option == "--no-dump:vmap") {
3411 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003412 } else if (option =="--dump:code_info_stack_maps") {
3413 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003414 } else if (option == "--no-disassemble") {
3415 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003416 } else if (option =="--header-only") {
3417 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003418 } else if (option.starts_with("--symbolize=")) {
3419 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3420 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003421 } else if (option.starts_with("--only-keep-debug")) {
3422 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003423 } else if (option.starts_with("--class-filter=")) {
3424 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003425 } else if (option.starts_with("--method-filter=")) {
3426 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003427 } else if (option.starts_with("--list-classes")) {
3428 list_classes_ = true;
3429 } else if (option.starts_with("--list-methods")) {
3430 list_methods_ = true;
3431 } else if (option.starts_with("--export-dex-to=")) {
3432 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3433 } else if (option.starts_with("--addr2instr=")) {
3434 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3435 *error_msg = "Address conversion failed";
3436 return kParseError;
3437 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003438 } else if (option.starts_with("--app-image=")) {
3439 app_image_ = option.substr(strlen("--app-image=")).data();
3440 } else if (option.starts_with("--app-oat=")) {
3441 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003442 } else if (option.starts_with("--dump-imt=")) {
3443 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3444 } else if (option == "--dump-imt-stats") {
3445 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003446 } else {
3447 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003448 }
3449
Igor Murashkin37743352014-11-13 14:38:00 -08003450 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003451 }
3452
Igor Murashkin37743352014-11-13 14:38:00 -08003453 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3454 // Infer boot image location from the image location if possible.
3455 if (boot_image_location_ == nullptr) {
3456 boot_image_location_ = image_location_;
3457 }
3458
3459 // Perform the parent checks.
3460 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3461 if (parent_checks != kParseOk) {
3462 return parent_checks;
3463 }
3464
3465 // Perform our own checks.
3466 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3467 *error_msg = "Either --image or --oat-file must be specified";
3468 return kParseError;
3469 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3470 *error_msg = "Either --image or --oat-file must be specified but not both";
3471 return kParseError;
3472 }
3473
3474 return kParseOk;
3475 }
3476
3477 virtual std::string GetUsage() const {
3478 std::string usage;
3479
3480 usage +=
3481 "Usage: oatdump [options] ...\n"
3482 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3483 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3484 "\n"
3485 // Either oat-file or image is required.
3486 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3487 " Example: --oat-file=/system/framework/boot.oat\n"
3488 "\n"
3489 " --image=<file.art>: specifies an input image location.\n"
3490 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003491 "\n"
3492 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003493 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003494 " Example: --app-image=app.art\n"
3495 "\n"
3496 " --app-oat=<file.odex>: specifies an input app oat.\n"
3497 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003498 "\n";
3499
3500 usage += Base::GetUsage();
3501
3502 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003503 " --no-dump:vmap may be used to disable vmap dumping.\n"
3504 " Example: --no-dump:vmap\n"
3505 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003506 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3507 " Example: --dump:code_info_stack_maps\n"
3508 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003509 " --no-disassemble may be used to disable disassembly.\n"
3510 " Example: --no-disassemble\n"
3511 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003512 " --header-only may be used to print only the oat header.\n"
3513 " Example: --header-only\n"
3514 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003515 " --list-classes may be used to list target file classes (can be used with filters).\n"
3516 " Example: --list-classes\n"
3517 " Example: --list-classes --class-filter=com.example.foo\n"
3518 "\n"
3519 " --list-methods may be used to list target file methods (can be used with filters).\n"
3520 " Example: --list-methods\n"
3521 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3522 "\n"
3523 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3524 " Example: --symbolize=/system/framework/boot.oat\n"
3525 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003526 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3527 " .rodata and .text sections are omitted in the output file to save space.\n"
3528 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3529 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003530 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3531 " Example: --class-filter=com.example.foo\n"
3532 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003533 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3534 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003535 "\n"
3536 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3537 " Example: --export-dex-to=/data/local/tmp\n"
3538 "\n"
3539 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3540 " address (e.g. PC from crash dump)\n"
3541 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003542 "\n"
3543 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3544 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003545 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003546 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003547 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003548 " Example: --dump-imt=imt.txt\n"
3549 "\n"
3550 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3551 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003552 "\n";
3553
3554 return usage;
3555 }
3556
3557 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003558 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003559 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003560 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003561 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003562 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003563 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003564 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003565 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003566 bool disassemble_code_ = true;
3567 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003568 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003569 bool list_classes_ = false;
3570 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003571 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003572 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003573 uint32_t addr2instr_ = 0;
3574 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003575 const char* app_image_ = nullptr;
3576 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003577};
3578
Igor Murashkin37743352014-11-13 14:38:00 -08003579struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3580 virtual bool NeedsRuntime() OVERRIDE {
3581 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003582
Igor Murashkin37743352014-11-13 14:38:00 -08003583 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3584 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3585
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003586 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003587 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003588 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003589 args_->disassemble_code_,
3590 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003591 args_->class_filter_,
3592 args_->method_filter_,
3593 args_->list_classes_,
3594 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003595 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003596 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003597 args_->app_image_,
3598 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003599 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003600
Andreas Gampe9fded872016-09-25 16:08:35 -07003601 return (args_->boot_image_location_ != nullptr ||
3602 args_->image_location_ != nullptr ||
3603 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003604 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003605 }
3606
Igor Murashkin37743352014-11-13 14:38:00 -08003607 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3608 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003609 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003610
Mathieu Chartierd424d082014-10-15 10:31:46 -07003611 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003612
Andreas Gampec24f3992014-12-17 20:40:11 -08003613 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003614 // ELF has special kind of section called SHT_NOBITS which allows us to create
3615 // sections which exist but their data is omitted from the ELF file to save space.
3616 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3617 // with only debug data. We use it in similar way to exclude .rodata and .text.
3618 bool no_bits = args_->only_keep_debug_;
3619 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003620 } else {
3621 return DumpOat(nullptr,
3622 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003623 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003624 args_->os_) == EXIT_SUCCESS;
3625 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003626 }
3627
Igor Murashkin37743352014-11-13 14:38:00 -08003628 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3629 CHECK(args_ != nullptr);
3630
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003631 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3632 return IMTDumper::Dump(runtime,
3633 args_->imt_dump_,
3634 args_->imt_stat_dump_,
3635 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003636 }
3637
Igor Murashkin37743352014-11-13 14:38:00 -08003638 if (args_->oat_filename_ != nullptr) {
3639 return DumpOat(runtime,
3640 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003641 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003642 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003643 }
Igor Murashkin37743352014-11-13 14:38:00 -08003644
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003645 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003646 }
3647
Igor Murashkin37743352014-11-13 14:38:00 -08003648 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3649};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003650
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003651} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003652
3653int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003654 art::OatdumpMain main;
3655 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003656}