blob: be78136f4e1aa5005f4dbec5be06858be3eebd24 [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 }
908 const size_t code_item_size = code_item->insns_size_in_code_units_;
909 const uint16_t* code_ptr = code_item->insns_;
910 const uint16_t* code_end = code_item->insns_ + code_item_size;
911
912 // If we inserted a new dex code item pointer, add to total code bytes.
913 if (dex_code_item_ptrs_.insert(code_ptr).second) {
914 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
915 }
916
917 while (code_ptr < code_end) {
918 const Instruction* inst = Instruction::At(code_ptr);
919 switch (inst->Opcode()) {
920 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800921 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700922 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
923 ++num_string_ids_from_code_;
924 break;
925 }
926 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800927 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700928 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
929 ++num_string_ids_from_code_;
930 break;
931 }
932 default:
933 break;
934 }
935
936 code_ptr += inst->SizeInCodeUnits();
937 }
938 }
939
940 // Unique string ids loaded from dex code.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700941 std::set<StringReference> unique_string_ids_from_code_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700942
943 // Total string ids loaded from dex code.
944 size_t num_string_ids_from_code_ = 0;
945
946 // Unique code pointers.
947 std::set<const void*> dex_code_item_ptrs_;
948
949 // Total "unique" dex code bytes.
950 size_t dex_code_bytes_ = 0;
951
952 // Other dex ids.
953 size_t num_string_ids_ = 0;
954 size_t num_method_ids_ = 0;
955 size_t num_field_ids_ = 0;
956 size_t num_type_ids_ = 0;
957 size_t num_class_defs_ = 0;
958 };
959
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
961 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800962 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700963 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800964 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800965 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700966
Andreas Gampe2ba88952016-04-29 17:52:07 -0700967 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100968 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
969
970 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700971 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100972 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700973 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
974 dex_offset,
975 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700976
Andreas Gampe2ba88952016-04-29 17:52:07 -0700977 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700978 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700979 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
980 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700981 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982 os << std::flush;
983 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700984 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985
Andreas Gampe2ba88952016-04-29 17:52:07 -0700986 // Print lookup table, if it exists.
987 if (oat_dex_file.GetLookupTableData() != nullptr) {
988 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
989 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700990 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700991 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
992 table_offset,
993 table_offset + table_size - 1);
994 }
995
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100996 VariableIndentationOutputStream vios(&os);
997 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800998 for (size_t class_def_index = 0;
999 class_def_index < dex_file->NumClassDefs();
1000 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001001 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
1002 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001003
1004 // TODO: Support regex
1005 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
1006 continue;
1007 }
1008
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001009 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001010 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -08001012 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001013 << " (" << oat_class.GetStatus() << ")"
1014 << " (" << oat_class.GetType() << ")\n";
1015 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001016 if (options_.list_classes_) {
1017 continue;
1018 }
1019 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001020 success = false;
1021 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001022 if (stop_analysis) {
1023 os << std::flush;
1024 return success;
1025 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001026 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001027 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001028 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001029 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001030 }
1031
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001032 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1033 std::string error_msg;
1034 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
1035
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001036 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001037 if (dex_file == nullptr) {
1038 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1039 return false;
1040 }
1041 size_t fsize = oat_dex_file.FileSize();
1042
1043 // Some quick checks just in case
1044 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1045 os << "Invalid dex file\n";
1046 return false;
1047 }
1048
1049 // Verify output directory exists
1050 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1051 // TODO: Extend OS::DirectoryExists if symlink support is required
1052 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1053 return false;
1054 }
1055
1056 // Beautify path names
1057 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1058 return false;
1059 }
1060
1061 std::string dex_orig_name;
1062 size_t dex_orig_pos = dex_file_location.rfind('/');
1063 if (dex_orig_pos == std::string::npos)
1064 dex_orig_name = dex_file_location;
1065 else
1066 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1067
1068 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001069 if (dex_orig_name.size() == 8 &&
1070 dex_orig_name.compare("base.apk") == 0 &&
1071 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001072 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1073 size_t apk_orig_pos = dex_file_location.rfind('/');
1074 if (apk_orig_pos != std::string::npos) {
1075 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1076 }
1077 }
1078
1079 std::string out_dex_path(options_.export_dex_location_);
1080 if (out_dex_path.back() != '/') {
1081 out_dex_path.append("/");
1082 }
1083 out_dex_path.append(dex_orig_name);
1084 out_dex_path.append("_export.dex");
1085 if (out_dex_path.length() > PATH_MAX) {
1086 return false;
1087 }
1088
1089 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1090 if (file.get() == nullptr) {
1091 os << "Failed to open output dex file " << out_dex_path;
1092 return false;
1093 }
1094
1095 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1096 os << "Failed to write dex file";
1097 file->Erase();
1098 return false;
1099 }
1100
1101 if (file->FlushCloseOrErase() != 0) {
1102 os << "Flush and close failed";
1103 return false;
1104 }
1105
1106 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1107 os << std::flush;
1108
1109 return true;
1110 }
1111
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 bool DumpOatClass(VariableIndentationOutputStream* vios,
1113 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001114 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001115 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001116 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001117 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001118 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001119 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001121 }
1122 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001123 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001125 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001126 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001127 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001128 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001129 success = false;
1130 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001131 if (addr_found) {
1132 *stop_analysis = true;
1133 return success;
1134 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001135 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001136 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001137 }
Ian Rogers0571d352011-11-03 19:51:38 -07001138 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001139 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001140 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001141 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001142 success = false;
1143 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001144 if (addr_found) {
1145 *stop_analysis = true;
1146 return success;
1147 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001148 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001149 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001150 }
Ian Rogers0571d352011-11-03 19:51:38 -07001151 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001152 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001153 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001154 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001155
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001156 static constexpr uint32_t kPrologueBytes = 16;
1157
1158 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1159 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1160
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001161 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1162 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001163 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001164 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001165 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001166 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001167 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001168
1169 // TODO: Support regex
1170 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1171 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001172 return success;
1173 }
1174
David Sehr709b0702016-10-13 09:12:37 -07001175 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001176 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1177 class_method_index, pretty_method.c_str(),
1178 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001179 if (options_.list_methods_) return success;
1180
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001181 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1182 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1183 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1184 uint32_t code_offset = oat_method.GetCodeOffset();
1185 uint32_t code_size = oat_method.GetQuickCodeSize();
1186 if (resolved_addr2instr_ != 0) {
1187 if (resolved_addr2instr_ > code_offset + code_size) {
1188 return success;
1189 } else {
1190 *addr_found = true; // stop analyzing file at next iteration
1191 }
1192 }
1193
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001194 // Everything below is indented at least once.
1195 ScopedIndentation indent1(vios);
1196
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001197 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001198 vios->Stream() << "DEX CODE:\n";
1199 ScopedIndentation indent2(vios);
1200 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001201 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001202
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001203 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001204 std::unique_ptr<verifier::MethodVerifier> verifier;
1205 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001206 // We need to have the handle scope stay live until after the verifier since the verifier has
1207 // a handle to the dex cache from hs.
1208 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001209 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1210 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001211 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001212 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001213 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001214 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001215 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001217 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001219 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001221 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001222 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001223 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1224 oat_method_offsets_offset, oat_file_.Size());
1225 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001226 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001228 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001229
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001230 ScopedIndentation indent2(vios);
1231 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1233 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001234 vios->Stream() << StringPrintf("WARNING: "
1235 "code offset 0x%08x is past end of file 0x%08zx.\n",
1236 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001237 success = false;
1238 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001239 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001240 }
1241 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001243 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1244 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001245 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1246 sizeof(*method_header) * kBitsPerByte,
1247 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001248 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001249 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001250 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001251 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001252 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001253 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001254 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1255 method_header_offset, oat_file_.Size());
1256 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001257 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001258 return false;
1259 }
1260
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001263 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001264 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001265 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001266 uint32_t vmap_table_offset = method_header ==
1267 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001268 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001269
1270 size_t vmap_table_offset_limit =
1271 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1272 ? oat_file_.GetVdexFile()->Size()
1273 : method_header->GetCode() - oat_file_.Begin();
1274 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 vios->Stream() << StringPrintf("WARNING: "
1276 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1277 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001278 vmap_table_offset,
1279 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001280 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001282 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001283 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001285 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001286 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001287 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001288
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001289 ScopedIndentation indent2(vios);
1290 vios->Stream()
1291 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1292 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1293 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1294 vios->Stream() << "\n";
1295 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1296 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1297 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001298 }
1299 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001300 // Based on spill masks from QuickMethodFrameInfo so placed
1301 // after it is dumped, but useful for understanding quick
1302 // code, so dumped here.
1303 ScopedIndentation indent2(vios);
1304 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001305 }
1306 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001307 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001308 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1309 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001310 ScopedIndentation indent2(vios);
1311 vios->Stream() << StringPrintf("WARNING: "
1312 "code size offset 0x%08x is past end of file 0x%08zx.",
1313 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001314 success = false;
1315 } else {
1316 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001317 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1318 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001319 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001320
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001321 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001322 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001323 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001324 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1325 code_offset,
1326 code_size_offset,
1327 code_size,
1328 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001329
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001330 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001331 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001332 vios->Stream() << StringPrintf("WARNING: "
1333 "start of code at 0x%08x is past end of file 0x%08zx.",
1334 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001335 success = false;
1336 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001337 vios->Stream() << StringPrintf(
1338 "WARNING: "
1339 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1340 "code size is 0x%08x loaded from offset 0x%08x.\n",
1341 aligned_code_end, oat_file_.Size(),
1342 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001343 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001344 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001345 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001346 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001347 }
1348 }
1349 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001350 vios->Stream() << StringPrintf(
1351 "WARNING: "
1352 "code size %d is bigger than max expected threshold of %d. "
1353 "code size is 0x%08x loaded from offset 0x%08x.\n",
1354 code_size, kMaxCodeSize,
1355 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001356 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001357 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001359 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001360 }
1361 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001362 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001363 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001364 }
1365 }
1366 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001367 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001368 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001369 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001370
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001371 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1372 if (spill_mask == 0) {
1373 return;
1374 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001375 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001376 for (size_t i = 0; i < 32; i++) {
1377 if ((spill_mask & (1 << i)) != 0) {
1378 if (is_float) {
1379 os << "fr" << i;
1380 } else {
1381 os << "r" << i;
1382 }
1383 spill_mask ^= 1 << i; // clear bit
1384 if (spill_mask != 0) {
1385 os << ", ";
1386 } else {
1387 break;
1388 }
1389 }
1390 }
1391 os << ")";
1392 }
1393
Roland Levillain442b46a2015-02-18 16:54:21 +00001394 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001395 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001396 const OatFile::OatMethod& oat_method,
1397 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001398 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1399 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001400 const void* raw_code_info = oat_method.GetVmapTable();
1401 if (raw_code_info != nullptr) {
1402 CodeInfo code_info(raw_code_info);
1403 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001404 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001405 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1406 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001407 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001408 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1409 // We don't encode the size in the table, so just emit that we have quickened
1410 // information.
1411 ScopedIndentation indent(vios);
1412 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001413 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001414 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001415 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001416 }
1417
1418 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001419 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001420 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001421 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001422 const DexFile::CodeItem& code_item,
1423 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001424 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001425 oat_method.GetCodeOffset(),
1426 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001427 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001428 instruction_set_,
1429 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001430 }
1431
Andreas Gampe36a296f2017-06-13 14:11:11 -07001432 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1433 // According to stack model, the first out is above the Method referernce.
1434 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1435 }
1436
1437 static uint32_t GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
1438 uint32_t core_spills,
1439 uint32_t fp_spills,
1440 size_t frame_size,
1441 int reg,
1442 InstructionSet isa) {
1443 PointerSize pointer_size = InstructionSetPointerSize(isa);
1444 if (kIsDebugBuild) {
1445 auto* runtime = Runtime::Current();
1446 if (runtime != nullptr) {
1447 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1448 }
1449 }
1450 DCHECK_ALIGNED(frame_size, kStackAlignment);
1451 DCHECK_NE(reg, -1);
1452 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1453 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1454 + sizeof(uint32_t); // Filler.
1455 int num_regs = code_item->registers_size_ - code_item->ins_size_;
1456 int temp_threshold = code_item->registers_size_;
1457 const int max_num_special_temps = 1;
1458 if (reg == temp_threshold) {
1459 // The current method pointer corresponds to special location on stack.
1460 return 0;
1461 } else if (reg >= temp_threshold + max_num_special_temps) {
1462 /*
1463 * Special temporaries may have custom locations and the logic above deals with that.
1464 * However, non-special temporaries are placed relative to the outs.
1465 */
1466 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
1467 + static_cast<size_t>(pointer_size) /* art method */;
1468 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1469 return temps_start + relative_offset;
1470 } else if (reg < num_regs) {
1471 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1472 return locals_start + (reg * sizeof(uint32_t));
1473 } else {
1474 // Handle ins.
1475 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1476 + static_cast<size_t>(pointer_size) /* art method */;
1477 }
1478 }
1479
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001480 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1481 const DexFile::CodeItem* code_item) {
1482 if (code_item != nullptr) {
1483 size_t num_locals_ins = code_item->registers_size_;
1484 size_t num_ins = code_item->ins_size_;
1485 size_t num_locals = num_locals_ins - num_ins;
1486 size_t num_outs = code_item->outs_size_;
1487
1488 os << "vr_stack_locations:";
1489 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1490 // For readability, delimit the different kinds of VRs.
1491 if (reg == num_locals_ins) {
1492 os << "\n\tmethod*:";
1493 } else if (reg == num_locals && num_ins > 0) {
1494 os << "\n\tins:";
1495 } else if (reg == 0 && num_locals > 0) {
1496 os << "\n\tlocals:";
1497 }
1498
Andreas Gampe36a296f2017-06-13 14:11:11 -07001499 uint32_t offset = GetVRegOffsetFromQuickCode(code_item,
1500 oat_method.GetCoreSpillMask(),
1501 oat_method.GetFpSpillMask(),
1502 oat_method.GetFrameSizeInBytes(),
1503 reg,
1504 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001505 os << " v" << reg << "[sp + #" << offset << "]";
1506 }
1507
1508 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1509 if (out_reg == 0) {
1510 os << "\n\touts:";
1511 }
1512
Andreas Gampe36a296f2017-06-13 14:11:11 -07001513 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001514 os << " v" << out_reg << "[sp + #" << offset << "]";
1515 }
1516
1517 os << "\n";
1518 }
1519 }
1520
Ian Rogersb23a7722012-10-09 16:54:26 -07001521 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001522 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001523 size_t i = 0;
1524 while (i < code_item->insns_size_in_code_units_) {
1525 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001526 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1527 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001528 i += instruction->SizeInCodeUnits();
1529 }
1530 }
1531 }
1532
Roland Levillainf2650d12015-05-28 14:53:28 +01001533 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1534 // the optimizing compiler?
1535 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1536 const DexFile::CodeItem* code_item) {
1537 // If the native GC map is null and the Dex `code_item` is not
1538 // null, then this method has been compiled with the optimizing
1539 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001540 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001541 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001542 code_item != nullptr;
1543 }
1544
1545 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1546 // the dextodex compiler?
1547 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1548 const DexFile::CodeItem* code_item) {
1549 // If the quick code is null, the Dex `code_item` is not
1550 // null, and the vmap table is not null, then this method has been compiled
1551 // with the dextodex compiler.
1552 return oat_method.GetQuickCode() == nullptr &&
1553 oat_method.GetVmapTable() != nullptr &&
1554 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001555 }
1556
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001557 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001558 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001559 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001560 const DexFile* dex_file,
1561 const DexFile::ClassDef& class_def,
1562 const DexFile::CodeItem* code_item,
1563 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 if ((method_access_flags & kAccNative) == 0) {
1565 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001566 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001567 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001568 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001569 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001570 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001571 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001572 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001573 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001575
1576 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 }
1578
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001579 // The StackMapsHelper provides the stack maps in the native PC order.
1580 // For identical native PCs, the order from the CodeInfo is preserved.
1581 class StackMapsHelper {
1582 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001583 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001584 : code_info_(raw_code_info),
1585 encoding_(code_info_.ExtractEncoding()),
1586 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1587 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001588 offset_(static_cast<uint32_t>(-1)),
1589 stack_map_index_(0u),
1590 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001591 if (number_of_stack_maps_ != 0u) {
1592 // Check if native PCs are ordered.
1593 bool ordered = true;
1594 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1595 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1596 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001597 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1598 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001599 ordered = false;
1600 break;
1601 }
1602 last = current;
1603 }
1604 if (!ordered) {
1605 // Create indirection indexes for access in native PC order. We do not optimize
1606 // for the fact that there can currently be only two separately ordered ranges,
1607 // namely normal stack maps and catch-point stack maps.
1608 indexes_.resize(number_of_stack_maps_);
1609 std::iota(indexes_.begin(), indexes_.end(), 0u);
1610 std::sort(indexes_.begin(),
1611 indexes_.end(),
1612 [this](size_t lhs, size_t rhs) {
1613 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001614 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001615 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001616 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001617 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001618 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001619 // If the PCs are the same, compare indexes to preserve the original order.
1620 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1621 });
1622 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001623 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001624 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001625 }
1626 }
1627
1628 const CodeInfo& GetCodeInfo() const {
1629 return code_info_;
1630 }
1631
1632 const CodeInfoEncoding& GetEncoding() const {
1633 return encoding_;
1634 }
1635
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001636 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001637 return offset_;
1638 }
1639
1640 StackMap GetStackMap() const {
1641 return GetStackMapAt(stack_map_index_);
1642 }
1643
1644 void Next() {
1645 ++stack_map_index_;
1646 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001647 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001648 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001649 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001650 }
1651
1652 private:
1653 StackMap GetStackMapAt(size_t i) const {
1654 if (!indexes_.empty()) {
1655 i = indexes_[i];
1656 }
1657 DCHECK_LT(i, number_of_stack_maps_);
1658 return code_info_.GetStackMapAt(i, encoding_);
1659 }
1660
1661 const CodeInfo code_info_;
1662 const CodeInfoEncoding encoding_;
1663 const size_t number_of_stack_maps_;
1664 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001665 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001666 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001667 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001668 };
1669
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001670 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001671 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1672 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001673 const void* quick_code = oat_method.GetQuickCode();
1674
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001675 if (code_size == 0) {
1676 code_size = oat_method.GetQuickCodeSize();
1677 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001678 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001679 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001680 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001681 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1682 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001683 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001684 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001685 {
1686 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001687 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001688 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001689 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001690 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001691 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001692 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001693 stats_.AddBits(
1694 Stats::kByteKindStackMapNativePc,
1695 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1696 stats_.AddBits(
1697 Stats::kByteKindStackMapDexPc,
1698 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1699 stats_.AddBits(
1700 Stats::kByteKindStackMapDexRegisterMap,
1701 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1702 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001703 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001704 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1705 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001706 Stats::kByteKindStackMapRegisterMaskIndex,
1707 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001708 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001709 Stats::kByteKindStackMapStackMaskIndex,
1710 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001711
1712 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001713 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001714 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001715 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001716
1717 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001718 stats_.AddBits(
1719 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001720 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001721
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001722 // Invoke infos
1723 if (encoding.invoke_info.num_entries > 0u) {
1724 stats_.AddBits(
1725 Stats::kByteKindCodeInfoInvokeInfo,
1726 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1727 }
1728
Mathieu Chartier1e083792017-02-08 13:30:04 -08001729 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001730 const size_t location_catalog_bytes =
1731 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1732 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1733 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001734 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001735 const size_t dex_register_bytes =
1736 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1737 stats_.AddBits(
1738 Stats::kByteKindCodeInfoDexRegisterMap,
1739 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001740
1741 // Inline infos.
1742 const size_t num_inline_infos = encoding.inline_info.num_entries;
1743 if (num_inline_infos > 0u) {
1744 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001745 Stats::kByteKindInlineInfoMethodIndexIdx,
1746 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1747 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001748 stats_.AddBits(
1749 Stats::kByteKindInlineInfoDexPc,
1750 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1751 stats_.AddBits(
1752 Stats::kByteKindInlineInfoExtraData,
1753 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1754 stats_.AddBits(
1755 Stats::kByteKindInlineInfoDexRegisterMap,
1756 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1757 num_inline_infos);
1758 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1759 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001760 }
1761 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001762 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1763 size_t offset = 0;
1764 while (offset < code_size) {
1765 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1766 if (offset == helper.GetOffset()) {
1767 ScopedIndentation indent1(vios);
1768 StackMap stack_map = helper.GetStackMap();
1769 DCHECK(stack_map.IsValid());
1770 stack_map.Dump(vios,
1771 helper.GetCodeInfo(),
1772 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001773 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001774 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001775 code_item->registers_size_,
1776 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001777 do {
1778 helper.Next();
1779 // There may be multiple stack maps at a given PC. We display only the first one.
1780 } while (offset == helper.GetOffset());
1781 }
1782 DCHECK_LT(offset, helper.GetOffset());
1783 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001784 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001785 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1786 size_t offset = 0;
1787 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001788 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001789 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001790 }
1791 }
1792
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001793 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001794 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001795 const OatDumperOptions& options_;
1796 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001797 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001798 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001799 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001800 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001801};
1802
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001803class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001804 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001805 ImageDumper(std::ostream* os,
1806 gc::space::ImageSpace& image_space,
1807 const ImageHeader& image_header,
1808 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001809 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001810 vios_(os),
1811 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001812 image_space_(image_space),
1813 image_header_(image_header),
1814 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001815
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001816 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001817 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001818 std::ostream& indent_os = vios_.Stream();
1819
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001820 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001821
Jeff Haodcdc85b2015-12-04 14:06:18 -08001822 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1823
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001824 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001825
Mathieu Chartiere401d142015-04-22 13:56:20 -07001826 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1827
1828 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1829 auto section = static_cast<ImageHeader::ImageSections>(i);
1830 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1831 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001832
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001833 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001834
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001835 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001836
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001837 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1838
1839 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1840
1841 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001842
Alex Lighta59dd802014-07-02 16:28:08 -07001843 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1844
Igor Murashkin46774762014-10-22 11:37:02 -07001845 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1846
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 {
1848 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001849 static_assert(arraysize(image_roots_descriptions_) ==
1850 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001851 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1852 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1854 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001855 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001856 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001857 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001858 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001859 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001860 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001861 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1862 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001863 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001864 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1865 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001866 run++;
1867 } else {
1868 break;
1869 }
1870 }
1871 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001872 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001873 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001874 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001875 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001876 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001877 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001878 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001879 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001880 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001882 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001883 }
1884 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001885 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001887 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001888 os << "METHOD ROOTS\n";
1889 static_assert(arraysize(image_methods_descriptions_) ==
1890 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1891 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1892 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1893 const char* description = image_methods_descriptions_[i];
1894 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001895 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001896 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001897 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001899
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001900 Runtime* const runtime = Runtime::Current();
1901 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001902 std::string image_filename = image_space_.GetImageFilename();
1903 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001904 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001905 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001906 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001907 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001908 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001909 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1910 }
1911 if (oat_file == nullptr) {
1912 oat_file = OatFile::Open(oat_location,
1913 oat_location,
1914 nullptr,
1915 nullptr,
1916 false,
1917 /*low_4gb*/false,
1918 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001919 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001920 }
1921 if (oat_file == nullptr) {
1922 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1923 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001924 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001925 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001926
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001927 stats_.oat_file_bytes = oat_file->Size();
1928
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001929 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001930
Mathieu Chartier02e25112013-08-14 16:14:24 -07001931 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001932 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001933 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1934 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001935 }
1936
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001937 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001938
Jeff Haodcdc85b2015-12-04 14:06:18 -08001939 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001940 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001941 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001942 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001943 {
1944 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1945 heap->FlushAllocStack();
1946 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001947 // Since FlushAllocStack() above resets the (active) allocation
1948 // stack. Need to revoke the thread-local allocation stacks that
1949 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001950 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001951 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001952 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001953 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001954 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001955 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001956 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001957 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001958 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001959 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001960 ObjPtr<mirror::DexCache> dex_cache =
1961 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001962 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001963 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001964 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001965 }
1966 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001967 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1968 DumpObject(obj);
1969 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001970 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001971 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001972 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001973 indent_os << "\n";
1974 // TODO: Dump fields.
1975 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001976 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001977 image_header_.VisitPackedArtMethods(&visitor,
1978 image_space_.Begin(),
1979 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001980 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001981 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001982 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001983 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001984 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001985 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001986 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1987 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001988 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001989 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001990 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001991 // If the image is compressed, adjust to decompressed size.
1992 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1993 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1994 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1995 }
1996 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001997 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001998 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001999 const auto& object_section = image_header_.GetObjectsSection();
2000 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002001 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002002 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2003 const auto& intern_section = image_header_.GetInternedStringsSection();
2004 const auto& class_table_section = image_header_.GetClassTableSection();
2005 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002006
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002007 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002008
2009 // Objects are kObjectAlignment-aligned.
2010 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2011 if (object_section.Offset() > header_bytes) {
2012 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2013 }
2014
2015 // Field section is 4-byte aligned.
2016 constexpr size_t kFieldSectionAlignment = 4U;
2017 uint32_t end_objects = object_section.Offset() + object_section.Size();
2018 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2019 stats_.alignment_bytes += field_section.Offset() - end_objects;
2020
2021 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2022 uint32_t end_fields = field_section.Offset() + field_section.Size();
2023 CHECK_ALIGNED(method_section.Offset(), 4);
2024 stats_.alignment_bytes += method_section.Offset() - end_fields;
2025
2026 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2027 uint32_t end_methods = method_section.Offset() + method_section.Size();
2028 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2029 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2030
2031 // Intern table is 8-byte aligned.
2032 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002033 CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t));
Andreas Gampeace0dc12016-01-20 13:33:13 -08002034 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2035
2036 // Add space between intern table and class table.
2037 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2038 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2039
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002040 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2041 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002042 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002043 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002044
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045 stats_.bitmap_bytes += bitmap_section.Size();
2046 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002047 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002048 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002049 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002050 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002051 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002052 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002053
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002054 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002055
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002056 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002057 }
2058
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002059 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002060 class DumpArtMethodVisitor : public ArtMethodVisitor {
2061 public:
2062 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2063
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002064 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002065 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002066 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002067 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002068 indent_os << "\n";
2069 }
2070
2071 private:
2072 ImageDumper* const image_dumper_;
2073 };
2074
Mathieu Chartier3398c782016-09-30 10:27:43 -07002075 static void PrettyObjectValue(std::ostream& os,
2076 ObjPtr<mirror::Class> type,
2077 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002078 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002079 CHECK(type != nullptr);
2080 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002081 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002082 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002083 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002085 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002086 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002087 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002088 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002089 } else {
David Sehr709b0702016-10-13 09:12:37 -07002090 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002091 }
2092 }
2093
Mathieu Chartier3398c782016-09-30 10:27:43 -07002094 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002095 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002096 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002097 switch (field->GetTypeAsPrimitiveType()) {
2098 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002099 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002100 break;
2101 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002102 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002103 break;
2104 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002105 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002106 break;
2107 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002108 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002109 break;
2110 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002111 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002112 break;
2113 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002114 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002115 break;
2116 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002117 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002118 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002119 break;
2120 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002121 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002122 break;
2123 case Primitive::kPrimNot: {
2124 // Get the value, don't compute the type unless it is non-null as we don't want
2125 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002126 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002127 if (value == nullptr) {
2128 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002129 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002130 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002131 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002132 if (field_type != nullptr) {
2133 PrettyObjectValue(os, field_type, value);
2134 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002135 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002136 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002137 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2138 }
Ian Rogers50239c72013-06-17 14:53:22 -07002139 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002140 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002141 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002142 default:
2143 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2144 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002145 }
2146 }
2147
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002148 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002149 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002150 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002151 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002153 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002154 for (ArtField& field : klass->GetIFields()) {
2155 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002156 }
2157 }
2158
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002159 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002160 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002161 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002162
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002163 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002164 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002165 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002166 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002167 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002168 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002169 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002170 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002171 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002172 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002173 }
2174
Mathieu Chartiere401d142015-04-22 13:56:20 -07002175 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002176 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002177 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2178 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002179 return 0;
2180 }
2181 return oat_code_begin[-1];
2182 }
2183
Mathieu Chartiere401d142015-04-22 13:56:20 -07002184 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002185 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002186 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002187 if (oat_code_begin == nullptr) {
2188 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002189 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002190 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002191 }
2192
Andreas Gampe0c183382017-07-13 22:26:24 -07002193 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002194 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002195 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002196 return;
2197 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002198
2199 size_t object_bytes = obj->SizeOf();
2200 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002201 stats_.object_bytes += object_bytes;
2202 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002203
Andreas Gampe0c183382017-07-13 22:26:24 -07002204 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002206 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002207 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002208 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002210 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002211 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002212 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2213 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002214 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002215 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002216 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002217 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002218 } else {
David Sehr709b0702016-10-13 09:12:37 -07002219 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002220 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002221 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002222 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002223 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002224 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002225 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2226 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002227 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002228 size_t run = 0;
2229 for (int32_t j = i + 1; j < length; j++) {
2230 if (value == obj_array->Get(j)) {
2231 run++;
2232 } else {
2233 break;
2234 }
2235 }
2236 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002237 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002238 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002239 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002240 i = i + run;
2241 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002242 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002243 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002244 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002245 }
2246 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002247 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002248 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002249 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002250 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002251 for (ArtField& field : klass->GetSFields()) {
2252 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002253 }
2254 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002255 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002256 auto it = dex_caches_.find(obj);
2257 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002258 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002259 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002260 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002261 size_t num_methods = dex_cache->NumResolvedMethods();
2262 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002263 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002264 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002265 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002266 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002267 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2268 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002269 size_t run = 0;
2270 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002271 j != length &&
2272 elem == mirror::DexCache::GetNativePairPtrSize(
2273 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002274 ++j) {
2275 ++run;
2276 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002277 if (run == 0) {
2278 os << StringPrintf("%zd: ", i);
2279 } else {
2280 os << StringPrintf("%zd to %zd: ", i, i + run);
2281 i = i + run;
2282 }
2283 std::string msg;
2284 if (elem == nullptr) {
2285 msg = "null";
2286 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002287 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002288 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002289 } else {
2290 msg = "<not in method section>";
2291 }
2292 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002293 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002294 }
2295 size_t num_fields = dex_cache->NumResolvedFields();
2296 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002297 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002298 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002299 auto* resolved_fields = dex_cache->GetResolvedFields();
2300 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002301 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002302 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002303 size_t run = 0;
2304 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002305 j != length &&
2306 elem == mirror::DexCache::GetNativePairPtrSize(
2307 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002308 ++j) {
2309 ++run;
2310 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002311 if (run == 0) {
2312 os << StringPrintf("%zd: ", i);
2313 } else {
2314 os << StringPrintf("%zd to %zd: ", i, i + run);
2315 i = i + run;
2316 }
2317 std::string msg;
2318 if (elem == nullptr) {
2319 msg = "null";
2320 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002321 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002322 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002323 } else {
2324 msg = "<not in field section>";
2325 }
2326 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002327 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002328 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002329 size_t num_types = dex_cache->NumResolvedTypes();
2330 if (num_types != 0u) {
2331 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002332 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002333 auto* resolved_types = dex_cache->GetResolvedTypes();
2334 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002335 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002336 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002337 for (size_t j = i + 1; j != num_types; ++j) {
2338 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2339 if (pair.index != other_pair.index ||
2340 pair.object.Read() != other_pair.object.Read()) {
2341 break;
2342 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002343 ++run;
2344 }
2345 if (run == 0) {
2346 os << StringPrintf("%zd: ", i);
2347 } else {
2348 os << StringPrintf("%zd to %zd: ", i, i + run);
2349 i = i + run;
2350 }
2351 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002352 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002353 if (elem == nullptr) {
2354 msg = "null";
2355 } else {
David Sehr709b0702016-10-13 09:12:37 -07002356 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002357 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002358 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002359 }
2360 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002361 }
2362 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002363 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002364 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002365 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002366
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002367 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002368 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002369 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002370 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2371 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002372 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002373 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2374 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002375 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002376 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002377 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2378 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002379 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002380 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002381 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002382 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2383 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002384 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002385 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002386 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002387 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002388 } else if (method->IsRuntimeMethod()) {
2389 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2390 if (table != nullptr) {
2391 indent_os << "IMT conflict table " << table << " method: ";
2392 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002393 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2394 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002395 }
2396 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002397 } else {
2398 const DexFile::CodeItem* code_item = method->GetCodeItem();
2399 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002400 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401
2402 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002403 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002404 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002405 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002406 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002407 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002408 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002409 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410 }
2411
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002412 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2413 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002415 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002416 if (method->IsConstructor()) {
2417 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002418 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002419 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002420 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002421 }
2422 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002423 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002424 }
2425 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002426 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002427
Igor Murashkin7617abd2015-07-10 18:27:47 -07002428 uint32_t method_access_flags = method->GetAccessFlags();
2429
Mathieu Chartiere401d142015-04-22 13:56:20 -07002430 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002431 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2432 dex_instruction_bytes,
2433 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002434 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002435
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002436 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002437 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002438
2439 double expansion =
2440 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002441 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002442 }
2443 }
2444
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002445 std::set<const void*> already_seen_;
2446 // Compute the size of the given data within the oat file and whether this is the first time
2447 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002448 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002449 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002450 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002451 already_seen_.insert(oat_data);
2452 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002453 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002454 }
2455 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002456 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002457
2458 public:
2459 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002460 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002461 size_t file_bytes;
2462
2463 size_t header_bytes;
2464 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002465 size_t art_field_bytes;
2466 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002467 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002468 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002469 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002470 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002471 size_t alignment_bytes;
2472
2473 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002474 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002476 size_t class_initializer_code_bytes;
2477 size_t large_initializer_code_bytes;
2478 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002480 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002481
2482 size_t dex_instruction_bytes;
2483
Mathieu Chartiere401d142015-04-22 13:56:20 -07002484 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002485 std::vector<size_t> method_outlier_size;
2486 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002487 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002488
Roland Levillain3887c462015-08-12 18:15:42 +01002489 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002490 : oat_file_bytes(0),
2491 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002492 header_bytes(0),
2493 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002494 art_field_bytes(0),
2495 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002496 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002497 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002498 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002499 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002500 alignment_bytes(0),
2501 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002502 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002503 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002504 class_initializer_code_bytes(0),
2505 large_initializer_code_bytes(0),
2506 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002507 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002508 dex_instruction_bytes(0) {}
2509
Elliott Hughesa0e18062012-04-13 15:59:59 -07002510 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002511 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002512 size_t bytes;
2513 size_t count;
2514 };
2515 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2516 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002517
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002518 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002519 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2520 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002521 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002522 it->second.count += 1;
2523 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002524 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002525 }
2526 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002527
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002528 double PercentOfOatBytes(size_t size) {
2529 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2530 }
2531
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002532 double PercentOfFileBytes(size_t size) {
2533 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2534 }
2535
2536 double PercentOfObjectBytes(size_t size) {
2537 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2538 }
2539
Mathieu Chartiere401d142015-04-22 13:56:20 -07002540 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002541 method_outlier_size.push_back(total_size);
2542 method_outlier_expansion.push_back(expansion);
2543 method_outlier.push_back(method);
2544 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002545
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002546 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002547 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002548 size_t sum_of_sizes = 0;
2549 size_t sum_of_sizes_squared = 0;
2550 size_t sum_of_expansion = 0;
2551 size_t sum_of_expansion_squared = 0;
2552 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002553 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002554 return;
2555 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002556 for (size_t i = 0; i < n; i++) {
2557 size_t cur_size = method_outlier_size[i];
2558 sum_of_sizes += cur_size;
2559 sum_of_sizes_squared += cur_size * cur_size;
2560 double cur_expansion = method_outlier_expansion[i];
2561 sum_of_expansion += cur_expansion;
2562 sum_of_expansion_squared += cur_expansion * cur_expansion;
2563 }
2564 size_t size_mean = sum_of_sizes / n;
2565 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2566 double expansion_mean = sum_of_expansion / n;
2567 double expansion_variance =
2568 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2569
2570 // Dump methods whose size is a certain number of standard deviations from the mean
2571 size_t dumped_values = 0;
2572 size_t skipped_values = 0;
2573 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2574 size_t cur_size_variance = i * i * size_variance;
2575 bool first = true;
2576 for (size_t j = 0; j < n; j++) {
2577 size_t cur_size = method_outlier_size[j];
2578 if (cur_size > size_mean) {
2579 size_t cur_var = cur_size - size_mean;
2580 cur_var = cur_var * cur_var;
2581 if (cur_var > cur_size_variance) {
2582 if (dumped_values > 20) {
2583 if (i == 1) {
2584 skipped_values++;
2585 } else {
2586 i = 2; // jump to counting for 1 standard deviation
2587 break;
2588 }
2589 } else {
2590 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002591 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002592 first = false;
2593 }
David Sehr709b0702016-10-13 09:12:37 -07002594 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002595 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002596 method_outlier_size[j] = 0; // don't consider this method again
2597 dumped_values++;
2598 }
2599 }
2600 }
2601 }
2602 }
2603 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002604 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002605 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002606 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002607 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002608
2609 // Dump methods whose expansion is a certain number of standard deviations from the mean
2610 dumped_values = 0;
2611 skipped_values = 0;
2612 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2613 double cur_expansion_variance = i * i * expansion_variance;
2614 bool first = true;
2615 for (size_t j = 0; j < n; j++) {
2616 double cur_expansion = method_outlier_expansion[j];
2617 if (cur_expansion > expansion_mean) {
2618 size_t cur_var = cur_expansion - expansion_mean;
2619 cur_var = cur_var * cur_var;
2620 if (cur_var > cur_expansion_variance) {
2621 if (dumped_values > 20) {
2622 if (i == 1) {
2623 skipped_values++;
2624 } else {
2625 i = 2; // jump to counting for 1 standard deviation
2626 break;
2627 }
2628 } else {
2629 if (first) {
2630 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002631 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002632 first = false;
2633 }
David Sehr709b0702016-10-13 09:12:37 -07002634 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002635 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002636 method_outlier_expansion[j] = 0.0; // don't consider this method again
2637 dumped_values++;
2638 }
2639 }
2640 }
2641 }
2642 }
2643 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002644 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002645 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002646 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002647 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002648 }
2649
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002650 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002651 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002652 {
2653 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2654 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002655 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2656 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2657 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2658 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2659 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2660 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002661 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002662 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2663 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002664 header_bytes, PercentOfFileBytes(header_bytes),
2665 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002666 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2667 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002668 dex_cache_arrays_bytes,
2669 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002670 interned_strings_bytes,
2671 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002672 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002673 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2675 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002676 CHECK_EQ(file_bytes,
2677 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2678 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2679 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002680 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002681
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002682 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002683 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002684 for (const auto& sizes_and_count : sizes_and_counts) {
2685 const std::string& descriptor(sizes_and_count.first);
2686 double average = static_cast<double>(sizes_and_count.second.bytes) /
2687 static_cast<double>(sizes_and_count.second.count);
2688 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002689 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002690 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002691 descriptor.c_str(), sizes_and_count.second.bytes,
2692 sizes_and_count.second.count, average, percent);
2693 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002694 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002695 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002696 CHECK_EQ(object_bytes, object_bytes_total);
2697
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 os << StringPrintf("oat_file_bytes = %8zd\n"
2699 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002700 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2701 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2702 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2703 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002704 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002705 managed_code_bytes,
2706 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002707 native_to_managed_code_bytes,
2708 PercentOfOatBytes(native_to_managed_code_bytes),
2709 class_initializer_code_bytes,
2710 PercentOfOatBytes(class_initializer_code_bytes),
2711 large_initializer_code_bytes,
2712 PercentOfOatBytes(large_initializer_code_bytes),
2713 large_method_code_bytes,
2714 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002715 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002716 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002717 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002718 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2719 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002720 }
2721
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002722 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002723 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002724 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002725
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002726 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2727 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002728 static_cast<double>(managed_code_bytes) /
2729 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002730 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002731 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002732 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002733
2734 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002735 }
2736 } stats_;
2737
2738 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002739 enum {
2740 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2741 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2742 kLargeConstructorDexBytes = 4000,
2743 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2744 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2745 kLargeMethodDexBytes = 16000
2746 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002747
2748 // For performance, use the *os_ directly for anything that doesn't need indentation
2749 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002750 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002751 VariableIndentationOutputStream vios_;
2752 ScopedIndentation indent1_;
2753
Ian Rogers1d54e732013-05-02 21:10:01 -07002754 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002755 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002756 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002757 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002758 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002759
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002760 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002761};
2762
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002763static int DumpImage(gc::space::ImageSpace* image_space,
2764 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002765 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002766 const ImageHeader& image_header = image_space->GetImageHeader();
2767 if (!image_header.IsValid()) {
2768 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2769 return EXIT_FAILURE;
2770 }
2771 ImageDumper image_dumper(os, *image_space, image_header, options);
2772 if (!image_dumper.Dump()) {
2773 return EXIT_FAILURE;
2774 }
2775 return EXIT_SUCCESS;
2776}
2777
2778static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002779 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002780 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002781 options->class_loader_ = &null_class_loader;
2782
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002783 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002784 if (options->app_image_ != nullptr) {
2785 if (options->app_oat_ == nullptr) {
2786 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002787 return EXIT_FAILURE;
2788 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002789 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2790 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2791 // pointers into 32 bit pointer sized ArtMethods.
2792 std::string error_msg;
2793 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2794 options->app_oat_,
2795 nullptr,
2796 nullptr,
2797 false,
2798 /*low_4gb*/true,
2799 nullptr,
2800 &error_msg));
2801 if (oat_file == nullptr) {
2802 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002803 return EXIT_FAILURE;
2804 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002805 std::unique_ptr<gc::space::ImageSpace> space(
2806 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2807 if (space == nullptr) {
2808 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2809 << error_msg;
2810 }
2811 // Open dex files for the image.
2812 std::vector<std::unique_ptr<const DexFile>> dex_files;
2813 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2814 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2815 << error_msg;
2816 }
2817 // Dump the actual image.
2818 int result = DumpImage(space.get(), options, os);
2819 if (result != EXIT_SUCCESS) {
2820 return result;
2821 }
2822 // Fall through to dump the boot images.
2823 }
2824
2825 gc::Heap* heap = runtime->GetHeap();
2826 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2827 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2828 int result = DumpImage(image_space, options, os);
2829 if (result != EXIT_SUCCESS) {
2830 return result;
2831 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002832 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002833 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002834}
2835
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002836static jobject InstallOatFile(Runtime* runtime,
2837 std::unique_ptr<OatFile> oat_file,
2838 std::vector<const DexFile*>* class_path)
2839 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002840 Thread* self = Thread::Current();
2841 CHECK(self != nullptr);
2842 // Need well-known-classes.
2843 WellKnownClasses::Init(self->GetJniEnv());
2844
2845 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002846 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002847 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002848 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2849 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002850 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002851 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002852 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002853 ObjPtr<mirror::DexCache> dex_cache =
2854 class_linker->RegisterDexFile(*dex_file, nullptr);
2855 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002856 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002857 }
2858
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002859 // Need a class loader. Fake that we're a compiler.
2860 // Note: this will run initializers through the unstarted runtime, so make sure it's
2861 // initialized.
2862 interpreter::UnstartedRuntime::Initialize();
2863
2864 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2865
2866 return class_loader;
2867}
2868
2869static int DumpOatWithRuntime(Runtime* runtime,
2870 std::unique_ptr<OatFile> oat_file,
2871 OatDumperOptions* options,
2872 std::ostream* os) {
2873 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2874 ScopedObjectAccess soa(Thread::Current());
2875
2876 OatFile* oat_file_ptr = oat_file.get();
2877 std::vector<const DexFile*> class_path;
2878 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002879
2880 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002881 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002882 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002883 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002884 options->class_loader_ = &loader_handle;
2885
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002886 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002887 bool success = oat_dumper.Dump(*os);
2888 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2889}
2890
2891static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002892 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002893 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002894 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002895 options->class_loader_ = &null_class_loader;
2896
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002897 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002898 bool success = oat_dumper.Dump(*os);
2899 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2900}
2901
2902static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2903 std::ostream* os) {
2904 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002905 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2906 oat_filename,
2907 nullptr,
2908 nullptr,
2909 false,
2910 /*low_4gb*/false,
2911 nullptr,
2912 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002913 if (oat_file == nullptr) {
2914 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2915 return EXIT_FAILURE;
2916 }
2917
2918 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002919 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002920 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002921 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002922 }
2923}
2924
David Srbecky2fdd03c2016-03-10 15:32:37 +00002925static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002926 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002927 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2928 oat_filename,
2929 nullptr,
2930 nullptr,
2931 false,
2932 /*low_4gb*/false,
2933 nullptr,
2934 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002935 if (oat_file == nullptr) {
2936 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2937 return EXIT_FAILURE;
2938 }
2939
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002940 bool result;
2941 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2942 // files for 64-bit code in the past.
2943 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002944 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002945 result = oat_symbolizer.Symbolize();
2946 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002947 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002948 result = oat_symbolizer.Symbolize();
2949 }
2950 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002951 fprintf(stderr, "Failed to symbolize\n");
2952 return EXIT_FAILURE;
2953 }
2954
2955 return EXIT_SUCCESS;
2956}
2957
Andreas Gampe9fded872016-09-25 16:08:35 -07002958class IMTDumper {
2959 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002960 static bool Dump(Runtime* runtime,
2961 const std::string& imt_file,
2962 bool dump_imt_stats,
2963 const char* oat_filename) {
2964 Thread* self = Thread::Current();
2965
2966 ScopedObjectAccess soa(self);
2967 StackHandleScope<1> scope(self);
2968 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2969 std::vector<const DexFile*> class_path;
2970
2971 if (oat_filename != nullptr) {
2972 std::string error_msg;
2973 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2974 oat_filename,
2975 nullptr,
2976 nullptr,
2977 false,
2978 /*low_4gb*/false,
2979 nullptr,
2980 &error_msg));
2981 if (oat_file == nullptr) {
2982 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2983 return false;
2984 }
2985
2986 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2987 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2988 } else {
2989 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2990 class_path = runtime->GetClassLinker()->GetBootClassPath();
2991 }
2992
2993 if (!imt_file.empty()) {
2994 return DumpImt(runtime, imt_file, class_loader);
2995 }
2996
2997 if (dump_imt_stats) {
2998 return DumpImtStats(runtime, class_path, class_loader);
2999 }
3000
3001 LOG(FATAL) << "Should not reach here";
3002 UNREACHABLE();
3003 }
3004
3005 private:
3006 static bool DumpImt(Runtime* runtime,
3007 const std::string& imt_file,
3008 Handle<mirror::ClassLoader> h_class_loader)
3009 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003010 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3011 std::unordered_set<std::string> prepared;
3012
3013 for (const std::string& line : lines) {
3014 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3015 // or a class descriptor and an interface method, in which case we will lookup the method,
3016 // determine its IMT slot, and check the class' IMT.
3017 size_t first_space = line.find(' ');
3018 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003019 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003020 } else {
3021 DumpIMTForMethod(runtime,
3022 line.substr(0, first_space),
3023 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003024 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003025 &prepared);
3026 }
3027 std::cerr << std::endl;
3028 }
3029
3030 return true;
3031 }
3032
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003033 static bool DumpImtStats(Runtime* runtime,
3034 const std::vector<const DexFile*>& dex_files,
3035 Handle<mirror::ClassLoader> h_class_loader)
3036 REQUIRES_SHARED(Locks::mutator_lock_) {
3037 size_t without_imt = 0;
3038 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003039 std::map<size_t, size_t> histogram;
3040
3041 ClassLinker* class_linker = runtime->GetClassLinker();
3042 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3043 std::unordered_set<std::string> prepared;
3044
3045 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003046 StackHandleScope<1> scope(self);
3047 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3048
3049 for (const DexFile* dex_file : dex_files) {
3050 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003051 class_def_index != dex_file->NumClassDefs();
3052 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003053 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3054 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003055 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003056 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003057 std::cerr << "Warning: could not load " << descriptor << std::endl;
3058 continue;
3059 }
3060
3061 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003062 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003063 continue;
3064 }
3065
3066 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3067 if (im_table == nullptr) {
3068 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003069 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003070 continue;
3071 }
3072
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003073 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003074 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3075 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3076 if (ptr->IsRuntimeMethod()) {
3077 if (ptr->IsImtUnimplementedMethod()) {
3078 histogram[0]++;
3079 } else {
3080 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3081 histogram[current_table->NumEntries(pointer_size)]++;
3082 }
3083 } else {
3084 histogram[1]++;
3085 }
3086 }
3087 }
3088 }
3089
3090 std::cerr << "IMT stats:"
3091 << std::endl << std::endl;
3092
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003093 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003094 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003095 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003096 << std::endl << std::endl;
3097
3098 double sum_one = 0;
3099 size_t count_one = 0;
3100
3101 std::cerr << " " << "IMT histogram" << std::endl;
3102 for (auto& bucket : histogram) {
3103 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3104 if (bucket.first > 0) {
3105 sum_one += bucket.second * bucket.first;
3106 count_one += bucket.second;
3107 }
3108 }
3109
3110 double count_zero = count_one + histogram[0];
3111 std::cerr << " Stats:" << std::endl;
3112 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3113 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3114
3115 return true;
3116 }
3117
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003118 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003119 static bool HasNoIMT(Runtime* runtime,
3120 Handle<mirror::Class> klass,
3121 const PointerSize pointer_size,
3122 std::unordered_set<std::string>* prepared)
3123 REQUIRES_SHARED(Locks::mutator_lock_) {
3124 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3125 return true;
3126 }
3127
3128 if (klass->GetImt(pointer_size) == nullptr) {
3129 PrepareClass(runtime, klass, prepared);
3130 }
3131
3132 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3133 DCHECK(object_class->IsObjectClass());
3134
3135 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3136
Mathieu Chartier6beced42016-11-15 15:51:31 -08003137 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003138 DCHECK(result);
3139 }
3140
3141 return result;
3142 }
3143
3144 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3145 REQUIRES_SHARED(Locks::mutator_lock_) {
3146 if (table == nullptr) {
3147 std::cerr << " <No IMT?>" << std::endl;
3148 return;
3149 }
3150 size_t table_index = 0;
3151 for (;;) {
3152 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3153 if (ptr == nullptr) {
3154 return;
3155 }
3156 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003157 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003158 }
3159 }
3160
3161 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3162 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003163 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003164 const std::string& class_name,
3165 const PointerSize pointer_size,
3166 mirror::Class** klass_out,
3167 std::unordered_set<std::string>* prepared)
3168 REQUIRES_SHARED(Locks::mutator_lock_) {
3169 if (class_name.empty()) {
3170 return nullptr;
3171 }
3172
3173 std::string descriptor;
3174 if (class_name[0] == 'L') {
3175 descriptor = class_name;
3176 } else {
3177 descriptor = DotToDescriptor(class_name.c_str());
3178 }
3179
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003180 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003181
3182 if (klass == nullptr) {
3183 self->ClearException();
3184 std::cerr << "Did not find " << class_name << std::endl;
3185 *klass_out = nullptr;
3186 return nullptr;
3187 }
3188
3189 StackHandleScope<1> scope(Thread::Current());
3190 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3191
3192 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3193 *klass_out = h_klass.Get();
3194 return ret;
3195 }
3196
3197 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3198 Handle<mirror::Class> h_klass,
3199 const PointerSize pointer_size,
3200 std::unordered_set<std::string>* prepared)
3201 REQUIRES_SHARED(Locks::mutator_lock_) {
3202 PrepareClass(runtime, h_klass, prepared);
3203 return h_klass->GetImt(pointer_size);
3204 }
3205
3206 static void DumpIMTForClass(Runtime* runtime,
3207 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003208 Handle<mirror::ClassLoader> h_loader,
3209 std::unordered_set<std::string>* prepared)
3210 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003211 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3212 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003213 ImTable* imt = PrepareAndGetImTable(runtime,
3214 Thread::Current(),
3215 h_loader,
3216 class_name,
3217 pointer_size,
3218 &klass,
3219 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003220 if (imt == nullptr) {
3221 return;
3222 }
3223
3224 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3225 for (size_t index = 0; index < ImTable::kSize; ++index) {
3226 std::cerr << " " << index << ":" << std::endl;
3227 ArtMethod* ptr = imt->Get(index, pointer_size);
3228 if (ptr->IsRuntimeMethod()) {
3229 if (ptr->IsImtUnimplementedMethod()) {
3230 std::cerr << " <empty>" << std::endl;
3231 } else {
3232 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3233 PrintTable(current_table, pointer_size);
3234 }
3235 } else {
David Sehr709b0702016-10-13 09:12:37 -07003236 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003237 }
3238 }
3239
3240 std::cerr << " Interfaces:" << std::endl;
3241 // Run through iftable, find methods that slot here, see if they fit.
3242 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003243 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3244 mirror::Class* iface = if_table->GetInterface(i);
3245 std::string iface_name;
3246 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003247
Mathieu Chartier6beced42016-11-15 15:51:31 -08003248 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3249 uint32_t class_hash, name_hash, signature_hash;
3250 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3251 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3252 std::cerr << " " << iface_method.PrettyMethod(true)
3253 << " slot=" << imt_slot
3254 << std::hex
3255 << " class_hash=0x" << class_hash
3256 << " name_hash=0x" << name_hash
3257 << " signature_hash=0x" << signature_hash
3258 << std::dec
3259 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003260 }
3261 }
3262 }
3263
3264 static void DumpIMTForMethod(Runtime* runtime,
3265 const std::string& class_name,
3266 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003267 Handle<mirror::ClassLoader> h_loader,
3268 std::unordered_set<std::string>* prepared)
3269 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003270 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3271 mirror::Class* klass;
3272 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003273 Thread::Current(),
3274 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003275 class_name,
3276 pointer_size,
3277 &klass,
3278 prepared);
3279 if (imt == nullptr) {
3280 return;
3281 }
3282
3283 std::cerr << class_name << " <" << method << ">" << std::endl;
3284 for (size_t index = 0; index < ImTable::kSize; ++index) {
3285 ArtMethod* ptr = imt->Get(index, pointer_size);
3286 if (ptr->IsRuntimeMethod()) {
3287 if (ptr->IsImtUnimplementedMethod()) {
3288 continue;
3289 }
3290
3291 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3292 if (current_table == nullptr) {
3293 continue;
3294 }
3295
3296 size_t table_index = 0;
3297 for (;;) {
3298 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3299 if (ptr2 == nullptr) {
3300 break;
3301 }
3302 table_index++;
3303
David Sehr709b0702016-10-13 09:12:37 -07003304 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003305 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003306 std::cerr << " Slot "
3307 << index
3308 << " ("
3309 << current_table->NumEntries(pointer_size)
3310 << ")"
3311 << std::endl;
3312 PrintTable(current_table, pointer_size);
3313 return;
3314 }
3315 }
3316 } else {
David Sehr709b0702016-10-13 09:12:37 -07003317 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003318 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003319 std::cerr << " Slot " << index << " (1)" << std::endl;
3320 std::cerr << " " << p_name << std::endl;
3321 } else {
3322 // Run through iftable, find methods that slot here, see if they fit.
3323 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003324 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3325 mirror::Class* iface = if_table->GetInterface(i);
3326 size_t num_methods = iface->NumDeclaredVirtualMethods();
3327 if (num_methods > 0) {
3328 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3329 if (ImTable::GetImtIndex(&iface_method) == index) {
3330 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003331 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003332 std::cerr << " Slot " << index << " (1)" << std::endl;
3333 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003334 }
3335 }
3336 }
3337 }
3338 }
3339 }
3340 }
3341 }
3342 }
3343
3344 // Read lines from the given stream, dropping comments and empty lines
3345 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3346 std::vector<std::string> output;
3347 while (in_stream.good()) {
3348 std::string dot;
3349 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003350 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003351 continue;
3352 }
3353 output.push_back(dot);
3354 }
3355 return output;
3356 }
3357
3358 // Read lines from the given file, dropping comments and empty lines.
3359 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3360 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3361 if (input_file.get() == nullptr) {
3362 LOG(ERROR) << "Failed to open input file " << input_filename;
3363 return std::vector<std::string>();
3364 }
3365 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3366 input_file->close();
3367 return result;
3368 }
3369
3370 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3371 // and note in the given set that the work was done.
3372 static void PrepareClass(Runtime* runtime,
3373 Handle<mirror::Class> h_klass,
3374 std::unordered_set<std::string>* done)
3375 REQUIRES_SHARED(Locks::mutator_lock_) {
3376 if (!h_klass->ShouldHaveImt()) {
3377 return;
3378 }
3379
3380 std::string name;
3381 name = h_klass->GetDescriptor(&name);
3382
3383 if (done->find(name) != done->end()) {
3384 return;
3385 }
3386 done->insert(name);
3387
3388 if (h_klass->HasSuperClass()) {
3389 StackHandleScope<1> h(Thread::Current());
3390 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3391 }
3392
3393 if (!h_klass->IsTemp()) {
3394 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3395 }
3396 }
3397};
3398
Igor Murashkin37743352014-11-13 14:38:00 -08003399struct OatdumpArgs : public CmdlineArgs {
3400 protected:
3401 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003402
Igor Murashkin37743352014-11-13 14:38:00 -08003403 virtual ParseStatus ParseCustom(const StringPiece& option,
3404 std::string* error_msg) OVERRIDE {
3405 {
3406 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3407 if (base_parse != kParseUnknownArgument) {
3408 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003409 }
3410 }
3411
Igor Murashkin37743352014-11-13 14:38:00 -08003412 if (option.starts_with("--oat-file=")) {
3413 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3414 } else if (option.starts_with("--image=")) {
3415 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003416 } else if (option == "--no-dump:vmap") {
3417 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003418 } else if (option =="--dump:code_info_stack_maps") {
3419 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003420 } else if (option == "--no-disassemble") {
3421 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003422 } else if (option =="--header-only") {
3423 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003424 } else if (option.starts_with("--symbolize=")) {
3425 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3426 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003427 } else if (option.starts_with("--only-keep-debug")) {
3428 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003429 } else if (option.starts_with("--class-filter=")) {
3430 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003431 } else if (option.starts_with("--method-filter=")) {
3432 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003433 } else if (option.starts_with("--list-classes")) {
3434 list_classes_ = true;
3435 } else if (option.starts_with("--list-methods")) {
3436 list_methods_ = true;
3437 } else if (option.starts_with("--export-dex-to=")) {
3438 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3439 } else if (option.starts_with("--addr2instr=")) {
3440 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3441 *error_msg = "Address conversion failed";
3442 return kParseError;
3443 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003444 } else if (option.starts_with("--app-image=")) {
3445 app_image_ = option.substr(strlen("--app-image=")).data();
3446 } else if (option.starts_with("--app-oat=")) {
3447 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003448 } else if (option.starts_with("--dump-imt=")) {
3449 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3450 } else if (option == "--dump-imt-stats") {
3451 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003452 } else {
3453 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003454 }
3455
Igor Murashkin37743352014-11-13 14:38:00 -08003456 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003457 }
3458
Igor Murashkin37743352014-11-13 14:38:00 -08003459 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3460 // Infer boot image location from the image location if possible.
3461 if (boot_image_location_ == nullptr) {
3462 boot_image_location_ = image_location_;
3463 }
3464
3465 // Perform the parent checks.
3466 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3467 if (parent_checks != kParseOk) {
3468 return parent_checks;
3469 }
3470
3471 // Perform our own checks.
3472 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3473 *error_msg = "Either --image or --oat-file must be specified";
3474 return kParseError;
3475 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3476 *error_msg = "Either --image or --oat-file must be specified but not both";
3477 return kParseError;
3478 }
3479
3480 return kParseOk;
3481 }
3482
3483 virtual std::string GetUsage() const {
3484 std::string usage;
3485
3486 usage +=
3487 "Usage: oatdump [options] ...\n"
3488 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3489 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3490 "\n"
3491 // Either oat-file or image is required.
3492 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3493 " Example: --oat-file=/system/framework/boot.oat\n"
3494 "\n"
3495 " --image=<file.art>: specifies an input image location.\n"
3496 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003497 "\n"
3498 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003499 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003500 " Example: --app-image=app.art\n"
3501 "\n"
3502 " --app-oat=<file.odex>: specifies an input app oat.\n"
3503 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003504 "\n";
3505
3506 usage += Base::GetUsage();
3507
3508 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003509 " --no-dump:vmap may be used to disable vmap dumping.\n"
3510 " Example: --no-dump:vmap\n"
3511 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003512 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3513 " Example: --dump:code_info_stack_maps\n"
3514 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003515 " --no-disassemble may be used to disable disassembly.\n"
3516 " Example: --no-disassemble\n"
3517 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003518 " --header-only may be used to print only the oat header.\n"
3519 " Example: --header-only\n"
3520 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003521 " --list-classes may be used to list target file classes (can be used with filters).\n"
3522 " Example: --list-classes\n"
3523 " Example: --list-classes --class-filter=com.example.foo\n"
3524 "\n"
3525 " --list-methods may be used to list target file methods (can be used with filters).\n"
3526 " Example: --list-methods\n"
3527 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3528 "\n"
3529 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3530 " Example: --symbolize=/system/framework/boot.oat\n"
3531 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003532 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3533 " .rodata and .text sections are omitted in the output file to save space.\n"
3534 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3535 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003536 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3537 " Example: --class-filter=com.example.foo\n"
3538 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003539 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3540 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003541 "\n"
3542 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3543 " Example: --export-dex-to=/data/local/tmp\n"
3544 "\n"
3545 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3546 " address (e.g. PC from crash dump)\n"
3547 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003548 "\n"
3549 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3550 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003551 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003552 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003553 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003554 " Example: --dump-imt=imt.txt\n"
3555 "\n"
3556 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3557 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003558 "\n";
3559
3560 return usage;
3561 }
3562
3563 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003564 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003565 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003566 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003567 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003568 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003569 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003570 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003571 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003572 bool disassemble_code_ = true;
3573 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003574 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003575 bool list_classes_ = false;
3576 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003577 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003578 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003579 uint32_t addr2instr_ = 0;
3580 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003581 const char* app_image_ = nullptr;
3582 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003583};
3584
Igor Murashkin37743352014-11-13 14:38:00 -08003585struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3586 virtual bool NeedsRuntime() OVERRIDE {
3587 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003588
Igor Murashkin37743352014-11-13 14:38:00 -08003589 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3590 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3591
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003592 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003593 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003594 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003595 args_->disassemble_code_,
3596 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003597 args_->class_filter_,
3598 args_->method_filter_,
3599 args_->list_classes_,
3600 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003601 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003602 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003603 args_->app_image_,
3604 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003605 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003606
Andreas Gampe9fded872016-09-25 16:08:35 -07003607 return (args_->boot_image_location_ != nullptr ||
3608 args_->image_location_ != nullptr ||
3609 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003610 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003611 }
3612
Igor Murashkin37743352014-11-13 14:38:00 -08003613 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3614 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003615 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003616
Mathieu Chartierd424d082014-10-15 10:31:46 -07003617 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003618
Andreas Gampec24f3992014-12-17 20:40:11 -08003619 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003620 // ELF has special kind of section called SHT_NOBITS which allows us to create
3621 // sections which exist but their data is omitted from the ELF file to save space.
3622 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3623 // with only debug data. We use it in similar way to exclude .rodata and .text.
3624 bool no_bits = args_->only_keep_debug_;
3625 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003626 } else {
3627 return DumpOat(nullptr,
3628 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003629 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003630 args_->os_) == EXIT_SUCCESS;
3631 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003632 }
3633
Igor Murashkin37743352014-11-13 14:38:00 -08003634 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3635 CHECK(args_ != nullptr);
3636
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003637 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3638 return IMTDumper::Dump(runtime,
3639 args_->imt_dump_,
3640 args_->imt_stat_dump_,
3641 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003642 }
3643
Igor Murashkin37743352014-11-13 14:38:00 -08003644 if (args_->oat_filename_ != nullptr) {
3645 return DumpOat(runtime,
3646 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003647 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003648 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003649 }
Igor Murashkin37743352014-11-13 14:38:00 -08003650
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003651 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003652 }
3653
Igor Murashkin37743352014-11-13 14:38:00 -08003654 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3655};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003656
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003657} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003658
3659int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003660 art::OatdumpMain main;
3661 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003662}