blob: 17b47a482a15001473f93ee0a4935f8a6c82043e [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 Gampe9186ced2016-12-12 14:28:21 -080029#include "android-base/strings.h"
30
Ian Rogersd582fa42014-11-05 23:46:43 -080031#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070032#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000034#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080035#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070036#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000038#include "debug/elf_debug_writer.h"
39#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070040#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070041#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080042#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070043#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070044#include "gc/space/image_space.h"
45#include "gc/space/large_object_space.h"
46#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080047#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070048#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080049#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070050#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000051#include "linker/buffered_output_stream.h"
52#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "mirror/array-inl.h"
54#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010055#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056#include "mirror/object-inl.h"
57#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010059#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070060#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080061#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070062#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070063#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000064#include "ScopedLocalRef.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070065#include "stack_map.h"
66#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080067#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070068#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010069#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080070#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000071#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070072#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070073
Igor Murashkin37743352014-11-13 14:38:00 -080074#include <sys/stat.h>
75#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070076
Igor Murashkin37743352014-11-13 14:38:00 -080077namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070078
Mathieu Chartiere401d142015-04-22 13:56:20 -070079const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080080 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070081 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070082 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010083 "kSaveAllCalleeSavesMethod",
84 "kSaveRefsOnlyMethod",
85 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010086 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070087};
88
89const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070090 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070091 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000092 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -070093};
94
Mathieu Chartierac8f4392015-08-27 13:54:20 -070095// Map is so that we don't allocate multiple dex files for the same OatDexFile.
96static std::map<const OatFile::OatDexFile*,
97 std::unique_ptr<const DexFile>> opened_dex_files;
98
99const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
100 DCHECK(oat_dex_file != nullptr);
101 auto it = opened_dex_files.find(oat_dex_file);
102 if (it != opened_dex_files.end()) {
103 return it->second.get();
104 }
105 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
106 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
107 return ret;
108}
109
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800110template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100111class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700112 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000113 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
114 oat_file_(oat_file),
115 builder_(nullptr),
116 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
117 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700118 }
119
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700120 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000121 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800122 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000123 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000124
125 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
126 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000127 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800128 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129
130 builder_->Start();
131
132 auto* rodata = builder_->GetRoData();
133 auto* text = builder_->GetText();
134 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135
David Srbecky6d8c8f02015-10-26 10:57:09 +0000136 const uint8_t* rodata_begin = oat_file_->Begin();
137 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000138 if (no_bits_) {
139 rodata->WriteNoBitsSection(rodata_size);
140 } else {
141 rodata->Start();
142 rodata->WriteFully(rodata_begin, rodata_size);
143 rodata->End();
144 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145
David Srbecky6d8c8f02015-10-26 10:57:09 +0000146 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
147 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000148 if (no_bits_) {
149 text->WriteNoBitsSection(text_size);
150 } else {
151 text->Start();
152 text->WriteFully(text_begin, text_size);
153 text->End();
154 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155
156 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000157 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158 }
159
Douglas Leung316a2182015-09-17 15:26:25 -0700160 if (isa == kMips || isa == kMips64) {
161 builder_->WriteMIPSabiflagsSection();
162 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000163 builder_->PrepareDynamicSection(elf_file->GetPath(),
164 rodata_size,
165 text_size,
166 oat_file_->BssSize(),
167 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000168 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700169
David Srbecky5d950762016-03-07 20:47:29 +0000170 Walk();
171 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
172 method_debug_infos_.push_back(trampoline);
173 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700174
David Srbecky5d950762016-03-07 20:47:29 +0000175 debug::WriteDebugInfo(builder_.get(),
176 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
177 dwarf::DW_DEBUG_FRAME_FORMAT,
178 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700179
David Srbecky6d8c8f02015-10-26 10:57:09 +0000180 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181
Vladimir Marko10c13562015-11-25 14:33:36 +0000182 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700183 }
184
David Srbecky5d950762016-03-07 20:47:29 +0000185 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700186 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
187 for (size_t i = 0; i < oat_dex_files.size(); i++) {
188 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700189 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000190 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191 }
192 }
193
David Srbecky5d950762016-03-07 20:47:29 +0000194 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700195 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700196 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
197 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 return;
199 }
200 for (size_t class_def_index = 0;
201 class_def_index < dex_file->NumClassDefs();
202 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700203 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
204 OatClassType type = oat_class.GetType();
205 switch (type) {
206 case kOatClassAllCompiled:
207 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000208 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700209 break;
210
211 case kOatClassNoneCompiled:
212 case kOatClassMax:
213 // Ignore.
214 break;
215 }
216 }
217 }
218
David Srbecky5d950762016-03-07 20:47:29 +0000219 void WalkOatClass(const OatFile::OatClass& oat_class,
220 const DexFile& dex_file,
221 uint32_t class_def_index) {
222 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700223 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 if (class_data == nullptr) { // empty class such as a marker interface?
225 return;
226 }
227 // Note: even if this is an interface or a native class, we still have to walk it, as there
228 // might be a static initializer.
229 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000231 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
232 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
233 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
234 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
235 dex_file,
236 class_def_index,
237 it.GetMemberIndex(),
238 it.GetMethodCodeItem(),
239 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 }
241 DCHECK(!it.HasNext());
242 }
243
David Srbecky5d950762016-03-07 20:47:29 +0000244 void WalkOatMethod(const OatFile::OatMethod& oat_method,
245 const DexFile& dex_file,
246 uint32_t class_def_index,
247 uint32_t dex_method_index,
248 const DexFile::CodeItem* code_item,
249 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700250 if ((method_access_flags & kAccAbstract) != 0) {
251 // Abstract method, no code.
252 return;
253 }
David Srbecky5d950762016-03-07 20:47:29 +0000254 const OatHeader& oat_header = oat_file_->GetOatHeader();
255 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
256 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 // No code.
258 return;
259 }
260
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100261 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
262 // Clear Thumb2 bit.
263 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
264
David Srbecky5d950762016-03-07 20:47:29 +0000265 debug::MethodDebugInfo info = debug::MethodDebugInfo();
266 info.trampoline_name = nullptr;
267 info.dex_file = &dex_file;
268 info.class_def_index = class_def_index;
269 info.dex_method_index = dex_method_index;
270 info.access_flags = method_access_flags;
271 info.code_item = code_item;
272 info.isa = oat_header.GetInstructionSet();
273 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
274 info.is_native_debuggable = oat_header.IsNativeDebuggable();
275 info.is_optimized = method_header->IsOptimized();
276 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100277 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000278 info.code_size = method_header->GetCodeSize();
279 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
280 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
281 info.cfi = ArrayRef<uint8_t>();
282 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700283 }
284
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700286 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800287 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000288 std::vector<debug::MethodDebugInfo> method_debug_infos_;
289 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700290 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000291 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700292};
293
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700294class OatDumperOptions {
295 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100296 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100297 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700298 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700299 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800300 const char* class_filter,
301 const char* method_filter,
302 bool list_classes,
303 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000304 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800305 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800306 const char* app_image,
307 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800308 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100309 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100310 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700311 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700312 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800313 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000314 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800315 list_classes_(list_classes),
316 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000317 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800319 app_image_(app_image),
320 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800321 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800322 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700323
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700324 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100325 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700326 const bool disassemble_code_;
327 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800328 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000329 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800330 const bool list_classes_;
331 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000332 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800334 const char* const app_image_;
335 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800336 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700337 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700338};
339
Elliott Hughese3c845c2012-02-28 17:23:01 -0800340class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700341 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100342 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000343 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800344 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800346 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800347 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
348 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700349 new DisassemblerOptions(
350 options_.absolute_addresses_,
351 oat_file.Begin(),
352 oat_file.End(),
353 true /* can_read_literals_ */,
354 Is64BitInstructionSet(instruction_set_)
355 ? &Thread::DumpThreadOffset<PointerSize::k64>
356 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800357 CHECK(options_.class_loader_ != nullptr);
358 CHECK(options_.class_filter_ != nullptr);
359 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800360 AddAllOffsets();
361 }
362
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700364 delete disassembler_;
365 }
366
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800367 InstructionSet GetInstructionSet() {
368 return instruction_set_;
369 }
370
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371 bool Dump(std::ostream& os) {
372 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800373 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700374
375 os << "MAGIC:\n";
376 os << oat_header.GetMagic() << "\n\n";
377
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800378 os << "LOCATION:\n";
379 os << oat_file_.GetLocation() << "\n\n";
380
Brian Carlstromaded5f72011-10-07 17:15:04 -0700381 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800382 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700383
Elliott Hughesa72ec822012-03-05 17:12:22 -0800384 os << "INSTRUCTION SET:\n";
385 os << oat_header.GetInstructionSet() << "\n\n";
386
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700387 {
388 std::unique_ptr<const InstructionSetFeatures> features(
389 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
390 oat_header.GetInstructionSetFeaturesBitmap()));
391 os << "INSTRUCTION SET FEATURES:\n";
392 os << features->GetFeatureString() << "\n\n";
393 }
Dave Allison70202782013-10-22 17:52:19 -0700394
Brian Carlstromaded5f72011-10-07 17:15:04 -0700395 os << "DEX FILE COUNT:\n";
396 os << oat_header.GetDexFileCount() << "\n\n";
397
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800398#define DUMP_OAT_HEADER_OFFSET(label, offset) \
399 os << label " OFFSET:\n"; \
400 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800401 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800402 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
403 } \
404 os << StringPrintf("\n\n");
405
406 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
407 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
408 GetInterpreterToInterpreterBridgeOffset);
409 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
410 GetInterpreterToCompiledCodeBridgeOffset);
411 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
412 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800413 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
414 GetQuickGenericJniTrampolineOffset);
415 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
416 GetQuickImtConflictTrampolineOffset);
417 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
418 GetQuickResolutionTrampolineOffset);
419 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
420 GetQuickToInterpreterBridgeOffset);
421#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700422
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700423 os << "IMAGE PATCH DELTA:\n";
424 os << StringPrintf("%d (0x%08x)\n\n",
425 oat_header.GetImagePatchDelta(),
426 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700427
Brian Carlstrom28db0122012-10-18 16:20:41 -0700428 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
429 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
430
431 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800432 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700433
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700434 // Print the key-value store.
435 {
436 os << "KEY VALUE STORE:\n";
437 size_t index = 0;
438 const char* key;
439 const char* value;
440 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
441 os << key << " = " << value << "\n";
442 index++;
443 }
444 os << "\n";
445 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700446
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800447 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700448 os << "BEGIN:\n";
449 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700450
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700451 os << "END:\n";
452 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
453 }
454
455 os << "SIZE:\n";
456 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700457
458 os << std::flush;
459
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800460 // If set, adjust relative address to be searched
461 if (options_.addr2instr_ != 0) {
462 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
463 os << "SEARCH ADDRESS (executable offset + input):\n";
464 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
465 }
466
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700467 // Dumping the dex file overview is compact enough to do even if header only.
468 DexFileData cumulative;
469 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
470 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
471 CHECK(oat_dex_file != nullptr);
472 std::string error_msg;
473 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
474 if (dex_file == nullptr) {
475 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
476 << error_msg;
477 continue;
478 }
479 DexFileData data(*dex_file);
480 os << "Dex file data for " << dex_file->GetLocation() << "\n";
481 data.Dump(os);
482 os << "\n";
483 cumulative.Add(data);
484 }
485 os << "Cumulative dex file data\n";
486 cumulative.Dump(os);
487 os << "\n";
488
David Brazdilc03d7b62016-03-02 12:18:03 +0000489 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000490 VariableIndentationOutputStream vios(&os);
491 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
492 if (vdex_header.IsValid()) {
493 std::string error_msg;
494 std::vector<const DexFile*> dex_files;
495 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
496 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
497 if (dex_file == nullptr) {
498 os << "Error opening dex file: " << error_msg << std::endl;
499 return false;
500 }
501 dex_files.push_back(dex_file);
502 }
503 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
504 deps.Dump(&vios);
505 } else {
506 os << "UNRECOGNIZED vdex file, magic "
507 << vdex_header.GetMagic()
508 << ", version "
509 << vdex_header.GetVersion()
510 << "\n";
511 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000512 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
513 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
514 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800515
David Brazdilc03d7b62016-03-02 12:18:03 +0000516 // If file export selected skip file analysis
517 if (options_.export_dex_location_) {
518 if (!ExportDexFile(os, *oat_dex_file)) {
519 success = false;
520 }
521 } else {
522 if (!DumpOatDexFile(os, *oat_dex_file)) {
523 success = false;
524 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800525 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700526 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700527 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000528
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700529 os << std::flush;
530 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700531 }
532
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700534 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
535 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 return 0; // Address not in oat file
537 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800538 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
539 reinterpret_cast<uintptr_t>(oat_file_.Begin());
540 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800541 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 return end_offset - begin_offset;
544 }
545
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800546 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700547 return oat_file_.GetOatHeader().GetInstructionSet();
548 }
549
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700550 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
552 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700553 CHECK(oat_dex_file != nullptr);
554 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700555 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
556 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700557 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
558 << "': " << error_msg;
559 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800560 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700561 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700562 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700563 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700564 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100565 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100567 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800568 }
569 }
570 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700571 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800572 }
573
Brian Carlstromaded5f72011-10-07 17:15:04 -0700574 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800575 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800576 // We don't know the length of the code for each method, but we need to know where to stop
577 // when disassembling. What we do know is that a region of code will be followed by some other
578 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
579 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
581 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700582 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700583 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700584 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
585 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700586 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
587 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800588 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800589 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800590 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800591 for (size_t class_def_index = 0;
592 class_def_index < dex_file->NumClassDefs();
593 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100595 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700596 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700597 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800598 ClassDataItemIterator it(*dex_file, class_data);
599 SkipAllFields(it);
600 uint32_t class_method_index = 0;
601 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100602 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800603 it.Next();
604 }
605 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100606 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800607 it.Next();
608 }
609 }
610 }
611 }
612
613 // If the last thing in the file is code for a method, there won't be an offset for the "next"
614 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
615 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800616 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800617 }
618
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700619 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
620 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
621 }
622
Elliott Hughese3c845c2012-02-28 17:23:01 -0800623 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800624 uint32_t code_offset = oat_method.GetCodeOffset();
625 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
626 code_offset &= ~0x1;
627 }
628 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800629 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800630 }
631
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700632 // Dex file data, may be for multiple different dex files.
633 class DexFileData {
634 public:
635 DexFileData() {}
636
637 explicit DexFileData(const DexFile& dex_file)
638 : num_string_ids_(dex_file.NumStringIds()),
639 num_method_ids_(dex_file.NumMethodIds()),
640 num_field_ids_(dex_file.NumFieldIds()),
641 num_type_ids_(dex_file.NumTypeIds()),
642 num_class_defs_(dex_file.NumClassDefs()) {
643 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
644 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
645 WalkClass(dex_file, class_def);
646 }
647 }
648
649 void Add(const DexFileData& other) {
650 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
651 num_string_ids_from_code_ += other.num_string_ids_from_code_;
652 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
653 dex_code_bytes_ += other.dex_code_bytes_;
654 num_string_ids_ += other.num_string_ids_;
655 num_method_ids_ += other.num_method_ids_;
656 num_field_ids_ += other.num_field_ids_;
657 num_type_ids_ += other.num_type_ids_;
658 num_class_defs_ += other.num_class_defs_;
659 }
660
661 void Dump(std::ostream& os) {
662 os << "Num string ids: " << num_string_ids_ << "\n";
663 os << "Num method ids: " << num_method_ids_ << "\n";
664 os << "Num field ids: " << num_field_ids_ << "\n";
665 os << "Num type ids: " << num_type_ids_ << "\n";
666 os << "Num class defs: " << num_class_defs_ << "\n";
667 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
668 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
669 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
670 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
671 }
672
673 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800674 // All of the elements from one container to another.
675 template <typename Dest, typename Src>
676 static void AddAll(Dest& dest, const Src& src) {
677 dest.insert(src.begin(), src.end());
678 }
679
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700680 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
681 const uint8_t* class_data = dex_file.GetClassData(class_def);
682 if (class_data == nullptr) { // empty class such as a marker interface?
683 return;
684 }
685 ClassDataItemIterator it(dex_file, class_data);
686 SkipAllFields(it);
687 while (it.HasNextDirectMethod()) {
688 WalkCodeItem(dex_file, it.GetMethodCodeItem());
689 it.Next();
690 }
691 while (it.HasNextVirtualMethod()) {
692 WalkCodeItem(dex_file, it.GetMethodCodeItem());
693 it.Next();
694 }
695 DCHECK(!it.HasNext());
696 }
697
698 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
699 if (code_item == nullptr) {
700 return;
701 }
702 const size_t code_item_size = code_item->insns_size_in_code_units_;
703 const uint16_t* code_ptr = code_item->insns_;
704 const uint16_t* code_end = code_item->insns_ + code_item_size;
705
706 // If we inserted a new dex code item pointer, add to total code bytes.
707 if (dex_code_item_ptrs_.insert(code_ptr).second) {
708 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
709 }
710
711 while (code_ptr < code_end) {
712 const Instruction* inst = Instruction::At(code_ptr);
713 switch (inst->Opcode()) {
714 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800715 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700716 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
717 ++num_string_ids_from_code_;
718 break;
719 }
720 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800721 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700722 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
723 ++num_string_ids_from_code_;
724 break;
725 }
726 default:
727 break;
728 }
729
730 code_ptr += inst->SizeInCodeUnits();
731 }
732 }
733
734 // Unique string ids loaded from dex code.
735 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
736
737 // Total string ids loaded from dex code.
738 size_t num_string_ids_from_code_ = 0;
739
740 // Unique code pointers.
741 std::set<const void*> dex_code_item_ptrs_;
742
743 // Total "unique" dex code bytes.
744 size_t dex_code_bytes_ = 0;
745
746 // Other dex ids.
747 size_t num_string_ids_ = 0;
748 size_t num_method_ids_ = 0;
749 size_t num_field_ids_ = 0;
750 size_t num_type_ids_ = 0;
751 size_t num_class_defs_ = 0;
752 };
753
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
755 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800756 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700757 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800758 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800759 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700760
Andreas Gampe2ba88952016-04-29 17:52:07 -0700761 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100762 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
763
764 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700765 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100766 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700767 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
768 dex_offset,
769 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700770
Andreas Gampe2ba88952016-04-29 17:52:07 -0700771 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700772 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700773 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
774 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700775 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700776 os << std::flush;
777 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700778 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100779
Andreas Gampe2ba88952016-04-29 17:52:07 -0700780 // Print lookup table, if it exists.
781 if (oat_dex_file.GetLookupTableData() != nullptr) {
782 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
783 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700784 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700785 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
786 table_offset,
787 table_offset + table_size - 1);
788 }
789
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100790 VariableIndentationOutputStream vios(&os);
791 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800792 for (size_t class_def_index = 0;
793 class_def_index < dex_file->NumClassDefs();
794 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700795 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
796 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797
798 // TODO: Support regex
799 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
800 continue;
801 }
802
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700803 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100804 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700805 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800806 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100807 << " (" << oat_class.GetStatus() << ")"
808 << " (" << oat_class.GetType() << ")\n";
809 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700810 if (options_.list_classes_) {
811 continue;
812 }
813 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700814 success = false;
815 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800816 if (stop_analysis) {
817 os << std::flush;
818 return success;
819 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700820 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700821 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700822 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700823 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700824 }
825
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800826 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
827 std::string error_msg;
828 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
829
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700830 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800831 if (dex_file == nullptr) {
832 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
833 return false;
834 }
835 size_t fsize = oat_dex_file.FileSize();
836
837 // Some quick checks just in case
838 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
839 os << "Invalid dex file\n";
840 return false;
841 }
842
843 // Verify output directory exists
844 if (!OS::DirectoryExists(options_.export_dex_location_)) {
845 // TODO: Extend OS::DirectoryExists if symlink support is required
846 os << options_.export_dex_location_ << " output directory not found or symlink\n";
847 return false;
848 }
849
850 // Beautify path names
851 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
852 return false;
853 }
854
855 std::string dex_orig_name;
856 size_t dex_orig_pos = dex_file_location.rfind('/');
857 if (dex_orig_pos == std::string::npos)
858 dex_orig_name = dex_file_location;
859 else
860 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
861
862 // A more elegant approach to efficiently name user installed apps is welcome
863 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
864 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
865 size_t apk_orig_pos = dex_file_location.rfind('/');
866 if (apk_orig_pos != std::string::npos) {
867 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
868 }
869 }
870
871 std::string out_dex_path(options_.export_dex_location_);
872 if (out_dex_path.back() != '/') {
873 out_dex_path.append("/");
874 }
875 out_dex_path.append(dex_orig_name);
876 out_dex_path.append("_export.dex");
877 if (out_dex_path.length() > PATH_MAX) {
878 return false;
879 }
880
881 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
882 if (file.get() == nullptr) {
883 os << "Failed to open output dex file " << out_dex_path;
884 return false;
885 }
886
887 if (!file->WriteFully(dex_file->Begin(), fsize)) {
888 os << "Failed to write dex file";
889 file->Erase();
890 return false;
891 }
892
893 if (file->FlushCloseOrErase() != 0) {
894 os << "Flush and close failed";
895 return false;
896 }
897
898 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
899 os << std::flush;
900
901 return true;
902 }
903
Elliott Hughese3c845c2012-02-28 17:23:01 -0800904 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700905 while (it.HasNextStaticField()) {
906 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700907 }
Ian Rogers0571d352011-11-03 19:51:38 -0700908 while (it.HasNextInstanceField()) {
909 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700910 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800911 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700912
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100913 bool DumpOatClass(VariableIndentationOutputStream* vios,
914 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700915 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800917 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700918 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700919 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700921 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800922 }
923 ClassDataItemIterator it(dex_file, class_data);
924 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700925 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700926 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100927 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700929 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 success = false;
931 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800932 if (addr_found) {
933 *stop_analysis = true;
934 return success;
935 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700937 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700938 }
Ian Rogers0571d352011-11-03 19:51:38 -0700939 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100940 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700942 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 success = false;
944 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800945 if (addr_found) {
946 *stop_analysis = true;
947 return success;
948 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700950 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700951 }
Ian Rogers0571d352011-11-03 19:51:38 -0700952 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700954 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700955 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800956
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700957 static constexpr uint32_t kPrologueBytes = 16;
958
959 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
960 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
961
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 bool DumpOatMethod(VariableIndentationOutputStream* vios,
963 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700964 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700965 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800966 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700967 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800969
970 // TODO: Support regex
971 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
972 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000973 return success;
974 }
975
David Sehr709b0702016-10-13 09:12:37 -0700976 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
978 class_method_index, pretty_method.c_str(),
979 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800980 if (options_.list_methods_) return success;
981
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800982 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
983 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
984 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
985 uint32_t code_offset = oat_method.GetCodeOffset();
986 uint32_t code_size = oat_method.GetQuickCodeSize();
987 if (resolved_addr2instr_ != 0) {
988 if (resolved_addr2instr_ > code_offset + code_size) {
989 return success;
990 } else {
991 *addr_found = true; // stop analyzing file at next iteration
992 }
993 }
994
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100995 // Everything below is indented at least once.
996 ScopedIndentation indent1(vios);
997
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800998 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 vios->Stream() << "DEX CODE:\n";
1000 ScopedIndentation indent2(vios);
1001 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001002 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001003
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001004 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001005 std::unique_ptr<verifier::MethodVerifier> verifier;
1006 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001007 // We need to have the handle scope stay live until after the verifier since the verifier has
1008 // a handle to the dex cache from hs.
1009 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001010 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1011 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001012 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001013 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001014 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001015 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001016 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001018 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001019 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001020 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001021 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001022 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001023 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001024 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1025 oat_method_offsets_offset, oat_file_.Size());
1026 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001027 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001028 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001029 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 ScopedIndentation indent2(vios);
1032 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001033 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1034 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001035 vios->Stream() << StringPrintf("WARNING: "
1036 "code offset 0x%08x is past end of file 0x%08zx.\n",
1037 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001038 success = false;
1039 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001040 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001041 }
1042 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001043 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001044 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1045 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001046
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001047 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001049 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001050 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001051 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001052 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1054 method_header_offset, oat_file_.Size());
1055 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001056 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001057 return false;
1058 }
1059
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001060 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001061 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001062 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001063 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001064 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001065 uint32_t vmap_table_offset = method_header ==
1066 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001067 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001068
1069 size_t vmap_table_offset_limit =
1070 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1071 ? oat_file_.GetVdexFile()->Size()
1072 : method_header->GetCode() - oat_file_.Begin();
1073 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001074 vios->Stream() << StringPrintf("WARNING: "
1075 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1076 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001077 vmap_table_offset,
1078 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001079 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001080 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001081 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001082 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001084 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001085 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001086 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001087
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001088 ScopedIndentation indent2(vios);
1089 vios->Stream()
1090 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1091 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1092 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1093 vios->Stream() << "\n";
1094 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1095 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1096 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001097 }
1098 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001099 // Based on spill masks from QuickMethodFrameInfo so placed
1100 // after it is dumped, but useful for understanding quick
1101 // code, so dumped here.
1102 ScopedIndentation indent2(vios);
1103 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001104 }
1105 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001106 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001107 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1108 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001109 ScopedIndentation indent2(vios);
1110 vios->Stream() << StringPrintf("WARNING: "
1111 "code size offset 0x%08x is past end of file 0x%08zx.",
1112 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 success = false;
1114 } else {
1115 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001116 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1117 uint64_t aligned_code_end = aligned_code_begin + code_size;
1118
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001119 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001120 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001121 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001122 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1123 code_offset,
1124 code_size_offset,
1125 code_size,
1126 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001127
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001128 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001129 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001130 vios->Stream() << StringPrintf("WARNING: "
1131 "start of code at 0x%08x is past end of file 0x%08zx.",
1132 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001133 success = false;
1134 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001135 vios->Stream() << StringPrintf(
1136 "WARNING: "
1137 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1138 "code size is 0x%08x loaded from offset 0x%08x.\n",
1139 aligned_code_end, oat_file_.Size(),
1140 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001141 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001142 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001143 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001144 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001145 }
1146 }
1147 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001148 vios->Stream() << StringPrintf(
1149 "WARNING: "
1150 "code size %d is bigger than max expected threshold of %d. "
1151 "code size is 0x%08x loaded from offset 0x%08x.\n",
1152 code_size, kMaxCodeSize,
1153 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001154 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001155 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001156 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001157 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001158 }
1159 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001160 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001161 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001162 }
1163 }
1164 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001165 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001166 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001167 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001168
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001169 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1170 if (spill_mask == 0) {
1171 return;
1172 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001173 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001174 for (size_t i = 0; i < 32; i++) {
1175 if ((spill_mask & (1 << i)) != 0) {
1176 if (is_float) {
1177 os << "fr" << i;
1178 } else {
1179 os << "r" << i;
1180 }
1181 spill_mask ^= 1 << i; // clear bit
1182 if (spill_mask != 0) {
1183 os << ", ";
1184 } else {
1185 break;
1186 }
1187 }
1188 }
1189 os << ")";
1190 }
1191
Roland Levillain442b46a2015-02-18 16:54:21 +00001192 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001194 const OatFile::OatMethod& oat_method,
1195 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001196 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1197 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001198 const void* raw_code_info = oat_method.GetVmapTable();
1199 if (raw_code_info != nullptr) {
1200 CodeInfo code_info(raw_code_info);
1201 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001202 ScopedIndentation indent1(vios);
1203 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001204 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001205 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1206 // We don't encode the size in the table, so just emit that we have quickened
1207 // information.
1208 ScopedIndentation indent(vios);
1209 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001210 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001211 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001212 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001213 }
1214
1215 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001217 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001218 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001219 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001221 oat_method.GetCodeOffset(),
1222 code_item.registers_size_,
1223 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001224 }
1225
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001226 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1227 const DexFile::CodeItem* code_item) {
1228 if (code_item != nullptr) {
1229 size_t num_locals_ins = code_item->registers_size_;
1230 size_t num_ins = code_item->ins_size_;
1231 size_t num_locals = num_locals_ins - num_ins;
1232 size_t num_outs = code_item->outs_size_;
1233
1234 os << "vr_stack_locations:";
1235 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1236 // For readability, delimit the different kinds of VRs.
1237 if (reg == num_locals_ins) {
1238 os << "\n\tmethod*:";
1239 } else if (reg == num_locals && num_ins > 0) {
1240 os << "\n\tins:";
1241 } else if (reg == 0 && num_locals > 0) {
1242 os << "\n\tlocals:";
1243 }
1244
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001245 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1246 code_item,
1247 oat_method.GetCoreSpillMask(),
1248 oat_method.GetFpSpillMask(),
1249 oat_method.GetFrameSizeInBytes(),
1250 reg,
1251 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001252 os << " v" << reg << "[sp + #" << offset << "]";
1253 }
1254
1255 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1256 if (out_reg == 0) {
1257 os << "\n\touts:";
1258 }
1259
1260 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1261 os << " v" << out_reg << "[sp + #" << offset << "]";
1262 }
1263
1264 os << "\n";
1265 }
1266 }
1267
Ian Rogersb23a7722012-10-09 16:54:26 -07001268 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001269 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001270 size_t i = 0;
1271 while (i < code_item->insns_size_in_code_units_) {
1272 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001273 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1274 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001275 i += instruction->SizeInCodeUnits();
1276 }
1277 }
1278 }
1279
Roland Levillainf2650d12015-05-28 14:53:28 +01001280 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1281 // the optimizing compiler?
1282 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1283 const DexFile::CodeItem* code_item) {
1284 // If the native GC map is null and the Dex `code_item` is not
1285 // null, then this method has been compiled with the optimizing
1286 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001287 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001288 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001289 code_item != nullptr;
1290 }
1291
1292 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1293 // the dextodex compiler?
1294 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1295 const DexFile::CodeItem* code_item) {
1296 // If the quick code is null, the Dex `code_item` is not
1297 // null, and the vmap table is not null, then this method has been compiled
1298 // with the dextodex compiler.
1299 return oat_method.GetQuickCode() == nullptr &&
1300 oat_method.GetVmapTable() != nullptr &&
1301 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001302 }
1303
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001305 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001306 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001307 const DexFile* dex_file,
1308 const DexFile::ClassDef& class_def,
1309 const DexFile::CodeItem* code_item,
1310 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001311 if ((method_access_flags & kAccNative) == 0) {
1312 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001313 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001314 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001315 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001316 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001317 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001318 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001319 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001320 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001321
1322 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001323 }
1324
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001325 // The StackMapsHelper provides the stack maps in the native PC order.
1326 // For identical native PCs, the order from the CodeInfo is preserved.
1327 class StackMapsHelper {
1328 public:
1329 explicit StackMapsHelper(const uint8_t* raw_code_info)
1330 : code_info_(raw_code_info),
1331 encoding_(code_info_.ExtractEncoding()),
1332 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1333 indexes_(),
1334 offset_(static_cast<size_t>(-1)),
1335 stack_map_index_(0u) {
1336 if (number_of_stack_maps_ != 0u) {
1337 // Check if native PCs are ordered.
1338 bool ordered = true;
1339 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1340 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1341 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1342 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1343 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1344 ordered = false;
1345 break;
1346 }
1347 last = current;
1348 }
1349 if (!ordered) {
1350 // Create indirection indexes for access in native PC order. We do not optimize
1351 // for the fact that there can currently be only two separately ordered ranges,
1352 // namely normal stack maps and catch-point stack maps.
1353 indexes_.resize(number_of_stack_maps_);
1354 std::iota(indexes_.begin(), indexes_.end(), 0u);
1355 std::sort(indexes_.begin(),
1356 indexes_.end(),
1357 [this](size_t lhs, size_t rhs) {
1358 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1359 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1360 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1361 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1362 // If the PCs are the same, compare indexes to preserve the original order.
1363 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1364 });
1365 }
1366 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1367 }
1368 }
1369
1370 const CodeInfo& GetCodeInfo() const {
1371 return code_info_;
1372 }
1373
1374 const CodeInfoEncoding& GetEncoding() const {
1375 return encoding_;
1376 }
1377
1378 size_t GetOffset() const {
1379 return offset_;
1380 }
1381
1382 StackMap GetStackMap() const {
1383 return GetStackMapAt(stack_map_index_);
1384 }
1385
1386 void Next() {
1387 ++stack_map_index_;
1388 offset_ = (stack_map_index_ == number_of_stack_maps_)
1389 ? static_cast<size_t>(-1)
1390 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1391 }
1392
1393 private:
1394 StackMap GetStackMapAt(size_t i) const {
1395 if (!indexes_.empty()) {
1396 i = indexes_[i];
1397 }
1398 DCHECK_LT(i, number_of_stack_maps_);
1399 return code_info_.GetStackMapAt(i, encoding_);
1400 }
1401
1402 const CodeInfo code_info_;
1403 const CodeInfoEncoding encoding_;
1404 const size_t number_of_stack_maps_;
1405 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1406 size_t offset_;
1407 size_t stack_map_index_;
1408 };
1409
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001410 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001411 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1412 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001413 const void* quick_code = oat_method.GetQuickCode();
1414
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001415 if (code_size == 0) {
1416 code_size = oat_method.GetQuickCodeSize();
1417 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001418 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001419 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001420 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001421 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1422 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1423 StackMapsHelper helper(oat_method.GetVmapTable());
1424 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1425 size_t offset = 0;
1426 while (offset < code_size) {
1427 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1428 if (offset == helper.GetOffset()) {
1429 ScopedIndentation indent1(vios);
1430 StackMap stack_map = helper.GetStackMap();
1431 DCHECK(stack_map.IsValid());
1432 stack_map.Dump(vios,
1433 helper.GetCodeInfo(),
1434 helper.GetEncoding(),
1435 oat_method.GetCodeOffset(),
1436 code_item->registers_size_);
1437 do {
1438 helper.Next();
1439 // There may be multiple stack maps at a given PC. We display only the first one.
1440 } while (offset == helper.GetOffset());
1441 }
1442 DCHECK_LT(offset, helper.GetOffset());
1443 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001444 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001445 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1446 size_t offset = 0;
1447 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001449 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001450 }
1451 }
1452
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001453 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001454 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001455 const OatDumperOptions& options_;
1456 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001457 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001458 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001459 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001460};
1461
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001462class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001463 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001464 ImageDumper(std::ostream* os,
1465 gc::space::ImageSpace& image_space,
1466 const ImageHeader& image_header,
1467 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001468 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001469 vios_(os),
1470 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001471 image_space_(image_space),
1472 image_header_(image_header),
1473 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001474
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001475 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001476 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001477 std::ostream& indent_os = vios_.Stream();
1478
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001480
Jeff Haodcdc85b2015-12-04 14:06:18 -08001481 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1482
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001484
Mathieu Chartiere401d142015-04-22 13:56:20 -07001485 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1486
1487 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1488 auto section = static_cast<ImageHeader::ImageSections>(i);
1489 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1490 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001491
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001492 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001493
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001494 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001495
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001496 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1497
1498 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1499
1500 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001501
Alex Lighta59dd802014-07-02 16:28:08 -07001502 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1503
Igor Murashkin46774762014-10-22 11:37:02 -07001504 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1505
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 {
1507 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001508 static_assert(arraysize(image_roots_descriptions_) ==
1509 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001510 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1511 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001512 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1513 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001514 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001515 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001516 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001517 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001518 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001519 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001520 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1521 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001522 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001523 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1524 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001525 run++;
1526 } else {
1527 break;
1528 }
1529 }
1530 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001531 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001532 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001533 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001534 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001535 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001536 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001537 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001538 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001539 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001540 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001541 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001542 }
1543 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001544 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001545
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001546 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001547 os << "METHOD ROOTS\n";
1548 static_assert(arraysize(image_methods_descriptions_) ==
1549 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1550 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1551 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1552 const char* description = image_methods_descriptions_[i];
1553 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001554 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001555 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001556 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001557 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001558
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001559 Runtime* const runtime = Runtime::Current();
1560 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001561 std::string image_filename = image_space_.GetImageFilename();
1562 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001563 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001565 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001566 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001567 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001568 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1569 }
1570 if (oat_file == nullptr) {
1571 oat_file = OatFile::Open(oat_location,
1572 oat_location,
1573 nullptr,
1574 nullptr,
1575 false,
1576 /*low_4gb*/false,
1577 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001578 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001579 }
1580 if (oat_file == nullptr) {
1581 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1582 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001583 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001585
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001586 stats_.oat_file_bytes = oat_file->Size();
1587
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001588 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001589
Mathieu Chartier02e25112013-08-14 16:14:24 -07001590 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001591 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001592 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1593 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001594 }
1595
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001596 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001597
Jeff Haodcdc85b2015-12-04 14:06:18 -08001598 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001599 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001600 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001601 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001602 {
1603 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1604 heap->FlushAllocStack();
1605 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001606 // Since FlushAllocStack() above resets the (active) allocation
1607 // stack. Need to revoke the thread-local allocation stacks that
1608 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001609 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001610 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001611 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001612 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001615 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001616 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001617 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001618 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001619 ObjPtr<mirror::DexCache> dex_cache =
1620 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001621 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001622 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001623 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001624 }
1625 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001627 // Dump the normal objects before ArtMethods.
1628 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1629 indent_os << "\n";
1630 // TODO: Dump fields.
1631 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001632 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001633 image_header_.VisitPackedArtMethods(&visitor,
1634 image_space_.Begin(),
1635 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001637 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001639 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001641 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001642 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1643 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001644 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001645 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001646 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001647 // If the image is compressed, adjust to decompressed size.
1648 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1649 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1650 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1651 }
1652 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001653 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001655 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001656 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1657 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001658 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1659 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001660 const auto& intern_section = image_header_.GetImageSection(
1661 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001662 const auto& class_table_section = image_header_.GetImageSection(
1663 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001664 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1665
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001666 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001667
1668 // Objects are kObjectAlignment-aligned.
1669 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1670 if (object_section.Offset() > header_bytes) {
1671 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1672 }
1673
1674 // Field section is 4-byte aligned.
1675 constexpr size_t kFieldSectionAlignment = 4U;
1676 uint32_t end_objects = object_section.Offset() + object_section.Size();
1677 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1678 stats_.alignment_bytes += field_section.Offset() - end_objects;
1679
1680 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1681 uint32_t end_fields = field_section.Offset() + field_section.Size();
1682 CHECK_ALIGNED(method_section.Offset(), 4);
1683 stats_.alignment_bytes += method_section.Offset() - end_fields;
1684
1685 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1686 uint32_t end_methods = method_section.Offset() + method_section.Size();
1687 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1688 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1689
1690 // Intern table is 8-byte aligned.
1691 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1692 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1693 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1694
1695 // Add space between intern table and class table.
1696 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1697 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1698
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001699 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1700 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001701 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001702 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001703
Mathieu Chartiere401d142015-04-22 13:56:20 -07001704 stats_.bitmap_bytes += bitmap_section.Size();
1705 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001706 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001707 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001708 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001709 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001710 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001713 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001714
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001715 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001716 }
1717
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001718 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001719 class DumpArtMethodVisitor : public ArtMethodVisitor {
1720 public:
1721 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1722
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001723 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001724 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001725 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001726 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001727 indent_os << "\n";
1728 }
1729
1730 private:
1731 ImageDumper* const image_dumper_;
1732 };
1733
Mathieu Chartier3398c782016-09-30 10:27:43 -07001734 static void PrettyObjectValue(std::ostream& os,
1735 ObjPtr<mirror::Class> type,
1736 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001737 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001738 CHECK(type != nullptr);
1739 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001740 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001741 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001742 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001743 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001744 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001745 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001746 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001747 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001748 } else {
David Sehr709b0702016-10-13 09:12:37 -07001749 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001750 }
1751 }
1752
Mathieu Chartier3398c782016-09-30 10:27:43 -07001753 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001754 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001755 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001756 switch (field->GetTypeAsPrimitiveType()) {
1757 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001758 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001759 break;
1760 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001761 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001762 break;
1763 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001764 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 break;
1766 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001768 break;
1769 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001770 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001771 break;
1772 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001773 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001774 break;
1775 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001776 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1777 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001778 break;
1779 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001780 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001781 break;
1782 case Primitive::kPrimNot: {
1783 // Get the value, don't compute the type unless it is non-null as we don't want
1784 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001785 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001786 if (value == nullptr) {
1787 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001788 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001789 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001790 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001791 if (field_type != nullptr) {
1792 PrettyObjectValue(os, field_type, value);
1793 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001794 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001795 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001796 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1797 }
Ian Rogers50239c72013-06-17 14:53:22 -07001798 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001799 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001800 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001801 default:
1802 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1803 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001804 }
1805 }
1806
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001807 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001808 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001809 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001810 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001811 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001812 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001813 for (ArtField& field : klass->GetIFields()) {
1814 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001815 }
1816 }
1817
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001818 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001819 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001820 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001821
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001822 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001823 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001824 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001825 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001826 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001827 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001828 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001829 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001830 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001831 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001832 }
1833
Mathieu Chartiere401d142015-04-22 13:56:20 -07001834 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001835 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001836 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1837 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001838 return 0;
1839 }
1840 return oat_code_begin[-1];
1841 }
1842
Mathieu Chartiere401d142015-04-22 13:56:20 -07001843 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001844 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001845 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001846 if (oat_code_begin == nullptr) {
1847 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001848 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001849 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001850 }
1851
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001852 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001853 DCHECK(obj != nullptr);
1854 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001855 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001856 if (!state->InDumpSpace(obj)) {
1857 return;
1858 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001859
1860 size_t object_bytes = obj->SizeOf();
1861 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1862 state->stats_.object_bytes += object_bytes;
1863 state->stats_.alignment_bytes += alignment_bytes;
1864
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001865 std::ostream& os = state->vios_.Stream();
1866
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001867 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001868 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001869 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001870 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001871 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001872 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001873 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
1874 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001876 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001878 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001879 } else {
David Sehr709b0702016-10-13 09:12:37 -07001880 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001881 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001882 ScopedIndentation indent1(&state->vios_);
1883 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001884 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001885 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1887 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001888 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001889 size_t run = 0;
1890 for (int32_t j = i + 1; j < length; j++) {
1891 if (value == obj_array->Get(j)) {
1892 run++;
1893 } else {
1894 break;
1895 }
1896 }
1897 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001898 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001899 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001900 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001901 i = i + run;
1902 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001903 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001904 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001905 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001906 }
1907 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001908 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001909 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001910 os << "STATICS:\n";
1911 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001912 for (ArtField& field : klass->GetSFields()) {
1913 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001914 }
1915 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001916 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001917 auto it = state->dex_caches_.find(obj);
1918 if (it != state->dex_caches_.end()) {
1919 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001920 const auto& field_section = state->image_header_.GetImageSection(
1921 ImageHeader::kSectionArtFields);
1922 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001923 size_t num_methods = dex_cache->NumResolvedMethods();
1924 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001925 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001926 ScopedIndentation indent2(&state->vios_);
1927 auto* resolved_methods = dex_cache->GetResolvedMethods();
1928 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001929 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1930 i,
1931 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001932 size_t run = 0;
1933 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001934 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1935 j,
1936 image_pointer_size);
1937 ++j) {
1938 ++run;
1939 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001940 if (run == 0) {
1941 os << StringPrintf("%zd: ", i);
1942 } else {
1943 os << StringPrintf("%zd to %zd: ", i, i + run);
1944 i = i + run;
1945 }
1946 std::string msg;
1947 if (elem == nullptr) {
1948 msg = "null";
1949 } else if (method_section.Contains(
1950 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001951 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01001952 } else {
1953 msg = "<not in method section>";
1954 }
1955 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001956 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001957 }
1958 size_t num_fields = dex_cache->NumResolvedFields();
1959 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001960 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001961 ScopedIndentation indent2(&state->vios_);
1962 auto* resolved_fields = dex_cache->GetResolvedFields();
1963 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001964 auto* elem = mirror::DexCache::GetElementPtrSize(
1965 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001966 size_t run = 0;
1967 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001968 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1969 j,
1970 image_pointer_size);
1971 ++j) {
1972 ++run;
1973 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001974 if (run == 0) {
1975 os << StringPrintf("%zd: ", i);
1976 } else {
1977 os << StringPrintf("%zd to %zd: ", i, i + run);
1978 i = i + run;
1979 }
1980 std::string msg;
1981 if (elem == nullptr) {
1982 msg = "null";
1983 } else if (field_section.Contains(
1984 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001985 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01001986 } else {
1987 msg = "<not in field section>";
1988 }
1989 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001990 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001991 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001992 size_t num_types = dex_cache->NumResolvedTypes();
1993 if (num_types != 0u) {
1994 os << "Types (size=" << num_types << "):\n";
1995 ScopedIndentation indent2(&state->vios_);
1996 auto* resolved_types = dex_cache->GetResolvedTypes();
1997 for (size_t i = 0; i < num_types; ++i) {
1998 auto* elem = resolved_types[i].Read();
1999 size_t run = 0;
2000 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
2001 ++run;
2002 }
2003 if (run == 0) {
2004 os << StringPrintf("%zd: ", i);
2005 } else {
2006 os << StringPrintf("%zd to %zd: ", i, i + run);
2007 i = i + run;
2008 }
2009 std::string msg;
2010 if (elem == nullptr) {
2011 msg = "null";
2012 } else {
David Sehr709b0702016-10-13 09:12:37 -07002013 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002014 }
2015 os << StringPrintf("%p %s\n", elem, msg.c_str());
2016 }
2017 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002018 }
2019 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002020 std::string temp;
2021 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002022 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002023
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002024 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002025 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002027 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2028 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002029 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002030 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2031 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002033 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002034 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2035 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002037 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002038 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002039 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2040 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002041 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002042 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002043 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002044 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002045 } else if (method->IsRuntimeMethod()) {
2046 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2047 if (table != nullptr) {
2048 indent_os << "IMT conflict table " << table << " method: ";
2049 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002050 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2051 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002052 }
2053 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002054 } else {
2055 const DexFile::CodeItem* code_item = method->GetCodeItem();
2056 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002057 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058
2059 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002060 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002061 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002062 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002063 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002064 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002065 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002066 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002067 }
2068
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002069 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2070 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002071 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002072 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002073 if (method->IsConstructor()) {
2074 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002075 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002077 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002078 }
2079 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002080 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002081 }
2082 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002083 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002084
Igor Murashkin7617abd2015-07-10 18:27:47 -07002085 uint32_t method_access_flags = method->GetAccessFlags();
2086
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002088 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2089 dex_instruction_bytes,
2090 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002091 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002092
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002093 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002094 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095
2096 double expansion =
2097 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002098 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002099 }
2100 }
2101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002102 std::set<const void*> already_seen_;
2103 // Compute the size of the given data within the oat file and whether this is the first time
2104 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002105 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002106 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002107 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 already_seen_.insert(oat_data);
2109 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002110 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002111 }
2112 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002113 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002114
2115 public:
2116 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002117 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002118 size_t file_bytes;
2119
2120 size_t header_bytes;
2121 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002122 size_t art_field_bytes;
2123 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002124 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002125 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002126 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002127 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002128 size_t alignment_bytes;
2129
2130 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002131 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002132 size_t managed_to_native_code_bytes;
2133 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002134 size_t class_initializer_code_bytes;
2135 size_t large_initializer_code_bytes;
2136 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002137
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002138 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002139
2140 size_t dex_instruction_bytes;
2141
Mathieu Chartiere401d142015-04-22 13:56:20 -07002142 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002143 std::vector<size_t> method_outlier_size;
2144 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002145 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002146
Roland Levillain3887c462015-08-12 18:15:42 +01002147 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148 : oat_file_bytes(0),
2149 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002150 header_bytes(0),
2151 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002152 art_field_bytes(0),
2153 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002154 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002155 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002156 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002157 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002158 alignment_bytes(0),
2159 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002160 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002161 managed_to_native_code_bytes(0),
2162 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002163 class_initializer_code_bytes(0),
2164 large_initializer_code_bytes(0),
2165 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002166 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002167 dex_instruction_bytes(0) {}
2168
Elliott Hughesa0e18062012-04-13 15:59:59 -07002169 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002170 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002171 size_t bytes;
2172 size_t count;
2173 };
2174 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2175 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002176
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002177 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002178 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2179 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002180 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002181 it->second.count += 1;
2182 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002183 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002184 }
2185 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002186
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002187 double PercentOfOatBytes(size_t size) {
2188 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2189 }
2190
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002191 double PercentOfFileBytes(size_t size) {
2192 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2193 }
2194
2195 double PercentOfObjectBytes(size_t size) {
2196 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2197 }
2198
Mathieu Chartiere401d142015-04-22 13:56:20 -07002199 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002200 method_outlier_size.push_back(total_size);
2201 method_outlier_expansion.push_back(expansion);
2202 method_outlier.push_back(method);
2203 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002204
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002205 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002206 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002207 size_t sum_of_sizes = 0;
2208 size_t sum_of_sizes_squared = 0;
2209 size_t sum_of_expansion = 0;
2210 size_t sum_of_expansion_squared = 0;
2211 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002212 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002213 return;
2214 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002215 for (size_t i = 0; i < n; i++) {
2216 size_t cur_size = method_outlier_size[i];
2217 sum_of_sizes += cur_size;
2218 sum_of_sizes_squared += cur_size * cur_size;
2219 double cur_expansion = method_outlier_expansion[i];
2220 sum_of_expansion += cur_expansion;
2221 sum_of_expansion_squared += cur_expansion * cur_expansion;
2222 }
2223 size_t size_mean = sum_of_sizes / n;
2224 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2225 double expansion_mean = sum_of_expansion / n;
2226 double expansion_variance =
2227 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2228
2229 // Dump methods whose size is a certain number of standard deviations from the mean
2230 size_t dumped_values = 0;
2231 size_t skipped_values = 0;
2232 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2233 size_t cur_size_variance = i * i * size_variance;
2234 bool first = true;
2235 for (size_t j = 0; j < n; j++) {
2236 size_t cur_size = method_outlier_size[j];
2237 if (cur_size > size_mean) {
2238 size_t cur_var = cur_size - size_mean;
2239 cur_var = cur_var * cur_var;
2240 if (cur_var > cur_size_variance) {
2241 if (dumped_values > 20) {
2242 if (i == 1) {
2243 skipped_values++;
2244 } else {
2245 i = 2; // jump to counting for 1 standard deviation
2246 break;
2247 }
2248 } else {
2249 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002250 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002251 first = false;
2252 }
David Sehr709b0702016-10-13 09:12:37 -07002253 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002254 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002255 method_outlier_size[j] = 0; // don't consider this method again
2256 dumped_values++;
2257 }
2258 }
2259 }
2260 }
2261 }
2262 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002264 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002265 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002266 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002267
2268 // Dump methods whose expansion is a certain number of standard deviations from the mean
2269 dumped_values = 0;
2270 skipped_values = 0;
2271 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2272 double cur_expansion_variance = i * i * expansion_variance;
2273 bool first = true;
2274 for (size_t j = 0; j < n; j++) {
2275 double cur_expansion = method_outlier_expansion[j];
2276 if (cur_expansion > expansion_mean) {
2277 size_t cur_var = cur_expansion - expansion_mean;
2278 cur_var = cur_var * cur_var;
2279 if (cur_var > cur_expansion_variance) {
2280 if (dumped_values > 20) {
2281 if (i == 1) {
2282 skipped_values++;
2283 } else {
2284 i = 2; // jump to counting for 1 standard deviation
2285 break;
2286 }
2287 } else {
2288 if (first) {
2289 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002290 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002291 first = false;
2292 }
David Sehr709b0702016-10-13 09:12:37 -07002293 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002294 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002295 method_outlier_expansion[j] = 0.0; // don't consider this method again
2296 dumped_values++;
2297 }
2298 }
2299 }
2300 }
2301 }
2302 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002304 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002305 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002306 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002307 }
2308
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002309 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002310 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 {
2312 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2313 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002314 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2315 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2316 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2317 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2318 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2319 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002320 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002321 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2322 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 header_bytes, PercentOfFileBytes(header_bytes),
2324 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002325 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2326 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002327 dex_cache_arrays_bytes,
2328 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002329 interned_strings_bytes,
2330 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002331 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002332 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002333 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2334 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002335 CHECK_EQ(file_bytes,
2336 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2337 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2338 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002340
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002342 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002343 for (const auto& sizes_and_count : sizes_and_counts) {
2344 const std::string& descriptor(sizes_and_count.first);
2345 double average = static_cast<double>(sizes_and_count.second.bytes) /
2346 static_cast<double>(sizes_and_count.second.count);
2347 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002349 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002350 descriptor.c_str(), sizes_and_count.second.bytes,
2351 sizes_and_count.second.count, average, percent);
2352 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002353 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002354 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002355 CHECK_EQ(object_bytes, object_bytes_total);
2356
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002357 os << StringPrintf("oat_file_bytes = %8zd\n"
2358 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2359 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2360 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2361 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2362 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2363 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002364 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002365 managed_code_bytes,
2366 PercentOfOatBytes(managed_code_bytes),
2367 managed_to_native_code_bytes,
2368 PercentOfOatBytes(managed_to_native_code_bytes),
2369 native_to_managed_code_bytes,
2370 PercentOfOatBytes(native_to_managed_code_bytes),
2371 class_initializer_code_bytes,
2372 PercentOfOatBytes(class_initializer_code_bytes),
2373 large_initializer_code_bytes,
2374 PercentOfOatBytes(large_initializer_code_bytes),
2375 large_method_code_bytes,
2376 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002378 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002379 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002380 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2381 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002382 }
2383
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002384 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002385 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002386 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002387
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002388 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2389 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002390 static_cast<double>(managed_code_bytes) /
2391 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002392 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002393 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002394 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002395
2396 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002397 }
2398 } stats_;
2399
2400 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002401 enum {
2402 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2403 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2404 kLargeConstructorDexBytes = 4000,
2405 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2406 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2407 kLargeMethodDexBytes = 16000
2408 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002409
2410 // For performance, use the *os_ directly for anything that doesn't need indentation
2411 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002412 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002413 VariableIndentationOutputStream vios_;
2414 ScopedIndentation indent1_;
2415
Ian Rogers1d54e732013-05-02 21:10:01 -07002416 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002417 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002418 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002419 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002420 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002421
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002422 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002423};
2424
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002425static int DumpImage(gc::space::ImageSpace* image_space,
2426 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002427 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002428 const ImageHeader& image_header = image_space->GetImageHeader();
2429 if (!image_header.IsValid()) {
2430 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2431 return EXIT_FAILURE;
2432 }
2433 ImageDumper image_dumper(os, *image_space, image_header, options);
2434 if (!image_dumper.Dump()) {
2435 return EXIT_FAILURE;
2436 }
2437 return EXIT_SUCCESS;
2438}
2439
2440static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002441 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002442 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002443 options->class_loader_ = &null_class_loader;
2444
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002445 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002446 if (options->app_image_ != nullptr) {
2447 if (options->app_oat_ == nullptr) {
2448 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002449 return EXIT_FAILURE;
2450 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002451 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2452 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2453 // pointers into 32 bit pointer sized ArtMethods.
2454 std::string error_msg;
2455 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2456 options->app_oat_,
2457 nullptr,
2458 nullptr,
2459 false,
2460 /*low_4gb*/true,
2461 nullptr,
2462 &error_msg));
2463 if (oat_file == nullptr) {
2464 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002465 return EXIT_FAILURE;
2466 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002467 std::unique_ptr<gc::space::ImageSpace> space(
2468 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2469 if (space == nullptr) {
2470 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2471 << error_msg;
2472 }
2473 // Open dex files for the image.
2474 std::vector<std::unique_ptr<const DexFile>> dex_files;
2475 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2476 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2477 << error_msg;
2478 }
2479 // Dump the actual image.
2480 int result = DumpImage(space.get(), options, os);
2481 if (result != EXIT_SUCCESS) {
2482 return result;
2483 }
2484 // Fall through to dump the boot images.
2485 }
2486
2487 gc::Heap* heap = runtime->GetHeap();
2488 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2489 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2490 int result = DumpImage(image_space, options, os);
2491 if (result != EXIT_SUCCESS) {
2492 return result;
2493 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002494 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002495 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002496}
2497
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002498static jobject InstallOatFile(Runtime* runtime,
2499 std::unique_ptr<OatFile> oat_file,
2500 std::vector<const DexFile*>* class_path)
2501 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002502 Thread* self = Thread::Current();
2503 CHECK(self != nullptr);
2504 // Need well-known-classes.
2505 WellKnownClasses::Init(self->GetJniEnv());
2506
2507 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002508 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002509 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002510 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2511 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002513 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002514 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002515 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002516 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517 }
2518
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002519 // Need a class loader. Fake that we're a compiler.
2520 // Note: this will run initializers through the unstarted runtime, so make sure it's
2521 // initialized.
2522 interpreter::UnstartedRuntime::Initialize();
2523
2524 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2525
2526 return class_loader;
2527}
2528
2529static int DumpOatWithRuntime(Runtime* runtime,
2530 std::unique_ptr<OatFile> oat_file,
2531 OatDumperOptions* options,
2532 std::ostream* os) {
2533 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2534 ScopedObjectAccess soa(Thread::Current());
2535
2536 OatFile* oat_file_ptr = oat_file.get();
2537 std::vector<const DexFile*> class_path;
2538 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002539
2540 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002541 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002542 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002543 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002544 options->class_loader_ = &loader_handle;
2545
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002546 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002547 bool success = oat_dumper.Dump(*os);
2548 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2549}
2550
2551static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002552 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002553 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002554 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002555 options->class_loader_ = &null_class_loader;
2556
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002557 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002558 bool success = oat_dumper.Dump(*os);
2559 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2560}
2561
2562static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2563 std::ostream* os) {
2564 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002565 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2566 oat_filename,
2567 nullptr,
2568 nullptr,
2569 false,
2570 /*low_4gb*/false,
2571 nullptr,
2572 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002573 if (oat_file == nullptr) {
2574 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2575 return EXIT_FAILURE;
2576 }
2577
2578 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002579 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002580 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002581 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002582 }
2583}
2584
David Srbecky2fdd03c2016-03-10 15:32:37 +00002585static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002586 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002587 OatFile* oat_file = OatFile::Open(oat_filename,
2588 oat_filename,
2589 nullptr,
2590 nullptr,
2591 false,
2592 /*low_4gb*/false,
2593 nullptr,
2594 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002595 if (oat_file == nullptr) {
2596 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2597 return EXIT_FAILURE;
2598 }
2599
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002600 bool result;
2601 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2602 // files for 64-bit code in the past.
2603 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002604 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002605 result = oat_symbolizer.Symbolize();
2606 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002607 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002608 result = oat_symbolizer.Symbolize();
2609 }
2610 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002611 fprintf(stderr, "Failed to symbolize\n");
2612 return EXIT_FAILURE;
2613 }
2614
2615 return EXIT_SUCCESS;
2616}
2617
Andreas Gampe9fded872016-09-25 16:08:35 -07002618class IMTDumper {
2619 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002620 static bool Dump(Runtime* runtime,
2621 const std::string& imt_file,
2622 bool dump_imt_stats,
2623 const char* oat_filename) {
2624 Thread* self = Thread::Current();
2625
2626 ScopedObjectAccess soa(self);
2627 StackHandleScope<1> scope(self);
2628 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2629 std::vector<const DexFile*> class_path;
2630
2631 if (oat_filename != nullptr) {
2632 std::string error_msg;
2633 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2634 oat_filename,
2635 nullptr,
2636 nullptr,
2637 false,
2638 /*low_4gb*/false,
2639 nullptr,
2640 &error_msg));
2641 if (oat_file == nullptr) {
2642 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2643 return false;
2644 }
2645
2646 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2647 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2648 } else {
2649 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2650 class_path = runtime->GetClassLinker()->GetBootClassPath();
2651 }
2652
2653 if (!imt_file.empty()) {
2654 return DumpImt(runtime, imt_file, class_loader);
2655 }
2656
2657 if (dump_imt_stats) {
2658 return DumpImtStats(runtime, class_path, class_loader);
2659 }
2660
2661 LOG(FATAL) << "Should not reach here";
2662 UNREACHABLE();
2663 }
2664
2665 private:
2666 static bool DumpImt(Runtime* runtime,
2667 const std::string& imt_file,
2668 Handle<mirror::ClassLoader> h_class_loader)
2669 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002670 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2671 std::unordered_set<std::string> prepared;
2672
2673 for (const std::string& line : lines) {
2674 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2675 // or a class descriptor and an interface method, in which case we will lookup the method,
2676 // determine its IMT slot, and check the class' IMT.
2677 size_t first_space = line.find(' ');
2678 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002679 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002680 } else {
2681 DumpIMTForMethod(runtime,
2682 line.substr(0, first_space),
2683 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002684 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002685 &prepared);
2686 }
2687 std::cerr << std::endl;
2688 }
2689
2690 return true;
2691 }
2692
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002693 static bool DumpImtStats(Runtime* runtime,
2694 const std::vector<const DexFile*>& dex_files,
2695 Handle<mirror::ClassLoader> h_class_loader)
2696 REQUIRES_SHARED(Locks::mutator_lock_) {
2697 size_t without_imt = 0;
2698 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002699 std::map<size_t, size_t> histogram;
2700
2701 ClassLinker* class_linker = runtime->GetClassLinker();
2702 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2703 std::unordered_set<std::string> prepared;
2704
2705 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002706 StackHandleScope<1> scope(self);
2707 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2708
2709 for (const DexFile* dex_file : dex_files) {
2710 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002711 class_def_index != dex_file->NumClassDefs();
2712 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002713 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2714 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002715 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002716 if (h_klass.Get() == nullptr) {
2717 std::cerr << "Warning: could not load " << descriptor << std::endl;
2718 continue;
2719 }
2720
2721 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002722 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002723 continue;
2724 }
2725
2726 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2727 if (im_table == nullptr) {
2728 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002729 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002730 continue;
2731 }
2732
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002733 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002734 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2735 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2736 if (ptr->IsRuntimeMethod()) {
2737 if (ptr->IsImtUnimplementedMethod()) {
2738 histogram[0]++;
2739 } else {
2740 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2741 histogram[current_table->NumEntries(pointer_size)]++;
2742 }
2743 } else {
2744 histogram[1]++;
2745 }
2746 }
2747 }
2748 }
2749
2750 std::cerr << "IMT stats:"
2751 << std::endl << std::endl;
2752
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002753 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002754 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002755 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002756 << std::endl << std::endl;
2757
2758 double sum_one = 0;
2759 size_t count_one = 0;
2760
2761 std::cerr << " " << "IMT histogram" << std::endl;
2762 for (auto& bucket : histogram) {
2763 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2764 if (bucket.first > 0) {
2765 sum_one += bucket.second * bucket.first;
2766 count_one += bucket.second;
2767 }
2768 }
2769
2770 double count_zero = count_one + histogram[0];
2771 std::cerr << " Stats:" << std::endl;
2772 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2773 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2774
2775 return true;
2776 }
2777
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002778 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002779 static bool HasNoIMT(Runtime* runtime,
2780 Handle<mirror::Class> klass,
2781 const PointerSize pointer_size,
2782 std::unordered_set<std::string>* prepared)
2783 REQUIRES_SHARED(Locks::mutator_lock_) {
2784 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2785 return true;
2786 }
2787
2788 if (klass->GetImt(pointer_size) == nullptr) {
2789 PrepareClass(runtime, klass, prepared);
2790 }
2791
2792 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2793 DCHECK(object_class->IsObjectClass());
2794
2795 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2796
Mathieu Chartier6beced42016-11-15 15:51:31 -08002797 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002798 DCHECK(result);
2799 }
2800
2801 return result;
2802 }
2803
2804 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2805 REQUIRES_SHARED(Locks::mutator_lock_) {
2806 if (table == nullptr) {
2807 std::cerr << " <No IMT?>" << std::endl;
2808 return;
2809 }
2810 size_t table_index = 0;
2811 for (;;) {
2812 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2813 if (ptr == nullptr) {
2814 return;
2815 }
2816 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002817 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002818 }
2819 }
2820
2821 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2822 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002823 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002824 const std::string& class_name,
2825 const PointerSize pointer_size,
2826 mirror::Class** klass_out,
2827 std::unordered_set<std::string>* prepared)
2828 REQUIRES_SHARED(Locks::mutator_lock_) {
2829 if (class_name.empty()) {
2830 return nullptr;
2831 }
2832
2833 std::string descriptor;
2834 if (class_name[0] == 'L') {
2835 descriptor = class_name;
2836 } else {
2837 descriptor = DotToDescriptor(class_name.c_str());
2838 }
2839
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002840 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002841
2842 if (klass == nullptr) {
2843 self->ClearException();
2844 std::cerr << "Did not find " << class_name << std::endl;
2845 *klass_out = nullptr;
2846 return nullptr;
2847 }
2848
2849 StackHandleScope<1> scope(Thread::Current());
2850 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2851
2852 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2853 *klass_out = h_klass.Get();
2854 return ret;
2855 }
2856
2857 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2858 Handle<mirror::Class> h_klass,
2859 const PointerSize pointer_size,
2860 std::unordered_set<std::string>* prepared)
2861 REQUIRES_SHARED(Locks::mutator_lock_) {
2862 PrepareClass(runtime, h_klass, prepared);
2863 return h_klass->GetImt(pointer_size);
2864 }
2865
2866 static void DumpIMTForClass(Runtime* runtime,
2867 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002868 Handle<mirror::ClassLoader> h_loader,
2869 std::unordered_set<std::string>* prepared)
2870 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002871 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2872 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002873 ImTable* imt = PrepareAndGetImTable(runtime,
2874 Thread::Current(),
2875 h_loader,
2876 class_name,
2877 pointer_size,
2878 &klass,
2879 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002880 if (imt == nullptr) {
2881 return;
2882 }
2883
2884 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2885 for (size_t index = 0; index < ImTable::kSize; ++index) {
2886 std::cerr << " " << index << ":" << std::endl;
2887 ArtMethod* ptr = imt->Get(index, pointer_size);
2888 if (ptr->IsRuntimeMethod()) {
2889 if (ptr->IsImtUnimplementedMethod()) {
2890 std::cerr << " <empty>" << std::endl;
2891 } else {
2892 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2893 PrintTable(current_table, pointer_size);
2894 }
2895 } else {
David Sehr709b0702016-10-13 09:12:37 -07002896 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002897 }
2898 }
2899
2900 std::cerr << " Interfaces:" << std::endl;
2901 // Run through iftable, find methods that slot here, see if they fit.
2902 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002903 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2904 mirror::Class* iface = if_table->GetInterface(i);
2905 std::string iface_name;
2906 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002907
Mathieu Chartier6beced42016-11-15 15:51:31 -08002908 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2909 uint32_t class_hash, name_hash, signature_hash;
2910 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
2911 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2912 std::cerr << " " << iface_method.PrettyMethod(true)
2913 << " slot=" << imt_slot
2914 << std::hex
2915 << " class_hash=0x" << class_hash
2916 << " name_hash=0x" << name_hash
2917 << " signature_hash=0x" << signature_hash
2918 << std::dec
2919 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002920 }
2921 }
2922 }
2923
2924 static void DumpIMTForMethod(Runtime* runtime,
2925 const std::string& class_name,
2926 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002927 Handle<mirror::ClassLoader> h_loader,
2928 std::unordered_set<std::string>* prepared)
2929 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002930 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2931 mirror::Class* klass;
2932 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002933 Thread::Current(),
2934 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002935 class_name,
2936 pointer_size,
2937 &klass,
2938 prepared);
2939 if (imt == nullptr) {
2940 return;
2941 }
2942
2943 std::cerr << class_name << " <" << method << ">" << std::endl;
2944 for (size_t index = 0; index < ImTable::kSize; ++index) {
2945 ArtMethod* ptr = imt->Get(index, pointer_size);
2946 if (ptr->IsRuntimeMethod()) {
2947 if (ptr->IsImtUnimplementedMethod()) {
2948 continue;
2949 }
2950
2951 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2952 if (current_table == nullptr) {
2953 continue;
2954 }
2955
2956 size_t table_index = 0;
2957 for (;;) {
2958 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2959 if (ptr2 == nullptr) {
2960 break;
2961 }
2962 table_index++;
2963
David Sehr709b0702016-10-13 09:12:37 -07002964 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002965 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002966 std::cerr << " Slot "
2967 << index
2968 << " ("
2969 << current_table->NumEntries(pointer_size)
2970 << ")"
2971 << std::endl;
2972 PrintTable(current_table, pointer_size);
2973 return;
2974 }
2975 }
2976 } else {
David Sehr709b0702016-10-13 09:12:37 -07002977 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002978 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002979 std::cerr << " Slot " << index << " (1)" << std::endl;
2980 std::cerr << " " << p_name << std::endl;
2981 } else {
2982 // Run through iftable, find methods that slot here, see if they fit.
2983 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002984 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2985 mirror::Class* iface = if_table->GetInterface(i);
2986 size_t num_methods = iface->NumDeclaredVirtualMethods();
2987 if (num_methods > 0) {
2988 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2989 if (ImTable::GetImtIndex(&iface_method) == index) {
2990 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002991 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08002992 std::cerr << " Slot " << index << " (1)" << std::endl;
2993 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002994 }
2995 }
2996 }
2997 }
2998 }
2999 }
3000 }
3001 }
3002 }
3003
3004 // Read lines from the given stream, dropping comments and empty lines
3005 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3006 std::vector<std::string> output;
3007 while (in_stream.good()) {
3008 std::string dot;
3009 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003010 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003011 continue;
3012 }
3013 output.push_back(dot);
3014 }
3015 return output;
3016 }
3017
3018 // Read lines from the given file, dropping comments and empty lines.
3019 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3020 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3021 if (input_file.get() == nullptr) {
3022 LOG(ERROR) << "Failed to open input file " << input_filename;
3023 return std::vector<std::string>();
3024 }
3025 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3026 input_file->close();
3027 return result;
3028 }
3029
3030 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3031 // and note in the given set that the work was done.
3032 static void PrepareClass(Runtime* runtime,
3033 Handle<mirror::Class> h_klass,
3034 std::unordered_set<std::string>* done)
3035 REQUIRES_SHARED(Locks::mutator_lock_) {
3036 if (!h_klass->ShouldHaveImt()) {
3037 return;
3038 }
3039
3040 std::string name;
3041 name = h_klass->GetDescriptor(&name);
3042
3043 if (done->find(name) != done->end()) {
3044 return;
3045 }
3046 done->insert(name);
3047
3048 if (h_klass->HasSuperClass()) {
3049 StackHandleScope<1> h(Thread::Current());
3050 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3051 }
3052
3053 if (!h_klass->IsTemp()) {
3054 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3055 }
3056 }
3057};
3058
Igor Murashkin37743352014-11-13 14:38:00 -08003059struct OatdumpArgs : public CmdlineArgs {
3060 protected:
3061 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003062
Igor Murashkin37743352014-11-13 14:38:00 -08003063 virtual ParseStatus ParseCustom(const StringPiece& option,
3064 std::string* error_msg) OVERRIDE {
3065 {
3066 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3067 if (base_parse != kParseUnknownArgument) {
3068 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003069 }
3070 }
3071
Igor Murashkin37743352014-11-13 14:38:00 -08003072 if (option.starts_with("--oat-file=")) {
3073 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3074 } else if (option.starts_with("--image=")) {
3075 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003076 } else if (option == "--no-dump:vmap") {
3077 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003078 } else if (option =="--dump:code_info_stack_maps") {
3079 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003080 } else if (option == "--no-disassemble") {
3081 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003082 } else if (option =="--header-only") {
3083 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003084 } else if (option.starts_with("--symbolize=")) {
3085 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3086 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003087 } else if (option.starts_with("--only-keep-debug")) {
3088 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003089 } else if (option.starts_with("--class-filter=")) {
3090 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003091 } else if (option.starts_with("--method-filter=")) {
3092 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003093 } else if (option.starts_with("--list-classes")) {
3094 list_classes_ = true;
3095 } else if (option.starts_with("--list-methods")) {
3096 list_methods_ = true;
3097 } else if (option.starts_with("--export-dex-to=")) {
3098 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3099 } else if (option.starts_with("--addr2instr=")) {
3100 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3101 *error_msg = "Address conversion failed";
3102 return kParseError;
3103 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003104 } else if (option.starts_with("--app-image=")) {
3105 app_image_ = option.substr(strlen("--app-image=")).data();
3106 } else if (option.starts_with("--app-oat=")) {
3107 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003108 } else if (option.starts_with("--dump-imt=")) {
3109 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3110 } else if (option == "--dump-imt-stats") {
3111 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003112 } else {
3113 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003114 }
3115
Igor Murashkin37743352014-11-13 14:38:00 -08003116 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003117 }
3118
Igor Murashkin37743352014-11-13 14:38:00 -08003119 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3120 // Infer boot image location from the image location if possible.
3121 if (boot_image_location_ == nullptr) {
3122 boot_image_location_ = image_location_;
3123 }
3124
3125 // Perform the parent checks.
3126 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3127 if (parent_checks != kParseOk) {
3128 return parent_checks;
3129 }
3130
3131 // Perform our own checks.
3132 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3133 *error_msg = "Either --image or --oat-file must be specified";
3134 return kParseError;
3135 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3136 *error_msg = "Either --image or --oat-file must be specified but not both";
3137 return kParseError;
3138 }
3139
3140 return kParseOk;
3141 }
3142
3143 virtual std::string GetUsage() const {
3144 std::string usage;
3145
3146 usage +=
3147 "Usage: oatdump [options] ...\n"
3148 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3149 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3150 "\n"
3151 // Either oat-file or image is required.
3152 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3153 " Example: --oat-file=/system/framework/boot.oat\n"
3154 "\n"
3155 " --image=<file.art>: specifies an input image location.\n"
3156 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003157 "\n"
3158 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3159 " boot image and app oat file.\n"
3160 " Example: --app-image=app.art\n"
3161 "\n"
3162 " --app-oat=<file.odex>: specifies an input app oat.\n"
3163 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003164 "\n";
3165
3166 usage += Base::GetUsage();
3167
3168 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003169 " --no-dump:vmap may be used to disable vmap dumping.\n"
3170 " Example: --no-dump:vmap\n"
3171 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003172 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3173 " Example: --dump:code_info_stack_maps\n"
3174 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003175 " --no-disassemble may be used to disable disassembly.\n"
3176 " Example: --no-disassemble\n"
3177 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003178 " --header-only may be used to print only the oat header.\n"
3179 " Example: --header-only\n"
3180 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003181 " --list-classes may be used to list target file classes (can be used with filters).\n"
3182 " Example: --list-classes\n"
3183 " Example: --list-classes --class-filter=com.example.foo\n"
3184 "\n"
3185 " --list-methods may be used to list target file methods (can be used with filters).\n"
3186 " Example: --list-methods\n"
3187 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3188 "\n"
3189 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3190 " Example: --symbolize=/system/framework/boot.oat\n"
3191 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003192 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3193 " .rodata and .text sections are omitted in the output file to save space.\n"
3194 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3195 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003196 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3197 " Example: --class-filter=com.example.foo\n"
3198 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003199 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3200 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003201 "\n"
3202 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3203 " Example: --export-dex-to=/data/local/tmp\n"
3204 "\n"
3205 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3206 " address (e.g. PC from crash dump)\n"
3207 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003208 "\n"
3209 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3210 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003211 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003212 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003213 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003214 " Example: --dump-imt=imt.txt\n"
3215 "\n"
3216 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3217 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003218 "\n";
3219
3220 return usage;
3221 }
3222
3223 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003224 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003225 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003226 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003227 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003228 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003229 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003230 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003231 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003232 bool disassemble_code_ = true;
3233 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003234 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003235 bool list_classes_ = false;
3236 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003237 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003238 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003239 uint32_t addr2instr_ = 0;
3240 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003241 const char* app_image_ = nullptr;
3242 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003243};
3244
Igor Murashkin37743352014-11-13 14:38:00 -08003245struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3246 virtual bool NeedsRuntime() OVERRIDE {
3247 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003248
Igor Murashkin37743352014-11-13 14:38:00 -08003249 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3250 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3251
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003252 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003253 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003254 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003255 args_->disassemble_code_,
3256 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003257 args_->class_filter_,
3258 args_->method_filter_,
3259 args_->list_classes_,
3260 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003261 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003262 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003263 args_->app_image_,
3264 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003265 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003266
Andreas Gampe9fded872016-09-25 16:08:35 -07003267 return (args_->boot_image_location_ != nullptr ||
3268 args_->image_location_ != nullptr ||
3269 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003270 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003271 }
3272
Igor Murashkin37743352014-11-13 14:38:00 -08003273 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3274 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003275 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003276
Mathieu Chartierd424d082014-10-15 10:31:46 -07003277 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003278
Andreas Gampec24f3992014-12-17 20:40:11 -08003279 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003280 // ELF has special kind of section called SHT_NOBITS which allows us to create
3281 // sections which exist but their data is omitted from the ELF file to save space.
3282 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3283 // with only debug data. We use it in similar way to exclude .rodata and .text.
3284 bool no_bits = args_->only_keep_debug_;
3285 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003286 } else {
3287 return DumpOat(nullptr,
3288 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003289 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003290 args_->os_) == EXIT_SUCCESS;
3291 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003292 }
3293
Igor Murashkin37743352014-11-13 14:38:00 -08003294 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3295 CHECK(args_ != nullptr);
3296
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003297 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3298 return IMTDumper::Dump(runtime,
3299 args_->imt_dump_,
3300 args_->imt_stat_dump_,
3301 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003302 }
3303
Igor Murashkin37743352014-11-13 14:38:00 -08003304 if (args_->oat_filename_ != nullptr) {
3305 return DumpOat(runtime,
3306 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003307 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003308 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003309 }
Igor Murashkin37743352014-11-13 14:38:00 -08003310
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003311 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003312 }
3313
Igor Murashkin37743352014-11-13 14:38:00 -08003314 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3315};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003316
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003317} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003318
3319int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003320 art::OatdumpMain main;
3321 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003322}